VirtualBox

source: kBuild/trunk/src/kmk/getopt.c@ 3387

Last change on this file since 3387 was 3140, checked in by bird, 7 years ago

kmk: Merged in changes from GNU make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6 / https://git.savannah.gnu.org/git/make.git).

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette