OpenCoverage

getopts.def

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/builtins/getopts.def
Source codeSwitch to Preprocessed file
LineSourceCount
1This file is getopts.def, from which is created getopts.c.-
2It implements the builtin "getopts" in Bash.-
3-
4Copyright (C) 1987-2015 Free Software Foundation, Inc.-
5-
6This file is part of GNU Bash, the Bourne Again SHell.-
7-
8Bash is free software: you can redistribute it and/or modify-
9it under the terms of the GNU General Public License as published by-
10the Free Software Foundation, either version 3 of the License, or-
11(at your option) any later version.-
12-
13Bash is distributed in the hope that it will be useful,-
14but WITHOUT ANY WARRANTY; without even the implied warranty of-
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-
16GNU General Public License for more details.-
17-
18You should have received a copy of the GNU General Public License-
19along with Bash. If not, see <http://www.gnu.org/licenses/>.-
20-
21$PRODUCES getopts.c-
22-
23$BUILTIN getopts-
24$FUNCTION getopts_builtin-
25$SHORT_DOC getopts optstring name [arg]-
26Parse option arguments.-
27-
28Getopts is used by shell procedures to parse positional parameters-
29as options.-
30-
31OPTSTRING contains the option letters to be recognized; if a letter-
32is followed by a colon, the option is expected to have an argument,-
33which should be separated from it by white space.-
34-
35Each time it is invoked, getopts will place the next option in the-
36shell variable $name, initializing name if it does not exist, and-
37the index of the next argument to be processed into the shell-
38variable OPTIND. OPTIND is initialized to 1 each time the shell or-
39a shell script is invoked. When an option requires an argument,-
40getopts places that argument into the shell variable OPTARG.-
41-
42getopts reports errors in one of two ways. If the first character-
43of OPTSTRING is a colon, getopts uses silent error reporting. In-
44this mode, no error messages are printed. If an invalid option is-
45seen, getopts places the option character found into OPTARG. If a-
46required argument is not found, getopts places a ':' into NAME and-
47sets OPTARG to the option character found. If getopts is not in-
48silent mode, and an invalid option is seen, getopts places '?' into-
49NAME and unsets OPTARG. If a required argument is not found, a '?'-
50is placed in NAME, OPTARG is unset, and a diagnostic message is-
51printed.-
52-
53If the shell variable OPTERR has the value 0, getopts disables the-
54printing of error messages, even if the first character of-
55OPTSTRING is not a colon. OPTERR has the value 1 by default.-
56-
57Getopts normally parses the positional parameters ($0 - $9), but if-
58more arguments are given, they are parsed instead.-
59-
60Exit Status:-
61Returns success if an option is found; fails if the end of options is-
62encountered or an error occurs.-
63$END-
64-
65#include <config.h>-
66-
67#include <stdio.h>-
68-
69#if defined (HAVE_UNISTD_H)-
70# ifdef _MINIX-
71# include <sys/types.h>-
72# endif-
73# include <unistd.h>-
74#endif-
75-
76#include "../bashansi.h"-
77#include "../bashintl.h"-
78-
79#include "../shell.h"-
80#include "../execute_cmd.h"-
81#include "common.h"-
82#include "bashgetopt.h"-
83#include "getopt.h"-
84-
85#define G_EOF -1-
86#define G_INVALID_OPT -2-
87#define G_ARG_MISSING -3-
88-
89static int getopts_unbind_variable __P((char *));-
90static int getopts_bind_variable __P((char *, char *));-
91static int dogetopts __P((int, char **));-
92-
93/* getopts_reset is magic code for when OPTIND is reset. N is the-
94 value that has just been assigned to OPTIND. */-
95void-
96getopts_reset (newind)-
97 int newind;-
98{-
99 sh_optind = newind;-
100 sh_badopt = 0;-
101}
executed 5455 times by 1 test: end of block
Executed by:
  • Self test
5455
102-
103static int-
104getopts_unbind_variable (name)-
105 char *name;-
106{-
107#if 0-
108 return (unbind_variable (name));-
109#else-
110 return (unbind_variable_noref (name));
executed 28 times by 1 test: return (unbind_variable_noref (name));
Executed by:
  • Self test
28
111#endif-
112}-
113-
114static int-
115getopts_bind_variable (name, value)-
116 char *name, *value;-
117{-
118 SHELL_VAR *v;-
119-
120 if (legal_identifier (name))
legal_identifier (name)Description
TRUEevaluated 75 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
1-75
121 {-
122 v = bind_variable (name, value, 0);-
123 if (v && (readonly_p (v) || noassign_p (v)))
vDescription
TRUEevaluated 70 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 5 times by 1 test
Evaluated by:
  • Self test
((((v)->attrib... (0x0000002)))Description
TRUEnever evaluated
FALSEevaluated 70 times by 1 test
Evaluated by:
  • Self test
((((v)->attrib... (0x0004000)))Description
TRUEnever evaluated
FALSEevaluated 70 times by 1 test
Evaluated by:
  • Self test
0-70
124 return (EX_MISCERROR);
never executed: return (2);
0
125 return (v ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
executed 75 times by 1 test: return (v ? 0 : 1);
Executed by:
  • Self test
75
126 }-
127 else-
128 {-
129 sh_invalidid (name);-
130 return (EXECUTION_FAILURE);
executed 1 time by 1 test: return (1);
Executed by:
  • Self test
1
131 }-
132}-
133-
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.-
139-
140 INVALID OPTIONS:-
141 name -> "?"-
142 if (special_error) then-
143 OPTARG = option character found-
144 no error output-
145 else-
146 OPTARG unset-
147 diagnostic message-
148 fi-
149 -
150 MISSING OPTION ARGUMENT;-
151 if (special_error) then-
152 name -> ":"-
153 OPTARG = option character found-
154 else-
155 name -> "?"-
156 OPTARG unset-
157 diagnostic message-
158 fi-
159 */-
160-
161static int-
162dogetopts (argc, argv)-
163 int argc;-
164 char **argv;-
165{-
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 */-
170 char *t;-
171-
172 if (argc < 3)
argc < 3Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 76 times by 1 test
Evaluated by:
  • Self test
1-76
173 {-
174 builtin_usage ();-
175 return (EX_USAGE);
executed 1 time by 1 test: return (258);
Executed by:
  • Self test
1
176 }-
177-
178 /* argv[0] is "getopts". */-
179-
180 optstr = argv[1];-
181 name = argv[2];-
182 argc -= 2;-
183 argv += 2;-
184-
185 special_error = optstr[0] == ':';-
186-
187 if (special_error)
special_errorDescription
TRUEevaluated 29 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 47 times by 1 test
Evaluated by:
  • Self test
29-47
188 {-
189 old_opterr = sh_opterr;-
190 optstr++;-
191 sh_opterr = 0; /* suppress diagnostic messages */-
192 }
executed 29 times by 1 test: end of block
Executed by:
  • Self test
29
193-
194 if (argc > 1)
argc > 1Description
TRUEevaluated 49 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 27 times by 1 test
Evaluated by:
  • Self test
27-49
195 {-
196 sh_getopt_restore_state (argv);-
197 t = argv[0];-
198 argv[0] = dollar_vars[0];-
199 ret = sh_getopt (argc, argv, optstr);-
200 argv[0] = t;-
201 }
executed 49 times by 1 test: end of block
Executed by:
  • Self test
49
202 else if (rest_of_args == (WORD_LIST *)NULL)
rest_of_args =...*) ((void *)0)Description
TRUEevaluated 24 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
3-24
203 {-
204 for (i = 0; i < 10 && dollar_vars[i]; i++)
i < 10Description
TRUEevaluated 94 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
dollar_vars[i]Description
TRUEevaluated 70 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 24 times by 1 test
Evaluated by:
  • Self test
0-94
205 ;
executed 70 times by 1 test: ;
Executed by:
  • Self test
70
206-
207 sh_getopt_restore_state (dollar_vars);-
208 ret = sh_getopt (i, dollar_vars, optstr);-
209 }
executed 24 times by 1 test: end of block
Executed by:
  • Self test
24
210 else-
211 {-
212 register WORD_LIST *words;-
213 char **v;-
214-
215 for (i = 0; i < 10 && dollar_vars[i]; i++)
i < 10Description
TRUEevaluated 30 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
dollar_vars[i]Description
TRUEevaluated 30 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-30
216 ;
executed 30 times by 1 test: ;
Executed by:
  • Self test
30
217 for (words = rest_of_args; words; words = words->next, i++)
wordsDescription
TRUEevaluated 18 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
3-18
218 ;
executed 18 times by 1 test: ;
Executed by:
  • Self test
18
219 v = strvec_create (i + 1);-
220 for (i = 0; i < 10 && dollar_vars[i]; i++)
i < 10Description
TRUEevaluated 30 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
dollar_vars[i]Description
TRUEevaluated 30 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-30
221 v[i] = dollar_vars[i];
executed 30 times by 1 test: v[i] = dollar_vars[i];
Executed by:
  • Self test
30
222 for (words = rest_of_args; words; words = words->next, i++)
wordsDescription
TRUEevaluated 18 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
3-18
223 v[i] = words->word->word;
executed 18 times by 1 test: v[i] = words->word->word;
Executed by:
  • Self test
18
224 v[i] = (char *)NULL;-
225 sh_getopt_restore_state (v);-
226 ret = sh_getopt (i, v, optstr);-
227 free (v);-
228 }
executed 3 times by 1 test: end of block
Executed by:
  • Self test
3
229-
230 if (special_error)
special_errorDescription
TRUEevaluated 29 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 47 times by 1 test
Evaluated by:
  • Self test
29-47
231 sh_opterr = old_opterr;
executed 29 times by 1 test: sh_opterr = old_opterr;
Executed by:
  • Self test
29
232-
233 /* Set the OPTIND variable in any case, to handle "--" skipping. It's-
234 highly unlikely that 14 digits will be too few. */-
235 if (sh_optind < 10)
sh_optind < 10Description
TRUEevaluated 73 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
3-73
236 {-
237 numval[14] = sh_optind + '0';-
238 numval[15] = '\0';-
239 i = 14;-
240 }
executed 73 times by 1 test: end of block
Executed by:
  • Self test
73
241 else-
242 {-
243 numval[i = 15] = '\0';-
244 n = sh_optind;-
245 do-
246 {-
247 numval[--i] = (n % 10) + '0';-
248 }
executed 6 times by 1 test: end of block
Executed by:
  • Self test
6
249 while (n /= 10);
n /= 10Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
3
250 }
executed 3 times by 1 test: end of block
Executed by:
  • Self test
3
251 bind_variable ("OPTIND", numval + i, 0);-
252-
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). */-
258 if (ret == '?')
ret == '?'Description
TRUEevaluated 13 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 63 times by 1 test
Evaluated by:
  • Self test
13-63
259 {-
260 if (sh_optarg == NULL)
sh_optarg == ((void *)0)Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
2-11
261 ret = G_INVALID_OPT;
executed 11 times by 1 test: ret = -2;
Executed by:
  • Self test
11
262 else if (sh_optarg[0] == '\0')
sh_optarg[0] == '\0'Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-2
263 ret = G_ARG_MISSING;
executed 2 times by 1 test: ret = -3;
Executed by:
  • Self test
2
264 }
executed 13 times by 1 test: end of block
Executed by:
  • Self test
13
265 -
266 if (ret == G_EOF)
ret == -1Description
TRUEevaluated 18 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 58 times by 1 test
Evaluated by:
  • Self test
18-58
267 {-
268 getopts_unbind_variable ("OPTARG");-
269 getopts_bind_variable (name, "?");-
270 return (EXECUTION_FAILURE);
executed 18 times by 1 test: return (1);
Executed by:
  • Self test
18
271 }-
272-
273 if (ret == G_INVALID_OPT)
ret == -2Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 47 times by 1 test
Evaluated by:
  • Self test
11-47
274 {-
275 /* Invalid option encountered. */-
276 ret = getopts_bind_variable (name, "?");-
277-
278 if (special_error)
special_errorDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 9 times by 1 test
Evaluated by:
  • Self test
2-9
279 {-
280 strval[0] = (char)sh_optopt;-
281 strval[1] = '\0';-
282 bind_variable ("OPTARG", strval, 0);-
283 }
executed 2 times by 1 test: end of block
Executed by:
  • Self test
2
284 else-
285 getopts_unbind_variable ("OPTARG");
executed 9 times by 1 test: getopts_unbind_variable ("OPTARG");
Executed by:
  • Self test
9
286-
287 return (ret);
executed 11 times by 1 test: return (ret);
Executed by:
  • Self test
11
288 }-
289-
290 if (ret == G_ARG_MISSING)
ret == -3Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 45 times by 1 test
Evaluated by:
  • Self test
2-45
291 {-
292 /* Required argument missing. */-
293 if (special_error)
special_errorDescription
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
1
294 {-
295 ret = getopts_bind_variable (name, ":");-
296-
297 strval[0] = (char)sh_optopt;-
298 strval[1] = '\0';-
299 bind_variable ("OPTARG", strval, 0);-
300 }
executed 1 time by 1 test: end of block
Executed by:
  • Self test
1
301 else-
302 {-
303 ret = getopts_bind_variable (name, "?");-
304 getopts_unbind_variable ("OPTARG");-
305 }
executed 1 time by 1 test: end of block
Executed by:
  • Self test
1
306 return (ret);
executed 2 times by 1 test: return (ret);
Executed by:
  • Self test
2
307 } -
308-
309 bind_variable ("OPTARG", sh_optarg, 0);-
310-
311 strval[0] = (char) ret;-
312 strval[1] = '\0';-
313 return (getopts_bind_variable (name, strval));
executed 45 times by 1 test: return (getopts_bind_variable (name, strval));
Executed by:
  • Self test
45
314}-
315-
316/* The getopts builtin. Build an argv, and call dogetopts with it. */-
317int-
318getopts_builtin (list)-
319 WORD_LIST *list;-
320{-
321 char **av;-
322 int ac, ret;-
323-
324 if (list == 0)
list == 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 78 times by 1 test
Evaluated by:
  • Self test
1-78
325 {-
326 builtin_usage ();-
327 return EX_USAGE;
executed 1 time by 1 test: return 258;
Executed by:
  • Self test
1
328 }-
329-
330 reset_internal_getopt ();-
331 if ((ret = internal_getopt (list, "")) != -1)
(ret = interna...st, "")) != -1Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 77 times by 1 test
Evaluated by:
  • Self test
1-77
332 {-
333 if (ret == GETOPT_HELP)
ret == -99Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
0-1
334 builtin_help ();
never executed: builtin_help ();
0
335 else-
336 builtin_usage ();
executed 1 time by 1 test: builtin_usage ();
Executed by:
  • Self test
1
337 return (EX_USAGE);
executed 1 time by 1 test: return (258);
Executed by:
  • Self test
1
338 }-
339 list = loptend;-
340-
341 av = make_builtin_argv (list, &ac);-
342 ret = dogetopts (ac, av);-
343 free ((char *)av);-
344-
345 return (ret);
executed 77 times by 1 test: return (ret);
Executed by:
  • Self test
77
346}-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2