1 /* arrayfunc.c -- High-level array functions used by other parts of the shell. */
3 /* Copyright (C) 2001-2010 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));
45 static char *quote_assign
__P((const char *));
46 static void quote_array_assignment_chars
__P((WORD_LIST
*));
47 static char *array_value_internal
__P((char *, int, int, int *, arrayind_t
*));
49 /* Standard error message to use when encountering an invalid array subscript */
50 const char * const bash_badsub_errmsg
= N_("bad array subscript");
52 /* **************************************************************** */
54 /* Functions to manipulate array variables and perform assignments */
56 /* **************************************************************** */
58 /* Convert a shell variable to an array variable. The original value is
61 convert_var_to_array (var
)
67 oldval
= value_cell (var
);
68 array
= array_create ();
70 array_insert (array
, 0, oldval
);
72 FREE (value_cell (var
));
73 var_setarray (var
, array
);
75 /* these aren't valid anymore */
76 var
->dynamic_value
= (sh_var_value_func_t
*)NULL
;
77 var
->assign_func
= (sh_var_assign_func_t
*)NULL
;
79 INVALIDATE_EXPORTSTR (var
);
83 VSETATTR (var
, att_array
);
84 VUNSETATTR (var
, att_invisible
);
89 /* Convert a shell variable to an array variable. The original value is
92 convert_var_to_assoc (var
)
98 oldval
= value_cell (var
);
99 hash
= assoc_create (0);
101 assoc_insert (hash
, savestring ("0"), oldval
);
103 FREE (value_cell (var
));
104 var_setassoc (var
, hash
);
106 /* these aren't valid anymore */
107 var
->dynamic_value
= (sh_var_value_func_t
*)NULL
;
108 var
->assign_func
= (sh_var_assign_func_t
*)NULL
;
110 INVALIDATE_EXPORTSTR (var
);
111 if (exported_p (var
))
112 array_needs_making
++;
114 VSETATTR (var
, att_assoc
);
115 VUNSETATTR (var
, att_invisible
);
121 bind_array_var_internal (entry
, ind
, key
, value
, flags
)
131 /* If we're appending, we need the old value of the array reference, so
132 fake out make_variable_value with a dummy SHELL_VAR */
133 if (flags
& ASS_APPEND
)
135 dentry
= (SHELL_VAR
*)xmalloc (sizeof (SHELL_VAR
));
136 dentry
->name
= savestring (entry
->name
);
138 newval
= assoc_reference (assoc_cell (entry
), key
);
140 newval
= array_reference (array_cell (entry
), ind
);
142 dentry
->value
= savestring (newval
);
145 dentry
->value
= (char *)xmalloc (1);
146 dentry
->value
[0] = '\0';
148 dentry
->exportstr
= 0;
149 dentry
->attributes
= entry
->attributes
& ~(att_array
|att_assoc
|att_exported
);
150 /* Leave the rest of the members uninitialized; the code doesn't look
152 newval
= make_variable_value (dentry
, value
, flags
);
153 dispose_variable (dentry
);
156 newval
= make_variable_value (entry
, value
, flags
);
158 if (entry
->assign_func
)
159 (*entry
->assign_func
) (entry
, newval
, ind
, key
);
160 else if (assoc_p (entry
))
161 assoc_insert (assoc_cell (entry
), key
, newval
);
163 array_insert (array_cell (entry
), ind
, newval
);
169 /* Perform an array assignment name[ind]=value. If NAME already exists and
170 is not an array, and IND is 0, perform name=value instead. If NAME exists
171 and is not an array, and IND is not 0, convert it into an array with the
172 existing value as name[0].
174 If NAME does not exist, just create an array variable, no matter what
175 IND's value may be. */
177 bind_array_variable (name
, ind
, value
, flags
)
185 entry
= var_lookup (name
, shell_variables
);
187 if (entry
== (SHELL_VAR
*) 0)
188 entry
= make_new_array_variable (name
);
189 else if (readonly_p (entry
) || noassign_p (entry
))
191 if (readonly_p (entry
))
195 else if (array_p (entry
) == 0)
196 entry
= convert_var_to_array (entry
);
198 /* ENTRY is an array variable, and ARRAY points to the value. */
199 return (bind_array_var_internal (entry
, ind
, 0, value
, flags
));
203 bind_array_element (entry
, ind
, value
, flags
)
209 return (bind_array_var_internal (entry
, ind
, 0, value
, flags
));
213 bind_assoc_variable (entry
, name
, key
, value
, flags
)
223 if (readonly_p (entry
) || noassign_p (entry
))
225 if (readonly_p (entry
))
230 return (bind_array_var_internal (entry
, 0, key
, value
, flags
));
233 /* Parse NAME, a lhs of an assignment statement of the form v[s], and
234 assign VALUE to that array element by calling bind_array_variable(). */
236 assign_array_element (name
, value
, flags
)
240 char *sub
, *vname
, *akey
;
245 vname
= array_variable_name (name
, &sub
, &sublen
);
248 return ((SHELL_VAR
*)NULL
);
250 if ((ALL_ELEMENT_SUB (sub
[0]) && sub
[1] == ']') || (sublen
<= 1))
253 err_badarraysub (name
);
254 return ((SHELL_VAR
*)NULL
);
257 entry
= find_variable (vname
);
259 if (entry
&& assoc_p (entry
))
261 sub
[sublen
-1] = '\0';
262 akey
= expand_assignment_string_to_string (sub
, 0); /* [ */
264 if (akey
== 0 || *akey
== 0)
267 err_badarraysub (name
);
268 return ((SHELL_VAR
*)NULL
);
270 entry
= bind_assoc_variable (entry
, vname
, akey
, value
, flags
);
274 ind
= array_expand_index (sub
, sublen
);
278 err_badarraysub (name
);
279 return ((SHELL_VAR
*)NULL
);
281 entry
= bind_array_variable (vname
, ind
, value
, flags
);
288 /* Find the array variable corresponding to NAME. If there is no variable,
289 create a new array variable. If the variable exists but is not an array,
290 convert it to an indexed array. If FLAGS&1 is non-zero, an existing
291 variable is checked for the readonly or noassign attribute in preparation
292 for assignment (e.g., by the `read' builtin). If FLAGS&2 is non-zero, we
293 create an associative array. */
295 find_or_make_array_variable (name
, flags
)
301 var
= find_variable (name
);
304 var
= (flags
& 2) ? make_new_assoc_variable (name
) : make_new_array_variable (name
);
305 else if ((flags
& 1) && (readonly_p (var
) || noassign_p (var
)))
307 if (readonly_p (var
))
309 return ((SHELL_VAR
*)NULL
);
311 else if ((flags
& 2) && array_p (var
))
313 report_error (_("%s: cannot convert indexed to associative array"), name
);
314 return ((SHELL_VAR
*)NULL
);
316 else if (array_p (var
) == 0 && assoc_p (var
) == 0)
317 var
= convert_var_to_array (var
);
322 /* Perform a compound assignment statement for array NAME, where VALUE is
323 the text between the parens: NAME=( VALUE ) */
325 assign_array_from_string (name
, value
, flags
)
333 if (flags
& ASS_MKASSOC
)
336 var
= find_or_make_array_variable (name
, vflags
);
338 return ((SHELL_VAR
*)NULL
);
340 return (assign_array_var_from_string (var
, value
, flags
));
343 /* Sequentially assign the indices of indexed array variable VAR from the
346 assign_array_var_from_word_list (var
, list
, flags
)
351 register arrayind_t i
;
352 register WORD_LIST
*l
;
355 a
= array_cell (var
);
356 i
= (flags
& ASS_APPEND
) ? array_max_index (a
) + 1 : 0;
358 for (l
= list
; l
; l
= l
->next
, i
++)
359 if (var
->assign_func
)
360 (*var
->assign_func
) (var
, l
->word
->word
, i
, 0);
362 array_insert (a
, i
, l
->word
->word
);
367 expand_compound_array_assignment (var
, value
, flags
)
372 WORD_LIST
*list
, *nlist
;
376 /* I don't believe this condition is ever true any more. */
377 if (*value
== '(') /*)*/
380 val
= extract_array_assignment_list (value
, &ni
);
382 return (WORD_LIST
*)NULL
;
387 /* Expand the value string into a list of words, performing all the
388 shell expansions including pathname generation and word splitting. */
389 /* First we split the string on whitespace, using the shell parser
390 (ksh93 seems to do this). */
391 list
= parse_string_to_word_list (val
, 1, "array assign");
393 /* If we're using [subscript]=value, we need to quote each [ and ] to
394 prevent unwanted filename expansion. */
396 quote_array_assignment_chars (list
);
398 /* Now that we've split it, perform the shell expansions on each
400 nlist
= list
? expand_words_no_vars (list
) : (WORD_LIST
*)NULL
;
402 dispose_words (list
);
410 /* Callers ensure that VAR is not NULL */
412 assign_compound_array_list (var
, nlist
, flags
)
420 char *w
, *val
, *nval
;
422 arrayind_t ind
, last_ind
;
425 a
= (var
&& array_p (var
)) ? array_cell (var
) : (ARRAY
*)0;
426 h
= (var
&& assoc_p (var
)) ? assoc_cell (var
) : (HASH_TABLE
*)0;
431 /* Now that we are ready to assign values to the array, kill the existing
433 if ((flags
& ASS_APPEND
) == 0)
435 if (a
&& array_p (var
))
437 else if (h
&& assoc_p (var
))
441 last_ind
= (a
&& (flags
& ASS_APPEND
)) ? array_max_index (a
) + 1 : 0;
443 for (list
= nlist
; list
; list
= list
->next
)
446 w
= list
->word
->word
;
448 /* We have a word of the form [ind]=value */
449 if ((list
->word
->flags
& W_ASSIGNMENT
) && w
[0] == '[')
451 len
= skipsubscript (w
, 0, (var
&& assoc_p (var
) != 0));
453 /* XXX - changes for `+=' */
454 if (w
[len
] != ']' || (w
[len
+1] != '=' && (w
[len
+1] != '+' || w
[len
+2] != '=')))
461 nval
= make_variable_value (var
, w
, flags
);
462 if (var
->assign_func
)
463 (*var
->assign_func
) (var
, nval
, last_ind
, 0);
465 array_insert (a
, last_ind
, nval
);
477 if (ALL_ELEMENT_SUB (w
[1]) && len
== 2)
480 report_error (_("%s: invalid associative array key"), w
);
482 report_error (_("%s: cannot assign to non-numeric index"), w
);
488 ind
= array_expand_index (w
+ 1, len
);
497 else if (assoc_p (var
))
499 akey
= substring (w
, 1, len
);
500 if (akey
== 0 || *akey
== 0)
507 /* XXX - changes for `+=' -- just accept the syntax. ksh93 doesn't do this */
508 if (w
[len
+ 1] == '+' && w
[len
+ 2] == '=')
510 iflags
|= ASS_APPEND
;
516 else if (assoc_p (var
))
518 report_error (_("%s: %s: must use subscript when assigning associative array"), var
->name
, w
);
521 else /* No [ind]=value, just a stray `=' */
528 this_command_name
= (char *)NULL
; /* no command name for errors */
529 bind_array_var_internal (var
, ind
, akey
, val
, iflags
);
534 /* Perform a compound array assignment: VAR->name=( VALUE ). The
535 VALUE has already had the parentheses stripped. */
537 assign_array_var_from_string (var
, value
, flags
)
547 nlist
= expand_compound_array_assignment (var
, value
, flags
);
548 assign_compound_array_list (var
, nlist
, flags
);
551 dispose_words (nlist
);
555 /* Quote globbing chars and characters in $IFS before the `=' in an assignment
556 statement (usually a compound array assignment) to protect them from
557 unwanted filename expansion or word splitting. */
559 quote_assign (string
)
564 char *temp
, *t
, *subs
;
565 const char *s
, *send
;
569 slen
= strlen (string
);
570 send
= string
+ slen
;
572 t
= temp
= (char *)xmalloc (slen
* 2 + 1);
574 for (s
= string
; *s
; )
578 if (saw_eq
== 0 && *s
== '[') /* looks like a subscript */
581 se
= skipsubscript (string
, ss
, 0);
582 subs
= substring (s
, ss
, se
);
592 if (saw_eq
== 0 && (glob_char_p (s
) || isifs (*s
)))
595 COPY_CHAR_P (t
, s
, send
);
601 /* For each word in a compound array assignment, if the word looks like
602 [ind]=value, quote globbing chars and characters in $IFS before the `='. */
604 quote_array_assignment_chars (list
)
610 for (l
= list
; l
; l
= l
->next
)
612 if (l
->word
== 0 || l
->word
->word
== 0 || l
->word
->word
[0] == '\0')
613 continue; /* should not happen, but just in case... */
614 /* Don't bother if it doesn't look like [ind]=value */
615 if (l
->word
->word
[0] != '[' || mbschr (l
->word
->word
, '=') == 0) /* ] */
617 nword
= quote_assign (l
->word
->word
);
618 free (l
->word
->word
);
619 l
->word
->word
= nword
;
623 /* skipsubscript moved to subst.c to use private functions. 2009/02/24. */
625 /* This function is called with SUB pointing to just after the beginning
626 `[' of an array subscript and removes the array element to which SUB
627 expands from array VAR. A subscript of `*' or `@' unsets the array. */
629 unbind_array_element (var
, sub
)
638 len
= skipsubscript (sub
, 0, 0);
639 if (sub
[len
] != ']' || len
== 0)
641 builtin_error ("%s[%s: %s", var
->name
, sub
, _(bash_badsub_errmsg
));
646 if (ALL_ELEMENT_SUB (sub
[0]) && sub
[1] == 0)
648 unbind_variable (var
->name
);
654 akey
= expand_assignment_string_to_string (sub
, 0); /* [ */
655 if (akey
== 0 || *akey
== 0)
657 builtin_error ("[%s]: %s", sub
, _(bash_badsub_errmsg
));
660 assoc_remove (assoc_cell (var
), akey
);
665 ind
= array_expand_index (sub
, len
+1);
668 builtin_error ("[%s]: %s", sub
, _(bash_badsub_errmsg
));
671 ae
= array_remove (array_cell (var
), ind
);
673 array_dispose_element (ae
);
679 /* Format and output an array assignment in compound form VAR=(VALUES),
680 suitable for re-use as input. */
682 print_array_assignment (var
, quoted
)
688 vstr
= array_to_assign (array_cell (var
), quoted
);
691 printf ("%s=%s\n", var
->name
, quoted
? "'()'" : "()");
694 printf ("%s=%s\n", var
->name
, vstr
);
699 /* Format and output an associative array assignment in compound form
700 VAR=(VALUES), suitable for re-use as input. */
702 print_assoc_assignment (var
, quoted
)
708 vstr
= assoc_to_assign (assoc_cell (var
), quoted
);
711 printf ("%s=%s\n", var
->name
, quoted
? "'()'" : "()");
714 printf ("%s=%s\n", var
->name
, vstr
);
719 /***********************************************************************/
721 /* Utility functions to manage arrays and their contents for expansion */
723 /***********************************************************************/
725 /* Return 1 if NAME is a properly-formed array reference v[sub]. */
727 valid_array_reference (name
)
733 t
= mbschr (name
, '['); /* ] */
737 r
= legal_identifier (name
);
741 /* Check for a properly-terminated non-blank subscript. */
742 len
= skipsubscript (t
, 0, 0);
743 if (t
[len
] != ']' || len
== 1)
745 for (r
= 1; r
< len
; r
++)
746 if (whitespace (t
[r
]) == 0)
753 /* Expand the array index beginning at S and extending LEN characters. */
755 array_expand_index (s
, len
)
763 exp
= (char *)xmalloc (len
);
764 strncpy (exp
, s
, len
- 1);
766 t
= expand_arith_string (exp
, 0);
767 this_command_name
= (char *)NULL
;
768 val
= evalexp (t
, &expok
);
773 last_command_exit_value
= EXECUTION_FAILURE
;
775 top_level_cleanup ();
776 jump_to_top_level (DISCARD
);
781 /* Return the name of the variable specified by S without any subscript.
782 If SUBP is non-null, return a pointer to the start of the subscript
783 in *SUBP. If LENP is non-null, the length of the subscript is returned
784 in *LENP. This returns newly-allocated memory. */
786 array_variable_name (s
, subp
, lenp
)
800 return ((char *)NULL
);
803 ni
= skipsubscript (s
, ind
, 0);
804 if (ni
<= ind
+ 1 || s
[ni
] != ']')
811 return ((char *)NULL
);
815 ret
= savestring (s
);
826 /* Return the variable specified by S without any subscript. If SUBP is
827 non-null, return a pointer to the start of the subscript in *SUBP.
828 If LENP is non-null, the length of the subscript is returned in *LENP. */
830 array_variable_part (s
, subp
, lenp
)
837 t
= array_variable_name (s
, subp
, lenp
);
839 return ((SHELL_VAR
*)NULL
);
840 var
= find_variable (t
);
843 return (var
== 0 || invisible_p (var
)) ? (SHELL_VAR
*)0 : var
;
846 #define INDEX_ERROR() \
850 err_badarraysub (var->name); \
854 err_badarraysub (s); \
855 t[-1] = '['; /* ] */\
857 return ((char *)NULL); \
861 /* Return a string containing the elements in the array and subscript
862 described by S. If the subscript is * or @, obeys quoting rules akin
863 to the expansion of $* and $@ including double quoting. If RTYPE
864 is non-null it gets 1 if the array reference is name[*], 2 if the
865 reference is name[@], and 0 otherwise. */
867 array_value_internal (s
, quoted
, flags
, rtype
, indp
)
869 int quoted
, flags
, *rtype
;
875 char *retval
, *t
, *temp
;
879 var
= array_variable_part (s
, &t
, &len
);
881 /* Expand the index, even if the variable doesn't exist, in case side
882 effects are needed, like ${w[i++]} where w is unset. */
889 return ((char *)NULL
); /* error message already printed */
892 if (ALL_ELEMENT_SUB (t
[0]) && t
[1] == ']')
895 *rtype
= (t
[0] == '*') ? 1 : 2;
896 if ((flags
& AV_ALLOWALL
) == 0)
899 return ((char *)NULL
);
901 else if (var
== 0 || value_cell (var
) == 0) /* XXX - check for invisible_p(var) ? */
902 return ((char *)NULL
);
903 else if (array_p (var
) == 0 && assoc_p (var
) == 0)
904 l
= add_string_to_list (value_cell (var
), (WORD_LIST
*)NULL
);
905 else if (assoc_p (var
))
907 l
= assoc_to_word_list (assoc_cell (var
));
908 if (l
== (WORD_LIST
*)NULL
)
909 return ((char *)NULL
);
913 l
= array_to_word_list (array_cell (var
));
914 if (l
== (WORD_LIST
*)NULL
)
915 return ((char *) NULL
);
918 if (t
[0] == '*' && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
920 temp
= string_list_dollar_star (l
);
921 retval
= quote_string (temp
);
924 else /* ${name[@]} or unquoted ${name[*]} */
925 retval
= string_list_dollar_at (l
, quoted
);
933 if (var
== 0 || array_p (var
) || assoc_p (var
) == 0)
935 if ((flags
& AV_USEIND
) == 0 || indp
== 0)
937 ind
= array_expand_index (t
, len
);
940 /* negative subscripts to indexed arrays count back from end */
941 if (var
&& array_p (var
))
942 ind
= array_max_index (array_cell (var
)) + 1 + ind
;
952 else if (assoc_p (var
))
955 akey
= expand_assignment_string_to_string (t
, 0); /* [ */
957 if (akey
== 0 || *akey
== 0)
961 if (var
== 0 || value_cell (var
) == 0) /* XXX - check invisible_p(var) ? */
962 return ((char *)NULL
);
963 if (array_p (var
) == 0 && assoc_p (var
) == 0)
964 return (ind
== 0 ? value_cell (var
) : (char *)NULL
);
965 else if (assoc_p (var
))
967 retval
= assoc_reference (assoc_cell (var
), akey
);
971 retval
= array_reference (array_cell (var
), ind
);
977 /* Return a string containing the elements described by the array and
978 subscript contained in S, obeying quoting for subscripts * and @. */
980 array_value (s
, quoted
, flags
, rtype
, indp
)
982 int quoted
, flags
, *rtype
;
985 return (array_value_internal (s
, quoted
, flags
|AV_ALLOWALL
, rtype
, indp
));
988 /* Return the value of the array indexing expression S as a single string.
989 If (FLAGS & AV_ALLOWALL) is 0, do not allow `@' and `*' subscripts. This
990 is used by other parts of the shell such as the arithmetic expression
991 evaluator in expr.c. */
993 get_array_value (s
, flags
, rtype
, indp
)
998 return (array_value_internal (s
, 0, flags
, rtype
, indp
));
1002 array_keys (s
, quoted
)
1007 char *retval
, *t
, *temp
;
1011 var
= array_variable_part (s
, &t
, &len
);
1014 if (var
== 0 || ALL_ELEMENT_SUB (t
[0]) == 0 || t
[1] != ']')
1015 return (char *)NULL
;
1017 if (var_isset (var
) == 0 || invisible_p (var
))
1018 return (char *)NULL
;
1020 if (array_p (var
) == 0 && assoc_p (var
) == 0)
1021 l
= add_string_to_list ("0", (WORD_LIST
*)NULL
);
1022 else if (assoc_p (var
))
1023 l
= assoc_keys_to_word_list (assoc_cell (var
));
1025 l
= array_keys_to_word_list (array_cell (var
));
1026 if (l
== (WORD_LIST
*)NULL
)
1027 return ((char *) NULL
);
1029 if (t
[0] == '*' && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
1031 temp
= string_list_dollar_star (l
);
1032 retval
= quote_string (temp
);
1035 else /* ${!name[@]} or unquoted ${!name[*]} */
1036 retval
= string_list_dollar_at (l
, quoted
);
1041 #endif /* ARRAY_VARS */