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