Debian Codegraph @

converter/other/fiasco/getopt.c

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