1 This file is getopts.def, from which is created getopts.c.
2 It implements the builtin "getopts" in Bash.
4 Copyright (C) 1987-2004 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 it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $FUNCTION getopts_builtin
26 $SHORT_DOC getopts optstring name [arg]
27 Parse option arguments.
29 Getopts is used by shell procedures to parse positional parameters
32 OPTSTRING contains the option letters to be recognized; if a letter
33 is followed by a colon, the option is expected to have an argument,
34 which should be separated from it by white space.
36 Each time it is invoked, getopts will place the next option in the
37 shell variable $name, initializing name if it does not exist, and
38 the index of the next argument to be processed into the shell
39 variable OPTIND. OPTIND is initialized to 1 each time the shell or
40 a shell script is invoked. When an option requires an argument,
41 getopts places that argument into the shell variable OPTARG.
43 getopts reports errors in one of two ways. If the first character
44 of OPTSTRING is a colon, getopts uses silent error reporting. In
45 this mode, no error messages are printed. If an invalid option is
46 seen, getopts places the option character found into OPTARG. If a
47 required argument is not found, getopts places a ':' into NAME and
48 sets OPTARG to the option character found. If getopts is not in
49 silent mode, and an invalid option is seen, getopts places '?' into
50 NAME and unsets OPTARG. If a required argument is not found, a '?'
51 is placed in NAME, OPTARG is unset, and a diagnostic message is
54 If the shell variable OPTERR has the value 0, getopts disables the
55 printing of error messages, even if the first character of
56 OPTSTRING is not a colon. OPTERR has the value 1 by default.
58 Getopts normally parses the positional parameters ($0 - $9), but if
59 more arguments are given, they are parsed instead.
62 Returns success if an option is found; fails if the end of options is
63 encountered or an error occurs.
70 #if defined (HAVE_UNISTD_H)
72 # include <sys/types.h>
77 #include "../bashansi.h"
81 #include "bashgetopt.h"
85 #define G_INVALID_OPT -2
86 #define G_ARG_MISSING -3
88 extern char *this_command_name;
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_bind_variable (name, value)
109 if (legal_identifier (name))
111 v = bind_variable (name, value, 0);
112 return (v && (readonly_p (v) == 0)) ? EXECUTION_SUCCESS : EXECUTION_FAILURE;
117 return (EXECUTION_FAILURE);
121 /* Error handling is now performed as specified by Posix.2, draft 11
122 (identical to that of ksh-88). The special handling is enabled if
123 the first character of the option string is a colon; this handling
124 disables diagnostic messages concerning missing option arguments
125 and invalid option characters. The handling is as follows.
129 if (special_error) then
130 OPTARG = option character found
137 MISSING OPTION ARGUMENT;
138 if (special_error) then
140 OPTARG = option character found
149 dogetopts (argc, argv)
153 int ret, special_error, old_opterr, i, n;
154 char strval[2], numval[16];
155 char *optstr; /* list of options */
156 char *name; /* variable to get flag val */
165 /* argv[0] is "getopts". */
172 special_error = optstr[0] == ':';
176 old_opterr = sh_opterr;
178 sh_opterr = 0; /* suppress diagnostic messages */
183 sh_getopt_restore_state (argv);
185 argv[0] = dollar_vars[0];
186 ret = sh_getopt (argc, argv, optstr);
189 else if (rest_of_args == (WORD_LIST *)NULL)
191 for (i = 0; i < 10 && dollar_vars[i]; i++)
194 sh_getopt_restore_state (dollar_vars);
195 ret = sh_getopt (i, dollar_vars, optstr);
199 register WORD_LIST *words;
202 for (i = 0; i < 10 && dollar_vars[i]; i++)
204 for (words = rest_of_args; words; words = words->next, i++)
206 v = strvec_create (i + 1);
207 for (i = 0; i < 10 && dollar_vars[i]; i++)
208 v[i] = dollar_vars[i];
209 for (words = rest_of_args; words; words = words->next, i++)
210 v[i] = words->word->word;
212 sh_getopt_restore_state (v);
213 ret = sh_getopt (i, v, optstr);
218 sh_opterr = old_opterr;
220 /* Set the OPTIND variable in any case, to handle "--" skipping. It's
221 highly unlikely that 14 digits will be too few. */
224 numval[14] = sh_optind + '0';
230 numval[i = 15] = '\0';
234 numval[--i] = (n % 10) + '0';
238 bind_variable ("OPTIND", numval + i, 0);
240 /* If an error occurred, decide which one it is and set the return
241 code appropriately. In all cases, the option character in error
242 is in OPTOPT. If an invalid option was encountered, OPTARG is
243 NULL. If a required option argument was missing, OPTARG points
244 to a NULL string (that is, sh_optarg[0] == 0). */
247 if (sh_optarg == NULL)
249 else if (sh_optarg[0] == '\0')
255 unbind_variable ("OPTARG");
256 getopts_bind_variable (name, "?");
257 return (EXECUTION_FAILURE);
260 if (ret == G_INVALID_OPT)
262 /* Invalid option encountered. */
263 ret = getopts_bind_variable (name, "?");
267 strval[0] = (char)sh_optopt;
269 bind_variable ("OPTARG", strval, 0);
272 unbind_variable ("OPTARG");
277 if (ret == G_ARG_MISSING)
279 /* Required argument missing. */
282 ret = getopts_bind_variable (name, ":");
284 strval[0] = (char)sh_optopt;
286 bind_variable ("OPTARG", strval, 0);
290 ret = getopts_bind_variable (name, "?");
291 unbind_variable ("OPTARG");
296 bind_variable ("OPTARG", sh_optarg, 0);
298 strval[0] = (char) ret;
300 return (getopts_bind_variable (name, strval));
303 /* The getopts builtin. Build an argv, and call dogetopts with it. */
305 getopts_builtin (list)
317 reset_internal_getopt ();
318 if (internal_getopt (list, "") != -1)
325 av = make_builtin_argv (list, &ac);
326 ret = dogetopts (ac, av);