textsec: handle email address without domain part
[emacs.git] / lib / getopt.c
blobf66f119ec50ab3eb0f5810f4dec40ce008a3b338
1 /* Getopt for GNU.
2 Copyright (C) 1987-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library and is also part of gnulib.
4 Patches to this file should be submitted to both projects.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #ifndef _LIBC
21 # include <config.h>
22 #endif
24 #include <getopt.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #ifdef _LIBC
32 /* When used as part of glibc, error printing must be done differently
33 for standards compliance. getopt is not a cancellation point, so
34 it must not call functions that are, and it is specified by an
35 older standard than stdio locking, so it must not refer to
36 functions in the "user namespace" related to stdio locking.
37 Finally, it must use glibc's internal message translation so that
38 the messages are looked up in the proper text domain. */
39 # include <libintl.h>
40 # define fprintf __fxprintf_nocancel
41 # define flockfile(fp) _IO_flockfile (fp)
42 # define funlockfile(fp) _IO_funlockfile (fp)
43 #else
44 # include "gettext.h"
45 # define _(msgid) gettext (msgid)
46 /* When used standalone, flockfile and funlockfile might not be
47 available. */
48 # if (!defined _POSIX_THREAD_SAFE_FUNCTIONS \
49 || (defined _WIN32 && ! defined __CYGWIN__))
50 # define flockfile(fp) /* nop */
51 # define funlockfile(fp) /* nop */
52 # endif
53 /* When used standalone, do not attempt to use alloca. */
54 # define __libc_use_alloca(size) 0
55 # undef alloca
56 # define alloca(size) (abort (), (void *)0)
57 #endif
59 /* This implementation of 'getopt' has three modes for handling
60 options interspersed with non-option arguments. It can stop
61 scanning for options at the first non-option argument encountered,
62 as POSIX specifies. It can continue scanning for options after the
63 first non-option argument, but permute 'argv' as it goes so that,
64 after 'getopt' is done, all the options precede all the non-option
65 arguments and 'optind' points to the first non-option argument.
66 Or, it can report non-option arguments as if they were arguments to
67 the option character '\x01'.
69 The default behavior of 'getopt_long' is to permute the argument list.
70 When this implementation is used standalone, the default behavior of
71 'getopt' is to stop at the first non-option argument, but when it is
72 used as part of GNU libc it also permutes the argument list. In both
73 cases, setting the environment variable POSIXLY_CORRECT to any value
74 disables permutation.
76 If the first character of the OPTSTRING argument to 'getopt' or
77 'getopt_long' is '+', both functions will stop at the first
78 non-option argument. If it is '-', both functions will report
79 non-option arguments as arguments to the option character '\x01'. */
81 #include "getopt_int.h"
83 /* For communication from 'getopt' to the caller.
84 When 'getopt' finds an option that takes an argument,
85 the argument value is returned here.
86 Also, when 'ordering' is RETURN_IN_ORDER,
87 each non-option ARGV-element is returned here. */
89 char *optarg;
91 /* Index in ARGV of the next element to be scanned.
92 This is used for communication to and from the caller
93 and for communication between successive calls to 'getopt'.
95 On entry to 'getopt', zero means this is the first call; initialize.
97 When 'getopt' returns -1, this is the index of the first of the
98 non-option elements that the caller should itself scan.
100 Otherwise, 'optind' communicates from one call to the next
101 how much of ARGV has been scanned so far. */
103 /* 1003.2 says this must be 1 before any call. */
104 int optind = 1;
106 /* Callers store zero here to inhibit the error message
107 for unrecognized options. */
109 int opterr = 1;
111 /* Set to an option character which was unrecognized.
112 This must be initialized on some systems to avoid linking in the
113 system's own getopt implementation. */
115 int optopt = '?';
117 /* Keep a global copy of all internal members of getopt_data. */
119 static struct _getopt_data getopt_data;
121 /* Exchange two adjacent subsequences of ARGV.
122 One subsequence is elements [first_nonopt,last_nonopt)
123 which contains all the non-options that have been skipped so far.
124 The other is elements [last_nonopt,optind), which contains all
125 the options processed since those non-options were skipped.
127 'first_nonopt' and 'last_nonopt' are relocated so that they describe
128 the new indices of the non-options in ARGV after they are moved. */
130 static void
131 exchange (char **argv, struct _getopt_data *d)
133 int bottom = d->__first_nonopt;
134 int middle = d->__last_nonopt;
135 int top = d->optind;
136 char *tem;
138 /* Exchange the shorter segment with the far end of the longer segment.
139 That puts the shorter segment into the right place.
140 It leaves the longer segment in the right place overall,
141 but it consists of two parts that need to be swapped next. */
143 while (top > middle && middle > bottom)
145 if (top - middle > middle - bottom)
147 /* Bottom segment is the short one. */
148 int len = middle - bottom;
149 int i;
151 /* Swap it with the top part of the top segment. */
152 for (i = 0; i < len; i++)
154 tem = argv[bottom + i];
155 argv[bottom + i] = argv[top - (middle - bottom) + i];
156 argv[top - (middle - bottom) + i] = tem;
158 /* Exclude the moved bottom segment from further swapping. */
159 top -= len;
161 else
163 /* Top segment is the short one. */
164 int len = top - middle;
165 int i;
167 /* Swap it with the bottom part of the bottom segment. */
168 for (i = 0; i < len; i++)
170 tem = argv[bottom + i];
171 argv[bottom + i] = argv[middle + i];
172 argv[middle + i] = tem;
174 /* Exclude the moved top segment from further swapping. */
175 bottom += len;
179 /* Update records for the slots the non-options now occupy. */
181 d->__first_nonopt += (d->optind - d->__last_nonopt);
182 d->__last_nonopt = d->optind;
185 /* Process the argument starting with d->__nextchar as a long option.
186 d->optind should *not* have been advanced over this argument.
188 If the value returned is -1, it was not actually a long option, the
189 state is unchanged, and the argument should be processed as a set
190 of short options (this can only happen when long_only is true).
191 Otherwise, the option (and its argument, if any) have been consumed
192 and the return value is the value to return from _getopt_internal_r. */
193 static int
194 process_long_option (int argc, char **argv, const char *optstring,
195 const struct option *longopts, int *longind,
196 int long_only, struct _getopt_data *d,
197 int print_errors, const char *prefix)
199 char *nameend;
200 size_t namelen;
201 const struct option *p;
202 const struct option *pfound = NULL;
203 int n_options;
204 int option_index;
206 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
207 /* Do nothing. */ ;
208 namelen = nameend - d->__nextchar;
210 /* First look for an exact match, counting the options as a side
211 effect. */
212 for (p = longopts, n_options = 0; p->name; p++, n_options++)
213 if (!strncmp (p->name, d->__nextchar, namelen)
214 && namelen == strlen (p->name))
216 /* Exact match found. */
217 pfound = p;
218 option_index = n_options;
219 break;
222 if (pfound == NULL)
224 /* Didn't find an exact match, so look for abbreviations. */
225 unsigned char *ambig_set = NULL;
226 /* Use simpler fallback diagnostic if ambig_set == &ambig_fallback. */
227 unsigned char ambig_fallback;
228 void *ambig_malloced = NULL;
229 int indfound = -1;
231 for (p = longopts, option_index = 0; p->name; p++, option_index++)
232 if (!strncmp (p->name, d->__nextchar, namelen))
234 if (pfound == NULL)
236 /* First nonexact match found. */
237 pfound = p;
238 indfound = option_index;
240 else if (long_only
241 || pfound->has_arg != p->has_arg
242 || pfound->flag != p->flag
243 || pfound->val != p->val)
245 /* Second or later nonexact match found. */
246 if (ambig_set != &ambig_fallback)
248 if (!print_errors)
249 /* Don't waste effort tracking the ambig set if
250 we're not going to print it anyway. */
251 ambig_set = &ambig_fallback;
252 else if (!ambig_set)
254 if (__libc_use_alloca (n_options))
255 ambig_set = alloca (n_options);
256 else
258 ambig_malloced = malloc (n_options);
259 /* Fall back to simpler diagnostic if
260 memory allocation fails. */
261 ambig_set = (ambig_malloced ? ambig_malloced
262 : &ambig_fallback);
265 if (ambig_set != &ambig_fallback)
267 memset (ambig_set, 0, n_options);
268 ambig_set[indfound] = 1;
271 if (ambig_set && ambig_set != &ambig_fallback)
272 ambig_set[option_index] = 1;
277 if (ambig_set)
279 if (print_errors)
281 if (ambig_set == &ambig_fallback)
282 fprintf (stderr, _("%s: option '%s%s' is ambiguous\n"),
283 argv[0], prefix, d->__nextchar);
284 else
286 flockfile (stderr);
287 fprintf (stderr,
288 _("%s: option '%s%s' is ambiguous; possibilities:"),
289 argv[0], prefix, d->__nextchar);
291 for (option_index = 0; option_index < n_options; option_index++)
292 if (ambig_set[option_index])
293 fprintf (stderr, " '%s%s'",
294 prefix, longopts[option_index].name);
296 /* This must use 'fprintf' even though it's only
297 printing a single character, so that it goes through
298 __fxprintf_nocancel when compiled as part of glibc. */
299 fprintf (stderr, "\n");
300 funlockfile (stderr);
303 free (ambig_malloced);
304 d->__nextchar += strlen (d->__nextchar);
305 d->optind++;
306 d->optopt = 0;
307 return '?';
310 option_index = indfound;
313 if (pfound == NULL)
315 /* Can't find it as a long option. If this is not getopt_long_only,
316 or the option starts with '--' or is not a valid short option,
317 then it's an error. */
318 if (!long_only || argv[d->optind][1] == '-'
319 || strchr (optstring, *d->__nextchar) == NULL)
321 if (print_errors)
322 fprintf (stderr, _("%s: unrecognized option '%s%s'\n"),
323 argv[0], prefix, d->__nextchar);
325 d->__nextchar = NULL;
326 d->optind++;
327 d->optopt = 0;
328 return '?';
331 /* Otherwise interpret it as a short option. */
332 return -1;
335 /* We have found a matching long option. Consume it. */
336 d->optind++;
337 d->__nextchar = NULL;
338 if (*nameend)
340 /* Don't test has_arg with >, because some C compilers don't
341 allow it to be used on enums. */
342 if (pfound->has_arg)
343 d->optarg = nameend + 1;
344 else
346 if (print_errors)
347 fprintf (stderr,
348 _("%s: option '%s%s' doesn't allow an argument\n"),
349 argv[0], prefix, pfound->name);
351 d->optopt = pfound->val;
352 return '?';
355 else if (pfound->has_arg == 1)
357 if (d->optind < argc)
358 d->optarg = argv[d->optind++];
359 else
361 if (print_errors)
362 fprintf (stderr,
363 _("%s: option '%s%s' requires an argument\n"),
364 argv[0], prefix, pfound->name);
366 d->optopt = pfound->val;
367 return optstring[0] == ':' ? ':' : '?';
371 if (longind != NULL)
372 *longind = option_index;
373 if (pfound->flag)
375 *(pfound->flag) = pfound->val;
376 return 0;
378 return pfound->val;
381 /* Initialize internal data upon the first call to getopt. */
383 static const char *
384 _getopt_initialize (_GL_UNUSED int argc,
385 _GL_UNUSED char **argv, const char *optstring,
386 struct _getopt_data *d, int posixly_correct)
388 /* Start processing options with ARGV-element 1 (since ARGV-element 0
389 is the program name); the sequence of previously skipped
390 non-option ARGV-elements is empty. */
391 if (d->optind == 0)
392 d->optind = 1;
394 d->__first_nonopt = d->__last_nonopt = d->optind;
395 d->__nextchar = NULL;
397 /* Determine how to handle the ordering of options and nonoptions. */
398 if (optstring[0] == '-')
400 d->__ordering = RETURN_IN_ORDER;
401 ++optstring;
403 else if (optstring[0] == '+')
405 d->__ordering = REQUIRE_ORDER;
406 ++optstring;
408 else if (posixly_correct || !!getenv ("POSIXLY_CORRECT"))
409 d->__ordering = REQUIRE_ORDER;
410 else
411 d->__ordering = PERMUTE;
413 d->__initialized = 1;
414 return optstring;
417 /* Scan elements of ARGV (whose length is ARGC) for option characters
418 given in OPTSTRING.
420 If an element of ARGV starts with '-', and is not exactly "-" or "--",
421 then it is an option element. The characters of this element
422 (aside from the initial '-') are option characters. If 'getopt'
423 is called repeatedly, it returns successively each of the option characters
424 from each of the option elements.
426 If 'getopt' finds another option character, it returns that character,
427 updating 'optind' and 'nextchar' so that the next call to 'getopt' can
428 resume the scan with the following option character or ARGV-element.
430 If there are no more option characters, 'getopt' returns -1.
431 Then 'optind' is the index in ARGV of the first ARGV-element
432 that is not an option. (The ARGV-elements have been permuted
433 so that those that are not options now come last.)
435 OPTSTRING is a string containing the legitimate option characters.
436 If an option character is seen that is not listed in OPTSTRING,
437 return '?' after printing an error message. If you set 'opterr' to
438 zero, the error message is suppressed but we still return '?'.
440 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
441 so the following text in the same ARGV-element, or the text of the following
442 ARGV-element, is returned in 'optarg'. Two colons mean an option that
443 wants an optional arg; if there is text in the current ARGV-element,
444 it is returned in 'optarg', otherwise 'optarg' is set to zero.
446 If OPTSTRING starts with '-' or '+', it requests different methods of
447 handling the non-option ARGV-elements.
448 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
450 Long-named options begin with '--' instead of '-'.
451 Their names may be abbreviated as long as the abbreviation is unique
452 or is an exact match for some defined option. If they have an
453 argument, it follows the option name in the same ARGV-element, separated
454 from the option name by a '=', or else the in next ARGV-element.
455 When 'getopt' finds a long-named option, it returns 0 if that option's
456 'flag' field is nonzero, the value of the option's 'val' field
457 if the 'flag' field is zero.
459 The elements of ARGV aren't really const, because we permute them.
460 But we pretend they're const in the prototype to be compatible
461 with other systems.
463 LONGOPTS is a vector of 'struct option' terminated by an
464 element containing a name which is zero.
466 LONGIND returns the index in LONGOPT of the long-named option found.
467 It is only valid when a long-named option has been found by the most
468 recent call.
470 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
471 long-named options. */
474 _getopt_internal_r (int argc, char **argv, const char *optstring,
475 const struct option *longopts, int *longind,
476 int long_only, struct _getopt_data *d, int posixly_correct)
478 int print_errors = d->opterr;
480 if (argc < 1)
481 return -1;
483 d->optarg = NULL;
485 if (d->optind == 0 || !d->__initialized)
486 optstring = _getopt_initialize (argc, argv, optstring, d, posixly_correct);
487 else if (optstring[0] == '-' || optstring[0] == '+')
488 optstring++;
490 if (optstring[0] == ':')
491 print_errors = 0;
493 /* Test whether ARGV[optind] points to a non-option argument. */
494 #define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
496 if (d->__nextchar == NULL || *d->__nextchar == '\0')
498 /* Advance to the next ARGV-element. */
500 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
501 moved back by the user (who may also have changed the arguments). */
502 if (d->__last_nonopt > d->optind)
503 d->__last_nonopt = d->optind;
504 if (d->__first_nonopt > d->optind)
505 d->__first_nonopt = d->optind;
507 if (d->__ordering == PERMUTE)
509 /* If we have just processed some options following some non-options,
510 exchange them so that the options come first. */
512 if (d->__first_nonopt != d->__last_nonopt
513 && d->__last_nonopt != d->optind)
514 exchange (argv, d);
515 else if (d->__last_nonopt != d->optind)
516 d->__first_nonopt = d->optind;
518 /* Skip any additional non-options
519 and extend the range of non-options previously skipped. */
521 while (d->optind < argc && NONOPTION_P)
522 d->optind++;
523 d->__last_nonopt = d->optind;
526 /* The special ARGV-element '--' means premature end of options.
527 Skip it like a null option,
528 then exchange with previous non-options as if it were an option,
529 then skip everything else like a non-option. */
531 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
533 d->optind++;
535 if (d->__first_nonopt != d->__last_nonopt
536 && d->__last_nonopt != d->optind)
537 exchange (argv, d);
538 else if (d->__first_nonopt == d->__last_nonopt)
539 d->__first_nonopt = d->optind;
540 d->__last_nonopt = argc;
542 d->optind = argc;
545 /* If we have done all the ARGV-elements, stop the scan
546 and back over any non-options that we skipped and permuted. */
548 if (d->optind == argc)
550 /* Set the next-arg-index to point at the non-options
551 that we previously skipped, so the caller will digest them. */
552 if (d->__first_nonopt != d->__last_nonopt)
553 d->optind = d->__first_nonopt;
554 return -1;
557 /* If we have come to a non-option and did not permute it,
558 either stop the scan or describe it to the caller and pass it by. */
560 if (NONOPTION_P)
562 if (d->__ordering == REQUIRE_ORDER)
563 return -1;
564 d->optarg = argv[d->optind++];
565 return 1;
568 /* We have found another option-ARGV-element.
569 Check whether it might be a long option. */
570 if (longopts)
572 if (argv[d->optind][1] == '-')
574 /* "--foo" is always a long option. The special option
575 "--" was handled above. */
576 d->__nextchar = argv[d->optind] + 2;
577 return process_long_option (argc, argv, optstring, longopts,
578 longind, long_only, d,
579 print_errors, "--");
582 /* If long_only and the ARGV-element has the form "-f",
583 where f is a valid short option, don't consider it an
584 abbreviated form of a long option that starts with f.
585 Otherwise there would be no way to give the -f short
586 option.
588 On the other hand, if there's a long option "fubar" and
589 the ARGV-element is "-fu", do consider that an
590 abbreviation of the long option, just like "--fu", and
591 not "-f" with arg "u".
593 This distinction seems to be the most useful approach. */
594 if (long_only && (argv[d->optind][2]
595 || !strchr (optstring, argv[d->optind][1])))
597 int code;
598 d->__nextchar = argv[d->optind] + 1;
599 code = process_long_option (argc, argv, optstring, longopts,
600 longind, long_only, d,
601 print_errors, "-");
602 if (code != -1)
603 return code;
607 /* It is not a long option. Skip the initial punctuation. */
608 d->__nextchar = argv[d->optind] + 1;
611 /* Look at and handle the next short option-character. */
614 char c = *d->__nextchar++;
615 const char *temp = strchr (optstring, c);
617 /* Increment 'optind' when we start to process its last character. */
618 if (*d->__nextchar == '\0')
619 ++d->optind;
621 if (temp == NULL || c == ':' || c == ';')
623 if (print_errors)
624 fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
625 d->optopt = c;
626 return '?';
629 /* Convenience. Treat POSIX -W foo same as long option --foo */
630 if (temp[0] == 'W' && temp[1] == ';' && longopts != NULL)
632 /* This is an option that requires an argument. */
633 if (*d->__nextchar != '\0')
634 d->optarg = d->__nextchar;
635 else if (d->optind == argc)
637 if (print_errors)
638 fprintf (stderr,
639 _("%s: option requires an argument -- '%c'\n"),
640 argv[0], c);
642 d->optopt = c;
643 if (optstring[0] == ':')
644 c = ':';
645 else
646 c = '?';
647 return c;
649 else
650 d->optarg = argv[d->optind];
652 d->__nextchar = d->optarg;
653 d->optarg = NULL;
654 return process_long_option (argc, argv, optstring, longopts, longind,
655 0 /* long_only */, d, print_errors, "-W ");
657 if (temp[1] == ':')
659 if (temp[2] == ':')
661 /* This is an option that accepts an argument optionally. */
662 if (*d->__nextchar != '\0')
664 d->optarg = d->__nextchar;
665 d->optind++;
667 else
668 d->optarg = NULL;
669 d->__nextchar = NULL;
671 else
673 /* This is an option that requires an argument. */
674 if (*d->__nextchar != '\0')
676 d->optarg = d->__nextchar;
677 /* If we end this ARGV-element by taking the rest as an arg,
678 we must advance to the next element now. */
679 d->optind++;
681 else if (d->optind == argc)
683 if (print_errors)
684 fprintf (stderr,
685 _("%s: option requires an argument -- '%c'\n"),
686 argv[0], c);
688 d->optopt = c;
689 if (optstring[0] == ':')
690 c = ':';
691 else
692 c = '?';
694 else
695 /* We already incremented 'optind' once;
696 increment it again when taking next ARGV-elt as argument. */
697 d->optarg = argv[d->optind++];
698 d->__nextchar = NULL;
701 return c;
706 _getopt_internal (int argc, char **argv, const char *optstring,
707 const struct option *longopts, int *longind, int long_only,
708 int posixly_correct)
710 int result;
712 getopt_data.optind = optind;
713 getopt_data.opterr = opterr;
715 result = _getopt_internal_r (argc, argv, optstring, longopts,
716 longind, long_only, &getopt_data,
717 posixly_correct);
719 optind = getopt_data.optind;
720 optarg = getopt_data.optarg;
721 optopt = getopt_data.optopt;
723 return result;
726 /* glibc gets a LSB-compliant getopt and a POSIX-complaint __posix_getopt.
727 Standalone applications just get a POSIX-compliant getopt.
728 POSIX and LSB both require these functions to take 'char *const *argv'
729 even though this is incorrect (because of the permutation). */
730 #define GETOPT_ENTRY(NAME, POSIXLY_CORRECT) \
731 int \
732 NAME (int argc, char *const *argv, const char *optstring) \
734 return _getopt_internal (argc, (char **)argv, optstring, \
735 0, 0, 0, POSIXLY_CORRECT); \
738 #ifdef _LIBC
739 GETOPT_ENTRY(getopt, 0)
740 GETOPT_ENTRY(__posix_getopt, 1)
741 #else
742 GETOPT_ENTRY(getopt, 1)
743 #endif
746 #ifdef TEST
748 /* Compile with -DTEST to make an executable for use in testing
749 the above definition of 'getopt'. */
752 main (int argc, char **argv)
754 int c;
755 int digit_optind = 0;
757 while (1)
759 int this_option_optind = optind ? optind : 1;
761 c = getopt (argc, argv, "abc:d:0123456789");
762 if (c == -1)
763 break;
765 switch (c)
767 case '0':
768 case '1':
769 case '2':
770 case '3':
771 case '4':
772 case '5':
773 case '6':
774 case '7':
775 case '8':
776 case '9':
777 if (digit_optind != 0 && digit_optind != this_option_optind)
778 printf ("digits occur in two different argv-elements.\n");
779 digit_optind = this_option_optind;
780 printf ("option %c\n", c);
781 break;
783 case 'a':
784 printf ("option a\n");
785 break;
787 case 'b':
788 printf ("option b\n");
789 break;
791 case 'c':
792 printf ("option c with value '%s'\n", optarg);
793 break;
795 case '?':
796 break;
798 default:
799 printf ("?? getopt returned character code 0%o ??\n", c);
803 if (optind < argc)
805 printf ("non-option ARGV-elements: ");
806 while (optind < argc)
807 printf ("%s ", argv[optind++]);
808 printf ("\n");
811 exit (0);
814 #endif /* TEST */