aboutsummaryrefslogtreecommitdiff
path: root/shell
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-07-25 15:18:57 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2017-07-25 15:18:57 +0200
commit645c697372b714f1293a37a185aa62965f600479 (patch)
tree5033362b96737f83526b776818bf4c9a4ef8a9ef /shell
parent64925384c9cf5e0d986e183577da286bb3207ce7 (diff)
downloadbusybox-w32-645c697372b714f1293a37a185aa62965f600479.tar.gz
busybox-w32-645c697372b714f1293a37a185aa62965f600479.tar.bz2
busybox-w32-645c697372b714f1293a37a185aa62965f600479.zip
hush: treat ${#?} as "length of $?"
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell')
-rw-r--r--shell/ash_test/ash-vars/param_expand_indicate_error.right43
-rwxr-xr-xshell/ash_test/ash-vars/param_expand_indicate_error.tests61
-rw-r--r--shell/hush.c12
-rw-r--r--shell/hush_test/hush-vars/param_expand_indicate_error.right2
-rwxr-xr-xshell/hush_test/hush-vars/param_expand_indicate_error.tests2
5 files changed, 115 insertions, 5 deletions
diff --git a/shell/ash_test/ash-vars/param_expand_indicate_error.right b/shell/ash_test/ash-vars/param_expand_indicate_error.right
new file mode 100644
index 000000000..33afacee0
--- /dev/null
+++ b/shell/ash_test/ash-vars/param_expand_indicate_error.right
@@ -0,0 +1,43 @@
1SHELL: line 1: syntax error: bad substitution
21
30
4====
5_
6SHELL: line 1: 1: parameter not set
7SHELL: line 1: 1: parameter not set or null
8SHELL: line 1: 1: message1
9SHELL: line 1: 1: message1
10SHELL: line 1: 1: unset!
11SHELL: line 1: 1: null or unset!
12====
13_aaaa
14_aaaa
15_aaaa
16_aaaa
17_aaaa
18_aaaa
19_aaaa
20====
21_
22SHELL: line 1: f: parameter not set
23SHELL: line 1: f: parameter not set or null
24SHELL: line 1: f: message3
25SHELL: line 1: f: message3
26SHELL: line 1: f: unset!
27SHELL: line 1: f: null or unset!
28====
29_
30_
31SHELL: line 1: f: parameter not set or null
32_
33SHELL: line 1: f: message4
34_
35SHELL: line 1: f: null or unset!
36====
37_fff
38_fff
39_fff
40_fff
41_fff
42_fff
43_fff
diff --git a/shell/ash_test/ash-vars/param_expand_indicate_error.tests b/shell/ash_test/ash-vars/param_expand_indicate_error.tests
new file mode 100755
index 000000000..0f3061949
--- /dev/null
+++ b/shell/ash_test/ash-vars/param_expand_indicate_error.tests
@@ -0,0 +1,61 @@
1# do all of these in subshells since it's supposed to error out
2# (set argv0 to "SHELL" to avoid "/path/to/shell: blah" in error messages)
3
4# first try some invalid patterns
5#"$THIS_SH" -c 'echo ${?}' SHELL -- this is valid as it's the same as $?
6"$THIS_SH" -c 'echo ${:?}' SHELL
7
8# then some funky ones
9# note: bash prints 1 - treats it as "length of $#"
10"$THIS_SH" -c 'echo ${#?}' SHELL
11# bash prints 0
12"$THIS_SH" -c 'echo ${#:?}' SHELL
13
14# now some valid ones
15export msg_unset="unset!"
16export msg_null_or_unset="null or unset!"
17
18echo ====
19"$THIS_SH" -c 'set --; echo _$1' SHELL
20"$THIS_SH" -c 'set --; echo _${1?}' SHELL
21"$THIS_SH" -c 'set --; echo _${1:?}' SHELL
22"$THIS_SH" -c 'set --; echo _${1?message1}' SHELL
23"$THIS_SH" -c 'set --; echo _${1:?message1}' SHELL
24"$THIS_SH" -c 'set --; echo _${1?$msg_unset}' SHELL
25"$THIS_SH" -c 'set --; echo _${1:?$msg_null_or_unset}' SHELL
26
27echo ====
28"$THIS_SH" -c 'set -- aaaa; echo _$1' SHELL
29"$THIS_SH" -c 'set -- aaaa; echo _${1?}' SHELL
30"$THIS_SH" -c 'set -- aaaa; echo _${1:?}' SHELL
31"$THIS_SH" -c 'set -- aaaa; echo _${1?word}' SHELL
32"$THIS_SH" -c 'set -- aaaa; echo _${1:?word}' SHELL
33"$THIS_SH" -c 'set -- aaaa; echo _${1?$msg_unset}' SHELL
34"$THIS_SH" -c 'set -- aaaa; echo _${1:?$msg_null_or_unset}' SHELL
35
36echo ====
37"$THIS_SH" -c 'unset f; echo _$f' SHELL
38"$THIS_SH" -c 'unset f; echo _${f?}' SHELL
39"$THIS_SH" -c 'unset f; echo _${f:?}' SHELL
40"$THIS_SH" -c 'unset f; echo _${f?message3}' SHELL
41"$THIS_SH" -c 'unset f; echo _${f:?message3}' SHELL
42"$THIS_SH" -c 'unset f; echo _${f?$msg_unset}' SHELL
43"$THIS_SH" -c 'unset f; echo _${f:?$msg_null_or_unset}' SHELL
44
45echo ====
46"$THIS_SH" -c 'f=; echo _$f' SHELL
47"$THIS_SH" -c 'f=; echo _${f?}' SHELL
48"$THIS_SH" -c 'f=; echo _${f:?}' SHELL
49"$THIS_SH" -c 'f=; echo _${f?word}' SHELL
50"$THIS_SH" -c 'f=; echo _${f:?message4}' SHELL
51"$THIS_SH" -c 'f=; echo _${f?$msg_unset}' SHELL
52"$THIS_SH" -c 'f=; echo _${f:?$msg_null_or_unset}' SHELL
53
54echo ====
55"$THIS_SH" -c 'f=fff; echo _$f' SHELL
56"$THIS_SH" -c 'f=fff; echo _${f?}' SHELL
57"$THIS_SH" -c 'f=fff; echo _${f:?}' SHELL
58"$THIS_SH" -c 'f=fff; echo _${f?word}' SHELL
59"$THIS_SH" -c 'f=fff; echo _${f:?word}' SHELL
60"$THIS_SH" -c 'f=fff; echo _${f?$msg_unset}' SHELL
61"$THIS_SH" -c 'f=fff; echo _${f:?$msg_null_or_unset}' SHELL
diff --git a/shell/hush.c b/shell/hush.c
index 20b092398..f9dad074f 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -5559,8 +5559,10 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
5559 first_char = arg[0] = arg0 & 0x7f; 5559 first_char = arg[0] = arg0 & 0x7f;
5560 exp_op = 0; 5560 exp_op = 0;
5561 5561
5562 if (first_char == '#' /* ${#... */ 5562 if (first_char == '#' && arg[1] /* ${#... but not ${#} */
5563 && arg[1] && !exp_saveptr /* not ${#} and not ${#<op_char>...} */ 5563 && (!exp_saveptr /* and (not ${#<op_char>...} */
5564 || (arg[1] == '?' && arg[2] == '\0') /* or ${#?} - "len of $?") */
5565 )
5564 ) { 5566 ) {
5565 /* It must be length operator: ${#var} */ 5567 /* It must be length operator: ${#var} */
5566 var++; 5568 var++;
@@ -5797,7 +5799,11 @@ static NOINLINE const char *expand_one_var(char **to_be_freed_pp, char *arg, cha
5797 /* mimic bash message */ 5799 /* mimic bash message */
5798 die_if_script("%s: %s", 5800 die_if_script("%s: %s",
5799 var, 5801 var,
5800 exp_word[0] ? exp_word : "parameter null or not set" 5802 exp_word[0]
5803 ? exp_word
5804 : "parameter null or not set"
5805 /* ash has more specific messages, a-la: */
5806 /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/
5801 ); 5807 );
5802//TODO: how interactive bash aborts expansion mid-command? 5808//TODO: how interactive bash aborts expansion mid-command?
5803 } else { 5809 } else {
diff --git a/shell/hush_test/hush-vars/param_expand_indicate_error.right b/shell/hush_test/hush-vars/param_expand_indicate_error.right
index 06fcc5104..acf293893 100644
--- a/shell/hush_test/hush-vars/param_expand_indicate_error.right
+++ b/shell/hush_test/hush-vars/param_expand_indicate_error.right
@@ -1,5 +1,5 @@
1hush: syntax error: unterminated ${name} 1hush: syntax error: unterminated ${name}
20 21
30 30
4==== 4====
5_ 5_
diff --git a/shell/hush_test/hush-vars/param_expand_indicate_error.tests b/shell/hush_test/hush-vars/param_expand_indicate_error.tests
index be14b1e37..5f946e39a 100755
--- a/shell/hush_test/hush-vars/param_expand_indicate_error.tests
+++ b/shell/hush_test/hush-vars/param_expand_indicate_error.tests
@@ -5,7 +5,7 @@
5"$THIS_SH" -c 'echo ${:?}' 5"$THIS_SH" -c 'echo ${:?}'
6 6
7# then some funky ones 7# then some funky ones
8# note: bash prints 1 - treats it as "length of $#"? We print 0 8# note: bash prints 1 - treats it as "length of $#"
9"$THIS_SH" -c 'echo ${#?}' 9"$THIS_SH" -c 'echo ${#?}'
10# bash prints 0 10# bash prints 0
11"$THIS_SH" -c 'echo ${#:?}' 11"$THIS_SH" -c 'echo ${#:?}'