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