riched20: Handle EM_FINDTEXTEX's unicode conversion in the host.
[wine/zf.git] / libs / port / getopt.c
blob686ebfc4f1a5dd541d63becbd6ca7ef33074e729
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 drepper@gnu.org
4 before changing it!
5 Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002
6 Free Software Foundation, Inc.
7 This file is part of the GNU C Library.
9 The GNU C Library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
14 The GNU C Library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with the GNU C Library; if not, write to the Free
21 Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
22 MA 02110-1301, USA */
24 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
25 Ditto for AIX 3.2 and <stdlib.h>. */
26 #ifndef _NO_PROTO
27 # define _NO_PROTO
28 #endif
30 #include "config.h"
31 #include "wine/port.h"
33 #ifndef HAVE_GETOPT_LONG_ONLY
35 #include <stdio.h>
36 #include <stdlib.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #ifdef HAVE_GETOPT_H
41 # include <getopt.h>
42 #endif
44 #ifndef _
45 /* This is for other GNU distributions with internationalized messages. */
46 # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
47 # include <libintl.h>
48 # ifndef _
49 # define _(msgid) gettext (msgid)
50 # endif
51 # else
52 # define _(msgid) (msgid)
53 # endif
54 # if defined _LIBC && defined USE_IN_LIBIO
55 # include <wchar.h>
56 # endif
57 #endif
59 #ifndef attribute_hidden
60 # define attribute_hidden
61 #endif
63 /* This version of `getopt' appears to the caller like standard Unix `getopt'
64 but it behaves differently for the user, since it allows the user
65 to intersperse the options with the other arguments.
67 As `getopt' works, it permutes the elements of ARGV so that,
68 when it is done, all the options precede everything else. Thus
69 all application programs are extended to handle flexible argument order.
71 Setting the environment variable POSIXLY_CORRECT disables permutation.
72 Then the behavior is completely standard.
74 GNU application programs can use a third alternative mode in which
75 they can distinguish the relative order of options and other arguments. */
77 /* For communication from `getopt' to the caller.
78 When `getopt' finds an option that takes an argument,
79 the argument value is returned here.
80 Also, when `ordering' is RETURN_IN_ORDER,
81 each non-option ARGV-element is returned here. */
83 char *optarg;
85 /* Index in ARGV of the next element to be scanned.
86 This is used for communication to and from the caller
87 and for communication between successive calls to `getopt'.
89 On entry to `getopt', zero means this is the first call; initialize.
91 When `getopt' returns -1, this is the index of the first of the
92 non-option elements that the caller should itself scan.
94 Otherwise, `optind' communicates from one call to the next
95 how much of ARGV has been scanned so far. */
97 /* 1003.2 says this must be 1 before any call. */
98 int optind = 1;
100 /* Formerly, initialization of getopt depended on optind==0, which
101 causes problems with re-calling getopt as programs generally don't
102 know that. */
104 int __getopt_initialized attribute_hidden;
106 /* The next char to be scanned in the option-element
107 in which the last option character we returned was found.
108 This allows us to pick up the scan where we left off.
110 If this is zero, or a null string, it means resume the scan
111 by advancing to the next ARGV-element. */
113 static char *nextchar;
115 /* Callers store zero here to inhibit the error message
116 for unrecognized options. */
118 int opterr = 1;
120 /* Set to an option character which was unrecognized.
121 This must be initialized on some systems to avoid linking in the
122 system's own getopt implementation. */
124 int optopt = '?';
126 /* Describe how to deal with options that follow non-option ARGV-elements.
128 If the caller did not specify anything,
129 the default is REQUIRE_ORDER if the environment variable
130 POSIXLY_CORRECT is defined, PERMUTE otherwise.
132 REQUIRE_ORDER means don't recognize them as options;
133 stop option processing when the first non-option is seen.
134 This is what Unix does.
135 This mode of operation is selected by either setting the environment
136 variable POSIXLY_CORRECT, or using `+' as the first character
137 of the list of option characters.
139 PERMUTE is the default. We permute the contents of ARGV as we scan,
140 so that eventually all the non-options are at the end. This allows options
141 to be given in any order, even with programs that were not written to
142 expect this.
144 RETURN_IN_ORDER is an option available to programs that were written
145 to expect options and other ARGV-elements in any order and that care about
146 the ordering of the two. We describe each non-option ARGV-element
147 as if it were the argument of an option with character code 1.
148 Using `-' as the first character of the list of option characters
149 selects this mode of operation.
151 The special argument `--' forces an end of option-scanning regardless
152 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
153 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
155 static enum
157 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
158 } ordering;
160 /* Value of POSIXLY_CORRECT environment variable. */
161 static char *posixly_correct;
163 #include <string.h>
164 #define my_index strchr
166 /* If using GCC, we can safely declare strlen this way.
167 If not using GCC, it is ok not to declare it. */
168 #ifdef __GNUC__
169 /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
170 That was relevant to code that was here before. */
171 # if (!defined __STDC__ || !__STDC__) && !defined strlen
172 /* gcc with -traditional declares the built-in strlen to return int,
173 and has done so at least since version 2.4.5. -- rms. */
174 extern int strlen (const char *);
175 # endif /* not __STDC__ */
176 #endif /* __GNUC__ */
178 /* Handle permutation of arguments. */
180 /* Describe the part of ARGV that contains non-options that have
181 been skipped. `first_nonopt' is the index in ARGV of the first of them;
182 `last_nonopt' is the index after the last of them. */
184 static int first_nonopt;
185 static int last_nonopt;
187 #ifdef _LIBC
188 /* Stored original parameters.
189 XXX This is no good solution. We should rather copy the args so
190 that we can compare them later. But we must not use malloc(3). */
191 extern int __libc_argc;
192 extern char **__libc_argv;
194 /* Bash 2.0 gives us an environment variable containing flags
195 indicating ARGV elements that should not be considered arguments. */
197 # ifdef USE_NONOPTION_FLAGS
198 /* Defined in getopt_init.c */
199 extern char *__getopt_nonoption_flags;
201 static int nonoption_flags_max_len;
202 static int nonoption_flags_len;
203 # endif
205 # ifdef USE_NONOPTION_FLAGS
206 # define SWAP_FLAGS(ch1, ch2) \
207 if (nonoption_flags_len > 0) \
209 char __tmp = __getopt_nonoption_flags[ch1]; \
210 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
211 __getopt_nonoption_flags[ch2] = __tmp; \
213 # else
214 # define SWAP_FLAGS(ch1, ch2)
215 # endif
216 #else /* !_LIBC */
217 # define SWAP_FLAGS(ch1, ch2)
218 #endif /* _LIBC */
220 /* Exchange two adjacent subsequences of ARGV.
221 One subsequence is elements [first_nonopt,last_nonopt)
222 which contains all the non-options that have been skipped so far.
223 The other is elements [last_nonopt,optind), which contains all
224 the options processed since those non-options were skipped.
226 `first_nonopt' and `last_nonopt' are relocated so that they describe
227 the new indices of the non-options in ARGV after they are moved. */
229 #if defined __STDC__ && __STDC__
230 static void exchange (char **);
231 #endif
233 static void
234 exchange (argv)
235 char **argv;
237 int bottom = first_nonopt;
238 int middle = last_nonopt;
239 int top = optind;
240 char *tem;
242 /* Exchange the shorter segment with the far end of the longer segment.
243 That puts the shorter segment into the right place.
244 It leaves the longer segment in the right place overall,
245 but it consists of two parts that need to be swapped next. */
247 #if defined _LIBC && defined USE_NONOPTION_FLAGS
248 /* First make sure the handling of the `__getopt_nonoption_flags'
249 string can work normally. Our top argument must be in the range
250 of the string. */
251 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
253 /* We must extend the array. The user plays games with us and
254 presents new arguments. */
255 char *new_str = malloc (top + 1);
256 if (new_str == NULL)
257 nonoption_flags_len = nonoption_flags_max_len = 0;
258 else
260 memset (__mempcpy (new_str, __getopt_nonoption_flags,
261 nonoption_flags_max_len),
262 '\0', top + 1 - nonoption_flags_max_len);
263 nonoption_flags_max_len = top + 1;
264 __getopt_nonoption_flags = new_str;
267 #endif
269 while (top > middle && middle > bottom)
271 if (top - middle > middle - bottom)
273 /* Bottom segment is the short one. */
274 int len = middle - bottom;
275 register int i;
277 /* Swap it with the top part of the top segment. */
278 for (i = 0; i < len; i++)
280 tem = argv[bottom + i];
281 argv[bottom + i] = argv[top - (middle - bottom) + i];
282 argv[top - (middle - bottom) + i] = tem;
283 SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
285 /* Exclude the moved bottom segment from further swapping. */
286 top -= len;
288 else
290 /* Top segment is the short one. */
291 int len = top - middle;
292 register int i;
294 /* Swap it with the bottom part of the bottom segment. */
295 for (i = 0; i < len; i++)
297 tem = argv[bottom + i];
298 argv[bottom + i] = argv[middle + i];
299 argv[middle + i] = tem;
300 SWAP_FLAGS (bottom + i, middle + i);
302 /* Exclude the moved top segment from further swapping. */
303 bottom += len;
307 /* Update records for the slots the non-options now occupy. */
309 first_nonopt += (optind - last_nonopt);
310 last_nonopt = optind;
313 /* Initialize the internal data when the first call is made. */
315 #if defined __STDC__ && __STDC__
316 static const char *_getopt_initialize (int, char *const *, const char *);
317 #endif
318 static const char *
319 _getopt_initialize (argc, argv, optstring)
320 int argc;
321 char *const *argv;
322 const char *optstring;
324 /* Start processing options with ARGV-element 1 (since ARGV-element 0
325 is the program name); the sequence of previously skipped
326 non-option ARGV-elements is empty. */
328 first_nonopt = last_nonopt = optind;
330 nextchar = NULL;
332 posixly_correct = getenv ("POSIXLY_CORRECT");
334 /* Determine how to handle the ordering of options and nonoptions. */
336 if (optstring[0] == '-')
338 ordering = RETURN_IN_ORDER;
339 ++optstring;
341 else if (optstring[0] == '+')
343 ordering = REQUIRE_ORDER;
344 ++optstring;
346 else if (posixly_correct != NULL)
347 ordering = REQUIRE_ORDER;
348 else
349 ordering = PERMUTE;
351 #if defined _LIBC && defined USE_NONOPTION_FLAGS
352 if (posixly_correct == NULL
353 && argc == __libc_argc && argv == __libc_argv)
355 if (nonoption_flags_max_len == 0)
357 if (__getopt_nonoption_flags == NULL
358 || __getopt_nonoption_flags[0] == '\0')
359 nonoption_flags_max_len = -1;
360 else
362 const char *orig_str = __getopt_nonoption_flags;
363 int len = nonoption_flags_max_len = strlen (orig_str);
364 if (nonoption_flags_max_len < argc)
365 nonoption_flags_max_len = argc;
366 __getopt_nonoption_flags = malloc (nonoption_flags_max_len);
367 if (__getopt_nonoption_flags == NULL)
368 nonoption_flags_max_len = -1;
369 else
370 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
371 '\0', nonoption_flags_max_len - len);
374 nonoption_flags_len = nonoption_flags_max_len;
376 else
377 nonoption_flags_len = 0;
378 #endif
380 return optstring;
383 /* Scan elements of ARGV (whose length is ARGC) for option characters
384 given in OPTSTRING.
386 If an element of ARGV starts with '-', and is not exactly "-" or "--",
387 then it is an option element. The characters of this element
388 (aside from the initial '-') are option characters. If `getopt'
389 is called repeatedly, it returns successively each of the option characters
390 from each of the option elements.
392 If `getopt' finds another option character, it returns that character,
393 updating `optind' and `nextchar' so that the next call to `getopt' can
394 resume the scan with the following option character or ARGV-element.
396 If there are no more option characters, `getopt' returns -1.
397 Then `optind' is the index in ARGV of the first ARGV-element
398 that is not an option. (The ARGV-elements have been permuted
399 so that those that are not options now come last.)
401 OPTSTRING is a string containing the legitimate option characters.
402 If an option character is seen that is not listed in OPTSTRING,
403 return '?' after printing an error message. If you set `opterr' to
404 zero, the error message is suppressed but we still return '?'.
406 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
407 so the following text in the same ARGV-element, or the text of the following
408 ARGV-element, is returned in `optarg'. Two colons mean an option that
409 wants an optional arg; if there is text in the current ARGV-element,
410 it is returned in `optarg', otherwise `optarg' is set to zero.
412 If OPTSTRING starts with `-' or `+', it requests different methods of
413 handling the non-option ARGV-elements.
414 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
416 Long-named options begin with `--' instead of `-'.
417 Their names may be abbreviated as long as the abbreviation is unique
418 or is an exact match for some defined option. If they have an
419 argument, it follows the option name in the same ARGV-element, separated
420 from the option name by a `=', or else the in next ARGV-element.
421 When `getopt' finds a long-named option, it returns 0 if that option's
422 `flag' field is nonzero, the value of the option's `val' field
423 if the `flag' field is zero.
425 The elements of ARGV aren't really const, because we permute them.
426 But we pretend they're const in the prototype to be compatible
427 with other systems.
429 LONGOPTS is a vector of `struct option' terminated by an
430 element containing a name which is zero.
432 LONGIND returns the index in LONGOPT of the long-named option found.
433 It is only valid when a long-named option has been found by the most
434 recent call.
436 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
437 long-named options. */
440 _getopt_internal( int argc, char * const *argv, const char *optstring, const struct option *longopts, int *longind, int long_only)
442 int print_errors = opterr;
443 if (optstring[0] == ':')
444 print_errors = 0;
446 if (argc < 1)
447 return -1;
449 optarg = NULL;
451 if (optind == 0 || !__getopt_initialized)
453 if (optind == 0)
454 optind = 1; /* Don't scan ARGV[0], the program name. */
455 optstring = _getopt_initialize (argc, argv, optstring);
456 __getopt_initialized = 1;
459 /* Test whether ARGV[optind] points to a non-option argument.
460 Either it does not have option syntax, or there is an environment flag
461 from the shell indicating it is not an option. The later information
462 is only used when the used in the GNU libc. */
463 #if defined _LIBC && defined USE_NONOPTION_FLAGS
464 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
465 || (optind < nonoption_flags_len \
466 && __getopt_nonoption_flags[optind] == '1'))
467 #else
468 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
469 #endif
471 if (nextchar == NULL || *nextchar == '\0')
473 /* Advance to the next ARGV-element. */
475 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
476 moved back by the user (who may also have changed the arguments). */
477 if (last_nonopt > optind)
478 last_nonopt = optind;
479 if (first_nonopt > optind)
480 first_nonopt = optind;
482 if (ordering == PERMUTE)
484 /* If we have just processed some options following some non-options,
485 exchange them so that the options come first. */
487 if (first_nonopt != last_nonopt && last_nonopt != optind)
488 exchange ((char **) argv);
489 else if (last_nonopt != optind)
490 first_nonopt = optind;
492 /* Skip any additional non-options
493 and extend the range of non-options previously skipped. */
495 while (optind < argc && NONOPTION_P)
496 optind++;
497 last_nonopt = optind;
500 /* The special ARGV-element `--' means premature end of options.
501 Skip it like a null option,
502 then exchange with previous non-options as if it were an option,
503 then skip everything else like a non-option. */
505 if (optind != argc && !strcmp (argv[optind], "--"))
507 optind++;
509 if (first_nonopt != last_nonopt && last_nonopt != optind)
510 exchange ((char **) argv);
511 else if (first_nonopt == last_nonopt)
512 first_nonopt = optind;
513 last_nonopt = argc;
515 optind = argc;
518 /* If we have done all the ARGV-elements, stop the scan
519 and back over any non-options that we skipped and permuted. */
521 if (optind == argc)
523 /* Set the next-arg-index to point at the non-options
524 that we previously skipped, so the caller will digest them. */
525 if (first_nonopt != last_nonopt)
526 optind = first_nonopt;
527 return -1;
530 /* If we have come to a non-option and did not permute it,
531 either stop the scan or describe it to the caller and pass it by. */
533 if (NONOPTION_P)
535 if (ordering == REQUIRE_ORDER)
536 return -1;
537 optarg = argv[optind++];
538 return 1;
541 /* We have found another option-ARGV-element.
542 Skip the initial punctuation. */
544 nextchar = (argv[optind] + 1
545 + (longopts != NULL && argv[optind][1] == '-'));
548 /* Decode the current option-ARGV-element. */
550 /* Check whether the ARGV-element is a long option.
552 If long_only and the ARGV-element has the form "-f", where f is
553 a valid short option, don't consider it an abbreviated form of
554 a long option that starts with f. Otherwise there would be no
555 way to give the -f short option.
557 On the other hand, if there's a long option "fubar" and
558 the ARGV-element is "-fu", do consider that an abbreviation of
559 the long option, just like "--fu", and not "-f" with arg "u".
561 This distinction seems to be the most useful approach. */
563 if (longopts != NULL
564 && (argv[optind][1] == '-'
565 || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
567 char *nameend;
568 const struct option *p;
569 const struct option *pfound = NULL;
570 int exact = 0;
571 int ambig = 0;
572 int indfound = -1;
573 int option_index;
575 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
576 /* Do nothing. */ ;
578 /* Test all long options for either exact match
579 or abbreviated matches. */
580 for (p = longopts, option_index = 0; p->name; p++, option_index++)
581 if (!strncmp (p->name, nextchar, nameend - nextchar))
583 if ((unsigned int) (nameend - nextchar)
584 == (unsigned int) strlen (p->name))
586 /* Exact match found. */
587 pfound = p;
588 indfound = option_index;
589 exact = 1;
590 break;
592 else if (pfound == NULL)
594 /* First nonexact match found. */
595 pfound = p;
596 indfound = option_index;
598 else if (long_only
599 || pfound->has_arg != p->has_arg
600 || pfound->flag != p->flag
601 || pfound->val != p->val)
602 /* Second or later nonexact match found. */
603 ambig = 1;
606 if (ambig && !exact)
608 if (print_errors)
610 #if defined _LIBC && defined USE_IN_LIBIO
611 char *buf;
613 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
614 argv[0], argv[optind]) >= 0)
617 if (_IO_fwide (stderr, 0) > 0)
618 __fwprintf (stderr, L"%s", buf);
619 else
620 fputs (buf, stderr);
622 free (buf);
624 #else
625 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
626 argv[0], argv[optind]);
627 #endif
629 nextchar += strlen (nextchar);
630 optind++;
631 optopt = 0;
632 return '?';
635 if (pfound != NULL)
637 option_index = indfound;
638 optind++;
639 if (*nameend)
641 /* Don't test has_arg with >, because some C compilers don't
642 allow it to be used on enums. */
643 if (pfound->has_arg)
644 optarg = nameend + 1;
645 else
647 if (print_errors)
649 #if defined _LIBC && defined USE_IN_LIBIO
650 char *buf;
651 int n;
652 #endif
654 if (argv[optind - 1][1] == '-')
656 /* --option */
657 #if defined _LIBC && defined USE_IN_LIBIO
658 n = __asprintf (&buf, _("\
659 %s: option `--%s' doesn't allow an argument\n"),
660 argv[0], pfound->name);
661 #else
662 fprintf (stderr, _("\
663 %s: option `--%s' doesn't allow an argument\n"),
664 argv[0], pfound->name);
665 #endif
667 else
669 /* +option or -option */
670 #if defined _LIBC && defined USE_IN_LIBIO
671 n = __asprintf (&buf, _("\
672 %s: option `%c%s' doesn't allow an argument\n"),
673 argv[0], argv[optind - 1][0],
674 pfound->name);
675 #else
676 fprintf (stderr, _("\
677 %s: option `%c%s' doesn't allow an argument\n"),
678 argv[0], argv[optind - 1][0], pfound->name);
679 #endif
682 #if defined _LIBC && defined USE_IN_LIBIO
683 if (n >= 0)
685 if (_IO_fwide (stderr, 0) > 0)
686 __fwprintf (stderr, L"%s", buf);
687 else
688 fputs (buf, stderr);
690 free (buf);
692 #endif
695 nextchar += strlen (nextchar);
697 optopt = pfound->val;
698 return '?';
701 else if (pfound->has_arg == 1)
703 if (optind < argc)
704 optarg = argv[optind++];
705 else
707 if (print_errors)
709 #if defined _LIBC && defined USE_IN_LIBIO
710 char *buf;
712 if (__asprintf (&buf, _("\
713 %s: option `%s' requires an argument\n"),
714 argv[0], argv[optind - 1]) >= 0)
716 if (_IO_fwide (stderr, 0) > 0)
717 __fwprintf (stderr, L"%s", buf);
718 else
719 fputs (buf, stderr);
721 free (buf);
723 #else
724 fprintf (stderr,
725 _("%s: option `%s' requires an argument\n"),
726 argv[0], argv[optind - 1]);
727 #endif
729 nextchar += strlen (nextchar);
730 optopt = pfound->val;
731 return optstring[0] == ':' ? ':' : '?';
734 nextchar += strlen (nextchar);
735 if (longind != NULL)
736 *longind = option_index;
737 if (pfound->flag)
739 *(pfound->flag) = pfound->val;
740 return 0;
742 return pfound->val;
745 /* Can't find it as a long option. If this is not getopt_long_only,
746 or the option starts with '--' or is not a valid short
747 option, then it's an error.
748 Otherwise interpret it as a short option. */
749 if (!long_only || argv[optind][1] == '-'
750 || my_index (optstring, *nextchar) == NULL)
752 if (print_errors)
754 #if defined _LIBC && defined USE_IN_LIBIO
755 char *buf;
756 int n;
757 #endif
759 if (argv[optind][1] == '-')
761 /* --option */
762 #if defined _LIBC && defined USE_IN_LIBIO
763 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
764 argv[0], nextchar);
765 #else
766 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
767 argv[0], nextchar);
768 #endif
770 else
772 /* +option or -option */
773 #if defined _LIBC && defined USE_IN_LIBIO
774 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
775 argv[0], argv[optind][0], nextchar);
776 #else
777 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
778 argv[0], argv[optind][0], nextchar);
779 #endif
782 #if defined _LIBC && defined USE_IN_LIBIO
783 if (n >= 0)
785 if (_IO_fwide (stderr, 0) > 0)
786 __fwprintf (stderr, L"%s", buf);
787 else
788 fputs (buf, stderr);
790 free (buf);
792 #endif
794 nextchar = (char *) "";
795 optind++;
796 optopt = 0;
797 return '?';
801 /* Look at and handle the next short option-character. */
804 char c = *nextchar++;
805 char *temp = my_index (optstring, c);
807 /* Increment `optind' when we start to process its last character. */
808 if (*nextchar == '\0')
809 ++optind;
811 if (temp == NULL || c == ':')
813 if (print_errors)
815 #if defined _LIBC && defined USE_IN_LIBIO
816 char *buf;
817 int n;
818 #endif
820 if (posixly_correct)
822 /* 1003.2 specifies the format of this message. */
823 #if defined _LIBC && defined USE_IN_LIBIO
824 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
825 argv[0], c);
826 #else
827 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
828 #endif
830 else
832 #if defined _LIBC && defined USE_IN_LIBIO
833 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
834 argv[0], c);
835 #else
836 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
837 #endif
840 #if defined _LIBC && defined USE_IN_LIBIO
841 if (n >= 0)
843 if (_IO_fwide (stderr, 0) > 0)
844 __fwprintf (stderr, L"%s", buf);
845 else
846 fputs (buf, stderr);
848 free (buf);
850 #endif
852 optopt = c;
853 return '?';
855 /* Convenience. Treat POSIX -W foo same as long option --foo */
856 if (temp[0] == 'W' && temp[1] == ';')
858 char *nameend;
859 const struct option *p;
860 const struct option *pfound = NULL;
861 int exact = 0;
862 int ambig = 0;
863 int indfound = 0;
864 int option_index;
866 /* This is an option that requires an argument. */
867 if (*nextchar != '\0')
869 optarg = nextchar;
870 /* If we end this ARGV-element by taking the rest as an arg,
871 we must advance to the next element now. */
872 optind++;
874 else if (optind == argc)
876 if (print_errors)
878 /* 1003.2 specifies the format of this message. */
879 #if defined _LIBC && defined USE_IN_LIBIO
880 char *buf;
882 if (__asprintf (&buf,
883 _("%s: option requires an argument -- %c\n"),
884 argv[0], c) >= 0)
886 if (_IO_fwide (stderr, 0) > 0)
887 __fwprintf (stderr, L"%s", buf);
888 else
889 fputs (buf, stderr);
891 free (buf);
893 #else
894 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
895 argv[0], c);
896 #endif
898 optopt = c;
899 if (optstring[0] == ':')
900 c = ':';
901 else
902 c = '?';
903 return c;
905 else
906 /* We already incremented `optind' once;
907 increment it again when taking next ARGV-elt as argument. */
908 optarg = argv[optind++];
910 /* optarg is now the argument, see if it's in the
911 table of longopts. */
913 for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
914 /* Do nothing. */ ;
916 /* Test all long options for either exact match
917 or abbreviated matches. */
918 for (p = longopts, option_index = 0; p->name; p++, option_index++)
919 if (!strncmp (p->name, nextchar, nameend - nextchar))
921 if ((unsigned int) (nameend - nextchar) == strlen (p->name))
923 /* Exact match found. */
924 pfound = p;
925 indfound = option_index;
926 exact = 1;
927 break;
929 else if (pfound == NULL)
931 /* First nonexact match found. */
932 pfound = p;
933 indfound = option_index;
935 else
936 /* Second or later nonexact match found. */
937 ambig = 1;
939 if (ambig && !exact)
941 if (print_errors)
943 #if defined _LIBC && defined USE_IN_LIBIO
944 char *buf;
946 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
947 argv[0], argv[optind]) >= 0)
949 if (_IO_fwide (stderr, 0) > 0)
950 __fwprintf (stderr, L"%s", buf);
951 else
952 fputs (buf, stderr);
954 free (buf);
956 #else
957 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
958 argv[0], argv[optind]);
959 #endif
961 nextchar += strlen (nextchar);
962 optind++;
963 return '?';
965 if (pfound != NULL)
967 option_index = indfound;
968 if (*nameend)
970 /* Don't test has_arg with >, because some C compilers don't
971 allow it to be used on enums. */
972 if (pfound->has_arg)
973 optarg = nameend + 1;
974 else
976 if (print_errors)
978 #if defined _LIBC && defined USE_IN_LIBIO
979 char *buf;
981 if (__asprintf (&buf, _("\
982 %s: option `-W %s' doesn't allow an argument\n"),
983 argv[0], pfound->name) >= 0)
985 if (_IO_fwide (stderr, 0) > 0)
986 __fwprintf (stderr, L"%s", buf);
987 else
988 fputs (buf, stderr);
990 free (buf);
992 #else
993 fprintf (stderr, _("\
994 %s: option `-W %s' doesn't allow an argument\n"),
995 argv[0], pfound->name);
996 #endif
999 nextchar += strlen (nextchar);
1000 return '?';
1003 else if (pfound->has_arg == 1)
1005 if (optind < argc)
1006 optarg = argv[optind++];
1007 else
1009 if (print_errors)
1011 #if defined _LIBC && defined USE_IN_LIBIO
1012 char *buf;
1014 if (__asprintf (&buf, _("\
1015 %s: option `%s' requires an argument\n"),
1016 argv[0], argv[optind - 1]) >= 0)
1018 if (_IO_fwide (stderr, 0) > 0)
1019 __fwprintf (stderr, L"%s", buf);
1020 else
1021 fputs (buf, stderr);
1023 free (buf);
1025 #else
1026 fprintf (stderr,
1027 _("%s: option `%s' requires an argument\n"),
1028 argv[0], argv[optind - 1]);
1029 #endif
1031 nextchar += strlen (nextchar);
1032 return optstring[0] == ':' ? ':' : '?';
1035 nextchar += strlen (nextchar);
1036 if (longind != NULL)
1037 *longind = option_index;
1038 if (pfound->flag)
1040 *(pfound->flag) = pfound->val;
1041 return 0;
1043 return pfound->val;
1045 nextchar = NULL;
1046 return 'W'; /* Let the application handle it. */
1048 if (temp[1] == ':')
1050 if (temp[2] == ':')
1052 /* This is an option that accepts an argument optionally. */
1053 if (*nextchar != '\0')
1055 optarg = nextchar;
1056 optind++;
1058 else
1059 optarg = NULL;
1060 nextchar = NULL;
1062 else
1064 /* This is an option that requires an argument. */
1065 if (*nextchar != '\0')
1067 optarg = nextchar;
1068 /* If we end this ARGV-element by taking the rest as an arg,
1069 we must advance to the next element now. */
1070 optind++;
1072 else if (optind == argc)
1074 if (print_errors)
1076 /* 1003.2 specifies the format of this message. */
1077 #if defined _LIBC && defined USE_IN_LIBIO
1078 char *buf;
1080 if (__asprintf (&buf, _("\
1081 %s: option requires an argument -- %c\n"),
1082 argv[0], c) >= 0)
1084 if (_IO_fwide (stderr, 0) > 0)
1085 __fwprintf (stderr, L"%s", buf);
1086 else
1087 fputs (buf, stderr);
1089 free (buf);
1091 #else
1092 fprintf (stderr,
1093 _("%s: option requires an argument -- %c\n"),
1094 argv[0], c);
1095 #endif
1097 optopt = c;
1098 if (optstring[0] == ':')
1099 c = ':';
1100 else
1101 c = '?';
1103 else
1104 /* We already incremented `optind' once;
1105 increment it again when taking next ARGV-elt as argument. */
1106 optarg = argv[optind++];
1107 nextchar = NULL;
1110 return c;
1115 getopt (int argc, char * const *argv, const char *optstring)
1117 return _getopt_internal (argc, argv, optstring,
1118 NULL,
1119 NULL,
1124 getopt_long (int argc, char * const *argv, const char *options, const struct option *long_options, int *opt_index)
1126 return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
1129 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
1130 If an option that starts with '-' (not '--') doesn't match a long option,
1131 but does match a short option, it is parsed as a short option
1132 instead. */
1135 getopt_long_only (int argc, char * const *argv, const char *options, const struct option *long_options, int *opt_index)
1137 return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
1140 #endif /* HAVE_GETOPT_LONG_ONLY */