From 89e9d5534d0e8879803ed9dbb25dff3989c31202 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 11 Apr 2018 01:15:33 +0200 Subject: hush: do not drop backslash from eval 'echo ok\' newer bash does not drop it, most other shells too function old new delta unbackslash 39 57 +18 parse_stream 2753 2751 -2 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/1 up/down: 18/-2) Total: 16 bytes Signed-off-by: Denys Vlasenko --- shell/ash.c | 2 +- shell/ash_test/ash-parsing/bkslash_eof1.right | 1 + shell/ash_test/ash-parsing/bkslash_eof1.tests | 1 + shell/ash_test/ash-redir/redir_exec1.right | 2 +- shell/hush.c | 22 +++++++++++++++++----- shell/hush_test/hush-parsing/bkslash_eof1.right | 2 +- 6 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 shell/ash_test/ash-parsing/bkslash_eof1.right create mode 100755 shell/ash_test/ash-parsing/bkslash_eof1.tests (limited to 'shell') diff --git a/shell/ash.c b/shell/ash.c index 713219b..6f8bc90 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -203,7 +203,7 @@ * TODO: * singleword+noglob expansion: * v='a b'; [[ $v = 'a b' ]]; echo 0:$? - * [[ /bin/* ]]; echo 0:$? + * [[ /bin/n* ]]; echo 0:$? * -a/-o are not AND/OR ops! (they are just strings) * quoting needs to be considered (-f is an operator, "-f" and ""-f are not; etc) * = is glob match operator, not equality operator: STR = GLOB diff --git a/shell/ash_test/ash-parsing/bkslash_eof1.right b/shell/ash_test/ash-parsing/bkslash_eof1.right new file mode 100644 index 0000000..6c6df0b --- /dev/null +++ b/shell/ash_test/ash-parsing/bkslash_eof1.right @@ -0,0 +1 @@ +ok\ diff --git a/shell/ash_test/ash-parsing/bkslash_eof1.tests b/shell/ash_test/ash-parsing/bkslash_eof1.tests new file mode 100755 index 0000000..97629cb --- /dev/null +++ b/shell/ash_test/ash-parsing/bkslash_eof1.tests @@ -0,0 +1 @@ +eval 'echo ok\' diff --git a/shell/ash_test/ash-redir/redir_exec1.right b/shell/ash_test/ash-redir/redir_exec1.right index d4393d1..c98455b 100644 --- a/shell/ash_test/ash-redir/redir_exec1.right +++ b/shell/ash_test/ash-redir/redir_exec1.right @@ -1,2 +1,2 @@ -redir_exec1.tests: line 1: can't create /cant/be/created: nonexistent directory +./redir_exec1.tests: line 1: can't create /cant/be/created: nonexistent directory First diff --git a/shell/hush.c b/shell/hush.c index 3afb70c..523fc1a 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -83,7 +83,7 @@ * Status of [[ support: * [[ args ]] are CMD_SINGLEWORD_NOGLOB: * v='a b'; [[ $v = 'a b' ]]; echo 0:$? - * [[ /bin/* ]]; echo 0:$? + * [[ /bin/n* ]]; echo 0:$? * TODO: * &&/|| are AND/OR ops, -a/-o are not * quoting needs to be considered (-f is an operator, "-f" and ""-f are not; etc) @@ -1426,8 +1426,19 @@ static char *unbackslash(char *src) { char *dst = src = strchrnul(src, '\\'); while (1) { - if (*src == '\\') + if (*src == '\\') { src++; + if (*src != '\0') { + /* \x -> x */ + *dst++ = *src++; + continue; + } + /* else: "\". Do not delete this backslash. + * Testcase: eval 'echo ok\' + */ + *dst++ = '\\'; + /* fallthrough */ + } if ((*dst++ = *src++) == '\0') break; } @@ -5392,16 +5403,17 @@ static struct pipe *parse_stream(char **pstring, continue; /* get next char */ case '\\': /*nommu_addchr(&ctx.as_string, '\\'); - already done */ + o_addchr(&ctx.word, '\\'); ch = i_getch(input); if (ch == EOF) { - /* Ignore this '\'. Testcase: eval 'echo Ok\' */ -#if !BB_MMU + /* Testcase: eval 'echo Ok\' */ + +#if 0 /* bash-4.3.43 was removing backslash, but 4.4.19 retains it, most other shells too */ /* Remove trailing '\' from ctx.as_string */ ctx.as_string.data[--ctx.as_string.length] = '\0'; #endif continue; /* get next char */ } - o_addchr(&ctx.word, '\\'); /* Example: echo Hello \2>file * we need to know that word 2 is quoted */ diff --git a/shell/hush_test/hush-parsing/bkslash_eof1.right b/shell/hush_test/hush-parsing/bkslash_eof1.right index 9766475..6c6df0b 100644 --- a/shell/hush_test/hush-parsing/bkslash_eof1.right +++ b/shell/hush_test/hush-parsing/bkslash_eof1.right @@ -1 +1 @@ -ok +ok\ -- cgit v1.1