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