OpenCoverage

bashgetopt.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/builtins/bashgetopt.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* bashgetopt.c -- `getopt' for use by the builtins. */-
2-
3/* Copyright (C) 1992-2002 Free Software Foundation, Inc.-
4-
5 This file is part of GNU Bash, the Bourne Again SHell.-
6-
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.-
11-
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.-
16-
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/>.-
19*/-
20-
21#include <config.h>-
22-
23#if defined (HAVE_UNISTD_H)-
24# include <unistd.h>-
25#endif-
26-
27#include "../bashansi.h"-
28#include <chartypes.h>-
29#include <errno.h>-
30-
31#include "../shell.h"-
32#include "common.h"-
33-
34#include "bashgetopt.h"-
35-
36#define ISOPT(s) (((*(s) == '-') || (plus && *(s) == '+')) && (s)[1])-
37#define NOTOPT(s) (((*(s) != '-') && (!plus || *(s) != '+')) || (s)[1] == '\0')-
38 -
39static int sp;-
40-
41char *list_optarg;-
42int list_optopt;-
43int list_opttype;-
44-
45static WORD_LIST *lhead = (WORD_LIST *)NULL;-
46WORD_LIST *lcurrent = (WORD_LIST *)NULL;-
47WORD_LIST *loptend; /* Points to the first non-option argument in the list */-
48-
49int-
50internal_getopt(list, opts)-
51WORD_LIST *list;-
52char *opts;-
53{-
54 register int c;-
55 register char *cp;-
56 int plus; /* nonzero means to handle +option */-
57 static char errstr[3] = { '-', '\0', '\0' };-
58-
59 plus = *opts == '+';-
60 if (plus)
plusDescription
TRUEevaluated 3264812 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4037354 times by 1 test
Evaluated by:
  • Self test
3264812-4037354
61 opts++;
executed 3264812 times by 1 test: opts++;
Executed by:
  • Self test
3264812
62-
63 if (list == 0) {
list == 0Description
TRUEevaluated 612 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 7301554 times by 1 test
Evaluated by:
  • Self test
612-7301554
64 list_optarg = (char *)NULL;-
65 loptend = (WORD_LIST *)NULL; /* No non-option arguments */-
66 return -1;
executed 612 times by 1 test: return -1;
Executed by:
  • Self test
612
67 }-
68-
69 if (list != lhead || lhead == 0) {
list != lheadDescription
TRUEevaluated 4645157 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2656397 times by 1 test
Evaluated by:
  • Self test
lhead == 0Description
TRUEnever evaluated
FALSEevaluated 2656397 times by 1 test
Evaluated by:
  • Self test
0-4645157
70 /* Hmmm.... called with a different word list. Reset. */-
71 sp = 1;-
72 lcurrent = lhead = list;-
73 loptend = (WORD_LIST *)NULL;-
74 }
executed 4645157 times by 1 test: end of block
Executed by:
  • Self test
4645157
75-
76 if (sp == 1) {
sp == 1Description
TRUEevaluated 5980174 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1321380 times by 1 test
Evaluated by:
  • Self test
1321380-5980174
77 if (lcurrent == 0 || NOTOPT(lcurrent->word->word)) {
lcurrent == 0Description
TRUEevaluated 1521 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 5978653 times by 1 test
Evaluated by:
  • Self test
(*(lcurrent->w...>word) != '-')Description
TRUEevaluated 4633464 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1345189 times by 1 test
Evaluated by:
  • Self test
!plusDescription
TRUEevaluated 1382788 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3250676 times by 1 test
Evaluated by:
  • Self test
*(lcurrent->word->word) != '+'Description
TRUEevaluated 3250305 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 371 times by 1 test
Evaluated by:
  • Self test
(lcurrent->wor...rd)[1] == '\0'Description
TRUEevaluated 30 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1345530 times by 1 test
Evaluated by:
  • Self test
30-5978653
78 lhead = (WORD_LIST *)NULL;-
79 loptend = lcurrent;-
80 return(-1);
executed 4634644 times by 1 test: return(-1);
Executed by:
  • Self test
4634644
81 } else if (ISHELP (lcurrent->word->word)) {
never executed: __result = (((const unsigned char *) (const char *) ( (lcurrent->word->word) ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( "--help" ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
((lcurrent->wo... ("--help")[0]Description
TRUEevaluated 1345159 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 371 times by 1 test
Evaluated by:
  • Self test
__extension__ ... )))); }) == 0Description
TRUEnever evaluated
FALSEevaluated 1345159 times by 1 test
Evaluated by:
  • Self test
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0-1345159
82 lhead = (WORD_LIST *)NULL;-
83 loptend = lcurrent;-
84 return (GETOPT_HELP);
never executed: return (-99);
0
85 } else if (lcurrent->word->word[0] == '-' &&
lcurrent->word->word[0] == '-'Description
TRUEevaluated 1345159 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 371 times by 1 test
Evaluated by:
  • Self test
371-1345159
86 lcurrent->word->word[1] == '-' &&
lcurrent->word->word[1] == '-'Description
TRUEevaluated 10441 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1334718 times by 1 test
Evaluated by:
  • Self test
10441-1334718
87 lcurrent->word->word[2] == 0) {
lcurrent->word->word[2] == 0Description
TRUEevaluated 10441 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-10441
88 lhead = (WORD_LIST *)NULL;-
89 loptend = lcurrent->next;-
90 return(-1);
executed 10441 times by 1 test: return(-1);
Executed by:
  • Self test
10441
91 }-
92 errstr[0] = list_opttype = lcurrent->word->word[0];-
93 }
executed 1335089 times by 1 test: end of block
Executed by:
  • Self test
1335089
94-
95 list_optopt = c = lcurrent->word->word[sp];-
96-
97 if (c == ':' || (cp = strchr(opts, c)) == NULL) {
c == ':'Description
TRUEnever evaluated
FALSEevaluated 2656469 times by 1 test
Evaluated by:
  • Self test
(cp = (__exten...== ((void *)0)Description
TRUEevaluated 57 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2656412 times by 1 test
Evaluated by:
  • Self test
__builtin_constant_p ( c )Description
TRUEnever evaluated
FALSEevaluated 2656469 times by 1 test
Evaluated by:
  • Self test
!__builtin_constant_p ( opts )Description
TRUEnever evaluated
FALSEnever evaluated
( c ) == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0-2656469
98 errstr[1] = c;-
99 sh_invalidopt (errstr); -
100 if (lcurrent->word->word[++sp] == '\0') {
lcurrent->word...[++sp] == '\0'Description
TRUEevaluated 57 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-57
101 lcurrent = lcurrent->next;-
102 sp = 1;-
103 }
executed 57 times by 1 test: end of block
Executed by:
  • Self test
57
104 list_optarg = NULL;-
105 if (lcurrent)
lcurrentDescription
TRUEevaluated 35 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 22 times by 1 test
Evaluated by:
  • Self test
22-35
106 loptend = lcurrent->next;
executed 35 times by 1 test: loptend = lcurrent->next;
Executed by:
  • Self test
35
107 return('?');
executed 57 times by 1 test: return('?');
Executed by:
  • Self test
57
108 }-
109-
110 if (*++cp == ':' || *cp == ';') {
*++cp == ':'Description
TRUEevaluated 1324560 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1331852 times by 1 test
Evaluated by:
  • Self test
*cp == ';'Description
TRUEevaluated 1114 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1330738 times by 1 test
Evaluated by:
  • Self test
1114-1331852
111 /* `:': Option requires an argument. */-
112 /* `;': option argument may be missing */-
113 /* We allow -l2 as equivalent to -l 2 */-
114 if (lcurrent->word->word[sp+1]) {
lcurrent->word->word[sp+1]Description
TRUEevaluated 1321209 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4465 times by 1 test
Evaluated by:
  • Self test
4465-1321209
115 list_optarg = lcurrent->word->word + sp + 1;-
116 lcurrent = lcurrent->next;-
117 /* If the specifier is `;', don't set optarg if the next-
118 argument looks like another option. */-
119#if 0-
120 } else if (lcurrent->next && (*cp == ':' || lcurrent->next->word->word[0] != '-')) {-
121#else-
122 } else if (lcurrent->next && (*cp == ':' || NOTOPT(lcurrent->next->word->word))) {
executed 1321209 times by 1 test: end of block
Executed by:
  • Self test
lcurrent->nextDescription
TRUEevaluated 4461 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4 times by 1 test
Evaluated by:
  • Self test
*cp == ':'Description
TRUEevaluated 3351 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1110 times by 1 test
Evaluated by:
  • Self test
(*(lcurrent->n...>word) != '-')Description
TRUEevaluated 1110 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
!plusDescription
TRUEevaluated 241 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 869 times by 1 test
Evaluated by:
  • Self test
*(lcurrent->ne...->word) != '+'Description
TRUEevaluated 869 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(lcurrent->nex...rd)[1] == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0-1321209
123#endif-
124 lcurrent = lcurrent->next;-
125 list_optarg = lcurrent->word->word;-
126 lcurrent = lcurrent->next;-
127 } else if (*cp == ';') {
executed 4461 times by 1 test: end of block
Executed by:
  • Self test
*cp == ';'Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-4461
128 list_optarg = (char *)NULL;-
129 lcurrent = lcurrent->next;-
130 } else { /* lcurrent->next == NULL */
executed 4 times by 1 test: end of block
Executed by:
  • Self test
4
131 errstr[1] = c;-
132 sh_needarg (errstr);-
133 sp = 1;-
134 list_optarg = (char *)NULL;-
135 return('?');
never executed: return('?');
0
136 }-
137 sp = 1;-
138 } else if (*cp == '#') {
executed 1325674 times by 1 test: end of block
Executed by:
  • Self test
*cp == '#'Description
TRUEnever evaluated
FALSEevaluated 1330738 times by 1 test
Evaluated by:
  • Self test
0-1330738
139 /* option requires a numeric argument */-
140 if (lcurrent->word->word[sp+1]) {
lcurrent->word->word[sp+1]Description
TRUEnever evaluated
FALSEnever evaluated
0
141 if (DIGIT(lcurrent->word->word[sp+1])) {
(lcurrent->wor...[sp+1]) >= '0'Description
TRUEnever evaluated
FALSEnever evaluated
(lcurrent->wor...[sp+1]) <= '9'Description
TRUEnever evaluated
FALSEnever evaluated
0
142 list_optarg = lcurrent->word->word + sp + 1;-
143 lcurrent = lcurrent->next;-
144 } else
never executed: end of block
0
145 list_optarg = (char *)NULL;
never executed: list_optarg = (char *) ((void *)0) ;
0
146 } else {-
147 if (lcurrent->next && legal_number(lcurrent->next->word->word, (intmax_t *)0)) {
lcurrent->nextDescription
TRUEnever evaluated
FALSEnever evaluated
legal_number(l...(intmax_t *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
148 lcurrent = lcurrent->next;-
149 list_optarg = lcurrent->word->word;-
150 lcurrent = lcurrent->next;-
151 } else {
never executed: end of block
0
152 errstr[1] = c;-
153 sh_neednumarg (errstr);-
154 sp = 1;-
155 list_optarg = (char *)NULL;-
156 return ('?');
never executed: return ('?');
0
157 }-
158 }-
159-
160 } else {-
161 /* No argument, just return the option. */-
162 if (lcurrent->word->word[++sp] == '\0') {
lcurrent->word...[++sp] == '\0'Description
TRUEevaluated 9358 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1321380 times by 1 test
Evaluated by:
  • Self test
9358-1321380
163 sp = 1;-
164 lcurrent = lcurrent->next;-
165 }
executed 9358 times by 1 test: end of block
Executed by:
  • Self test
9358
166 list_optarg = (char *)NULL;-
167 }
executed 1330738 times by 1 test: end of block
Executed by:
  • Self test
1330738
168-
169 return(c);
executed 2656412 times by 1 test: return(c);
Executed by:
  • Self test
2656412
170}-
171-
172/*-
173 * reset_internal_getopt -- force the in[ft]ernal getopt to reset-
174 */-
175-
176void-
177reset_internal_getopt ()-
178{-
179 lhead = lcurrent = loptend = (WORD_LIST *)NULL;-
180 sp = 1;-
181}
executed 4645769 times by 1 test: end of block
Executed by:
  • Self test
4645769
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2