Commit | Line | Data |
---|---|---|
f87489ac WJ |
1 | /* Getopt for GNU. |
2 | Copyright (C) 1987, 1989, 1990, 1991 Free Software Foundation, Inc. | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2, or (at your option) | |
7 | any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
17 | \f | |
18 | #ifdef __STDC__ | |
19 | #define CONST const | |
20 | #else | |
21 | #define CONST | |
22 | #endif | |
23 | ||
24 | /* This version of `getopt' appears to the caller like standard Unix `getopt' | |
25 | but it behaves differently for the user, since it allows the user | |
26 | to intersperse the options with the other arguments. | |
27 | ||
28 | As `getopt' works, it permutes the elements of `argv' so that, | |
29 | when it is done, all the options precede everything else. Thus | |
30 | all application programs are extended to handle flexible argument order. | |
31 | ||
32 | Setting the environment variable _POSIX_OPTION_ORDER disables permutation. | |
33 | Then the behavior is completely standard. | |
34 | ||
35 | GNU application programs can use a third alternative mode in which | |
36 | they can distinguish the relative order of options and other arguments. */ | |
37 | ||
38 | #include <stdio.h> | |
39 | ||
40 | /* If compiled with GNU C, use the built-in alloca */ | |
41 | #ifdef __GNUC__ | |
42 | #define alloca __builtin_alloca | |
43 | #else /* not __GNUC__ */ | |
44 | #ifdef sparc | |
45 | #include <alloca.h> | |
46 | #else | |
47 | #ifdef _AIX | |
48 | #pragma alloca | |
49 | #else | |
50 | char *alloca (); | |
51 | #endif | |
52 | #endif /* sparc */ | |
53 | #endif /* not __GNUC__ */ | |
54 | ||
55 | #if defined(STDC_HEADERS) || defined(__GNU_LIBRARY__) | |
56 | #include <stdlib.h> | |
57 | #else /* STDC_HEADERS or __GNU_LIBRARY__ */ | |
58 | char *getenv (); | |
59 | char *malloc (); | |
60 | #endif /* STDC_HEADERS or __GNU_LIBRARY__ */ | |
61 | ||
62 | #if defined(USG) || defined(STDC_HEADERS) || defined(__GNU_LIBRARY__) | |
63 | #include <string.h> | |
64 | #define bcopy(s, d, n) memcpy ((d), (s), (n)) | |
65 | #define index strchr | |
66 | #else /* USG or STDC_HEADERS or __GNU_LIBRARY__ */ | |
67 | #ifdef VMS | |
68 | #include <string.h> | |
69 | #else /* VMS */ | |
70 | #include <strings.h> | |
71 | #endif /* VMS */ | |
72 | /* Declaring bcopy causes errors on systems whose declarations are different. | |
73 | If the declaration is omitted, everything works fine. */ | |
74 | #endif /* USG or STDC_HEADERS or __GNU_LIBRARY__ */ | |
75 | ||
76 | /* For communication from `getopt' to the caller. | |
77 | When `getopt' finds an option that takes an argument, | |
78 | the argument value is returned here. | |
79 | Also, when `ordering' is RETURN_IN_ORDER, | |
80 | each non-option ARGV-element is returned here. */ | |
81 | ||
82 | char *optarg = 0; | |
83 | ||
84 | /* Index in ARGV of the next element to be scanned. | |
85 | This is used for communication to and from the caller | |
86 | and for communication between successive calls to `getopt'. | |
87 | ||
88 | On entry to `getopt', zero means this is the first call; initialize. | |
89 | ||
90 | When `getopt' returns EOF, this is the index of the first of the | |
91 | non-option elements that the caller should itself scan. | |
92 | ||
93 | Otherwise, `optind' communicates from one call to the next | |
94 | how much of ARGV has been scanned so far. */ | |
95 | ||
96 | int optind = 0; | |
97 | ||
98 | /* The next char to be scanned in the option-element | |
99 | in which the last option character we returned was found. | |
100 | This allows us to pick up the scan where we left off. | |
101 | ||
102 | If this is zero, or a null string, it means resume the scan | |
103 | by advancing to the next ARGV-element. */ | |
104 | ||
105 | static char *nextchar; | |
106 | ||
107 | /* Callers store zero here to inhibit the error message | |
108 | for unrecognized options. */ | |
109 | ||
110 | int opterr = 1; | |
111 | ||
112 | /* Describe how to deal with options that follow non-option ARGV-elements. | |
113 | ||
114 | If the caller did not specify anything, | |
115 | the default is REQUIRE_ORDER if the environment variable | |
116 | _POSIX_OPTION_ORDER is defined, PERMUTE otherwise. | |
117 | ||
118 | REQUIRE_ORDER means don't recognize them as options; | |
119 | stop option processing when the first non-option is seen. | |
120 | This is what Unix does. | |
121 | This mode of operation is selected by either setting the environment | |
122 | variable _POSIX_OPTION_ORDER, or using `+' as the first character | |
123 | of the list of option characters. | |
124 | ||
125 | PERMUTE is the default. We permute the contents of ARGV as we scan, | |
126 | so that eventually all the non-options are at the end. This allows options | |
127 | to be given in any order, even with programs that were not written to | |
128 | expect this. | |
129 | ||
130 | RETURN_IN_ORDER is an option available to programs that were written | |
131 | to expect options and other ARGV-elements in any order and that care about | |
132 | the ordering of the two. We describe each non-option ARGV-element | |
133 | as if it were the argument of an option with character code 1. | |
134 | Using `-' as the first character of the list of option characters | |
135 | selects this mode of operation. | |
136 | ||
137 | The special argument `--' forces an end of option-scanning regardless | |
138 | of the value of `ordering'. In the case of RETURN_IN_ORDER, only | |
139 | `--' can cause `getopt' to return EOF with `optind' != ARGC. */ | |
140 | ||
141 | static enum | |
142 | { | |
143 | REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER | |
144 | } ordering; | |
145 | ||
146 | /* Describe the long-named options requested by the application. | |
147 | _GETOPT_LONG_OPTIONS is a vector of `struct option' terminated by an | |
148 | element containing a name which is zero. | |
149 | The field `has_arg' is 1 if the option takes an argument, | |
150 | 2 if it takes an optional argument. */ | |
151 | ||
152 | struct option | |
153 | { | |
154 | char *name; | |
155 | int has_arg; | |
156 | int *flag; | |
157 | int val; | |
158 | }; | |
159 | ||
160 | CONST struct option *_getopt_long_options; | |
161 | ||
162 | int _getopt_long_only = 0; | |
163 | ||
164 | /* Index in _GETOPT_LONG_OPTIONS of the long-named option actually found. | |
165 | Only valid when a long-named option was found. */ | |
166 | ||
167 | int option_index; | |
168 | \f | |
169 | /* Handle permutation of arguments. */ | |
170 | ||
171 | /* Describe the part of ARGV that contains non-options that have | |
172 | been skipped. `first_nonopt' is the index in ARGV of the first of them; | |
173 | `last_nonopt' is the index after the last of them. */ | |
174 | ||
175 | static int first_nonopt; | |
176 | static int last_nonopt; | |
177 | ||
178 | /* Exchange two adjacent subsequences of ARGV. | |
179 | One subsequence is elements [first_nonopt,last_nonopt) | |
180 | which contains all the non-options that have been skipped so far. | |
181 | The other is elements [last_nonopt,optind), which contains all | |
182 | the options processed since those non-options were skipped. | |
183 | ||
184 | `first_nonopt' and `last_nonopt' are relocated so that they describe | |
185 | the new indices of the non-options in ARGV after they are moved. */ | |
186 | ||
187 | static void | |
188 | exchange (argv) | |
189 | char **argv; | |
190 | { | |
191 | int nonopts_size = (last_nonopt - first_nonopt) * sizeof (char *); | |
192 | char **temp = (char **) alloca (nonopts_size); | |
193 | ||
194 | /* Interchange the two blocks of data in ARGV. */ | |
195 | ||
196 | bcopy (&argv[first_nonopt], temp, nonopts_size); | |
197 | bcopy (&argv[last_nonopt], &argv[first_nonopt], | |
198 | (optind - last_nonopt) * sizeof (char *)); | |
199 | bcopy (temp, &argv[first_nonopt + optind - last_nonopt], nonopts_size); | |
200 | ||
201 | /* Update records for the slots the non-options now occupy. */ | |
202 | ||
203 | first_nonopt += (optind - last_nonopt); | |
204 | last_nonopt = optind; | |
205 | } | |
206 | \f | |
207 | /* Scan elements of ARGV (whose length is ARGC) for option characters | |
208 | given in OPTSTRING. | |
209 | ||
210 | If an element of ARGV starts with '-', and is not exactly "-" or "--", | |
211 | then it is an option element. The characters of this element | |
212 | (aside from the initial '-') are option characters. If `getopt' | |
213 | is called repeatedly, it returns successively each of the option characters | |
214 | from each of the option elements. | |
215 | ||
216 | If `getopt' finds another option character, it returns that character, | |
217 | updating `optind' and `nextchar' so that the next call to `getopt' can | |
218 | resume the scan with the following option character or ARGV-element. | |
219 | ||
220 | If there are no more option characters, `getopt' returns `EOF'. | |
221 | Then `optind' is the index in ARGV of the first ARGV-element | |
222 | that is not an option. (The ARGV-elements have been permuted | |
223 | so that those that are not options now come last.) | |
224 | ||
225 | OPTSTRING is a string containing the legitimate option characters. | |
226 | If an option character is seen that is not listed in OPTSTRING, | |
227 | return '?' after printing an error message. If you set `opterr' to | |
228 | zero, the error message is suppressed but we still return '?'. | |
229 | ||
230 | If a char in OPTSTRING is followed by a colon, that means it wants an arg, | |
231 | so the following text in the same ARGV-element, or the text of the following | |
232 | ARGV-element, is returned in `optarg'. Two colons mean an option that | |
233 | wants an optional arg; if there is text in the current ARGV-element, | |
234 | it is returned in `optarg', otherwise `optarg' is set to zero. | |
235 | ||
236 | If OPTSTRING starts with `-' or `+', it requests different methods of | |
237 | handling the non-option ARGV-elements. | |
238 | See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above. | |
239 | ||
240 | Long-named options begin with `+' instead of `-'. | |
241 | Their names may be abbreviated as long as the abbreviation is unique | |
242 | or is an exact match for some defined option. If they have an | |
243 | argument, it follows the option name in the same ARGV-element, separated | |
244 | from the option name by a `=', or else the in next ARGV-element. | |
245 | When `getopt' finds a long-named option, it returns 0 if that option's | |
246 | `flag' field is nonzero, the value of the option's `val' field | |
247 | otherwise. */ | |
248 | ||
249 | int | |
250 | getopt (argc, argv, optstring) | |
251 | int argc; | |
252 | char **argv; | |
253 | CONST char *optstring; | |
254 | { | |
255 | optarg = 0; | |
256 | ||
257 | /* Initialize the internal data when the first call is made. | |
258 | Start processing options with ARGV-element 1 (since ARGV-element 0 | |
259 | is the program name); the sequence of previously skipped | |
260 | non-option ARGV-elements is empty. */ | |
261 | ||
262 | if (optind == 0) | |
263 | { | |
264 | first_nonopt = last_nonopt = optind = 1; | |
265 | ||
266 | nextchar = 0; | |
267 | ||
268 | /* Determine how to handle the ordering of options and nonoptions. */ | |
269 | ||
270 | if (optstring[0] == '-') | |
271 | { | |
272 | ordering = RETURN_IN_ORDER; | |
273 | ++optstring; | |
274 | } | |
275 | else if (optstring[0] == '+') | |
276 | { | |
277 | ordering = REQUIRE_ORDER; | |
278 | ++optstring; | |
279 | } | |
280 | else if (getenv ("_POSIX_OPTION_ORDER") != 0) | |
281 | ordering = REQUIRE_ORDER; | |
282 | else | |
283 | ordering = PERMUTE; | |
284 | } | |
285 | ||
286 | if (nextchar == 0 || *nextchar == 0) | |
287 | { | |
288 | if (ordering == PERMUTE) | |
289 | { | |
290 | /* If we have just processed some options following some non-options, | |
291 | exchange them so that the options come first. */ | |
292 | ||
293 | if (first_nonopt != last_nonopt && last_nonopt != optind) | |
294 | exchange (argv); | |
295 | else if (last_nonopt != optind) | |
296 | first_nonopt = optind; | |
297 | ||
298 | /* Now skip any additional non-options | |
299 | and extend the range of non-options previously skipped. */ | |
300 | ||
301 | while (optind < argc | |
302 | && (argv[optind][0] != '-' | |
303 | || argv[optind][1] == 0) | |
304 | && (_getopt_long_options == 0 | |
305 | || argv[optind][0] != '+' | |
306 | || argv[optind][1] == 0)) | |
307 | optind++; | |
308 | last_nonopt = optind; | |
309 | } | |
310 | ||
311 | /* Special ARGV-element `--' means premature end of options. | |
312 | Skip it like a null option, | |
313 | then exchange with previous non-options as if it were an option, | |
314 | then skip everything else like a non-option. */ | |
315 | ||
316 | if (optind != argc && !strcmp (argv[optind], "--")) | |
317 | { | |
318 | optind++; | |
319 | ||
320 | if (first_nonopt != last_nonopt && last_nonopt != optind) | |
321 | exchange (argv); | |
322 | else if (first_nonopt == last_nonopt) | |
323 | first_nonopt = optind; | |
324 | last_nonopt = argc; | |
325 | ||
326 | optind = argc; | |
327 | } | |
328 | ||
329 | /* If we have done all the ARGV-elements, stop the scan | |
330 | and back over any non-options that we skipped and permuted. */ | |
331 | ||
332 | if (optind == argc) | |
333 | { | |
334 | /* Set the next-arg-index to point at the non-options | |
335 | that we previously skipped, so the caller will digest them. */ | |
336 | if (first_nonopt != last_nonopt) | |
337 | optind = first_nonopt; | |
338 | return EOF; | |
339 | } | |
340 | ||
341 | /* If we have come to a non-option and did not permute it, | |
342 | either stop the scan or describe it to the caller and pass it by. */ | |
343 | ||
344 | if ((argv[optind][0] != '-' || argv[optind][1] == 0) | |
345 | && (_getopt_long_options == 0 | |
346 | || argv[optind][0] != '+' || argv[optind][1] == 0)) | |
347 | { | |
348 | if (ordering == REQUIRE_ORDER) | |
349 | return EOF; | |
350 | optarg = argv[optind++]; | |
351 | return 1; | |
352 | } | |
353 | ||
354 | /* We have found another option-ARGV-element. | |
355 | Start decoding its characters. */ | |
356 | ||
357 | nextchar = argv[optind] + 1; | |
358 | } | |
359 | ||
360 | if (_getopt_long_options != 0 | |
361 | && (argv[optind][0] == '+' | |
362 | || (_getopt_long_only && argv[optind][0] == '-')) | |
363 | ) | |
364 | { | |
365 | CONST struct option *p; | |
366 | char *s = nextchar; | |
367 | int exact = 0; | |
368 | int ambig = 0; | |
369 | CONST struct option *pfound = 0; | |
370 | int indfound; | |
371 | ||
372 | while (*s && *s != '=') | |
373 | s++; | |
374 | ||
375 | /* Test all options for either exact match or abbreviated matches. */ | |
376 | for (p = _getopt_long_options, option_index = 0; p->name; | |
377 | p++, option_index++) | |
378 | if (!strncmp (p->name, nextchar, s - nextchar)) | |
379 | { | |
380 | if (s - nextchar == strlen (p->name)) | |
381 | { | |
382 | /* Exact match found. */ | |
383 | pfound = p; | |
384 | indfound = option_index; | |
385 | exact = 1; | |
386 | break; | |
387 | } | |
388 | else if (pfound == 0) | |
389 | { | |
390 | /* First nonexact match found. */ | |
391 | pfound = p; | |
392 | indfound = option_index; | |
393 | } | |
394 | else | |
395 | /* Second nonexact match found. */ | |
396 | ambig = 1; | |
397 | } | |
398 | ||
399 | if (ambig && !exact) | |
400 | { | |
401 | fprintf (stderr, "%s: option `%s' is ambiguous\n", | |
402 | argv[0], argv[optind]); | |
403 | nextchar += strlen (nextchar); | |
404 | optind++; | |
405 | return '?'; | |
406 | } | |
407 | ||
408 | if (pfound != 0) | |
409 | { | |
410 | option_index = indfound; | |
411 | optind++; | |
412 | if (*s) | |
413 | { | |
414 | if (pfound->has_arg > 0) | |
415 | optarg = s + 1; | |
416 | else | |
417 | { | |
418 | fprintf (stderr, | |
419 | "%s: option `%c%s' doesn't allow an argument\n", | |
420 | argv[0], argv[optind - 1][0], pfound->name); | |
421 | nextchar += strlen (nextchar); | |
422 | return '?'; | |
423 | } | |
424 | } | |
425 | else if (pfound->has_arg == 1) | |
426 | { | |
427 | if (optind < argc) | |
428 | optarg = argv[optind++]; | |
429 | else | |
430 | { | |
431 | fprintf (stderr, "%s: option `%s' requires an argument\n", | |
432 | argv[0], argv[optind - 1]); | |
433 | nextchar += strlen (nextchar); | |
434 | return '?'; | |
435 | } | |
436 | } | |
437 | nextchar += strlen (nextchar); | |
438 | if (pfound->flag) | |
439 | { | |
440 | *(pfound->flag) = pfound->val; | |
441 | return 0; | |
442 | } | |
443 | return pfound->val; | |
444 | } | |
445 | /* Can't find it as a long option. If this is getopt_long_only, | |
446 | and the option starts with '-' and is a valid short | |
447 | option, then interpret it as a short option. Otherwise it's | |
448 | an error. */ | |
449 | if (_getopt_long_only == 0 || argv[optind][0] == '+' || | |
450 | index (optstring, *nextchar) == 0) | |
451 | { | |
452 | if (opterr != 0) | |
453 | fprintf (stderr, "%s: unrecognized option `%c%s'\n", | |
454 | argv[0], argv[optind][0], nextchar); | |
455 | nextchar += strlen (nextchar); | |
456 | optind++; | |
457 | return '?'; | |
458 | } | |
459 | } | |
460 | ||
461 | /* Look at and handle the next option-character. */ | |
462 | ||
463 | { | |
464 | char c = *nextchar++; | |
465 | char *temp = index (optstring, c); | |
466 | ||
467 | /* Increment `optind' when we start to process its last character. */ | |
468 | if (*nextchar == 0) | |
469 | optind++; | |
470 | ||
471 | if (temp == 0 || c == ':') | |
472 | { | |
473 | if (opterr != 0) | |
474 | { | |
475 | if (c < 040 || c >= 0177) | |
476 | fprintf (stderr, "%s: unrecognized option, character code 0%o\n", | |
477 | argv[0], c); | |
478 | else | |
479 | fprintf (stderr, "%s: unrecognized option `-%c'\n", | |
480 | argv[0], c); | |
481 | } | |
482 | return '?'; | |
483 | } | |
484 | if (temp[1] == ':') | |
485 | { | |
486 | if (temp[2] == ':') | |
487 | { | |
488 | /* This is an option that accepts an argument optionally. */ | |
489 | if (*nextchar != 0) | |
490 | { | |
491 | optarg = nextchar; | |
492 | optind++; | |
493 | } | |
494 | else | |
495 | optarg = 0; | |
496 | nextchar = 0; | |
497 | } | |
498 | else | |
499 | { | |
500 | /* This is an option that requires an argument. */ | |
501 | if (*nextchar != 0) | |
502 | { | |
503 | optarg = nextchar; | |
504 | /* If we end this ARGV-element by taking the rest as an arg, | |
505 | we must advance to the next element now. */ | |
506 | optind++; | |
507 | } | |
508 | else if (optind == argc) | |
509 | { | |
510 | if (opterr != 0) | |
511 | fprintf (stderr, "%s: option `-%c' requires an argument\n", | |
512 | argv[0], c); | |
513 | c = '?'; | |
514 | } | |
515 | else | |
516 | /* We already incremented `optind' once; | |
517 | increment it again when taking next ARGV-elt as argument. */ | |
518 | optarg = argv[optind++]; | |
519 | nextchar = 0; | |
520 | } | |
521 | } | |
522 | return c; | |
523 | } | |
524 | } | |
525 | \f | |
526 | #ifdef TEST | |
527 | ||
528 | /* Compile with -DTEST to make an executable for use in testing | |
529 | the above definition of `getopt'. */ | |
530 | ||
531 | int | |
532 | main (argc, argv) | |
533 | int argc; | |
534 | char **argv; | |
535 | { | |
536 | int c; | |
537 | int digit_optind = 0; | |
538 | ||
539 | while (1) | |
540 | { | |
541 | int this_option_optind = optind ? optind : 1; | |
542 | ||
543 | c = getopt (argc, argv, "abc:d:0123456789"); | |
544 | if (c == EOF) | |
545 | break; | |
546 | ||
547 | switch (c) | |
548 | { | |
549 | case '0': | |
550 | case '1': | |
551 | case '2': | |
552 | case '3': | |
553 | case '4': | |
554 | case '5': | |
555 | case '6': | |
556 | case '7': | |
557 | case '8': | |
558 | case '9': | |
559 | if (digit_optind != 0 && digit_optind != this_option_optind) | |
560 | printf ("digits occur in two different argv-elements.\n"); | |
561 | digit_optind = this_option_optind; | |
562 | printf ("option %c\n", c); | |
563 | break; | |
564 | ||
565 | case 'a': | |
566 | printf ("option a\n"); | |
567 | break; | |
568 | ||
569 | case 'b': | |
570 | printf ("option b\n"); | |
571 | break; | |
572 | ||
573 | case 'c': | |
574 | printf ("option c with value `%s'\n", optarg); | |
575 | break; | |
576 | ||
577 | case '?': | |
578 | break; | |
579 | ||
580 | default: | |
581 | printf ("?? getopt returned character code 0%o ??\n", c); | |
582 | } | |
583 | } | |
584 | ||
585 | if (optind < argc) | |
586 | { | |
587 | printf ("non-option ARGV-elements: "); | |
588 | while (optind < argc) | |
589 | printf ("%s ", argv[optind++]); | |
590 | printf ("\n"); | |
591 | } | |
592 | ||
593 | exit (0); | |
594 | } | |
595 | ||
596 | #endif /* TEST */ |