blob: f4c11309290defc5e523c8add82d8d7683af1d4e [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* 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.
8
9 The GNU C Library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 The GNU C Library 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 GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with the GNU C Library; if not, write to the Free
21 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 02111-1307 USA. */
23
24/*
25 * Modified for uClibc by Manuel Novoa III on 1/5/01.
26 * Modified once again for uClibc by Erik Andersen 8/7/02
27 */
28
29/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
30 Ditto for AIX 3.2 and <stdlib.h>. */
31#ifndef _NO_PROTO
32# define _NO_PROTO
33#endif
34
35#ifdef HAVE_CONFIG_H
36# include <config.h>
37#endif
38
39#define __FORCE_GLIBC
40#include <features.h>
41
42#include <stdio.h>
43
44/* Comment out all this code if we are using the GNU C Library, and are not
45 actually compiling the library itself. This code is part of the GNU C
46 Library, but also included in many other GNU distributions. Compiling
47 and linking in this code is a waste when using the GNU C library
48 (especially if it is a shared library). Rather than having every GNU
49 program understand `configure --with-gnu-libc' and omit the object files,
50 it is simpler to just do this in the source for each such file. */
51
52#define GETOPT_INTERFACE_VERSION 2
53#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
54# include <gnu-versions.h>
55# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
56# define ELIDE_CODE
57# endif
58#endif
59
60#ifndef ELIDE_CODE
61
62
63/* This needs to come after some library #include
64 to get __GNU_LIBRARY__ defined. */
65#ifdef __GNU_LIBRARY__
66/* Don't include stdlib.h for non-GNU C libraries because some of them
67 contain conflicting prototypes for getopt. */
68# include <stdlib.h>
69# include <unistd.h>
70#endif /* GNU C library. */
71
72#include <string.h>
73
74#ifdef VMS
75# include <unixlib.h>
76#endif
77
78#if !defined __UCLIBC__ && !defined __UCLIBC_HAS_GETTEXT_AWARENESS__
79#ifdef _LIBC
80# include <libintl.h>
81#else
82# include "gettext.h"
83# define _(msgid) gettext (msgid)
84#endif
85#else
86#ifdef __UCLIBC_MJN3_ONLY__
87#warning TODO: Enable gettext awareness.
88#endif /* __UCLIBC_MJN3_ONLY__ */
89
90#undef _
91#define _(X) X
92
93#endif
94
95/* Treat '-W foo' the same as the long option '--foo',
96 * disabled for the moment since it costs about 2k... */
97#undef SPECIAL_TREATMENT_FOR_W
98
99#if defined _LIBC && defined USE_IN_LIBIO
100# include <wchar.h>
101#endif
102
103#ifndef attribute_hidden
104# define attribute_hidden
105#endif
106
107/* This version of `getopt' appears to the caller like standard Unix `getopt'
108 but it behaves differently for the user, since it allows the user
109 to intersperse the options with the other arguments.
110
111 As `getopt' works, it permutes the elements of ARGV so that,
112 when it is done, all the options precede everything else. Thus
113 all application programs are extended to handle flexible argument order.
114
115 Setting the environment variable POSIXLY_CORRECT disables permutation.
116 Then the behavior is completely standard.
117
118 GNU application programs can use a third alternative mode in which
119 they can distinguish the relative order of options and other arguments. */
120
121#include <getopt.h>
122#include "getopt_int.h"
123
124
125/* For communication from `getopt' to the caller.
126 When `getopt' finds an option that takes an argument,
127 the argument value is returned here.
128 Also, when `ordering' is RETURN_IN_ORDER,
129 each non-option ARGV-element is returned here. */
130
131char *optarg;
132
133/* Index in ARGV of the next element to be scanned.
134 This is used for communication to and from the caller
135 and for communication between successive calls to `getopt'.
136
137 On entry to `getopt', zero means this is the first call; initialize.
138
139 When `getopt' returns -1, this is the index of the first of the
140 non-option elements that the caller should itself scan.
141
142 Otherwise, `optind' communicates from one call to the next
143 how much of ARGV has been scanned so far. */
144
145/* 1003.2 says this must be 1 before any call. */
146int optind = 1;
147
148/* Callers store zero here to inhibit the error message
149 for unrecognized options. */
150
151int opterr = 1;
152
153/* Set to an option character which was unrecognized.
154 This must be initialized on some systems to avoid linking in the
155 system's own getopt implementation. */
156
157int optopt = '?';
158
159/* Keep a global copy of all internal members of getopt_data. */
160
161static struct _getopt_data getopt_data;
162
163
164#ifndef __GNU_LIBRARY__
165
166/* Avoid depending on library functions or files
167 whose names are inconsistent. */
168
169#ifndef getenv
170extern char *getenv ();
171#endif
172
173#endif /* not __GNU_LIBRARY__ */
174
175#ifdef _LIBC
176/* Stored original parameters.
177 XXX This is no good solution. We should rather copy the args so
178 that we can compare them later. But we must not use malloc(3). */
179# ifdef USE_NONOPTION_FLAGS
180extern int __libc_argc;
181extern char **__libc_argv;
182
183/* Bash 2.0 gives us an environment variable containing flags
184 indicating ARGV elements that should not be considered arguments. */
185
186/* Defined in getopt_init.c */
187extern char *__getopt_nonoption_flags;
188
189# define SWAP_FLAGS(ch1, ch2) \
190 if (d->__nonoption_flags_len > 0) \
191 { \
192 char __tmp = __getopt_nonoption_flags[ch1]; \
193 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
194 __getopt_nonoption_flags[ch2] = __tmp; \
195 }
196# else
197# define SWAP_FLAGS(ch1, ch2)
198# endif
199#else /* !_LIBC */
200# define SWAP_FLAGS(ch1, ch2)
201#endif /* _LIBC */
202
203/* Exchange two adjacent subsequences of ARGV.
204 One subsequence is elements [first_nonopt,last_nonopt)
205 which contains all the non-options that have been skipped so far.
206 The other is elements [last_nonopt,optind), which contains all
207 the options processed since those non-options were skipped.
208
209 `first_nonopt' and `last_nonopt' are relocated so that they describe
210 the new indices of the non-options in ARGV after they are moved. */
211
212static void
213exchange (char **argv, struct _getopt_data *d)
214{
215 int bottom = d->__first_nonopt;
216 int middle = d->__last_nonopt;
217 int top = d->optind;
218 char *tem;
219
220 /* Exchange the shorter segment with the far end of the longer segment.
221 That puts the shorter segment into the right place.
222 It leaves the longer segment in the right place overall,
223 but it consists of two parts that need to be swapped next. */
224
225#if defined _LIBC && defined USE_NONOPTION_FLAGS
226 /* First make sure the handling of the `__getopt_nonoption_flags'
227 string can work normally. Our top argument must be in the range
228 of the string. */
229 if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
230 {
231 /* We must extend the array. The user plays games with us and
232 presents new arguments. */
233 char *new_str = malloc (top + 1);
234 if (new_str == NULL)
235 d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
236 else
237 {
238 memset (__mempcpy (new_str, __getopt_nonoption_flags,
239 d->__nonoption_flags_max_len),
240 '\0', top + 1 - d->__nonoption_flags_max_len);
241 d->__nonoption_flags_max_len = top + 1;
242 __getopt_nonoption_flags = new_str;
243 }
244 }
245#endif
246
247 while (top > middle && middle > bottom)
248 {
249 if (top - middle > middle - bottom)
250 {
251 /* Bottom segment is the short one. */
252 int len = middle - bottom;
253 register int i;
254
255 /* Swap it with the top part of the top segment. */
256 for (i = 0; i < len; i++)
257 {
258 tem = argv[bottom + i];
259 argv[bottom + i] = argv[top - (middle - bottom) + i];
260 argv[top - (middle - bottom) + i] = tem;
261 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
262 }
263 /* Exclude the moved bottom segment from further swapping. */
264 top -= len;
265 }
266 else
267 {
268 /* Top segment is the short one. */
269 int len = top - middle;
270 register int i;
271
272 /* Swap it with the bottom part of the bottom segment. */
273 for (i = 0; i < len; i++)
274 {
275 tem = argv[bottom + i];
276 argv[bottom + i] = argv[middle + i];
277 argv[middle + i] = tem;
278 SWAP_FLAGS (bottom + i, middle + i);
279 }
280 /* Exclude the moved top segment from further swapping. */
281 bottom += len;
282 }
283 }
284
285 /* Update records for the slots the non-options now occupy. */
286
287 d->__first_nonopt += (d->optind - d->__last_nonopt);
288 d->__last_nonopt = d->optind;
289}
290
291/* Initialize the internal data when the first call is made. */
292
293static const char *
294_getopt_initialize (attribute_unused int argc, attribute_unused char *const *argv, const char *optstring,
295 struct _getopt_data *d)
296{
297 /* Start processing options with ARGV-element 1 (since ARGV-element 0
298 is the program name); the sequence of previously skipped
299 non-option ARGV-elements is empty. */
300
301 d->__first_nonopt = d->__last_nonopt = d->optind;
302
303 d->__nextchar = NULL;
304
305 d->__posixly_correct = !!getenv ("POSIXLY_CORRECT");
306
307 /* Determine how to handle the ordering of options and nonoptions. */
308
309 if (optstring[0] == '-')
310 {
311 d->__ordering = RETURN_IN_ORDER;
312 ++optstring;
313 }
314 else if (optstring[0] == '+')
315 {
316 d->__ordering = REQUIRE_ORDER;
317 ++optstring;
318 }
319 else if (d->__posixly_correct)
320 d->__ordering = REQUIRE_ORDER;
321 else
322 d->__ordering = PERMUTE;
323
324#if defined _LIBC && defined USE_NONOPTION_FLAGS
325 if (!d->__posixly_correct
326 && argc == __libc_argc && argv == __libc_argv)
327 {
328 if (d->__nonoption_flags_max_len == 0)
329 {
330 if (__getopt_nonoption_flags == NULL
331 || __getopt_nonoption_flags[0] == '\0')
332 d->__nonoption_flags_max_len = -1;
333 else
334 {
335 const char *orig_str = __getopt_nonoption_flags;
336 int len = d->__nonoption_flags_max_len = strlen (orig_str);
337 if (d->__nonoption_flags_max_len < argc)
338 d->__nonoption_flags_max_len = argc;
339 __getopt_nonoption_flags =
340 (char *) malloc (d->__nonoption_flags_max_len);
341 if (__getopt_nonoption_flags == NULL)
342 d->__nonoption_flags_max_len = -1;
343 else
344 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
345 '\0', d->__nonoption_flags_max_len - len);
346 }
347 }
348 d->__nonoption_flags_len = d->__nonoption_flags_max_len;
349 }
350 else
351 d->__nonoption_flags_len = 0;
352#endif
353
354 return optstring;
355}
356
357/* Scan elements of ARGV (whose length is ARGC) for option characters
358 given in OPTSTRING.
359
360 If an element of ARGV starts with '-', and is not exactly "-" or "--",
361 then it is an option element. The characters of this element
362 (aside from the initial '-') are option characters. If `getopt'
363 is called repeatedly, it returns successively each of the option characters
364 from each of the option elements.
365
366 If `getopt' finds another option character, it returns that character,
367 updating `optind' and `nextchar' so that the next call to `getopt' can
368 resume the scan with the following option character or ARGV-element.
369
370 If there are no more option characters, `getopt' returns -1.
371 Then `optind' is the index in ARGV of the first ARGV-element
372 that is not an option. (The ARGV-elements have been permuted
373 so that those that are not options now come last.)
374
375 OPTSTRING is a string containing the legitimate option characters.
376 If an option character is seen that is not listed in OPTSTRING,
377 return '?' after printing an error message. If you set `opterr' to
378 zero, the error message is suppressed but we still return '?'.
379
380 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
381 so the following text in the same ARGV-element, or the text of the following
382 ARGV-element, is returned in `optarg'. Two colons mean an option that
383 wants an optional arg; if there is text in the current ARGV-element,
384 it is returned in `optarg', otherwise `optarg' is set to zero.
385
386 If OPTSTRING starts with `-' or `+', it requests different methods of
387 handling the non-option ARGV-elements.
388 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
389
390 Long-named options begin with `--' instead of `-'.
391 Their names may be abbreviated as long as the abbreviation is unique
392 or is an exact match for some defined option. If they have an
393 argument, it follows the option name in the same ARGV-element, separated
394 from the option name by a `=', or else the in next ARGV-element.
395 When `getopt' finds a long-named option, it returns 0 if that option's
396 `flag' field is nonzero, the value of the option's `val' field
397 if the `flag' field is zero.
398
399 The elements of ARGV aren't really const, because we permute them.
400 But we pretend they're const in the prototype to be compatible
401 with other systems.
402
403 LONGOPTS is a vector of `struct option' terminated by an
404 element containing a name which is zero.
405
406 LONGIND returns the index in LONGOPT of the long-named option found.
407 It is only valid when a long-named option has been found by the most
408 recent call.
409
410 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
411 long-named options. */
412
413static int
414_getopt_internal_r (int argc, char *const *argv, const char *optstring,
415 const struct option *longopts, int *longind,
416 int long_only, struct _getopt_data *d)
417{
418 int print_errors = d->opterr;
419 if (optstring[0] == ':')
420 print_errors = 0;
421
422 if (argc < 1)
423 return -1;
424
425 d->optarg = NULL;
426
427 if (d->optind == 0 || !d->__initialized)
428 {
429 if (d->optind == 0)
430 d->optind = 1; /* Don't scan ARGV[0], the program name. */
431 optstring = _getopt_initialize (argc, argv, optstring, d);
432 d->__initialized = 1;
433 }
434
435 /* Test whether ARGV[optind] points to a non-option argument.
436 Either it does not have option syntax, or there is an environment flag
437 from the shell indicating it is not an option. The later information
438 is only used when the used in the GNU libc. */
439#if defined _LIBC && defined USE_NONOPTION_FLAGS
440# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
441 || (d->optind < d->__nonoption_flags_len \
442 && __getopt_nonoption_flags[d->optind] == '1'))
443#else
444# define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
445#endif
446
447 if (d->__nextchar == NULL || *d->__nextchar == '\0')
448 {
449 /* Advance to the next ARGV-element. */
450
451 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
452 moved back by the user (who may also have changed the arguments). */
453 if (d->__last_nonopt > d->optind)
454 d->__last_nonopt = d->optind;
455 if (d->__first_nonopt > d->optind)
456 d->__first_nonopt = d->optind;
457
458 if (d->__ordering == PERMUTE)
459 {
460 /* If we have just processed some options following some non-options,
461 exchange them so that the options come first. */
462
463 if (d->__first_nonopt != d->__last_nonopt
464 && d->__last_nonopt != d->optind)
465 exchange ((char **) argv, d);
466 else if (d->__last_nonopt != d->optind)
467 d->__first_nonopt = d->optind;
468
469 /* Skip any additional non-options
470 and extend the range of non-options previously skipped. */
471
472 while (d->optind < argc && NONOPTION_P)
473 d->optind++;
474 d->__last_nonopt = d->optind;
475 }
476
477 /* The special ARGV-element `--' means premature end of options.
478 Skip it like a null option,
479 then exchange with previous non-options as if it were an option,
480 then skip everything else like a non-option. */
481
482 if (d->optind != argc && !strcmp (argv[d->optind], "--"))
483 {
484 d->optind++;
485
486 if (d->__first_nonopt != d->__last_nonopt
487 && d->__last_nonopt != d->optind)
488 exchange ((char **) argv, d);
489 else if (d->__first_nonopt == d->__last_nonopt)
490 d->__first_nonopt = d->optind;
491 d->__last_nonopt = argc;
492
493 d->optind = argc;
494 }
495
496 /* If we have done all the ARGV-elements, stop the scan
497 and back over any non-options that we skipped and permuted. */
498
499 if (d->optind == argc)
500 {
501 /* Set the next-arg-index to point at the non-options
502 that we previously skipped, so the caller will digest them. */
503 if (d->__first_nonopt != d->__last_nonopt)
504 d->optind = d->__first_nonopt;
505 return -1;
506 }
507
508 /* If we have come to a non-option and did not permute it,
509 either stop the scan or describe it to the caller and pass it by. */
510
511 if (NONOPTION_P)
512 {
513 if (d->__ordering == REQUIRE_ORDER)
514 return -1;
515 d->optarg = argv[d->optind++];
516 return 1;
517 }
518
519 /* We have found another option-ARGV-element.
520 Skip the initial punctuation. */
521
522 d->__nextchar = (argv[d->optind] + 1
523 + (longopts != NULL && argv[d->optind][1] == '-'));
524 }
525
526 /* Decode the current option-ARGV-element. */
527
528 /* Check whether the ARGV-element is a long option.
529
530 If long_only and the ARGV-element has the form "-f", where f is
531 a valid short option, don't consider it an abbreviated form of
532 a long option that starts with f. Otherwise there would be no
533 way to give the -f short option.
534
535 On the other hand, if there's a long option "fubar" and
536 the ARGV-element is "-fu", do consider that an abbreviation of
537 the long option, just like "--fu", and not "-f" with arg "u".
538
539 This distinction seems to be the most useful approach. */
540
541 if (longopts != NULL
542 && (argv[d->optind][1] == '-'
543 || (long_only && (argv[d->optind][2]
544 || !strchr (optstring, argv[d->optind][1])))))
545 {
546 char *nameend;
547 const struct option *p;
548 const struct option *pfound = NULL;
549 int exact = 0;
550 int ambig = 0;
551 int indfound = -1;
552 int option_index;
553
554 for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
555 /* Do nothing. */ ;
556
557 /* Test all long options for either exact match
558 or abbreviated matches. */
559 for (p = longopts, option_index = 0; p->name; p++, option_index++)
560 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
561 {
562 if ((unsigned int) (nameend - d->__nextchar)
563 == (unsigned int) strlen (p->name))
564 {
565 /* Exact match found. */
566 pfound = p;
567 indfound = option_index;
568 exact = 1;
569 break;
570 }
571 else if (pfound == NULL)
572 {
573 /* First nonexact match found. */
574 pfound = p;
575 indfound = option_index;
576 }
577 else if (long_only
578 || pfound->has_arg != p->has_arg
579 || pfound->flag != p->flag
580 || pfound->val != p->val)
581 /* Second or later nonexact match found. */
582 ambig = 1;
583 }
584
585 if (ambig && !exact)
586 {
587 if (print_errors)
588 {
589#if defined _LIBC && defined USE_IN_LIBIO
590 char *buf;
591
592 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
593 argv[0], argv[d->optind]) >= 0)
594 {
595 _IO_flockfile (stderr);
596
597 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
598 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
599
600 __fxprintf (NULL, "%s", buf);
601
602 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
603 _IO_funlockfile (stderr);
604
605 free (buf);
606 }
607#else
608 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
609 argv[0], argv[d->optind]);
610#endif
611 }
612 d->__nextchar += strlen (d->__nextchar);
613 d->optind++;
614 d->optopt = 0;
615 return '?';
616 }
617
618 if (pfound != NULL)
619 {
620 option_index = indfound;
621 d->optind++;
622 if (*nameend)
623 {
624 /* Don't test has_arg with >, because some C compilers don't
625 allow it to be used on enums. */
626 if (pfound->has_arg)
627 d->optarg = nameend + 1;
628 else
629 {
630 if (print_errors)
631 {
632#if defined _LIBC && defined USE_IN_LIBIO
633 char *buf;
634 int n;
635#endif
636
637 if (argv[d->optind - 1][1] == '-')
638 {
639 /* --option */
640#if defined _LIBC && defined USE_IN_LIBIO
641 n = __asprintf (&buf, _("\
642%s: option `--%s' doesn't allow an argument\n"),
643 argv[0], pfound->name);
644#else
645 fprintf (stderr, _("\
646%s: option `--%s' doesn't allow an argument\n"),
647 argv[0], pfound->name);
648#endif
649 }
650 else
651 {
652 /* +option or -option */
653#if defined _LIBC && defined USE_IN_LIBIO
654 n = __asprintf (&buf, _("\
655%s: option `%c%s' doesn't allow an argument\n"),
656 argv[0], argv[d->optind - 1][0],
657 pfound->name);
658#else
659 fprintf (stderr, _("\
660%s: option `%c%s' doesn't allow an argument\n"),
661 argv[0], argv[d->optind - 1][0],
662 pfound->name);
663#endif
664 }
665
666#if defined _LIBC && defined USE_IN_LIBIO
667 if (n >= 0)
668 {
669 _IO_flockfile (stderr);
670
671 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
672 ((_IO_FILE *) stderr)->_flags2
673 |= _IO_FLAGS2_NOTCANCEL;
674
675 __fxprintf (NULL, "%s", buf);
676
677 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
678 _IO_funlockfile (stderr);
679
680 free (buf);
681 }
682#endif
683 }
684
685 d->__nextchar += strlen (d->__nextchar);
686
687 d->optopt = pfound->val;
688 return '?';
689 }
690 }
691 else if (pfound->has_arg == 1)
692 {
693 if (d->optind < argc)
694 d->optarg = argv[d->optind++];
695 else
696 {
697 if (print_errors)
698 {
699#if defined _LIBC && defined USE_IN_LIBIO
700 char *buf;
701
702 if (__asprintf (&buf, _("\
703%s: option `%s' requires an argument\n"),
704 argv[0], argv[d->optind - 1]) >= 0)
705 {
706 _IO_flockfile (stderr);
707
708 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
709 ((_IO_FILE *) stderr)->_flags2
710 |= _IO_FLAGS2_NOTCANCEL;
711
712 __fxprintf (NULL, "%s", buf);
713
714 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
715 _IO_funlockfile (stderr);
716
717 free (buf);
718 }
719#else
720 fprintf (stderr,
721 _("%s: option `%s' requires an argument\n"),
722 argv[0], argv[d->optind - 1]);
723#endif
724 }
725 d->__nextchar += strlen (d->__nextchar);
726 d->optopt = pfound->val;
727 return optstring[0] == ':' ? ':' : '?';
728 }
729 }
730 d->__nextchar += strlen (d->__nextchar);
731 if (longind != NULL)
732 *longind = option_index;
733 if (pfound->flag)
734 {
735 *(pfound->flag) = pfound->val;
736 return 0;
737 }
738 return pfound->val;
739 }
740
741 /* Can't find it as a long option. If this is not getopt_long_only,
742 or the option starts with '--' or is not a valid short
743 option, then it's an error.
744 Otherwise interpret it as a short option. */
745 if (!long_only || argv[d->optind][1] == '-'
746 || strchr (optstring, *d->__nextchar) == NULL)
747 {
748 if (print_errors)
749 {
750#if defined _LIBC && defined USE_IN_LIBIO
751 char *buf;
752 int n;
753#endif
754
755 if (argv[d->optind][1] == '-')
756 {
757 /* --option */
758#if defined _LIBC && defined USE_IN_LIBIO
759 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
760 argv[0], d->__nextchar);
761#else
762 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
763 argv[0], d->__nextchar);
764#endif
765 }
766 else
767 {
768 /* +option or -option */
769#if defined _LIBC && defined USE_IN_LIBIO
770 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
771 argv[0], argv[d->optind][0], d->__nextchar);
772#else
773 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
774 argv[0], argv[d->optind][0], d->__nextchar);
775#endif
776 }
777
778#if defined _LIBC && defined USE_IN_LIBIO
779 if (n >= 0)
780 {
781 _IO_flockfile (stderr);
782
783 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
784 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
785
786 __fxprintf (NULL, "%s", buf);
787
788 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
789 _IO_funlockfile (stderr);
790
791 free (buf);
792 }
793#endif
794 }
795 d->__nextchar = (char *) "";
796 d->optind++;
797 d->optopt = 0;
798 return '?';
799 }
800 }
801
802 /* Look at and handle the next short option-character. */
803
804 {
805 char c = *d->__nextchar++;
806 char *temp = strchr (optstring, c);
807
808 /* Increment `optind' when we start to process its last character. */
809 if (*d->__nextchar == '\0')
810 ++d->optind;
811
812 if (temp == NULL || c == ':')
813 {
814 if (print_errors)
815 {
816#if defined _LIBC && defined USE_IN_LIBIO
817 char *buf;
818 int n;
819#endif
820
821 if (d->__posixly_correct)
822 {
823 /* 1003.2 specifies the format of this message. */
824#if defined _LIBC && defined USE_IN_LIBIO
825 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
826 argv[0], c);
827#else
828 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
829#endif
830 }
831 else
832 {
833#if defined _LIBC && defined USE_IN_LIBIO
834 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
835 argv[0], c);
836#else
837 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
838#endif
839 }
840
841#if defined _LIBC && defined USE_IN_LIBIO
842 if (n >= 0)
843 {
844 _IO_flockfile (stderr);
845
846 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
847 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
848
849 __fxprintf (NULL, "%s", buf);
850
851 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
852 _IO_funlockfile (stderr);
853
854 free (buf);
855 }
856#endif
857 }
858 d->optopt = c;
859 return '?';
860 }
861#ifdef SPECIAL_TREATMENT_FOR_W
862 /* Convenience. Treat POSIX -W foo same as long option --foo */
863 if (temp[0] == 'W' && temp[1] == ';')
864 {
865 char *nameend;
866 const struct option *p;
867 const struct option *pfound = NULL;
868 int exact = 0;
869 int ambig = 0;
870 int indfound = 0;
871 int option_index;
872
873 /* This is an option that requires an argument. */
874 if (*d->__nextchar != '\0')
875 {
876 d->optarg = d->__nextchar;
877 /* If we end this ARGV-element by taking the rest as an arg,
878 we must advance to the next element now. */
879 d->optind++;
880 }
881 else if (d->optind == argc)
882 {
883 if (print_errors)
884 {
885 /* 1003.2 specifies the format of this message. */
886#if defined _LIBC && defined USE_IN_LIBIO
887 char *buf;
888
889 if (__asprintf (&buf,
890 _("%s: option requires an argument -- %c\n"),
891 argv[0], c) >= 0)
892 {
893 _IO_flockfile (stderr);
894
895 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
896 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
897
898 __fxprintf (NULL, "%s", buf);
899
900 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
901 _IO_funlockfile (stderr);
902
903 free (buf);
904 }
905#else
906 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
907 argv[0], c);
908#endif
909 }
910 d->optopt = c;
911 if (optstring[0] == ':')
912 c = ':';
913 else
914 c = '?';
915 return c;
916 }
917 else
918 /* We already incremented `d->optind' once;
919 increment it again when taking next ARGV-elt as argument. */
920 d->optarg = argv[d->optind++];
921
922 /* optarg is now the argument, see if it's in the
923 table of longopts. */
924
925 for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
926 nameend++)
927 /* Do nothing. */ ;
928
929 /* Test all long options for either exact match
930 or abbreviated matches. */
931 for (p = longopts, option_index = 0; p->name; p++, option_index++)
932 if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
933 {
934 if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
935 {
936 /* Exact match found. */
937 pfound = p;
938 indfound = option_index;
939 exact = 1;
940 break;
941 }
942 else if (pfound == NULL)
943 {
944 /* First nonexact match found. */
945 pfound = p;
946 indfound = option_index;
947 }
948 else
949 /* Second or later nonexact match found. */
950 ambig = 1;
951 }
952 if (ambig && !exact)
953 {
954 if (print_errors)
955 {
956#if defined _LIBC && defined USE_IN_LIBIO
957 char *buf;
958
959 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
960 argv[0], argv[d->optind]) >= 0)
961 {
962 _IO_flockfile (stderr);
963
964 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
965 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
966
967 __fxprintf (NULL, "%s", buf);
968
969 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
970 _IO_funlockfile (stderr);
971
972 free (buf);
973 }
974#else
975 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
976 argv[0], argv[d->optind]);
977#endif
978 }
979 d->__nextchar += strlen (d->__nextchar);
980 d->optind++;
981 return '?';
982 }
983 if (pfound != NULL)
984 {
985 option_index = indfound;
986 if (*nameend)
987 {
988 /* Don't test has_arg with >, because some C compilers don't
989 allow it to be used on enums. */
990 if (pfound->has_arg)
991 d->optarg = nameend + 1;
992 else
993 {
994 if (print_errors)
995 {
996#if defined _LIBC && defined USE_IN_LIBIO
997 char *buf;
998
999 if (__asprintf (&buf, _("\
1000%s: option `-W %s' doesn't allow an argument\n"),
1001 argv[0], pfound->name) >= 0)
1002 {
1003 _IO_flockfile (stderr);
1004
1005 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1006 ((_IO_FILE *) stderr)->_flags2
1007 |= _IO_FLAGS2_NOTCANCEL;
1008
1009 __fxprintf (NULL, "%s", buf);
1010
1011 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1012 _IO_funlockfile (stderr);
1013
1014 free (buf);
1015 }
1016#else
1017 fprintf (stderr, _("\
1018%s: option `-W %s' doesn't allow an argument\n"),
1019 argv[0], pfound->name);
1020#endif
1021 }
1022
1023 d->__nextchar += strlen (d->__nextchar);
1024 return '?';
1025 }
1026 }
1027 else if (pfound->has_arg == 1)
1028 {
1029 if (d->optind < argc)
1030 d->optarg = argv[d->optind++];
1031 else
1032 {
1033 if (print_errors)
1034 {
1035#if defined _LIBC && defined USE_IN_LIBIO
1036 char *buf;
1037
1038 if (__asprintf (&buf, _("\
1039%s: option `%s' requires an argument\n"),
1040 argv[0], argv[d->optind - 1]) >= 0)
1041 {
1042 _IO_flockfile (stderr);
1043
1044 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1045 ((_IO_FILE *) stderr)->_flags2
1046 |= _IO_FLAGS2_NOTCANCEL;
1047
1048 __fxprintf (NULL, "%s", buf);
1049
1050 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1051 _IO_funlockfile (stderr);
1052
1053 free (buf);
1054 }
1055#else
1056 fprintf (stderr,
1057 _("%s: option `%s' requires an argument\n"),
1058 argv[0], argv[d->optind - 1]);
1059#endif
1060 }
1061 d->__nextchar += strlen (d->__nextchar);
1062 return optstring[0] == ':' ? ':' : '?';
1063 }
1064 }
1065 d->__nextchar += strlen (d->__nextchar);
1066 if (longind != NULL)
1067 *longind = option_index;
1068 if (pfound->flag)
1069 {
1070 *(pfound->flag) = pfound->val;
1071 return 0;
1072 }
1073 return pfound->val;
1074 }
1075 d->__nextchar = NULL;
1076 return 'W'; /* Let the application handle it. */
1077 }
1078#endif
1079 if (temp[1] == ':')
1080 {
1081 if (temp[2] == ':')
1082 {
1083 /* This is an option that accepts an argument optionally. */
1084 if (*d->__nextchar != '\0')
1085 {
1086 d->optarg = d->__nextchar;
1087 d->optind++;
1088 }
1089 else
1090 d->optarg = NULL;
1091 d->__nextchar = NULL;
1092 }
1093 else
1094 {
1095 /* This is an option that requires an argument. */
1096 if (*d->__nextchar != '\0')
1097 {
1098 d->optarg = d->__nextchar;
1099 /* If we end this ARGV-element by taking the rest as an arg,
1100 we must advance to the next element now. */
1101 d->optind++;
1102 }
1103 else if (d->optind == argc)
1104 {
1105 if (print_errors)
1106 {
1107 /* 1003.2 specifies the format of this message. */
1108#if defined _LIBC && defined USE_IN_LIBIO
1109 char *buf;
1110
1111 if (__asprintf (&buf, _("\
1112%s: option requires an argument -- %c\n"),
1113 argv[0], c) >= 0)
1114 {
1115 _IO_flockfile (stderr);
1116
1117 int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1118 ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1119
1120 __fxprintf (NULL, "%s", buf);
1121
1122 ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1123 _IO_funlockfile (stderr);
1124
1125 free (buf);
1126 }
1127#else
1128 fprintf (stderr,
1129 _("%s: option requires an argument -- %c\n"),
1130 argv[0], c);
1131#endif
1132 }
1133 d->optopt = c;
1134 if (optstring[0] == ':')
1135 c = ':';
1136 else
1137 c = '?';
1138 }
1139 else
1140 /* We already incremented `optind' once;
1141 increment it again when taking next ARGV-elt as argument. */
1142 d->optarg = argv[d->optind++];
1143 d->__nextchar = NULL;
1144 }
1145 }
1146 return c;
1147 }
1148}
1149
1150int
1151_getopt_internal (int argc, char *const *argv, const char *optstring,
1152 const struct option *longopts, int *longind, int long_only)
1153{
1154 int result;
1155
1156 getopt_data.optind = optind;
1157 getopt_data.opterr = opterr;
1158
1159 result = _getopt_internal_r (argc, argv, optstring, longopts,
1160 longind, long_only, &getopt_data);
1161
1162 optind = getopt_data.optind;
1163 optarg = getopt_data.optarg;
1164 optopt = getopt_data.optopt;
1165
1166 return result;
1167}
1168
1169int
1170getopt (int argc, char *const *argv, const char *optstring)
1171{
1172 return _getopt_internal (argc, argv, optstring,
1173 (const struct option *) 0,
1174 (int *) 0,
1175 0);
1176}
1177libc_hidden_def(getopt)
1178
1179int
1180getopt_long (int argc, char *const *argv, const char *options,
1181 const struct option *long_options, int *opt_index)
1182{
1183 return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
1184}
1185
1186/* Like getopt_long, but '-' as well as '--' can indicate a long option.
1187 If an option that starts with '-' (not '--') doesn't match a long option,
1188 but does match a short option, it is parsed as a short option
1189 instead. */
1190
1191int
1192getopt_long_only (int argc, char *const *argv, const char *options,
1193 const struct option *long_options, int *opt_index)
1194{
1195 return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
1196}
1197
1198#endif /* Not ELIDE_CODE. */