1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 1987-1997 Free Software Foundation, Inc.
5 * Copyright (C) 2005-2010 the VideoLAN team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
25 #include <vlc_common.h>
30 /* This version of `getopt' appears to the caller like standard Unix `getopt'
31 but it behaves differently for the user, since it allows the user
32 to intersperse the options with the other arguments.
34 As `getopt' works, it permutes the elements of ARGV so that,
35 when it is done, all the options precede everything else. Thus
36 all application programs are extended to handle flexible argument order.
38 Setting the environment variable POSIXLY_CORRECT disables permutation.
39 Then the behavior is completely standard.
41 GNU application programs can use a third alternative mode in which
42 they can distinguish the relative order of options and other arguments. */
44 #include "vlc_getopt.h"
46 /* For communication from `getopt' to the caller.
47 When `getopt' finds an option that takes an argument,
48 the argument value is returned here. */
50 char *vlc_optarg = NULL;
52 /* Index in ARGV of the next element to be scanned.
53 This is used for communication to and from the caller
54 and for communication between successive calls to `getopt'.
56 On entry to `getopt', zero means this is the first call; initialize.
58 When `getopt' returns -1, this is the index of the first of the
59 non-option elements that the caller should itself scan.
61 Otherwise, `optind' communicates from one call to the next
62 how much of ARGV has been scanned so far. */
64 /* 1003.2 says this must be 1 before any call. */
67 /* The next char to be scanned in the option-element
68 in which the last option character we returned was found.
69 This allows us to pick up the scan where we left off.
71 If this is zero, or a null string, it means resume the scan
72 by advancing to the next ARGV-element. */
74 static char *nextchar;
76 /* Set to an option character which was unrecognized.
77 This must be initialized on some systems to avoid linking in the
78 system's own getopt implementation. */
82 /* Describe how to deal with options that follow non-option ARGV-elements.
84 If the caller did not specify anything,
85 the default is REQUIRE_ORDER if the environment variable
86 POSIXLY_CORRECT is defined, PERMUTE otherwise.
88 REQUIRE_ORDER means don't recognize them as options;
89 stop option processing when the first non-option is seen.
90 This is what Unix does.
91 This mode of operation is selected by either setting the environment
92 variable POSIXLY_CORRECT, or using `+' as the first character
93 of the list of option characters.
95 PERMUTE is the default. We permute the contents of ARGV as we scan,
96 so that eventually all the non-options are at the end. This allows options
97 to be given in any order, even with programs that were not written to
100 The special argument `--' forces an end of option-scanning regardless
101 of the value of `ordering'. */
105 REQUIRE_ORDER, PERMUTE
109 /* Handle permutation of arguments. */
111 /* Describe the part of ARGV that contains non-options that have
112 been skipped. `first_nonopt' is the index in ARGV of the first of them;
113 `last_nonopt' is the index after the last of them. */
115 static int first_nonopt;
116 static int last_nonopt;
118 /* Exchange two adjacent subsequences of ARGV.
119 One subsequence is elements [first_nonopt,last_nonopt)
120 which contains all the non-options that have been skipped so far.
121 The other is elements [last_nonopt,optind), which contains all
122 the options processed since those non-options were skipped.
124 `first_nonopt' and `last_nonopt' are relocated so that they describe
125 the new indices of the non-options in ARGV after they are moved. */
127 static void exchange(char **);
133 int bottom = first_nonopt;
134 int middle = last_nonopt;
135 int top = vlc_optind;
138 /* Exchange the shorter segment with the far end of the longer segment.
139 That puts the shorter segment into the right place.
140 It leaves the longer segment in the right place overall,
141 but it consists of two parts that need to be swapped next. */
143 while (top > middle && middle > bottom)
145 if (top - middle > middle - bottom)
147 /* Bottom segment is the short one. */
148 int len = middle - bottom;
151 /* Swap it with the top part of the top segment. */
152 for (i = 0; i < len; i++)
154 tem = argv[bottom + i];
155 argv[bottom + i] = argv[top - (middle - bottom) + i];
156 argv[top - (middle - bottom) + i] = tem;
158 /* Exclude the moved bottom segment from further swapping. */
163 /* Top segment is the short one. */
164 int len = top - middle;
167 /* Swap it with the bottom part of the bottom segment. */
168 for (i = 0; i < len; i++)
170 tem = argv[bottom + i];
171 argv[bottom + i] = argv[middle + i];
172 argv[middle + i] = tem;
174 /* Exclude the moved top segment from further swapping. */
179 /* Update records for the slots the non-options now occupy. */
181 first_nonopt += (vlc_optind - last_nonopt);
182 last_nonopt = vlc_optind;
185 /* Initialize the internal data when the first call is made. */
187 static const char *vlc_getopt_initialize(const char *optstring)
189 /* Start processing options with ARGV-element 1 (since ARGV-element 0
190 is the program name); the sequence of previously skipped
191 non-option ARGV-elements is empty. */
193 first_nonopt = last_nonopt = vlc_optind = 1;
197 const char *posixly_correct = getenv("POSIXLY_CORRECT");
199 /* Determine how to handle the ordering of options and nonoptions. */
201 if (posixly_correct != NULL)
202 ordering = REQUIRE_ORDER;
209 /* Scan elements of ARGV (whose length is ARGC) for option characters
212 If an element of ARGV starts with '-', and is not exactly "-" or "--",
213 then it is an option element. The characters of this element
214 (aside from the initial '-') are option characters. If `getopt'
215 is called repeatedly, it returns successively each of the option characters
216 from each of the option elements.
218 If `getopt' finds another option character, it returns that character,
219 updating `optind' and `nextchar' so that the next call to `getopt' can
220 resume the scan with the following option character or ARGV-element.
222 If there are no more option characters, `getopt' returns -1.
223 Then `optind' is the index in ARGV of the first ARGV-element
224 that is not an option. (The ARGV-elements have been permuted
225 so that those that are not options now come last.)
227 OPTSTRING is a string containing the legitimate option characters.
228 If an option character is seen that is not listed in OPTSTRING,
231 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
232 so the following text in the same ARGV-element, or the text of the following
233 ARGV-element, is returned in `optarg'.
235 If OPTSTRING starts with `-' or `+', it requests different methods of
236 handling the non-option ARGV-elements.
237 See the comments about REQUIRE_ORDER, above.
239 Long-named options begin with `--' instead of `-'.
240 Their names may be abbreviated as long as the abbreviation is unique
241 or is an exact match for some defined option. If they have an
242 argument, it follows the option name in the same ARGV-element, separated
243 from the option name by a `=', or else the in next ARGV-element.
244 When `getopt' finds a long-named option, it returns 0 if that option's
245 `flag' field is nonzero, the value of the option's `val' field
246 if the `flag' field is zero.
248 The elements of ARGV aren't really const, because we permute them.
249 But we pretend they're const in the prototype to be compatible
252 LONGOPTS is a vector of `struct option' terminated by an
253 element containing a name which is zero.
255 LONGIND returns the index in LONGOPT of the long-named option found.
256 It is only valid when a long-named option has been found by the most
260 vlc_getopt_long(argc, argv, optstring, longopts, longind)
263 const char *optstring;
264 const struct vlc_option *restrict longopts;
271 optstring = vlc_getopt_initialize(optstring);
272 vlc_optind = 1; /* Don't scan ARGV[0], the program name. */
275 #define NONOPTION_P (argv[vlc_optind][0] != '-' || argv[vlc_optind][1] == '\0')
277 if (nextchar == NULL || *nextchar == '\0')
279 /* Advance to the next ARGV-element. */
281 /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
282 moved back by the user (who may also have changed the arguments). */
283 if (last_nonopt > vlc_optind)
284 last_nonopt = vlc_optind;
285 if (first_nonopt > vlc_optind)
286 first_nonopt = vlc_optind;
288 if (ordering == PERMUTE)
290 /* If we have just processed some options following some non-options,
291 exchange them so that the options come first. */
293 if (first_nonopt != last_nonopt && last_nonopt != vlc_optind)
294 exchange((char **) argv);
295 else if (last_nonopt != vlc_optind)
296 first_nonopt = vlc_optind;
298 /* Skip any additional non-options
299 and extend the range of non-options previously skipped. */
301 while (vlc_optind < argc && NONOPTION_P)
303 last_nonopt = vlc_optind;
306 /* The special ARGV-element `--' means premature end of options.
307 Skip it like a null option,
308 then exchange with previous non-options as if it were an option,
309 then skip everything else like a non-option. */
311 if (vlc_optind != argc && !strcmp(argv[vlc_optind], "--"))
315 if (first_nonopt != last_nonopt && last_nonopt != vlc_optind)
316 exchange((char **) argv);
317 else if (first_nonopt == last_nonopt)
318 first_nonopt = vlc_optind;
324 /* If we have done all the ARGV-elements, stop the scan
325 and back over any non-options that we skipped and permuted. */
327 if (vlc_optind == argc)
329 /* Set the next-arg-index to point at the non-options
330 that we previously skipped, so the caller will digest them. */
331 if (first_nonopt != last_nonopt)
332 vlc_optind = first_nonopt;
336 /* If we have come to a non-option and did not permute it,
337 either stop the scan or describe it to the caller and pass it by. */
341 if (ordering == REQUIRE_ORDER)
343 vlc_optarg = argv[vlc_optind++];
347 /* We have found another option-ARGV-element.
348 Skip the initial punctuation. */
350 nextchar = (argv[vlc_optind] + 1
351 + (argv[vlc_optind][1] == '-'));
354 /* Decode the current option-ARGV-element. */
356 /* Check whether the ARGV-element is a long option. */
358 if (argv[vlc_optind][1] == '-')
361 const struct vlc_option *p;
362 const struct vlc_option *pfound = NULL;
368 for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
371 /* Test all long options for either exact match
372 or abbreviated matches. */
373 for (p = longopts, option_index = 0; p->name; p++, option_index++)
374 if (!strncmp(p->name, nextchar, nameend - nextchar))
376 if ((unsigned int) (nameend - nextchar)
377 == (unsigned int) strlen(p->name))
379 /* Exact match found. */
381 indfound = option_index;
385 else if (pfound == NULL)
387 /* First nonexact match found. */
389 indfound = option_index;
392 /* Second or later nonexact match found. */
398 nextchar += strlen(nextchar);
406 option_index = indfound;
411 vlc_optarg = nameend + 1;
414 nextchar += strlen(nextchar);
416 vlc_optopt = pfound->val;
420 else if (pfound->has_arg)
422 if (vlc_optind < argc)
423 vlc_optarg = argv[vlc_optind++];
426 nextchar += strlen(nextchar);
427 vlc_optopt = pfound->val;
428 return optstring[0] == ':' ? ':' : '?';
431 nextchar += strlen(nextchar);
433 *longind = option_index;
436 *(pfound->flag) = pfound->val;
442 nextchar = (char *) "";
448 /* Look at and handle the next short option-character. */
451 char c = *nextchar++;
452 char *temp = strchr(optstring, c);
454 /* Increment `optind' when we start to process its last character. */
455 if (*nextchar == '\0')
458 if (temp == NULL || c == ':')
463 /* Convenience. Treat POSIX -W foo same as long option --foo */
464 if (temp[0] == 'W' && temp[1] == ';')
467 const struct vlc_option *p;
468 const struct vlc_option *pfound = NULL;
474 /* This is an option that requires an argument. */
475 if (*nextchar != '\0')
477 vlc_optarg = nextchar;
478 /* If we end this ARGV-element by taking the rest as an arg,
479 we must advance to the next element now. */
482 else if (vlc_optind == argc)
485 if (optstring[0] == ':')
492 /* We already incremented `optind' once;
493 increment it again when taking next ARGV-elt as argument. */
494 vlc_optarg = argv[vlc_optind++];
496 /* optarg is now the argument, see if it's in the
497 table of longopts. */
499 for (nextchar = nameend = vlc_optarg; *nameend && *nameend != '='; nameend++)
502 /* Test all long options for either exact match
503 or abbreviated matches. */
504 for (p = longopts, option_index = 0; p->name; p++, option_index++)
505 if (!strncmp(p->name, nextchar, nameend - nextchar))
507 if ((unsigned int) (nameend - nextchar) == strlen(p->name))
509 /* Exact match found. */
511 indfound = option_index;
515 else if (pfound == NULL)
517 /* First nonexact match found. */
519 indfound = option_index;
522 /* Second or later nonexact match found. */
527 nextchar += strlen(nextchar);
533 option_index = indfound;
537 vlc_optarg = nameend + 1;
540 nextchar += strlen(nextchar);
544 else if (pfound->has_arg)
546 if (vlc_optind < argc)
547 vlc_optarg = argv[vlc_optind++];
550 nextchar += strlen(nextchar);
551 return optstring[0] == ':' ? ':' : '?';
554 nextchar += strlen(nextchar);
556 *longind = option_index;
559 *(pfound->flag) = pfound->val;
565 return 'W'; /* Let the application handle it. */
569 /* This is an option that requires an argument. */
570 if (*nextchar != '\0')
572 vlc_optarg = nextchar;
573 /* If we end this ARGV-element by taking the rest as an arg,
574 we must advance to the next element now. */
577 else if (vlc_optind == argc)
580 if (optstring[0] == ':')
586 /* We already incremented `optind' once;
587 increment it again when taking next ARGV-elt as argument. */
588 vlc_optarg = argv[vlc_optind++];