OpenCoverage

arrayfunc.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/bash/src/arrayfunc.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* arrayfunc.c -- High-level array functions used by other parts of the shell. */-
2-
3/* Copyright (C) 2001-2016 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 (ARRAY_VARS)-
24-
25#if defined (HAVE_UNISTD_H)-
26# include <unistd.h>-
27#endif-
28#include <stdio.h>-
29-
30#include "bashintl.h"-
31-
32#include "shell.h"-
33#include "execute_cmd.h"-
34#include "pathexp.h"-
35-
36#include "shmbutil.h"-
37#if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR)-
38# include <mbstr.h> /* mbschr */-
39#endif-
40-
41#include "builtins/common.h"-
42-
43/* This variable means to not expand associative array subscripts more than-
44 once, when performing variable expansion. */-
45int assoc_expand_once = 0;-
46-
47/* Ditto for indexed array subscripts -- currently unused */-
48int array_expand_once = 0;-
49-
50static SHELL_VAR *bind_array_var_internal __P((SHELL_VAR *, arrayind_t, char *, char *, int));-
51static SHELL_VAR *assign_array_element_internal __P((SHELL_VAR *, char *, char *, char *, int, char *, int));-
52-
53static char *quote_assign __P((const char *));-
54static void quote_array_assignment_chars __P((WORD_LIST *));-
55static char *array_value_internal __P((const char *, int, int, int *, arrayind_t *));-
56-
57/* Standard error message to use when encountering an invalid array subscript */-
58const char * const bash_badsub_errmsg = N_("bad array subscript");-
59-
60/* **************************************************************** */-
61/* */-
62/* Functions to manipulate array variables and perform assignments */-
63/* */-
64/* **************************************************************** */-
65-
66/* Convert a shell variable to an array variable. The original value is-
67 saved as array[0]. */-
68SHELL_VAR *-
69convert_var_to_array (var)-
70 SHELL_VAR *var;-
71{-
72 char *oldval;-
73 ARRAY *array;-
74-
75 oldval = value_cell (var);-
76 array = array_create ();-
77 if (oldval)
oldvalDescription
TRUEevaluated 65 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 18 times by 1 test
Evaluated by:
  • Self test
18-65
78 array_insert (array, 0, oldval);
executed 65 times by 1 test: array_insert (array, 0, oldval);
Executed by:
  • Self test
65
79-
80 FREE (value_cell (var));
executed 65 times by 1 test: sh_xfree((((var)->value)), "arrayfunc.c", 80);
Executed by:
  • Self test
((var)->value)Description
TRUEevaluated 65 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 18 times by 1 test
Evaluated by:
  • Self test
18-65
81 var_setarray (var, array);-
82-
83 /* these aren't valid anymore */-
84 var->dynamic_value = (sh_var_value_func_t *)NULL;-
85 var->assign_func = (sh_var_assign_func_t *)NULL;-
86-
87 INVALIDATE_EXPORTSTR (var);
never executed: end of block
(var)->exportstrDescription
TRUEnever evaluated
FALSEevaluated 83 times by 1 test
Evaluated by:
  • Self test
0-83
88 if (exported_p (var))
((((var)->attr... (0x0000001)))Description
TRUEnever evaluated
FALSEevaluated 83 times by 1 test
Evaluated by:
  • Self test
0-83
89 array_needs_making++;
never executed: array_needs_making++;
0
90-
91 VSETATTR (var, att_array);-
92 VUNSETATTR (var, att_invisible);-
93-
94 /* Make sure it's not marked as an associative array any more */-
95 VUNSETATTR (var, att_assoc);-
96-
97 /* Since namerefs can't be array variables, turn off nameref attribute */-
98 VUNSETATTR (var, att_nameref);-
99-
100 return var;
executed 83 times by 1 test: return var;
Executed by:
  • Self test
83
101}-
102-
103/* Convert a shell variable to an array variable. The original value is-
104 saved as array[0]. */-
105SHELL_VAR *-
106convert_var_to_assoc (var)-
107 SHELL_VAR *var;-
108{-
109 char *oldval;-
110 HASH_TABLE *hash;-
111-
112 oldval = value_cell (var);-
113 hash = assoc_create (0);-
114 if (oldval)
oldvalDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-2
115 assoc_insert (hash, savestring ("0"), oldval);
executed 2 times by 1 test: assoc_insert (hash, (char *)strcpy (sh_xmalloc((1 + strlen ("0")), "arrayfunc.c", 115), ("0")), oldval);
Executed by:
  • Self test
2
116-
117 FREE (value_cell (var));
executed 2 times by 1 test: sh_xfree((((var)->value)), "arrayfunc.c", 117);
Executed by:
  • Self test
((var)->value)Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-2
118 var_setassoc (var, hash);-
119-
120 /* these aren't valid anymore */-
121 var->dynamic_value = (sh_var_value_func_t *)NULL;-
122 var->assign_func = (sh_var_assign_func_t *)NULL;-
123-
124 INVALIDATE_EXPORTSTR (var);
never executed: end of block
(var)->exportstrDescription
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
0-2
125 if (exported_p (var))
((((var)->attr... (0x0000001)))Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
0-2
126 array_needs_making++;
never executed: array_needs_making++;
0
127-
128 VSETATTR (var, att_assoc);-
129 VUNSETATTR (var, att_invisible);-
130-
131 /* Make sure it's not marked as an indexed array any more */-
132 VUNSETATTR (var, att_array);-
133-
134 /* Since namerefs can't be array variables, turn off nameref attribute */-
135 VUNSETATTR (var, att_nameref);-
136-
137 return var;
executed 2 times by 1 test: return var;
Executed by:
  • Self test
2
138}-
139-
140char *-
141make_array_variable_value (entry, ind, key, value, flags)-
142 SHELL_VAR *entry;-
143 arrayind_t ind;-
144 char *key;-
145 char *value;-
146 int flags;-
147{-
148 SHELL_VAR *dentry;-
149 char *newval;-
150-
151 /* If we're appending, we need the old value of the array reference, so-
152 fake out make_variable_value with a dummy SHELL_VAR */-
153 if (flags & ASS_APPEND)
flags & 0x0001Description
TRUEevaluated 14 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 14214 times by 1 test
Evaluated by:
  • Self test
14-14214
154 {-
155 dentry = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));-
156 dentry->name = savestring (entry->name);-
157 if (assoc_p (entry))
((((entry)->at... (0x0000040)))Description
TRUEevaluated 7 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 7 times by 1 test
Evaluated by:
  • Self test
7
158 newval = assoc_reference (assoc_cell (entry), key);
executed 7 times by 1 test: newval = assoc_reference ((HASH_TABLE *)((entry)->value), key);
Executed by:
  • Self test
7
159 else-
160 newval = array_reference (array_cell (entry), ind);
executed 7 times by 1 test: newval = array_reference ((ARRAY *)((entry)->value), ind);
Executed by:
  • Self test
7
161 if (newval)
newvalDescription
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3 times by 1 test
Evaluated by:
  • Self test
3-11
162 dentry->value = savestring (newval);
executed 11 times by 1 test: dentry->value = (char *)strcpy (sh_xmalloc((1 + strlen (newval)), "arrayfunc.c", 162), (newval));
Executed by:
  • Self test
11
163 else-
164 {-
165 dentry->value = (char *)xmalloc (1);-
166 dentry->value[0] = '\0';-
167 }
executed 3 times by 1 test: end of block
Executed by:
  • Self test
3
168 dentry->exportstr = 0;-
169 dentry->attributes = entry->attributes & ~(att_array|att_assoc|att_exported);-
170 /* Leave the rest of the members uninitialized; the code doesn't look-
171 at them. */-
172 newval = make_variable_value (dentry, value, flags); -
173 dispose_variable (dentry);-
174 }
executed 14 times by 1 test: end of block
Executed by:
  • Self test
14
175 else-
176 newval = make_variable_value (entry, value, flags);
executed 14214 times by 1 test: newval = make_variable_value (entry, value, flags);
Executed by:
  • Self test
14214
177-
178 return newval;
executed 14228 times by 1 test: return newval;
Executed by:
  • Self test
14228
179}-
180 -
181static SHELL_VAR *-
182bind_array_var_internal (entry, ind, key, value, flags)-
183 SHELL_VAR *entry;-
184 arrayind_t ind;-
185 char *key;-
186 char *value;-
187 int flags;-
188{-
189 char *newval;-
190-
191 newval = make_array_variable_value (entry, ind, key, value, flags);-
192-
193 if (entry->assign_func)
entry->assign_funcDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 14193 times by 1 test
Evaluated by:
  • Self test
5-14193
194 (*entry->assign_func) (entry, newval, ind, key);
executed 5 times by 1 test: (*entry->assign_func) (entry, newval, ind, key);
Executed by:
  • Self test
5
195 else if (assoc_p (entry))
((((entry)->at... (0x0000040)))Description
TRUEevaluated 523 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 13670 times by 1 test
Evaluated by:
  • Self test
523-13670
196 assoc_insert (assoc_cell (entry), key, newval);
executed 523 times by 1 test: assoc_insert ((HASH_TABLE *)((entry)->value), key, newval);
Executed by:
  • Self test
523
197 else-
198 array_insert (array_cell (entry), ind, newval);
executed 13670 times by 1 test: array_insert ((ARRAY *)((entry)->value), ind, newval);
Executed by:
  • Self test
13670
199 FREE (newval);
executed 14198 times by 1 test: sh_xfree((newval), "arrayfunc.c", 199);
Executed by:
  • Self test
newvalDescription
TRUEevaluated 14198 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-14198
200-
201 VUNSETATTR (entry, att_invisible); /* no longer invisible */-
202 return (entry);
executed 14198 times by 1 test: return (entry);
Executed by:
  • Self test
14198
203}-
204-
205/* Perform an array assignment name[ind]=value. If NAME already exists and-
206 is not an array, and IND is 0, perform name=value instead. If NAME exists-
207 and is not an array, and IND is not 0, convert it into an array with the-
208 existing value as name[0].-
209-
210 If NAME does not exist, just create an array variable, no matter what-
211 IND's value may be. */-
212SHELL_VAR *-
213bind_array_variable (name, ind, value, flags)-
214 char *name;-
215 arrayind_t ind;-
216 char *value;-
217 int flags;-
218{-
219 SHELL_VAR *entry;-
220-
221 entry = find_shell_variable (name);-
222-
223 if (entry == (SHELL_VAR *) 0)
entry == (SHELL_VAR *) 0Description
TRUEevaluated 48 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10384 times by 1 test
Evaluated by:
  • Self test
48-10384
224 {-
225 /* Is NAME a nameref variable that points to an unset variable? */-
226 entry = find_variable_nameref_for_create (name, 0);-
227 if (entry == INVALID_NAMEREF_VALUE)
entry == (void..._invalid_valueDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 43 times by 1 test
Evaluated by:
  • Self test
5-43
228 return ((SHELL_VAR *)0);
executed 5 times by 1 test: return ((SHELL_VAR *)0);
Executed by:
  • Self test
5
229 if (entry && nameref_p (entry))
entryDescription
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 41 times by 1 test
Evaluated by:
  • Self test
((((entry)->at... (0x0000800)))Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-41
230 entry = make_new_array_variable (nameref_cell (entry));
executed 2 times by 1 test: entry = make_new_array_variable (((entry)->value));
Executed by:
  • Self test
2
231 }
executed 43 times by 1 test: end of block
Executed by:
  • Self test
43
232 if (entry == (SHELL_VAR *) 0)
entry == (SHELL_VAR *) 0Description
TRUEevaluated 41 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10386 times by 1 test
Evaluated by:
  • Self test
41-10386
233 entry = make_new_array_variable (name);
executed 41 times by 1 test: entry = make_new_array_variable (name);
Executed by:
  • Self test
41
234 else if ((readonly_p (entry) && (flags&ASS_FORCE) == 0) || noassign_p (entry))
((((entry)->at... (0x0000002)))Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10375 times by 1 test
Evaluated by:
  • Self test
(flags&0x0020) == 0Description
TRUEevaluated 10 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
((((entry)->at... (0x0004000)))Description
TRUEnever evaluated
FALSEevaluated 10376 times by 1 test
Evaluated by:
  • Self test
0-10376
235 {-
236 if (readonly_p (entry))
((((entry)->at... (0x0000002)))Description
TRUEevaluated 10 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-10
237 err_readonly (name);
executed 10 times by 1 test: err_readonly (name);
Executed by:
  • Self test
10
238 return (entry);
executed 10 times by 1 test: return (entry);
Executed by:
  • Self test
10
239 }-
240 else if (array_p (entry) == 0)
((((entry)->at...000004))) == 0Description
TRUEevaluated 24 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10352 times by 1 test
Evaluated by:
  • Self test
24-10352
241 entry = convert_var_to_array (entry);
executed 24 times by 1 test: entry = convert_var_to_array (entry);
Executed by:
  • Self test
24
242-
243 /* ENTRY is an array variable, and ARRAY points to the value. */-
244 return (bind_array_var_internal (entry, ind, 0, value, flags));
executed 10417 times by 1 test: return (bind_array_var_internal (entry, ind, 0, value, flags));
Executed by:
  • Self test
10417
245}-
246-
247SHELL_VAR *-
248bind_array_element (entry, ind, value, flags)-
249 SHELL_VAR *entry;-
250 arrayind_t ind;-
251 char *value;-
252 int flags;-
253{-
254 return (bind_array_var_internal (entry, ind, 0, value, flags));
executed 141 times by 1 test: return (bind_array_var_internal (entry, ind, 0, value, flags));
Executed by:
  • Self test
141
255}-
256 -
257SHELL_VAR *-
258bind_assoc_variable (entry, name, key, value, flags)-
259 SHELL_VAR *entry;-
260 char *name;-
261 char *key;-
262 char *value;-
263 int flags;-
264{-
265 if ((readonly_p (entry) && (flags&ASS_FORCE) == 0) || noassign_p (entry))
((((entry)->at... (0x0000002)))Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 371 times by 1 test
Evaluated by:
  • Self test
(flags&0x0020) == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
((((entry)->at... (0x0004000)))Description
TRUEnever evaluated
FALSEevaluated 371 times by 1 test
Evaluated by:
  • Self test
0-371
266 {-
267 if (readonly_p (entry))
((((entry)->at... (0x0000002)))Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-2
268 err_readonly (name);
executed 2 times by 1 test: err_readonly (name);
Executed by:
  • Self test
2
269 return (entry);
executed 2 times by 1 test: return (entry);
Executed by:
  • Self test
2
270 }-
271-
272 return (bind_array_var_internal (entry, 0, key, value, flags));
executed 371 times by 1 test: return (bind_array_var_internal (entry, 0, key, value, flags));
Executed by:
  • Self test
371
273}-
274-
275/* Parse NAME, a lhs of an assignment statement of the form v[s], and-
276 assign VALUE to that array element by calling bind_array_variable().-
277 Flags are ASS_ assignment flags */-
278SHELL_VAR *-
279assign_array_element (name, value, flags)-
280 char *name, *value;-
281 int flags;-
282{-
283 char *sub, *vname;-
284 int sublen;-
285 SHELL_VAR *entry;-
286-
287 vname = array_variable_name (name, (flags & ASS_NOEXPAND) != 0, &sub, &sublen);-
288-
289 if (vname == 0)
vname == 0Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10787 times by 1 test
Evaluated by:
  • Self test
11-10787
290 return ((SHELL_VAR *)NULL);
executed 11 times by 1 test: return ((SHELL_VAR *) ((void *)0) );
Executed by:
  • Self test
11
291-
292 if ((ALL_ELEMENT_SUB (sub[0]) && sub[1] == ']') || (sublen <= 1))
(sub[0]) == '@'Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10785 times by 1 test
Evaluated by:
  • Self test
(sub[0]) == '*'Description
TRUEevaluated 14 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10771 times by 1 test
Evaluated by:
  • Self test
sub[1] == ']'Description
TRUEevaluated 16 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
(sublen <= 1)Description
TRUEnever evaluated
FALSEevaluated 10771 times by 1 test
Evaluated by:
  • Self test
0-10785
293 {-
294 free (vname);-
295 err_badarraysub (name);-
296 return ((SHELL_VAR *)NULL);
executed 16 times by 1 test: return ((SHELL_VAR *) ((void *)0) );
Executed by:
  • Self test
16
297 }-
298-
299 entry = find_variable (vname);-
300 entry = assign_array_element_internal (entry, name, vname, sub, sublen, value, flags);-
301-
302 free (vname);-
303 return entry;
executed 10762 times by 1 test: return entry;
Executed by:
  • Self test
10762
304}-
305-
306static SHELL_VAR *-
307assign_array_element_internal (entry, name, vname, sub, sublen, value, flags)-
308 SHELL_VAR *entry;-
309 char *name; /* only used for error messages */-
310 char *vname;-
311 char *sub;-
312 int sublen;-
313 char *value;-
314 int flags;-
315{-
316 char *akey;-
317 arrayind_t ind;-
318-
319 if (entry && assoc_p (entry))
entryDescription
TRUEevaluated 10723 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 48 times by 1 test
Evaluated by:
  • Self test
((((entry)->at... (0x0000040)))Description
TRUEevaluated 372 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10351 times by 1 test
Evaluated by:
  • Self test
48-10723
320 {-
321 sub[sublen-1] = '\0';-
322 if ((flags & ASS_NOEXPAND) == 0)
(flags & 0x0080) == 0Description
TRUEevaluated 362 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10 times by 1 test
Evaluated by:
  • Self test
10-362
323 akey = expand_assignment_string_to_string (sub, 0); /* [ */
executed 362 times by 1 test: akey = expand_assignment_string_to_string (sub, 0);
Executed by:
  • Self test
362
324 else-
325 akey = savestring (sub);
executed 10 times by 1 test: akey = (char *)strcpy (sh_xmalloc((1 + strlen (sub)), "arrayfunc.c", 325), (sub));
Executed by:
  • Self test
10
326 sub[sublen-1] = ']';-
327 if (akey == 0 || *akey == 0)
akey == 0Description
TRUEnever evaluated
FALSEevaluated 371 times by 1 test
Evaluated by:
  • Self test
*akey == 0Description
TRUEnever evaluated
FALSEevaluated 371 times by 1 test
Evaluated by:
  • Self test
0-371
328 {-
329 err_badarraysub (name);-
330 FREE (akey);
never executed: sh_xfree((akey), "arrayfunc.c", 330);
akeyDescription
TRUEnever evaluated
FALSEnever evaluated
0
331 return ((SHELL_VAR *)NULL);
never executed: return ((SHELL_VAR *) ((void *)0) );
0
332 }-
333 entry = bind_assoc_variable (entry, vname, akey, value, flags);-
334 }
executed 371 times by 1 test: end of block
Executed by:
  • Self test
371
335 else-
336 {-
337 ind = array_expand_index (entry, sub, sublen, 0);-
338 /* negative subscripts to indexed arrays count back from end */-
339 if (entry && ind < 0)
entryDescription
TRUEevaluated 10343 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 48 times by 1 test
Evaluated by:
  • Self test
ind < 0Description
TRUEevaluated 14 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10329 times by 1 test
Evaluated by:
  • Self test
14-10343
340 ind = (array_p (entry) ? array_max_index (array_cell (entry)) : 0) + 1 + ind;
executed 14 times by 1 test: ind = (((((entry)->attributes) & (0x0000004))) ? (((ARRAY *)((entry)->value))->max_index) : 0) + 1 + ind;
Executed by:
  • Self test
((((entry)->at... (0x0000004)))Description
TRUEevaluated 14 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-14
341 if (ind < 0)
ind < 0Description
TRUEevaluated 10 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10381 times by 1 test
Evaluated by:
  • Self test
10-10381
342 {-
343 err_badarraysub (name);-
344 return ((SHELL_VAR *)NULL);
executed 10 times by 1 test: return ((SHELL_VAR *) ((void *)0) );
Executed by:
  • Self test
10
345 }-
346 entry = bind_array_variable (vname, ind, value, flags);-
347 }
executed 10381 times by 1 test: end of block
Executed by:
  • Self test
10381
348-
349 return (entry);
executed 10752 times by 1 test: return (entry);
Executed by:
  • Self test
10752
350}-
351-
352/* Find the array variable corresponding to NAME. If there is no variable,-
353 create a new array variable. If the variable exists but is not an array,-
354 convert it to an indexed array. If FLAGS&1 is non-zero, an existing-
355 variable is checked for the readonly or noassign attribute in preparation-
356 for assignment (e.g., by the `read' builtin). If FLAGS&2 is non-zero, we-
357 create an associative array. */-
358SHELL_VAR *-
359find_or_make_array_variable (name, flags)-
360 char *name;-
361 int flags;-
362{-
363 SHELL_VAR *var;-
364-
365 var = find_variable (name);-
366 if (var == 0)
var == 0Description
TRUEevaluated 202 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 269 times by 1 test
Evaluated by:
  • Self test
202-269
367 {-
368 /* See if we have a nameref pointing to a variable that hasn't been-
369 created yet. */-
370 var = find_variable_last_nameref (name, 1);-
371 if (var && nameref_p (var) && invisible_p (var))
varDescription
TRUEevaluated 14 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 188 times by 1 test
Evaluated by:
  • Self test
((((var)->attr... (0x0000800)))Description
TRUEevaluated 14 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
((((var)->attr... (0x0001000)))Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 9 times by 1 test
Evaluated by:
  • Self test
0-188
372 {-
373 internal_warning (_("%s: removing nameref attribute"), name);-
374 VUNSETATTR (var, att_nameref);-
375 }
executed 5 times by 1 test: end of block
Executed by:
  • Self test
5
376 if (var && nameref_p (var))
varDescription
TRUEevaluated 14 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 188 times by 1 test
Evaluated by:
  • Self test
((((var)->attr... (0x0000800)))Description
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 5 times by 1 test
Evaluated by:
  • Self test
5-188
377 {-
378 if (valid_nameref_value (nameref_cell (var), 2) == 0)
valid_nameref_...alue), 2) == 0Description
TRUEevaluated 8 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
1-8
379 {-
380 sh_invalidid (nameref_cell (var));-
381 return ((SHELL_VAR *)NULL);
executed 8 times by 1 test: return ((SHELL_VAR *) ((void *)0) );
Executed by:
  • Self test
8
382 }-
383 var = (flags & 2) ? make_new_assoc_variable (nameref_cell (var)) : make_new_array_variable (nameref_cell (var));
(flags & 2)Description
TRUEnever evaluated
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
0-1
384 }
executed 1 time by 1 test: end of block
Executed by:
  • Self test
1
385 }
executed 194 times by 1 test: end of block
Executed by:
  • Self test
194
386-
387 if (var == 0)
var == 0Description
TRUEevaluated 188 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 275 times by 1 test
Evaluated by:
  • Self test
188-275
388 var = (flags & 2) ? make_new_assoc_variable (name) : make_new_array_variable (name);
executed 188 times by 1 test: var = (flags & 2) ? make_new_assoc_variable (name) : make_new_array_variable (name);
Executed by:
  • Self test
(flags & 2)Description
TRUEnever evaluated
FALSEevaluated 188 times by 1 test
Evaluated by:
  • Self test
0-188
389 else if ((flags & 1) && (readonly_p (var) || noassign_p (var)))
(flags & 1)Description
TRUEevaluated 275 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
((((var)->attr... (0x0000002)))Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 264 times by 1 test
Evaluated by:
  • Self test
((((var)->attr... (0x0004000)))Description
TRUEnever evaluated
FALSEevaluated 264 times by 1 test
Evaluated by:
  • Self test
0-275
390 {-
391 if (readonly_p (var))
((((var)->attr... (0x0000002)))Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-11
392 err_readonly (name);
executed 11 times by 1 test: err_readonly (name);
Executed by:
  • Self test
11
393 return ((SHELL_VAR *)NULL);
executed 11 times by 1 test: return ((SHELL_VAR *) ((void *)0) );
Executed by:
  • Self test
11
394 }-
395 else if ((flags & 2) && array_p (var))
(flags & 2)Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 252 times by 1 test
Evaluated by:
  • Self test
((((var)->attr... (0x0000004)))Description
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • Self test
0-252
396 {-
397 last_command_exit_value = 1;-
398 report_error (_("%s: cannot convert indexed to associative array"), name);-
399 return ((SHELL_VAR *)NULL);
never executed: return ((SHELL_VAR *) ((void *)0) );
0
400 }-
401 else if (array_p (var) == 0 && assoc_p (var) == 0)
((((var)->attr...000004))) == 0Description
TRUEevaluated 90 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 174 times by 1 test
Evaluated by:
  • Self test
((((var)->attr...000040))) == 0Description
TRUEevaluated 29 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 61 times by 1 test
Evaluated by:
  • Self test
29-174
402 var = convert_var_to_array (var);
executed 29 times by 1 test: var = convert_var_to_array (var);
Executed by:
  • Self test
29
403-
404 return (var);
executed 452 times by 1 test: return (var);
Executed by:
  • Self test
452
405}-
406 -
407/* Perform a compound assignment statement for array NAME, where VALUE is-
408 the text between the parens: NAME=( VALUE ) */-
409SHELL_VAR *-
410assign_array_from_string (name, value, flags)-
411 char *name, *value;-
412 int flags;-
413{-
414 SHELL_VAR *var;-
415 int vflags;-
416-
417 vflags = 1;-
418 if (flags & ASS_MKASSOC)
flags & 0x0004Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 407 times by 1 test
Evaluated by:
  • Self test
12-407
419 vflags |= 2;
executed 12 times by 1 test: vflags |= 2;
Executed by:
  • Self test
12
420-
421 var = find_or_make_array_variable (name, vflags);-
422 if (var == 0)
var == 0Description
TRUEevaluated 15 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 404 times by 1 test
Evaluated by:
  • Self test
15-404
423 return ((SHELL_VAR *)NULL);
executed 15 times by 1 test: return ((SHELL_VAR *) ((void *)0) );
Executed by:
  • Self test
15
424-
425 return (assign_array_var_from_string (var, value, flags));
executed 404 times by 1 test: return (assign_array_var_from_string (var, value, flags));
Executed by:
  • Self test
404
426}-
427-
428/* Sequentially assign the indices of indexed array variable VAR from the-
429 words in LIST. */-
430SHELL_VAR *-
431assign_array_var_from_word_list (var, list, flags)-
432 SHELL_VAR *var;-
433 WORD_LIST *list;-
434 int flags;-
435{-
436 register arrayind_t i;-
437 register WORD_LIST *l;-
438 ARRAY *a;-
439-
440 a = array_cell (var);-
441 i = (flags & ASS_APPEND) ? array_max_index (a) + 1 : 0;
(flags & 0x0001)Description
TRUEnever evaluated
FALSEevaluated 25 times by 1 test
Evaluated by:
  • Self test
0-25
442-
443 for (l = list; l; l = l->next, i++)
lDescription
TRUEevaluated 148 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 25 times by 1 test
Evaluated by:
  • Self test
25-148
444 bind_array_var_internal (var, i, 0, l->word->word, flags & ~ASS_APPEND);
executed 148 times by 1 test: bind_array_var_internal (var, i, 0, l->word->word, flags & ~0x0001);
Executed by:
  • Self test
148
445-
446 VUNSETATTR (var, att_invisible); /* no longer invisible */-
447-
448 return var;
executed 25 times by 1 test: return var;
Executed by:
  • Self test
25
449}-
450-
451WORD_LIST *-
452expand_compound_array_assignment (var, value, flags)-
453 SHELL_VAR *var;-
454 char *value;-
455 int flags;-
456{-
457 WORD_LIST *list, *nlist;-
458 char *val;-
459 int ni;-
460-
461 /* This condition is true when invoked from the declare builtin with a-
462 command like-
463 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")' */-
464 if (*value == '(') /*)*/
*value == '('Description
TRUEevaluated 80 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 435 times by 1 test
Evaluated by:
  • Self test
80-435
465 {-
466 ni = 1;-
467 val = extract_array_assignment_list (value, &ni);-
468 if (val == 0)
val == 0Description
TRUEnever evaluated
FALSEevaluated 80 times by 1 test
Evaluated by:
  • Self test
0-80
469 return (WORD_LIST *)NULL;
never executed: return (WORD_LIST *) ((void *)0) ;
0
470 }
executed 80 times by 1 test: end of block
Executed by:
  • Self test
80
471 else-
472 val = value;
executed 435 times by 1 test: val = value;
Executed by:
  • Self test
435
473-
474 /* Expand the value string into a list of words, performing all the-
475 shell expansions including pathname generation and word splitting. */-
476 /* First we split the string on whitespace, using the shell parser-
477 (ksh93 seems to do this). */-
478 list = parse_string_to_word_list (val, 1, "array assign");-
479-
480 if (var && assoc_p (var))
varDescription
TRUEevaluated 515 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
((((var)->attr... (0x0000040)))Description
TRUEevaluated 77 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 438 times by 1 test
Evaluated by:
  • Self test
0-515
481 {-
482 if (val != value)
val != valueDescription
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 65 times by 1 test
Evaluated by:
  • Self test
12-65
483 free (val);
executed 12 times by 1 test: sh_xfree((val), "arrayfunc.c", 483);
Executed by:
  • Self test
12
484 return list;
executed 77 times by 1 test: return list;
Executed by:
  • Self test
77
485 }-
486-
487 /* If we're using [subscript]=value, we need to quote each [ and ] to-
488 prevent unwanted filename expansion. This doesn't need to be done-
489 for associative array expansion, since that uses a different expansion-
490 function (see assign_compound_array_list below). */-
491 if (list)
listDescription
TRUEevaluated 411 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 27 times by 1 test
Evaluated by:
  • Self test
27-411
492 quote_array_assignment_chars (list);
executed 411 times by 1 test: quote_array_assignment_chars (list);
Executed by:
  • Self test
411
493-
494 /* Now that we've split it, perform the shell expansions on each-
495 word in the list. */-
496 nlist = list ? expand_words_no_vars (list) : (WORD_LIST *)NULL;
listDescription
TRUEevaluated 411 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 27 times by 1 test
Evaluated by:
  • Self test
27-411
497-
498 dispose_words (list);-
499-
500 if (val != value)
val != valueDescription
TRUEevaluated 66 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 369 times by 1 test
Evaluated by:
  • Self test
66-369
501 free (val);
executed 66 times by 1 test: sh_xfree((val), "arrayfunc.c", 501);
Executed by:
  • Self test
66
502-
503 return nlist;
executed 435 times by 1 test: return nlist;
Executed by:
  • Self test
435
504}-
505-
506/* Callers ensure that VAR is not NULL */-
507void-
508assign_compound_array_list (var, nlist, flags)-
509 SHELL_VAR *var;-
510 WORD_LIST *nlist;-
511 int flags;-
512{-
513 ARRAY *a;-
514 HASH_TABLE *h;-
515 WORD_LIST *list;-
516 char *w, *val, *nval, *savecmd;-
517 int len, iflags, free_val;-
518 arrayind_t ind, last_ind;-
519 char *akey;-
520-
521 a = (var && array_p (var)) ? array_cell (var) : (ARRAY *)0;
varDescription
TRUEevaluated 512 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
((((var)->attr... (0x0000004)))Description
TRUEevaluated 435 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 77 times by 1 test
Evaluated by:
  • Self test
0-512
522 h = (var && assoc_p (var)) ? assoc_cell (var) : (HASH_TABLE *)0;
varDescription
TRUEevaluated 512 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
((((var)->attr... (0x0000040)))Description
TRUEevaluated 77 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 435 times by 1 test
Evaluated by:
  • Self test
0-512
523-
524 akey = (char *)0;-
525 ind = 0;-
526-
527 /* Now that we are ready to assign values to the array, kill the existing-
528 value. */-
529 if ((flags & ASS_APPEND) == 0)
(flags & 0x0001) == 0Description
TRUEevaluated 498 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 14 times by 1 test
Evaluated by:
  • Self test
14-498
530 {-
531 if (a && array_p (var))
aDescription
TRUEevaluated 428 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 70 times by 1 test
Evaluated by:
  • Self test
((((var)->attr... (0x0000004)))Description
TRUEevaluated 428 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-428
532 array_flush (a);
executed 428 times by 1 test: array_flush (a);
Executed by:
  • Self test
428
533 else if (h && assoc_p (var))
hDescription
TRUEevaluated 70 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
((((var)->attr... (0x0000040)))Description
TRUEevaluated 70 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-70
534 assoc_flush (h);
executed 70 times by 1 test: assoc_flush (h);
Executed by:
  • Self test
70
535 }
executed 498 times by 1 test: end of block
Executed by:
  • Self test
498
536-
537 last_ind = (a && (flags & ASS_APPEND)) ? array_max_index (a) + 1 : 0;
aDescription
TRUEevaluated 435 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 77 times by 1 test
Evaluated by:
  • Self test
(flags & 0x0001)Description
TRUEevaluated 7 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 428 times by 1 test
Evaluated by:
  • Self test
7-435
538-
539 for (list = nlist; list; list = list->next)
listDescription
TRUEevaluated 3173 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 509 times by 1 test
Evaluated by:
  • Self test
509-3173
540 {-
541 /* Don't allow var+=(values) to make assignments in VALUES append to-
542 existing values by default. */-
543 iflags = flags & ~ASS_APPEND;-
544 w = list->word->word;-
545-
546 /* We have a word of the form [ind]=value */-
547 if ((list->word->flags & W_ASSIGNMENT) && w[0] == '[')
(list->word->flags & 0x000004)Description
TRUEevaluated 2108 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1065 times by 1 test
Evaluated by:
  • Self test
w[0] == '['Description
TRUEevaluated 2108 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-2108
548 {-
549 /* Don't have to handle embedded quotes specially any more, since-
550 associative array subscripts have not been expanded yet (see-
551 above). */-
552 len = skipsubscript (w, 0, 0);-
553-
554 /* XXX - changes for `+=' */-
555 if (w[len] != ']' || (w[len+1] != '=' && (w[len+1] != '+' || w[len+2] != '=')))
w[len] != ']'Description
TRUEnever evaluated
FALSEevaluated 2108 times by 1 test
Evaluated by:
  • Self test
w[len+1] != '='Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2104 times by 1 test
Evaluated by:
  • Self test
w[len+1] != '+'Description
TRUEnever evaluated
FALSEevaluated 4 times by 1 test
Evaluated by:
  • Self test
w[len+2] != '='Description
TRUEnever evaluated
FALSEevaluated 4 times by 1 test
Evaluated by:
  • Self test
0-2108
556 {-
557 if (assoc_p (var))
((((var)->attr... (0x0000040)))Description
TRUEnever evaluated
FALSEnever evaluated
0
558 {-
559 err_badarraysub (w);-
560 continue;
never executed: continue;
0
561 }-
562 nval = make_variable_value (var, w, flags);-
563 if (var->assign_func)
var->assign_funcDescription
TRUEnever evaluated
FALSEnever evaluated
0
564 (*var->assign_func) (var, nval, last_ind, 0);
never executed: (*var->assign_func) (var, nval, last_ind, 0);
0
565 else-
566 array_insert (a, last_ind, nval);
never executed: array_insert (a, last_ind, nval);
0
567 FREE (nval);
never executed: sh_xfree((nval), "arrayfunc.c", 567);
nvalDescription
TRUEnever evaluated
FALSEnever evaluated
0
568 last_ind++;-
569 continue;
never executed: continue;
0
570 }-
571-
572 if (len == 1)
len == 1Description
TRUEevaluated 10 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2098 times by 1 test
Evaluated by:
  • Self test
10-2098
573 {-
574 err_badarraysub (w);-
575 continue;
executed 10 times by 1 test: continue;
Executed by:
  • Self test
10
576 }-
577-
578 if (ALL_ELEMENT_SUB (w[1]) && len == 2)
(w[1]) == '@'Description
TRUEnever evaluated
FALSEevaluated 2098 times by 1 test
Evaluated by:
  • Self test
(w[1]) == '*'Description
TRUEevaluated 14 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2084 times by 1 test
Evaluated by:
  • Self test
len == 2Description
TRUEevaluated 14 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-2098
579 {-
580 last_command_exit_value = 1;-
581 if (assoc_p (var))
((((var)->attr... (0x0000040)))Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10 times by 1 test
Evaluated by:
  • Self test
4-10
582 report_error (_("%s: invalid associative array key"), w);
executed 4 times by 1 test: report_error ( dcgettext (((void *)0), "%s: invalid associative array key" , 5) , w);
Executed by:
  • Self test
4
583 else-
584 report_error (_("%s: cannot assign to non-numeric index"), w);
executed 10 times by 1 test: report_error ( dcgettext (((void *)0), "%s: cannot assign to non-numeric index" , 5) , w);
Executed by:
  • Self test
10
585 continue;
executed 14 times by 1 test: continue;
Executed by:
  • Self test
14
586 }-
587-
588 if (array_p (var))
((((var)->attr... (0x0000004)))Description
TRUEevaluated 1928 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 156 times by 1 test
Evaluated by:
  • Self test
156-1928
589 {-
590 ind = array_expand_index (var, w + 1, len, 0);-
591 /* negative subscripts to indexed arrays count back from end */-
592 if (ind < 0)
ind < 0Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1914 times by 1 test
Evaluated by:
  • Self test
11-1914
593 ind = array_max_index (array_cell (var)) + 1 + ind;
executed 11 times by 1 test: ind = (((ARRAY *)((var)->value))->max_index) + 1 + ind;
Executed by:
  • Self test
11
594 if (ind < 0)
ind < 0Description
TRUEevaluated 10 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1915 times by 1 test
Evaluated by:
  • Self test
10-1915
595 {-
596 err_badarraysub (w);-
597 continue;
executed 10 times by 1 test: continue;
Executed by:
  • Self test
10
598 }-
599-
600 last_ind = ind;-
601 }
executed 1915 times by 1 test: end of block
Executed by:
  • Self test
1915
602 else if (assoc_p (var))
((((var)->attr... (0x0000040)))Description
TRUEevaluated 156 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-156
603 {-
604 /* This is not performed above, see expand_compound_array_assignment */-
605 w[len] = '\0'; /*[*/-
606 akey = expand_assignment_string_to_string (w+1, 0);-
607 w[len] = ']';-
608 /* And we need to expand the value also, see below */-
609 if (akey == 0 || *akey == 0)
akey == 0Description
TRUEnever evaluated
FALSEevaluated 156 times by 1 test
Evaluated by:
  • Self test
*akey == 0Description
TRUEnever evaluated
FALSEevaluated 156 times by 1 test
Evaluated by:
  • Self test
0-156
610 {-
611 err_badarraysub (w);-
612 FREE (akey);
never executed: sh_xfree((akey), "arrayfunc.c", 612);
akeyDescription
TRUEnever evaluated
FALSEnever evaluated
0
613 continue;
never executed: continue;
0
614 }-
615 }
executed 156 times by 1 test: end of block
Executed by:
  • Self test
156
616-
617 /* XXX - changes for `+=' -- just accept the syntax. ksh93 doesn't do this */-
618 if (w[len + 1] == '+' && w[len + 2] == '=')
w[len + 1] == '+'Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2067 times by 1 test
Evaluated by:
  • Self test
w[len + 2] == '='Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-2067
619 {-
620 iflags |= ASS_APPEND;-
621 val = w + len + 3;-
622 }
executed 4 times by 1 test: end of block
Executed by:
  • Self test
4
623 else-
624 val = w + len + 2;
executed 2067 times by 1 test: val = w + len + 2;
Executed by:
  • Self test
2067
625 }-
626 else if (assoc_p (var))
((((var)->attr... (0x0000040)))Description
TRUEevaluated 15 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1050 times by 1 test
Evaluated by:
  • Self test
15-1050
627 {-
628 last_command_exit_value = 1;-
629 report_error (_("%s: %s: must use subscript when assigning associative array"), var->name, w);-
630 continue;
executed 15 times by 1 test: continue;
Executed by:
  • Self test
15
631 }-
632 else /* No [ind]=value, just a stray `=' */-
633 {-
634 ind = last_ind;-
635 val = w;-
636 }
executed 1050 times by 1 test: end of block
Executed by:
  • Self test
1050
637-
638 free_val = 0;-
639 /* See above; we need to expand the value here */-
640 if (assoc_p (var))
((((var)->attr... (0x0000040)))Description
TRUEevaluated 156 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2965 times by 1 test
Evaluated by:
  • Self test
156-2965
641 {-
642 val = expand_assignment_string_to_string (val, 0);-
643 if (val == 0)
val == 0Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 151 times by 1 test
Evaluated by:
  • Self test
5-151
644 {-
645 val = (char *)xmalloc (1);-
646 val[0] = '\0'; /* like do_assignment_internal */-
647 }
executed 5 times by 1 test: end of block
Executed by:
  • Self test
5
648 free_val = 1;-
649 }
executed 156 times by 1 test: end of block
Executed by:
  • Self test
156
650-
651 savecmd = this_command_name;-
652 if (integer_p (var))
((((var)->attr... (0x0000010)))Description
TRUEevaluated 55 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 3066 times by 1 test
Evaluated by:
  • Self test
55-3066
653 this_command_name = (char *)NULL; /* no command name for errors */
executed 55 times by 1 test: this_command_name = (char *) ((void *)0) ;
Executed by:
  • Self test
55
654 bind_array_var_internal (var, ind, akey, val, iflags);-
655 last_ind++;-
656 this_command_name = savecmd;-
657-
658 if (free_val)
free_valDescription
TRUEevaluated 156 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2965 times by 1 test
Evaluated by:
  • Self test
156-2965
659 free (val);
executed 156 times by 1 test: sh_xfree((val), "arrayfunc.c", 659);
Executed by:
  • Self test
156
660 }
executed 3121 times by 1 test: end of block
Executed by:
  • Self test
3121
661}
executed 509 times by 1 test: end of block
Executed by:
  • Self test
509
662-
663/* Perform a compound array assignment: VAR->name=( VALUE ). The-
664 VALUE has already had the parentheses stripped. */-
665SHELL_VAR *-
666assign_array_var_from_string (var, value, flags)-
667 SHELL_VAR *var;-
668 char *value;-
669 int flags;-
670{-
671 WORD_LIST *nlist;-
672-
673 if (value == 0)
value == 0Description
TRUEnever evaluated
FALSEevaluated 484 times by 1 test
Evaluated by:
  • Self test
0-484
674 return var;
never executed: return var;
0
675-
676 nlist = expand_compound_array_assignment (var, value, flags);-
677 assign_compound_array_list (var, nlist, flags);-
678-
679 if (nlist)
nlistDescription
TRUEevaluated 448 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 30 times by 1 test
Evaluated by:
  • Self test
30-448
680 dispose_words (nlist);
executed 448 times by 1 test: dispose_words (nlist);
Executed by:
  • Self test
448
681-
682 if (var)
varDescription
TRUEevaluated 478 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-478
683 VUNSETATTR (var, att_invisible); /* no longer invisible */
executed 478 times by 1 test: ((var)->attributes &= ~(0x0001000));
Executed by:
  • Self test
478
684-
685 return (var);
executed 478 times by 1 test: return (var);
Executed by:
  • Self test
478
686}-
687-
688/* Quote globbing chars and characters in $IFS before the `=' in an assignment-
689 statement (usually a compound array assignment) to protect them from-
690 unwanted filename expansion or word splitting. */-
691static char *-
692quote_assign (string)-
693 const char *string;-
694{-
695 size_t slen;-
696 int saw_eq;-
697 char *temp, *t, *subs;-
698 const char *s, *send;-
699 int ss, se;-
700 DECLARE_MBSTATE;-
701-
702 slen = strlen (string);-
703 send = string + slen;-
704-
705 t = temp = (char *)xmalloc (slen * 2 + 1);-
706 saw_eq = 0;-
707 for (s = string; *s; )
*sDescription
TRUEevaluated 9911 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1952 times by 1 test
Evaluated by:
  • Self test
1952-9911
708 {-
709 if (*s == '=')
*s == '='Description
TRUEevaluated 1955 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 7956 times by 1 test
Evaluated by:
  • Self test
1955-7956
710 saw_eq = 1;
executed 1955 times by 1 test: saw_eq = 1;
Executed by:
  • Self test
1955
711 if (saw_eq == 0 && *s == '[') /* looks like a subscript */
saw_eq == 0Description
TRUEevaluated 1954 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 7957 times by 1 test
Evaluated by:
  • Self test
*s == '['Description
TRUEevaluated 1952 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
2-7957
712 {-
713 ss = s - string;-
714 se = skipsubscript (string, ss, 0);-
715 subs = substring (s, ss, se);-
716 *t++ = '\\';-
717 strcpy (t, subs);-
718 t += se - ss;-
719 *t++ = '\\';-
720 *t++ = ']';-
721 s += se + 1;-
722 free (subs);-
723 continue;
executed 1952 times by 1 test: continue;
Executed by:
  • Self test
1952
724 }-
725 if (saw_eq == 0 && (glob_char_p (s) || isifs (*s)))
saw_eq == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 7957 times by 1 test
Evaluated by:
  • Self test
glob_char_p (s)Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
(ifs_cmap[(uns...ar)(*s)] != 0)Description
TRUEnever evaluated
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
0-7957
726 *t++ = '\\';
never executed: *t++ = '\\';
0
727-
728 COPY_CHAR_P (t, s, send);
executed 6438 times by 1 test: mblength = 1;
Executed by:
  • Self test
executed 131 times by 1 test: mblength = 1;
Executed by:
  • Self test
executed 1390 times by 1 test: end of block
Executed by:
  • Self test
executed 199 times by 1 test: end of block
Executed by:
  • Self test
executed 7760 times by 1 test: mblength = (mblength < 1) ? 1 : mblength;
Executed by:
  • Self test
executed 9946 times by 1 test: *(t)++ = *(s)++;
Executed by:
  • Self test
executed 7959 times by 1 test: end of block
Executed by:
  • Self test
never executed: *(t)++ = *(s)++;
locale_mb_cur_max > 1Description
TRUEevaluated 7959 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
_kDescription
TRUEevaluated 6438 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1521 times by 1 test
Evaluated by:
  • Self test
_k < mblengthDescription
TRUEevaluated 9946 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 7959 times by 1 test
Evaluated by:
  • Self test
(mblength < 1)Description
TRUEnever evaluated
FALSEevaluated 7760 times by 1 test
Evaluated by:
  • Self test
locale_utf8localeDescription
TRUEevaluated 1521 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
((*(s) & 0x80) == 0)Description
TRUEevaluated 131 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1390 times by 1 test
Evaluated by:
  • Self test
mblength == (size_t)-2Description
TRUEnever evaluated
FALSEevaluated 7959 times by 1 test
Evaluated by:
  • Self test
mblength == (size_t)-1Description
TRUEevaluated 199 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 7760 times by 1 test
Evaluated by:
  • Self test
0-9946
729 }
executed 7959 times by 1 test: end of block
Executed by:
  • Self test
7959
730 *t = '\0';-
731 return temp;
executed 1952 times by 1 test: return temp;
Executed by:
  • Self test
1952
732}-
733-
734/* For each word in a compound array assignment, if the word looks like-
735 [ind]=value, quote globbing chars and characters in $IFS before the `='. */-
736static void-
737quote_array_assignment_chars (list)-
738 WORD_LIST *list;-
739{-
740 char *nword;-
741 WORD_LIST *l;-
742-
743 for (l = list; l; l = l->next)
lDescription
TRUEevaluated 2815 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 411 times by 1 test
Evaluated by:
  • Self test
411-2815
744 {-
745 if (l->word == 0 || l->word->word == 0 || l->word->word[0] == '\0')
l->word == 0Description
TRUEnever evaluated
FALSEevaluated 2815 times by 1 test
Evaluated by:
  • Self test
l->word->word == 0Description
TRUEnever evaluated
FALSEevaluated 2815 times by 1 test
Evaluated by:
  • Self test
l->word->word[0] == '\0'Description
TRUEnever evaluated
FALSEevaluated 2815 times by 1 test
Evaluated by:
  • Self test
0-2815
746 continue; /* should not happen, but just in case... */
never executed: continue;
0
747 /* Don't bother if it hasn't been recognized as an assignment or-
748 doesn't look like [ind]=value */-
749 if ((l->word->flags & W_ASSIGNMENT) == 0)
(l->word->flag...0x000004) == 0Description
TRUEevaluated 863 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1952 times by 1 test
Evaluated by:
  • Self test
863-1952
750 continue;
executed 863 times by 1 test: continue;
Executed by:
  • Self test
863
751 if (l->word->word[0] != '[' || mbschr (l->word->word, '=') == 0) /* ] */
l->word->word[0] != '['Description
TRUEnever evaluated
FALSEevaluated 1952 times by 1 test
Evaluated by:
  • Self test
mbschr (l->wor...ord, '=') == 0Description
TRUEnever evaluated
FALSEevaluated 1952 times by 1 test
Evaluated by:
  • Self test
0-1952
752 continue;
never executed: continue;
0
753-
754 nword = quote_assign (l->word->word);-
755 free (l->word->word);-
756 l->word->word = nword;-
757 l->word->flags |= W_NOGLOB; /* XXX - W_NOSPLIT also? */-
758 }
executed 1952 times by 1 test: end of block
Executed by:
  • Self test
1952
759}
executed 411 times by 1 test: end of block
Executed by:
  • Self test
411
760-
761/* skipsubscript moved to subst.c to use private functions. 2009/02/24. */-
762-
763/* This function is called with SUB pointing to just after the beginning-
764 `[' of an array subscript and removes the array element to which SUB-
765 expands from array VAR. A subscript of `*' or `@' unsets the array. */-
766/* If FLAGS&1 we don't expand the subscript; we just use it as-is. */-
767int-
768unbind_array_element (var, sub, flags)-
769 SHELL_VAR *var;-
770 char *sub;-
771 int flags;-
772{-
773 int len;-
774 arrayind_t ind;-
775 char *akey;-
776 ARRAY_ELEMENT *ae;-
777-
778 len = skipsubscript (sub, 0, (flags&1) || (var && assoc_p(var)));-
779 if (sub[len] != ']' || len == 0)
sub[len] != ']'Description
TRUEnever evaluated
FALSEevaluated 112 times by 1 test
Evaluated by:
  • Self test
len == 0Description
TRUEnever evaluated
FALSEevaluated 112 times by 1 test
Evaluated by:
  • Self test
0-112
780 {-
781 builtin_error ("%s[%s: %s", var->name, sub, _(bash_badsub_errmsg));-
782 return -1;
never executed: return -1;
0
783 }-
784 sub[len] = '\0';-
785-
786 if (ALL_ELEMENT_SUB (sub[0]) && sub[1] == 0)
(sub[0]) == '@'Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 109 times by 1 test
Evaluated by:
  • Self test
(sub[0]) == '*'Description
TRUEevaluated 10 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 99 times by 1 test
Evaluated by:
  • Self test
sub[1] == 0Description
TRUEevaluated 13 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-109
787 {-
788 if (array_p (var) || assoc_p (var))
((((var)->attr... (0x0000004)))Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
((((var)->attr... (0x0000040)))Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-11
789 {-
790 unbind_variable (var->name); /* XXX -- {array,assoc}_flush ? */-
791 return (0);
executed 13 times by 1 test: return (0);
Executed by:
  • Self test
13
792 }-
793 else-
794 return -2; /* don't allow this to unset scalar variables */
never executed: return -2;
0
795 }-
796-
797 if (assoc_p (var))
((((var)->attr... (0x0000040)))Description
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 90 times by 1 test
Evaluated by:
  • Self test
9-90
798 {-
799 akey = (flags & 1) ? sub : expand_assignment_string_to_string (sub, 0);
(flags & 1)Description
TRUEevaluated 4 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 5 times by 1 test
Evaluated by:
  • Self test
4-5
800 if (akey == 0 || *akey == 0)
akey == 0Description
TRUEnever evaluated
FALSEevaluated 9 times by 1 test
Evaluated by:
  • Self test
*akey == 0Description
TRUEnever evaluated
FALSEevaluated 9 times by 1 test
Evaluated by:
  • Self test
0-9
801 {-
802 builtin_error ("[%s]: %s", sub, _(bash_badsub_errmsg));-
803 FREE (akey);
never executed: sh_xfree((akey), "arrayfunc.c", 803);
akeyDescription
TRUEnever evaluated
FALSEnever evaluated
0
804 return -1;
never executed: return -1;
0
805 }-
806 assoc_remove (assoc_cell (var), akey);-
807 if (akey != sub)
akey != subDescription
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 4 times by 1 test
Evaluated by:
  • Self test
4-5
808 free (akey);
executed 5 times by 1 test: sh_xfree((akey), "arrayfunc.c", 808);
Executed by:
  • Self test
5
809 }
executed 9 times by 1 test: end of block
Executed by:
  • Self test
9
810 else if (array_p (var))
((((var)->attr... (0x0000004)))Description
TRUEevaluated 79 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 11 times by 1 test
Evaluated by:
  • Self test
11-79
811 {-
812 ind = array_expand_index (var, sub, len+1, 0);-
813 /* negative subscripts to indexed arrays count back from end */-
814 if (ind < 0)
ind < 0Description
TRUEevaluated 8 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 71 times by 1 test
Evaluated by:
  • Self test
8-71
815 ind = array_max_index (array_cell (var)) + 1 + ind;
executed 8 times by 1 test: ind = (((ARRAY *)((var)->value))->max_index) + 1 + ind;
Executed by:
  • Self test
8
816 if (ind < 0)
ind < 0Description
TRUEevaluated 6 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 73 times by 1 test
Evaluated by:
  • Self test
6-73
817 {-
818 builtin_error ("[%s]: %s", sub, _(bash_badsub_errmsg));-
819 return -1;
executed 6 times by 1 test: return -1;
Executed by:
  • Self test
6
820 }-
821 ae = array_remove (array_cell (var), ind);-
822 if (ae)
aeDescription
TRUEevaluated 35 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 38 times by 1 test
Evaluated by:
  • Self test
35-38
823 array_dispose_element (ae);
executed 35 times by 1 test: array_dispose_element (ae);
Executed by:
  • Self test
35
824 }
executed 73 times by 1 test: end of block
Executed by:
  • Self test
73
825 else /* array_p (var) == 0 && assoc_p (var) == 0 */-
826 {-
827 akey = this_command_name;-
828 ind = array_expand_index (var, sub, len+1, 0);-
829 this_command_name = akey;-
830 if (ind == 0)
ind == 0Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10 times by 1 test
Evaluated by:
  • Self test
1-10
831 {-
832 unbind_variable (var->name);-
833 return (0);
executed 1 time by 1 test: return (0);
Executed by:
  • Self test
1
834 }-
835 else-
836 return -2; /* any subscript other than 0 is invalid with scalar variables */
executed 10 times by 1 test: return -2;
Executed by:
  • Self test
10
837 }-
838-
839 return 0;
executed 82 times by 1 test: return 0;
Executed by:
  • Self test
82
840}-
841-
842/* Format and output an array assignment in compound form VAR=(VALUES),-
843 suitable for re-use as input. */-
844void-
845print_array_assignment (var, quoted)-
846 SHELL_VAR *var;-
847 int quoted;-
848{-
849 char *vstr;-
850-
851 vstr = array_to_assign (array_cell (var), quoted);-
852-
853 if (vstr == 0)
vstr == 0Description
TRUEevaluated 49 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 224 times by 1 test
Evaluated by:
  • Self test
49-224
854 printf ("%s=%s\n", var->name, quoted ? "'()'" : "()");
executed 49 times by 1 test: printf ("%s=%s\n", var->name, quoted ? "'()'" : "()");
Executed by:
  • Self test
49
855 else-
856 {-
857 printf ("%s=%s\n", var->name, vstr);-
858 free (vstr);-
859 }
executed 224 times by 1 test: end of block
Executed by:
  • Self test
224
860}-
861-
862/* Format and output an associative array assignment in compound form-
863 VAR=(VALUES), suitable for re-use as input. */-
864void-
865print_assoc_assignment (var, quoted)-
866 SHELL_VAR *var;-
867 int quoted;-
868{-
869 char *vstr;-
870-
871 vstr = assoc_to_assign (assoc_cell (var), quoted);-
872-
873 if (vstr == 0)
vstr == 0Description
TRUEevaluated 34 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 85 times by 1 test
Evaluated by:
  • Self test
34-85
874 printf ("%s=%s\n", var->name, quoted ? "'()'" : "()");
executed 34 times by 1 test: printf ("%s=%s\n", var->name, quoted ? "'()'" : "()");
Executed by:
  • Self test
34
875 else-
876 {-
877 printf ("%s=%s\n", var->name, vstr);-
878 free (vstr);-
879 }
executed 85 times by 1 test: end of block
Executed by:
  • Self test
85
880}-
881-
882/***********************************************************************/-
883/* */-
884/* Utility functions to manage arrays and their contents for expansion */-
885/* */-
886/***********************************************************************/-
887-
888/* Return 1 if NAME is a properly-formed array reference v[sub]. */-
889int-
890valid_array_reference (name, flags)-
891 const char *name;-
892 int flags;-
893{-
894 char *t;-
895 int r, len;-
896-
897 t = mbschr (name, '['); /* ] */-
898 if (t)
tDescription
TRUEevaluated 35206 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 17891781 times by 1 test
Evaluated by:
  • Self test
35206-17891781
899 {-
900 *t = '\0';-
901 r = legal_identifier (name);-
902 *t = '[';-
903 if (r == 0)
r == 0Description
TRUEevaluated 3 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 35203 times by 1 test
Evaluated by:
  • Self test
3-35203
904 return 0;
executed 3 times by 1 test: return 0;
Executed by:
  • Self test
3
905 /* Check for a properly-terminated non-null subscript. */-
906 len = skipsubscript (t, 0, flags);-
907 if (t[len] != ']' || len == 1)
t[len] != ']'Description
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 35194 times by 1 test
Evaluated by:
  • Self test
len == 1Description
TRUEnever evaluated
FALSEevaluated 35194 times by 1 test
Evaluated by:
  • Self test
0-35194
908 return 0;
executed 9 times by 1 test: return 0;
Executed by:
  • Self test
9
909 if (t[len+1] != '\0')
t[len+1] != '\0'Description
TRUEnever evaluated
FALSEevaluated 35194 times by 1 test
Evaluated by:
  • Self test
0-35194
910 return 0;
never executed: return 0;
0
911#if 1-
912 /* Could check and allow subscripts consisting only of whitespace for-
913 existing associative arrays. */-
914 for (r = 1; r < len; r++)
r < lenDescription
TRUEevaluated 35232 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 30 times by 1 test
Evaluated by:
  • Self test
30-35232
915 if (whitespace (t[r]) == 0)
(((t[r]) == ' ...== '\t')) == 0Description
TRUEevaluated 35164 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 68 times by 1 test
Evaluated by:
  • Self test
((t[r]) == ' ')Description
TRUEevaluated 68 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 35164 times by 1 test
Evaluated by:
  • Self test
((t[r]) == '\t')Description
TRUEnever evaluated
FALSEevaluated 35164 times by 1 test
Evaluated by:
  • Self test
0-35164
916 return 1;
executed 35164 times by 1 test: return 1;
Executed by:
  • Self test
35164
917 return 0;
executed 30 times by 1 test: return 0;
Executed by:
  • Self test
30
918#else-
919 /* This allows blank subscripts */-
920 return 1;-
921#endif-
922 }-
923 return 0;
executed 17891781 times by 1 test: return 0;
Executed by:
  • Self test
17891781
924}-
925-
926/* Expand the array index beginning at S and extending LEN characters. */-
927arrayind_t-
928array_expand_index (var, s, len, flags)-
929 SHELL_VAR *var;-
930 char *s;-
931 int len;-
932 int flags;-
933{-
934 char *exp, *t, *savecmd;-
935 int expok;-
936 arrayind_t val;-
937-
938 exp = (char *)xmalloc (len);-
939 strncpy (exp, s, len - 1);-
940 exp[len - 1] = '\0';-
941#if 0 /* XXX - not yet -- maybe bash-5.0 */-
942 if ((flags & AV_NOEXPAND) == 0)-
943 t = expand_arith_string (exp, Q_DOUBLE_QUOTES|Q_ARITH|Q_ARRAYSUB); /* XXX - Q_ARRAYSUB for future use */-
944 else-
945 t = exp;-
946#endif-
947 t = expand_arith_string (exp, Q_DOUBLE_QUOTES|Q_ARITH|Q_ARRAYSUB); /* XXX - Q_ARRAYSUB for future use */-
948 savecmd = this_command_name;-
949 this_command_name = (char *)NULL;-
950 val = evalexp (t, 0, &expok);-
951 this_command_name = savecmd;-
952 if (t != exp)
t != expDescription
TRUEevaluated 27600 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-27600
953 free (t);
executed 27600 times by 1 test: sh_xfree((t), "arrayfunc.c", 953);
Executed by:
  • Self test
27600
954 free (exp);-
955 if (expok == 0)
expok == 0Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 27588 times by 1 test
Evaluated by:
  • Self test
12-27588
956 {-
957 last_command_exit_value = EXECUTION_FAILURE;-
958-
959 if (no_longjmp_on_fatal_error)
no_longjmp_on_fatal_errorDescription
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • Self test
0-12
960 return 0;
never executed: return 0;
0
961 top_level_cleanup (); -
962 jump_to_top_level (DISCARD);-
963 }
never executed: end of block
0
964 return val;
executed 27588 times by 1 test: return val;
Executed by:
  • Self test
27588
965}-
966-
967/* Return the name of the variable specified by S without any subscript.-
968 If SUBP is non-null, return a pointer to the start of the subscript-
969 in *SUBP. If LENP is non-null, the length of the subscript is returned-
970 in *LENP. This returns newly-allocated memory. */-
971char *-
972array_variable_name (s, flags, subp, lenp)-
973 const char *s;-
974 int flags;-
975 char **subp;-
976 int *lenp;-
977{-
978 char *t, *ret;-
979 int ind, ni;-
980-
981 t = mbschr (s, '[');-
982 if (t == 0)
t == 0Description
TRUEnever evaluated
FALSEevaluated 64120 times by 1 test
Evaluated by:
  • Self test
0-64120
983 {-
984 if (subp)
subpDescription
TRUEnever evaluated
FALSEnever evaluated
0
985 *subp = t;
never executed: *subp = t;
0
986 if (lenp)
lenpDescription
TRUEnever evaluated
FALSEnever evaluated
0
987 *lenp = 0;
never executed: *lenp = 0;
0
988 return ((char *)NULL);
never executed: return ((char *) ((void *)0) );
0
989 }-
990 ind = t - s;-
991 ni = skipsubscript (s, ind, flags); /* XXX - was 0 not flags */-
992 if (ni <= ind + 1 || s[ni] != ']')
ni <= ind + 1Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 64109 times by 1 test
Evaluated by:
  • Self test
s[ni] != ']'Description
TRUEnever evaluated
FALSEevaluated 64109 times by 1 test
Evaluated by:
  • Self test
0-64109
993 {-
994 err_badarraysub (s);-
995 if (subp)
subpDescription
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-11
996 *subp = t;
executed 11 times by 1 test: *subp = t;
Executed by:
  • Self test
11
997 if (lenp)
lenpDescription
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-11
998 *lenp = 0;
executed 11 times by 1 test: *lenp = 0;
Executed by:
  • Self test
11
999 return ((char *)NULL);
executed 11 times by 1 test: return ((char *) ((void *)0) );
Executed by:
  • Self test
11
1000 }-
1001-
1002 *t = '\0';-
1003 ret = savestring (s);-
1004 *t++ = '['; /* ] */-
1005-
1006 if (subp)
subpDescription
TRUEevaluated 33858 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 30251 times by 1 test
Evaluated by:
  • Self test
30251-33858
1007 *subp = t;
executed 33858 times by 1 test: *subp = t;
Executed by:
  • Self test
33858
1008 if (lenp)
lenpDescription
TRUEevaluated 27300 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 36809 times by 1 test
Evaluated by:
  • Self test
27300-36809
1009 *lenp = ni - ind;
executed 27300 times by 1 test: *lenp = ni - ind;
Executed by:
  • Self test
27300
1010-
1011 return ret;
executed 64109 times by 1 test: return ret;
Executed by:
  • Self test
64109
1012}-
1013-
1014/* Return the variable specified by S without any subscript. If SUBP is-
1015 non-null, return a pointer to the start of the subscript in *SUBP.-
1016 If LENP is non-null, the length of the subscript is returned in *LENP. */-
1017SHELL_VAR *-
1018array_variable_part (s, flags, subp, lenp)-
1019 const char *s;-
1020 int flags;-
1021 char **subp;-
1022 int *lenp;-
1023{-
1024 char *t;-
1025 SHELL_VAR *var;-
1026-
1027 t = array_variable_name (s, flags, subp, lenp);-
1028 if (t == 0)
t == 0Description
TRUEnever evaluated
FALSEevaluated 43180 times by 1 test
Evaluated by:
  • Self test
0-43180
1029 return ((SHELL_VAR *)NULL);
never executed: return ((SHELL_VAR *) ((void *)0) );
0
1030 var = find_variable (t); /* XXX - handle namerefs here? */-
1031-
1032 free (t);-
1033 return var; /* now return invisible variables; caller must handle */
executed 43180 times by 1 test: return var;
Executed by:
  • Self test
43180
1034}-
1035-
1036#define INDEX_ERROR() \-
1037 do \-
1038 { \-
1039 if (var) \-
1040 err_badarraysub (var->name); \-
1041 else \-
1042 { \-
1043 t[-1] = '\0'; \-
1044 err_badarraysub (s); \-
1045 t[-1] = '['; /* ] */\-
1046 } \-
1047 return ((char *)NULL); \-
1048 } \-
1049 while (0)-
1050-
1051/* Return a string containing the elements in the array and subscript-
1052 described by S. If the subscript is * or @, obeys quoting rules akin-
1053 to the expansion of $* and $@ including double quoting. If RTYPE-
1054 is non-null it gets 1 if the array reference is name[*], 2 if the-
1055 reference is name[@], and 0 otherwise. */-
1056static char *-
1057array_value_internal (s, quoted, flags, rtype, indp)-
1058 const char *s;-
1059 int quoted, flags, *rtype;-
1060 arrayind_t *indp;-
1061{-
1062 int len;-
1063 arrayind_t ind;-
1064 char *akey;-
1065 char *retval, *t, *temp;-
1066 WORD_LIST *l;-
1067 SHELL_VAR *var;-
1068-
1069 var = array_variable_part (s, (flags&AV_NOEXPAND) ? 1 : 0, &t, &len); /* XXX */-
1070-
1071 /* Expand the index, even if the variable doesn't exist, in case side-
1072 effects are needed, like ${w[i++]} where w is unset. */-
1073#if 0-
1074 if (var == 0)-
1075 return (char *)NULL;-
1076#endif-
1077-
1078 if (len == 0)
len == 0Description
TRUEnever evaluated
FALSEevaluated 16207 times by 1 test
Evaluated by:
  • Self test
0-16207
1079 return ((char *)NULL); /* error message already printed */
never executed: return ((char *) ((void *)0) );
0
1080-
1081 /* [ */-
1082 akey = 0;-
1083 if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
(t[0]) == '@'Description
TRUEevaluated 672 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 15535 times by 1 test
Evaluated by:
  • Self test
(t[0]) == '*'Description
TRUEevaluated 156 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 15379 times by 1 test
Evaluated by:
  • Self test
t[1] == ']'Description
TRUEevaluated 828 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-15535
1084 {-
1085 if (rtype)
rtypeDescription
TRUEevaluated 828 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-828
1086 *rtype = (t[0] == '*') ? 1 : 2;
executed 828 times by 1 test: *rtype = (t[0] == '*') ? 1 : 2;
Executed by:
  • Self test
(t[0] == '*')Description
TRUEevaluated 156 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 672 times by 1 test
Evaluated by:
  • Self test
156-828
1087 if ((flags & AV_ALLOWALL) == 0)
(flags & 0x001) == 0Description
TRUEnever evaluated
FALSEevaluated 828 times by 1 test
Evaluated by:
  • Self test
0-828
1088 {-
1089 err_badarraysub (s);-
1090 return ((char *)NULL);
never executed: return ((char *) ((void *)0) );
0
1091 }-
1092 else if (var == 0 || value_cell (var) == 0) /* XXX - check for invisible_p(var) ? */
var == 0Description
TRUEevaluated 5 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 823 times by 1 test
Evaluated by:
  • Self test
((var)->value) == 0Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 821 times by 1 test
Evaluated by:
  • Self test
2-823
1093 return ((char *)NULL);
executed 7 times by 1 test: return ((char *) ((void *)0) );
Executed by:
  • Self test
7
1094 else if (array_p (var) == 0 && assoc_p (var) == 0)
((((var)->attr...000004))) == 0Description
TRUEevaluated 94 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 727 times by 1 test
Evaluated by:
  • Self test
((((var)->attr...000040))) == 0Description
TRUEevaluated 9 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 85 times by 1 test
Evaluated by:
  • Self test
9-727
1095 l = add_string_to_list (value_cell (var), (WORD_LIST *)NULL);
executed 9 times by 1 test: l = make_word_list (make_word(((var)->value)), ((WORD_LIST *) ((void *)0) ));
Executed by:
  • Self test
9
1096 else if (assoc_p (var))
((((var)->attr... (0x0000040)))Description
TRUEevaluated 85 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 727 times by 1 test
Evaluated by:
  • Self test
85-727
1097 {-
1098 l = assoc_to_word_list (assoc_cell (var));-
1099 if (l == (WORD_LIST *)NULL)
l == (WORD_LIST *) ((void *)0)Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 84 times by 1 test
Evaluated by:
  • Self test
1-84
1100 return ((char *)NULL);
executed 1 time by 1 test: return ((char *) ((void *)0) );
Executed by:
  • Self test
1
1101 }
executed 84 times by 1 test: end of block
Executed by:
  • Self test
84
1102 else-
1103 {-
1104 l = array_to_word_list (array_cell (var));-
1105 if (l == (WORD_LIST *)NULL)
l == (WORD_LIST *) ((void *)0)Description
TRUEevaluated 26 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 701 times by 1 test
Evaluated by:
  • Self test
26-701
1106 return ((char *) NULL);
executed 26 times by 1 test: return ((char *) ((void *)0) );
Executed by:
  • Self test
26
1107 }
executed 701 times by 1 test: end of block
Executed by:
  • Self test
701
1108-
1109 /* Caller of array_value takes care of inspecting rtype and duplicating-
1110 retval if rtype == 0, so this is not a memory leak */-
1111 if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
t[0] == '*'Description
TRUEevaluated 156 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 638 times by 1 test
Evaluated by:
  • Self test
(quoted & (0x002|0x001))Description
TRUEevaluated 50 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 106 times by 1 test
Evaluated by:
  • Self test
50-638
1112 {-
1113 temp = string_list_dollar_star (l, quoted, (flags & AV_ASSIGNRHS) ? PF_ASSIGNRHS : 0);-
1114 retval = quote_string (temp);-
1115 free (temp);-
1116 }
executed 50 times by 1 test: end of block
Executed by:
  • Self test
50
1117 else /* ${name[@]} or unquoted ${name[*]} */-
1118 /* XXX - bash-4.4/bash-5.0 test AV_ASSIGNRHS and pass PF_ASSIGNRHS */-
1119 retval = string_list_dollar_at (l, quoted, (flags & AV_ASSIGNRHS) ? PF_ASSIGNRHS : 0);
executed 744 times by 1 test: retval = string_list_dollar_at (l, quoted, (flags & 0x010) ? 0x08 : 0);
Executed by:
  • Self test
744
1120-
1121 dispose_words (l);-
1122 }
executed 794 times by 1 test: end of block
Executed by:
  • Self test
794
1123 else-
1124 {-
1125 if (rtype)
rtypeDescription
TRUEevaluated 5124 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 10255 times by 1 test
Evaluated by:
  • Self test
5124-10255
1126 *rtype = 0;
executed 5124 times by 1 test: *rtype = 0;
Executed by:
  • Self test
5124
1127 if (var == 0 || array_p (var) || assoc_p (var) == 0)
var == 0Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 15367 times by 1 test
Evaluated by:
  • Self test
((((var)->attr... (0x0000004)))Description
TRUEevaluated 15240 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 127 times by 1 test
Evaluated by:
  • Self test
((((var)->attr...000040))) == 0Description
TRUEevaluated 63 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 64 times by 1 test
Evaluated by:
  • Self test
12-15367
1128 {-
1129 if ((flags & AV_USEIND) == 0 || indp == 0)
(flags & 0x004) == 0Description
TRUEevaluated 15174 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 141 times by 1 test
Evaluated by:
  • Self test
indp == 0Description
TRUEnever evaluated
FALSEevaluated 141 times by 1 test
Evaluated by:
  • Self test
0-15174
1130 {-
1131 ind = array_expand_index (var, t, len, flags);-
1132 if (ind < 0)
ind < 0Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 15147 times by 1 test
Evaluated by:
  • Self test
11-15147
1133 {-
1134 /* negative subscripts to indexed arrays count back from end */-
1135 if (var && array_p (var))
varDescription
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
((((var)->attr... (0x0000004)))Description
TRUEevaluated 11 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-11
1136 ind = array_max_index (array_cell (var)) + 1 + ind;
executed 11 times by 1 test: ind = (((ARRAY *)((var)->value))->max_index) + 1 + ind;
Executed by:
  • Self test
11
1137 if (ind < 0)
ind < 0Description
TRUEevaluated 10 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
1-10
1138 INDEX_ERROR();
executed 10 times by 1 test: err_badarraysub (var->name);
Executed by:
  • Self test
never executed: end of block
executed 10 times by 1 test: return ((char *) ((void *)0) );
Executed by:
  • Self test
never executed: end of block
varDescription
TRUEevaluated 10 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-10
1139 }
executed 1 time by 1 test: end of block
Executed by:
  • Self test
1
1140 if (indp)
indpDescription
TRUEevaluated 15143 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 5 times by 1 test
Evaluated by:
  • Self test
5-15143
1141 *indp = ind;
executed 15143 times by 1 test: *indp = ind;
Executed by:
  • Self test
15143
1142 }
executed 15148 times by 1 test: end of block
Executed by:
  • Self test
15148
1143 else if (indp)
indpDescription
TRUEevaluated 141 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-141
1144 ind = *indp;
executed 141 times by 1 test: ind = *indp;
Executed by:
  • Self test
141
1145 }
executed 15289 times by 1 test: end of block
Executed by:
  • Self test
15289
1146 else if (assoc_p (var))
((((var)->attr... (0x0000040)))Description
TRUEevaluated 64 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
0-64
1147 {-
1148 t[len - 1] = '\0';-
1149 if ((flags & AV_NOEXPAND) == 0)
(flags & 0x020) == 0Description
TRUEevaluated 57 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 7 times by 1 test
Evaluated by:
  • Self test
7-57
1150 akey = expand_assignment_string_to_string (t, 0); /* [ */
executed 57 times by 1 test: akey = expand_assignment_string_to_string (t, 0);
Executed by:
  • Self test
57
1151 else-
1152 akey = savestring (t);
executed 7 times by 1 test: akey = (char *)strcpy (sh_xmalloc((1 + strlen (t)), "arrayfunc.c", 1152), (t));
Executed by:
  • Self test
7
1153 t[len - 1] = ']';-
1154 if (akey == 0 || *akey == 0)
akey == 0Description
TRUEnever evaluated
FALSEevaluated 64 times by 1 test
Evaluated by:
  • Self test
*akey == 0Description
TRUEnever evaluated
FALSEevaluated 64 times by 1 test
Evaluated by:
  • Self test
0-64
1155 {-
1156 FREE (akey);
never executed: sh_xfree((akey), "arrayfunc.c", 1156);
akeyDescription
TRUEnever evaluated
FALSEnever evaluated
0
1157 INDEX_ERROR();
never executed: err_badarraysub (var->name);
never executed: end of block
never executed: return ((char *) ((void *)0) );
varDescription
TRUEnever evaluated
FALSEnever evaluated
0
1158 }-
1159 }
executed 64 times by 1 test: end of block
Executed by:
  • Self test
64
1160 -
1161 if (var == 0 || value_cell (var) == 0) /* XXX - check invisible_p(var) ? */
var == 0Description
TRUEevaluated 12 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 15341 times by 1 test
Evaluated by:
  • Self test
((var)->value) == 0Description
TRUEnever evaluated
FALSEevaluated 15341 times by 1 test
Evaluated by:
  • Self test
0-15341
1162 {-
1163 FREE (akey);
never executed: sh_xfree((akey), "arrayfunc.c", 1163);
akeyDescription
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • Self test
0-12
1164 return ((char *)NULL);
executed 12 times by 1 test: return ((char *) ((void *)0) );
Executed by:
  • Self test
12
1165 }-
1166 if (array_p (var) == 0 && assoc_p (var) == 0)
((((var)->attr...000004))) == 0Description
TRUEevaluated 127 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 15214 times by 1 test
Evaluated by:
  • Self test
((((var)->attr...000040))) == 0Description
TRUEevaluated 63 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 64 times by 1 test
Evaluated by:
  • Self test
63-15214
1167 return (ind == 0 ? value_cell (var) : (char *)NULL);
executed 63 times by 1 test: return (ind == 0 ? ((var)->value) : (char *) ((void *)0) );
Executed by:
  • Self test
63
1168 else if (assoc_p (var))
((((var)->attr... (0x0000040)))Description
TRUEevaluated 64 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 15214 times by 1 test
Evaluated by:
  • Self test
64-15214
1169 {-
1170 retval = assoc_reference (assoc_cell (var), akey);-
1171 free (akey);-
1172 }
executed 64 times by 1 test: end of block
Executed by:
  • Self test
64
1173 else-
1174 retval = array_reference (array_cell (var), ind);
executed 15214 times by 1 test: retval = array_reference ((ARRAY *)((var)->value), ind);
Executed by:
  • Self test
15214
1175 }-
1176-
1177 return retval;
executed 16072 times by 1 test: return retval;
Executed by:
  • Self test
16072
1178}-
1179-
1180/* Return a string containing the elements described by the array and-
1181 subscript contained in S, obeying quoting for subscripts * and @. */-
1182char *-
1183array_value (s, quoted, flags, rtype, indp)-
1184 const char *s;-
1185 int quoted, flags, *rtype;-
1186 arrayind_t *indp;-
1187{-
1188 return (array_value_internal (s, quoted, flags|AV_ALLOWALL, rtype, indp));
executed 6102 times by 1 test: return (array_value_internal (s, quoted, flags|0x001, rtype, indp));
Executed by:
  • Self test
6102
1189}-
1190-
1191/* Return the value of the array indexing expression S as a single string.-
1192 If (FLAGS & AV_ALLOWALL) is 0, do not allow `@' and `*' subscripts. This-
1193 is used by other parts of the shell such as the arithmetic expression-
1194 evaluator in expr.c. */-
1195char *-
1196get_array_value (s, flags, rtype, indp)-
1197 const char *s;-
1198 int flags, *rtype;-
1199 arrayind_t *indp;-
1200{-
1201 return (array_value_internal (s, 0, flags, rtype, indp));
executed 10105 times by 1 test: return (array_value_internal (s, 0, flags, rtype, indp));
Executed by:
  • Self test
10105
1202}-
1203-
1204char *-
1205array_keys (s, quoted)-
1206 char *s;-
1207 int quoted;-
1208{-
1209 int len;-
1210 char *retval, *t, *temp;-
1211 WORD_LIST *l;-
1212 SHELL_VAR *var;-
1213-
1214 var = array_variable_part (s, 0, &t, &len);-
1215-
1216 /* [ */-
1217 if (var == 0 || ALL_ELEMENT_SUB (t[0]) == 0 || t[1] != ']')
var == 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • Self test
((t[0]) == '@'...) == '*') == 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • Self test
(t[0]) == '@'Description
TRUEevaluated 38 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 2 times by 1 test
Evaluated by:
  • Self test
(t[0]) == '*'Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEnever evaluated
t[1] != ']'Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • Self test
0-40
1218 return (char *)NULL;
never executed: return (char *) ((void *)0) ;
0
1219-
1220 if (var_isset (var) == 0 || invisible_p (var))
((var)->value != 0) == 0Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • Self test
((((var)->attr... (0x0001000)))Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • Self test
0-40
1221 return (char *)NULL;
never executed: return (char *) ((void *)0) ;
0
1222-
1223 if (array_p (var) == 0 && assoc_p (var) == 0)
((((var)->attr...000004))) == 0Description
TRUEevaluated 32 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 8 times by 1 test
Evaluated by:
  • Self test
((((var)->attr...000040))) == 0Description
TRUEnever evaluated
FALSEevaluated 32 times by 1 test
Evaluated by:
  • Self test
0-32
1224 l = add_string_to_list ("0", (WORD_LIST *)NULL);
never executed: l = make_word_list (make_word("0"), ((WORD_LIST *) ((void *)0) ));
0
1225 else if (assoc_p (var))
((((var)->attr... (0x0000040)))Description
TRUEevaluated 32 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 8 times by 1 test
Evaluated by:
  • Self test
8-32
1226 l = assoc_keys_to_word_list (assoc_cell (var));
executed 32 times by 1 test: l = assoc_keys_to_word_list ((HASH_TABLE *)((var)->value));
Executed by:
  • Self test
32
1227 else-
1228 l = array_keys_to_word_list (array_cell (var));
executed 8 times by 1 test: l = array_keys_to_word_list ((ARRAY *)((var)->value));
Executed by:
  • Self test
8
1229 if (l == (WORD_LIST *)NULL)
l == (WORD_LIST *) ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 40 times by 1 test
Evaluated by:
  • Self test
0-40
1230 return ((char *) NULL);
never executed: return ((char *) ((void *)0) );
0
1231-
1232 if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
t[0] == '*'Description
TRUEevaluated 2 times by 1 test
Evaluated by:
  • Self test
FALSEevaluated 38 times by 1 test
Evaluated by:
  • Self test
(quoted & (0x002|0x001))Description
TRUEevaluated 1 time by 1 test
Evaluated by:
  • Self test
FALSEevaluated 1 time by 1 test
Evaluated by:
  • Self test
1-38
1233 {-
1234 temp = string_list_dollar_star (l, quoted, 0);-
1235 retval = quote_string (temp);-
1236 free (temp);-
1237 }
executed 1 time by 1 test: end of block
Executed by:
  • Self test
1
1238 else /* ${!name[@]} or unquoted ${!name[*]} */-
1239 retval = string_list_dollar_at (l, quoted, 0);
executed 39 times by 1 test: retval = string_list_dollar_at (l, quoted, 0);
Executed by:
  • Self test
39
1240-
1241 dispose_words (l);-
1242 return retval;
executed 40 times by 1 test: return retval;
Executed by:
  • Self test
40
1243}-
1244#endif /* ARRAY_VARS */-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.1.2