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