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