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