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