Skip to content

Commit

Permalink
Skip initial expansion of valid array references in unset
Browse files Browse the repository at this point in the history
  • Loading branch information
konsolebox committed Apr 19, 2021
1 parent f3a35a2 commit 575ddfa
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
7 changes: 3 additions & 4 deletions builtins/set.def
Expand Up @@ -872,10 +872,6 @@ unset_builtin (list)
else if (unset_function && nameref)
nameref = 0;

#if defined (ARRAY_VARS)
vflags = assoc_expand_once ? (VA_NOEXPAND|VA_ONEWORD) : 0;
#endif

while (list)
{
SHELL_VAR *var;
Expand All @@ -890,6 +886,9 @@ unset_builtin (list)
unset_variable = global_unset_var;

#if defined (ARRAY_VARS)
vflags = (assoc_expand_once && (list->word->flags & W_NOEXPAND) == 0) ?
(VA_NOEXPAND|VA_ONEWORD) : 0;

unset_array = 0;
/* XXX valid array reference second arg was 0 */
if (!unset_function && nameref == 0 && valid_array_reference (name, vflags))
Expand Down
1 change: 1 addition & 0 deletions command.h
Expand Up @@ -104,6 +104,7 @@ enum command_type { cm_for, cm_case, cm_while, cm_if, cm_simple, cm_select,
#define W_CHKLOCAL (1 << 28) /* check for local vars on assignment */
#define W_NOASSNTILDE (1 << 29) /* don't do tilde expansion like an assignment statement */
#define W_FORCELOCAL (1 << 30) /* force assignments to be to local variables, non-fatal on assignment errors */
#define W_NOEXPAND (1 << 31) /* inhibits any form of expansion */

/* Flags for the `pflags' argument to param_expand() and various
parameter_brace_expand_xxx functions; also used for string_list_dollar_at */
Expand Down
1 change: 1 addition & 0 deletions parser.h
Expand Up @@ -48,6 +48,7 @@
#define PST_REDIRLIST 0x080000 /* parsing a list of redirections preceding a simple command name */
#define PST_COMMENT 0x100000 /* parsing a shell comment; used by aliases */
#define PST_ENDALIAS 0x200000 /* just finished expanding and consuming an alias */
#define PST_UNSET 0x400000

/* Definition of the delimiter stack. Needed by parse.y and bashhist.c. */
struct dstack {
Expand Down
14 changes: 12 additions & 2 deletions subst.c
Expand Up @@ -4378,6 +4378,8 @@ dequote_list (list)

for (tlist = list; tlist; tlist = tlist->next)
{
if (tlist->word->flags & W_NOEXPAND)
continue;
s = dequote_string (tlist->word->word);
if (QUOTED_NULL (tlist->word->word))
tlist->word->flags &= ~W_HASQUOTEDNULL;
Expand Down Expand Up @@ -11391,9 +11393,11 @@ glob_expand_word_list (tlist, eflags)
words are freed. */
next = tlist->next;

if (tlist->word->flags & W_NOEXPAND)
PREPEND_LIST (tlist, output_list);
/* If the word isn't an assignment and contains an unquoted
pattern matching character, then glob it. */
if ((tlist->word->flags & W_NOGLOB) == 0 &&
else if ((tlist->word->flags & W_NOGLOB) == 0 &&
unquoted_glob_pattern_p (tlist->word->word))
{
glob_array = shell_glob_filename (tlist->word->word, QGLOB_CTLESC); /* XXX */
Expand Down Expand Up @@ -11487,7 +11491,7 @@ brace_expand_word_list (tlist, eflags)
{
next = tlist->next;

if (tlist->word->flags & W_NOBRACE)
if (tlist->word->flags & (W_NOBRACE|W_NOEXPAND))
{
/*itrace("brace_expand_word_list: %s: W_NOBRACE", tlist->word->word);*/
PREPEND_LIST (tlist, output_list);
Expand Down Expand Up @@ -11863,6 +11867,12 @@ shell_expand_word_list (tlist, eflags)

next = tlist->next;

if (tlist->word->flags & W_NOEXPAND)
{
new_list = make_word_list (copy_word (tlist->word), new_list);
continue;
}

#if defined (ARRAY_VARS)
/* If this is a compound array assignment to a builtin that accepts
such assignments (e.g., `declare'), take the assignment and perform
Expand Down
8 changes: 8 additions & 0 deletions y.tab.c
Expand Up @@ -7700,7 +7700,15 @@ read_token_word (character)
parser_state |= PST_ASSIGNOK;
else if (STREQ (token, "eval") || STREQ (token, "let"))
parser_state |= PST_ASSIGNOK;
#if defined (ARRAY_VARS)
else if (STREQ (token, "unset"))
parser_state |= PST_UNSET;
#endif
}
#if defined (ARRAY_VARS)
else if (parser_state & PST_UNSET && valid_array_reference (token, 0))
the_word->flags |= W_NOEXPAND;
#endif

yylval.word = the_word;

Expand Down

0 comments on commit 575ddfa

Please sign in to comment.