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 drepper@gnu.org
6 Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
7 Free Software Foundation, Inc.
8 Copyright (C) 2004,2009,2010,2015 Olly Betts (reworked to allow compilation as C++)
10 The GNU C Library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
15 The GNU C Library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Library General Public
21 License along with the GNU C Library; see the file COPYING.LIB. If not,
22 write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 Boston, MA 02110-1301, USA. */
30 #include "gnu_getopt.h"
32 /* #ifdef out all this code if we are using the GNU C Library. GNU getopt
33 is included in the GNU C Library, and linking in this code is a waste when
34 using the GNU C library (especially if it is a shared library). */
36 #ifndef USE_GLIBC_GNUGETOPT
47 /* This is for other GNU distributions with internationalized messages. */
48 # if 0 //defined HAVE_LIBINTL_H || defined _LIBC
51 # define _(msgid) gettext (msgid)
54 # define _(msgid) (msgid)
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.
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.
67 Setting the environment variable POSIXLY_CORRECT disables permutation.
68 Then the behavior is completely standard.
70 GNU application programs can use a third alternative mode in which
71 they can distinguish the relative order of options and other arguments. */
73 /* For communication from `getopt' to the caller.
74 When `getopt' finds an option that takes an argument,
75 the argument value is returned here.
76 Also, when `ordering' is RETURN_IN_ORDER,
77 each non-option ARGV-element is returned here. */
81 /* Index in ARGV of the next element to be scanned.
82 This is used for communication to and from the caller
83 and for communication between successive calls to `getopt'.
85 On entry to `getopt', zero means this is the first call; initialize.
87 When `getopt' returns -1, this is the index of the first of the
88 non-option elements that the caller should itself scan.
90 Otherwise, `optind' communicates from one call to the next
91 how much of ARGV has been scanned so far. */
93 /* 1003.2 says this must be 1 before any call. */
96 /* Callers store zero here to inhibit the error message
97 for unrecognized options. */
101 /* Set to an option character which was unrecognized.
102 This must be initialized on some systems to avoid linking in the
103 system's own getopt implementation. */
108 /* Formerly, initialization of getopt depended on optind==0, which
109 causes problems with re-calling getopt as programs generally don't
112 static int getopt_initialized
;
114 /* The next char to be scanned in the option-element
115 in which the last option character we returned was found.
116 This allows us to pick up the scan where we left off.
118 If this is zero, or a null string, it means resume the scan
119 by advancing to the next ARGV-element. */
121 static char *nextchar
;
123 /* Describe how to deal with options that follow non-option ARGV-elements.
125 If the caller did not specify anything,
126 the default is REQUIRE_ORDER if the environment variable
127 POSIXLY_CORRECT is defined, PERMUTE otherwise.
129 REQUIRE_ORDER means don't recognize them as options;
130 stop option processing when the first non-option is seen.
131 This is what Unix does.
132 This mode of operation is selected by either setting the environment
133 variable POSIXLY_CORRECT, or using `+' as the first character
134 of the list of option characters.
136 PERMUTE is the default. We permute the contents of ARGV as we scan,
137 so that eventually all the non-options are at the end. This allows options
138 to be given in any order, even with programs that were not written to
141 RETURN_IN_ORDER is an option available to programs that were written
142 to expect options and other ARGV-elements in any order and that care about
143 the ordering of the two. We describe each non-option ARGV-element
144 as if it were the argument of an option with character code 1.
145 Using `-' as the first character of the list of option characters
146 selects this mode of operation.
148 The special argument `--' forces an end of option-scanning regardless
149 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
150 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
154 REQUIRE_ORDER
, PERMUTE
, RETURN_IN_ORDER
157 /* Value of POSIXLY_CORRECT environment variable. */
158 static char *posixly_correct
;
170 /* Handle permutation of arguments. */
172 /* Describe the part of ARGV that contains non-options that have
173 been skipped. `first_nonopt' is the index in ARGV of the first of them;
174 `last_nonopt' is the index after the last of them. */
176 static int first_nonopt
;
177 static int last_nonopt
;
179 /* Exchange two adjacent subsequences of ARGV.
180 One subsequence is elements [first_nonopt,last_nonopt)
181 which contains all the non-options that have been skipped so far.
182 The other is elements [last_nonopt,optind), which contains all
183 the options processed since those non-options were skipped.
185 `first_nonopt' and `last_nonopt' are relocated so that they describe
186 the new indices of the non-options in ARGV after they are moved. */
189 exchange (char **argv
)
191 int bottom
= first_nonopt
;
192 int middle
= last_nonopt
;
196 /* Exchange the shorter segment with the far end of the longer segment.
197 That puts the shorter segment into the right place.
198 It leaves the longer segment in the right place overall,
199 but it consists of two parts that need to be swapped next. */
201 while (top
> middle
&& middle
> bottom
)
203 if (top
- middle
> middle
- bottom
)
205 /* Bottom segment is the short one. */
206 int len
= middle
- bottom
;
209 /* Swap it with the top part of the top segment. */
210 for (i
= 0; i
< len
; i
++)
212 tem
= argv
[bottom
+ i
];
213 argv
[bottom
+ i
] = argv
[top
- (middle
- bottom
) + i
];
214 argv
[top
- (middle
- bottom
) + i
] = tem
;
216 /* Exclude the moved bottom segment from further swapping. */
221 /* Top segment is the short one. */
222 int len
= top
- middle
;
225 /* Swap it with the bottom part of the bottom segment. */
226 for (i
= 0; i
< len
; i
++)
228 tem
= argv
[bottom
+ i
];
229 argv
[bottom
+ i
] = argv
[middle
+ i
];
230 argv
[middle
+ i
] = tem
;
232 /* Exclude the moved top segment from further swapping. */
237 /* Update records for the slots the non-options now occupy. */
239 first_nonopt
+= (optind
- last_nonopt
);
240 last_nonopt
= optind
;
243 /* Initialize the internal data when the first call is made. */
246 getopt_initialize (int argc
, char *const *argv
, const char *optstring
)
248 /* Suppress possible unused warnings */
252 /* Start processing options with ARGV-element 1 (since ARGV-element 0
253 is the program name); the sequence of previously skipped
254 non-option ARGV-elements is empty. */
256 first_nonopt
= last_nonopt
= optind
;
260 posixly_correct
= getenv ("POSIXLY_CORRECT");
262 /* Determine how to handle the ordering of options and nonoptions. */
264 if (optstring
[0] == '-')
266 ordering
= RETURN_IN_ORDER
;
269 else if (optstring
[0] == '+')
271 ordering
= REQUIRE_ORDER
;
274 else if (posixly_correct
!= NULL
)
275 ordering
= REQUIRE_ORDER
;
282 /* Scan elements of ARGV (whose length is ARGC) for option characters
285 If an element of ARGV starts with '-', and is not exactly "-" or "--",
286 then it is an option element. The characters of this element
287 (aside from the initial '-') are option characters. If `getopt'
288 is called repeatedly, it returns successively each of the option characters
289 from each of the option elements.
291 If `getopt' finds another option character, it returns that character,
292 updating `optind' and `nextchar' so that the next call to `getopt' can
293 resume the scan with the following option character or ARGV-element.
295 If there are no more option characters, `getopt' returns -1.
296 Then `optind' is the index in ARGV of the first ARGV-element
297 that is not an option. (The ARGV-elements have been permuted
298 so that those that are not options now come last.)
300 OPTSTRING is a string containing the legitimate option characters.
301 If an option character is seen that is not listed in OPTSTRING,
302 return '?' after printing an error message. If you set `opterr' to
303 zero, the error message is suppressed but we still return '?'.
305 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
306 so the following text in the same ARGV-element, or the text of the following
307 ARGV-element, is returned in `optarg'. Two colons mean an option that
308 wants an optional arg; if there is text in the current ARGV-element,
309 it is returned in `optarg', otherwise `optarg' is set to zero.
311 If OPTSTRING starts with `-' or `+', it requests different methods of
312 handling the non-option ARGV-elements.
313 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
315 Long-named options begin with `--' instead of `-'.
316 Their names may be abbreviated as long as the abbreviation is unique
317 or is an exact match for some defined option. If they have an
318 argument, it follows the option name in the same ARGV-element, separated
319 from the option name by a `=', or else the in next ARGV-element.
320 When `getopt' finds a long-named option, it returns 0 if that option's
321 `flag' field is nonzero, the value of the option's `val' field
322 if the `flag' field is zero.
324 The elements of ARGV aren't really const, because we permute them.
325 But we pretend they're const in the prototype to be compatible
328 LONGOPTS is a vector of `struct option' terminated by an
329 element containing a name which is zero.
331 LONGIND returns the index in LONGOPT of the long-named option found.
332 It is only valid when a long-named option has been found by the most
335 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
336 long-named options. */
339 gnu_getopt_internal_(int argc
, char *const *argv
, const char *optstring
, const struct option
*longopts
, int *longind
, int long_only
)
341 int print_errors
= opterr
;
342 if (optstring
[0] == ':')
350 if (optind
== 0 || !getopt_initialized
)
353 optind
= 1; /* Don't scan ARGV[0], the program name. */
354 optstring
= getopt_initialize (argc
, argv
, optstring
);
355 getopt_initialized
= 1;
358 /* Test whether ARGV[optind] points to a non-option argument (i.e. it does
359 not have option syntax). */
360 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
362 if (nextchar
== NULL
|| *nextchar
== '\0')
364 /* Advance to the next ARGV-element. */
366 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
367 moved back by the user (who may also have changed the arguments). */
368 if (last_nonopt
> optind
)
369 last_nonopt
= optind
;
370 if (first_nonopt
> optind
)
371 first_nonopt
= optind
;
373 if (ordering
== PERMUTE
)
375 /* If we have just processed some options following some non-options,
376 exchange them so that the options come first. */
378 if (first_nonopt
!= last_nonopt
&& last_nonopt
!= optind
)
379 exchange (const_cast<char **>(argv
));
380 else if (last_nonopt
!= optind
)
381 first_nonopt
= optind
;
383 /* Skip any additional non-options
384 and extend the range of non-options previously skipped. */
386 while (optind
< argc
&& NONOPTION_P
)
388 last_nonopt
= optind
;
391 /* The special ARGV-element `--' means premature end of options.
392 Skip it like a null option,
393 then exchange with previous non-options as if it were an option,
394 then skip everything else like a non-option. */
396 if (optind
!= argc
&& !strcmp (argv
[optind
], "--"))
400 if (first_nonopt
!= last_nonopt
&& last_nonopt
!= optind
)
401 exchange (const_cast<char **>(argv
));
402 else if (first_nonopt
== last_nonopt
)
403 first_nonopt
= optind
;
409 /* If we have done all the ARGV-elements, stop the scan
410 and back over any non-options that we skipped and permuted. */
414 /* Set the next-arg-index to point at the non-options
415 that we previously skipped, so the caller will digest them. */
416 if (first_nonopt
!= last_nonopt
)
417 optind
= first_nonopt
;
421 /* If we have come to a non-option and did not permute it,
422 either stop the scan or describe it to the caller and pass it by. */
426 if (ordering
== REQUIRE_ORDER
)
428 optarg
= argv
[optind
++];
432 /* We have found another option-ARGV-element.
433 Skip the initial punctuation. */
435 nextchar
= (argv
[optind
] + 1
436 + (longopts
!= NULL
&& argv
[optind
][1] == '-'));
439 /* Decode the current option-ARGV-element. */
441 /* Check whether the ARGV-element is a long option.
443 If long_only and the ARGV-element has the form "-f", where f is
444 a valid short option, don't consider it an abbreviated form of
445 a long option that starts with f. Otherwise there would be no
446 way to give the -f short option.
448 On the other hand, if there's a long option "fubar" and
449 the ARGV-element is "-fu", do consider that an abbreviation of
450 the long option, just like "--fu", and not "-f" with arg "u".
452 This distinction seems to be the most useful approach. */
455 && (argv
[optind
][1] == '-'
456 || (long_only
&& (argv
[optind
][2] || !strchr (optstring
, argv
[optind
][1])))))
459 const struct option
*p
;
460 const struct option
*pfound
= NULL
;
466 for (nameend
= nextchar
; *nameend
&& *nameend
!= '='; nameend
++)
469 /* Test all long options for either exact match
470 or abbreviated matches. */
471 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
472 if (!strncmp (p
->name
, nextchar
, nameend
- nextchar
))
474 if (unsigned(nameend
- nextchar
) == unsigned(strlen(p
->name
)))
476 /* Exact match found. */
478 indfound
= option_index
;
482 else if (pfound
== NULL
)
484 /* First nonexact match found. */
486 indfound
= option_index
;
489 || pfound
->has_arg
!= p
->has_arg
490 || pfound
->flag
!= p
->flag
491 || pfound
->val
!= p
->val
)
492 /* Second or later nonexact match found. */
499 fprintf (stderr
, _("%s: option '%s' is ambiguous\n"),
500 argv
[0], argv
[optind
]);
501 nextchar
+= strlen (nextchar
);
509 option_index
= indfound
;
513 /* Don't test has_arg with >, because some C compilers don't
514 allow it to be used on enums. */
516 optarg
= nameend
+ 1;
521 if (argv
[optind
- 1][1] == '-')
524 _("%s: option '--%s' doesn't allow an argument\n"),
525 argv
[0], pfound
->name
);
527 /* +option or -option */
529 _("%s: option '%c%s' doesn't allow an argument\n"),
530 argv
[0], argv
[optind
- 1][0], pfound
->name
);
533 nextchar
+= strlen (nextchar
);
535 optopt
= pfound
->val
;
539 else if (pfound
->has_arg
== 1)
542 optarg
= argv
[optind
++];
547 _("%s: option '%s' requires an argument\n"),
548 argv
[0], argv
[optind
- 1]);
549 nextchar
+= strlen (nextchar
);
550 optopt
= pfound
->val
;
551 return optstring
[0] == ':' ? ':' : '?';
554 nextchar
+= strlen (nextchar
);
556 *longind
= option_index
;
559 *(pfound
->flag
) = pfound
->val
;
565 /* Can't find it as a long option. If this is not getopt_long_only,
566 or the option starts with '--' or is not a valid short
567 option, then it's an error.
568 Otherwise interpret it as a short option. */
569 if (!long_only
|| argv
[optind
][1] == '-'
570 || strchr (optstring
, *nextchar
) == NULL
)
574 if (argv
[optind
][1] == '-')
576 fprintf (stderr
, _("%s: unrecognized option '--%s'\n"),
579 /* +option or -option */
580 fprintf (stderr
, _("%s: unrecognized option '%c%s'\n"),
581 argv
[0], argv
[optind
][0], nextchar
);
583 nextchar
= const_cast<char *>("");
590 /* Look at and handle the next short option-character. */
593 char c
= *nextchar
++;
594 const char *temp
= strchr (optstring
, c
);
596 /* Increment `optind' when we start to process its last character. */
597 if (*nextchar
== '\0')
600 if (temp
== NULL
|| c
== ':')
605 /* 1003.2 specifies the format of this message. */
606 fprintf (stderr
, _("%s: illegal option -- %c\n"),
609 fprintf (stderr
, _("%s: invalid option -- %c\n"),
615 /* Convenience. Treat POSIX -W foo same as long option --foo */
616 if (temp
[0] == 'W' && temp
[1] == ';')
619 const struct option
*p
;
620 const struct option
*pfound
= NULL
;
626 /* This is an option that requires an argument. */
627 if (*nextchar
!= '\0')
630 /* If we end this ARGV-element by taking the rest as an arg,
631 we must advance to the next element now. */
634 else if (optind
== argc
)
638 /* 1003.2 specifies the format of this message. */
639 fprintf (stderr
, _("%s: option requires an argument -- %c\n"),
643 if (optstring
[0] == ':')
650 /* We already incremented `optind' once;
651 increment it again when taking next ARGV-elt as argument. */
652 optarg
= argv
[optind
++];
654 /* optarg is now the argument, see if it's in the
655 table of longopts. */
657 for (nextchar
= nameend
= optarg
; *nameend
&& *nameend
!= '='; nameend
++)
660 /* Test all long options for either exact match
661 or abbreviated matches. */
662 for (p
= longopts
, option_index
= 0; p
->name
; p
++, option_index
++)
663 if (!strncmp (p
->name
, nextchar
, nameend
- nextchar
))
665 if (unsigned(nameend
- nextchar
) == unsigned(strlen(p
->name
)))
667 /* Exact match found. */
669 indfound
= option_index
;
673 else if (pfound
== NULL
)
675 /* First nonexact match found. */
677 indfound
= option_index
;
680 /* Second or later nonexact match found. */
686 fprintf (stderr
, _("%s: option '-W %s' is ambiguous\n"),
687 argv
[0], argv
[optind
]);
688 nextchar
+= strlen (nextchar
);
694 option_index
= indfound
;
697 /* Don't test has_arg with >, because some C compilers don't
698 allow it to be used on enums. */
700 optarg
= nameend
+ 1;
704 fprintf (stderr
, _("\
705 %s: option '-W %s' doesn't allow an argument\n"),
706 argv
[0], pfound
->name
);
708 nextchar
+= strlen (nextchar
);
712 else if (pfound
->has_arg
== 1)
715 optarg
= argv
[optind
++];
720 _("%s: option '%s' requires an argument\n"),
721 argv
[0], argv
[optind
- 1]);
722 nextchar
+= strlen (nextchar
);
723 return optstring
[0] == ':' ? ':' : '?';
726 nextchar
+= strlen (nextchar
);
728 *longind
= option_index
;
731 *(pfound
->flag
) = pfound
->val
;
737 return 'W'; /* Let the application handle it. */
743 /* This is an option that accepts an argument optionally. */
744 if (*nextchar
!= '\0')
755 /* This is an option that requires an argument. */
756 if (*nextchar
!= '\0')
759 /* If we end this ARGV-element by taking the rest as an arg,
760 we must advance to the next element now. */
763 else if (optind
== argc
)
767 /* 1003.2 specifies the format of this message. */
769 _("%s: option requires an argument -- %c\n"),
773 if (optstring
[0] == ':')
779 /* We already incremented `optind' once;
780 increment it again when taking next ARGV-elt as argument. */
781 optarg
= argv
[optind
++];
789 #endif /* Not ELIDE_CODE. */