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)
24 # include <sys/types.h>
30 #include "../memalloc.h"
34 /* For communication from `sh_getopt' to the caller.
35 When `sh_getopt' finds an option that takes an argument,
36 the argument value is returned here. */
39 /* Index in ARGV of the next element to be scanned.
40 This is used for communication to and from the caller
41 and for communication between successive calls to `sh_getopt'.
43 On entry to `sh_getopt', zero means this is the first call; initialize.
45 When `sh_getopt' returns EOF, this is the index of the first of the
46 non-option elements that the caller should itself scan.
48 Otherwise, `sh_optind' communicates from one call to the next
49 how much of ARGV has been scanned so far. */
51 /* XXX 1003.2 says this must be 1 before any call. */
54 /* Index of the current argument. */
57 /* The next char to be scanned in the option-element
58 in which the last option character we returned was found.
59 This allows us to pick up the scan where we left off.
61 If this is zero, or a null string, it means resume the scan
62 by advancing to the next ARGV-element. */
64 static char *nextchar;
65 static int sh_charindex;
67 /* Callers store zero here to inhibit the error message
68 for unrecognized options. */
72 /* Set to an option character which was unrecognized.
73 This must be initialized on some systems to avoid linking in the
74 system's own getopt implementation. */
78 /* Set to 1 when we see an illegal option; public so getopts can reset it. */
81 /* Scan elements of ARGV (whose length is ARGC) for option characters
84 If an element of ARGV starts with '-', and is not exactly "-" or "--",
85 then it is an option element. The characters of this element
86 (aside from the initial '-') are option characters. If `sh_getopt'
87 is called repeatedly, it returns successively each of the option characters
88 from each of the option elements.
90 If `sh_getopt' finds another option character, it returns that character,
91 updating `sh_optind' and `nextchar' so that the next call to `sh_getopt' can
92 resume the scan with the following option character or ARGV-element.
94 If there are no more option characters, `sh_getopt' returns `EOF'.
95 Then `sh_optind' is the index in ARGV of the first ARGV-element
96 that is not an option.
98 OPTSTRING is a string containing the legitimate option characters.
99 If an option character is seen that is not listed in OPTSTRING,
100 return '?' after printing an error message. If you set `sh_opterr' to
101 zero, the error message is suppressed but we still return '?'.
103 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
104 so the following text in the same ARGV-element, or the text of the following
105 ARGV-element, is returned in `sh_optarg'. */
107 /* 1003.2 specifies the format of this message. */
108 #define BADOPT(x) fprintf (stderr, "%s: illegal option -- %c\n", argv[0], x)
109 #define NEEDARG(x) fprintf (stderr, "%s: option requires an argument -- %c\n", argv[0], x)
112 sh_getopt (argc, argv, optstring)
115 const char *optstring;
121 if (sh_optind >= argc || sh_optind < 0) /* XXX was sh_optind > argc */
127 /* Initialize the internal data when the first call is made.
128 Start processing options with ARGV-element 1 (since ARGV-element 0
129 is the program name); the sequence of previously skipped
130 non-option ARGV-elements is empty. */
135 nextchar = (char *)NULL;
138 /* Do the increment of `sh_optind' we deferred because the last option
140 if (sh_badopt && (nextchar == 0 || *nextchar == '\0'))
144 nextchar = (char *)NULL;
147 if (nextchar == 0 || *nextchar == '\0')
149 /* If we have done all the ARGV-elements, stop the scan. */
150 if (sh_optind >= argc)
153 temp = argv[sh_optind];
155 /* Special ARGV-element `--' means premature end of options.
156 Skip it like a null option, and return EOF. */
157 if (temp[0] == '-' && temp[1] == '-' && temp[2] == '\0')
163 /* If we have come to a non-option, either stop the scan or describe
164 it to the caller and pass it by. This makes the pseudo-option
165 `-' mean the end of options, but does not skip over it. */
166 if (temp[0] != '-' || temp[1] == '\0')
169 /* We have found another option-ARGV-element.
170 Start decoding its characters. */
171 nextchar = argv[sh_curopt = sh_optind] + 1;
175 /* Look at and handle the next option-character. */
177 c = *nextchar++; sh_charindex++;
178 temp = strchr (optstring, c);
182 /* If the option is illegal, return an error, but defer updating sh_optind
183 until the next call so $OPTIND is correct. */
184 if (sh_badopt = (temp == NULL || c == ':'))
192 /* Increment `sh_optind' when we start to process its last character. */
193 if (nextchar == 0 || *nextchar == '\0')
196 nextchar = (char *)NULL;
201 if (nextchar && *nextchar)
203 /* This is an option that requires an argument. */
204 sh_optarg = nextchar;
205 /* If we end this ARGV-element by taking the rest as an arg,
206 we must advance to the next element now. */
209 else if (sh_optind == argc)
215 sh_optarg = ""; /* Needed by getopts. */
216 c = (optstring[0] == ':') ? ':' : '?';
219 /* We already incremented `sh_optind' once;
220 increment it again when taking next ARGV-elt as argument. */
221 sh_optarg = argv[sh_optind++];
222 nextchar = (char *)NULL;
228 sh_getopt_restore_state (argv)
232 nextchar = argv[sh_curopt] + sh_charindex;
237 /* Compile with -DTEST to make an executable for use in testing
238 the above definition of `sh_getopt'. */
246 int digit_sh_optind = 0;
250 int this_option_sh_optind = sh_optind ? sh_optind : 1;
252 c = sh_getopt (argc, argv, "abc:d:0123456789");
268 if (digit_sh_optind != 0 && digit_sh_optind != this_option_sh_optind)
269 printf ("digits occur in two different argv-elements.\n");
270 digit_sh_optind = this_option_sh_optind;
271 printf ("option %c\n", c);
275 printf ("option a\n");
279 printf ("option b\n");
283 printf ("option c with value `%s'\n", sh_optarg);
290 printf ("?? sh_getopt returned character code 0%o ??\n", c);
294 if (sh_optind < argc)
296 printf ("non-option ARGV-elements: ");
297 while (sh_optind < argc)
298 printf ("%s ", argv[sh_optind++]);