file_case.{h,cpp}: wether->whether; 2017
[s-roff.git] / src / lib-roff / getopt.c
blob65f50ee0e9f86643bc22c1764b149449c28de200
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 drepper@gnu.org
4 before changing it!
5 Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004
6 Free Software Foundation, Inc.
7 This file is part of the GNU C Library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation,
21 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
24 Ditto for AIX 3.2 and <stdlib.h>. */
25 #ifndef _NO_PROTO
26 # define _NO_PROTO
27 #endif
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
33 #include <stdio.h>
35 /* This needs to come after some library #include
36 to get __GNU_LIBRARY__ defined. */
37 #ifdef __GNU_LIBRARY__
38 /* Don't include stdlib.h for non-GNU C libraries because some of them
39 contain conflicting prototypes for getopt. */
40 # include <stdlib.h>
41 # include <unistd.h>
42 #endif /* GNU C library. */
44 #include <string.h>
46 #ifdef VMS
47 # include <unixlib.h>
48 #endif
50 #ifdef _LIBC
51 # include <libintl.h>
52 #else
53 # include "gettext.h"
54 # define _(msgid) gettext (msgid)
55 #endif
57 #if defined _LIBC && defined USE_IN_LIBIO
58 # include <wchar.h>
59 #endif
61 #ifndef attribute_hidden
62 # define attribute_hidden
63 #endif
65 /* Unlike standard Unix `getopt', functions like `getopt_long'
66 let the user intersperse the options with the other arguments.
68 As `getopt_long' works, it permutes the elements of ARGV so that,
69 when it is done, all the options precede everything else. Thus
70 all application programs are extended to handle flexible argument order.
72 Using `getopt' or setting the environment variable POSIXLY_CORRECT
73 disables permutation.
74 Then the application's behavior is completely standard.
76 GNU application programs can use a third alternative mode in which
77 they can distinguish the relative order of options and other arguments. */
79 #include "getopt.h"
80 #include "getopt_int.h"
82 /* For communication from `getopt' to the caller.
83 When `getopt' finds an option that takes an argument,
84 the argument value is returned here.
85 Also, when `ordering' is RETURN_IN_ORDER,
86 each non-option ARGV-element is returned here. */
88 char *optarg;
90 /* Index in ARGV of the next element to be scanned.
91 This is used for communication to and from the caller
92 and for communication between successive calls to `getopt'.
94 On entry to `getopt', zero means this is the first call; initialize.
96 When `getopt' returns -1, this is the index of the first of the
97 non-option elements that the caller should itself scan.
99 Otherwise, `optind' communicates from one call to the next
100 how much of ARGV has been scanned so far. */
102 /* 1003.2 says this must be 1 before any call. */
103 int optind = 1;
105 /* Callers store zero here to inhibit the error message
106 for unrecognized options. */
108 int opterr = 1;
110 /* Set to an option character which was unrecognized.
111 This must be initialized on some systems to avoid linking in the
112 system's own getopt implementation. */
114 int optopt = '?';
116 /* Keep a global copy of all internal members of getopt_data. */
118 static struct _getopt_data getopt_data;
120 #ifndef __GNU_LIBRARY__
122 /* Avoid depending on library functions or files
123 whose names are inconsistent. */
125 #ifndef getenv
126 extern char *getenv ();
127 #endif
129 #endif /* not __GNU_LIBRARY__ */
131 #ifdef _LIBC
132 /* Stored original parameters.
133 XXX This is no good solution. We should rather copy the args so
134 that we can compare them later. But we must not use malloc(3). */
135 extern int __libc_argc;
136 extern char **__libc_argv;
138 /* Bash 2.0 gives us an environment variable containing flags
139 indicating ARGV elements that should not be considered arguments. */
141 # ifdef USE_NONOPTION_FLAGS
142 /* Defined in getopt_init.c */
143 extern char *__getopt_nonoption_flags;
144 # endif
146 # ifdef USE_NONOPTION_FLAGS
147 # define SWAP_FLAGS(ch1, ch2) \
148 if (d->__nonoption_flags_len > 0) \
150 char __tmp = __getopt_nonoption_flags[ch1]; \
151 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
152 __getopt_nonoption_flags[ch2] = __tmp; \
154 # else
155 # define SWAP_FLAGS(ch1, ch2)
156 # endif
157 #else /* !_LIBC */
158 # define SWAP_FLAGS(ch1, ch2)
159 #endif /* _LIBC */
161 /* Exchange two adjacent subsequences of ARGV.
162 One subsequence is elements [first_nonopt,last_nonopt)
163 which contains all the non-options that have been skipped so far.
164 The other is elements [last_nonopt,optind), which contains all
165 the options processed since those non-options were skipped.
167 `first_nonopt' and `last_nonopt' are relocated so that they describe
168 the new indices of the non-options in ARGV after they are moved. */
170 static void
171 exchange (char **argv, struct _getopt_data *d)
173 int bottom = d->__first_nonopt;
174 int middle = d->__last_nonopt;
175 int top = d->optind;
176 char *tem;
178 /* Exchange the shorter segment with the far end of the longer segment.
179 That puts the shorter segment into the right place.
180 It leaves the longer segment in the right place overall,
181 but it consists of two parts that need to be swapped next. */
183 #if defined _LIBC && defined USE_NONOPTION_FLAGS
184 /* First make sure the handling of the `__getopt_nonoption_flags'
185 string can work normally. Our top argument must be in the range
186 of the string. */
187 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
189 /* We must extend the array. The user plays games with us and
190 presents new arguments. */
191 char *new_str = malloc (top + 1);
192 if (new_str == NULL)
193 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
194 else
196 memset (__mempcpy (new_str, __getopt_nonoption_flags,
197 d->__nonoption_flags_max_len),
198 '\0', top + 1 - d->__nonoption_flags_max_len);
199 d->__nonoption_flags_max_len = top + 1;
200 __getopt_nonoption_flags = new_str;
203 #endif
205 while (top > middle && middle > bottom)
207 if (top - middle > middle - bottom)
209 /* Bottom segment is the short one. */
210 int len = middle - bottom;
211 register int i;
213 /* Swap it with the top part of the top segment. */
214 for (i = 0; i < len; i++)
216 tem = argv[bottom + i];
217 argv[bottom + i] = argv[top - (middle - bottom) + i];
218 argv[top - (middle - bottom) + i] = tem;
219 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
221 /* Exclude the moved bottom segment from further swapping. */
222 top -= len;
224 else
226 /* Top segment is the short one. */
227 int len = top - middle;
228 register int i;
230 /* Swap it with the bottom part of the bottom segment. */
231 for (i = 0; i < len; i++)
233 tem = argv[bottom + i];
234 argv[bottom + i] = argv[middle + i];
235 argv[middle + i] = tem;
236 SWAP_FLAGS (bottom + i, middle + i);
238 /* Exclude the moved top segment from further swapping. */
239 bottom += len;
243 /* Update records for the slots the non-options now occupy. */
245 d->__first_nonopt += (d->optind - d->__last_nonopt);
246 d->__last_nonopt = d->optind;
249 /* Initialize the internal data when the first call is made. */
251 static const char *
252 _getopt_initialize (int argc, char **argv, const char *optstring,
253 int posixly_correct, struct _getopt_data *d)
255 /* Start processing options with ARGV-element 1 (since ARGV-element 0
256 is the program name); the sequence of previously skipped
257 non-option ARGV-elements is empty. */
259 d->__first_nonopt = d->__last_nonopt = d->optind;
261 d->__nextchar = NULL;
263 d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
265 /* Determine how to handle the ordering of options and nonoptions. */
267 if (optstring[0] == '-')
269 d->__ordering = RETURN_IN_ORDER;
270 ++optstring;
272 else if (optstring[0] == '+')
274 d->__ordering = REQUIRE_ORDER;
275 ++optstring;
277 else if (d->__posixly_correct)
278 d->__ordering = REQUIRE_ORDER;
279 else
280 d->__ordering = PERMUTE;
282 #if defined _LIBC && defined USE_NONOPTION_FLAGS
283 if (!d->__posixly_correct
284 && argc == __libc_argc && argv == __libc_argv)
286 if (d->__nonoption_flags_max_len == 0)
288 if (__getopt_nonoption_flags == NULL
289 || __getopt_nonoption_flags[0] == '\0')
290 d->__nonoption_flags_max_len = -1;
291 else
293 const char *orig_str = __getopt_nonoption_flags;
294 int len = d->__nonoption_flags_max_len = strlen (orig_str);
295 if (d->__nonoption_flags_max_len < argc)
296 d->__nonoption_flags_max_len = argc;
297 __getopt_nonoption_flags =
298 (char *) malloc (d->__nonoption_flags_max_len);
299 if (__getopt_nonoption_flags == NULL)
300 d->__nonoption_flags_max_len = -1;
301 else
302 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
303 '\0', d->__nonoption_flags_max_len - len);
306 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
308 else
309 d->__nonoption_flags_len = 0;
310 #endif
312 return optstring;
315 /* Scan elements of ARGV (whose length is ARGC) for option characters
316 given in OPTSTRING.
318 If an element of ARGV starts with '-', and is not exactly "-" or "--",
319 then it is an option element. The characters of this element
320 (aside from the initial '-') are option characters. If `getopt'
321 is called repeatedly, it returns successively each of the option characters
322 from each of the option elements.
324 If `getopt' finds another option character, it returns that character,
325 updating `optind' and `nextchar' so that the next call to `getopt' can
326 resume the scan with the following option character or ARGV-element.
328 If there are no more option characters, `getopt' returns -1.
329 Then `optind' is the index in ARGV of the first ARGV-element
330 that is not an option. (The ARGV-elements have been permuted
331 so that those that are not options now come last.)
333 OPTSTRING is a string containing the legitimate option characters.
334 If an option character is seen that is not listed in OPTSTRING,
335 return '?' after printing an error message. If you set `opterr' to
336 zero, the error message is suppressed but we still return '?'.
338 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
339 so the following text in the same ARGV-element, or the text of the following
340 ARGV-element, is returned in `optarg'. Two colons mean an option that
341 wants an optional arg; if there is text in the current ARGV-element,
342 it is returned in `optarg', otherwise `optarg' is set to zero.
344 If OPTSTRING starts with `-' or `+', it requests different methods of
345 handling the non-option ARGV-elements.
346 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
348 Long-named options begin with `--' instead of `-'.
349 Their names may be abbreviated as long as the abbreviation is unique
350 or is an exact match for some defined option. If they have an
351 argument, it follows the option name in the same ARGV-element, separated
352 from the option name by a `=', or else the in next ARGV-element.
353 When `getopt' finds a long-named option, it returns 0 if that option's
354 `flag' field is nonzero, the value of the option's `val' field
355 if the `flag' field is zero.
357 LONGOPTS is a vector of `struct option' terminated by an
358 element containing a name which is zero.
360 LONGIND returns the index in LONGOPT of the long-named option found.
361 It is only valid when a long-named option has been found by the most
362 recent call.
364 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
365 long-named options.
367 If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
368 environment variable were set. */
371 _getopt_internal_r (int argc, char **argv, const char *optstring,
372 const struct option *longopts, int *longind,
373 int long_only, int posixly_correct, struct _getopt_data *d)
375 int print_errors = d->opterr;
376 if (optstring[0] == ':')
377 print_errors = 0;
379 if (argc < 1)
380 return -1;
382 d->optarg = NULL;
384 if (d->optind == 0 || !d->__initialized)
386 if (d->optind == 0)
387 d->optind = 1; /* Don't scan ARGV[0], the program name. */
388 optstring = _getopt_initialize (argc, argv, optstring,
389 posixly_correct, d);
390 d->__initialized = 1;
393 /* Test whether ARGV[optind] points to a non-option argument.
394 Either it does not have option syntax, or there is an environment flag
395 from the shell indicating it is not an option. The later information
396 is only used when the used in the GNU libc. */
397 #if defined _LIBC && defined USE_NONOPTION_FLAGS
398 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
399 || (d->optind < d->__nonoption_flags_len \
400 && __getopt_nonoption_flags[d->optind] == '1'))
401 #else
402 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
403 #endif
405 if (d->__nextchar == NULL || *d->__nextchar == '\0')
407 /* Advance to the next ARGV-element. */
409 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
410 moved back by the user (who may also have changed the arguments). */
411 if (d->__last_nonopt > d->optind)
412 d->__last_nonopt = d->optind;
413 if (d->__first_nonopt > d->optind)
414 d->__first_nonopt = d->optind;
416 if (d->__ordering == PERMUTE)
418 /* If we have just processed some options following some non-options,
419 exchange them so that the options come first. */
421 if (d->__first_nonopt != d->__last_nonopt
422 && d->__last_nonopt != d->optind)
423 exchange ((char **) argv, d);
424 else if (d->__last_nonopt != d->optind)
425 d->__first_nonopt = d->optind;
427 /* Skip any additional non-options
428 and extend the range of non-options previously skipped. */
430 while (d->optind < argc && NONOPTION_P)
431 d->optind++;
432 d->__last_nonopt = d->optind;
435 /* The special ARGV-element `--' means premature end of options.
436 Skip it like a null option,
437 then exchange with previous non-options as if it were an option,
438 then skip everything else like a non-option. */
440 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
442 d->optind++;
444 if (d->__first_nonopt != d->__last_nonopt
445 && d->__last_nonopt != d->optind)
446 exchange ((char **) argv, d);
447 else if (d->__first_nonopt == d->__last_nonopt)
448 d->__first_nonopt = d->optind;
449 d->__last_nonopt = argc;
451 d->optind = argc;
454 /* If we have done all the ARGV-elements, stop the scan
455 and back over any non-options that we skipped and permuted. */
457 if (d->optind == argc)
459 /* Set the next-arg-index to point at the non-options
460 that we previously skipped, so the caller will digest them. */
461 if (d->__first_nonopt != d->__last_nonopt)
462 d->optind = d->__first_nonopt;
463 return -1;
466 /* If we have come to a non-option and did not permute it,
467 either stop the scan or describe it to the caller and pass it by. */
469 if (NONOPTION_P)
471 if (d->__ordering == REQUIRE_ORDER)
472 return -1;
473 d->optarg = argv[d->optind++];
474 return 1;
477 /* We have found another option-ARGV-element.
478 Skip the initial punctuation. */
480 d->__nextchar = (argv[d->optind] + 1
481 + (longopts != NULL && argv[d->optind][1] == '-'));
484 /* Decode the current option-ARGV-element. */
486 /* Check whether the ARGV-element is a long option.
488 If long_only and the ARGV-element has the form "-f", where f is
489 a valid short option, don't consider it an abbreviated form of
490 a long option that starts with f. Otherwise there would be no
491 way to give the -f short option.
493 On the other hand, if there's a long option "fubar" and
494 the ARGV-element is "-fu", do consider that an abbreviation of
495 the long option, just like "--fu", and not "-f" with arg "u".
497 This distinction seems to be the most useful approach. */
499 if (longopts != NULL
500 && (argv[d->optind][1] == '-'
501 || (long_only && (argv[d->optind][2]
502 || !strchr (optstring, argv[d->optind][1])))))
504 char *nameend;
505 const struct option *p;
506 const struct option *pfound = NULL;
507 int exact = 0;
508 int ambig = 0;
509 int indfound = -1;
510 int option_index;
512 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
513 /* Do nothing. */ ;
515 /* Test all long options for either exact match
516 or abbreviated matches. */
517 for (p = longopts, option_index = 0; p->name; p++, option_index++)
518 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
520 if ((unsigned int) (nameend - d->__nextchar)
521 == (unsigned int) strlen (p->name))
523 /* Exact match found. */
524 pfound = p;
525 indfound = option_index;
526 exact = 1;
527 break;
529 else if (pfound == NULL)
531 /* First nonexact match found. */
532 pfound = p;
533 indfound = option_index;
535 else if (long_only
536 || pfound->has_arg != p->has_arg
537 || pfound->flag != p->flag
538 || pfound->val != p->val)
539 /* Second or later nonexact match found. */
540 ambig = 1;
543 if (ambig && !exact)
545 if (print_errors)
547 #if defined _LIBC && defined USE_IN_LIBIO
548 char *buf;
550 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
551 argv[0], argv[d->optind]) >= 0)
553 _IO_flockfile (stderr);
555 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
556 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
558 if (_IO_fwide (stderr, 0) > 0)
559 __fwprintf (stderr, L"%s", buf);
560 else
561 fputs (buf, stderr);
563 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
564 _IO_funlockfile (stderr);
566 free (buf);
568 #else
569 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
570 argv[0], argv[d->optind]);
571 #endif
573 d->__nextchar += strlen (d->__nextchar);
574 d->optind++;
575 d->optopt = 0;
576 return '?';
579 if (pfound != NULL)
581 option_index = indfound;
582 d->optind++;
583 if (*nameend)
585 /* Don't test has_arg with >, because some C compilers don't
586 allow it to be used on enums. */
587 if (pfound->has_arg)
588 d->optarg = nameend + 1;
589 else
591 if (print_errors)
593 #if defined _LIBC && defined USE_IN_LIBIO
594 char *buf;
595 int n;
596 #endif
598 if (argv[d->optind - 1][1] == '-')
600 /* --option */
601 #if defined _LIBC && defined USE_IN_LIBIO
602 n = __asprintf (&buf, _("\
603 %s: option `--%s' doesn't allow an argument\n"),
604 argv[0], pfound->name);
605 #else
606 fprintf (stderr, _("\
607 %s: option `--%s' doesn't allow an argument\n"),
608 argv[0], pfound->name);
609 #endif
611 else
613 /* +option or -option */
614 #if defined _LIBC && defined USE_IN_LIBIO
615 n = __asprintf (&buf, _("\
616 %s: option `%c%s' doesn't allow an argument\n"),
617 argv[0], argv[d->optind - 1][0],
618 pfound->name);
619 #else
620 fprintf (stderr, _("\
621 %s: option `%c%s' doesn't allow an argument\n"),
622 argv[0], argv[d->optind - 1][0],
623 pfound->name);
624 #endif
627 #if defined _LIBC && defined USE_IN_LIBIO
628 if (n >= 0)
630 _IO_flockfile (stderr);
632 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
633 ((_IO_FILE *) stderr)->_flags2
634 |= _IO_FLAGS2_NOTCANCEL;
636 if (_IO_fwide (stderr, 0) > 0)
637 __fwprintf (stderr, L"%s", buf);
638 else
639 fputs (buf, stderr);
641 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
642 _IO_funlockfile (stderr);
644 free (buf);
646 #endif
649 d->__nextchar += strlen (d->__nextchar);
651 d->optopt = pfound->val;
652 return '?';
655 else if (pfound->has_arg == 1)
657 if (d->optind < argc)
658 d->optarg = argv[d->optind++];
659 else
661 if (print_errors)
663 #if defined _LIBC && defined USE_IN_LIBIO
664 char *buf;
666 if (__asprintf (&buf, _("\
667 %s: option `%s' requires an argument\n"),
668 argv[0], argv[d->optind - 1]) >= 0)
670 _IO_flockfile (stderr);
672 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
673 ((_IO_FILE *) stderr)->_flags2
674 |= _IO_FLAGS2_NOTCANCEL;
676 if (_IO_fwide (stderr, 0) > 0)
677 __fwprintf (stderr, L"%s", buf);
678 else
679 fputs (buf, stderr);
681 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
682 _IO_funlockfile (stderr);
684 free (buf);
686 #else
687 fprintf (stderr,
688 _("%s: option `%s' requires an argument\n"),
689 argv[0], argv[d->optind - 1]);
690 #endif
692 d->__nextchar += strlen (d->__nextchar);
693 d->optopt = pfound->val;
694 return optstring[0] == ':' ? ':' : '?';
697 d->__nextchar += strlen (d->__nextchar);
698 if (longind != NULL)
699 *longind = option_index;
700 if (pfound->flag)
702 *(pfound->flag) = pfound->val;
703 return 0;
705 return pfound->val;
708 /* Can't find it as a long option. If this is not getopt_long_only,
709 or the option starts with '--' or is not a valid short
710 option, then it's an error.
711 Otherwise interpret it as a short option. */
712 if (!long_only || argv[d->optind][1] == '-'
713 || strchr (optstring, *d->__nextchar) == NULL)
715 if (print_errors)
717 #if defined _LIBC && defined USE_IN_LIBIO
718 char *buf;
719 int n;
720 #endif
722 if (argv[d->optind][1] == '-')
724 /* --option */
725 #if defined _LIBC && defined USE_IN_LIBIO
726 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
727 argv[0], d->__nextchar);
728 #else
729 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
730 argv[0], d->__nextchar);
731 #endif
733 else
735 /* +option or -option */
736 #if defined _LIBC && defined USE_IN_LIBIO
737 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
738 argv[0], argv[d->optind][0], d->__nextchar);
739 #else
740 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
741 argv[0], argv[d->optind][0], d->__nextchar);
742 #endif
745 #if defined _LIBC && defined USE_IN_LIBIO
746 if (n >= 0)
748 _IO_flockfile (stderr);
750 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
751 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
753 if (_IO_fwide (stderr, 0) > 0)
754 __fwprintf (stderr, L"%s", buf);
755 else
756 fputs (buf, stderr);
758 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
759 _IO_funlockfile (stderr);
761 free (buf);
763 #endif
765 d->__nextchar = (char *) "";
766 d->optind++;
767 d->optopt = 0;
768 return '?';
772 /* Look at and handle the next short option-character. */
775 char c = *d->__nextchar++;
776 char *temp = strchr (optstring, c);
778 /* Increment `optind' when we start to process its last character. */
779 if (*d->__nextchar == '\0')
780 ++d->optind;
782 if (temp == NULL || c == ':')
784 if (print_errors)
786 #if defined _LIBC && defined USE_IN_LIBIO
787 char *buf;
788 int n;
789 #endif
791 if (d->__posixly_correct)
793 /* 1003.2 specifies the format of this message. */
794 #if defined _LIBC && defined USE_IN_LIBIO
795 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
796 argv[0], c);
797 #else
798 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
799 #endif
801 else
803 #if defined _LIBC && defined USE_IN_LIBIO
804 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
805 argv[0], c);
806 #else
807 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
808 #endif
811 #if defined _LIBC && defined USE_IN_LIBIO
812 if (n >= 0)
814 _IO_flockfile (stderr);
816 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
817 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
819 if (_IO_fwide (stderr, 0) > 0)
820 __fwprintf (stderr, L"%s", buf);
821 else
822 fputs (buf, stderr);
824 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
825 _IO_funlockfile (stderr);
827 free (buf);
829 #endif
831 d->optopt = c;
832 return '?';
834 /* Convenience. Treat POSIX -W foo same as long option --foo */
835 if (temp[0] == 'W' && temp[1] == ';')
837 char *nameend;
838 const struct option *p;
839 const struct option *pfound = NULL;
840 int exact = 0;
841 int ambig = 0;
842 int indfound = 0;
843 int option_index;
845 /* This is an option that requires an argument. */
846 if (*d->__nextchar != '\0')
848 d->optarg = d->__nextchar;
849 /* If we end this ARGV-element by taking the rest as an arg,
850 we must advance to the next element now. */
851 d->optind++;
853 else if (d->optind == argc)
855 if (print_errors)
857 /* 1003.2 specifies the format of this message. */
858 #if defined _LIBC && defined USE_IN_LIBIO
859 char *buf;
861 if (__asprintf (&buf,
862 _("%s: option requires an argument -- %c\n"),
863 argv[0], c) >= 0)
865 _IO_flockfile (stderr);
867 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
868 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
870 if (_IO_fwide (stderr, 0) > 0)
871 __fwprintf (stderr, L"%s", buf);
872 else
873 fputs (buf, stderr);
875 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
876 _IO_funlockfile (stderr);
878 free (buf);
880 #else
881 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
882 argv[0], c);
883 #endif
885 d->optopt = c;
886 if (optstring[0] == ':')
887 c = ':';
888 else
889 c = '?';
890 return c;
892 else
893 /* We already incremented `d->optind' once;
894 increment it again when taking next ARGV-elt as argument. */
895 d->optarg = argv[d->optind++];
897 /* optarg is now the argument, see if it's in the
898 table of longopts. */
900 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
901 nameend++)
902 /* Do nothing. */ ;
904 /* Test all long options for either exact match
905 or abbreviated matches. */
906 for (p = longopts, option_index = 0; p->name; p++, option_index++)
907 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
909 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
911 /* Exact match found. */
912 pfound = p;
913 indfound = option_index;
914 exact = 1;
915 break;
917 else if (pfound == NULL)
919 /* First nonexact match found. */
920 pfound = p;
921 indfound = option_index;
923 else
924 /* Second or later nonexact match found. */
925 ambig = 1;
927 if (ambig && !exact)
929 if (print_errors)
931 #if defined _LIBC && defined USE_IN_LIBIO
932 char *buf;
934 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
935 argv[0], argv[d->optind]) >= 0)
937 _IO_flockfile (stderr);
939 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
940 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
942 if (_IO_fwide (stderr, 0) > 0)
943 __fwprintf (stderr, L"%s", buf);
944 else
945 fputs (buf, stderr);
947 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
948 _IO_funlockfile (stderr);
950 free (buf);
952 #else
953 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
954 argv[0], argv[d->optind]);
955 #endif
957 d->__nextchar += strlen (d->__nextchar);
958 d->optind++;
959 return '?';
961 if (pfound != NULL)
963 option_index = indfound;
964 if (*nameend)
966 /* Don't test has_arg with >, because some C compilers don't
967 allow it to be used on enums. */
968 if (pfound->has_arg)
969 d->optarg = nameend + 1;
970 else
972 if (print_errors)
974 #if defined _LIBC && defined USE_IN_LIBIO
975 char *buf;
977 if (__asprintf (&buf, _("\
978 %s: option `-W %s' doesn't allow an argument\n"),
979 argv[0], pfound->name) >= 0)
981 _IO_flockfile (stderr);
983 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
984 ((_IO_FILE *) stderr)->_flags2
985 |= _IO_FLAGS2_NOTCANCEL;
987 if (_IO_fwide (stderr, 0) > 0)
988 __fwprintf (stderr, L"%s", buf);
989 else
990 fputs (buf, stderr);
992 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
993 _IO_funlockfile (stderr);
995 free (buf);
997 #else
998 fprintf (stderr, _("\
999 %s: option `-W %s' doesn't allow an argument\n"),
1000 argv[0], pfound->name);
1001 #endif
1004 d->__nextchar += strlen (d->__nextchar);
1005 return '?';
1008 else if (pfound->has_arg == 1)
1010 if (d->optind < argc)
1011 d->optarg = argv[d->optind++];
1012 else
1014 if (print_errors)
1016 #if defined _LIBC && defined USE_IN_LIBIO
1017 char *buf;
1019 if (__asprintf (&buf, _("\
1020 %s: option `%s' requires an argument\n"),
1021 argv[0], argv[d->optind - 1]) >= 0)
1023 _IO_flockfile (stderr);
1025 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1026 ((_IO_FILE *) stderr)->_flags2
1027 |= _IO_FLAGS2_NOTCANCEL;
1029 if (_IO_fwide (stderr, 0) > 0)
1030 __fwprintf (stderr, L"%s", buf);
1031 else
1032 fputs (buf, stderr);
1034 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1035 _IO_funlockfile (stderr);
1037 free (buf);
1039 #else
1040 fprintf (stderr,
1041 _("%s: option `%s' requires an argument\n"),
1042 argv[0], argv[d->optind - 1]);
1043 #endif
1045 d->__nextchar += strlen (d->__nextchar);
1046 return optstring[0] == ':' ? ':' : '?';
1049 d->__nextchar += strlen (d->__nextchar);
1050 if (longind != NULL)
1051 *longind = option_index;
1052 if (pfound->flag)
1054 *(pfound->flag) = pfound->val;
1055 return 0;
1057 return pfound->val;
1059 d->__nextchar = NULL;
1060 return 'W'; /* Let the application handle it. */
1062 if (temp[1] == ':')
1064 if (temp[2] == ':')
1066 /* This is an option that accepts an argument optionally. */
1067 if (*d->__nextchar != '\0')
1069 d->optarg = d->__nextchar;
1070 d->optind++;
1072 else
1073 d->optarg = NULL;
1074 d->__nextchar = NULL;
1076 else
1078 /* This is an option that requires an argument. */
1079 if (*d->__nextchar != '\0')
1081 d->optarg = d->__nextchar;
1082 /* If we end this ARGV-element by taking the rest as an arg,
1083 we must advance to the next element now. */
1084 d->optind++;
1086 else if (d->optind == argc)
1088 if (print_errors)
1090 /* 1003.2 specifies the format of this message. */
1091 #if defined _LIBC && defined USE_IN_LIBIO
1092 char *buf;
1094 if (__asprintf (&buf, _("\
1095 %s: option requires an argument -- %c\n"),
1096 argv[0], c) >= 0)
1098 _IO_flockfile (stderr);
1100 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1101 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1103 if (_IO_fwide (stderr, 0) > 0)
1104 __fwprintf (stderr, L"%s", buf);
1105 else
1106 fputs (buf, stderr);
1108 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1109 _IO_funlockfile (stderr);
1111 free (buf);
1113 #else
1114 fprintf (stderr,
1115 _("%s: option requires an argument -- %c\n"),
1116 argv[0], c);
1117 #endif
1119 d->optopt = c;
1120 if (optstring[0] == ':')
1121 c = ':';
1122 else
1123 c = '?';
1125 else
1126 /* We already incremented `optind' once;
1127 increment it again when taking next ARGV-elt as argument. */
1128 d->optarg = argv[d->optind++];
1129 d->__nextchar = NULL;
1132 return c;
1137 _getopt_internal (int argc, char **argv, const char *optstring,
1138 const struct option *longopts, int *longind,
1139 int long_only, int posixly_correct)
1141 int result;
1143 getopt_data.optind = optind;
1144 getopt_data.opterr = opterr;
1146 result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1147 long_only, posixly_correct, &getopt_data);
1149 optind = getopt_data.optind;
1150 optarg = getopt_data.optarg;
1151 optopt = getopt_data.optopt;
1153 return result;
1156 /* glibc gets a LSB-compliant getopt.
1157 Standalone applications get a POSIX-compliant getopt. */
1158 #if _LIBC
1159 enum { POSIXLY_CORRECT = 0 };
1160 #else
1161 enum { POSIXLY_CORRECT = 1 };
1162 #endif
1165 getopt (int argc, char *const *argv, const char *optstring)
1167 return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
1168 POSIXLY_CORRECT);
1171 #ifdef TEST
1173 /* Compile with -DTEST to make an executable for use in testing
1174 the above definition of `getopt'. */
1177 main (int argc, char **argv)
1179 int c;
1180 int digit_optind = 0;
1182 while (1)
1184 int this_option_optind = optind ? optind : 1;
1186 c = getopt (argc, argv, "abc:d:0123456789");
1187 if (c == -1)
1188 break;
1190 switch (c)
1192 case '0':
1193 case '1':
1194 case '2':
1195 case '3':
1196 case '4':
1197 case '5':
1198 case '6':
1199 case '7':
1200 case '8':
1201 case '9':
1202 if (digit_optind != 0 && digit_optind != this_option_optind)
1203 printf ("digits occur in two different argv-elements.\n");
1204 digit_optind = this_option_optind;
1205 printf ("option %c\n", c);
1206 break;
1208 case 'a':
1209 printf ("option a\n");
1210 break;
1212 case 'b':
1213 printf ("option b\n");
1214 break;
1216 case 'c':
1217 printf ("option c with value `%s'\n", optarg);
1218 break;
1220 case '?':
1221 break;
1223 default:
1224 printf ("?? getopt returned character code 0%o ??\n", c);
1228 if (optind < argc)
1230 printf ("non-option ARGV-elements: ");
1231 while (optind < argc)
1232 printf ("%s ", argv[optind++]);
1233 printf ("\n");
1236 exit (0);
1239 #endif /* TEST */