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