1 /* arrayfunc.c -- High-level array functions used by other parts of the shell. */
3 /* Copyright (C) 2001-2011 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
23 #if defined (ARRAY_VARS)
25 #if defined (HAVE_UNISTD_H)
37 #include "builtins/common.h"
39 extern char *this_command_name;
40 extern int last_command_exit_value;
41 extern int array_needs_making;
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));
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 *));
50 /* Standard error message to use when encountering an invalid array subscript */
51 const char * const bash_badsub_errmsg = N_("bad array subscript");
53 /* **************************************************************** */
55 /* Functions to manipulate array variables and perform assignments */
57 /* **************************************************************** */
59 /* Convert a shell variable to an array variable. The original value is
62 convert_var_to_array (var)
68 oldval = value_cell (var);
69 array = array_create ();
71 array_insert (array, 0, oldval);
73 FREE (value_cell (var));
74 var_setarray (var, array);
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;
80 INVALIDATE_EXPORTSTR (var);
84 VSETATTR (var, att_array);
85 VUNSETATTR (var, att_invisible);
90 /* Convert a shell variable to an array variable. The original value is
93 convert_var_to_assoc (var)
99 oldval = value_cell (var);
100 hash = assoc_create (0);
102 assoc_insert (hash, savestring ("0"), oldval);
104 FREE (value_cell (var));
105 var_setassoc (var, hash);
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;
111 INVALIDATE_EXPORTSTR (var);
112 if (exported_p (var))
113 array_needs_making++;
115 VSETATTR (var, att_assoc);
116 VUNSETATTR (var, att_invisible);
122 make_array_variable_value (entry, ind, key, value, flags)
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)
136 dentry = (SHELL_VAR *)xmalloc (sizeof (SHELL_VAR));
137 dentry->name = savestring (entry->name);
139 newval = assoc_reference (assoc_cell (entry), key);
141 newval = array_reference (array_cell (entry), ind);
143 dentry->value = savestring (newval);
146 dentry->value = (char *)xmalloc (1);
147 dentry->value[0] = '\0';
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
153 newval = make_variable_value (dentry, value, flags);
154 dispose_variable (dentry);
157 newval = make_variable_value (entry, value, flags);
163 bind_array_var_internal (entry, ind, key, value, flags)
172 newval = make_array_variable_value (entry, ind, key, value, flags);
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);
179 array_insert (array_cell (entry), ind, newval);
182 VUNSETATTR (entry, att_invisible); /* no longer invisible */
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].
191 If NAME does not exist, just create an array variable, no matter what
192 IND's value may be. */
194 bind_array_variable (name, ind, value, flags)
202 entry = find_shell_variable (name);
204 if (entry == (SHELL_VAR *) 0)
205 entry = make_new_array_variable (name);
206 else if (readonly_p (entry) || noassign_p (entry))
208 if (readonly_p (entry))
212 else if (array_p (entry) == 0)
213 entry = convert_var_to_array (entry);
215 /* ENTRY is an array variable, and ARRAY points to the value. */
216 return (bind_array_var_internal (entry, ind, 0, value, flags));
220 bind_array_element (entry, ind, value, flags)
226 return (bind_array_var_internal (entry, ind, 0, value, flags));
230 bind_assoc_variable (entry, name, key, value, flags)
240 if (readonly_p (entry) || noassign_p (entry))
242 if (readonly_p (entry))
247 return (bind_array_var_internal (entry, 0, key, value, flags));
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(). */
253 assign_array_element (name, value, flags)
261 vname = array_variable_name (name, &sub, &sublen);
264 return ((SHELL_VAR *)NULL);
266 if ((ALL_ELEMENT_SUB (sub[0]) && sub[1] == ']') || (sublen <= 1))
269 err_badarraysub (name);
270 return ((SHELL_VAR *)NULL);
273 entry = find_variable (vname);
274 entry = assign_array_element_internal (entry, name, vname, sub, sublen, value, flags);
281 assign_array_element_internal (entry, name, vname, sub, sublen, value, flags)
283 char *name; /* only used for error messages */
293 if (entry && assoc_p (entry))
295 sub[sublen-1] = '\0';
296 akey = expand_assignment_string_to_string (sub, 0); /* [ */
298 if (akey == 0 || *akey == 0)
300 err_badarraysub (name);
302 return ((SHELL_VAR *)NULL);
304 entry = bind_assoc_variable (entry, vname, akey, value, flags);
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;
314 err_badarraysub (name);
315 return ((SHELL_VAR *)NULL);
317 entry = bind_array_variable (vname, ind, value, flags);
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. */
330 find_or_make_array_variable (name, flags)
336 var = find_variable (name);
339 /* See if we have a nameref pointing to a variable that hasn't been
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));
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)))
350 if (readonly_p (var))
352 return ((SHELL_VAR *)NULL);
354 else if ((flags & 2) && array_p (var))
356 last_command_exit_value = 1;
357 report_error (_("%s: cannot convert indexed to associative array"), name);
358 return ((SHELL_VAR *)NULL);
360 else if (array_p (var) == 0 && assoc_p (var) == 0)
361 var = convert_var_to_array (var);
366 /* Perform a compound assignment statement for array NAME, where VALUE is
367 the text between the parens: NAME=( VALUE ) */
369 assign_array_from_string (name, value, flags)
377 if (flags & ASS_MKASSOC)
380 var = find_or_make_array_variable (name, vflags);
382 return ((SHELL_VAR *)NULL);
384 return (assign_array_var_from_string (var, value, flags));
387 /* Sequentially assign the indices of indexed array variable VAR from the
390 assign_array_var_from_word_list (var, list, flags)
395 register arrayind_t i;
396 register WORD_LIST *l;
399 a = array_cell (var);
400 i = (flags & ASS_APPEND) ? array_max_index (a) + 1 : 0;
402 for (l = list; l; l = l->next, i++)
403 if (var->assign_func)
404 (*var->assign_func) (var, l->word->word, i, 0);
406 array_insert (a, i, l->word->word);
411 expand_compound_array_assignment (var, value, flags)
416 WORD_LIST *list, *nlist;
417 WORD_LIST *hd, *tl, *t, *n;
421 /* This condition is true when invoked from the declare builtin with a
423 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")' */
424 if (*value == '(') /*)*/
427 val = extract_array_assignment_list (value, &ni);
429 return (WORD_LIST *)NULL;
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");
440 if (var && assoc_p (var))
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). */
452 quote_array_assignment_chars (list);
454 /* Now that we've split it, perform the shell expansions on each
456 nlist = list ? expand_words_no_vars (list) : (WORD_LIST *)NULL;
458 dispose_words (list);
466 /* Callers ensure that VAR is not NULL */
468 assign_compound_array_list (var, nlist, flags)
476 char *w, *val, *nval;
477 int len, iflags, free_val;
478 arrayind_t ind, last_ind;
481 a = (var && array_p (var)) ? array_cell (var) : (ARRAY *)0;
482 h = (var && assoc_p (var)) ? assoc_cell (var) : (HASH_TABLE *)0;
487 /* Now that we are ready to assign values to the array, kill the existing
489 if ((flags & ASS_APPEND) == 0)
491 if (a && array_p (var))
493 else if (h && assoc_p (var))
497 last_ind = (a && (flags & ASS_APPEND)) ? array_max_index (a) + 1 : 0;
499 for (list = nlist; list; list = list->next)
502 w = list->word->word;
504 /* We have a word of the form [ind]=value */
505 if ((list->word->flags & W_ASSIGNMENT) && w[0] == '[')
507 /* Don't have to handle embedded quotes specially any more, since
508 associative array subscripts have not been expanded yet (see
510 len = skipsubscript (w, 0, 0);
512 /* XXX - changes for `+=' */
513 if (w[len] != ']' || (w[len+1] != '=' && (w[len+1] != '+' || w[len+2] != '=')))
520 nval = make_variable_value (var, w, flags);
521 if (var->assign_func)
522 (*var->assign_func) (var, nval, last_ind, 0);
524 array_insert (a, last_ind, nval);
536 if (ALL_ELEMENT_SUB (w[1]) && len == 2)
538 last_command_exit_value = 1;
540 report_error (_("%s: invalid associative array key"), w);
542 report_error (_("%s: cannot assign to non-numeric index"), w);
548 ind = array_expand_index (var, w + 1, len);
549 /* negative subscripts to indexed arrays count back from end */
551 ind = array_max_index (array_cell (var)) + 1 + ind;
560 else if (assoc_p (var))
562 /* This is not performed above, see expand_compound_array_assignment */
564 akey = expand_assignment_string_to_string (w+1, 0);
566 /* And we need to expand the value also, see below */
567 if (akey == 0 || *akey == 0)
575 /* XXX - changes for `+=' -- just accept the syntax. ksh93 doesn't do this */
576 if (w[len + 1] == '+' && w[len + 2] == '=')
578 iflags |= ASS_APPEND;
584 else if (assoc_p (var))
586 last_command_exit_value = 1;
587 report_error (_("%s: %s: must use subscript when assigning associative array"), var->name, w);
590 else /* No [ind]=value, just a stray `=' */
597 /* See above; we need to expand the value here */
600 val = expand_assignment_string_to_string (val, 0);
603 val = (char *)xmalloc (1);
604 val[0] = '\0'; /* like do_assignment_internal */
610 this_command_name = (char *)NULL; /* no command name for errors */
611 bind_array_var_internal (var, ind, akey, val, iflags);
619 /* Perform a compound array assignment: VAR->name=( VALUE ). The
620 VALUE has already had the parentheses stripped. */
622 assign_array_var_from_string (var, value, flags)
632 nlist = expand_compound_array_assignment (var, value, flags);
633 assign_compound_array_list (var, nlist, flags);
636 dispose_words (nlist);
640 /* Quote globbing chars and characters in $IFS before the `=' in an assignment
641 statement (usually a compound array assignment) to protect them from
642 unwanted filename expansion or word splitting. */
644 quote_assign (string)
649 char *temp, *t, *subs;
650 const char *s, *send;
654 slen = strlen (string);
655 send = string + slen;
657 t = temp = (char *)xmalloc (slen * 2 + 1);
659 for (s = string; *s; )
663 if (saw_eq == 0 && *s == '[') /* looks like a subscript */
666 se = skipsubscript (string, ss, 0);
667 subs = substring (s, ss, se);
677 if (saw_eq == 0 && (glob_char_p (s) || isifs (*s)))
680 COPY_CHAR_P (t, s, send);
686 /* For each word in a compound array assignment, if the word looks like
687 [ind]=value, quote globbing chars and characters in $IFS before the `='. */
689 quote_array_assignment_chars (list)
695 for (l = list; l; l = l->next)
697 if (l->word == 0 || l->word->word == 0 || l->word->word[0] == '\0')
698 continue; /* should not happen, but just in case... */
699 /* Don't bother if it hasn't been recognized as an assignment or
700 doesn't look like [ind]=value */
701 if ((l->word->flags & W_ASSIGNMENT) == 0)
703 if (l->word->word[0] != '[' || mbschr (l->word->word, '=') == 0) /* ] */
706 nword = quote_assign (l->word->word);
707 free (l->word->word);
708 l->word->word = nword;
709 l->word->flags |= W_NOGLOB; /* XXX - W_NOSPLIT also? */
713 /* skipsubscript moved to subst.c to use private functions. 2009/02/24. */
715 /* This function is called with SUB pointing to just after the beginning
716 `[' of an array subscript and removes the array element to which SUB
717 expands from array VAR. A subscript of `*' or `@' unsets the array. */
719 unbind_array_element (var, sub)
728 len = skipsubscript (sub, 0, 0);
729 if (sub[len] != ']' || len == 0)
731 builtin_error ("%s[%s: %s", var->name, sub, _(bash_badsub_errmsg));
736 if (ALL_ELEMENT_SUB (sub[0]) && sub[1] == 0)
738 unbind_variable (var->name);
744 akey = expand_assignment_string_to_string (sub, 0); /* [ */
745 if (akey == 0 || *akey == 0)
747 builtin_error ("[%s]: %s", sub, _(bash_badsub_errmsg));
751 assoc_remove (assoc_cell (var), akey);
756 ind = array_expand_index (var, sub, len+1);
757 /* negative subscripts to indexed arrays count back from end */
759 ind = array_max_index (array_cell (var)) + 1 + ind;
762 builtin_error ("[%s]: %s", sub, _(bash_badsub_errmsg));
765 ae = array_remove (array_cell (var), ind);
767 array_dispose_element (ae);
773 /* Format and output an array assignment in compound form VAR=(VALUES),
774 suitable for re-use as input. */
776 print_array_assignment (var, quoted)
782 vstr = array_to_assign (array_cell (var), quoted);
785 printf ("%s=%s\n", var->name, quoted ? "'()'" : "()");
788 printf ("%s=%s\n", var->name, vstr);
793 /* Format and output an associative array assignment in compound form
794 VAR=(VALUES), suitable for re-use as input. */
796 print_assoc_assignment (var, quoted)
802 vstr = assoc_to_assign (assoc_cell (var), quoted);
805 printf ("%s=%s\n", var->name, quoted ? "'()'" : "()");
808 printf ("%s=%s\n", var->name, vstr);
813 /***********************************************************************/
815 /* Utility functions to manage arrays and their contents for expansion */
817 /***********************************************************************/
819 /* Return 1 if NAME is a properly-formed array reference v[sub]. */
821 valid_array_reference (name)
827 t = mbschr (name, '['); /* ] */
831 r = legal_identifier (name);
835 /* Check for a properly-terminated non-blank subscript. */
836 len = skipsubscript (t, 0, 0);
837 if (t[len] != ']' || len == 1)
839 for (r = 1; r < len; r++)
840 if (whitespace (t[r]) == 0)
847 /* Expand the array index beginning at S and extending LEN characters. */
849 array_expand_index (var, s, len)
858 exp = (char *)xmalloc (len);
859 strncpy (exp, s, len - 1);
861 t = expand_arith_string (exp, 0);
862 this_command_name = (char *)NULL;
863 val = evalexp (t, &expok);
868 last_command_exit_value = EXECUTION_FAILURE;
870 top_level_cleanup ();
871 jump_to_top_level (DISCARD);
876 /* Return the name of the variable specified by S without any subscript.
877 If SUBP is non-null, return a pointer to the start of the subscript
878 in *SUBP. If LENP is non-null, the length of the subscript is returned
879 in *LENP. This returns newly-allocated memory. */
881 array_variable_name (s, subp, lenp)
895 return ((char *)NULL);
898 ni = skipsubscript (s, ind, 0);
899 if (ni <= ind + 1 || s[ni] != ']')
906 return ((char *)NULL);
910 ret = savestring (s);
921 /* Return the variable specified by S without any subscript. If SUBP is
922 non-null, return a pointer to the start of the subscript in *SUBP.
923 If LENP is non-null, the length of the subscript is returned in *LENP. */
925 array_variable_part (s, subp, lenp)
932 t = array_variable_name (s, subp, lenp);
934 return ((SHELL_VAR *)NULL);
935 var = find_variable (t);
939 return (var == 0 || invisible_p (var)) ? (SHELL_VAR *)0 : var;
941 return var; /* now return invisible variables; caller must handle */
945 #define INDEX_ERROR() \
949 err_badarraysub (var->name); \
953 err_badarraysub (s); \
954 t[-1] = '['; /* ] */\
956 return ((char *)NULL); \
960 /* Return a string containing the elements in the array and subscript
961 described by S. If the subscript is * or @, obeys quoting rules akin
962 to the expansion of $* and $@ including double quoting. If RTYPE
963 is non-null it gets 1 if the array reference is name[*], 2 if the
964 reference is name[@], and 0 otherwise. */
966 array_value_internal (s, quoted, flags, rtype, indp)
968 int quoted, flags, *rtype;
974 char *retval, *t, *temp;
978 var = array_variable_part (s, &t, &len);
980 /* Expand the index, even if the variable doesn't exist, in case side
981 effects are needed, like ${w[i++]} where w is unset. */
988 return ((char *)NULL); /* error message already printed */
992 if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
995 *rtype = (t[0] == '*') ? 1 : 2;
996 if ((flags & AV_ALLOWALL) == 0)
999 return ((char *)NULL);
1001 else if (var == 0 || value_cell (var) == 0) /* XXX - check for invisible_p(var) ? */
1002 return ((char *)NULL);
1003 else if (array_p (var) == 0 && assoc_p (var) == 0)
1004 l = add_string_to_list (value_cell (var), (WORD_LIST *)NULL);
1005 else if (assoc_p (var))
1007 l = assoc_to_word_list (assoc_cell (var));
1008 if (l == (WORD_LIST *)NULL)
1009 return ((char *)NULL);
1013 l = array_to_word_list (array_cell (var));
1014 if (l == (WORD_LIST *)NULL)
1015 return ((char *) NULL);
1018 if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
1020 temp = string_list_dollar_star (l);
1021 retval = quote_string (temp); /* XXX - leak here */
1024 else /* ${name[@]} or unquoted ${name[*]} */
1025 retval = string_list_dollar_at (l, quoted); /* XXX - leak here */
1033 if (var == 0 || array_p (var) || assoc_p (var) == 0)
1035 if ((flags & AV_USEIND) == 0 || indp == 0)
1037 ind = array_expand_index (var, t, len);
1040 /* negative subscripts to indexed arrays count back from end */
1041 if (var && array_p (var))
1042 ind = array_max_index (array_cell (var)) + 1 + ind;
1052 else if (assoc_p (var))
1055 akey = expand_assignment_string_to_string (t, 0); /* [ */
1057 if (akey == 0 || *akey == 0)
1064 if (var == 0 || value_cell (var) == 0) /* XXX - check invisible_p(var) ? */
1067 return ((char *)NULL);
1069 if (array_p (var) == 0 && assoc_p (var) == 0)
1070 return (ind == 0 ? value_cell (var) : (char *)NULL);
1071 else if (assoc_p (var))
1073 retval = assoc_reference (assoc_cell (var), akey);
1077 retval = array_reference (array_cell (var), ind);
1083 /* Return a string containing the elements described by the array and
1084 subscript contained in S, obeying quoting for subscripts * and @. */
1086 array_value (s, quoted, flags, rtype, indp)
1088 int quoted, flags, *rtype;
1091 return (array_value_internal (s, quoted, flags|AV_ALLOWALL, rtype, indp));
1094 /* Return the value of the array indexing expression S as a single string.
1095 If (FLAGS & AV_ALLOWALL) is 0, do not allow `@' and `*' subscripts. This
1096 is used by other parts of the shell such as the arithmetic expression
1097 evaluator in expr.c. */
1099 get_array_value (s, flags, rtype, indp)
1104 return (array_value_internal (s, 0, flags, rtype, indp));
1108 array_keys (s, quoted)
1113 char *retval, *t, *temp;
1117 var = array_variable_part (s, &t, &len);
1120 if (var == 0 || ALL_ELEMENT_SUB (t[0]) == 0 || t[1] != ']')
1121 return (char *)NULL;
1123 if (var_isset (var) == 0 || invisible_p (var))
1124 return (char *)NULL;
1126 if (array_p (var) == 0 && assoc_p (var) == 0)
1127 l = add_string_to_list ("0", (WORD_LIST *)NULL);
1128 else if (assoc_p (var))
1129 l = assoc_keys_to_word_list (assoc_cell (var));
1131 l = array_keys_to_word_list (array_cell (var));
1132 if (l == (WORD_LIST *)NULL)
1133 return ((char *) NULL);
1135 if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
1137 temp = string_list_dollar_star (l);
1138 retval = quote_string (temp);
1141 else /* ${!name[@]} or unquoted ${!name[*]} */
1142 retval = string_list_dollar_at (l, quoted);
1147 #endif /* ARRAY_VARS */