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 Getopts is used by shell procedures to parse positional parameters.
29 OPTSTRING contains the option letters to be recognized
; if a letter
30 is followed by a colon
, the option is expected to have an argument
,
31 which should be separated from it by white space.
33 Each time it is invoked
, getopts will place the next option in the
34 shell variable $name
, initializing name if it does not exist
, and
35 the index of the next argument to be processed into the shell
36 variable OPTIND. OPTIND is initialized to
1 each time the shell or
37 a shell script is invoked. When an option requires an argument
,
38 getopts places that argument into the shell variable OPTARG.
40 getopts reports errors in one of two ways. If the first character
41 of OPTSTRING is a colon
, getopts uses silent error reporting. In
42 this mode
, no error messages are printed. If an invalid option is
43 seen
, getopts places the option character found into OPTARG. If a
44 required argument is not found
, getopts places a
':' into NAME and
45 sets OPTARG to the option character found. If getopts is not in
46 silent mode
, and an invalid option is seen
, getopts places
'?' into
47 NAME and unsets OPTARG. If a required argument is not found
, a
'?'
48 is placed in NAME
, OPTARG is unset
, and a diagnostic message is
51 If the shell variable OPTERR has the value
0, getopts disables the
52 printing of error messages
, even if the first character of
53 OPTSTRING is not a colon. OPTERR has the value
1 by default.
55 Getopts normally parses the positional
parameters ($
0 - $
9), but if
56 more arguments are given
, they are parsed instead.
63 #if
defined (HAVE_UNISTD_H
)
65 # include
<sys
/types.h
>
70 #include
"../bashansi.h"
74 #include
"bashgetopt.h"
78 #define G_INVALID_OPT
-2
79 #define G_ARG_MISSING
-3
81 extern char
*this_command_name
;
83 static int getopts_bind_variable
__P((char
*, char *)
);
84 static int dogetopts
__P((int
, char
**)
);
86 /* getopts_reset is magic code for when OPTIND is reset. N is the
87 value that has just been assigned to OPTIND.
*/
89 getopts_reset (newind
)
97 getopts_bind_variable (name
, value
)
102 if (legal_identifier (name
))
104 v
= bind_variable (name
, value
, 0);
105 return (v
&& (readonly_p (v
) == 0)) ? EXECUTION_SUCCESS
: EXECUTION_FAILURE
;
110 return (EXECUTION_FAILURE
);
114 /* Error handling is now performed as specified by Posix
.2, draft
11
115 (identical to that of ksh
-88). The special handling is enabled if
116 the first character of the option string is a colon
; this handling
117 disables diagnostic messages concerning missing option arguments
118 and invalid option characters. The handling is as follows.
122 if (special_error
) then
123 OPTARG
= option character found
130 MISSING OPTION ARGUMENT
;
131 if (special_error
) then
133 OPTARG
= option character found
142 dogetopts (argc
, argv
)
146 int ret
, special_error
, old_opterr
, i
, n
;
147 char strval
[2], numval
[16];
148 char
*optstr
; /* list of options
*/
149 char
*name
; /* variable to get flag val
*/
158 /* argv
[0] is
"getopts".
*/
165 special_error
= optstr
[0] == ':';
169 old_opterr
= sh_opterr
;
171 sh_opterr
= 0; /* suppress diagnostic messages
*/
176 sh_getopt_restore_state (argv
);
178 argv
[0] = dollar_vars
[0];
179 ret
= sh_getopt (argc
, argv
, optstr
);
182 else
if (rest_of_args
== (WORD_LIST *)NULL
)
184 for (i
= 0; i
< 10 && dollar_vars
[i
]; i
++)
187 sh_getopt_restore_state (dollar_vars
);
188 ret
= sh_getopt (i
, dollar_vars
, optstr
);
192 register WORD_LIST
*words
;
195 for (i
= 0; i
< 10 && dollar_vars
[i
]; i
++)
197 for (words
= rest_of_args
; words
; words
= words
->next
, i
++)
199 v
= strvec_create (i
+ 1);
200 for (i
= 0; i
< 10 && dollar_vars
[i
]; i
++)
201 v
[i
] = dollar_vars
[i
];
202 for (words
= rest_of_args
; words
; words
= words
->next
, i
++)
203 v
[i
] = words
->word
->word
;
205 sh_getopt_restore_state (v
);
206 ret
= sh_getopt (i
, v
, optstr
);
211 sh_opterr
= old_opterr
;
213 /* Set the OPTIND variable in any case
, to handle
"--" skipping. It
's
214 highly unlikely that 14 digits will be too few. */
217 numval[14] = sh_optind + '0';
223 numval[i = 15] = '\
0';
227 numval[--i] = (n % 10) + '0';
231 bind_variable ("OPTIND", numval + i, 0);
233 /* If an error occurred, decide which one it is and set the return
234 code appropriately. In all cases, the option character in error
235 is in OPTOPT. If an invalid option was encountered, OPTARG is
236 NULL. If a required option argument was missing, OPTARG points
237 to a NULL string (that is, sh_optarg[0] == 0). */
240 if (sh_optarg == NULL)
242 else if (sh_optarg[0] == '\
0')
248 unbind_variable ("OPTARG");
249 getopts_bind_variable (name, "?");
250 return (EXECUTION_FAILURE);
253 if (ret == G_INVALID_OPT)
255 /* Invalid option encountered. */
256 ret = getopts_bind_variable (name, "?");
260 strval[0] = (char)sh_optopt;
262 bind_variable ("OPTARG", strval, 0);
265 unbind_variable ("OPTARG");
270 if (ret == G_ARG_MISSING)
272 /* Required argument missing. */
275 ret = getopts_bind_variable (name, ":");
277 strval[0] = (char)sh_optopt;
279 bind_variable ("OPTARG", strval, 0);
283 ret = getopts_bind_variable (name, "?");
284 unbind_variable ("OPTARG");
289 bind_variable ("OPTARG", sh_optarg, 0);
291 strval[0] = (char) ret;
293 return (getopts_bind_variable (name, strval));
296 /* The getopts builtin. Build an argv, and call dogetopts with it. */
298 getopts_builtin (list)
310 reset_internal_getopt ();
311 if (internal_getopt (list, "") != -1)
318 av = make_builtin_argv (list, &ac);
319 ret = dogetopts (ac, av);