Imported from ../bash-3.0.tar.gz.
This commit is contained in:
76
arrayfunc.c
76
arrayfunc.c
@@ -1,6 +1,6 @@
|
||||
/* arrayfunc.c -- High-level array functions used by other parts of the shell. */
|
||||
|
||||
/* Copyright (C) 2001-2002 Free Software Foundation, Inc.
|
||||
/* Copyright (C) 2001-2003 Free Software Foundation, Inc.
|
||||
|
||||
This file is part of GNU Bash, the Bourne Again SHell.
|
||||
|
||||
@@ -27,6 +27,8 @@
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
|
||||
#include "bashintl.h"
|
||||
|
||||
#include "shell.h"
|
||||
|
||||
#include "shmbutil.h"
|
||||
@@ -39,6 +41,9 @@ extern int last_command_exit_value;
|
||||
static void quote_array_assignment_chars __P((WORD_LIST *));
|
||||
static char *array_value_internal __P((char *, int, int, int *));
|
||||
|
||||
/* Standard error message to use when encountering an invalid array subscript */
|
||||
char *bash_badsub_errmsg = N_("bad array subscript");
|
||||
|
||||
/* **************************************************************** */
|
||||
/* */
|
||||
/* Functions to manipulate array variables and perform assignments */
|
||||
@@ -56,7 +61,8 @@ convert_var_to_array (var)
|
||||
|
||||
oldval = value_cell (var);
|
||||
array = array_create ();
|
||||
array_insert (array, 0, oldval);
|
||||
if (oldval)
|
||||
array_insert (array, 0, oldval);
|
||||
|
||||
FREE (value_cell (var));
|
||||
var_setarray (var, array);
|
||||
@@ -245,7 +251,7 @@ assign_array_var_from_string (var, value)
|
||||
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");
|
||||
list = parse_string_to_word_list (val, 1, "array assign");
|
||||
|
||||
/* If we're using [subscript]=value, we need to quote each [ and ] to
|
||||
prevent unwanted filename expansion. */
|
||||
@@ -273,7 +279,7 @@ assign_array_var_from_string (var, value)
|
||||
w = list->word->word;
|
||||
|
||||
/* We have a word of the form [ind]=value */
|
||||
if (w[0] == '[')
|
||||
if ((list->word->flags & W_ASSIGNMENT) && w[0] == '[')
|
||||
{
|
||||
len = skipsubscript (w, 0);
|
||||
|
||||
@@ -297,7 +303,7 @@ assign_array_var_from_string (var, value)
|
||||
|
||||
if (ALL_ELEMENT_SUB (w[1]) && len == 2)
|
||||
{
|
||||
report_error ("%s: cannot assign to non-numeric index", w);
|
||||
report_error (_("%s: cannot assign to non-numeric index"), w);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -395,13 +401,13 @@ skipsubscript (s, i)
|
||||
state_bak = state;
|
||||
mblength = mbrlen (s + i, slength, &state);
|
||||
|
||||
if (mblength == (size_t)-2 || mblength == (size_t)-1)
|
||||
if (MB_INVALIDCH (mblength))
|
||||
{
|
||||
state = state_bak;
|
||||
i++;
|
||||
slength--;
|
||||
}
|
||||
else if (mblength == 0)
|
||||
else if (MB_NULLWCH (mblength))
|
||||
return i;
|
||||
else
|
||||
{
|
||||
@@ -441,7 +447,7 @@ unbind_array_element (var, sub)
|
||||
len = skipsubscript (sub, 0);
|
||||
if (sub[len] != ']' || len == 0)
|
||||
{
|
||||
builtin_error ("%s[%s: bad array subscript", var->name, sub);
|
||||
builtin_error ("%s[%s: %s", var->name, sub, _(bash_badsub_errmsg));
|
||||
return -1;
|
||||
}
|
||||
sub[len] = '\0';
|
||||
@@ -454,7 +460,7 @@ unbind_array_element (var, sub)
|
||||
ind = array_expand_index (sub, len+1);
|
||||
if (ind < 0)
|
||||
{
|
||||
builtin_error ("[%s]: bad array subscript", sub);
|
||||
builtin_error ("[%s]: %s", sub, _(bash_badsub_errmsg));
|
||||
return -1;
|
||||
}
|
||||
ae = array_remove (array_cell (var), ind);
|
||||
@@ -557,12 +563,22 @@ array_variable_name (s, subp, lenp)
|
||||
|
||||
t = xstrchr (s, '[');
|
||||
if (t == 0)
|
||||
return ((char *)NULL);
|
||||
{
|
||||
if (subp)
|
||||
*subp = t;
|
||||
if (lenp)
|
||||
*lenp = 0;
|
||||
return ((char *)NULL);
|
||||
}
|
||||
ind = t - s;
|
||||
ni = skipsubscript (s, ind);
|
||||
if (ni <= ind + 1 || s[ni] != ']')
|
||||
{
|
||||
err_badarraysub (s);
|
||||
if (subp)
|
||||
*subp = t;
|
||||
if (lenp)
|
||||
*lenp = 0;
|
||||
return ((char *)NULL);
|
||||
}
|
||||
|
||||
@@ -623,6 +639,9 @@ array_value_internal (s, quoted, allow_all, rtype)
|
||||
return (char *)NULL;
|
||||
#endif
|
||||
|
||||
if (len == 0)
|
||||
return ((char *)NULL); /* error message already printed */
|
||||
|
||||
/* [ */
|
||||
if (ALL_ELEMENT_SUB (t[0]) && t[1] == ']')
|
||||
{
|
||||
@@ -704,4 +723,41 @@ get_array_value (s, allow_all, rtype)
|
||||
return (array_value_internal (s, 0, allow_all, rtype));
|
||||
}
|
||||
|
||||
char *
|
||||
array_keys (s, quoted)
|
||||
char *s;
|
||||
int quoted;
|
||||
{
|
||||
int len;
|
||||
char *retval, *t, *temp;
|
||||
WORD_LIST *l;
|
||||
SHELL_VAR *var;
|
||||
|
||||
var = array_variable_part (s, &t, &len);
|
||||
|
||||
/* [ */
|
||||
if (var == 0 || ALL_ELEMENT_SUB (t[0]) == 0 || t[1] != ']')
|
||||
return (char *)NULL;
|
||||
|
||||
if (array_p (var) == 0)
|
||||
l = add_string_to_list ("0", (WORD_LIST *)NULL);
|
||||
else
|
||||
{
|
||||
l = array_keys_to_word_list (array_cell (var));
|
||||
if (l == (WORD_LIST *)NULL)
|
||||
return ((char *) NULL);
|
||||
}
|
||||
|
||||
if (t[0] == '*' && (quoted & (Q_HERE_DOCUMENT|Q_DOUBLE_QUOTES)))
|
||||
{
|
||||
temp = string_list_dollar_star (l);
|
||||
retval = quote_string (temp);
|
||||
free (temp);
|
||||
}
|
||||
else /* ${!name[@]} or unquoted ${!name[*]} */
|
||||
retval = string_list_dollar_at (l, quoted);
|
||||
|
||||
dispose_words (l);
|
||||
return retval;
|
||||
}
|
||||
#endif /* ARRAY_VARS */
|
||||
|
||||
Reference in New Issue
Block a user