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