diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-07-06 19:48:20 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-07-06 19:48:20 +0200 |
commit | 5dad7bdc3bdad8e9934d45301d5a8c51e843cd7b (patch) | |
tree | db493d6ca6cabddcfe3e9a58f33ee712bd1584d4 | |
parent | 3234045d07c3fb2a9ef8afd02f821158317adbd3 (diff) | |
download | busybox-w32-5dad7bdc3bdad8e9934d45301d5a8c51e843cd7b.tar.gz busybox-w32-5dad7bdc3bdad8e9934d45301d5a8c51e843cd7b.tar.bz2 busybox-w32-5dad7bdc3bdad8e9934d45301d5a8c51e843cd7b.zip |
hush: implement negative start in the ${v: -n[:m]} idiom
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/ash_test/ash-vars/var_bash6.right | 5 | ||||
-rwxr-xr-x | shell/ash_test/ash-vars/var_bash6.tests | 9 | ||||
-rw-r--r-- | shell/hush.c | 8 | ||||
-rw-r--r-- | shell/hush_test/hush-vars/var_bash1a.right | 6 | ||||
-rwxr-xr-x | shell/hush_test/hush-vars/var_bash1a.tests | 11 |
5 files changed, 37 insertions, 2 deletions
diff --git a/shell/ash_test/ash-vars/var_bash6.right b/shell/ash_test/ash-vars/var_bash6.right new file mode 100644 index 000000000..63fc23df8 --- /dev/null +++ b/shell/ash_test/ash-vars/var_bash6.right | |||
@@ -0,0 +1,5 @@ | |||
1 | Expected Actual | ||
2 | a*z : a*z | ||
3 | \z : \z | ||
4 | a1z a2z: a1z a2z | ||
5 | z : z | ||
diff --git a/shell/ash_test/ash-vars/var_bash6.tests b/shell/ash_test/ash-vars/var_bash6.tests new file mode 100755 index 000000000..cf2e4f020 --- /dev/null +++ b/shell/ash_test/ash-vars/var_bash6.tests | |||
@@ -0,0 +1,9 @@ | |||
1 | # This testcase checks globbing correctness in ${v/a/b} | ||
2 | |||
3 | >a1z; >a2z; | ||
4 | echo 'Expected' 'Actual' | ||
5 | v='a bz'; echo 'a*z :' "${v/a*z/a*z}" | ||
6 | v='a bz'; echo '\z :' "${v/a*z/\z}" | ||
7 | v='a bz'; echo 'a1z a2z:' ${v/a*z/a*z} | ||
8 | v='a bz'; echo 'z :' ${v/a*z/\z} | ||
9 | rm a1z a2z | ||
diff --git a/shell/hush.c b/shell/hush.c index 64b33cf1c..f6b50dec6 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -5619,8 +5619,12 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha | |||
5619 | goto arith_err; | 5619 | goto arith_err; |
5620 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); | 5620 | debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len); |
5621 | if (len >= 0) { /* bash compat: len < 0 is illegal */ | 5621 | if (len >= 0) { /* bash compat: len < 0 is illegal */ |
5622 | if (beg < 0) /* bash compat */ | 5622 | if (beg < 0) { |
5623 | beg = 0; | 5623 | /* negative beg counts from the end */ |
5624 | beg = (arith_t)strlen(val) + beg; | ||
5625 | if (beg < 0) /* ${v: -999999} is "" */ | ||
5626 | beg = len = 0; | ||
5627 | } | ||
5624 | debug_printf_varexp("from val:'%s'\n", val); | 5628 | debug_printf_varexp("from val:'%s'\n", val); |
5625 | if (len == 0 || !val || beg >= strlen(val)) { | 5629 | if (len == 0 || !val || beg >= strlen(val)) { |
5626 | arith_err: | 5630 | arith_err: |
diff --git a/shell/hush_test/hush-vars/var_bash1a.right b/shell/hush_test/hush-vars/var_bash1a.right new file mode 100644 index 000000000..1965b5c6c --- /dev/null +++ b/shell/hush_test/hush-vars/var_bash1a.right | |||
@@ -0,0 +1,6 @@ | |||
1 | parameter 'abcdef' | ||
2 | varoffset2 'cdef' | ||
3 | varoffset-2 'ef' | ||
4 | literal '2' 'cdef' | ||
5 | literal '-2' 'abcdef' | ||
6 | literal ' -2' 'ef' | ||
diff --git a/shell/hush_test/hush-vars/var_bash1a.tests b/shell/hush_test/hush-vars/var_bash1a.tests new file mode 100755 index 000000000..551dd9acc --- /dev/null +++ b/shell/hush_test/hush-vars/var_bash1a.tests | |||
@@ -0,0 +1,11 @@ | |||
1 | parameter=abcdef | ||
2 | offset=2 | ||
3 | noffset=-2 | ||
4 | echo "parameter '${parameter}'" | ||
5 | echo "varoffset2 '${parameter:${offset}}'" | ||
6 | echo "varoffset-2 '${parameter:${noffset}}'" | ||
7 | echo "literal '2' '${parameter:2}'" | ||
8 | # This is not inrpreted as ${VAR:POS{:LEN}}, | ||
9 | # but as ${VAR:=WORD} - if VAR is unset or null, substitute WORD | ||
10 | echo "literal '-2' '${parameter:-2}'" | ||
11 | echo "literal ' -2' '${parameter: -2}'" | ||