source: git/Singular/fegetopt.c @ dcd92d

spielwiese
Last change on this file since dcd92d was ba5e9e, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Changed configure-scripts to generate individual public config files for each package: resources, libpolys, singular (main) fix: sources should include correct corresponding config headers.
  • Property mode set to 100644
File size: 23.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5/* Getopt for GNU.
6   NOTE: getopt is now part of the C library, so if you don't know what
7   "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu
8   before changing it!
9
10   Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94
11        Free Software Foundation, Inc.
12
13   This program is free software; you can redistribute it and/or modify it
14   under the terms of the GNU General Public License as published by the
15   Free Software Foundation; either version 2, or (at your option) any
16   later version.
17
18   This program is distributed in the hope that it will be useful,
19   but WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21   GNU General Public License for more details.
22
23   You should have received a copy of the GNU General Public License
24   along with this program; if not, write to the Free Software
25   Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
26
27/*
28   obachman 9/99: adapted to Singular by
29    * adding prefix fe_ to global variables
30    * extended fe_option structure   
31*/
32
33
34#ifdef HAVE_CONFIG_H
35#include "singularconfig.h"
36#endif /* HAVE_CONFIG_H */
37#include <kernel/mod2.h>
38
39#ifndef __STDC__
40#  ifndef const
41#    define const
42#  endif
43#endif
44
45/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.  */
46#ifndef _NO_PROTO
47#define _NO_PROTO
48#endif
49
50#include <stdio.h>
51/* #include "tailor.h" */
52
53/* Comment out all this code if we are using the GNU C Library, and are not
54   actually compiling the library itself.  This code is part of the GNU C
55   Library, but also included in many other GNU distributions.  Compiling
56   and linking in this code is a waste when using the GNU C library
57   (especially if it is a shared library).  Rather than having every GNU
58   program understand `configure --with-gnu-libc' and omit the object files,
59   it is simpler to just do this in the source for each such file.  */
60
61/* This needs to come after some library #include
62   to get __GNU_LIBRARY__ defined.  */
63#ifdef  __GNU_LIBRARY__
64/* Don't include stdlib.h for non-GNU C libraries because some of them
65   contain conflicting prototypes for getopt.  */
66#include <stdlib.h>
67#endif  /* GNU C library.  */
68
69/* If GETOPT_COMPAT is defined, `+' as well as `--' can introduce a
70   long-named option.  Because this is not POSIX.2 compliant, it is
71   being phased out.  */
72/* #define GETOPT_COMPAT */
73
74/* This version of `getopt' appears to the caller like standard Unix `getopt'
75   but it behaves differently for the user, since it allows the user
76   to intersperse the options with the other arguments.
77
78   As `getopt' works, it permutes the elements of ARGV so that,
79   when it is done, all the options precede everything else.  Thus
80   all application programs are extended to handle flexible argument order.
81
82   Setting the environment variable POSIXLY_CORRECT disables permutation.
83   Then the behavior is completely standard.
84
85   GNU application programs can use a third alternative mode in which
86   they can distinguish the relative order of options and other arguments.  */
87
88#include <Singular/fegetopt.h>
89
90/* For communication from `getopt' to the caller.
91   When `getopt' finds an option that takes an argument,
92   the argument value is returned here.
93   Also, when `ordering' is RETURN_IN_ORDER,
94   each non-option ARGV-element is returned here.  */
95
96char *fe_optarg = 0;
97
98/* Index in ARGV of the next element to be scanned.
99   This is used for communication to and from the caller
100   and for communication between successive calls to `getopt'.
101
102   On entry to `getopt', zero means this is the first call; initialize.
103
104   When `getopt' returns EOF, this is the index of the first of the
105   non-option elements that the caller should itself scan.
106
107   Otherwise, `fe_optind' communicates from one call to the next
108   how much of ARGV has been scanned so far.  */
109
110/* XXX 1003.2 says this must be 1 before any call.  */
111int fe_optind = 0;
112
113/* The next char to be scanned in the option-element
114   in which the last option character we returned was found.
115   This allows us to pick up the scan where we left off.
116
117   If this is zero, or a null string, it means resume the scan
118   by advancing to the next ARGV-element.  */
119
120static char *nextchar;
121
122/* Callers store zero here to inhibit the error message
123   for unrecognized options.  */
124
125int fe_opterr = 1;
126
127/* Set to an option character which was unrecognized.
128   This must be initialized on some systems to avoid linking in the
129   system's own getopt implementation.  */
130
131#define BAD_OPTION '\0'
132int fe_optopt = BAD_OPTION;
133
134/* Describe how to deal with options that follow non-option ARGV-elements.
135
136   If the caller did not specify anything,
137   the default is REQUIRE_ORDER if the environment variable
138   POSIXLY_CORRECT is defined, PERMUTE otherwise.
139
140   REQUIRE_ORDER means don't recognize them as options;
141   stop option processing when the first non-option is seen.
142   This is what Unix does.
143   This mode of operation is selected by either setting the environment
144   variable POSIXLY_CORRECT, or using `+' as the first character
145   of the list of option characters.
146
147   PERMUTE is the default.  We permute the contents of ARGV as we scan,
148   so that eventually all the non-options are at the end.  This allows options
149   to be given in any order, even with programs that were not written to
150   expect this.
151
152   RETURN_IN_ORDER is an option available to programs that were written
153   to expect options and other ARGV-elements in any order and that care about
154   the ordering of the two.  We describe each non-option ARGV-element
155   as if it were the argument of an option with character code 1.
156   Using `-' as the first character of the list of option characters
157   selects this mode of operation.
158
159   The special argument `--' forces an end of option-scanning regardless
160   of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
161   `--' can cause `getopt' to return EOF with `fe_optind' != ARGC.  */
162
163static enum
164{
165  REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
166} ordering;
167
168#ifdef  __GNU_LIBRARY__
169/* We want to avoid inclusion of string.h with non-GNU libraries
170   because there are many ways it can cause trouble.
171   On some systems, it contains special magic macros that don't work
172   in GCC.  */
173#include <string.h>
174#define my_index        strchr
175#define my_strlen       strlen
176#else
177
178/* Avoid depending on library functions or files
179   whose names are inconsistent.  */
180
181#if __STDC__ || defined(PROTO)
182extern char *getenv(const char *name);
183extern int  strcmp (const char *s1, const char *s2);
184/*extern int  strncmp(const char *s1, const char *s2, int n);*/
185
186static int my_strlen(const char *s);
187static const char *my_index (const char *str, int chr);
188#else
189extern char *getenv ();
190#endif
191
192static int my_strlen (const char *str)
193{
194  int n = 0;
195  while (*str++)
196    n++;
197  return n;
198}
199
200static const char * my_index (const char *str, int chr)
201{
202  while (*str)
203    {
204      if (*str == chr)
205        return (const char *) str;
206      str++;
207    }
208  return 0;
209}
210
211#endif                          /* GNU C library.  */
212
213/* Handle permutation of arguments.  */
214
215/* Describe the part of ARGV that contains non-options that have
216   been skipped.  `first_nonopt' is the index in ARGV of the first of them;
217   `last_nonopt' is the index after the last of them.  */
218
219static int first_nonopt;
220static int last_nonopt;
221
222/* Exchange two adjacent subsequences of ARGV.
223   One subsequence is elements [first_nonopt,last_nonopt)
224   which contains all the non-options that have been skipped so far.
225   The other is elements [last_nonopt,fe_optind), which contains all
226   the options processed since those non-options were skipped.
227
228   `first_nonopt' and `last_nonopt' are relocated so that they describe
229   the new indices of the non-options in ARGV after they are moved.
230
231   To perform the swap, we first reverse the order of all elements. So
232   all options now come before all non options, but they are in the
233   wrong order. So we put back the options and non options in original
234   order by reversing them again. For example:
235       original input:      a b c -x -y
236       reverse all:         -y -x c b a
237       reverse options:     -x -y c b a
238       reverse non options: -x -y a b c
239*/
240
241#if __STDC__ || defined(PROTO)
242static void exchange (char **argv);
243#endif
244
245static void exchange (char **argv)
246{
247  char *temp, **first, **last;
248
249  /* Reverse all the elements [first_nonopt, fe_optind) */
250  first = &argv[first_nonopt];
251  last  = &argv[fe_optind-1];
252  while (first < last) {
253    temp = *first; *first = *last; *last = temp; first++; last--;
254  }
255  /* Put back the options in order */
256  first = &argv[first_nonopt];
257  first_nonopt += (fe_optind - last_nonopt);
258  last  = &argv[first_nonopt - 1];
259  while (first < last) {
260    temp = *first; *first = *last; *last = temp; first++; last--;
261  }
262
263  /* Put back the non options in order */
264  first = &argv[first_nonopt];
265  last_nonopt = fe_optind;
266  last  = &argv[last_nonopt-1];
267  while (first < last) {
268    temp = *first; *first = *last; *last = temp; first++; last--;
269  }
270}
271
272/* Scan elements of ARGV (whose length is ARGC) for option characters
273   given in OPTSTRING.
274
275   If an element of ARGV starts with '-', and is not exactly "-" or "--",
276   then it is an option element.  The characters of this element
277   (aside from the initial '-') are option characters.  If `getopt'
278   is called repeatedly, it returns successively each of the option characters
279   from each of the option elements.
280
281   If `getopt' finds another option character, it returns that character,
282   updating `fe_optind' and `nextchar' so that the next call to `getopt' can
283   resume the scan with the following option character or ARGV-element.
284
285   If there are no more option characters, `getopt' returns `EOF'.
286   Then `fe_optind' is the index in ARGV of the first ARGV-element
287   that is not an option.  (The ARGV-elements have been permuted
288   so that those that are not options now come last.)
289
290   OPTSTRING is a string containing the legitimate option characters.
291   If an option character is seen that is not listed in OPTSTRING,
292   return BAD_OPTION after printing an error message.  If you set `fe_opterr' to
293   zero, the error message is suppressed but we still return BAD_OPTION.
294
295   If a char in OPTSTRING is followed by a colon, that means it wants an arg,
296   so the following text in the same ARGV-element, or the text of the following
297   ARGV-element, is returned in `fe_optarg'.  Two colons mean an option that
298   wants an optional arg; if there is text in the current ARGV-element,
299   it is returned in `fe_optarg', otherwise `fe_optarg' is set to zero.
300
301   If OPTSTRING starts with `-' or `+', it requests different methods of
302   handling the non-option ARGV-elements.
303   See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
304
305   Long-named options begin with `--' instead of `-'.
306   Their names may be abbreviated as long as the abbreviation is unique
307   or is an exact match for some defined option.  If they have an
308   argument, it follows the option name in the same ARGV-element, separated
309   from the option name by a `=', or else the in next ARGV-element.
310   When `getopt' finds a long-named option, it returns
311   the value of the option's `val' field.
312
313   The elements of ARGV aren't really const, because we permute them.
314   But we pretend they're const in the prototype to be compatible
315   with other systems.
316
317   LONGOPTS is a vector of `struct fe_option' terminated by an
318   element containing a name which is zero.
319
320   LONGIND returns the index in LONGOPT of the long-named option found.
321   It is only valid when a long-named option has been found by the most
322   recent call.
323
324   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
325   long-named options.  */
326
327int _fe_getopt_internal (
328     int argc,
329     char *const *argv,
330     const char *optstring,
331     const struct fe_option *longopts,
332     int *longind,
333     int long_only)
334{
335  int option_index;
336
337  fe_optarg = 0;
338
339  /* Initialize the internal data when the first call is made.
340     Start processing options with ARGV-element 1 (since ARGV-element 0
341     is the program name); the sequence of previously skipped
342     non-option ARGV-elements is empty.  */
343
344  if (fe_optind == 0)
345    {
346      first_nonopt = last_nonopt = fe_optind = 1;
347
348      nextchar = NULL;
349
350      /* Determine how to handle the ordering of options and nonoptions.  */
351
352      if (optstring[0] == '-')
353        {
354          ordering = RETURN_IN_ORDER;
355          ++optstring;
356        }
357      else if (optstring[0] == '+')
358        {
359          ordering = REQUIRE_ORDER;
360          ++optstring;
361        }
362      else if (getenv ("POSIXLY_CORRECT") != NULL)
363        ordering = REQUIRE_ORDER;
364      else
365        ordering = PERMUTE;
366    }
367
368  if (nextchar == NULL || *nextchar == '\0')
369    {
370      if (ordering == PERMUTE)
371        {
372          /* If we have just processed some options following some non-options,
373             exchange them so that the options come first.  */
374
375          if (first_nonopt != last_nonopt && last_nonopt != fe_optind)
376            exchange ((char **) argv);
377          else if (last_nonopt != fe_optind)
378            first_nonopt = fe_optind;
379
380          /* Now skip any additional non-options
381             and extend the range of non-options previously skipped.  */
382
383          while (fe_optind < argc
384                 && (argv[fe_optind][0] != '-' || argv[fe_optind][1] == '\0')
385#ifdef GETOPT_COMPAT
386                 && (longopts == NULL
387                     || argv[fe_optind][0] != '+' || argv[fe_optind][1] == '\0')
388#endif                          /* GETOPT_COMPAT */
389                 )
390            fe_optind++;
391          last_nonopt = fe_optind;
392        }
393
394      /* Special ARGV-element `--' means premature end of options.
395         Skip it like a null option,
396         then exchange with previous non-options as if it were an option,
397         then skip everything else like a non-option.  */
398
399      if (fe_optind != argc && !strcmp (argv[fe_optind], "--"))
400        {
401          fe_optind++;
402
403          if (first_nonopt != last_nonopt && last_nonopt != fe_optind)
404            exchange ((char **) argv);
405          else if (first_nonopt == last_nonopt)
406            first_nonopt = fe_optind;
407          last_nonopt = argc;
408
409          fe_optind = argc;
410        }
411
412      /* If we have done all the ARGV-elements, stop the scan
413         and back over any non-options that we skipped and permuted.  */
414
415      if (fe_optind == argc)
416        {
417          /* Set the next-arg-index to point at the non-options
418             that we previously skipped, so the caller will digest them.  */
419          if (first_nonopt != last_nonopt)
420            fe_optind = first_nonopt;
421          return EOF;
422        }
423
424      /* If we have come to a non-option and did not permute it,
425         either stop the scan or describe it to the caller and pass it by.  */
426
427      if ((argv[fe_optind][0] != '-' || argv[fe_optind][1] == '\0')
428#ifdef GETOPT_COMPAT
429          && (longopts == NULL
430              || argv[fe_optind][0] != '+' || argv[fe_optind][1] == '\0')
431#endif                          /* GETOPT_COMPAT */
432          )
433        {
434          if (ordering == REQUIRE_ORDER)
435            return EOF;
436          fe_optarg = argv[fe_optind++];
437          return 1;
438        }
439
440      /* We have found another option-ARGV-element.
441         Start decoding its characters.  */
442
443      nextchar = (argv[fe_optind] + 1
444                  + (longopts != NULL && argv[fe_optind][1] == '-'));
445    }
446
447  if (longopts != NULL
448      && ((argv[fe_optind][0] == '-'
449           && (argv[fe_optind][1] == '-' || long_only))
450#ifdef GETOPT_COMPAT
451          || argv[fe_optind][0] == '+'
452#endif                          /* GETOPT_COMPAT */
453          ))
454    {
455      const struct fe_option *p;
456      char *s = nextchar;
457      int exact = 0;
458      int ambig = 0;
459      const struct fe_option *pfound = NULL;
460      int indfound = 0;
461
462      while (*s && *s != '=')
463        s++;
464
465      /* Test all options for either exact match or abbreviated matches.  */
466      for (p = longopts, option_index = 0; p->name;
467           p++, option_index++)
468        if (!strncmp (p->name, nextchar, s - nextchar))
469          {
470            if (s - nextchar == my_strlen (p->name))
471              {
472                /* Exact match found.  */
473                pfound = p;
474                indfound = option_index;
475                exact = 1;
476                break;
477              }
478            else if (pfound == NULL)
479              {
480                /* First nonexact match found.  */
481                pfound = p;
482                indfound = option_index;
483              }
484            else
485              /* Second nonexact match found.  */
486              ambig = 1;
487          }
488
489      if (ambig && !exact)
490        {
491          if (fe_opterr)
492            fprintf (stderr, "%s: option `%s' is ambiguous\n",
493                     argv[0], argv[fe_optind]);
494          nextchar += my_strlen (nextchar);
495          fe_optind++;
496          return BAD_OPTION;
497        }
498
499      if (pfound != NULL)
500        {
501          option_index = indfound;
502          fe_optind++;
503          if (*s)
504            {
505              /* Don't test has_arg with >, because some C compilers don't
506                 allow it to be used on enums.  */
507              if (pfound->has_arg)
508                fe_optarg = s + 1;
509              else
510                {
511                  if (fe_opterr)
512                    {
513                      if (argv[fe_optind - 1][1] == '-')
514                        /* --option */
515                        fprintf (stderr,
516                                 "%s: option `--%s' doesn't allow an argument\n",
517                                 argv[0], pfound->name);
518                      else
519                        /* +option or -option */
520                        fprintf (stderr,
521                             "%s: option `%c%s' doesn't allow an argument\n",
522                             argv[0], argv[fe_optind - 1][0], pfound->name);
523                    }
524                  nextchar += my_strlen (nextchar);
525                  return BAD_OPTION;
526                }
527            }
528          else if (pfound->has_arg == 1)
529            {
530              if (fe_optind < argc)
531                fe_optarg = argv[fe_optind++];
532              else
533                {
534                  if (fe_opterr)
535                    fprintf (stderr, "%s: option `%s' requires an argument\n",
536                             argv[0], argv[fe_optind - 1]);
537                  nextchar += my_strlen (nextchar);
538                  return optstring[0] == ':' ? ':' : BAD_OPTION;
539                }
540            }
541          nextchar += my_strlen (nextchar);
542          if (longind != NULL)
543            *longind = option_index;
544          return pfound->val;
545        }
546      /* Can't find it as a long option.  If this is not getopt_long_only,
547         or the option starts with '--' or is not a valid short
548         option, then it's an error.
549         Otherwise interpret it as a short option.  */
550      if (!long_only || argv[fe_optind][1] == '-'
551#ifdef GETOPT_COMPAT
552          || argv[fe_optind][0] == '+'
553#endif                          /* GETOPT_COMPAT */
554          || my_index (optstring, *nextchar) == NULL)
555        {
556          if (fe_opterr)
557            {
558              if (argv[fe_optind][1] == '-')
559                /* --option */
560                fprintf (stderr, "%s: unrecognized option `--%s'\n",
561                         argv[0], nextchar);
562              else
563                /* +option or -option */
564                fprintf (stderr, "%s: unrecognized option `%c%s'\n",
565                         argv[0], argv[fe_optind][0], nextchar);
566            }
567          nextchar = (char *) "";
568          fe_optind++;
569          return BAD_OPTION;
570        }
571    }
572
573  /* Look at and handle the next option-character.  */
574
575  {
576    char c = *nextchar++;
577    const char *temp = my_index (optstring, c);
578
579    /* Increment `fe_optind' when we start to process its last character.  */
580    if (*nextchar == '\0')
581      ++fe_optind;
582
583    if (temp == NULL || c == ':')
584      {
585        if (fe_opterr)
586          {
587#if 0
588            if (c < 040 || c >= 0177)
589              fprintf (stderr, "%s: unrecognized option, character code 0%o\n",
590                       argv[0], c);
591            else
592              fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c);
593#else
594            /* 1003.2 specifies the format of this message.  */
595            fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
596#endif
597          }
598        fe_optopt = c;
599        return BAD_OPTION;
600      }
601    if (temp[1] == ':')
602      {
603        if (temp[2] == ':')
604          {
605            /* This is an option that accepts an argument optionally.  */
606            if (*nextchar != '\0')
607              {
608                fe_optarg = nextchar;
609                fe_optind++;
610              }
611            else
612              fe_optarg = 0;
613            nextchar = NULL;
614          }
615        else
616          {
617            /* This is an option that requires an argument.  */
618            if (*nextchar != '\0')
619              {
620                fe_optarg = nextchar;
621                /* If we end this ARGV-element by taking the rest as an arg,
622                   we must advance to the next element now.  */
623                fe_optind++;
624              }
625            else if (fe_optind == argc)
626              {
627                if (fe_opterr)
628                  {
629#if 0
630                    fprintf (stderr, "%s: option `-%c' requires an argument\n",
631                             argv[0], c);
632#else
633                    /* 1003.2 specifies the format of this message.  */
634                    fprintf (stderr, "%s: option requires an argument -- %c\n",
635                             argv[0], c);
636#endif
637                  }
638                fe_optopt = c;
639                if (optstring[0] == ':')
640                  c = ':';
641                else
642                  c = BAD_OPTION;
643              }
644            else
645              /* We already incremented `fe_optind' once;
646                 increment it again when taking next ARGV-elt as argument.  */
647              fe_optarg = argv[fe_optind++];
648            nextchar = NULL;
649          }
650      }
651    return c;
652  }
653}
654
655int fe_getopt (
656     int argc,
657     char *const *argv,
658     const char *optstring)
659{
660  return _fe_getopt_internal (argc, argv, optstring,
661                           (const struct fe_option *) 0,
662                           (int *) 0,
663                           0);
664}
665
666int fe_getopt_long (
667     int argc,
668     char *const *argv,
669     const char *options,
670     const struct fe_option *long_options,
671     int *opt_index)
672{
673  return _fe_getopt_internal (argc, argv, options, long_options, opt_index, 0);
674}
675
676int fe_getopt_long_only (
677     int argc,
678     char *const *argv,
679     const char *options,
680     const struct fe_option *long_options,
681     int *opt_index)
682{
683  return _fe_getopt_internal (argc, argv, options, long_options, opt_index, 1);
684}
685
686#ifdef TEST_GETOPT
687
688/* Compile with -DTEST_GETOPT to make an executable for use in testing
689   the above definition of `getopt'.  */
690
691int main (int argc, char **argv)
692{
693  int c;
694  int digit_optind = 0;
695
696  while (1)
697    {
698      int this_option_optind = optind ? optind : 1;
699
700      c = fe_getopt (argc, argv, "abc:d:0123456789");
701      if (c == EOF)
702        break;
703
704      switch (c)
705        {
706        case '0':
707        case '1':
708        case '2':
709        case '3':
710        case '4':
711        case '5':
712        case '6':
713        case '7':
714        case '8':
715        case '9':
716          if (digit_optind != 0 && digit_optind != this_option_optind)
717            printf ("digits occur in two different argv-elements.\n");
718          digit_optind = this_option_optind;
719          printf ("option %c\n", c);
720          break;
721
722        case 'a':
723          printf ("option a\n");
724          break;
725
726        case 'b':
727          printf ("option b\n");
728          break;
729
730        case 'c':
731          printf ("option c with value `%s'\n", fe_optarg);
732          break;
733
734        case BAD_OPTION:
735          break;
736
737        default:
738          printf ("?? fe_getopt returned character code 0%o ??\n", c);
739        }
740    }
741
742  if (fe_optind < argc)
743    {
744      printf ("non-option ARGV-elements: ");
745      while (fe_optind < argc)
746        printf ("%s ", argv[fe_optind++]);
747      printf ("\n");
748    }
749
750  exit (0);
751}
752
753#endif /* TEST_GETOPT */
Note: See TracBrowser for help on using the repository browser.