Add NEWS for 5.6.3
[xz.git] / lib / getopt.c
blobab3ff879ced0d7cefab2b3937b072c3b933a07a6
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 /* Getopt for GNU.
4 Copyright (C) 1987-2023 Free Software Foundation, Inc.
5 This file is part of the GNU C Library and is also part of gnulib.
6 Patches to this file should be submitted to both projects.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 The GNU C Library 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 GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library; if not, see
20 <https://www.gnu.org/licenses/>. */
22 #ifndef _LIBC
23 # ifdef HAVE_CONFIG_H
24 # include <config.h>
25 # endif
26 #endif
28 #include "getopt.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #ifndef _MSC_VER
34 # include <unistd.h>
35 #endif
37 #ifdef _LIBC
38 /* When used as part of glibc, error printing must be done differently
39 for standards compliance. getopt is not a cancellation point, so
40 it must not call functions that are, and it is specified by an
41 older standard than stdio locking, so it must not refer to
42 functions in the "user namespace" related to stdio locking.
43 Finally, it must use glibc's internal message translation so that
44 the messages are looked up in the proper text domain. */
45 # include <libintl.h>
46 # define fprintf __fxprintf_nocancel
47 # define flockfile(fp) _IO_flockfile (fp)
48 # define funlockfile(fp) _IO_funlockfile (fp)
49 #else
50 /* Completely disable NLS for getopt. We won't include translations for it
51 anyway. If the system lacks getopt_long, missing translations probably
52 aren't a problem. */
53 //# include "gettext.h"
54 //# define _(msgid) gettext (msgid)
55 #define _(msgid) (msgid)
56 /* When used standalone, flockfile and funlockfile might not be
57 available. */
58 # if (!defined _POSIX_THREAD_SAFE_FUNCTIONS \
59 || (defined _WIN32 && ! defined __CYGWIN__))
60 # define flockfile(fp) /* nop */
61 # define funlockfile(fp) /* nop */
62 # endif
63 /* When used standalone, do not attempt to use alloca. */
64 # define __libc_use_alloca(size) 0
65 # undef alloca
66 # define alloca(size) (abort (), (void *)0)
67 #endif
69 /* This implementation of 'getopt' has three modes for handling
70 options interspersed with non-option arguments. It can stop
71 scanning for options at the first non-option argument encountered,
72 as POSIX specifies. It can continue scanning for options after the
73 first non-option argument, but permute 'argv' as it goes so that,
74 after 'getopt' is done, all the options precede all the non-option
75 arguments and 'optind' points to the first non-option argument.
76 Or, it can report non-option arguments as if they were arguments to
77 the option character '\x01'.
79 The default behavior of 'getopt_long' is to permute the argument list.
80 When this implementation is used standalone, the default behavior of
81 'getopt' is to stop at the first non-option argument, but when it is
82 used as part of GNU libc it also permutes the argument list. In both
83 cases, setting the environment variable POSIXLY_CORRECT to any value
84 disables permutation.
86 If the first character of the OPTSTRING argument to 'getopt' or
87 'getopt_long' is '+', both functions will stop at the first
88 non-option argument. If it is '-', both functions will report
89 non-option arguments as arguments to the option character '\x01'. */
91 #include "getopt_int.h"
93 /* For communication from 'getopt' to the caller.
94 When 'getopt' finds an option that takes an argument,
95 the argument value is returned here.
96 Also, when 'ordering' is RETURN_IN_ORDER,
97 each non-option ARGV-element is returned here. */
99 char *optarg;
101 /* Index in ARGV of the next element to be scanned.
102 This is used for communication to and from the caller
103 and for communication between successive calls to 'getopt'.
105 On entry to 'getopt', zero means this is the first call; initialize.
107 When 'getopt' returns -1, this is the index of the first of the
108 non-option elements that the caller should itself scan.
110 Otherwise, 'optind' communicates from one call to the next
111 how much of ARGV has been scanned so far. */
113 /* 1003.2 says this must be 1 before any call. */
114 int optind = 1;
116 /* Callers store zero here to inhibit the error message
117 for unrecognized options. */
119 int opterr = 1;
121 /* Set to an option character which was unrecognized.
122 This must be initialized on some systems to avoid linking in the
123 system's own getopt implementation. */
125 int optopt = '?';
127 /* Keep a global copy of all internal members of getopt_data. */
129 static struct _getopt_data getopt_data;
131 /* Exchange two adjacent subsequences of ARGV.
132 One subsequence is elements [first_nonopt,last_nonopt)
133 which contains all the non-options that have been skipped so far.
134 The other is elements [last_nonopt,optind), which contains all
135 the options processed since those non-options were skipped.
137 'first_nonopt' and 'last_nonopt' are relocated so that they describe
138 the new indices of the non-options in ARGV after they are moved. */
140 static void
141 exchange (char **argv, struct _getopt_data *d)
143 int bottom = d->__first_nonopt;
144 int middle = d->__last_nonopt;
145 int top = d->optind;
146 char *tem;
148 /* Exchange the shorter segment with the far end of the longer segment.
149 That puts the shorter segment into the right place.
150 It leaves the longer segment in the right place overall,
151 but it consists of two parts that need to be swapped next. */
153 while (top > middle && middle > bottom)
155 if (top - middle > middle - bottom)
157 /* Bottom segment is the short one. */
158 int len = middle - bottom;
159 int i;
161 /* Swap it with the top part of the top segment. */
162 for (i = 0; i < len; i++)
164 tem = argv[bottom + i];
165 argv[bottom + i] = argv[top - (middle - bottom) + i];
166 argv[top - (middle - bottom) + i] = tem;
168 /* Exclude the moved bottom segment from further swapping. */
169 top -= len;
171 else
173 /* Top segment is the short one. */
174 int len = top - middle;
175 int i;
177 /* Swap it with the bottom part of the bottom segment. */
178 for (i = 0; i < len; i++)
180 tem = argv[bottom + i];
181 argv[bottom + i] = argv[middle + i];
182 argv[middle + i] = tem;
184 /* Exclude the moved top segment from further swapping. */
185 bottom += len;
189 /* Update records for the slots the non-options now occupy. */
191 d->__first_nonopt += (d->optind - d->__last_nonopt);
192 d->__last_nonopt = d->optind;
195 /* Process the argument starting with d->__nextchar as a long option.
196 d->optind should *not* have been advanced over this argument.
198 If the value returned is -1, it was not actually a long option, the
199 state is unchanged, and the argument should be processed as a set
200 of short options (this can only happen when long_only is true).
201 Otherwise, the option (and its argument, if any) have been consumed
202 and the return value is the value to return from _getopt_internal_r. */
203 static int
204 process_long_option (int argc, char **argv, const char *optstring,
205 const struct option *longopts, int *longind,
206 int long_only, struct _getopt_data *d,
207 int print_errors, const char *prefix)
209 char *nameend;
210 size_t namelen;
211 const struct option *p;
212 const struct option *pfound = NULL;
213 int n_options;
214 int option_index;
216 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
217 /* Do nothing. */ ;
218 namelen = (size_t)(nameend - d->__nextchar);
220 /* First look for an exact match, counting the options as a side
221 effect. */
222 for (p = longopts, n_options = 0; p->name; p++, n_options++)
223 if (!strncmp (p->name, d->__nextchar, namelen)
224 && namelen == strlen (p->name))
226 /* Exact match found. */
227 pfound = p;
228 option_index = n_options;
229 break;
232 if (pfound == NULL)
234 /* Didn't find an exact match, so look for abbreviations. */
235 unsigned char *ambig_set = NULL;
236 int ambig_malloced = 0;
237 int ambig_fallback = 0;
238 int indfound = -1;
240 for (p = longopts, option_index = 0; p->name; p++, option_index++)
241 if (!strncmp (p->name, d->__nextchar, namelen))
243 if (pfound == NULL)
245 /* First nonexact match found. */
246 pfound = p;
247 indfound = option_index;
249 else if (long_only
250 || pfound->has_arg != p->has_arg
251 || pfound->flag != p->flag
252 || pfound->val != p->val)
254 /* Second or later nonexact match found. */
255 if (!ambig_fallback)
257 if (!print_errors)
258 /* Don't waste effort tracking the ambig set if
259 we're not going to print it anyway. */
260 ambig_fallback = 1;
261 else if (!ambig_set)
263 if (__libc_use_alloca (n_options))
264 ambig_set = alloca (n_options);
265 else if ((ambig_set = malloc ((size_t)n_options)) == NULL)
266 /* Fall back to simpler error message. */
267 ambig_fallback = 1;
268 else
269 ambig_malloced = 1;
271 if (ambig_set)
273 memset (ambig_set, 0, (size_t)n_options);
274 ambig_set[indfound] = 1;
277 if (ambig_set)
278 ambig_set[option_index] = 1;
283 if (ambig_set || ambig_fallback)
285 if (print_errors)
287 if (ambig_fallback)
288 fprintf (stderr, _("%s: option '%s%s' is ambiguous\n"),
289 argv[0], prefix, d->__nextchar);
290 else
292 flockfile (stderr);
293 fprintf (stderr,
294 _("%s: option '%s%s' is ambiguous; possibilities:"),
295 argv[0], prefix, d->__nextchar);
297 for (option_index = 0; option_index < n_options; option_index++)
298 if (ambig_set[option_index])
299 fprintf (stderr, " '%s%s'",
300 prefix, longopts[option_index].name);
302 /* This must use 'fprintf' even though it's only
303 printing a single character, so that it goes through
304 __fxprintf_nocancel when compiled as part of glibc. */
305 fprintf (stderr, "\n");
306 funlockfile (stderr);
309 if (ambig_malloced)
310 free (ambig_set);
311 d->__nextchar += strlen (d->__nextchar);
312 d->optind++;
313 d->optopt = 0;
314 return '?';
317 option_index = indfound;
320 if (pfound == NULL)
322 /* Can't find it as a long option. If this is not getopt_long_only,
323 or the option starts with '--' or is not a valid short option,
324 then it's an error. */
325 if (!long_only || argv[d->optind][1] == '-'
326 || strchr (optstring, *d->__nextchar) == NULL)
328 if (print_errors)
329 fprintf (stderr, _("%s: unrecognized option '%s%s'\n"),
330 argv[0], prefix, d->__nextchar);
332 d->__nextchar = NULL;
333 d->optind++;
334 d->optopt = 0;
335 return '?';
338 /* Otherwise interpret it as a short option. */
339 return -1;
342 /* We have found a matching long option. Consume it. */
343 d->optind++;
344 d->__nextchar = NULL;
345 if (*nameend)
347 /* Don't test has_arg with >, because some C compilers don't
348 allow it to be used on enums. */
349 if (pfound->has_arg)
350 d->optarg = nameend + 1;
351 else
353 if (print_errors)
354 fprintf (stderr,
355 _("%s: option '%s%s' doesn't allow an argument\n"),
356 argv[0], prefix, pfound->name);
358 d->optopt = pfound->val;
359 return '?';
362 else if (pfound->has_arg == 1)
364 if (d->optind < argc)
365 d->optarg = argv[d->optind++];
366 else
368 if (print_errors)
369 fprintf (stderr,
370 _("%s: option '%s%s' requires an argument\n"),
371 argv[0], prefix, pfound->name);
373 d->optopt = pfound->val;
374 return optstring[0] == ':' ? ':' : '?';
378 if (longind != NULL)
379 *longind = option_index;
380 if (pfound->flag)
382 *(pfound->flag) = pfound->val;
383 return 0;
385 return pfound->val;
388 /* Initialize internal data upon the first call to getopt. */
390 static const char *
391 _getopt_initialize (int argc,
392 char **argv, const char *optstring,
393 struct _getopt_data *d, int posixly_correct)
395 (void)argc;
396 (void)argv;
397 /* Start processing options with ARGV-element 1 (since ARGV-element 0
398 is the program name); the sequence of previously skipped
399 non-option ARGV-elements is empty. */
400 if (d->optind == 0)
401 d->optind = 1;
403 d->__first_nonopt = d->__last_nonopt = d->optind;
404 d->__nextchar = NULL;
406 /* Determine how to handle the ordering of options and nonoptions. */
407 if (optstring[0] == '-')
409 d->__ordering = RETURN_IN_ORDER;
410 ++optstring;
412 else if (optstring[0] == '+')
414 d->__ordering = REQUIRE_ORDER;
415 ++optstring;
417 else if (posixly_correct || !!getenv ("POSIXLY_CORRECT"))
418 d->__ordering = REQUIRE_ORDER;
419 else
420 d->__ordering = PERMUTE;
422 d->__initialized = 1;
423 return optstring;
426 /* Scan elements of ARGV (whose length is ARGC) for option characters
427 given in OPTSTRING.
429 If an element of ARGV starts with '-', and is not exactly "-" or "--",
430 then it is an option element. The characters of this element
431 (aside from the initial '-') are option characters. If 'getopt'
432 is called repeatedly, it returns successively each of the option characters
433 from each of the option elements.
435 If 'getopt' finds another option character, it returns that character,
436 updating 'optind' and 'nextchar' so that the next call to 'getopt' can
437 resume the scan with the following option character or ARGV-element.
439 If there are no more option characters, 'getopt' returns -1.
440 Then 'optind' is the index in ARGV of the first ARGV-element
441 that is not an option. (The ARGV-elements have been permuted
442 so that those that are not options now come last.)
444 OPTSTRING is a string containing the legitimate option characters.
445 If an option character is seen that is not listed in OPTSTRING,
446 return '?' after printing an error message. If you set 'opterr' to
447 zero, the error message is suppressed but we still return '?'.
449 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
450 so the following text in the same ARGV-element, or the text of the following
451 ARGV-element, is returned in 'optarg'. Two colons mean an option that
452 wants an optional arg; if there is text in the current ARGV-element,
453 it is returned in 'optarg', otherwise 'optarg' is set to zero.
455 If OPTSTRING starts with '-' or '+', it requests different methods of
456 handling the non-option ARGV-elements.
457 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
459 Long-named options begin with '--' instead of '-'.
460 Their names may be abbreviated as long as the abbreviation is unique
461 or is an exact match for some defined option. If they have an
462 argument, it follows the option name in the same ARGV-element, separated
463 from the option name by a '=', or else the in next ARGV-element.
464 When 'getopt' finds a long-named option, it returns 0 if that option's
465 'flag' field is nonzero, the value of the option's 'val' field
466 if the 'flag' field is zero.
468 The elements of ARGV aren't really const, because we permute them.
469 But we pretend they're const in the prototype to be compatible
470 with other systems.
472 LONGOPTS is a vector of 'struct option' terminated by an
473 element containing a name which is zero.
475 LONGIND returns the index in LONGOPT of the long-named option found.
476 It is only valid when a long-named option has been found by the most
477 recent call.
479 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
480 long-named options. */
483 _getopt_internal_r (int argc, char **argv, const char *optstring,
484 const struct option *longopts, int *longind,
485 int long_only, struct _getopt_data *d, int posixly_correct)
487 int print_errors = d->opterr;
489 if (argc < 1)
490 return -1;
492 d->optarg = NULL;
494 if (d->optind == 0 || !d->__initialized)
495 optstring = _getopt_initialize (argc, argv, optstring, d, posixly_correct);
496 else if (optstring[0] == '-' || optstring[0] == '+')
497 optstring++;
499 if (optstring[0] == ':')
500 print_errors = 0;
502 /* Test whether ARGV[optind] points to a non-option argument. */
503 #define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
505 if (d->__nextchar == NULL || *d->__nextchar == '\0')
507 /* Advance to the next ARGV-element. */
509 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
510 moved back by the user (who may also have changed the arguments). */
511 if (d->__last_nonopt > d->optind)
512 d->__last_nonopt = d->optind;
513 if (d->__first_nonopt > d->optind)
514 d->__first_nonopt = d->optind;
516 if (d->__ordering == PERMUTE)
518 /* If we have just processed some options following some non-options,
519 exchange them so that the options come first. */
521 if (d->__first_nonopt != d->__last_nonopt
522 && d->__last_nonopt != d->optind)
523 exchange (argv, d);
524 else if (d->__last_nonopt != d->optind)
525 d->__first_nonopt = d->optind;
527 /* Skip any additional non-options
528 and extend the range of non-options previously skipped. */
530 while (d->optind < argc && NONOPTION_P)
531 d->optind++;
532 d->__last_nonopt = d->optind;
535 /* The special ARGV-element '--' means premature end of options.
536 Skip it like a null option,
537 then exchange with previous non-options as if it were an option,
538 then skip everything else like a non-option. */
540 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
542 d->optind++;
544 if (d->__first_nonopt != d->__last_nonopt
545 && d->__last_nonopt != d->optind)
546 exchange (argv, d);
547 else if (d->__first_nonopt == d->__last_nonopt)
548 d->__first_nonopt = d->optind;
549 d->__last_nonopt = argc;
551 d->optind = argc;
554 /* If we have done all the ARGV-elements, stop the scan
555 and back over any non-options that we skipped and permuted. */
557 if (d->optind == argc)
559 /* Set the next-arg-index to point at the non-options
560 that we previously skipped, so the caller will digest them. */
561 if (d->__first_nonopt != d->__last_nonopt)
562 d->optind = d->__first_nonopt;
563 return -1;
566 /* If we have come to a non-option and did not permute it,
567 either stop the scan or describe it to the caller and pass it by. */
569 if (NONOPTION_P)
571 if (d->__ordering == REQUIRE_ORDER)
572 return -1;
573 d->optarg = argv[d->optind++];
574 return 1;
577 /* We have found another option-ARGV-element.
578 Check whether it might be a long option. */
579 if (longopts)
581 if (argv[d->optind][1] == '-')
583 /* "--foo" is always a long option. The special option
584 "--" was handled above. */
585 d->__nextchar = argv[d->optind] + 2;
586 return process_long_option (argc, argv, optstring, longopts,
587 longind, long_only, d,
588 print_errors, "--");
591 /* If long_only and the ARGV-element has the form "-f",
592 where f is a valid short option, don't consider it an
593 abbreviated form of a long option that starts with f.
594 Otherwise there would be no way to give the -f short
595 option.
597 On the other hand, if there's a long option "fubar" and
598 the ARGV-element is "-fu", do consider that an
599 abbreviation of the long option, just like "--fu", and
600 not "-f" with arg "u".
602 This distinction seems to be the most useful approach. */
603 if (long_only && (argv[d->optind][2]
604 || !strchr (optstring, argv[d->optind][1])))
606 int code;
607 d->__nextchar = argv[d->optind] + 1;
608 code = process_long_option (argc, argv, optstring, longopts,
609 longind, long_only, d,
610 print_errors, "-");
611 if (code != -1)
612 return code;
616 /* It is not a long option. Skip the initial punctuation. */
617 d->__nextchar = argv[d->optind] + 1;
620 /* Look at and handle the next short option-character. */
623 char c = *d->__nextchar++;
624 const char *temp = strchr (optstring, c);
626 /* Increment 'optind' when we start to process its last character. */
627 if (*d->__nextchar == '\0')
628 ++d->optind;
630 if (temp == NULL || c == ':' || c == ';')
632 if (print_errors)
633 fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
634 d->optopt = c;
635 return '?';
638 /* Convenience. Treat POSIX -W foo same as long option --foo */
639 if (temp[0] == 'W' && temp[1] == ';' && longopts != NULL)
641 /* This is an option that requires an argument. */
642 if (*d->__nextchar != '\0')
643 d->optarg = d->__nextchar;
644 else if (d->optind == argc)
646 if (print_errors)
647 fprintf (stderr,
648 _("%s: option requires an argument -- '%c'\n"),
649 argv[0], c);
651 d->optopt = c;
652 if (optstring[0] == ':')
653 c = ':';
654 else
655 c = '?';
656 return c;
658 else
659 d->optarg = argv[d->optind];
661 d->__nextchar = d->optarg;
662 d->optarg = NULL;
663 return process_long_option (argc, argv, optstring, longopts, longind,
664 0 /* long_only */, d, print_errors, "-W ");
666 if (temp[1] == ':')
668 if (temp[2] == ':')
670 /* This is an option that accepts an argument optionally. */
671 if (*d->__nextchar != '\0')
673 d->optarg = d->__nextchar;
674 d->optind++;
676 else
677 d->optarg = NULL;
678 d->__nextchar = NULL;
680 else
682 /* This is an option that requires an argument. */
683 if (*d->__nextchar != '\0')
685 d->optarg = d->__nextchar;
686 /* If we end this ARGV-element by taking the rest as an arg,
687 we must advance to the next element now. */
688 d->optind++;
690 else if (d->optind == argc)
692 if (print_errors)
693 fprintf (stderr,
694 _("%s: option requires an argument -- '%c'\n"),
695 argv[0], c);
697 d->optopt = c;
698 if (optstring[0] == ':')
699 c = ':';
700 else
701 c = '?';
703 else
704 /* We already incremented 'optind' once;
705 increment it again when taking next ARGV-elt as argument. */
706 d->optarg = argv[d->optind++];
707 d->__nextchar = NULL;
710 return c;
715 _getopt_internal (int argc, char **argv, const char *optstring,
716 const struct option *longopts, int *longind, int long_only,
717 int posixly_correct)
719 int result;
721 getopt_data.optind = optind;
722 getopt_data.opterr = opterr;
724 result = _getopt_internal_r (argc, argv, optstring, longopts,
725 longind, long_only, &getopt_data,
726 posixly_correct);
728 optind = getopt_data.optind;
729 optarg = getopt_data.optarg;
730 optopt = getopt_data.optopt;
732 return result;
735 /* glibc gets a LSB-compliant getopt and a POSIX-complaint __posix_getopt.
736 Standalone applications just get a POSIX-compliant getopt.
737 POSIX and LSB both require these functions to take 'char *const *argv'
738 even though this is incorrect (because of the permutation). */
739 #define GETOPT_ENTRY(NAME, POSIXLY_CORRECT) \
740 int \
741 NAME (int argc, char *const *argv, const char *optstring) \
743 return _getopt_internal (argc, (char **)argv, optstring, \
744 0, 0, 0, POSIXLY_CORRECT); \
747 #ifdef _LIBC
748 GETOPT_ENTRY(getopt, 0)
749 GETOPT_ENTRY(__posix_getopt, 1)
750 #else
751 GETOPT_ENTRY(getopt, 1)
752 #endif
755 #ifdef TEST
757 /* Compile with -DTEST to make an executable for use in testing
758 the above definition of 'getopt'. */
761 main (int argc, char **argv)
763 int c;
764 int digit_optind = 0;
766 while (1)
768 int this_option_optind = optind ? optind : 1;
770 c = getopt (argc, argv, "abc:d:0123456789");
771 if (c == -1)
772 break;
774 switch (c)
776 case '0':
777 case '1':
778 case '2':
779 case '3':
780 case '4':
781 case '5':
782 case '6':
783 case '7':
784 case '8':
785 case '9':
786 if (digit_optind != 0 && digit_optind != this_option_optind)
787 printf ("digits occur in two different argv-elements.\n");
788 digit_optind = this_option_optind;
789 printf ("option %c\n", c);
790 break;
792 case 'a':
793 printf ("option a\n");
794 break;
796 case 'b':
797 printf ("option b\n");
798 break;
800 case 'c':
801 printf ("option c with value '%s'\n", optarg);
802 break;
804 case '?':
805 break;
807 default:
808 printf ("?? getopt returned character code 0%o ??\n", c);
812 if (optind < argc)
814 printf ("non-option ARGV-elements: ");
815 while (optind < argc)
816 printf ("%s ", argv[optind++]);
817 printf ("\n");
820 exit (0);
823 #endif /* TEST */