diff options
| author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-05-22 00:26:06 +0200 |
|---|---|---|
| committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-05-22 00:26:06 +0200 |
| commit | a6ad397ea92cd9c53973243728d3e52640fe63ec (patch) | |
| tree | 058f34aaf8877c15a1c667efaa491267f046c7e7 /shell | |
| parent | 7436950a7516d1f4498285ccc81bf6d926f3af5e (diff) | |
| download | busybox-w32-a6ad397ea92cd9c53973243728d3e52640fe63ec.tar.gz busybox-w32-a6ad397ea92cd9c53973243728d3e52640fe63ec.tar.bz2 busybox-w32-a6ad397ea92cd9c53973243728d3e52640fe63ec.zip | |
hush: fix more obscure ${var%...} cases
function old new delta
add_till_closing_paren 313 359 +46
builtin_exit 48 47 -1
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell')
| -rw-r--r-- | shell/hush.c | 39 | ||||
| -rw-r--r-- | shell/hush_test/hush-arith/arith.right | 9 | ||||
| -rwxr-xr-x | shell/hush_test/hush-arith/arith.tests | 19 | ||||
| -rw-r--r-- | shell/hush_test/hush-vars/var_posix1.right | 1 | ||||
| -rwxr-xr-x | shell/hush_test/hush-vars/var_posix1.tests | 1 |
5 files changed, 45 insertions, 24 deletions
diff --git a/shell/hush.c b/shell/hush.c index a3df5edcd..32b90876f 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
| @@ -45,6 +45,8 @@ | |||
| 45 | * follow IFS rules more precisely, including update semantics | 45 | * follow IFS rules more precisely, including update semantics |
| 46 | * builtins mandated by standards we don't support: | 46 | * builtins mandated by standards we don't support: |
| 47 | * [un]alias, command, fc, getopts, newgrp, readonly, times | 47 | * [un]alias, command, fc, getopts, newgrp, readonly, times |
| 48 | * make complex ${var%...} constructs support optional | ||
| 49 | * make here documents optional | ||
| 48 | * | 50 | * |
| 49 | * Bash compat TODO: | 51 | * Bash compat TODO: |
| 50 | * redirection of stdout+stderr: &> and >& | 52 | * redirection of stdout+stderr: &> and >& |
| @@ -5887,36 +5889,36 @@ static void add_till_backquote(o_string *dest, struct in_str *input) | |||
| 5887 | * echo $(echo 'TEST)' BEST) TEST) BEST | 5889 | * echo $(echo 'TEST)' BEST) TEST) BEST |
| 5888 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST | 5890 | * echo $(echo \(\(TEST\) BEST) ((TEST) BEST |
| 5889 | * | 5891 | * |
| 5890 | * BUG: enter: echo $(( `printf '(\x28 1'` + `echo 2))` )) | 5892 | * Also adapted to eat ${var%...} constructs, since ... part |
| 5891 | * on the command line, press Enter. You get > prompt which is impossible | 5893 | * can contain arbitrary constructs, just like $(cmd). |
| 5892 | * to exit with ^C. | ||
| 5893 | */ | 5894 | */ |
| 5894 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 | 5895 | #define DOUBLE_CLOSE_CHAR_FLAG 0x80 |
| 5895 | static void add_till_closing_paren(o_string *dest, struct in_str *input, char end_ch) | 5896 | static void add_till_closing_paren(o_string *dest, struct in_str *input, char end_ch) |
| 5896 | { | 5897 | { |
| 5897 | int count = 0; | ||
| 5898 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; | 5898 | char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG; |
| 5899 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG-1); | 5899 | end_ch &= (DOUBLE_CLOSE_CHAR_FLAG-1); |
| 5900 | while (1) { | 5900 | while (1) { |
| 5901 | int ch = i_getch(input); | 5901 | int ch = i_getch(input); |
| 5902 | if (ch == EOF) { | 5902 | if (ch == EOF) { |
| 5903 | syntax_error_unterm_ch(')'); | 5903 | syntax_error_unterm_ch(end_ch); |
| 5904 | /*xfunc_die(); - redundant */ | 5904 | /*xfunc_die(); - redundant */ |
| 5905 | } | 5905 | } |
| 5906 | if (ch == '(' || ch == '{') | 5906 | if (ch == end_ch) { |
| 5907 | count++; | 5907 | if (!dbl) |
| 5908 | if (ch == ')' || ch == '}') { | 5908 | break; |
| 5909 | count--; | 5909 | /* we look for closing )) of $((EXPR)) */ |
| 5910 | if (count < 0 && ch == end_ch) { | 5910 | if (i_peek(input) == end_ch) { |
| 5911 | if (!dbl) | 5911 | i_getch(input); /* eat second ')' */ |
| 5912 | break; | 5912 | break; |
| 5913 | if (i_peek(input) == ')') { | ||
| 5914 | i_getch(input); | ||
| 5915 | break; | ||
| 5916 | } | ||
| 5917 | } | 5913 | } |
| 5918 | } | 5914 | } |
| 5919 | o_addchr(dest, ch); | 5915 | o_addchr(dest, ch); |
| 5916 | if (ch == '(' || ch == '{') { | ||
| 5917 | ch = (ch == '(' ? ')' : '}'); | ||
| 5918 | add_till_closing_paren(dest, input, ch); | ||
| 5919 | o_addchr(dest, ch); | ||
| 5920 | continue; | ||
| 5921 | } | ||
| 5920 | if (ch == '\'') { | 5922 | if (ch == '\'') { |
| 5921 | add_till_single_quote(dest, input); | 5923 | add_till_single_quote(dest, input); |
| 5922 | o_addchr(dest, ch); | 5924 | o_addchr(dest, ch); |
| @@ -5927,6 +5929,11 @@ static void add_till_closing_paren(o_string *dest, struct in_str *input, char en | |||
| 5927 | o_addchr(dest, ch); | 5929 | o_addchr(dest, ch); |
| 5928 | continue; | 5930 | continue; |
| 5929 | } | 5931 | } |
| 5932 | if (ch == '`') { | ||
| 5933 | add_till_backquote(dest, input); | ||
| 5934 | o_addchr(dest, ch); | ||
| 5935 | continue; | ||
| 5936 | } | ||
| 5930 | if (ch == '\\') { | 5937 | if (ch == '\\') { |
| 5931 | /* \x. Copy verbatim. Important for \(, \) */ | 5938 | /* \x. Copy verbatim. Important for \(, \) */ |
| 5932 | ch = i_getch(input); | 5939 | ch = i_getch(input); |
diff --git a/shell/hush_test/hush-arith/arith.right b/shell/hush_test/hush-arith/arith.right index 83155fb03..718c26ad0 100644 --- a/shell/hush_test/hush-arith/arith.right +++ b/shell/hush_test/hush-arith/arith.right | |||
| @@ -43,21 +43,30 @@ Format: 'expected actual' | |||
| 43 | 4 4 | 43 | 4 4 |
| 44 | 29 29 | 44 | 29 29 |
| 45 | 5 5 | 45 | 5 5 |
| 46 | unary plus, minus | ||
| 46 | -4 -4 | 47 | -4 -4 |
| 47 | 4 4 | 48 | 4 4 |
| 49 | conditional expressions | ||
| 48 | 1 1 | 50 | 1 1 |
| 49 | 32 32 | 51 | 32 32 |
| 50 | 32 32 | 52 | 32 32 |
| 51 | 1 1 | 53 | 1 1 |
| 52 | 1 1 | 54 | 1 1 |
| 53 | 32 32 | 55 | 32 32 |
| 56 | check that parentheses in `cmd` are interpreted correctly | ||
| 57 | 3 3 | ||
| 58 | check that the unevaluated part of the ternary operator does not do evaluation or assignment | ||
| 54 | 20 20 | 59 | 20 20 |
| 55 | 30 30 | 60 | 30 30 |
| 56 | 20 20 | 61 | 20 20 |
| 57 | 30 30 | 62 | 30 30 |
| 63 | check precedence of assignment vs. conditional operator | ||
| 58 | hush: error in arithmetic | 64 | hush: error in arithmetic |
| 65 | check precedence of assignment vs. conditional operator | ||
| 66 | associativity of assignment-operator operator | ||
| 59 | 6 6 | 67 | 6 6 |
| 60 | 6,5,3 6,5,3 | 68 | 6,5,3 6,5,3 |
| 69 | octal, hex | ||
| 61 | 263 263 | 70 | 263 263 |
| 62 | 255 255 | 71 | 255 255 |
| 63 | 40 40 | 72 | 40 40 |
diff --git a/shell/hush_test/hush-arith/arith.tests b/shell/hush_test/hush-arith/arith.tests index 57e66e888..bc6b341d1 100755 --- a/shell/hush_test/hush-arith/arith.tests +++ b/shell/hush_test/hush-arith/arith.tests | |||
| @@ -75,11 +75,11 @@ echo 4 $(( iv &= 4 )) | |||
| 75 | echo 29 $(( iv += (jv + 9))) | 75 | echo 29 $(( iv += (jv + 9))) |
| 76 | echo 5 $(( (iv + 4) % 7 )) | 76 | echo 5 $(( (iv + 4) % 7 )) |
| 77 | 77 | ||
| 78 | # unary plus, minus | 78 | echo unary plus, minus |
| 79 | echo -4 $(( +4 - 8 )) | 79 | echo -4 $(( +4 - 8 )) |
| 80 | echo 4 $(( -4 + 8 )) | 80 | echo 4 $(( -4 + 8 )) |
| 81 | 81 | ||
| 82 | # conditional expressions | 82 | echo conditional expressions |
| 83 | echo 1 $(( 4<5 ? 1 : 32)) | 83 | echo 1 $(( 4<5 ? 1 : 32)) |
| 84 | echo 32 $(( 4>5 ? 1 : 32)) | 84 | echo 32 $(( 4>5 ? 1 : 32)) |
| 85 | echo 32 $(( 4>(2+3) ? 1 : 32)) | 85 | echo 32 $(( 4>(2+3) ? 1 : 32)) |
| @@ -87,8 +87,11 @@ echo 1 $(( 4<(2+3) ? 1 : 32)) | |||
| 87 | echo 1 $(( (2+2)<(2+3) ? 1 : 32)) | 87 | echo 1 $(( (2+2)<(2+3) ? 1 : 32)) |
| 88 | echo 32 $(( (2+2)>(2+3) ? 1 : 32)) | 88 | echo 32 $(( (2+2)>(2+3) ? 1 : 32)) |
| 89 | 89 | ||
| 90 | # check that the unevaluated part of the ternary operator does not do | 90 | echo 'check that parentheses in `cmd` are interpreted correctly' |
| 91 | # evaluation or assignment | 91 | # \x28 is '(' |
| 92 | echo 3 $(( ( `printf '(\x28 1'` + `echo 2\)\)` ) )) | ||
| 93 | |||
| 94 | echo check that the unevaluated part of the ternary operator does not do evaluation or assignment | ||
| 92 | x=i+=2 | 95 | x=i+=2 |
| 93 | y=j+=2 | 96 | y=j+=2 |
| 94 | #ash# declare -i i=1 j=1 | 97 | #ash# declare -i i=1 j=1 |
| @@ -109,20 +112,20 @@ echo 20 $((1 ? 20 : (x+=2))) | |||
| 109 | echo 30 $((0 ? (y+=2) : 30)) | 112 | echo 30 $((0 ? (y+=2) : 30)) |
| 110 | #ash# echo $i,$y # ash mishandles this | 113 | #ash# echo $i,$y # ash mishandles this |
| 111 | 114 | ||
| 112 | # check precedence of assignment vs. conditional operator | 115 | echo check precedence of assignment vs. conditional operator |
| 113 | # should be an error | 116 | # should be an error |
| 114 | #ash# declare -i x=2 | 117 | #ash# declare -i x=2 |
| 115 | x=2 | 118 | x=2 |
| 116 | #ashnote# bash reports error but continues, ash aborts - using subshell to 'emulate' bash: | 119 | #ashnote# bash reports error but continues, ash aborts - using subshell to 'emulate' bash: |
| 117 | ( y=$((1 ? 20 : x+=2)) ) | 120 | ( y=$((1 ? 20 : x+=2)) ) |
| 118 | 121 | ||
| 119 | # check precedence of assignment vs. conditional operator | 122 | echo check precedence of assignment vs. conditional operator |
| 120 | #ash# declare -i x=2 | 123 | #ash# declare -i x=2 |
| 121 | x=2 | 124 | x=2 |
| 122 | # ash says "line NNN: syntax error: 0 ? x+=2 : 20" | 125 | # ash says "line NNN: syntax error: 0 ? x+=2 : 20" |
| 123 | #ash# echo 20 $((0 ? x+=2 : 20)) | 126 | #ash# echo 20 $((0 ? x+=2 : 20)) |
| 124 | 127 | ||
| 125 | # associativity of assignment-operator operator | 128 | echo associativity of assignment-operator operator |
| 126 | #ash# declare -i i=1 j=2 k=3 | 129 | #ash# declare -i i=1 j=2 k=3 |
| 127 | i=1 | 130 | i=1 |
| 128 | j=2 | 131 | j=2 |
| @@ -130,7 +133,7 @@ k=3 | |||
| 130 | echo 6 $((i += j += k)) | 133 | echo 6 $((i += j += k)) |
| 131 | echo 6,5,3 $i,$j,$k | 134 | echo 6,5,3 $i,$j,$k |
| 132 | 135 | ||
| 133 | # octal, hex | 136 | echo octal, hex |
| 134 | echo 263 $(( 0x100 | 007 )) | 137 | echo 263 $(( 0x100 | 007 )) |
| 135 | echo 255 $(( 0xff )) | 138 | echo 255 $(( 0xff )) |
| 136 | #ash# echo 255 $(( 16#ff )) | 139 | #ash# echo 255 $(( 16#ff )) |
diff --git a/shell/hush_test/hush-vars/var_posix1.right b/shell/hush_test/hush-vars/var_posix1.right index 813437e2f..7ff618ad0 100644 --- a/shell/hush_test/hush-vars/var_posix1.right +++ b/shell/hush_test/hush-vars/var_posix1.right | |||
| @@ -37,4 +37,5 @@ ababcdcd_tail | |||
| 37 | ababcdcd | 37 | ababcdcd |
| 38 | ab | 38 | ab |
| 39 | ab | 39 | ab |
| 40 | ab | ||
| 40 | End | 41 | End |
diff --git a/shell/hush_test/hush-vars/var_posix1.tests b/shell/hush_test/hush-vars/var_posix1.tests index e48fd98c7..82abe8198 100755 --- a/shell/hush_test/hush-vars/var_posix1.tests +++ b/shell/hush_test/hush-vars/var_posix1.tests | |||
| @@ -47,5 +47,6 @@ echo ${var%\\*} | |||
| 47 | 47 | ||
| 48 | a=ab}; echo ${a%\}}; | 48 | a=ab}; echo ${a%\}}; |
| 49 | a=abc; c=c; echo ${a%${c}} | 49 | a=abc; c=c; echo ${a%${c}} |
| 50 | a=ab{{c; echo ${a%`echo {{c`} | ||
| 50 | 51 | ||
| 51 | echo End | 52 | echo End |
