1 This file is getopts.def, from which is created getopts.c.
2 It implements the builtin "getopts" in Bash.
4 Copyright (C) 1987-2015 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash 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.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 $FUNCTION getopts_builtin
25 $SHORT_DOC getopts optstring name [arg]
26 Parse option arguments.
28 Getopts is used by shell procedures to parse positional parameters
31 OPTSTRING contains the option letters to be recognized; if a letter
32 is followed by a colon, the option is expected to have an argument,
33 which should be separated from it by white space.
35 Each time it is invoked, getopts will place the next option in the
36 shell variable $name, initializing name if it does not exist, and
37 the index of the next argument to be processed into the shell
38 variable OPTIND. OPTIND is initialized to 1 each time the shell or
39 a shell script is invoked. When an option requires an argument,
40 getopts places that argument into the shell variable OPTARG.
42 getopts reports errors in one of two ways. If the first character
43 of OPTSTRING is a colon, getopts uses silent error reporting. In
44 this mode, no error messages are printed. If an invalid option is
45 seen, getopts places the option character found into OPTARG. If a
46 required argument is not found, getopts places a ':' into NAME and
47 sets OPTARG to the option character found. If getopts is not in
48 silent mode, and an invalid option is seen, getopts places '?' into
49 NAME and unsets OPTARG. If a required argument is not found, a '?'
50 is placed in NAME, OPTARG is unset, and a diagnostic message is
53 If the shell variable OPTERR has the value 0, getopts disables the
54 printing of error messages, even if the first character of
55 OPTSTRING is not a colon. OPTERR has the value 1 by default.
57 Getopts normally parses the positional parameters ($0 - $9), but if
58 more arguments are given, they are parsed instead.
61 Returns success if an option is found; fails if the end of options is
62 encountered or an error occurs.
69 #if defined (HAVE_UNISTD_H)
71 # include <sys/types.h>
76 #include "../bashansi.h"
77 #include "../bashintl.h"
80 #include "../execute_cmd.h"
82 #include "bashgetopt.h"
86 #define G_INVALID_OPT -2
87 #define G_ARG_MISSING -3
89 static int getopts_unbind_variable __P((char *));
90 static int getopts_bind_variable __P((char *, char *));
91 static int dogetopts __P((int, char **));
93 /* getopts_reset is magic code for when OPTIND is reset. N is the
94 value that has just been assigned to OPTIND. */
96 getopts_reset (newind)
104 getopts_unbind_variable (name)
108 return (unbind_variable (name));
110 return (unbind_variable_noref (name));
115 getopts_bind_variable (name, value)
120 if (legal_identifier (name))
122 v = bind_variable (name, value, 0);
123 if (v && (readonly_p (v) || noassign_p (v)))
124 return (EX_MISCERROR);
125 return (v ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
130 return (EXECUTION_FAILURE);
134 /* Error handling is now performed as specified by Posix.2, draft 11
135 (identical to that of ksh-88). The special handling is enabled if
136 the first character of the option string is a colon; this handling
137 disables diagnostic messages concerning missing option arguments
138 and invalid option characters. The handling is as follows.
142 if (special_error) then
143 OPTARG = option character found
150 MISSING OPTION ARGUMENT;
151 if (special_error) then
153 OPTARG = option character found
162 dogetopts (argc, argv)
166 int ret, special_error, old_opterr, i, n;
167 char strval[2], numval[16];
168 char *optstr; /* list of options */
169 char *name; /* variable to get flag val */
178 /* argv[0] is "getopts". */
185 special_error = optstr[0] == ':';
189 old_opterr = sh_opterr;
191 sh_opterr = 0; /* suppress diagnostic messages */
196 sh_getopt_restore_state (argv);
198 argv[0] = dollar_vars[0];
199 ret = sh_getopt (argc, argv, optstr);
202 else if (rest_of_args == (WORD_LIST *)NULL)
204 for (i = 0; i < 10 && dollar_vars[i]; i++)
207 sh_getopt_restore_state (dollar_vars);
208 ret = sh_getopt (i, dollar_vars, optstr);
212 register WORD_LIST *words;
215 for (i = 0; i < 10 && dollar_vars[i]; i++)
217 for (words = rest_of_args; words; words = words->next, i++)
219 v = strvec_create (i + 1);
220 for (i = 0; i < 10 && dollar_vars[i]; i++)
221 v[i] = dollar_vars[i];
222 for (words = rest_of_args; words; words = words->next, i++)
223 v[i] = words->word->word;
225 sh_getopt_restore_state (v);
226 ret = sh_getopt (i, v, optstr);
231 sh_opterr = old_opterr;
233 /* Set the OPTIND variable in any case, to handle "--" skipping. It's
234 highly unlikely that 14 digits will be too few. */
237 numval[14] = sh_optind + '0';
243 numval[i = 15] = '\0';
247 numval[--i] = (n % 10) + '0';
251 bind_variable ("OPTIND", numval + i, 0);
253 /* If an error occurred, decide which one it is and set the return
254 code appropriately. In all cases, the option character in error
255 is in OPTOPT. If an invalid option was encountered, OPTARG is
256 NULL. If a required option argument was missing, OPTARG points
257 to a NULL string (that is, sh_optarg[0] == 0). */
260 if (sh_optarg == NULL)
262 else if (sh_optarg[0] == '\0')
268 getopts_unbind_variable ("OPTARG");
269 getopts_bind_variable (name, "?");
270 return (EXECUTION_FAILURE);
273 if (ret == G_INVALID_OPT)
275 /* Invalid option encountered. */
276 ret = getopts_bind_variable (name, "?");
280 strval[0] = (char)sh_optopt;
282 bind_variable ("OPTARG", strval, 0);
285 getopts_unbind_variable ("OPTARG");
290 if (ret == G_ARG_MISSING)
292 /* Required argument missing. */
295 ret = getopts_bind_variable (name, ":");
297 strval[0] = (char)sh_optopt;
299 bind_variable ("OPTARG", strval, 0);
303 ret = getopts_bind_variable (name, "?");
304 getopts_unbind_variable ("OPTARG");
309 bind_variable ("OPTARG", sh_optarg, 0);
311 strval[0] = (char) ret;
313 return (getopts_bind_variable (name, strval));
316 /* The getopts builtin. Build an argv, and call dogetopts with it. */
318 getopts_builtin (list)
330 reset_internal_getopt ();
331 if ((ret = internal_getopt (list, "")) != -1)
333 if (ret == GETOPT_HELP)
341 av = make_builtin_argv (list, &ac);
342 ret = dogetopts (ac, av);