-/* Perform a compound assignment statement for array NAME, where VALUE is
- the text between the parens: NAME=( VALUE ) */
-SHELL_VAR *
-assign_array_from_string (name, value)
- char *name, *value;
-{
- SHELL_VAR *var;
-
- var = find_variable (name);
- if (var == 0)
- var = make_new_array_variable (name);
- else if (readonly_p (var) || noassign_p (var))
- {
- if (readonly_p (var))
- report_error ("%s: readonly variable", name);
- return ((SHELL_VAR *)NULL);
- }
- else if (array_p (var) == 0)
- var = convert_var_to_array (var);
-
- return (assign_array_var_from_string (var, value));
-}
-
-SHELL_VAR *
-assign_array_var_from_word_list (var, list)
- SHELL_VAR *var;
- WORD_LIST *list;
-{
- register int i;
- register WORD_LIST *l;
- ARRAY *a;
-
- for (a = array_cell (var), l = list, i = 0; l; l = l->next, i++)
- if (var->assign_func)
- (*var->assign_func) (var, i, l->word->word);
- else
- array_add_element (a, i, l->word->word);
- return var;
-}
-
-/* For each word in a compound array assignment, if the word looks like
- [ind]=value, quote the `[' and `]' before the `=' to protect them from
- unwanted filename expansion. */
-static void
-quote_array_assignment_chars (list)
- WORD_LIST *list;
-{
- char *s, *t, *nword;
- int saw_eq;
- WORD_LIST *l;
-
- for (l = list; l; l = l->next)
- {
- 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] != '[' || strchr (l->word->word, '=') == 0) /* ] */
- continue;
- s = nword = xmalloc (strlen (l->word->word) * 2 + 1);
- saw_eq = 0;
- for (t = l->word->word; *t; )
- {
- if (*t == '=')
- saw_eq = 1;
- if (saw_eq == 0 && (*t == '[' || *t == ']'))
- *s++ = '\\';
- *s++ = *t++;
- }
- *s = '\0';
- free (l->word->word);
- l->word->word = nword;
- }
-}
-
-/* Perform a compound array assignment: VAR->name=( VALUE ). The
- VALUE has already had the parentheses stripped. */
-SHELL_VAR *
-assign_array_var_from_string (var, value)
- SHELL_VAR *var;
- char *value;
-{
- ARRAY *a;
- WORD_LIST *list, *nlist;
- char *w, *val, *nval;
- int ni, len, ind, last_ind;
-
- if (value == 0)
- return var;
-
- /* If this is called from declare_builtin, value[0] == '(' and
- strchr(value, ')') != 0. In this case, we need to extract
- the value from between the parens before going on. */
- if (*value == '(') /*)*/
- {
- ni = 1;
- val = extract_array_assignment_list (value, &ni);
- if (val == 0)
- return var;
- }
- else
- val = value;
-
- /* Expand the value string into a list of words, performing all the
- shell expansions including pathname generation and word splitting. */
- /* First we split the string on whitespace, using the shell parser
- (ksh93 seems to do this). */
- list = parse_string_to_word_list (val, "array assign");
-
- /* If we're using [subscript]=value, we need to quote each [ and ] to
- prevent unwanted filename expansion. */
- if (list)
- quote_array_assignment_chars (list);
-
- /* Now that we've split it, perform the shell expansions on each
- word in the list. */
- nlist = list ? expand_words_no_vars (list) : (WORD_LIST *)NULL;
-
- dispose_words (list);
-
- if (val != value)
- free (val);
-
- a = array_cell (var);
-
- /* Now that we are ready to assign values to the array, kill the existing
- value. */
- if (a)
- empty_array (a);
-
- for (last_ind = 0, list = nlist; list; list = list->next)
- {
- w = list->word->word;
-
- /* We have a word of the form [ind]=value */
- if (w[0] == '[')
- {
- len = skipsubscript (w, 0);
-
- if (w[len] != ']' || w[len+1] != '=')
- {
- nval = make_variable_value (var, w);
- if (var->assign_func)
- (*var->assign_func) (var, last_ind, nval);
- else
- array_add_element (a, last_ind, nval);
- FREE (nval);
- last_ind++;
- continue;
- }
-
- if (len == 1)
- {
- report_error ("%s: bad array subscript", w);
- continue;
- }
-
- if (ALL_ELEMENT_SUB (w[1]) && len == 2)
- {
- report_error ("%s: cannot assign to non-numeric index", w);
- continue;
- }
-
- ind = array_expand_index (w + 1, len);
- if (ind < 0)
- {
- report_error ("%s: bad array subscript", w);
- continue;
- }
- last_ind = ind;
- val = w + len + 2;
- }
- else /* No [ind]=value, just a stray `=' */
- {
- ind = last_ind;
- val = w;
- }
-
- if (integer_p (var))
- this_command_name = (char *)NULL; /* no command name for errors */
- nval = make_variable_value (var, val);
- if (var->assign_func)
- (*var->assign_func) (var, ind, nval);
- else
- array_add_element (a, ind, nval);
- FREE (nval);
- last_ind++;
- }
-
- dispose_words (nlist);
- return (var);
-}
-#endif /* ARRAY_VARS */
-