From 7a5423dfb66fd71a77e4399a0336b3a512cff9f1 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Mon, 6 Jun 2022 02:12:33 +0100 Subject: [PATCH] Fix more spurious comsub execution in tab completion (re: 7a2d3564) Comsubs were either executed or caused a syntax error when attempting to complete them within single quotes. Since single quotes do not expand anything, no such completion should take place. $ '`/de-ksh: /dev/: cannot execute [Is a directory] $ '$(/de-ksh: syntax error: `end of file' unexpected src/cmd/ksh93/edit/completion.c: - find_begin(): - Remove recursive handling for '`' comsubs from 7a2d3564; it is sufficient to set the return pointer to the current location cp (the character following '`') if we're not in single quotes. - For '$' and '`', if we are within single quotes, set type '\'' and set the return pointer bp to the location of the '$' or '`'. - ed_expand(): If find_begin() sets type '\'' and the current begin character is $ or `, refuse to attempt completion; return -1 to cause a terminal beep. Related: https://github.com/ksh93/ksh/issues/268 https://github.com/ksh93/ksh/issues/462#issuecomment-1038482307 --- NEWS | 6 ++++++ src/cmd/ksh93/edit/completion.c | 24 ++++++++++++++++++------ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/tests/pty.sh | 15 +++++++++++++++ 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 123e7a3fe..9e91f5431 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,12 @@ For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0 Any uppercase BUG_* names are modernish shell bug IDs. +2022-06-05: + +- Fixed a bug where tab completion would spuriously execute a command + substitution after entering: '`something or generate a spurious + 'end of file unexpected' syntax error after entering: '$(something. + 2022-06-04: - Added a new --functrace long-form shell option which causes the -x/--xtrace diff --git a/src/cmd/ksh93/edit/completion.c b/src/cmd/ksh93/edit/completion.c index bd5fdfc34..b22af55fb 100644 --- a/src/cmd/ksh93/edit/completion.c +++ b/src/cmd/ksh93/edit/completion.c @@ -135,7 +135,11 @@ static char *find_begin(char outbuff[], char *last, int endchar, int *type) break; case '$': if(inquote == '\'') + { + *type = '\''; + bp = xp; break; + } c = *(unsigned char*)cp; if(mode!='*' && (isaletter(c) || c=='{')) { @@ -176,11 +180,12 @@ static char *find_begin(char outbuff[], char *last, int endchar, int *type) break; case '`': if(inquote=='\'') - break; - *type = mode; - xp = find_begin(cp,last,'`',type); - if(*(cp=xp)!='`') + { + *type = '\''; bp = xp; + } + else + bp = cp; break; case '=': if(!inquote) @@ -287,9 +292,15 @@ int ed_expand(Edit_t *ep, char outbuff[],int *cur,int *eol,int mode, int count) c = *(unsigned char*)out; var = mode; begin = out = find_begin(outbuff,last,0,&var); - /* addstar set to zero if * should not be added */ - if(var=='$') + if(var=='\'' && (*begin=='$' || *begin=='`')) { + /* avoid spurious expansion or comsub execution within '...' */ + rval = -1; + goto done; + } + else if(var=='$') + { + /* expand ${!varname@} to complete variable name(s) */ stakputs("${!"); stakwrite(out,last-out); stakputs("@}"); @@ -297,6 +308,7 @@ int ed_expand(Edit_t *ep, char outbuff[],int *cur,int *eol,int mode, int count) } else { + /* addstar set to zero if * should not be added */ addstar = '*'; while(out < last) { diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index 9c0daad67..95a1a5593 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -21,7 +21,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.0.0-beta.2" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2022-06-04" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2022-06-05" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2022 Contributors to ksh " SH_RELEASE_FORK /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/cmd/ksh93/tests/pty.sh b/src/cmd/ksh93/tests/pty.sh index aed6a2662..bf6fb9ede 100755 --- a/src/cmd/ksh93/tests/pty.sh +++ b/src/cmd/ksh93/tests/pty.sh @@ -831,6 +831,7 @@ r ^:test-1: test \$\{.sh.level\}\r\n$ ((SHOPT_VSH || SHOPT_ESH)) && tst $LINENO <<"!" L tab completion executes command substitutions # https://github.com/ksh93/ksh/issues/268 +# https://github.com/ksh93/ksh/issues/462#issuecomment-1038482307 d 15 p :test-1: @@ -839,6 +840,20 @@ r ^:test-1: \$\(echo true\)\r\n$ p :test-2: w `echo true`\t r ^:test-2: `echo true`\r\n$ +p :test-3: +w '`/dev\t +r ^:test-3: '`/dev[[:blank:]]*\r\n$ +# escape from PS2 prompt with Ctrl+C +r ^> $ +c \cC +p :test-4: +w '$(/dev\t +r ^:test-4: '\$\(/dev[[:blank:]]*\r\n$ +r ^> $ +c \cC +p :test-5: +w $'`/dev\t +r ^:test-5: \$'`/dev[[:blank:]]*\r\n$ ! ((SHOPT_ESH)) && VISUAL=emacs tst $LINENO <<"!"