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