1 /* arrayfunc.c -- High-level array functions used by other parts of the shell. */
3 /* Copyright (C) 2001-2016 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)
33 #include "execute_cmd.h"
37 #if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR)
38 # include <mbstr.h> /* mbschr */
41 #include "builtins/common.h"
43 /* This variable means to not expand associative array subscripts more than
44 once, when performing variable expansion. */
45 int assoc_expand_once
= 0;
47 /* Ditto for indexed array subscripts -- currently unused */
48 int array_expand_once
= 0;
50 static SHELL_VAR
*bind_array_var_internal
__P((SHELL_VAR
*, arrayind_t
, char *, char *, int));
51 static SHELL_VAR
*assign_array_element_internal
__P((SHELL_VAR
*, char *, char *, char *, int, char *, int));
53 static char *quote_assign
__P((const char *));
54 static void quote_array_assignment_chars
__P((WORD_LIST
*));
55 static char *array_value_internal
__P((const char *, int, int, int *, arrayind_t
*));
57 /* Standard error message to use when encountering an invalid array subscript */
58 const char * const bash_badsub_errmsg
= N_("bad array subscript");
60 /* **************************************************************** */
62 /* Functions to manipulate array variables and perform assignments */
64 /* **************************************************************** */
66 /* Convert a shell variable to an array variable. The original value is
69 convert_var_to_array (var
)
75 oldval
= value_cell (var
);
76 array
= array_create ();
78 array_insert (array
, 0, oldval
);
80 FREE (value_cell (var
));
81 var_setarray (var
, array
);
83 /* these aren't valid anymore */
84 var
->dynamic_value
= (sh_var_value_func_t
*)NULL
;
85 var
->assign_func
= (sh_var_assign_func_t
*)NULL
;
87 INVALIDATE_EXPORTSTR (var
);
91 VSETATTR (var
, att_array
);
92 VUNSETATTR (var
, att_invisible
);
94 /* Make sure it's not marked as an associative array any more */
95 VUNSETATTR (var
, att_assoc
);
97 /* Since namerefs can't be array variables, turn off nameref attribute */
98 VUNSETATTR (var
, att_nameref
);
103 /* Convert a shell variable to an array variable. The original value is
104 saved as array[0]. */
106 convert_var_to_assoc (var
)
112 oldval
= value_cell (var
);
113 hash
= assoc_create (0);
115 assoc_insert (hash
, savestring ("0"), oldval
);
117 FREE (value_cell (var
));
118 var_setassoc (var
, hash
);
120 /* these aren't valid anymore */
121 var
->dynamic_value
= (sh_var_value_func_t
*)NULL
;
122 var
->assign_func
= (sh_var_assign_func_t
*)NULL
;
124 INVALIDATE_EXPORTSTR (var
);
125 if (exported_p (var
))
126 array_needs_making
++;
128 VSETATTR (var
, att_assoc
);
129 VUNSETATTR (var
, att_invisible
);
131 /* Make sure it's not marked as an indexed array any more */
132 VUNSETATTR (var
, att_array
);
134 /* Since namerefs can't be array variables, turn off nameref attribute */
135 VUNSETATTR (var
, att_nameref
);
141 make_array_variable_value (entry
, ind
, key
, value
, flags
)
151 /* If we're appending, we need the old value of the array reference, so
152 fake out make_variable_value with a dummy SHELL_VAR */
153 if (flags
& ASS_APPEND
)
155 dentry
= (SHELL_VAR
*)xmalloc (sizeof (SHELL_VAR
));
156 dentry
->name
= savestring (entry
->name
);
158 newval
= assoc_reference (assoc_cell (entry
), key
);
160 newval
= array_reference (array_cell (entry
), ind
);
162 dentry
->value
= savestring (newval
);
165 dentry
->value
= (char *)xmalloc (1);
166 dentry
->value
[0] = '\0';
168 dentry
->exportstr
= 0;
169 dentry
->attributes
= entry
->attributes
& ~(att_array
|att_assoc
|att_exported
);
170 /* Leave the rest of the members uninitialized; the code doesn't look
172 newval
= make_variable_value (dentry
, value
, flags
);
173 dispose_variable (dentry
);
176 newval
= make_variable_value (entry
, value
, flags
);
182 bind_array_var_internal (entry
, ind
, key
, value
, flags
)
191 newval
= make_array_variable_value (entry
, ind
, key
, value
, flags
);
193 if (entry
->assign_func
)
194 (*entry
->assign_func
) (entry
, newval
, ind
, key
);
195 else if (assoc_p (entry
))
196 assoc_insert (assoc_cell (entry
), key
, newval
);
198 array_insert (array_cell (entry
), ind
, newval
);
201 VUNSETATTR (entry
, att_invisible
); /* no longer invisible */
203 /* check mark_modified_variables if we ever want to export array vars */
207 /* Perform an array assignment name[ind]=value. If NAME already exists and
208 is not an array, and IND is 0, perform name=value instead. If NAME exists
209 and is not an array, and IND is not 0, convert it into an array with the
210 existing value as name[0].
212 If NAME does not exist, just create an array variable, no matter what
213 IND's value may be. */
215 bind_array_variable (name
, ind
, value
, flags
)
223 entry
= find_shell_variable (name
);
225 if (entry
== (SHELL_VAR
*) 0)
227 /* Is NAME a nameref variable that points to an unset variable? */
228 entry
= find_variable_nameref_for_create (name
, 0);
229 if (entry
== INVALID_NAMEREF_VALUE
)
230 return ((SHELL_VAR
*)0);
231 if (entry
&& nameref_p (entry
))
232 entry
= make_new_array_variable (nameref_cell (entry
));
234 if (entry
== (SHELL_VAR
*) 0)
235 entry
= make_new_array_variable (name
);
236 else if ((readonly_p (entry
) && (flags
&ASS_FORCE
) == 0) || noassign_p (entry
))
238 if (readonly_p (entry
))
242 else if (array_p (entry
) == 0)
243 entry
= convert_var_to_array (entry
);
245 /* ENTRY is an array variable, and ARRAY points to the value. */
246 return (bind_array_var_internal (entry
, ind
, 0, value
, flags
));
250 bind_array_element (entry
, ind
, value
, flags
)
256 return (bind_array_var_internal (entry
, ind
, 0, value
, flags
));
260 bind_assoc_variable (entry
, name
, key
, value
, flags
)
267 if ((readonly_p (entry
) && (flags
&ASS_FORCE
) == 0) || noassign_p (entry
))
269 if (readonly_p (entry
))
274 return (bind_array_var_internal (entry
, 0, key
, value
, flags
));
277 /* Parse NAME, a lhs of an assignment statement of the form v[s], and
278 assign VALUE to that array element by calling bind_array_variable().
279 Flags are ASS_ assignment flags */
281 assign_array_element (name
, value
, flags
)
289 vname
= array_variable_name (name
, (flags
& ASS_NOEXPAND
) != 0, &sub
, &sublen
);
292 return ((SHELL_VAR
*)NULL
);
294 entry
= find_variable (vname
);
295 isassoc
= entry
&& assoc_p (entry
);
297 if (((isassoc
== 0 || (flags
& ASS_NOEXPAND
) == 0) && (ALL_ELEMENT_SUB (sub
[0]) && sub
[1] == ']')) || (sublen
<= 1))
300 err_badarraysub (name
);
301 return ((SHELL_VAR
*)NULL
);
304 entry
= assign_array_element_internal (entry
, name
, vname
, sub
, sublen
, value
, flags
);
311 assign_array_element_internal (entry
, name
, vname
, sub
, sublen
, value
, flags
)
313 char *name
; /* only used for error messages */
323 if (entry
&& assoc_p (entry
))
325 sub
[sublen
-1] = '\0';
326 if ((flags
& ASS_NOEXPAND
) == 0)
327 akey
= expand_assignment_string_to_string (sub
, 0); /* [ */
329 akey
= savestring (sub
);
331 if (akey
== 0 || *akey
== 0)
333 err_badarraysub (name
);
335 return ((SHELL_VAR
*)NULL
);
337 entry
= bind_assoc_variable (entry
, vname
, akey
, value
, flags
);
341 ind
= array_expand_index (entry
, sub
, sublen
, 0);
342 /* negative subscripts to indexed arrays count back from end */
343 if (entry
&& ind
< 0)
344 ind
= (array_p (entry
) ? array_max_index (array_cell (entry
)) : 0) + 1 + ind
;
347 err_badarraysub (name
);
348 return ((SHELL_VAR
*)NULL
);
350 entry
= bind_array_variable (vname
, ind
, value
, flags
);
356 /* Find the array variable corresponding to NAME. If there is no variable,
357 create a new array variable. If the variable exists but is not an array,
358 convert it to an indexed array. If FLAGS&1 is non-zero, an existing
359 variable is checked for the readonly or noassign attribute in preparation
360 for assignment (e.g., by the `read' builtin). If FLAGS&2 is non-zero, we
361 create an associative array. */
363 find_or_make_array_variable (name
, flags
)
369 var
= find_variable (name
);
372 /* See if we have a nameref pointing to a variable that hasn't been
374 var
= find_variable_last_nameref (name
, 1);
375 if (var
&& nameref_p (var
) && invisible_p (var
))
377 internal_warning (_("%s: removing nameref attribute"), name
);
378 VUNSETATTR (var
, att_nameref
);
380 if (var
&& nameref_p (var
))
382 if (valid_nameref_value (nameref_cell (var
), 2) == 0)
384 sh_invalidid (nameref_cell (var
));
385 return ((SHELL_VAR
*)NULL
);
387 var
= (flags
& 2) ? make_new_assoc_variable (nameref_cell (var
)) : make_new_array_variable (nameref_cell (var
));
392 var
= (flags
& 2) ? make_new_assoc_variable (name
) : make_new_array_variable (name
);
393 else if ((flags
& 1) && (readonly_p (var
) || noassign_p (var
)))
395 if (readonly_p (var
))
397 return ((SHELL_VAR
*)NULL
);
399 else if ((flags
& 2) && array_p (var
))
401 last_command_exit_value
= 1;
402 report_error (_("%s: cannot convert indexed to associative array"), name
);
403 return ((SHELL_VAR
*)NULL
);
405 else if (array_p (var
) == 0 && assoc_p (var
) == 0)
406 var
= convert_var_to_array (var
);
411 /* Perform a compound assignment statement for array NAME, where VALUE is
412 the text between the parens: NAME=( VALUE ) */
414 assign_array_from_string (name
, value
, flags
)
422 if (flags
& ASS_MKASSOC
)
425 var
= find_or_make_array_variable (name
, vflags
);
427 return ((SHELL_VAR
*)NULL
);
429 return (assign_array_var_from_string (var
, value
, flags
));
432 /* Sequentially assign the indices of indexed array variable VAR from the
435 assign_array_var_from_word_list (var
, list
, flags
)
440 register arrayind_t i
;
441 register WORD_LIST
*l
;
444 a
= array_cell (var
);
445 i
= (flags
& ASS_APPEND
) ? array_max_index (a
) + 1 : 0;
447 for (l
= list
; l
; l
= l
->next
, i
++)
448 bind_array_var_internal (var
, i
, 0, l
->word
->word
, flags
& ~ASS_APPEND
);
450 VUNSETATTR (var
, att_invisible
); /* no longer invisible */
456 expand_compound_array_assignment (var
, value
, flags
)
461 WORD_LIST
*list
, *nlist
;
465 /* This condition is true when invoked from the declare builtin with a
467 declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")' */
468 if (*value
== '(') /*)*/
471 val
= extract_array_assignment_list (value
, &ni
);
473 return (WORD_LIST
*)NULL
;
478 /* Expand the value string into a list of words, performing all the
479 shell expansions including pathname generation and word splitting. */
480 /* First we split the string on whitespace, using the shell parser
481 (ksh93 seems to do this). */
482 list
= parse_string_to_word_list (val
, 1, "array assign");
484 if (var
&& assoc_p (var
))
491 /* If we're using [subscript]=value, we need to quote each [ and ] to
492 prevent unwanted filename expansion. This doesn't need to be done
493 for associative array expansion, since that uses a different expansion
494 function (see assign_compound_array_list below). */
496 quote_array_assignment_chars (list
);
498 /* Now that we've split it, perform the shell expansions on each
500 nlist
= list
? expand_words_no_vars (list
) : (WORD_LIST
*)NULL
;
502 dispose_words (list
);
510 /* Callers ensure that VAR is not NULL */
512 assign_compound_array_list (var
, nlist
, flags
)
520 char *w
, *val
, *nval
, *savecmd
;
521 int len
, iflags
, free_val
;
522 arrayind_t ind
, last_ind
;
525 a
= (var
&& array_p (var
)) ? array_cell (var
) : (ARRAY
*)0;
526 h
= (var
&& assoc_p (var
)) ? assoc_cell (var
) : (HASH_TABLE
*)0;
531 /* Now that we are ready to assign values to the array, kill the existing
533 if ((flags
& ASS_APPEND
) == 0)
535 if (a
&& array_p (var
))
537 else if (h
&& assoc_p (var
))
541 last_ind
= (a
&& (flags
& ASS_APPEND
)) ? array_max_index (a
) + 1 : 0;
543 for (list
= nlist
; list
; list
= list
->next
)
545 /* Don't allow var+=(values) to make assignments in VALUES append to
546 existing values by default. */
547 iflags
= flags
& ~ASS_APPEND
;
548 w
= list
->word
->word
;
550 /* We have a word of the form [ind]=value */
551 if ((list
->word
->flags
& W_ASSIGNMENT
) && w
[0] == '[')
553 /* Don't have to handle embedded quotes specially any more, since
554 associative array subscripts have not been expanded yet (see
556 len
= skipsubscript (w
, 0, 0);
558 /* XXX - changes for `+=' */
559 if (w
[len
] != ']' || (w
[len
+1] != '=' && (w
[len
+1] != '+' || w
[len
+2] != '=')))
566 nval
= make_variable_value (var
, w
, flags
);
567 if (var
->assign_func
)
568 (*var
->assign_func
) (var
, nval
, last_ind
, 0);
570 array_insert (a
, last_ind
, nval
);
582 if (ALL_ELEMENT_SUB (w
[1]) && len
== 2)
584 last_command_exit_value
= 1;
586 report_error (_("%s: invalid associative array key"), w
);
588 report_error (_("%s: cannot assign to non-numeric index"), w
);
594 ind
= array_expand_index (var
, w
+ 1, len
, 0);
595 /* negative subscripts to indexed arrays count back from end */
597 ind
= array_max_index (array_cell (var
)) + 1 + ind
;
606 else if (assoc_p (var
))
608 /* This is not performed above, see expand_compound_array_assignment */
610 akey
= expand_assignment_string_to_string (w
+1, 0);
612 /* And we need to expand the value also, see below */
613 if (akey
== 0 || *akey
== 0)
621 /* XXX - changes for `+=' -- just accept the syntax. ksh93 doesn't do this */
622 if (w
[len
+ 1] == '+' && w
[len
+ 2] == '=')
624 iflags
|= ASS_APPEND
;
630 else if (assoc_p (var
))
632 last_command_exit_value
= 1;
633 report_error (_("%s: %s: must use subscript when assigning associative array"), var
->name
, w
);
636 else /* No [ind]=value, just a stray `=' */
643 /* See above; we need to expand the value here */
646 val
= expand_assignment_string_to_string (val
, 0);
649 val
= (char *)xmalloc (1);
650 val
[0] = '\0'; /* like do_assignment_internal */
655 savecmd
= this_command_name
;
657 this_command_name
= (char *)NULL
; /* no command name for errors */
658 bind_array_var_internal (var
, ind
, akey
, val
, iflags
);
660 this_command_name
= savecmd
;
667 /* Perform a compound array assignment: VAR->name=( VALUE ). The
668 VALUE has already had the parentheses stripped. */
670 assign_array_var_from_string (var
, value
, flags
)
680 nlist
= expand_compound_array_assignment (var
, value
, flags
);
681 assign_compound_array_list (var
, nlist
, flags
);
684 dispose_words (nlist
);
687 VUNSETATTR (var
, att_invisible
); /* no longer invisible */
692 /* Quote globbing chars and characters in $IFS before the `=' in an assignment
693 statement (usually a compound array assignment) to protect them from
694 unwanted filename expansion or word splitting. */
696 quote_assign (string
)
701 char *temp
, *t
, *subs
;
702 const char *s
, *send
;
706 slen
= strlen (string
);
707 send
= string
+ slen
;
709 t
= temp
= (char *)xmalloc (slen
* 2 + 1);
711 for (s
= string
; *s
; )
715 if (saw_eq
== 0 && *s
== '[') /* looks like a subscript */
718 se
= skipsubscript (string
, ss
, 0);
719 subs
= substring (s
, ss
, se
);
729 if (saw_eq
== 0 && (glob_char_p (s
) || isifs (*s
)))
732 COPY_CHAR_P (t
, s
, send
);
738 /* For each word in a compound array assignment, if the word looks like
739 [ind]=value, quote globbing chars and characters in $IFS before the `='. */
741 quote_array_assignment_chars (list
)
747 for (l
= list
; l
; l
= l
->next
)
749 if (l
->word
== 0 || l
->word
->word
== 0 || l
->word
->word
[0] == '\0')
750 continue; /* should not happen, but just in case... */
751 /* Don't bother if it hasn't been recognized as an assignment or
752 doesn't look like [ind]=value */
753 if ((l
->word
->flags
& W_ASSIGNMENT
) == 0)
755 if (l
->word
->word
[0] != '[' || mbschr (l
->word
->word
, '=') == 0) /* ] */
758 nword
= quote_assign (l
->word
->word
);
759 free (l
->word
->word
);
760 l
->word
->word
= nword
;
761 l
->word
->flags
|= W_NOGLOB
; /* XXX - W_NOSPLIT also? */
765 /* skipsubscript moved to subst.c to use private functions. 2009/02/24. */
767 /* This function is called with SUB pointing to just after the beginning
768 `[' of an array subscript and removes the array element to which SUB
769 expands from array VAR. A subscript of `*' or `@' unsets the array. */
770 /* If FLAGS&1 we don't expand the subscript; we just use it as-is. */
772 unbind_array_element (var
, sub
, flags
)
782 len
= skipsubscript (sub
, 0, (flags
&1) || (var
&& assoc_p(var
))); /* XXX */
783 if (sub
[len
] != ']' || len
== 0)
785 builtin_error ("%s[%s: %s", var
->name
, sub
, _(bash_badsub_errmsg
));
790 if (ALL_ELEMENT_SUB (sub
[0]) && sub
[1] == 0)
792 if (array_p (var
) || assoc_p (var
))
794 unbind_variable (var
->name
); /* XXX -- {array,assoc}_flush ? */
798 return -2; /* don't allow this to unset scalar variables */
803 akey
= (flags
& 1) ? sub
: expand_assignment_string_to_string (sub
, 0);
804 if (akey
== 0 || *akey
== 0)
806 builtin_error ("[%s]: %s", sub
, _(bash_badsub_errmsg
));
810 assoc_remove (assoc_cell (var
), akey
);
814 else if (array_p (var
))
816 ind
= array_expand_index (var
, sub
, len
+1, 0);
817 /* negative subscripts to indexed arrays count back from end */
819 ind
= array_max_index (array_cell (var
)) + 1 + ind
;
822 builtin_error ("[%s]: %s", sub
, _(bash_badsub_errmsg
));
825 ae
= array_remove (array_cell (var
), ind
);
827 array_dispose_element (ae
);
829 else /* array_p (var) == 0 && assoc_p (var) == 0 */
831 akey
= this_command_name
;
832 ind
= array_expand_index (var
, sub
, len
+1, 0);
833 this_command_name
= akey
;
836 unbind_variable (var
->name
);
840 return -2; /* any subscript other than 0 is invalid with scalar variables */
846 /* Format and output an array assignment in compound form VAR=(VALUES),
847 suitable for re-use as input. */
849 print_array_assignment (var
, quoted
)
855 vstr
= array_to_assign (array_cell (var
), quoted
);
858 printf ("%s=%s\n", var
->name
, quoted
? "'()'" : "()");
861 printf ("%s=%s\n", var
->name
, vstr
);
866 /* Format and output an associative array assignment in compound form
867 VAR=(VALUES), suitable for re-use as input. */
869 print_assoc_assignment (var
, quoted
)
875 vstr
= assoc_to_assign (assoc_cell (var
), quoted
);
878 printf ("%s=%s\n", var
->name
, quoted
? "'()'" : "()");
881 printf ("%s=%s\n", var
->name
, vstr
);
886 /***********************************************************************/
888 /* Utility functions to manage arrays and their contents for expansion */
890 /***********************************************************************/
892 /* Return 1 if NAME is a properly-formed array reference v[sub]. */
894 /* We need to reserve 1 for FLAGS, which we pass to skipsubscript. */
896 valid_array_reference (name
, flags
)
904 t
= mbschr (name
, '['); /* ] */
909 r
= legal_identifier (name
);
910 if (flags
& VA_NOEXPAND
) /* Don't waste a lookup if we don't need one */
911 isassoc
= (entry
= find_variable (name
)) && assoc_p (entry
);
916 if (isassoc
&& ((flags
& (VA_NOEXPAND
|VA_ONEWORD
)) == (VA_NOEXPAND
|VA_ONEWORD
)))
917 len
= strlen (t
) - 1;
919 len
= skipsubscript (t
, 0, flags
&VA_NOEXPAND
); /* VA_NOEXPAND must be 1 */
921 /* Check for a properly-terminated non-null subscript. */
922 len
= skipsubscript (t
, 0, 0); /* arithmetic expression */
924 if (t
[len
] != ']' || len
== 1 || t
[len
+1] != '\0')
928 /* Could check and allow subscripts consisting only of whitespace for
929 existing associative arrays, using isassoc */
930 for (r
= 1; r
< len
; r
++)
931 if (whitespace (t
[r
]) == 0)
935 /* This allows blank subscripts */
942 /* Expand the array index beginning at S and extending LEN characters. */
944 array_expand_index (var
, s
, len
, flags
)
950 char *exp
, *t
, *savecmd
;
954 exp
= (char *)xmalloc (len
);
955 strncpy (exp
, s
, len
- 1);
957 #if 0 /* XXX - not yet -- maybe bash-5.1 */
958 if ((flags
& AV_NOEXPAND
) == 0)
959 t
= expand_arith_string (exp
, Q_DOUBLE_QUOTES
|Q_ARITH
|Q_ARRAYSUB
); /* XXX - Q_ARRAYSUB for future use */
963 t
= expand_arith_string (exp
, Q_DOUBLE_QUOTES
|Q_ARITH
|Q_ARRAYSUB
); /* XXX - Q_ARRAYSUB for future use */
964 savecmd
= this_command_name
;
965 this_command_name
= (char *)NULL
;
966 val
= evalexp (t
, 0, &expok
);
967 this_command_name
= savecmd
;
973 last_command_exit_value
= EXECUTION_FAILURE
;
975 if (no_longjmp_on_fatal_error
)
977 top_level_cleanup ();
978 jump_to_top_level (DISCARD
);
983 /* Return the name of the variable specified by S without any subscript.
984 If SUBP is non-null, return a pointer to the start of the subscript
985 in *SUBP. If LENP is non-null, the length of the subscript is returned
986 in *LENP. This returns newly-allocated memory. */
988 array_variable_name (s
, flags
, subp
, lenp
)
1004 return ((char *)NULL
);
1007 ni
= skipsubscript (s
, ind
, flags
); /* XXX - was 0 not flags */
1008 if (ni
<= ind
+ 1 || s
[ni
] != ']')
1010 err_badarraysub (s
);
1015 return ((char *)NULL
);
1019 ret
= savestring (s
);
1030 /* Return the variable specified by S without any subscript. If SUBP is
1031 non-null, return a pointer to the start of the subscript in *SUBP.
1032 If LENP is non-null, the length of the subscript is returned in *LENP. */
1034 array_variable_part (s
, flags
, subp
, lenp
)
1043 t
= array_variable_name (s
, flags
, subp
, lenp
);
1045 return ((SHELL_VAR
*)NULL
);
1046 var
= find_variable (t
); /* XXX - handle namerefs here? */
1049 return var
; /* now return invisible variables; caller must handle */
1052 #define INDEX_ERROR() \
1056 err_badarraysub (var->name); \
1060 err_badarraysub (s); \
1061 t[-1] = '['; /* ] */\
1063 return ((char *)NULL); \
1067 /* Return a string containing the elements in the array and subscript
1068 described by S. If the subscript is * or @, obeys quoting rules akin
1069 to the expansion of $* and $@ including double quoting. If RTYPE
1070 is non-null it gets 1 if the array reference is name[*], 2 if the
1071 reference is name[@], and 0 otherwise. */
1073 array_value_internal (s
, quoted
, flags
, rtype
, indp
)
1075 int quoted
, flags
, *rtype
;
1081 char *retval
, *t
, *temp
;
1085 var
= array_variable_part (s
, (flags
&AV_NOEXPAND
) ? 1 : 0, &t
, &len
); /* XXX */
1087 /* Expand the index, even if the variable doesn't exist, in case side
1088 effects are needed, like ${w[i++]} where w is unset. */
1091 return (char *)NULL
;
1095 return ((char *)NULL
); /* error message already printed */
1099 if (ALL_ELEMENT_SUB (t
[0]) && t
[1] == ']')
1102 *rtype
= (t
[0] == '*') ? 1 : 2;
1103 if ((flags
& AV_ALLOWALL
) == 0)
1105 err_badarraysub (s
);
1106 return ((char *)NULL
);
1108 else if (var
== 0 || value_cell (var
) == 0) /* XXX - check for invisible_p(var) ? */
1109 return ((char *)NULL
);
1110 else if (invisible_p (var
))
1111 return ((char *)NULL
);
1112 else if (array_p (var
) == 0 && assoc_p (var
) == 0)
1113 l
= add_string_to_list (value_cell (var
), (WORD_LIST
*)NULL
);
1114 else if (assoc_p (var
))
1116 l
= assoc_to_word_list (assoc_cell (var
));
1117 if (l
== (WORD_LIST
*)NULL
)
1118 return ((char *)NULL
);
1122 l
= array_to_word_list (array_cell (var
));
1123 if (l
== (WORD_LIST
*)NULL
)
1124 return ((char *) NULL
);
1127 /* Caller of array_value takes care of inspecting rtype and duplicating
1128 retval if rtype == 0, so this is not a memory leak */
1129 if (t
[0] == '*' && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
1131 temp
= string_list_dollar_star (l
, quoted
, (flags
& AV_ASSIGNRHS
) ? PF_ASSIGNRHS
: 0);
1132 retval
= quote_string (temp
);
1135 else /* ${name[@]} or unquoted ${name[*]} */
1136 retval
= string_list_dollar_at (l
, quoted
, (flags
& AV_ASSIGNRHS
) ? PF_ASSIGNRHS
: 0);
1144 if (var
== 0 || array_p (var
) || assoc_p (var
) == 0)
1146 if ((flags
& AV_USEIND
) == 0 || indp
== 0)
1148 ind
= array_expand_index (var
, t
, len
, flags
);
1151 /* negative subscripts to indexed arrays count back from end */
1152 if (var
&& array_p (var
))
1153 ind
= array_max_index (array_cell (var
)) + 1 + ind
;
1163 else if (assoc_p (var
))
1166 if ((flags
& AV_NOEXPAND
) == 0)
1167 akey
= expand_assignment_string_to_string (t
, 0); /* [ */
1169 akey
= savestring (t
);
1171 if (akey
== 0 || *akey
== 0)
1178 if (var
== 0 || value_cell (var
) == 0) /* XXX - check invisible_p(var) ? */
1181 return ((char *)NULL
);
1183 else if (invisible_p (var
))
1186 return ((char *)NULL
);
1188 if (array_p (var
) == 0 && assoc_p (var
) == 0)
1189 return (ind
== 0 ? value_cell (var
) : (char *)NULL
);
1190 else if (assoc_p (var
))
1192 retval
= assoc_reference (assoc_cell (var
), akey
);
1196 retval
= array_reference (array_cell (var
), ind
);
1202 /* Return a string containing the elements described by the array and
1203 subscript contained in S, obeying quoting for subscripts * and @. */
1205 array_value (s
, quoted
, flags
, rtype
, indp
)
1207 int quoted
, flags
, *rtype
;
1210 return (array_value_internal (s
, quoted
, flags
|AV_ALLOWALL
, rtype
, indp
));
1213 /* Return the value of the array indexing expression S as a single string.
1214 If (FLAGS & AV_ALLOWALL) is 0, do not allow `@' and `*' subscripts. This
1215 is used by other parts of the shell such as the arithmetic expression
1216 evaluator in expr.c. */
1218 get_array_value (s
, flags
, rtype
, indp
)
1223 return (array_value_internal (s
, 0, flags
, rtype
, indp
));
1227 array_keys (s
, quoted
)
1232 char *retval
, *t
, *temp
;
1236 var
= array_variable_part (s
, 0, &t
, &len
);
1239 if (var
== 0 || ALL_ELEMENT_SUB (t
[0]) == 0 || t
[1] != ']')
1240 return (char *)NULL
;
1242 if (var_isset (var
) == 0 || invisible_p (var
))
1243 return (char *)NULL
;
1245 if (array_p (var
) == 0 && assoc_p (var
) == 0)
1246 l
= add_string_to_list ("0", (WORD_LIST
*)NULL
);
1247 else if (assoc_p (var
))
1248 l
= assoc_keys_to_word_list (assoc_cell (var
));
1250 l
= array_keys_to_word_list (array_cell (var
));
1251 if (l
== (WORD_LIST
*)NULL
)
1252 return ((char *) NULL
);
1254 if (t
[0] == '*' && (quoted
& (Q_HERE_DOCUMENT
|Q_DOUBLE_QUOTES
)))
1256 temp
= string_list_dollar_star (l
, quoted
, 0);
1257 retval
= quote_string (temp
);
1260 else /* ${!name[@]} or unquoted ${!name[*]} */
1261 retval
= string_list_dollar_at (l
, quoted
, 0);
1266 #endif /* ARRAY_VARS */