dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] parser: Fix alias expansion after heredoc or newlines
@ 2020-04-28 14:19 Herbert Xu
  0 siblings, 0 replies; only message in thread
From: Herbert Xu @ 2020-04-28 14:19 UTC (permalink / raw)
  To: dash, Harald van Dijk, Martijn Dekker

The following patch is meant to be applied on top of:

https://patchwork.kernel.org/patch/11512439/

---8<---
This script should print OK:

	alias a="case x in " b=x
	a
	b) echo BAD;; esac

	alias BEGIN={ END=}
	BEGIN
		cat <<- EOF > /dev/null
			$(:)
		EOF
	END

	: <<- EOF &&
		$(:)
	EOF
	BEGIN
		echo OK
	END

However, because the value of checkkwd is either zeroed when it
shouldn't, or isn't zeroed when it should, dash currently gets
it wrong in every case.

This patch fixes it by saving checkkwd and zeroing it where needed.

Suggested-by: Harald van Dijk <harald@gigawatt.nl>
Reported-by: Harald van Dijk <harald@gigawatt.nl>
Reported-by: Martijn Dekker <martijn@inlv.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

diff --git a/src/parser.c b/src/parser.c
index 5c9e9a0..be84f8b 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -704,10 +704,14 @@ top:
 	if (kwd & CHKNL) {
 		while (t == TNL) {
 			parseheredoc();
+			checkkwd = 0;
 			t = xxreadtoken();
 		}
 	}
 
+	kwd |= checkkwd;
+	checkkwd = 0;
+
 	if (t != TWORD || quoteflag) {
 		goto out;
 	}
@@ -725,7 +729,7 @@ top:
 		}
 	}
 
-	if (checkkwd & CHKALIAS) {
+	if (kwd & CHKALIAS) {
 		struct alias *ap;
 		if ((ap = lookupalias(wordtext, 1)) != NULL) {
 			if (*ap->val) {
@@ -735,7 +739,6 @@ top:
 		}
 	}
 out:
-	checkkwd = 0;
 #ifdef DEBUG
 	if (!alreadyseen)
 	    TRACE(("token %s %s\n", tokname[t], t == TWORD ? wordtext : ""));
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2020-04-28 14:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-28 14:19 [PATCH] parser: Fix alias expansion after heredoc or newlines Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).