3 Copyright (C) 1993, 1994
4 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #if defined (HAVE_UNISTD_H)
27 #include "../memalloc.h"
31 /* For communication from `sh_getopt' to the caller.
32 When `sh_getopt' finds an option that takes an argument,
33 the argument value is returned here. */
36 /* Index in ARGV of the next element to be scanned.
37 This is used for communication to and from the caller
38 and for communication between successive calls to `sh_getopt'.
40 On entry to `sh_getopt', zero means this is the first call; initialize.
42 When `sh_getopt' returns EOF, this is the index of the first of the
43 non-option elements that the caller should itself scan.
45 Otherwise, `sh_optind' communicates from one call to the next
46 how much of ARGV has been scanned so far. */
48 /* XXX 1003.2 says this must be 1 before any call. */
51 /* Index of the current argument. */
54 /* The next char to be scanned in the option-element
55 in which the last option character we returned was found.
56 This allows us to pick up the scan where we left off.
58 If this is zero, or a null string, it means resume the scan
59 by advancing to the next ARGV-element. */
61 static char *nextchar;
62 static int sh_charindex;
64 /* Callers store zero here to inhibit the error message
65 for unrecognized options. */
69 /* Set to an option character which was unrecognized.
70 This must be initialized on some systems to avoid linking in the
71 system's own getopt implementation. */
75 /* Set to 1 when we see an illegal option; public so getopts can reset it. */
78 /* Scan elements of ARGV (whose length is ARGC) for option characters
81 If an element of ARGV starts with '-', and is not exactly "-" or "--",
82 then it is an option element. The characters of this element
83 (aside from the initial '-') are option characters. If `sh_getopt'
84 is called repeatedly, it returns successively each of the option characters
85 from each of the option elements.
87 If `sh_getopt' finds another option character, it returns that character,
88 updating `sh_optind' and `nextchar' so that the next call to `sh_getopt' can
89 resume the scan with the following option character or ARGV-element.
91 If there are no more option characters, `sh_getopt' returns `EOF'.
92 Then `sh_optind' is the index in ARGV of the first ARGV-element
93 that is not an option.
95 OPTSTRING is a string containing the legitimate option characters.
96 If an option character is seen that is not listed in OPTSTRING,
97 return '?' after printing an error message. If you set `sh_opterr' to
98 zero, the error message is suppressed but we still return '?'.
100 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
101 so the following text in the same ARGV-element, or the text of the following
102 ARGV-element, is returned in `sh_optarg'. */
104 /* 1003.2 specifies the format of this message. */
105 #define BADOPT(x) fprintf (stderr, "%s: illegal option -- %c\n", argv[0], x)
106 #define NEEDARG(x) fprintf (stderr, "%s: option requires an argument -- %c\n", argv[0], x)
109 sh_getopt (argc, argv, optstring)
112 const char *optstring;
118 if (sh_optind >= argc || sh_optind < 0) /* XXX was sh_optind > argc */
124 /* Initialize the internal data when the first call is made.
125 Start processing options with ARGV-element 1 (since ARGV-element 0
126 is the program name); the sequence of previously skipped
127 non-option ARGV-elements is empty. */
132 nextchar = (char *)NULL;
135 /* Do the increment of `sh_optind' we deferred because the last option
137 if (sh_badopt && (nextchar == 0 || *nextchar == '\0'))
141 nextchar = (char *)NULL;
144 if (nextchar == 0 || *nextchar == '\0')
146 /* If we have done all the ARGV-elements, stop the scan. */
147 if (sh_optind >= argc)
150 temp = argv[sh_optind];
152 /* Special ARGV-element `--' means premature end of options.
153 Skip it like a null option, and return EOF. */
154 if (temp[0] == '-' && temp[1] == '-' && temp[2] == '\0')
160 /* If we have come to a non-option, either stop the scan or describe
161 it to the caller and pass it by. This makes the pseudo-option
162 `-' mean the end of options, but does not skip over it. */
163 if (temp[0] != '-' || temp[1] == '\0')
166 /* We have found another option-ARGV-element.
167 Start decoding its characters. */
168 nextchar = argv[sh_curopt = sh_optind] + 1;
172 /* Look at and handle the next option-character. */
174 c = *nextchar++; sh_charindex++;
175 temp = strchr (optstring, c);
179 /* If the option is illegal, return an error, but defer updating sh_optind
180 until the next call so $OPTIND is correct. */
181 if (sh_badopt = (temp == NULL || c == ':'))
189 /* Increment `sh_optind' when we start to process its last character. */
190 if (nextchar == 0 || *nextchar == '\0')
193 nextchar = (char *)NULL;
198 if (nextchar && *nextchar)
200 /* This is an option that requires an argument. */
201 sh_optarg = nextchar;
202 /* If we end this ARGV-element by taking the rest as an arg,
203 we must advance to the next element now. */
206 else if (sh_optind == argc)
212 sh_optarg = ""; /* Needed by getopts. */
213 c = (optstring[0] == ':') ? ':' : '?';
216 /* We already incremented `sh_optind' once;
217 increment it again when taking next ARGV-elt as argument. */
218 sh_optarg = argv[sh_optind++];
219 nextchar = (char *)NULL;
225 sh_getopt_restore_state (argv)
229 nextchar = argv[sh_curopt] + sh_charindex;
234 /* Compile with -DTEST to make an executable for use in testing
235 the above definition of `sh_getopt'. */
243 int digit_sh_optind = 0;
247 int this_option_sh_optind = sh_optind ? sh_optind : 1;
249 c = sh_getopt (argc, argv, "abc:d:0123456789");
265 if (digit_sh_optind != 0 && digit_sh_optind != this_option_sh_optind)
266 printf ("digits occur in two different argv-elements.\n");
267 digit_sh_optind = this_option_sh_optind;
268 printf ("option %c\n", c);
272 printf ("option a\n");
276 printf ("option b\n");
280 printf ("option c with value `%s'\n", sh_optarg);
287 printf ("?? sh_getopt returned character code 0%o ??\n", c);
291 if (sh_optind < argc)
293 printf ("non-option ARGV-elements: ");
294 while (sh_optind < argc)
295 printf ("%s ", argv[sh_optind++]);