fix tilde expansion in associative array compound assignments

This commit is contained in:
Chet Ramey
2023-02-07 10:16:51 -05:00
parent 315095ad7f
commit 7411382644
10 changed files with 144 additions and 11 deletions
+16
View File
@@ -5251,3 +5251,19 @@ execute_cmd.c
print_cmd.c
- print_function_def,named_function_string: pass POSIXLY_CORRECT to
valid_function_name as the FLAGS argument
2/6
---
subst.c
- expand_subscript_string: don't set W_NOTILDE in td.flags so we
perform `normal' tilde expansion (expansion at the start of the
word) on the array subscript
- expand_oneword,expand_compound_assignment_word: change second arg to
ATYPE, which better reflects its use
arrayfunc.c
- assign_assoc_from_kvlist,assign_compound_array_list,expand_and_quote_assoc_word:
use expand_assignment_string_to_string instead of expand_subscript_string
for the rhs of the assignment or the second word in the kvpair to
be more consistent with how assignment statements behave (e.g.,
tilde expansion). Fixes issue reported by maroloccio@gmail.com
+3 -1
View File
@@ -942,6 +942,7 @@ tests/array27.sub f
tests/array28.sub f
tests/array29.sub f
tests/array30.sub f
tests/array31.sub f
tests/array-at-star f
tests/array2.right f
tests/assoc.tests f
@@ -964,7 +965,8 @@ tests/assoc15.sub f
tests/assoc16.sub f
tests/assoc17.sub f
tests/assoc18.sub f
tests/attr.tests f
tests/assoc19.sub f
`tests/attr.tests f
tests/attr.right f
tests/attr1.sub f
tests/attr2.sub f
+4 -3
View File
@@ -597,7 +597,7 @@ assign_assoc_from_kvlist (SHELL_VAR *var, WORD_LIST *nlist, HASH_TABLE *h, int f
continue;
}
aval = expand_subscript_string (v, 0);
aval = expand_assignment_string_to_string (v, 0);
if (aval == 0)
{
aval = (char *)xmalloc (1);
@@ -784,7 +784,7 @@ assign_compound_array_list (SHELL_VAR *var, WORD_LIST *nlist, int flags)
/* See above; we need to expand the value here */
if (assoc_p (var))
{
val = expand_subscript_string (val, 0);
val = expand_assignment_string_to_string (val, 0);
if (val == 0)
{
val = (char *)xmalloc (1);
@@ -964,7 +964,8 @@ expand_and_quote_assoc_word (char *w, int type)
nword[i++] = w[ind++];
nword[i++] = w[ind++];
t = expand_subscript_string (w+ind, 0);
t = expand_assignment_string_to_string (w+ind, 0);
s = (t && strchr (t, CTLESC)) ? quote_escapes (t) : t;
value = sh_single_quote (s ? s : "");
if (s != t)
+12 -7
View File
@@ -10518,7 +10518,11 @@ expand_subscript_string (const char *string, int quoted)
oe = expand_no_split_dollar_star;
ret = (char *)NULL;
#if 0
td.flags = W_NOPROCSUB|W_NOTILDE|W_NOSPLIT2; /* XXX - W_NOCOMSUB? */
#else
td.flags = W_NOPROCSUB|W_NOSPLIT2; /* XXX - W_NOCOMSUB? */
#endif
td.word = savestring (string); /* in case it's freed on error */
expand_no_split_dollar_star = 1;
@@ -10587,6 +10591,7 @@ expand_array_subscript (const char *string, int *sindex, int quoted, int flags)
exp = substring (string, si+1, ni);
t = expand_subscript_string (exp, quoted & ~(Q_ARITH|Q_DOUBLE_QUOTES));
free (exp);
/* XXX - use sh_single_quote here? */
exp = t ? sh_backslash_quote (t, abstab, 0) : savestring ("");
free (t);
@@ -12214,19 +12219,19 @@ make_internal_declare (char *word, char *option, char *cmd)
['expanded-ind']='expanded-value'. */
static WORD_LIST *
expand_oneword (char *value, int flags)
expand_oneword (char *value, int atype)
{
WORD_LIST *l, *nl;
char *t;
int kvpair;
if (flags == 0)
if (atype == 0)
{
/* Indexed array */
l = expand_compound_array_assignment ((SHELL_VAR *)NULL, value, flags);
l = expand_compound_array_assignment ((SHELL_VAR *)NULL, value, atype);
/* Now we quote the results of the expansion above to prevent double
expansion. */
quote_compound_array_list (l, flags);
quote_compound_array_list (l, atype);
return l;
}
else
@@ -12251,7 +12256,7 @@ expand_oneword (char *value, int flags)
if ((nl->word->flags & W_ASSIGNMENT) == 0)
t = sh_single_quote (nl->word->word ? nl->word->word : "");
else
t = expand_and_quote_assoc_word (nl->word->word, flags);
t = expand_and_quote_assoc_word (nl->word->word, atype);
free (nl->word->word);
nl->word->word = t;
}
@@ -12267,7 +12272,7 @@ expand_oneword (char *value, int flags)
handling, see expand_oneword() above. The return value is
NAME[+]=( expanded-and-quoted-VALUE ). */
static void
expand_compound_assignment_word (WORD_LIST *tlist, int flags)
expand_compound_assignment_word (WORD_LIST *tlist, int atype)
{
WORD_LIST *l;
int wlen, oind, t;
@@ -12281,7 +12286,7 @@ expand_compound_assignment_word (WORD_LIST *tlist, int flags)
value = extract_array_assignment_list (tlist->word->word + t + 1, &oind);
/* This performs one round of expansion on the index/key and value and
single-quotes each word in the result. */
l = expand_oneword (value, flags);
l = expand_oneword (value, atype);
free (value);
value = string_list (l);
+6
View File
@@ -787,3 +787,9 @@ FOO
declare -Au A=([Darwin]="FOO" )
FOO
declare -Au A=(["@"]="FOO" )
declare -a aa=([0]="/homes/cj/Desktop")
declare -a aa=([0]="/homes/cj/Desktop")
declare -a aa=([0]="/homes/cj/Desktop:/homes/cj/Library:/homes/cj/Documents")
declare -a aa=([0]="/homes/cj/Desktop:/homes/cj/Library:/homes/cj/Documents")
declare -a aa=([0]="/homes/cj/Desktop:/homes/cj/Library:/homes/cj/Documents" [1]="/homes/cj/Applications")
declare -a aa=([0]="/homes/cj/Desktop:/homes/cj/Library:/homes/cj/Documents" [1]="/homes/cj/Applications")
+1
View File
@@ -427,3 +427,4 @@ ${THIS_SH} ./array27.sub
${THIS_SH} ./array28.sub
${THIS_SH} ./array29.sub
${THIS_SH} ./array30.sub
${THIS_SH} ./array31.sub
+43
View File
@@ -0,0 +1,43 @@
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Tilde expansion in indexed array assignments; prep for future work
HOME=/homes/cj
declare -a aa
aa=([0]=~/Desktop)
declare -p aa
unset aa
declare -a aa=([0]=~/Desktop)
declare -p aa
unset aa
declare -a aa=([0]=~/Desktop:~/Library:~/Documents)
declare -p aa
unset aa
declare -a aa
aa=([0]=~/Desktop:~/Library:~/Documents)
declare -p aa
unset aa
declare -a aa=([0]=~/Desktop:~/Library:~/Documents [1]=~/Applications)
declare -p aa
unset aa
declare -a aa
aa=([0]=~/Desktop:~/Library:~/Documents [1]=~/Applications)
declare -p aa
unset aa
+7
View File
@@ -398,3 +398,10 @@ declare -A A=()
declare -A A=(["]"]="rbracket" ["["]="lbracket" )
declare -A A=()
5: ok 1
declare -A aa=([key]="/homes/cj/Desktop" )
declare -A aa=([key]="/homes/cj/Desktop" )
declare -A aa=([k2]="/homes/cj/Library" [key]="/homes/cj/Desktop" )
declare -A aa=([/homes/cj/key]="/homes/cj/Desktop" )
declare -A aa=([/homes/cj/Documents]="/homes/cj/Library" [/homes/cj/key]="/homes/cj/Desktop" )
declare -A aa=([/homes/cj/Documents]="/homes/cj/Library" [/homes/cj/key]="/homes/cj/Desktop:/homes/cj/Documents:/homes/cj/Applications" )
declare -A aa=([/homes/cj/Documents]="/homes/cj/Library" [/homes/cj/key]="/homes/cj/Desktop:/homes/cj/Documents:/homes/cj/Applications" )
+2
View File
@@ -266,3 +266,5 @@ ${THIS_SH} ./assoc17.sub
# tests with `[' and `]' subscripts and printf/read/wait builtins
${THIS_SH} ./assoc18.sub
# tests with tilde expansion in keys and values post-bash-5.2
${THIS_SH} ./assoc19.sub
+50
View File
@@ -0,0 +1,50 @@
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Problems with tilde expansion of keys and values in bash-5.2
HOME=/homes/cj
declare -A aa=([key]=~/Desktop)
declare -p aa
unset aa
declare -A aa
aa=([key]=~/Desktop)
declare -p aa
unset aa
declare -A aa=([key]=~/Desktop [k2]=~/Library )
declare -p aa
unset aa
declare -A aa=([~/key]=~/Desktop)
declare -p aa
unset aa
declare -A aa
aa=([~/key]=~/Desktop)
aa[~/Documents]=~/Library
declare -p aa
unset aa
declare -A aa
aa=([~/key]=~/Desktop:~/Documents:~/Applications)
aa[~/Documents]=~/Library
declare -p aa
unset aa
declare -A aa=([~/key]=~/Desktop:~/Documents:~/Applications [~/Documents]=~/Library)
declare -p aa