Debian Codegraph @

src/compat/pgetopt.cpp

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