2 * wgetopt.c: Wide-character versions of getopt, getopt_long, and
5 * This has been modified from the original, which is
6 * Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
7 * 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
8 * 2010 Free Software Foundation, Inc.
10 * This file is free software; you can redistribute it and/or modify it under
11 * the terms of the GNU General Public License as published by the Free Software
12 * Foundation; either version 3 of the License, or (at your option) any later
15 * This file is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along with
20 * this file; if not, see http://www.gnu.org/licenses/.
27 /* For communication from `getopt' to the caller.
28 When `getopt' finds an option that takes an argument,
29 the argument value is returned here.
30 Also, when `ordering' is RETURN_IN_ORDER,
31 each non-option ARGV-element is returned here. */
33 wchar_t *woptarg = NULL;
35 /* Index in ARGV of the next element to be scanned.
36 This is used for communication to and from the caller
37 and for communication between successive calls to `getopt'.
39 On entry to `getopt', zero means this is the first call; initialize.
41 When `getopt' returns -1, this is the index of the first of the
42 non-option elements that the caller should itself scan.
44 Otherwise, `woptind' communicates from one call to the next
45 how much of ARGV has been scanned so far. */
47 /* 1003.2 says this must be 1 before any call. */
50 /* Formerly, initialization of getopt depended on woptind==0, which
51 causes problems with re-calling getopt as programs generally don't
54 int __getopt_initialized = 0;
56 /* The next char to be scanned in the option-element
57 in which the last option character we returned was found.
58 This allows us to pick up the scan where we left off.
60 If this is zero, or a null string, it means resume the scan
61 by advancing to the next ARGV-element. */
63 static wchar_t *nextchar;
65 /* Callers store zero here to inhibit the error message
66 for unrecognized options. */
70 /* Set to an option character which was unrecognized.
71 This must be initialized on some systems to avoid linking in the
72 system's own getopt implementation. */
76 /* Describe how to deal with options that follow non-option ARGV-elements.
78 If the caller did not specify anything,
79 the default is REQUIRE_ORDER if the environment variable
80 POSIXLY_CORRECT is defined, PERMUTE otherwise.
82 REQUIRE_ORDER means don't recognize them as options;
83 stop option processing when the first non-option is seen.
84 This is what Unix does.
85 This mode of operation is selected by either setting the environment
86 variable POSIXLY_CORRECT, or using `+' as the first character
87 of the list of option characters.
89 PERMUTE is the default. We permute the contents of ARGV as we scan,
90 so that eventually all the non-options are at the end. This allows options
91 to be given in any order, even with programs that were not written to
94 RETURN_IN_ORDER is an option available to programs that were written
95 to expect options and other ARGV-elements in any order and that care about
96 the ordering of the two. We describe each non-option ARGV-element
97 as if it were the argument of an option with character code 1.
98 Using `-' as the first character of the list of option characters
99 selects this mode of operation.
101 The special argument `--' forces an end of option-scanning regardless
102 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
103 `--' can cause `getopt' to return -1 with `woptind' != ARGC. */
107 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
110 /* Value of POSIXLY_CORRECT environment variable. */
111 static char *posixly_correct;
114 /* Handle permutation of arguments. */
116 /* Describe the part of ARGV that contains non-options that have
117 been skipped. `first_nonopt' is the index in ARGV of the first of them;
118 `last_nonopt' is the index after the last of them. */
120 static int first_nonopt;
121 static int last_nonopt;
123 /* Exchange two adjacent subsequences of ARGV.
124 One subsequence is elements [first_nonopt,last_nonopt)
125 which contains all the non-options that have been skipped so far.
126 The other is elements [last_nonopt,woptind), which contains all
127 the options processed since those non-options were skipped.
129 `first_nonopt' and `last_nonopt' are relocated so that they describe
130 the new indices of the non-options in ARGV after they are moved. */
133 exchange (wchar_t **argv)
135 int bottom = first_nonopt;
136 int middle = last_nonopt;
140 /* Exchange the shorter segment with the far end of the longer segment.
141 That puts the shorter segment into the right place.
142 It leaves the longer segment in the right place overall,
143 but it consists of two parts that need to be swapped next. */
145 while (top > middle && middle > bottom)
147 if (top - middle > middle - bottom)
149 /* Bottom segment is the short one. */
150 int len = middle - bottom;
153 /* Swap it with the top part of the top segment. */
154 for (i = 0; i < len; i++)
156 tem = argv[bottom + i];
157 argv[bottom + i] = argv[top - (middle - bottom) + i];
158 argv[top - (middle - bottom) + i] = tem;
160 /* Exclude the moved bottom segment from further swapping. */
165 /* Top segment is the short one. */
166 int len = top - middle;
169 /* Swap it with the bottom part of the bottom segment. */
170 for (i = 0; i < len; i++)
172 tem = argv[bottom + i];
173 argv[bottom + i] = argv[middle + i];
174 argv[middle + i] = tem;
176 /* Exclude the moved top segment from further swapping. */
181 /* Update records for the slots the non-options now occupy. */
183 first_nonopt += (woptind - last_nonopt);
184 last_nonopt = woptind;
187 /* Initialize the internal data when the first call is made. */
189 static const wchar_t *
190 _getopt_initialize (int argc, wchar_t *const *argv, const wchar_t *optstring)
192 /* Start processing options with ARGV-element 1 (since ARGV-element 0
193 is the program name); the sequence of previously skipped
194 non-option ARGV-elements is empty. */
196 first_nonopt = last_nonopt = woptind;
200 posixly_correct = getenv ("POSIXLY_CORRECT");
202 /* Determine how to handle the ordering of options and nonoptions. */
204 if (optstring[0] == L'-')
206 ordering = RETURN_IN_ORDER;
209 else if (optstring[0] == L'+')
211 ordering = REQUIRE_ORDER;
214 else if (posixly_correct != NULL)
215 ordering = REQUIRE_ORDER;
222 /* Scan elements of ARGV (whose length is ARGC) for option characters
225 If an element of ARGV starts with '-', and is not exactly "-" or "--",
226 then it is an option element. The characters of this element
227 (aside from the initial '-') are option characters. If `getopt'
228 is called repeatedly, it returns successively each of the option characters
229 from each of the option elements.
231 If `getopt' finds another option character, it returns that character,
232 updating `woptind' and `nextchar' so that the next call to `getopt' can
233 resume the scan with the following option character or ARGV-element.
235 If there are no more option characters, `getopt' returns -1.
236 Then `woptind' is the index in ARGV of the first ARGV-element
237 that is not an option. (The ARGV-elements have been permuted
238 so that those that are not options now come last.)
240 OPTSTRING is a string containing the legitimate option characters.
241 If an option character is seen that is not listed in OPTSTRING,
242 return '?' after printing an error message. If you set `wopterr' to
243 zero, the error message is suppressed but we still return '?'.
245 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
246 so the following text in the same ARGV-element, or the text of the following
247 ARGV-element, is returned in `woptarg'. Two colons mean an option that
248 wants an optional arg; if there is text in the current ARGV-element,
249 it is returned in `woptarg', otherwise `woptarg' is set to zero.
251 If OPTSTRING starts with `-' or `+', it requests different methods of
252 handling the non-option ARGV-elements.
253 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
255 Long-named options begin with `--' instead of `-'.
256 Their names may be abbreviated as long as the abbreviation is unique
257 or is an exact match for some defined option. If they have an
258 argument, it follows the option name in the same ARGV-element, separated
259 from the option name by a `=', or else the in next ARGV-element.
260 When `getopt' finds a long-named option, it returns 0 if that option's
261 `flag' field is nonzero, the value of the option's `val' field
262 if the `flag' field is zero.
264 The elements of ARGV aren't really const, because we permute them.
265 But we pretend they're const in the prototype to be compatible
268 LONGOPTS is a vector of `struct woption' terminated by an
269 element containing a name which is zero.
271 LONGIND returns the index in LONGOPT of the long-named option found.
272 It is only valid when a long-named option has been found by the most
275 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
276 long-named options. */
279 _wgetopt_internal (int argc, wchar_t *const *argv, const wchar_t *optstring,
280 const struct woption *longopts, int *longind, int long_only)
284 if (woptind == 0 || !__getopt_initialized)
287 woptind = 1; /* Don't scan ARGV[0], the program name. */
288 optstring = _getopt_initialize (argc, argv, optstring);
289 __getopt_initialized = 1;
292 /* Test whether ARGV[woptind] points to a non-option argument.
293 Either it does not have option syntax, or there is an environment flag
294 from the shell indicating it is not an option. The later information
295 is only used when the used in the GNU libc. */
296 # define NONOPTION_P (argv[woptind][0] != L'-' || argv[woptind][1] == L'\0')
298 if (nextchar == NULL || *nextchar == '\0')
300 /* Advance to the next ARGV-element. */
302 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
303 moved back by the user (who may also have changed the arguments). */
304 if (last_nonopt > woptind)
305 last_nonopt = woptind;
306 if (first_nonopt > woptind)
307 first_nonopt = woptind;
309 if (ordering == PERMUTE)
311 /* If we have just processed some options following some non-options,
312 exchange them so that the options come first. */
314 if (first_nonopt != last_nonopt && last_nonopt != woptind)
315 exchange ((wchar_t **) argv);
316 else if (last_nonopt != woptind)
317 first_nonopt = woptind;
319 /* Skip any additional non-options
320 and extend the range of non-options previously skipped. */
322 while (woptind < argc && NONOPTION_P)
324 last_nonopt = woptind;
327 /* The special ARGV-element `--' means premature end of options.
328 Skip it like a null option,
329 then exchange with previous non-options as if it were an option,
330 then skip everything else like a non-option. */
332 if (woptind != argc && !wcscmp (argv[woptind], L"--"))
336 if (first_nonopt != last_nonopt && last_nonopt != woptind)
337 exchange ((wchar_t **) argv);
338 else if (first_nonopt == last_nonopt)
339 first_nonopt = woptind;
345 /* If we have done all the ARGV-elements, stop the scan
346 and back over any non-options that we skipped and permuted. */
350 /* Set the next-arg-index to point at the non-options
351 that we previously skipped, so the caller will digest them. */
352 if (first_nonopt != last_nonopt)
353 woptind = first_nonopt;
357 /* If we have come to a non-option and did not permute it,
358 either stop the scan or describe it to the caller and pass it by. */
362 if (ordering == REQUIRE_ORDER)
364 woptarg = argv[woptind++];
368 /* We have found another option-ARGV-element.
369 Skip the initial punctuation. */
371 nextchar = (argv[woptind] + 1
372 + (longopts != NULL && argv[woptind][1] == L'-'));
375 /* Decode the current option-ARGV-element. */
377 /* Check whether the ARGV-element is a long option.
379 If long_only and the ARGV-element has the form "-f", where f is
380 a valid short option, don't consider it an abbreviated form of
381 a long option that starts with f. Otherwise there would be no
382 way to give the -f short option.
384 On the other hand, if there's a long option "fubar" and
385 the ARGV-element is "-fu", do consider that an abbreviation of
386 the long option, just like "--fu", and not "-f" with arg "u".
388 This distinction seems to be the most useful approach. */
391 && (argv[woptind][1] == L'-'
392 || (long_only && (argv[woptind][2] || !wcschr (optstring, argv[woptind][1])))))
395 const struct woption *p;
396 const struct woption *pfound = NULL;
402 for (nameend = nextchar; *nameend && *nameend != L'='; nameend++)
405 /* Test all long options for either exact match
406 or abbreviated matches. */
407 for (p = longopts, option_index = 0; p->name; p++, option_index++)
408 if (!wcsncmp (p->name, nextchar, nameend - nextchar))
410 if ((unsigned int) (nameend - nextchar)
411 == (unsigned int) wcslen (p->name))
413 /* Exact match found. */
415 indfound = option_index;
419 else if (pfound == NULL)
421 /* First nonexact match found. */
423 indfound = option_index;
426 /* Second or later nonexact match found. */
433 fwprintf (stderr, L"%ls: option `%ls' is ambiguous\n",
434 argv[0], argv[woptind]);
435 nextchar += wcslen (nextchar);
443 option_index = indfound;
447 /* Don't test has_arg with >, because some C compilers don't
448 allow it to be used on enums. */
450 woptarg = nameend + 1;
454 if (argv[woptind - 1][1] == L'-')
457 L"%ls: option `--%ls' doesn't allow an argument\n",
458 argv[0], pfound->name);
460 /* +option or -option */
462 L"%ls: option `%lc%ls' doesn't allow an argument\n",
463 argv[0], argv[woptind - 1][0], pfound->name);
466 nextchar += wcslen (nextchar);
468 woptopt = pfound->val;
472 else if (pfound->has_arg == 1)
475 woptarg = argv[woptind++];
480 L"%ls: option `%ls' requires an argument\n",
481 argv[0], argv[woptind - 1]);
482 nextchar += wcslen (nextchar);
483 woptopt = pfound->val;
484 return optstring[0] == L':' ? L':' : L'?';
487 nextchar += wcslen (nextchar);
489 *longind = option_index;
492 *(pfound->flag) = pfound->val;
498 /* Can't find it as a long option. If this is not getopt_long_only,
499 or the option starts with '--' or is not a valid short
500 option, then it's an error.
501 Otherwise interpret it as a short option. */
502 if (!long_only || argv[woptind][1] == L'-'
503 || wcschr (optstring, *nextchar) == NULL)
507 if (argv[woptind][1] == '-')
509 fwprintf (stderr, L"%ls: unrecognized option `--%ls'\n",
512 /* +option or -option */
513 fwprintf (stderr, L"%ls: unrecognized option `%lc%ls'\n",
514 argv[0], argv[woptind][0], nextchar);
516 nextchar = (wchar_t *) L"";
523 /* Look at and handle the next short option-character. */
526 wchar_t c = *nextchar++;
527 wchar_t *temp = wcschr (optstring, c);
529 /* Increment `woptind' when we start to process its last character. */
530 if (*nextchar == L'\0')
533 if (temp == NULL || c == L':')
538 /* 1003.2 specifies the format of this message. */
539 fwprintf (stderr, L"%ls: illegal option -- %lc\n",
542 fwprintf (stderr, L"%ls: invalid option -- %lc\n",
548 /* Convenience. Treat POSIX -W foo same as long option --foo */
549 if (temp[0] == L'W' && temp[1] == L';')
552 const struct woption *p;
553 const struct woption *pfound = NULL;
559 /* This is an option that requires an argument. */
560 if (*nextchar != L'\0')
563 /* If we end this ARGV-element by taking the rest as an arg,
564 we must advance to the next element now. */
567 else if (woptind == argc)
571 /* 1003.2 specifies the format of this message. */
572 fwprintf (stderr, L"%ls: option requires an argument -- %lc\n",
576 if (optstring[0] == L':')
583 /* We already incremented `woptind' once;
584 increment it again when taking next ARGV-elt as argument. */
585 woptarg = argv[woptind++];
587 /* woptarg is now the argument, see if it's in the
588 table of longopts. */
590 for (nextchar = nameend = woptarg; *nameend && *nameend != L'='; nameend++)
593 /* Test all long options for either exact match
594 or abbreviated matches. */
595 for (p = longopts, option_index = 0; p->name; p++, option_index++)
596 if (!wcsncmp (p->name, nextchar, nameend - nextchar))
598 if ((unsigned int) (nameend - nextchar) == wcslen (p->name))
600 /* Exact match found. */
602 indfound = option_index;
606 else if (pfound == NULL)
608 /* First nonexact match found. */
610 indfound = option_index;
613 /* Second or later nonexact match found. */
619 fwprintf (stderr, L"%ls: option `-W %ls' is ambiguous\n",
620 argv[0], argv[woptind]);
621 nextchar += wcslen (nextchar);
627 option_index = indfound;
630 /* Don't test has_arg with >, because some C compilers don't
631 allow it to be used on enums. */
633 woptarg = nameend + 1;
637 fwprintf (stderr, L"\
638 %ls: option `-W %ls' doesn't allow an argument\n",
639 argv[0], pfound->name);
641 nextchar += wcslen (nextchar);
645 else if (pfound->has_arg == 1)
648 woptarg = argv[woptind++];
653 L"%ls: option `%ls' requires an argument\n",
654 argv[0], argv[woptind - 1]);
655 nextchar += wcslen (nextchar);
656 return optstring[0] == L':' ? L':' : L'?';
659 nextchar += wcslen (nextchar);
661 *longind = option_index;
664 *(pfound->flag) = pfound->val;
670 return L'W'; /* Let the application handle it. */
676 /* This is an option that accepts an argument optionally. */
677 if (*nextchar != L'\0')
688 /* This is an option that requires an argument. */
689 if (*nextchar != L'\0')
692 /* If we end this ARGV-element by taking the rest as an arg,
693 we must advance to the next element now. */
696 else if (woptind == argc)
700 /* 1003.2 specifies the format of this message. */
702 L"%ls: option requires an argument -- %lc\n",
706 if (optstring[0] == L':')
712 /* We already incremented `woptind' once;
713 increment it again when taking next ARGV-elt as argument. */
714 woptarg = argv[woptind++];
723 wgetopt (int argc, wchar_t *const *argv, const wchar_t *optstring)
725 return _wgetopt_internal (argc, argv, optstring,
726 (const struct woption *) 0,
732 wgetopt_long (int argc, wchar_t * const *argv, const wchar_t *options,
733 const struct woption *long_options, int *opt_index)
735 return _wgetopt_internal (argc, argv, options, long_options, opt_index, 0);
738 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
739 If an option that starts with '-' (not '--') doesn't match a long option,
740 but does match a short option, it is parsed as a short option
743 wgetopt_long_only (int argc, wchar_t * const *argv, const wchar_t *options,
744 const struct woption *long_options, int *opt_index)
746 return _wgetopt_internal (argc, argv, options, long_options, opt_index, 1);