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 part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option)
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with wimlib; if not, see http://www.gnu.org/licenses/.
30 /* For communication from `getopt' to the caller.
31 When `getopt' finds an option that takes an argument,
32 the argument value is returned here.
33 Also, when `ordering' is RETURN_IN_ORDER,
34 each non-option ARGV-element is returned here. */
36 wchar_t *woptarg = NULL;
38 /* Index in ARGV of the next element to be scanned.
39 This is used for communication to and from the caller
40 and for communication between successive calls to `getopt'.
42 On entry to `getopt', zero means this is the first call; initialize.
44 When `getopt' returns -1, this is the index of the first of the
45 non-option elements that the caller should itself scan.
47 Otherwise, `woptind' communicates from one call to the next
48 how much of ARGV has been scanned so far. */
50 /* 1003.2 says this must be 1 before any call. */
53 /* Formerly, initialization of getopt depended on woptind==0, which
54 causes problems with re-calling getopt as programs generally don't
57 int __getopt_initialized = 0;
59 /* The next char to be scanned in the option-element
60 in which the last option character we returned was found.
61 This allows us to pick up the scan where we left off.
63 If this is zero, or a null string, it means resume the scan
64 by advancing to the next ARGV-element. */
66 static wchar_t *nextchar;
68 /* Callers store zero here to inhibit the error message
69 for unrecognized options. */
73 /* Set to an option character which was unrecognized.
74 This must be initialized on some systems to avoid linking in the
75 system's own getopt implementation. */
79 /* Describe how to deal with options that follow non-option ARGV-elements.
81 If the caller did not specify anything,
82 the default is REQUIRE_ORDER if the environment variable
83 POSIXLY_CORRECT is defined, PERMUTE otherwise.
85 REQUIRE_ORDER means don't recognize them as options;
86 stop option processing when the first non-option is seen.
87 This is what Unix does.
88 This mode of operation is selected by either setting the environment
89 variable POSIXLY_CORRECT, or using `+' as the first character
90 of the list of option characters.
92 PERMUTE is the default. We permute the contents of ARGV as we scan,
93 so that eventually all the non-options are at the end. This allows options
94 to be given in any order, even with programs that were not written to
97 RETURN_IN_ORDER is an option available to programs that were written
98 to expect options and other ARGV-elements in any order and that care about
99 the ordering of the two. We describe each non-option ARGV-element
100 as if it were the argument of an option with character code 1.
101 Using `-' as the first character of the list of option characters
102 selects this mode of operation.
104 The special argument `--' forces an end of option-scanning regardless
105 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
106 `--' can cause `getopt' to return -1 with `woptind' != ARGC. */
110 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
113 /* Value of POSIXLY_CORRECT environment variable. */
114 static char *posixly_correct;
117 /* Handle permutation of arguments. */
119 /* Describe the part of ARGV that contains non-options that have
120 been skipped. `first_nonopt' is the index in ARGV of the first of them;
121 `last_nonopt' is the index after the last of them. */
123 static int first_nonopt;
124 static int last_nonopt;
126 /* Exchange two adjacent subsequences of ARGV.
127 One subsequence is elements [first_nonopt,last_nonopt)
128 which contains all the non-options that have been skipped so far.
129 The other is elements [last_nonopt,woptind), which contains all
130 the options processed since those non-options were skipped.
132 `first_nonopt' and `last_nonopt' are relocated so that they describe
133 the new indices of the non-options in ARGV after they are moved. */
136 exchange (wchar_t **argv)
138 int bottom = first_nonopt;
139 int middle = last_nonopt;
143 /* Exchange the shorter segment with the far end of the longer segment.
144 That puts the shorter segment into the right place.
145 It leaves the longer segment in the right place overall,
146 but it consists of two parts that need to be swapped next. */
148 while (top > middle && middle > bottom)
150 if (top - middle > middle - bottom)
152 /* Bottom segment is the short one. */
153 int len = middle - bottom;
156 /* Swap it with the top part of the top segment. */
157 for (i = 0; i < len; i++)
159 tem = argv[bottom + i];
160 argv[bottom + i] = argv[top - (middle - bottom) + i];
161 argv[top - (middle - bottom) + i] = tem;
163 /* Exclude the moved bottom segment from further swapping. */
168 /* Top segment is the short one. */
169 int len = top - middle;
172 /* Swap it with the bottom part of the bottom segment. */
173 for (i = 0; i < len; i++)
175 tem = argv[bottom + i];
176 argv[bottom + i] = argv[middle + i];
177 argv[middle + i] = tem;
179 /* Exclude the moved top segment from further swapping. */
184 /* Update records for the slots the non-options now occupy. */
186 first_nonopt += (woptind - last_nonopt);
187 last_nonopt = woptind;
190 /* Initialize the internal data when the first call is made. */
192 static const wchar_t *
193 _getopt_initialize (int argc, wchar_t *const *argv, const wchar_t *optstring)
195 /* Start processing options with ARGV-element 1 (since ARGV-element 0
196 is the program name); the sequence of previously skipped
197 non-option ARGV-elements is empty. */
199 first_nonopt = last_nonopt = woptind;
203 posixly_correct = getenv ("POSIXLY_CORRECT");
205 /* Determine how to handle the ordering of options and nonoptions. */
207 if (optstring[0] == L'-')
209 ordering = RETURN_IN_ORDER;
212 else if (optstring[0] == L'+')
214 ordering = REQUIRE_ORDER;
217 else if (posixly_correct != NULL)
218 ordering = REQUIRE_ORDER;
225 /* Scan elements of ARGV (whose length is ARGC) for option characters
228 If an element of ARGV starts with '-', and is not exactly "-" or "--",
229 then it is an option element. The characters of this element
230 (aside from the initial '-') are option characters. If `getopt'
231 is called repeatedly, it returns successively each of the option characters
232 from each of the option elements.
234 If `getopt' finds another option character, it returns that character,
235 updating `woptind' and `nextchar' so that the next call to `getopt' can
236 resume the scan with the following option character or ARGV-element.
238 If there are no more option characters, `getopt' returns -1.
239 Then `woptind' is the index in ARGV of the first ARGV-element
240 that is not an option. (The ARGV-elements have been permuted
241 so that those that are not options now come last.)
243 OPTSTRING is a string containing the legitimate option characters.
244 If an option character is seen that is not listed in OPTSTRING,
245 return '?' after printing an error message. If you set `wopterr' to
246 zero, the error message is suppressed but we still return '?'.
248 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
249 so the following text in the same ARGV-element, or the text of the following
250 ARGV-element, is returned in `woptarg'. Two colons mean an option that
251 wants an optional arg; if there is text in the current ARGV-element,
252 it is returned in `woptarg', otherwise `woptarg' is set to zero.
254 If OPTSTRING starts with `-' or `+', it requests different methods of
255 handling the non-option ARGV-elements.
256 See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
258 Long-named options begin with `--' instead of `-'.
259 Their names may be abbreviated as long as the abbreviation is unique
260 or is an exact match for some defined option. If they have an
261 argument, it follows the option name in the same ARGV-element, separated
262 from the option name by a `=', or else the in next ARGV-element.
263 When `getopt' finds a long-named option, it returns 0 if that option's
264 `flag' field is nonzero, the value of the option's `val' field
265 if the `flag' field is zero.
267 The elements of ARGV aren't really const, because we permute them.
268 But we pretend they're const in the prototype to be compatible
271 LONGOPTS is a vector of `struct woption' terminated by an
272 element containing a name which is zero.
274 LONGIND returns the index in LONGOPT of the long-named option found.
275 It is only valid when a long-named option has been found by the most
278 If LONG_ONLY is nonzero, '-' as well as '--' can introduce
279 long-named options. */
282 _wgetopt_internal (int argc, wchar_t *const *argv, const wchar_t *optstring,
283 const struct woption *longopts, int *longind, int long_only)
287 if (woptind == 0 || !__getopt_initialized)
290 woptind = 1; /* Don't scan ARGV[0], the program name. */
291 optstring = _getopt_initialize (argc, argv, optstring);
292 __getopt_initialized = 1;
295 /* Test whether ARGV[woptind] points to a non-option argument.
296 Either it does not have option syntax, or there is an environment flag
297 from the shell indicating it is not an option. The later information
298 is only used when the used in the GNU libc. */
299 # define NONOPTION_P (argv[woptind][0] != L'-' || argv[woptind][1] == L'\0')
301 if (nextchar == NULL || *nextchar == '\0')
303 /* Advance to the next ARGV-element. */
305 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
306 moved back by the user (who may also have changed the arguments). */
307 if (last_nonopt > woptind)
308 last_nonopt = woptind;
309 if (first_nonopt > woptind)
310 first_nonopt = woptind;
312 if (ordering == PERMUTE)
314 /* If we have just processed some options following some non-options,
315 exchange them so that the options come first. */
317 if (first_nonopt != last_nonopt && last_nonopt != woptind)
318 exchange ((wchar_t **) argv);
319 else if (last_nonopt != woptind)
320 first_nonopt = woptind;
322 /* Skip any additional non-options
323 and extend the range of non-options previously skipped. */
325 while (woptind < argc && NONOPTION_P)
327 last_nonopt = woptind;
330 /* The special ARGV-element `--' means premature end of options.
331 Skip it like a null option,
332 then exchange with previous non-options as if it were an option,
333 then skip everything else like a non-option. */
335 if (woptind != argc && !wcscmp (argv[woptind], L"--"))
339 if (first_nonopt != last_nonopt && last_nonopt != woptind)
340 exchange ((wchar_t **) argv);
341 else if (first_nonopt == last_nonopt)
342 first_nonopt = woptind;
348 /* If we have done all the ARGV-elements, stop the scan
349 and back over any non-options that we skipped and permuted. */
353 /* Set the next-arg-index to point at the non-options
354 that we previously skipped, so the caller will digest them. */
355 if (first_nonopt != last_nonopt)
356 woptind = first_nonopt;
360 /* If we have come to a non-option and did not permute it,
361 either stop the scan or describe it to the caller and pass it by. */
365 if (ordering == REQUIRE_ORDER)
367 woptarg = argv[woptind++];
371 /* We have found another option-ARGV-element.
372 Skip the initial punctuation. */
374 nextchar = (argv[woptind] + 1
375 + (longopts != NULL && argv[woptind][1] == L'-'));
378 /* Decode the current option-ARGV-element. */
380 /* Check whether the ARGV-element is a long option.
382 If long_only and the ARGV-element has the form "-f", where f is
383 a valid short option, don't consider it an abbreviated form of
384 a long option that starts with f. Otherwise there would be no
385 way to give the -f short option.
387 On the other hand, if there's a long option "fubar" and
388 the ARGV-element is "-fu", do consider that an abbreviation of
389 the long option, just like "--fu", and not "-f" with arg "u".
391 This distinction seems to be the most useful approach. */
394 && (argv[woptind][1] == L'-'
395 || (long_only && (argv[woptind][2] || !wcschr (optstring, argv[woptind][1])))))
398 const struct woption *p;
399 const struct woption *pfound = NULL;
405 for (nameend = nextchar; *nameend && *nameend != L'='; nameend++)
408 /* Test all long options for either exact match
409 or abbreviated matches. */
410 for (p = longopts, option_index = 0; p->name; p++, option_index++)
411 if (!wcsncmp (p->name, nextchar, nameend - nextchar))
413 if ((unsigned int) (nameend - nextchar)
414 == (unsigned int) wcslen (p->name))
416 /* Exact match found. */
418 indfound = option_index;
422 else if (pfound == NULL)
424 /* First nonexact match found. */
426 indfound = option_index;
429 /* Second or later nonexact match found. */
436 fwprintf (stderr, L"%ls: option `%ls' is ambiguous\n",
437 argv[0], argv[woptind]);
438 nextchar += wcslen (nextchar);
446 option_index = indfound;
450 /* Don't test has_arg with >, because some C compilers don't
451 allow it to be used on enums. */
453 woptarg = nameend + 1;
457 if (argv[woptind - 1][1] == L'-')
460 L"%ls: option `--%ls' doesn't allow an argument\n",
461 argv[0], pfound->name);
463 /* +option or -option */
465 L"%ls: option `%lc%ls' doesn't allow an argument\n",
466 argv[0], argv[woptind - 1][0], pfound->name);
469 nextchar += wcslen (nextchar);
471 woptopt = pfound->val;
475 else if (pfound->has_arg == 1)
478 woptarg = argv[woptind++];
483 L"%ls: option `%ls' requires an argument\n",
484 argv[0], argv[woptind - 1]);
485 nextchar += wcslen (nextchar);
486 woptopt = pfound->val;
487 return optstring[0] == L':' ? L':' : L'?';
490 nextchar += wcslen (nextchar);
492 *longind = option_index;
495 *(pfound->flag) = pfound->val;
501 /* Can't find it as a long option. If this is not getopt_long_only,
502 or the option starts with '--' or is not a valid short
503 option, then it's an error.
504 Otherwise interpret it as a short option. */
505 if (!long_only || argv[woptind][1] == L'-'
506 || wcschr (optstring, *nextchar) == NULL)
510 if (argv[woptind][1] == '-')
512 fwprintf (stderr, L"%ls: unrecognized option `--%ls'\n",
515 /* +option or -option */
516 fwprintf (stderr, L"%ls: unrecognized option `%lc%ls'\n",
517 argv[0], argv[woptind][0], nextchar);
519 nextchar = (wchar_t *) L"";
526 /* Look at and handle the next short option-character. */
529 wchar_t c = *nextchar++;
530 wchar_t *temp = wcschr (optstring, c);
532 /* Increment `woptind' when we start to process its last character. */
533 if (*nextchar == L'\0')
536 if (temp == NULL || c == L':')
541 /* 1003.2 specifies the format of this message. */
542 fwprintf (stderr, L"%ls: illegal option -- %lc\n",
545 fwprintf (stderr, L"%ls: invalid option -- %lc\n",
551 /* Convenience. Treat POSIX -W foo same as long option --foo */
552 if (temp[0] == L'W' && temp[1] == L';')
555 const struct woption *p;
556 const struct woption *pfound = NULL;
562 /* This is an option that requires an argument. */
563 if (*nextchar != L'\0')
566 /* If we end this ARGV-element by taking the rest as an arg,
567 we must advance to the next element now. */
570 else if (woptind == argc)
574 /* 1003.2 specifies the format of this message. */
575 fwprintf (stderr, L"%ls: option requires an argument -- %lc\n",
579 if (optstring[0] == L':')
586 /* We already incremented `woptind' once;
587 increment it again when taking next ARGV-elt as argument. */
588 woptarg = argv[woptind++];
590 /* woptarg is now the argument, see if it's in the
591 table of longopts. */
593 for (nextchar = nameend = woptarg; *nameend && *nameend != L'='; nameend++)
596 /* Test all long options for either exact match
597 or abbreviated matches. */
598 for (p = longopts, option_index = 0; p->name; p++, option_index++)
599 if (!wcsncmp (p->name, nextchar, nameend - nextchar))
601 if ((unsigned int) (nameend - nextchar) == wcslen (p->name))
603 /* Exact match found. */
605 indfound = option_index;
609 else if (pfound == NULL)
611 /* First nonexact match found. */
613 indfound = option_index;
616 /* Second or later nonexact match found. */
622 fwprintf (stderr, L"%ls: option `-W %ls' is ambiguous\n",
623 argv[0], argv[woptind]);
624 nextchar += wcslen (nextchar);
630 option_index = indfound;
633 /* Don't test has_arg with >, because some C compilers don't
634 allow it to be used on enums. */
636 woptarg = nameend + 1;
640 fwprintf (stderr, L"\
641 %ls: option `-W %ls' doesn't allow an argument\n",
642 argv[0], pfound->name);
644 nextchar += wcslen (nextchar);
648 else if (pfound->has_arg == 1)
651 woptarg = argv[woptind++];
656 L"%ls: option `%ls' requires an argument\n",
657 argv[0], argv[woptind - 1]);
658 nextchar += wcslen (nextchar);
659 return optstring[0] == L':' ? L':' : L'?';
662 nextchar += wcslen (nextchar);
664 *longind = option_index;
667 *(pfound->flag) = pfound->val;
673 return L'W'; /* Let the application handle it. */
679 /* This is an option that accepts an argument optionally. */
680 if (*nextchar != L'\0')
691 /* This is an option that requires an argument. */
692 if (*nextchar != L'\0')
695 /* If we end this ARGV-element by taking the rest as an arg,
696 we must advance to the next element now. */
699 else if (woptind == argc)
703 /* 1003.2 specifies the format of this message. */
705 L"%ls: option requires an argument -- %lc\n",
709 if (optstring[0] == L':')
715 /* We already incremented `woptind' once;
716 increment it again when taking next ARGV-elt as argument. */
717 woptarg = argv[woptind++];
726 wgetopt (int argc, wchar_t *const *argv, const wchar_t *optstring)
728 return _wgetopt_internal (argc, argv, optstring,
729 (const struct woption *) 0,
735 wgetopt_long (int argc, wchar_t * const *argv, const wchar_t *options,
736 const struct woption *long_options, int *opt_index)
738 return _wgetopt_internal (argc, argv, options, long_options, opt_index, 0);
741 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
742 If an option that starts with '-' (not '--') doesn't match a long option,
743 but does match a short option, it is parsed as a short option
746 wgetopt_long_only (int argc, wchar_t * const *argv, const wchar_t *options,
747 const struct woption *long_options, int *opt_index)
749 return _wgetopt_internal (argc, argv, options, long_options, opt_index, 1);