1 /* getopt.c - getopt for Bash. Used by the getopt builtin. */
3 /* Copyright (C) 1993-2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #if defined (HAVE_UNISTD_H)
25 # include <sys/types.h>
32 #include "../bashintl.h"
36 /* For communication from `sh_getopt' to the caller.
37 When `sh_getopt' finds an option that takes an argument,
38 the argument value is returned here. */
41 /* Index in ARGV of the next element to be scanned.
42 This is used for communication to and from the caller
43 and for communication between successive calls to `sh_getopt'.
45 On entry to `sh_getopt', zero means this is the first call; initialize.
47 When `sh_getopt' returns EOF, this is the index of the first of the
48 non-option elements that the caller should itself scan.
50 Otherwise, `sh_optind' communicates from one call to the next
51 how much of ARGV has been scanned so far. */
53 /* XXX 1003.2 says this must be 1 before any call. */
56 /* Index of the current argument. */
59 /* The next char to be scanned in the option-element
60 in which the last option character we returned was found.
61 This allows us to pick up the scan where we left off.
63 If this is zero, or a null string, it means resume the scan
64 by advancing to the next ARGV-element. */
66 static char *nextchar;
67 static int sh_charindex;
69 /* Callers store zero here to inhibit the error message
70 for unrecognized options. */
74 /* Set to an option character which was unrecognized.
75 This must be initialized on some systems to avoid linking in the
76 system's own getopt implementation. */
80 /* Set to 1 when we see an invalid option; public so getopts can reset it. */
83 /* Scan elements of ARGV (whose length is ARGC) for option characters
86 If an element of ARGV starts with '-', and is not exactly "-" or "--",
87 then it is an option element. The characters of this element
88 (aside from the initial '-') are option characters. If `sh_getopt'
89 is called repeatedly, it returns successively each of the option characters
90 from each of the option elements.
92 If `sh_getopt' finds another option character, it returns that character,
93 updating `sh_optind' and `nextchar' so that the next call to `sh_getopt' can
94 resume the scan with the following option character or ARGV-element.
96 If there are no more option characters, `sh_getopt' returns `EOF'.
97 Then `sh_optind' is the index in ARGV of the first ARGV-element
98 that is not an option.
100 OPTSTRING is a string containing the legitimate option characters.
101 If an option character is seen that is not listed in OPTSTRING,
102 return '?' after printing an error message. If you set `sh_opterr' to
103 zero, the error message is suppressed but we still return '?'.
105 If a char in OPTSTRING is followed by a colon, that means it wants an arg,
106 so the following text in the same ARGV-element, or the text of the following
107 ARGV-element, is returned in `sh_optarg'. */
109 /* 1003.2 specifies the format of this message. */
110 #define BADOPT(x) fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], x)
111 #define NEEDARG(x) fprintf (stderr, _("%s: option requires an argument -- %c\n"), argv[0], x)
114 sh_getopt (argc, argv, optstring)
117 const char *optstring;
123 if (sh_optind >= argc || sh_optind < 0) /* XXX was sh_optind > argc */
129 /* Initialize the internal data when the first call is made.
130 Start processing options with ARGV-element 1 (since ARGV-element 0
131 is the program name); the sequence of previously skipped
132 non-option ARGV-elements is empty. */
137 nextchar = (char *)NULL;
140 if (nextchar == 0 || *nextchar == '\0')
142 /* If we have done all the ARGV-elements, stop the scan. */
143 if (sh_optind >= argc)
146 temp = argv[sh_optind];
148 /* Special ARGV-element `--' means premature end of options.
149 Skip it like a null option, and return EOF. */
150 if (temp[0] == '-' && temp[1] == '-' && temp[2] == '\0')
156 /* If we have come to a non-option, either stop the scan or describe
157 it to the caller and pass it by. This makes the pseudo-option
158 `-' mean the end of options, but does not skip over it. */
159 if (temp[0] != '-' || temp[1] == '\0')
162 /* We have found another option-ARGV-element.
163 Start decoding its characters. */
164 nextchar = argv[sh_curopt = sh_optind] + 1;
168 /* Look at and handle the next option-character. */
170 c = *nextchar++; sh_charindex++;
171 temp = strchr (optstring, c);
175 /* Increment `sh_optind' when we start to process its last character. */
176 if (nextchar == 0 || *nextchar == '\0')
179 nextchar = (char *)NULL;
182 if (sh_badopt = (temp == NULL || c == ':'))
192 if (nextchar && *nextchar)
194 /* This is an option that requires an argument. */
195 sh_optarg = nextchar;
196 /* If we end this ARGV-element by taking the rest as an arg,
197 we must advance to the next element now. */
200 else if (sh_optind == argc)
206 sh_optarg = ""; /* Needed by getopts. */
207 c = (optstring[0] == ':') ? ':' : '?';
210 /* We already incremented `sh_optind' once;
211 increment it again when taking next ARGV-elt as argument. */
212 sh_optarg = argv[sh_optind++];
213 nextchar = (char *)NULL;
219 sh_getopt_restore_state (argv)
223 nextchar = argv[sh_curopt] + sh_charindex;
228 sh_getopt_debug_restore_state (argv)
231 if (nextchar && nextchar != argv[sh_curopt] + sh_charindex)
233 itrace("sh_getopt_debug_restore_state: resetting nextchar");
234 nextchar = argv[sh_curopt] + sh_charindex;
241 /* Compile with -DTEST to make an executable for use in testing
242 the above definition of `sh_getopt'. */
250 int digit_sh_optind = 0;
254 int this_option_sh_optind = sh_optind ? sh_optind : 1;
256 c = sh_getopt (argc, argv, "abc:d:0123456789");
272 if (digit_sh_optind != 0 && digit_sh_optind != this_option_sh_optind)
273 printf ("digits occur in two different argv-elements.\n");
274 digit_sh_optind = this_option_sh_optind;
275 printf ("option %c\n", c);
279 printf ("option a\n");
283 printf ("option b\n");
287 printf ("option c with value `%s'\n", sh_optarg);
294 printf ("?? sh_getopt returned character code 0%o ??\n", c);
298 if (sh_optind < argc)
300 printf ("non-option ARGV-elements: ");
301 while (sh_optind < argc)
302 printf ("%s ", argv[sh_optind++]);