/* arrayfunc.c -- High-level array functions used by other parts of the shell. */
-/* Copyright (C) 2001-2009 Free Software Foundation, Inc.
+/* Copyright (C) 2001-2011 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
extern int array_needs_making;
static SHELL_VAR *bind_array_var_internal __P((SHELL_VAR *, arrayind_t, char *, char *, int));
+static SHELL_VAR *assign_array_element_internal __P((SHELL_VAR *, char *, char *, char *, int, char *, int));
static char *quote_assign __P((const char *));
static void quote_array_assignment_chars __P((WORD_LIST *));
-static char *array_value_internal __P((char *, int, int, int *));
+static char *array_value_internal __P((char *, int, int, int *, arrayind_t *));
/* Standard error message to use when encountering an invalid array subscript */
const char * const bash_badsub_errmsg = N_("bad array subscript");
return var;
}
-static SHELL_VAR *
-bind_array_var_internal (entry, ind, key, value, flags)
+char *
+make_array_variable_value (entry, ind, key, value, flags)
SHELL_VAR *entry;
arrayind_t ind;
char *key;
else
newval = make_variable_value (entry, value, flags);
+ return newval;
+}
+
+static SHELL_VAR *
+bind_array_var_internal (entry, ind, key, value, flags)
+ SHELL_VAR *entry;
+ arrayind_t ind;
+ char *key;
+ char *value;
+ int flags;
+{
+ char *newval;
+
+ newval = make_array_variable_value (entry, ind, key, value, flags);
+
if (entry->assign_func)
(*entry->assign_func) (entry, newval, ind, key);
else if (assoc_p (entry))
{
SHELL_VAR *entry;
- entry = var_lookup (name, shell_variables);
+ entry = find_shell_variable (name);
if (entry == (SHELL_VAR *) 0)
entry = make_new_array_variable (name);
char *name, *value;
int flags;
{
- char *sub, *vname, *akey;
- arrayind_t ind;
+ char *sub, *vname;
int sublen;
SHELL_VAR *entry;
}
entry = find_variable (vname);
+ entry = assign_array_element_internal (entry, name, vname, sub, sublen, value, flags);
+
+ free (vname);
+ return entry;
+}
+
+static SHELL_VAR *
+assign_array_element_internal (entry, name, vname, sub, sublen, value, flags)
+ SHELL_VAR *entry;
+ char *name; /* only used for error messages */
+ char *vname;
+ char *sub;
+ int sublen;
+ char *value;
+ int flags;
+{
+ char *akey;
+ arrayind_t ind;
if (entry && assoc_p (entry))
{
sub[sublen-1] = ']';
if (akey == 0 || *akey == 0)
{
- free (vname);
err_badarraysub (name);
+ FREE (akey);
return ((SHELL_VAR *)NULL);
}
entry = bind_assoc_variable (entry, vname, akey, value, flags);
}
else
{
- ind = array_expand_index (sub, sublen);
+ ind = array_expand_index (entry, sub, sublen);
+ /* negative subscripts to indexed arrays count back from end */
+ if (entry && ind < 0)
+ ind = (array_p (entry) ? array_max_index (array_cell (entry)) : 0) + 1 + ind;
if (ind < 0)
{
- free (vname);
err_badarraysub (name);
return ((SHELL_VAR *)NULL);
}
entry = bind_array_variable (vname, ind, value, flags);
}
- free (vname);
return (entry);
}
SHELL_VAR *var;
var = find_variable (name);
+ if (var == 0)
+ {
+ /* See if we have a nameref pointing to a variable that hasn't been
+ created yet. */
+ var = find_variable_last_nameref (name);
+ if (var && nameref_p (var))
+ var = (flags & 2) ? make_new_assoc_variable (nameref_cell (var)) : make_new_array_variable (nameref_cell (var));
+ }
if (var == 0)
var = (flags & 2) ? make_new_assoc_variable (name) : make_new_array_variable (name);
}
else if ((flags & 2) && array_p (var))
{
+ last_command_exit_value = 1;
report_error (_("%s: cannot convert indexed to associative array"), name);
return ((SHELL_VAR *)NULL);
}
int flags;
{
WORD_LIST *list, *nlist;
+ WORD_LIST *hd, *tl, *t, *n;
char *val;
int ni;
- /* I don't believe this condition is ever true any more. */
+ /* This condition is true when invoked from the declare builtin with a
+ command like
+ declare -a d='([1]="" [2]="bdef" [5]="hello world" "test")' */
if (*value == '(') /*)*/
{
ni = 1;
(ksh93 seems to do this). */
list = parse_string_to_word_list (val, 1, "array assign");
+ if (var && assoc_p (var))
+ {
+ if (val != value)
+ free (val);
+ return list;
+ }
+
/* If we're using [subscript]=value, we need to quote each [ and ] to
- prevent unwanted filename expansion. */
+ prevent unwanted filename expansion. This doesn't need to be done
+ for associative array expansion, since that uses a different expansion
+ function (see assign_compound_array_list below). */
if (list)
quote_array_assignment_chars (list);
return nlist;
}
+/* Callers ensure that VAR is not NULL */
void
assign_compound_array_list (var, nlist, flags)
SHELL_VAR *var;
HASH_TABLE *h;
WORD_LIST *list;
char *w, *val, *nval;
- int len, iflags;
+ int len, iflags, free_val;
arrayind_t ind, last_ind;
char *akey;
value. */
if ((flags & ASS_APPEND) == 0)
{
- if (array_p (var) && a)
+ if (a && array_p (var))
array_flush (a);
- else if (assoc_p (var) && h)
+ else if (h && assoc_p (var))
assoc_flush (h);
}
/* We have a word of the form [ind]=value */
if ((list->word->flags & W_ASSIGNMENT) && w[0] == '[')
{
- len = skipsubscript (w, 0);
+ /* Don't have to handle embedded quotes specially any more, since
+ associative array subscripts have not been expanded yet (see
+ above). */
+ len = skipsubscript (w, 0, 0);
/* XXX - changes for `+=' */
if (w[len] != ']' || (w[len+1] != '=' && (w[len+1] != '+' || w[len+2] != '=')))
if (ALL_ELEMENT_SUB (w[1]) && len == 2)
{
+ last_command_exit_value = 1;
if (assoc_p (var))
report_error (_("%s: invalid associative array key"), w);
else
if (array_p (var))
{
- ind = array_expand_index (w + 1, len);
+ ind = array_expand_index (var, w + 1, len);
+ /* negative subscripts to indexed arrays count back from end */
+ if (ind < 0)
+ ind = array_max_index (array_cell (var)) + 1 + ind;
if (ind < 0)
{
err_badarraysub (w);
}
else if (assoc_p (var))
{
- akey = substring (w, 1, len);
+ /* This is not performed above, see expand_compound_array_assignment */
+ w[len] = '\0'; /*[*/
+ akey = expand_assignment_string_to_string (w+1, 0);
+ w[len] = ']';
+ /* And we need to expand the value also, see below */
if (akey == 0 || *akey == 0)
{
err_badarraysub (w);
+ FREE (akey);
continue;
}
}
val = w + len + 3;
}
else
- val = w + len + 2;
+ val = w + len + 2;
}
else if (assoc_p (var))
{
+ last_command_exit_value = 1;
report_error (_("%s: %s: must use subscript when assigning associative array"), var->name, w);
continue;
}
val = w;
}
+ free_val = 0;
+ /* See above; we need to expand the value here */
+ if (assoc_p (var))
+ {
+ val = expand_assignment_string_to_string (val, 0);
+ free_val = 1;
+ }
+
if (integer_p (var))
this_command_name = (char *)NULL; /* no command name for errors */
bind_array_var_internal (var, ind, akey, val, iflags);
last_ind++;
+
+ if (free_val)
+ free (val);
}
}
{
size_t slen;
int saw_eq;
- char *temp, *t;
+ char *temp, *t, *subs;
const char *s, *send;
+ int ss, se;
DECLARE_MBSTATE;
slen = strlen (string);
{
if (*s == '=')
saw_eq = 1;
+ if (saw_eq == 0 && *s == '[') /* looks like a subscript */
+ {
+ ss = s - string;
+ se = skipsubscript (string, ss, 0);
+ subs = substring (s, ss, se);
+ *t++ = '\\';
+ strcpy (t, subs);
+ t += se - ss;
+ *t++ = '\\';
+ *t++ = ']';
+ s += se + 1;
+ free (subs);
+ continue;
+ }
if (saw_eq == 0 && (glob_char_p (s) || isifs (*s)))
*t++ = '\\';
{
if (l->word == 0 || l->word->word == 0 || l->word->word[0] == '\0')
continue; /* should not happen, but just in case... */
- /* Don't bother if it doesn't look like [ind]=value */
- if (l->word->word[0] != '[' || xstrchr (l->word->word, '=') == 0) /* ] */
+ /* Don't bother if it hasn't been recognized as an assignment or
+ doesn't look like [ind]=value */
+ if ((l->word->flags & W_ASSIGNMENT) == 0)
+ continue;
+ if (l->word->word[0] != '[' || mbschr (l->word->word, '=') == 0) /* ] */
continue;
+
nword = quote_assign (l->word->word);
free (l->word->word);
l->word->word = nword;
+ l->word->flags |= W_NOGLOB; /* XXX - W_NOSPLIT also? */
}
}
char *akey;
ARRAY_ELEMENT *ae;
- len = skipsubscript (sub, 0);
+ len = skipsubscript (sub, 0, 0);
if (sub[len] != ']' || len == 0)
{
builtin_error ("%s[%s: %s", var->name, sub, _(bash_badsub_errmsg));
if (akey == 0 || *akey == 0)
{
builtin_error ("[%s]: %s", sub, _(bash_badsub_errmsg));
+ FREE (akey);
return -1;
}
assoc_remove (assoc_cell (var), akey);
+ free (akey);
}
else
{
- ind = array_expand_index (sub, len+1);
+ ind = array_expand_index (var, sub, len+1);
+ /* negative subscripts to indexed arrays count back from end */
+ if (ind < 0)
+ ind = array_max_index (array_cell (var)) + 1 + ind;
if (ind < 0)
{
builtin_error ("[%s]: %s", sub, _(bash_badsub_errmsg));
char *t;
int r, len;
- t = xstrchr (name, '['); /* ] */
+ t = mbschr (name, '['); /* ] */
if (t)
{
*t = '\0';
if (r == 0)
return 0;
/* Check for a properly-terminated non-blank subscript. */
- len = skipsubscript (t, 0);
+ len = skipsubscript (t, 0, 0);
if (t[len] != ']' || len == 1)
return 0;
for (r = 1; r < len; r++)
/* Expand the array index beginning at S and extending LEN characters. */
arrayind_t
-array_expand_index (s, len)
+array_expand_index (var, s, len)
+ SHELL_VAR *var;
char *s;
int len;
{
char *t, *ret;
int ind, ni;
- t = xstrchr (s, '[');
+ t = mbschr (s, '[');
if (t == 0)
{
if (subp)
return ((char *)NULL);
}
ind = t - s;
- ni = skipsubscript (s, ind);
+ ni = skipsubscript (s, ind, 0);
if (ni <= ind + 1 || s[ni] != ']')
{
err_badarraysub (s);
var = find_variable (t);
free (t);
+#if 0
return (var == 0 || invisible_p (var)) ? (SHELL_VAR *)0 : var;
+#else
+ return var; /* now return invisible variables; caller must handle */
+#endif
}
+#define INDEX_ERROR() \
+ do \
+ { \
+ if (var) \
+ err_badarraysub (var->name); \
+ else \
+ { \
+ t[-1] = '\0'; \
+ err_badarraysub (s); \
+ t[-1] = '['; /* ] */\
+ } \
+ return ((char *)NULL); \
+ } \
+ while (0)
+
/* Return a string containing the elements in the array and subscript
described by S. If the subscript is * or @, obeys quoting rules akin
to the expansion of $* and $@ including double quoting. If RTYPE
is non-null it gets 1 if the array reference is name[*], 2 if the
reference is name[@], and 0 otherwise. */
static char *
-array_value_internal (s, quoted, allow_all, rtype)
+array_value_internal (s, quoted, flags, rtype, indp)
char *s;
- int quoted, allow_all, *rtype;
+ int quoted, flags, *rtype;
+ arrayind_t *indp;
{
int len;
arrayind_t ind;
return ((char *)NULL); /* error message already printed */
/* [ */
+ akey = 0;
if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
{
if (rtype)
*rtype = (t[0] == '*') ? 1 : 2;
- if (allow_all == 0)
+ if ((flags & AV_ALLOWALL) == 0)
{
err_badarraysub (s);
return ((char *)NULL);
}
- else if (var == 0 || value_cell (var) == 0)
+ else if (var == 0 || value_cell (var) == 0) /* XXX - check for invisible_p(var) ? */
return ((char *)NULL);
else if (array_p (var) == 0 && assoc_p (var) == 0)
l = add_string_to_list (value_cell (var), (WORD_LIST *)NULL);
if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
{
temp = string_list_dollar_star (l);
- retval = quote_string (temp);
+ retval = quote_string (temp); /* XXX - leak here */
free (temp);
}
else /* ${name[@]} or unquoted ${name[*]} */
- retval = string_list_dollar_at (l, quoted);
+ retval = string_list_dollar_at (l, quoted); /* XXX - leak here */
dispose_words (l);
}
*rtype = 0;
if (var == 0 || array_p (var) || assoc_p (var) == 0)
{
- ind = array_expand_index (t, len);
- if (ind < 0)
+ if ((flags & AV_USEIND) == 0 || indp == 0)
{
-index_error:
- if (var)
- err_badarraysub (var->name);
- else
+ ind = array_expand_index (var, t, len);
+ if (ind < 0)
{
- t[-1] = '\0';
- err_badarraysub (s);
- t[-1] = '['; /* ] */
+ /* negative subscripts to indexed arrays count back from end */
+ if (var && array_p (var))
+ ind = array_max_index (array_cell (var)) + 1 + ind;
+ if (ind < 0)
+ INDEX_ERROR();
}
- return ((char *)NULL);
+ if (indp)
+ *indp = ind;
}
+ else if (indp)
+ ind = *indp;
}
else if (assoc_p (var))
{
akey = expand_assignment_string_to_string (t, 0); /* [ */
t[len - 1] = ']';
if (akey == 0 || *akey == 0)
- goto index_error;
+ {
+ FREE (akey);
+ INDEX_ERROR();
+ }
}
- if (var == 0)
- return ((char *)NULL);
+ if (var == 0 || value_cell (var) == 0) /* XXX - check invisible_p(var) ? */
+ {
+ FREE (akey);
+ return ((char *)NULL);
+ }
if (array_p (var) == 0 && assoc_p (var) == 0)
return (ind == 0 ? value_cell (var) : (char *)NULL);
else if (assoc_p (var))
- retval = assoc_reference (assoc_cell (var), akey);
+ {
+ retval = assoc_reference (assoc_cell (var), akey);
+ free (akey);
+ }
else
retval = array_reference (array_cell (var), ind);
}
/* Return a string containing the elements described by the array and
subscript contained in S, obeying quoting for subscripts * and @. */
char *
-array_value (s, quoted, rtype)
+array_value (s, quoted, flags, rtype, indp)
char *s;
- int quoted, *rtype;
+ int quoted, flags, *rtype;
+ arrayind_t *indp;
{
- return (array_value_internal (s, quoted, 1, rtype));
+ return (array_value_internal (s, quoted, flags|AV_ALLOWALL, rtype, indp));
}
/* Return the value of the array indexing expression S as a single string.
- If ALLOW_ALL is 0, do not allow `@' and `*' subscripts. This is used
- by other parts of the shell such as the arithmetic expression evaluator
- in expr.c. */
+ If (FLAGS & AV_ALLOWALL) is 0, do not allow `@' and `*' subscripts. This
+ is used by other parts of the shell such as the arithmetic expression
+ evaluator in expr.c. */
char *
-get_array_value (s, allow_all, rtype)
+get_array_value (s, flags, rtype, indp)
char *s;
- int allow_all, *rtype;
+ int flags, *rtype;
+ arrayind_t *indp;
{
- return (array_value_internal (s, 0, allow_all, rtype));
+ return (array_value_internal (s, 0, flags, rtype, indp));
}
char *
if (var == 0 || ALL_ELEMENT_SUB (t[0]) == 0 || t[1] != ']')
return (char *)NULL;
+ if (var_isset (var) == 0 || invisible_p (var))
+ return (char *)NULL;
+
if (array_p (var) == 0 && assoc_p (var) == 0)
l = add_string_to_list ("0", (WORD_LIST *)NULL);
else if (assoc_p (var))