]> git.ipfire.org Git - thirdparty/bash.git/blob - arrayfunc.c
commit bash-20180928 snapshot
[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)));
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 if (t)
906 {
907 *t = '\0';
908 r = legal_identifier (name);
909 isassoc = (entry = find_variable (name)) && assoc_p (entry);
910 *t = '[';
911 if (r == 0)
912 return 0;
913
914 if (isassoc && ((flags & (VA_NOEXPAND|VA_ONEWORD)) == (VA_NOEXPAND|VA_ONEWORD)))
915 len = strlen (t) - 1;
916 else
917 /* Check for a properly-terminated non-null subscript. */
918 len = skipsubscript (t, 0, flags&VA_NOEXPAND); /* VA_NOEXPAND must be 1 */
919 if (t[len] != ']' || len == 1 || t[len+1] != '\0')
920 return 0;
921
922 #if 0
923 /* Could check and allow subscripts consisting only of whitespace for
924 existing associative arrays, using isassoc */
925 for (r = 1; r < len; r++)
926 if (whitespace (t[r]) == 0)
927 return 1;
928 return 0;
929 #else
930 /* This allows blank subscripts */
931 return 1;
932 #endif
933 }
934 return 0;
935 }
936
937 /* Expand the array index beginning at S and extending LEN characters. */
938 arrayind_t
939 array_expand_index (var, s, len, flags)
940 SHELL_VAR *var;
941 char *s;
942 int len;
943 int flags;
944 {
945 char *exp, *t, *savecmd;
946 int expok;
947 arrayind_t val;
948
949 exp = (char *)xmalloc (len);
950 strncpy (exp, s, len - 1);
951 exp[len - 1] = '\0';
952 #if 0 /* XXX - not yet -- maybe bash-5.0 */
953 if ((flags & AV_NOEXPAND) == 0)
954 t = expand_arith_string (exp, Q_DOUBLE_QUOTES|Q_ARITH|Q_ARRAYSUB); /* XXX - Q_ARRAYSUB for future use */
955 else
956 t = exp;
957 #endif
958 t = expand_arith_string (exp, Q_DOUBLE_QUOTES|Q_ARITH|Q_ARRAYSUB); /* XXX - Q_ARRAYSUB for future use */
959 savecmd = this_command_name;
960 this_command_name = (char *)NULL;
961 val = evalexp (t, 0, &expok);
962 this_command_name = savecmd;
963 if (t != exp)
964 free (t);
965 free (exp);
966 if (expok == 0)
967 {
968 last_command_exit_value = EXECUTION_FAILURE;
969
970 if (no_longjmp_on_fatal_error)
971 return 0;
972 top_level_cleanup ();
973 jump_to_top_level (DISCARD);
974 }
975 return val;
976 }
977
978 /* Return the name of the variable specified by S without any subscript.
979 If SUBP is non-null, return a pointer to the start of the subscript
980 in *SUBP. If LENP is non-null, the length of the subscript is returned
981 in *LENP. This returns newly-allocated memory. */
982 char *
983 array_variable_name (s, flags, subp, lenp)
984 const char *s;
985 int flags;
986 char **subp;
987 int *lenp;
988 {
989 char *t, *ret;
990 int ind, ni;
991
992 t = mbschr (s, '[');
993 if (t == 0)
994 {
995 if (subp)
996 *subp = t;
997 if (lenp)
998 *lenp = 0;
999 return ((char *)NULL);
1000 }
1001 ind = t - s;
1002 ni = skipsubscript (s, ind, flags); /* XXX - was 0 not flags */
1003 if (ni <= ind + 1 || s[ni] != ']')
1004 {
1005 err_badarraysub (s);
1006 if (subp)
1007 *subp = t;
1008 if (lenp)
1009 *lenp = 0;
1010 return ((char *)NULL);
1011 }
1012
1013 *t = '\0';
1014 ret = savestring (s);
1015 *t++ = '['; /* ] */
1016
1017 if (subp)
1018 *subp = t;
1019 if (lenp)
1020 *lenp = ni - ind;
1021
1022 return ret;
1023 }
1024
1025 /* Return the variable specified by S without any subscript. If SUBP is
1026 non-null, return a pointer to the start of the subscript in *SUBP.
1027 If LENP is non-null, the length of the subscript is returned in *LENP. */
1028 SHELL_VAR *
1029 array_variable_part (s, flags, subp, lenp)
1030 const char *s;
1031 int flags;
1032 char **subp;
1033 int *lenp;
1034 {
1035 char *t;
1036 SHELL_VAR *var;
1037
1038 t = array_variable_name (s, flags, subp, lenp);
1039 if (t == 0)
1040 return ((SHELL_VAR *)NULL);
1041 var = find_variable (t); /* XXX - handle namerefs here? */
1042
1043 free (t);
1044 return var; /* now return invisible variables; caller must handle */
1045 }
1046
1047 #define INDEX_ERROR() \
1048 do \
1049 { \
1050 if (var) \
1051 err_badarraysub (var->name); \
1052 else \
1053 { \
1054 t[-1] = '\0'; \
1055 err_badarraysub (s); \
1056 t[-1] = '['; /* ] */\
1057 } \
1058 return ((char *)NULL); \
1059 } \
1060 while (0)
1061
1062 /* Return a string containing the elements in the array and subscript
1063 described by S. If the subscript is * or @, obeys quoting rules akin
1064 to the expansion of $* and $@ including double quoting. If RTYPE
1065 is non-null it gets 1 if the array reference is name[*], 2 if the
1066 reference is name[@], and 0 otherwise. */
1067 static char *
1068 array_value_internal (s, quoted, flags, rtype, indp)
1069 const char *s;
1070 int quoted, flags, *rtype;
1071 arrayind_t *indp;
1072 {
1073 int len;
1074 arrayind_t ind;
1075 char *akey;
1076 char *retval, *t, *temp;
1077 WORD_LIST *l;
1078 SHELL_VAR *var;
1079
1080 var = array_variable_part (s, (flags&AV_NOEXPAND) ? 1 : 0, &t, &len); /* XXX */
1081
1082 /* Expand the index, even if the variable doesn't exist, in case side
1083 effects are needed, like ${w[i++]} where w is unset. */
1084 #if 0
1085 if (var == 0)
1086 return (char *)NULL;
1087 #endif
1088
1089 if (len == 0)
1090 return ((char *)NULL); /* error message already printed */
1091
1092 /* [ */
1093 akey = 0;
1094 if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
1095 {
1096 if (rtype)
1097 *rtype = (t[0] == '*') ? 1 : 2;
1098 if ((flags & AV_ALLOWALL) == 0)
1099 {
1100 err_badarraysub (s);
1101 return ((char *)NULL);
1102 }
1103 else if (var == 0 || value_cell (var) == 0) /* XXX - check for invisible_p(var) ? */
1104 return ((char *)NULL);
1105 else if (array_p (var) == 0 && assoc_p (var) == 0)
1106 l = add_string_to_list (value_cell (var), (WORD_LIST *)NULL);
1107 else if (assoc_p (var))
1108 {
1109 l = assoc_to_word_list (assoc_cell (var));
1110 if (l == (WORD_LIST *)NULL)
1111 return ((char *)NULL);
1112 }
1113 else
1114 {
1115 l = array_to_word_list (array_cell (var));
1116 if (l == (WORD_LIST *)NULL)
1117 return ((char *) NULL);
1118 }
1119
1120 /* Caller of array_value takes care of inspecting rtype and duplicating
1121 retval if rtype == 0, so this is not a memory leak */
1122 if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
1123 {
1124 temp = string_list_dollar_star (l, quoted, (flags & AV_ASSIGNRHS) ? PF_ASSIGNRHS : 0);
1125 retval = quote_string (temp);
1126 free (temp);
1127 }
1128 else /* ${name[@]} or unquoted ${name[*]} */
1129 /* XXX - bash-4.4/bash-5.0 test AV_ASSIGNRHS and pass PF_ASSIGNRHS */
1130 retval = string_list_dollar_at (l, quoted, (flags & AV_ASSIGNRHS) ? PF_ASSIGNRHS : 0);
1131
1132 dispose_words (l);
1133 }
1134 else
1135 {
1136 if (rtype)
1137 *rtype = 0;
1138 if (var == 0 || array_p (var) || assoc_p (var) == 0)
1139 {
1140 if ((flags & AV_USEIND) == 0 || indp == 0)
1141 {
1142 ind = array_expand_index (var, t, len, flags);
1143 if (ind < 0)
1144 {
1145 /* negative subscripts to indexed arrays count back from end */
1146 if (var && array_p (var))
1147 ind = array_max_index (array_cell (var)) + 1 + ind;
1148 if (ind < 0)
1149 INDEX_ERROR();
1150 }
1151 if (indp)
1152 *indp = ind;
1153 }
1154 else if (indp)
1155 ind = *indp;
1156 }
1157 else if (assoc_p (var))
1158 {
1159 t[len - 1] = '\0';
1160 if ((flags & AV_NOEXPAND) == 0)
1161 akey = expand_assignment_string_to_string (t, 0); /* [ */
1162 else
1163 akey = savestring (t);
1164 t[len - 1] = ']';
1165 if (akey == 0 || *akey == 0)
1166 {
1167 FREE (akey);
1168 INDEX_ERROR();
1169 }
1170 }
1171
1172 if (var == 0 || value_cell (var) == 0) /* XXX - check invisible_p(var) ? */
1173 {
1174 FREE (akey);
1175 return ((char *)NULL);
1176 }
1177 if (array_p (var) == 0 && assoc_p (var) == 0)
1178 return (ind == 0 ? value_cell (var) : (char *)NULL);
1179 else if (assoc_p (var))
1180 {
1181 retval = assoc_reference (assoc_cell (var), akey);
1182 free (akey);
1183 }
1184 else
1185 retval = array_reference (array_cell (var), ind);
1186 }
1187
1188 return retval;
1189 }
1190
1191 /* Return a string containing the elements described by the array and
1192 subscript contained in S, obeying quoting for subscripts * and @. */
1193 char *
1194 array_value (s, quoted, flags, rtype, indp)
1195 const char *s;
1196 int quoted, flags, *rtype;
1197 arrayind_t *indp;
1198 {
1199 return (array_value_internal (s, quoted, flags|AV_ALLOWALL, rtype, indp));
1200 }
1201
1202 /* Return the value of the array indexing expression S as a single string.
1203 If (FLAGS & AV_ALLOWALL) is 0, do not allow `@' and `*' subscripts. This
1204 is used by other parts of the shell such as the arithmetic expression
1205 evaluator in expr.c. */
1206 char *
1207 get_array_value (s, flags, rtype, indp)
1208 const char *s;
1209 int flags, *rtype;
1210 arrayind_t *indp;
1211 {
1212 return (array_value_internal (s, 0, flags, rtype, indp));
1213 }
1214
1215 char *
1216 array_keys (s, quoted)
1217 char *s;
1218 int quoted;
1219 {
1220 int len;
1221 char *retval, *t, *temp;
1222 WORD_LIST *l;
1223 SHELL_VAR *var;
1224
1225 var = array_variable_part (s, 0, &t, &len);
1226
1227 /* [ */
1228 if (var == 0 || ALL_ELEMENT_SUB (t[0]) == 0 || t[1] != ']')
1229 return (char *)NULL;
1230
1231 if (var_isset (var) == 0 || invisible_p (var))
1232 return (char *)NULL;
1233
1234 if (array_p (var) == 0 && assoc_p (var) == 0)
1235 l = add_string_to_list ("0", (WORD_LIST *)NULL);
1236 else if (assoc_p (var))
1237 l = assoc_keys_to_word_list (assoc_cell (var));
1238 else
1239 l = array_keys_to_word_list (array_cell (var));
1240 if (l == (WORD_LIST *)NULL)
1241 return ((char *) NULL);
1242
1243 if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
1244 {
1245 temp = string_list_dollar_star (l, quoted, 0);
1246 retval = quote_string (temp);
1247 free (temp);
1248 }
1249 else /* ${!name[@]} or unquoted ${!name[*]} */
1250 retval = string_list_dollar_at (l, quoted, 0);
1251
1252 dispose_words (l);
1253 return retval;
1254 }
1255 #endif /* ARRAY_VARS */