From e37aa358bf89337e1cab9885fcbcbef63c7ef153 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Tue, 16 Feb 2021 06:50:12 +0000 Subject: [PATCH] Fix BUG_CASEEMPT: empty 'case' list was syntax error 'case x in esac' should be syntactically correct, but was an error: $ ksh -c 'case x in esac' ksh: syntax error at line 1: `case' unmatched Inserting a newline was a workaround: $ ksh -c $'case x in\nesac' (no output) The problem was that the 'esac' reserved word was not being recognised if it immediately followed the 'in' reserved word. src/cmd/ksh93/sh/lex.c: sh_lex(): - Do not turn off recognition of reserved words after 'in' if we're in a 'case' construct; only do this for 'for' and 'select'. src/cmd/ksh93/tests/case.sh: - Add seven regression test for correct recognition of 'esac'. Only two failed on ksh93. The rest is to catch future bugs. Fixes: https://github.com/ksh93/ksh/issues/177 --- NEWS | 3 +++ src/cmd/ksh93/sh/lex.c | 2 +- src/cmd/ksh93/tests/case.sh | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index fecc39850..dcb02f114 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,9 @@ Any uppercase BUG_* names are modernish shell bug IDs. 2021-02-15: +- Fixed a regression introduced by ksh93 (was not in ksh88): an empty 'case' + list on a single line ('case x in esac') was a syntax error. + - Fixed a bug in the emacs built-in editor, introduced on 2020-09-17, that made the Meta-D and Meta-H keys delete single characters instead of words. diff --git a/src/cmd/ksh93/sh/lex.c b/src/cmd/ksh93/sh/lex.c index 31614b42c..64487b421 100644 --- a/src/cmd/ksh93/sh/lex.c +++ b/src/cmd/ksh93/sh/lex.c @@ -1503,7 +1503,7 @@ breakloop: } else lp->lex.skipword = 0; - if(c==INSYM) + if(c==INSYM && !lp->lex.incase) lp->lex.reservok = 0; else if(c==TIMESYM) { diff --git a/src/cmd/ksh93/tests/case.sh b/src/cmd/ksh93/tests/case.sh index 2ed08c1e3..9eb9a19db 100755 --- a/src/cmd/ksh93/tests/case.sh +++ b/src/cmd/ksh93/tests/case.sh @@ -84,4 +84,15 @@ esac') != b ]] && err_exit 'bug in ;& at end of script' x=$($SHELL -ec 'case a in a) echo 1; false; echo 2 ;& b) echo 3;; esac') [[ $x == 1 ]] || err_exit 'set -e ignored on case fail through' +# ====== +# An empty 'case' list was a syntax error: https://github.com/ksh93/ksh/issues/177 +got=$(eval 'case x in esac' 2>&1) || err_exit "empty case list fails: got $(printf %q "$got")" +got=$(eval ': || for i in esac; do :; done' 2>&1) || err_exit "'for i in esac' fails: got $(printf %q "$got")" +got=$(eval ': || select i in esac; do :; done' 2>&1) || err_exit "'select i in esac' fails: got $(printf %q "$got")" +(eval 'case x in esac) foo;; esac') 2>/dev/null && err_exit "unquoted 'esac' keyword as first selector fails to fail" +(eval 'case x in y) bar;; esac) foo;; esac') 2>/dev/null && err_exit "unquoted 'esac' kwyword as nth selector fails to fail" +got=$(eval 'case x in e\sac) foo;; esac' 2>&1) || err_exit "quoted 'esac' pattern (1) fails: got $(printf %q "$got")" +got=$(eval 'case x in y) bar;; es\ac) foo;; esac' 2>&1) || err_exit "quoted 'esac' pattern (2) fails: got $(printf %q "$got")" + +# ====== exit $((Errors<125?Errors:125))