a57977
From 6b8f3f9906ee44be46e022480b6e01755feeaa99 Mon Sep 17 00:00:00 2001
a57977
From: Peter Stephenson <pws@zsh.org>
a57977
Date: Tue, 6 Jan 2015 17:05:17 +0000
eb5f7e
Subject: [PATCH 1/9] Fix command substitutions to parse contents as they are
a57977
 read in.
a57977
a57977
Do this by refactoring misnamed lexsave()/lexrestore() to allow
a57977
continuity of history and input.
a57977
a57977
Add test.
a57977
a57977
Upstream-commit: c0d01a6fe0c67911650730cf13a2b9a0db16e59b
a57977
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
a57977
---
a57977
 Src/init.c            |   3 +-
a57977
 Src/input.c           |  13 +-
a57977
 Src/lex.c             | 498 ++++++++++++++++++++++++++++++++------------------
a57977
 Src/parse.c           |  29 ++-
a57977
 Src/zsh.h             |   9 +
a57977
 Test/D08cmdsubst.ztst |  42 +++++
a57977
 6 files changed, 401 insertions(+), 193 deletions(-)
a57977
a57977
diff --git a/Src/init.c b/Src/init.c
a57977
index 0742a9f..78f171d 100644
a57977
--- a/Src/init.c
a57977
+++ b/Src/init.c
a57977
@@ -129,7 +129,8 @@ loop(int toplevel, int justonce)
a57977
 	use_exit_printed = 0;
a57977
 	intr();			/* interrupts on            */
a57977
 	lexinit();              /* initialize lexical state */
a57977
-	if (!(prog = parse_event())) {	/* if we couldn't parse a list */
a57977
+	if (!(prog = parse_event(ENDINPUT))) {
a57977
+	    /* if we couldn't parse a list */
a57977
 	    hend(NULL);
a57977
 	    if ((tok == ENDINPUT && !errflag) ||
a57977
 		(tok == LEXERR && (!isset(SHINSTDIN) || !toplevel)) ||
a57977
diff --git a/Src/input.c b/Src/input.c
a57977
index 5cff22d..1579762 100644
a57977
--- a/Src/input.c
a57977
+++ b/Src/input.c
a57977
@@ -175,12 +175,12 @@ shingetline(void)
a57977
 /* Get the next character from the input.
a57977
  * Will call inputline() to get a new line where necessary.
a57977
  */
a57977
-  
a57977
+
a57977
 /**/
a57977
 int
a57977
 ingetc(void)
a57977
 {
a57977
-    int lastc;
a57977
+    int lastc = ' ';
a57977
 
a57977
     if (lexstop)
a57977
 	return ' ';
a57977
@@ -192,7 +192,7 @@ ingetc(void)
a57977
 		continue;
a57977
 	    if (((inbufflags & INP_LINENO) || !strin) && lastc == '\n')
a57977
 		lineno++;
a57977
-	    return lastc;
a57977
+	    break;
a57977
 	}
a57977
 
a57977
 	/*
a57977
@@ -204,7 +204,7 @@ ingetc(void)
a57977
 	 */
a57977
 	if (!inbufct && (strin || errflag)) {
a57977
 	    lexstop = 1;
a57977
-	    return ' ';
a57977
+	    break;
a57977
 	}
a57977
 	/* If the next element down the input stack is a continuation of
a57977
 	 * this, use it.
a57977
@@ -215,8 +215,10 @@ ingetc(void)
a57977
 	}
a57977
 	/* As a last resort, get some more input */
a57977
 	if (inputline())
a57977
-	    return ' ';
a57977
+	    break;
a57977
     }
a57977
+    zshlex_raw_add(lastc);
a57977
+    return lastc;
a57977
 }
a57977
 
a57977
 /* Read a line from the current command stream and store it as input */
a57977
@@ -421,6 +423,7 @@ inungetc(int c)
a57977
 	    inbufleft = 0;
a57977
 	    inbuf = inbufptr = "";
a57977
 	}
a57977
+	zshlex_raw_back();
a57977
     }
a57977
 }
a57977
 
a57977
diff --git a/Src/lex.c b/Src/lex.c
a57977
index 82bf848..bcceda6 100644
a57977
--- a/Src/lex.c
a57977
+++ b/Src/lex.c
a57977
@@ -146,6 +146,16 @@ mod_export int parend;
a57977
 /**/
a57977
 mod_export int nocomments;
a57977
 
a57977
+/* add raw input characters while parsing command substitution */
a57977
+
a57977
+/**/
a57977
+static int lex_add_raw;
a57977
+
a57977
+/* variables associated with the above */
a57977
+
a57977
+static char *tokstr_raw, *bptr_raw;
a57977
+static int len_raw, bsiz_raw;
a57977
+
a57977
 /* text of punctuation tokens */
a57977
 
a57977
 /**/
a57977
@@ -214,6 +224,11 @@ struct lexstack {
a57977
     char *bptr;
a57977
     int bsiz;
a57977
     int len;
a57977
+    int lex_add_raw;
a57977
+    char *tokstr_raw;
a57977
+    char *bptr_raw;
a57977
+    int bsiz_raw;
a57977
+    int len_raw;
a57977
     short *chwords;
a57977
     int chwordlen;
a57977
     int chwordpos;
a57977
@@ -239,89 +254,121 @@ struct lexstack {
a57977
 
a57977
 static struct lexstack *lstack = NULL;
a57977
 
a57977
-/* save the lexical state */
a57977
+/* save the context or parts thereof */
a57977
 
a57977
 /* is this a hack or what? */
a57977
 
a57977
 /**/
a57977
 mod_export void
a57977
-lexsave(void)
a57977
+lexsave_partial(int parts)
a57977
 {
a57977
     struct lexstack *ls;
a57977
 
a57977
     ls = (struct lexstack *)malloc(sizeof(struct lexstack));
a57977
 
a57977
-    ls->incmdpos = incmdpos;
a57977
-    ls->incond = incond;
a57977
-    ls->incasepat = incasepat;
a57977
-    ls->dbparens = dbparens;
a57977
-    ls->isfirstln = isfirstln;
a57977
-    ls->isfirstch = isfirstch;
a57977
-    ls->histactive = histactive;
a57977
-    ls->histdone = histdone;
a57977
-    ls->lexflags = lexflags;
a57977
-    ls->stophist = stophist;
a57977
-    stophist = 0;
a57977
-    if (!lstack) {
a57977
-	/* top level, make this version visible to ZLE */
a57977
-	zle_chline = chline;
a57977
-	/* ensure line stored is NULL-terminated */
a57977
-	if (hptr)
a57977
-	    *hptr = '\0';
a57977
+    if (parts & ZCONTEXT_LEX) {
a57977
+	ls->incmdpos = incmdpos;
a57977
+	ls->incond = incond;
a57977
+	ls->incasepat = incasepat;
a57977
+	ls->dbparens = dbparens;
a57977
+	ls->isfirstln = isfirstln;
a57977
+	ls->isfirstch = isfirstch;
a57977
+	ls->lexflags = lexflags;
a57977
+
a57977
+	ls->tok = tok;
a57977
+	ls->isnewlin = isnewlin;
a57977
+	ls->tokstr = tokstr;
a57977
+	ls->zshlextext = zshlextext;
a57977
+	ls->bptr = bptr;
a57977
+	ls->bsiz = bsiz;
a57977
+	ls->len = len;
a57977
+	ls->lex_add_raw = lex_add_raw;
a57977
+	ls->tokstr_raw = tokstr_raw;
a57977
+	ls->bptr_raw = bptr_raw;
a57977
+	ls->bsiz_raw = bsiz_raw;
a57977
+	ls->len_raw = len_raw;
a57977
+	ls->lexstop = lexstop;
a57977
+	ls->toklineno = toklineno;
a57977
+
a57977
+	tokstr = zshlextext = bptr = NULL;
a57977
+	bsiz = 256;
a57977
+	tokstr_raw = bptr_raw = NULL;
a57977
+	bsiz_raw = len_raw = lex_add_raw = 0;
a57977
+
a57977
+	inredir = 0;
a57977
+    }
a57977
+    if (parts & ZCONTEXT_HIST) {
a57977
+	if (!lstack) {
a57977
+	    /* top level, make this version visible to ZLE */
a57977
+	    zle_chline = chline;
a57977
+	    /* ensure line stored is NULL-terminated */
a57977
+	    if (hptr)
a57977
+		*hptr = '\0';
a57977
+	}
a57977
+	ls->histactive = histactive;
a57977
+	ls->histdone = histdone;
a57977
+	ls->stophist = stophist;
a57977
+	ls->hline = chline;
a57977
+	ls->hptr = hptr;
a57977
+	ls->chwords = chwords;
a57977
+	ls->chwordlen = chwordlen;
a57977
+	ls->chwordpos = chwordpos;
a57977
+	ls->hwgetword = hwgetword;
a57977
+	ls->hgetc = hgetc;
a57977
+	ls->hungetc = hungetc;
a57977
+	ls->hwaddc = hwaddc;
a57977
+	ls->hwbegin = hwbegin;
a57977
+	ls->hwend = hwend;
a57977
+	ls->addtoline = addtoline;
a57977
+	ls->hlinesz = hlinesz;
a57977
+	/*
a57977
+	 * We save and restore the command stack with history
a57977
+	 * as it's visible to the user interactively, so if
a57977
+	 * we're preserving history state we'll continue to
a57977
+	 * show the current set of commands from input.
a57977
+	 */
a57977
+	ls->cstack = cmdstack;
a57977
+	ls->csp = cmdsp;
a57977
+
a57977
+	stophist = 0;
a57977
+	chline = NULL;
a57977
+	hptr = NULL;
a57977
+	histactive = 0;
a57977
+	cmdstack = (unsigned char *)zalloc(CMDSTACKSZ);
a57977
+	cmdsp = 0;
a57977
+    }
a57977
+    if (parts & ZCONTEXT_PARSE) {
a57977
+	ls->hdocs = hdocs;
a57977
+	ls->eclen = eclen;
a57977
+	ls->ecused = ecused;
a57977
+	ls->ecnpats = ecnpats;
a57977
+	ls->ecbuf = ecbuf;
a57977
+	ls->ecstrs = ecstrs;
a57977
+	ls->ecsoffs = ecsoffs;
a57977
+	ls->ecssub = ecssub;
a57977
+	ls->ecnfunc = ecnfunc;
a57977
+	ecbuf = NULL;
a57977
+	hdocs = NULL;
a57977
     }
a57977
-    ls->hline = chline;
a57977
-    chline = NULL;
a57977
-    ls->hptr = hptr;
a57977
-    hptr = NULL;
a57977
-    ls->hlinesz = hlinesz;
a57977
-    ls->cstack = cmdstack;
a57977
-    ls->csp = cmdsp;
a57977
-    cmdstack = (unsigned char *)zalloc(CMDSTACKSZ);
a57977
-    ls->tok = tok;
a57977
-    ls->isnewlin = isnewlin;
a57977
-    ls->tokstr = tokstr;
a57977
-    ls->zshlextext = zshlextext;
a57977
-    ls->bptr = bptr;
a57977
-    tokstr = zshlextext = bptr = NULL;
a57977
-    ls->bsiz = bsiz;
a57977
-    bsiz = 256;
a57977
-    ls->len = len;
a57977
-    ls->chwords = chwords;
a57977
-    ls->chwordlen = chwordlen;
a57977
-    ls->chwordpos = chwordpos;
a57977
-    ls->hwgetword = hwgetword;
a57977
-    ls->lexstop = lexstop;
a57977
-    ls->hdocs = hdocs;
a57977
-    ls->hgetc = hgetc;
a57977
-    ls->hungetc = hungetc;
a57977
-    ls->hwaddc = hwaddc;
a57977
-    ls->hwbegin = hwbegin;
a57977
-    ls->hwend = hwend;
a57977
-    ls->addtoline = addtoline;
a57977
-    ls->eclen = eclen;
a57977
-    ls->ecused = ecused;
a57977
-    ls->ecnpats = ecnpats;
a57977
-    ls->ecbuf = ecbuf;
a57977
-    ls->ecstrs = ecstrs;
a57977
-    ls->ecsoffs = ecsoffs;
a57977
-    ls->ecssub = ecssub;
a57977
-    ls->ecnfunc = ecnfunc;
a57977
-    ls->toklineno = toklineno;
a57977
-    cmdsp = 0;
a57977
-    inredir = 0;
a57977
-    hdocs = NULL;
a57977
-    histactive = 0;
a57977
-    ecbuf = NULL;
a57977
 
a57977
     ls->next = lstack;
a57977
     lstack = ls;
a57977
 }
a57977
 
a57977
-/* restore lexical state */
a57977
+/* save context in full */
a57977
 
a57977
 /**/
a57977
 mod_export void
a57977
-lexrestore(void)
a57977
+lexsave(void)
a57977
+{
a57977
+    lexsave_partial(ZCONTEXT_HIST|ZCONTEXT_LEX|ZCONTEXT_PARSE);
a57977
+}
a57977
+
a57977
+/* restore context or part therefore */
a57977
+
a57977
+/**/
a57977
+mod_export void
a57977
+lexrestore_partial(int parts)
a57977
 {
a57977
     struct lexstack *ln = lstack;
a57977
 
a57977
@@ -330,65 +377,89 @@ lexrestore(void)
a57977
     queue_signals();
a57977
     lstack = lstack->next;
a57977
 
a57977
-    if (!lstack) {
a57977
-	/* Back to top level: don't need special ZLE value */
a57977
-	DPUTS(ln->hline != zle_chline, "BUG: Ouch, wrong chline for ZLE");
a57977
-	zle_chline = NULL;
a57977
+    if (parts & ZCONTEXT_LEX) {
a57977
+	incmdpos = ln->incmdpos;
a57977
+	incond = ln->incond;
a57977
+	incasepat = ln->incasepat;
a57977
+	dbparens = ln->dbparens;
a57977
+	isfirstln = ln->isfirstln;
a57977
+	isfirstch = ln->isfirstch;
a57977
+	lexflags = ln->lexflags;
a57977
+	tok = ln->tok;
a57977
+	isnewlin = ln->isnewlin;
a57977
+	tokstr = ln->tokstr;
a57977
+	zshlextext = ln->zshlextext;
a57977
+	bptr = ln->bptr;
a57977
+	bsiz = ln->bsiz;
a57977
+	len = ln->len;
a57977
+	lex_add_raw = ln->lex_add_raw;
a57977
+	tokstr_raw = ln->tokstr_raw;
a57977
+	bptr_raw = ln->bptr_raw;
a57977
+	bsiz_raw = ln->bsiz_raw;
a57977
+	len_raw = ln->len_raw;
a57977
+	lexstop = ln->lexstop;
a57977
+	toklineno = ln->toklineno;
a57977
+    }
a57977
+
a57977
+    if (parts & ZCONTEXT_HIST) {
a57977
+	if (!lstack) {
a57977
+	    /* Back to top level: don't need special ZLE value */
a57977
+	    DPUTS(ln->hline != zle_chline, "BUG: Ouch, wrong chline for ZLE");
a57977
+	    zle_chline = NULL;
a57977
+	}
a57977
+	histactive = ln->histactive;
a57977
+	histdone = ln->histdone;
a57977
+	stophist = ln->stophist;
a57977
+	chline = ln->hline;
a57977
+	hptr = ln->hptr;
a57977
+	chwords = ln->chwords;
a57977
+	chwordlen = ln->chwordlen;
a57977
+	chwordpos = ln->chwordpos;
a57977
+	hwgetword = ln->hwgetword;
a57977
+	hgetc = ln->hgetc;
a57977
+	hungetc = ln->hungetc;
a57977
+	hwaddc = ln->hwaddc;
a57977
+	hwbegin = ln->hwbegin;
a57977
+	hwend = ln->hwend;
a57977
+	addtoline = ln->addtoline;
a57977
+	hlinesz = ln->hlinesz;
a57977
+	if (cmdstack)
a57977
+	    zfree(cmdstack, CMDSTACKSZ);
a57977
+	cmdstack = ln->cstack;
a57977
+	cmdsp = ln->csp;
a57977
+    }
a57977
+
a57977
+    if (parts & ZCONTEXT_PARSE) {
a57977
+	if (ecbuf)
a57977
+	    zfree(ecbuf, eclen);
a57977
+
a57977
+	hdocs = ln->hdocs;
a57977
+	eclen = ln->eclen;
a57977
+	ecused = ln->ecused;
a57977
+	ecnpats = ln->ecnpats;
a57977
+	ecbuf = ln->ecbuf;
a57977
+	ecstrs = ln->ecstrs;
a57977
+	ecsoffs = ln->ecsoffs;
a57977
+	ecssub = ln->ecssub;
a57977
+	ecnfunc = ln->ecnfunc;
a57977
+
a57977
+	errflag = 0;
a57977
     }
a57977
 
a57977
-    incmdpos = ln->incmdpos;
a57977
-    incond = ln->incond;
a57977
-    incasepat = ln->incasepat;
a57977
-    dbparens = ln->dbparens;
a57977
-    isfirstln = ln->isfirstln;
a57977
-    isfirstch = ln->isfirstch;
a57977
-    histactive = ln->histactive;
a57977
-    histdone = ln->histdone;
a57977
-    lexflags = ln->lexflags;
a57977
-    stophist = ln->stophist;
a57977
-    chline = ln->hline;
a57977
-    hptr = ln->hptr;
a57977
-    if (cmdstack)
a57977
-	zfree(cmdstack, CMDSTACKSZ);
a57977
-    cmdstack = ln->cstack;
a57977
-    cmdsp = ln->csp;
a57977
-    tok = ln->tok;
a57977
-    isnewlin = ln->isnewlin;
a57977
-    tokstr = ln->tokstr;
a57977
-    zshlextext = ln->zshlextext;
a57977
-    bptr = ln->bptr;
a57977
-    bsiz = ln->bsiz;
a57977
-    len = ln->len;
a57977
-    chwords = ln->chwords;
a57977
-    chwordlen = ln->chwordlen;
a57977
-    chwordpos = ln->chwordpos;
a57977
-    hwgetword = ln->hwgetword;
a57977
-    lexstop = ln->lexstop;
a57977
-    hdocs = ln->hdocs;
a57977
-    hgetc = ln->hgetc;
a57977
-    hungetc = ln->hungetc;
a57977
-    hwaddc = ln->hwaddc;
a57977
-    hwbegin = ln->hwbegin;
a57977
-    hwend = ln->hwend;
a57977
-    addtoline = ln->addtoline;
a57977
-    if (ecbuf)
a57977
-	zfree(ecbuf, eclen);
a57977
-    eclen = ln->eclen;
a57977
-    ecused = ln->ecused;
a57977
-    ecnpats = ln->ecnpats;
a57977
-    ecbuf = ln->ecbuf;
a57977
-    ecstrs = ln->ecstrs;
a57977
-    ecsoffs = ln->ecsoffs;
a57977
-    ecssub = ln->ecssub;
a57977
-    ecnfunc = ln->ecnfunc;
a57977
-    hlinesz = ln->hlinesz;
a57977
-    toklineno = ln->toklineno;
a57977
-    errflag = 0;
a57977
     free(ln);
a57977
 
a57977
     unqueue_signals();
a57977
 }
a57977
 
a57977
+/* complete restore context */
a57977
+
a57977
+/**/
a57977
+mod_export void
a57977
+lexrestore(void)
a57977
+{
a57977
+    lexrestore_partial(ZCONTEXT_HIST|ZCONTEXT_LEX|ZCONTEXT_PARSE);
a57977
+}
a57977
+
a57977
 /**/
a57977
 void
a57977
 zshlex(void)
a57977
@@ -1889,80 +1960,151 @@ exalias(void)
a57977
     return 0;
a57977
 }
a57977
 
a57977
-/* skip (...) */
a57977
+/**/
a57977
+void
a57977
+zshlex_raw_add(int c)
a57977
+{
a57977
+    if (!lex_add_raw)
a57977
+	return;
a57977
+
a57977
+    *bptr_raw++ = c;
a57977
+    if (bsiz_raw == ++len_raw) {
a57977
+	int newbsiz = bsiz_raw * 2;
a57977
+
a57977
+	tokstr_raw = (char *)hrealloc(tokstr_raw, bsiz_raw, newbsiz);
a57977
+	bptr_raw = tokstr_raw + len_raw;
a57977
+	memset(bptr_raw, 0, newbsiz - bsiz_raw);
a57977
+	bsiz_raw = newbsiz;
a57977
+    }
a57977
+}
a57977
+
a57977
+/**/
a57977
+void
a57977
+zshlex_raw_back(void)
a57977
+{
a57977
+    if (!lex_add_raw)
a57977
+	return;
a57977
+    bptr_raw--;
a57977
+    len_raw--;
a57977
+}
a57977
+
a57977
+/*
a57977
+ * Skip (...) for command-style substitutions: $(...), <(...), >(...)
a57977
+ *
a57977
+ * In order to ensure we don't stop at closing parentheses with
a57977
+ * some other syntactic significance, we'll parse the input until
a57977
+ * we find an unmatched closing parenthesis.  However, we'll throw
a57977
+ * away the result of the parsing and just keep the string we've built
a57977
+ * up on the way.
a57977
+ */
a57977
 
a57977
 /**/
a57977
 static int
a57977
 skipcomm(void)
a57977
 {
a57977
-    int pct = 1, c, start = 1;
a57977
+    char *new_tokstr, *new_bptr = bptr_raw;
a57977
+    int new_len, new_bsiz, new_lexstop, new_lex_add_raw;
a57977
 
a57977
     cmdpush(CS_CMDSUBST);
a57977
     SETPARBEGIN
a57977
-    c = Inpar;
a57977
-    do {
a57977
-	int iswhite;
a57977
-	add(c);
a57977
-	c = hgetc();
a57977
-	if (itok(c) || lexstop)
a57977
-	    break;
a57977
-	iswhite = inblank(c);
a57977
-	switch (c) {
a57977
-	case '(':
a57977
-	    pct++;
a57977
-	    break;
a57977
-	case ')':
a57977
-	    pct--;
a57977
-	    break;
a57977
-	case '\\':
a57977
-	    add(c);
a57977
-	    c = hgetc();
a57977
-	    break;
a57977
-	case '\'': {
a57977
-	    int strquote = bptr[-1] == '$';
a57977
-	    add(c);
a57977
-	    STOPHIST
a57977
-	    while ((c = hgetc()) != '\'' && !lexstop) {
a57977
-		if (c == '\\' && strquote) {
a57977
-		    add(c);
a57977
-		    c = hgetc();
a57977
-		}
a57977
-		add(c);
a57977
-	    }
a57977
-	    ALLOWHIST
a57977
-	    break;
a57977
-	}
a57977
-	case '\"':
a57977
-	    add(c);
a57977
-	    while ((c = hgetc()) != '\"' && !lexstop)
a57977
-		if (c == '\\') {
a57977
-		    add(c);
a57977
-		    add(hgetc());
a57977
-		} else
a57977
-		    add(c);
a57977
-	    break;
a57977
-	case '`':
a57977
-	    add(c);
a57977
-	    while ((c = hgetc()) != '`' && !lexstop)
a57977
-		if (c == '\\')
a57977
-		    add(c), add(hgetc());
a57977
-		else
a57977
-		    add(c);
a57977
-	    break;
a57977
-	case '#':
a57977
-	    if (start) {
a57977
-		add(c);
a57977
-		while ((c = hgetc()) != '\n' && !lexstop)
a57977
-		    add(c);
a57977
-		iswhite = 1;
a57977
-	    }
a57977
-	    break;
a57977
+    add(Inpar);
a57977
+
a57977
+    new_lex_add_raw = lex_add_raw + 1;
a57977
+    if (!lex_add_raw) {
a57977
+	/*
a57977
+	 * We'll combine the string so far with the input
a57977
+	 * read in for the command substitution.  To do this
a57977
+	 * we'll just propagate the current tokstr etc. as the
a57977
+	 * variables used for adding raw input, and
a57977
+	 * ensure we swap those for the real tokstr etc. at the end.
a57977
+	 *
a57977
+	 * However, we need to save and restore the rest of the
a57977
+	 * lexical and parse state as we're effectively parsing
a57977
+	 * an internal string.  Because we're still parsing it from
a57977
+	 * the original input source (we have to --- we don't know
a57977
+	 * when to stop inputting it otherwise and can't rely on
a57977
+	 * the input being recoverable until we've read it) we need
a57977
+	 * to keep the same history context.
a57977
+	 */
a57977
+	new_tokstr = tokstr;
a57977
+	new_bptr = bptr;
a57977
+	new_len = len;
a57977
+	new_bsiz = bsiz;
a57977
+
a57977
+	lexsave_partial(ZCONTEXT_LEX|ZCONTEXT_PARSE);
a57977
+    } else {
a57977
+	/*
a57977
+	 * Set up for nested command subsitution, however
a57977
+	 * we don't actually need the string until we get
a57977
+	 * back to the top level and recover the lot.
a57977
+	 * The $() body just appears empty.
a57977
+	 *
a57977
+	 * We do need to propagate the raw variables which would
a57977
+	 * otherwise by cleared, though.
a57977
+	 */
a57977
+	new_tokstr = tokstr_raw;
a57977
+	new_bptr = bptr_raw;
a57977
+	new_len = len_raw;
a57977
+	new_bsiz = bsiz_raw;
a57977
+
a57977
+	lexsave_partial(ZCONTEXT_LEX|ZCONTEXT_PARSE);
a57977
+    }
a57977
+    tokstr_raw = new_tokstr;
a57977
+    bsiz_raw = new_bsiz;
a57977
+    len_raw = new_len;
a57977
+    bptr_raw = new_bptr;
a57977
+    lex_add_raw = new_lex_add_raw;
a57977
+
a57977
+    if (!parse_event(OUTPAR) || tok != OUTPAR)
a57977
+	lexstop = 1;
a57977
+     /* Outpar lexical token gets added in caller if present */
a57977
+
a57977
+    /*
a57977
+     * We're going to keep the full raw input string
a57977
+     * as the current token string after popping the stack.
a57977
+     */
a57977
+    new_tokstr = tokstr_raw;
a57977
+    new_bptr = bptr_raw;
a57977
+    new_len = len_raw;
a57977
+    new_bsiz = bsiz_raw;
a57977
+    /*
a57977
+     * We're also going to propagate the lexical state:
a57977
+     * if we couldn't parse the command substitution we
a57977
+     * can't continue.
a57977
+     */
a57977
+    new_lexstop = lexstop;
a57977
+
a57977
+    lexrestore_partial(ZCONTEXT_LEX|ZCONTEXT_PARSE);
a57977
+
a57977
+    if (lex_add_raw) {
a57977
+	/*
a57977
+	 * Keep going, so retain the raw variables.
a57977
+	 */
a57977
+	tokstr_raw = new_tokstr;
a57977
+	bptr_raw = new_bptr;
a57977
+	len_raw = new_len;
a57977
+	bsiz_raw = new_bsiz;
a57977
+    } else {
a57977
+	if (!new_lexstop) {
a57977
+	    /* Ignore the ')' added on input */
a57977
+	    new_len--;
a57977
+	    *--new_bptr = '\0';
a57977
 	}
a57977
-	start = iswhite;
a57977
+
a57977
+	/*
a57977
+	 * Convince the rest of lex.c we were examining a string
a57977
+	 * all along.
a57977
+	 */
a57977
+	tokstr = new_tokstr;
a57977
+	bptr = new_bptr;
a57977
+	len = new_len;
a57977
+	bsiz = new_bsiz;
a57977
+	lexstop = new_lexstop;
a57977
     }
a57977
-    while (pct);
a57977
+
a57977
     if (!lexstop)
a57977
 	SETPAREND
a57977
     cmdpop();
a57977
+
a57977
     return lexstop;
a57977
 }
a57977
diff --git a/Src/parse.c b/Src/parse.c
a57977
index 753080d..b0a7624 100644
a57977
--- a/Src/parse.c
a57977
+++ b/Src/parse.c
a57977
@@ -360,7 +360,8 @@ ecstrcode(char *s)
a57977
 
a57977
 /* Initialise wordcode buffer. */
a57977
 
a57977
-static void
a57977
+/**/
a57977
+void
a57977
 init_parse(void)
a57977
 {
a57977
     if (ecbuf) zfree(ecbuf, eclen);
a57977
@@ -439,11 +440,15 @@ clear_hdocs()
a57977
  * event	: ENDINPUT
a57977
  *			| SEPER
a57977
  *			| sublist [ SEPER | AMPER | AMPERBANG ]
a57977
+ *
a57977
+ * cmdsubst indicates our event is part of a command-style
a57977
+ * substitution terminated by the token indicationg, usual closing
a57977
+ * parenthesis.  In other cases endtok is ENDINPUT.
a57977
  */
a57977
 
a57977
 /**/
a57977
 Eprog
a57977
-parse_event(void)
a57977
+parse_event(int endtok)
a57977
 {
a57977
     tok = ENDINPUT;
a57977
     incmdpos = 1;
a57977
@@ -451,36 +456,42 @@ parse_event(void)
a57977
     zshlex();
a57977
     init_parse();
a57977
 
a57977
-    if (!par_event()) {
a57977
+    if (!par_event(endtok)) {
a57977
         clear_hdocs();
a57977
         return NULL;
a57977
     }
a57977
+    if (endtok != ENDINPUT) {
a57977
+	/* don't need to build an eprog for this */
a57977
+	return &dummy_eprog;
a57977
+    }
a57977
     return bld_eprog();
a57977
 }
a57977
 
a57977
 /**/
a57977
-static int
a57977
-par_event(void)
a57977
+int
a57977
+par_event(int endtok)
a57977
 {
a57977
     int r = 0, p, c = 0;
a57977
 
a57977
     while (tok == SEPER) {
a57977
-	if (isnewlin > 0)
a57977
+	if (isnewlin > 0 && endtok == ENDINPUT)
a57977
 	    return 0;
a57977
 	zshlex();
a57977
     }
a57977
     if (tok == ENDINPUT)
a57977
 	return 0;
a57977
+    if (tok == endtok)
a57977
+	return 0;
a57977
 
a57977
     p = ecadd(0);
a57977
 
a57977
     if (par_sublist(&c)) {
a57977
-	if (tok == ENDINPUT) {
a57977
+	if (tok == ENDINPUT || tok == endtok) {
a57977
 	    set_list_code(p, Z_SYNC, c);
a57977
 	    r = 1;
a57977
 	} else if (tok == SEPER) {
a57977
 	    set_list_code(p, Z_SYNC, c);
a57977
-	    if (isnewlin <= 0)
a57977
+	    if (isnewlin <= 0 || endtok != ENDINPUT)
a57977
 		zshlex();
a57977
 	    r = 1;
a57977
 	} else if (tok == AMPER) {
a57977
@@ -509,7 +520,7 @@ par_event(void)
a57977
     } else {
a57977
 	int oec = ecused;
a57977
 
a57977
-	if (!par_event()) {
a57977
+	if (!par_event(endtok)) {
a57977
 	    ecused = oec;
a57977
 	    ecbuf[p] |= wc_bdata(Z_END);
a57977
 	}
a57977
diff --git a/Src/zsh.h b/Src/zsh.h
a57977
index 207ef18..b3391ed 100644
a57977
--- a/Src/zsh.h
a57977
+++ b/Src/zsh.h
a57977
@@ -395,6 +395,15 @@ enum {
a57977
 #define META_HEAPDUP	6
a57977
 #define META_HREALLOC	7
a57977
 
a57977
+/* Context to save and restore (bit fields) */
a57977
+enum {
a57977
+    /* History mechanism */
a57977
+    ZCONTEXT_HIST       = (1<<0),
a57977
+    /* Lexical analyser */
a57977
+    ZCONTEXT_LEX        = (1<<1),
a57977
+    /* Parser */
a57977
+    ZCONTEXT_PARSE      = (1<<2)
a57977
+};
a57977
 
a57977
 /**************************/
a57977
 /* Abstract types for zsh */
a57977
diff --git a/Test/D08cmdsubst.ztst b/Test/D08cmdsubst.ztst
a57977
index 5661b0a..a4c69a0 100644
a57977
--- a/Test/D08cmdsubst.ztst
a57977
+++ b/Test/D08cmdsubst.ztst
a57977
@@ -106,3 +106,45 @@
a57977
 >34
a57977
 >"
a57977
 >" OK
a57977
+
a57977
+ echo $(case foo in
a57977
+ foo)
a57977
+ echo This test worked.
a57977
+ ;;
a57977
+ bar)
a57977
+ echo This test failed in a rather bizarre way.
a57977
+ ;;
a57977
+ *)
a57977
+ echo This test failed.
a57977
+ ;;
a57977
+ esac)
a57977
+0:Parsing of command substitution with unmatched parentheses: case, basic
a57977
+>This test worked.
a57977
+
a57977
+ echo "$(case bar in
a57977
+ foo)
a57977
+ echo This test spoobed.
a57977
+ ;;
a57977
+ bar)
a57977
+ echo This test plurbled.
a57977
+ ;;
a57977
+ *)
a57977
+ echo This test bzonked.
a57977
+ ;;
a57977
+ esac)"
a57977
+0:Parsing of command substitution with unmatched parentheses: case with quotes
a57977
+>This test plurbled.
a57977
+
a57977
+ echo before $(
a57977
+ echo start; echo unpretentious |
a57977
+ while read line; do
a57977
+   case $line in
a57977
+   u*)
a57977
+   print Word began with u
a57977
+   print and ended with a crunch
a57977
+   ;;
a57977
+   esac
a57977
+ done | sed -e 's/Word/Universe/'; echo end
a57977
+ ) after
a57977
+0:Parsing of command substitution with ummatched parentheses: with frills
a57977
+>before start Universe began with u and ended with a crunch end after
a57977
-- 
a57977
2.4.3
a57977
a57977
ebecab
From 925112048811087520954e0c739b29371eee188a Mon Sep 17 00:00:00 2001
ebecab
From: Kamil Dudka <kdudka@redhat.com>
ebecab
Date: Thu, 8 Jan 2015 21:39:26 +0000
eb5f7e
Subject: [PATCH 2/9] Resolves: #1338689 - better initialize parser state
ebecab
ebecab
This fix is isolated out from a huge upstream commit that includes major
ebecab
code refactoring changes together with the initialization fix actually
ebecab
needed to resolve #1338689.
ebecab
ebecab
Upstream-commit: cfd91eac0732da8ece012ca4ab051d928a85c9dd
ebecab
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
ebecab
---
ebecab
 Src/hist.c  |  5 +++++
ebecab
 Src/parse.c | 20 ++++++++++++++++++--
ebecab
 2 files changed, 23 insertions(+), 2 deletions(-)
ebecab
ebecab
diff --git a/Src/hist.c b/Src/hist.c
ebecab
index 561e2ac..4ba61b1 100644
ebecab
--- a/Src/hist.c
ebecab
+++ b/Src/hist.c
ebecab
@@ -804,6 +804,11 @@ strinbeg(int dohist)
ebecab
     strin++;
ebecab
     hbegin(dohist);
ebecab
     lexinit();
ebecab
+    /*
ebecab
+     * Also initialise some variables owned by the parser but
ebecab
+     * used for communication between the parser and lexer.
ebecab
+     */
ebecab
+    init_parse_status();
ebecab
 }
ebecab
 
ebecab
 /* done reading a string */
ebecab
diff --git a/Src/parse.c b/Src/parse.c
ebecab
index b0a7624..b3b004b 100644
ebecab
--- a/Src/parse.c
ebecab
+++ b/Src/parse.c
ebecab
@@ -358,6 +358,21 @@ ecstrcode(char *s)
ebecab
     } while (0)
ebecab
 
ebecab
 
ebecab
+/**/
ebecab
+mod_export void
ebecab
+init_parse_status(void)
ebecab
+{
ebecab
+    /*
ebecab
+     * These variables are currently declared by the parser, so we
ebecab
+     * initialise them here.  Possibly they are more naturally declared
ebecab
+     * by the lexical anaylser; however, as they are used for signalling
ebecab
+     * between the two it's a bit ambiguous.  We clear them when
ebecab
+     * using the lexical analyser for strings as well as here.
ebecab
+     */
ebecab
+    incasepat = incond = inredir = infor = 0;
ebecab
+    incmdpos = 1;
ebecab
+}
ebecab
+
ebecab
 /* Initialise wordcode buffer. */
ebecab
 
ebecab
 /**/
ebecab
@@ -372,6 +387,8 @@ init_parse(void)
ebecab
     ecsoffs = ecnpats = 0;
ebecab
     ecssub = 0;
ebecab
     ecnfunc = 0;
ebecab
+
ebecab
+    init_parse_status();
ebecab
 }
ebecab
 
ebecab
 /* Build eprog. */
ebecab
@@ -535,9 +552,8 @@ parse_list(void)
ebecab
     int c = 0;
ebecab
 
ebecab
     tok = ENDINPUT;
ebecab
-    incmdpos = 1;
ebecab
-    zshlex();
ebecab
     init_parse();
ebecab
+    zshlex();
ebecab
     par_list(&c);
ebecab
     if (tok != ENDINPUT) {
ebecab
         clear_hdocs();
ebecab
-- 
ebecab
2.5.5
ebecab
ebecab
a57977
From 00bc31b497525433dbaeafd3e7b92c7fe364dc8c Mon Sep 17 00:00:00 2001
a57977
From: Peter Stephenson <pws@zsh.org>
a57977
Date: Wed, 15 Apr 2015 10:20:06 +0100
eb5f7e
Subject: [PATCH 3/9] 34892 (slightly tweaked): math evaluation fix
a57977
a57977
An empty expression resulting from substitution includes a
a57977
Nularg, which needs handling the same as an empty string.
a57977
a57977
Upstream-commit: 2ef4b38461dfb554ed2226d9de8958703bc00b98
a57977
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
a57977
---
a57977
 Src/math.c         | 15 ++++++++++++++-
a57977
 Test/C01arith.ztst |  4 ++++
a57977
 2 files changed, 18 insertions(+), 1 deletion(-)
a57977
a57977
diff --git a/Src/math.c b/Src/math.c
a57977
index e90d6a5..d6db7d3 100644
a57977
--- a/Src/math.c
a57977
+++ b/Src/math.c
a57977
@@ -1330,7 +1330,7 @@ matheval(char *s)
a57977
     if (!mlevel)
a57977
 	outputradix = 0;
a57977
 
a57977
-    if (!*s) {
a57977
+    if (!*s || *s == Nularg) {
a57977
 	x.type = MN_INTEGER;
a57977
 	x.u.l = 0;
a57977
 	return x;
a57977
@@ -1358,6 +1358,19 @@ mathevalarg(char *s, char **ss)
a57977
     mnumber x;
a57977
     int xmtok = mtok;
a57977
 
a57977
+    /*
a57977
+     * At this entry point we don't allow an empty expression,
a57977
+     * whereas we do with matheval().  I'm not sure if this
a57977
+     * difference is deliberate, but it does mean that e.g.
a57977
+     * $array[$ind] where ind hasn't been set produces an error,
a57977
+     * which is probably safe.
a57977
+     *
a57977
+     * To avoid a more opaque error further in, bail out here.
a57977
+     */
a57977
+    if (!*s || *s == Nularg) {
a57977
+       zerr("bad math expression: empty string");
a57977
+       return (zlong)0;
a57977
+    }
a57977
     x = mathevall(s, MPREC_ARG, ss);
a57977
     if (mtok == COMMA)
a57977
 	(*ss)--;
a57977
diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst
a57977
index 02d1519..33b03ef 100644
a57977
--- a/Test/C01arith.ztst
a57977
+++ b/Test/C01arith.ztst
a57977
@@ -243,3 +243,7 @@
a57977
 >6000000
a57977
 >5000
a57977
 >255
a57977
+
a57977
+  print $((`:`))
a57977
+0:Null string in arithmetic evaluation after command substitution
a57977
+>0
a57977
-- 
a57977
2.4.6
a57977
a57977
a57977
From 0c1450a286e578a1cfe266bf743faf2f0719f85b Mon Sep 17 00:00:00 2001
a57977
From: "Barton E. Schaefer" <schaefer@zsh.org>
a57977
Date: Wed, 29 Jul 2015 22:36:45 -0700
eb5f7e
Subject: [PATCH 4/9] 35953: fix handling of command substitution in math
a57977
 context
a57977
a57977
Upstream-commit: c0a80171ee615b52a15a6fc8efe83c2bb53451d2
a57977
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
a57977
---
a57977
 Src/lex.c            | 6 +++++-
a57977
 Test/A01grammar.ztst | 6 ++++++
a57977
 2 files changed, 11 insertions(+), 1 deletion(-)
a57977
a57977
diff --git a/Src/lex.c b/Src/lex.c
a57977
index bcceda6..f43b92b 100644
a57977
--- a/Src/lex.c
a57977
+++ b/Src/lex.c
a57977
@@ -1541,7 +1541,7 @@ dquote_parse(char endchar, int sub)
a57977
 {
a57977
     int pct = 0, brct = 0, bct = 0, intick = 0, err = 0;
a57977
     int c;
a57977
-    int math = endchar == ')' || endchar == ']';
a57977
+    int math = endchar == ')' || endchar == ']' || infor;
a57977
     int zlemath = math && zlemetacs > zlemetall + addedx - inbufct;
a57977
 
a57977
     while (((c = hgetc()) != endchar || bct ||
a57977
@@ -2004,7 +2004,9 @@ skipcomm(void)
a57977
 {
a57977
     char *new_tokstr, *new_bptr = bptr_raw;
a57977
     int new_len, new_bsiz, new_lexstop, new_lex_add_raw;
a57977
+    int save_infor = infor;
a57977
 
a57977
+    infor = 0;
a57977
     cmdpush(CS_CMDSUBST);
a57977
     SETPARBEGIN
a57977
     add(Inpar);
a57977
@@ -2054,6 +2056,7 @@ skipcomm(void)
a57977
     len_raw = new_len;
a57977
     bptr_raw = new_bptr;
a57977
     lex_add_raw = new_lex_add_raw;
a57977
+    dbparens = 0;	/* restored by zcontext_restore_partial() */
a57977
 
a57977
     if (!parse_event(OUTPAR) || tok != OUTPAR)
a57977
 	lexstop = 1;
a57977
@@ -2105,6 +2108,7 @@ skipcomm(void)
a57977
     if (!lexstop)
a57977
 	SETPAREND
a57977
     cmdpop();
a57977
+    infor = save_infor;
a57977
 
a57977
     return lexstop;
a57977
 }
a57977
diff --git a/Test/A01grammar.ztst b/Test/A01grammar.ztst
a57977
index f04ddda..584ebd6 100644
a57977
--- a/Test/A01grammar.ztst
a57977
+++ b/Test/A01grammar.ztst
a57977
@@ -169,6 +169,12 @@
a57977
 >1
a57977
 >2
a57977
 
a57977
+  for (( $(true); ; )); do break; done
a57977
+  for (( ; $(true); )); do break; done
a57977
+  for (( ; ; $(true) )); do break; done
a57977
+  for (( ; $((1)); )); do break; done
a57977
+0:regression test, nested cmdsubst in arithmetic `for' loop
a57977
+
a57977
   for keyvar valvar in key1 val1 key2 val2; do
a57977
      print key=$keyvar val=$valvar
a57977
   done
a57977
-- 
a57977
2.4.6
a57977
a57977
a57977
From 821815bd9c24a84d8bb5796732ab6144b35e7d27 Mon Sep 17 00:00:00 2001
a57977
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
a57977
Date: Sat, 10 Jan 2015 20:28:57 +0000
eb5f7e
Subject: [PATCH 5/9] 34220: new $(...) handling needs to back up over alias
a57977
 expansion
a57977
a57977
Upstream-commit: 3b32abafdb019cfb8f29908bc3d148e01518981d
a57977
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
a57977
---
a57977
 Src/input.c | 6 ++++++
a57977
 1 file changed, 6 insertions(+)
a57977
a57977
diff --git a/Src/input.c b/Src/input.c
a57977
index 1579762..5b782dc 100644
a57977
--- a/Src/input.c
a57977
+++ b/Src/input.c
a57977
@@ -532,6 +532,12 @@ inpush(char *str, int flags, Alias inalias)
a57977
 static void
a57977
 inpoptop(void)
a57977
 {
a57977
+    if (!lexstop) {
a57977
+	inbufflags &= ~INP_ALCONT;
a57977
+	while (inbufptr > inbuf)
a57977
+	    inungetc(inbufptr[-1]);
a57977
+    }
a57977
+
a57977
     if (inbuf && (inbufflags & INP_FREE))
a57977
 	free(inbuf);
a57977
 
a57977
-- 
a57977
2.4.6
a57977
c6dbd9
c6dbd9
From 1c731b7d1178a2623aa1b986f38a7decebf2c993 Mon Sep 17 00:00:00 2001
c6dbd9
From: Peter Stephenson <pws@zsh.org>
c6dbd9
Date: Fri, 16 Jan 2015 13:20:05 +0000
eb5f7e
Subject: [PATCH 6/9] 34304: improve use of new cmd subst in completion
c6dbd9
c6dbd9
Upstream-commit: db05cc51fa2298cf128e480d3ac8e5373029f6b9
c6dbd9
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c6dbd9
---
c6dbd9
 Src/lex.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
c6dbd9
 1 file changed, 103 insertions(+), 10 deletions(-)
c6dbd9
c6dbd9
diff --git a/Src/lex.c b/Src/lex.c
c6dbd9
index f43b92b..45c1117 100644
c6dbd9
--- a/Src/lex.c
c6dbd9
+++ b/Src/lex.c
c6dbd9
@@ -88,6 +88,12 @@ int inalmore;
c6dbd9
 int nocorrect;
c6dbd9
 
c6dbd9
 /*
c6dbd9
+ * TBD: the following exported variables are part of the non-interface
c6dbd9
+ * with ZLE for completion.  They are poorly named and the whole
c6dbd9
+ * scheme is incredibly brittle.  One piece of robustness is applied:
c6dbd9
+ * the variables are only set if LEXFLAGS_ZLE is set.  Improvements
c6dbd9
+ * should therefore concentrate on areas with this flag set.
c6dbd9
+ *
c6dbd9
  * Cursor position and line length in zle when the line is
c6dbd9
  * metafied for access from the main shell.
c6dbd9
  */
c6dbd9
@@ -111,6 +117,16 @@ mod_export int addedx;
c6dbd9
 /**/
c6dbd9
 mod_export int wb, we;
c6dbd9
 
c6dbd9
+/**/
c6dbd9
+mod_export int wordbeg;
c6dbd9
+
c6dbd9
+/**/
c6dbd9
+mod_export int parbegin;
c6dbd9
+
c6dbd9
+/**/
c6dbd9
+mod_export int parend;
c6dbd9
+
c6dbd9
+
c6dbd9
 /* 1 if aliases should not be expanded */
c6dbd9
 
c6dbd9
 /**/
c6dbd9
@@ -132,15 +148,6 @@ mod_export int noaliases;
c6dbd9
 /**/
c6dbd9
 mod_export int lexflags;
c6dbd9
 
c6dbd9
-/**/
c6dbd9
-mod_export int wordbeg;
c6dbd9
-
c6dbd9
-/**/
c6dbd9
-mod_export int parbegin;
c6dbd9
-
c6dbd9
-/**/
c6dbd9
-mod_export int parend;
c6dbd9
-
c6dbd9
 /* don't recognize comments */
c6dbd9
 
c6dbd9
 /**/
c6dbd9
@@ -791,7 +798,8 @@ gettok(void)
c6dbd9
     if (lexstop)
c6dbd9
 	return (errflag) ? LEXERR : ENDINPUT;
c6dbd9
     isfirstln = 0;
c6dbd9
-    wordbeg = inbufct - (qbang && c == bangchar);
c6dbd9
+    if ((lexflags & LEXFLAGS_ZLE))
c6dbd9
+	wordbeg = inbufct - (qbang && c == bangchar);
c6dbd9
     hwbegin(-1-(qbang && c == bangchar));
c6dbd9
     /* word includes the last character read and possibly \ before ! */
c6dbd9
     if (dbparens) {
c6dbd9
@@ -2002,6 +2010,78 @@ zshlex_raw_back(void)
c6dbd9
 static int
c6dbd9
 skipcomm(void)
c6dbd9
 {
c6dbd9
+#ifdef ZSH_OLD_SKIPCOMM
c6dbd9
+    int pct = 1, c, start = 1;
c6dbd9
+
c6dbd9
+    cmdpush(CS_CMDSUBST);
c6dbd9
+    SETPARBEGIN
c6dbd9
+    c = Inpar;
c6dbd9
+    do {
c6dbd9
+	int iswhite;
c6dbd9
+	add(c);
c6dbd9
+	c = hgetc();
c6dbd9
+	if (itok(c) || lexstop)
c6dbd9
+	    break;
c6dbd9
+	iswhite = inblank(c);
c6dbd9
+	switch (c) {
c6dbd9
+	case '(':
c6dbd9
+	    pct++;
c6dbd9
+	    break;
c6dbd9
+	case ')':
c6dbd9
+	    pct--;
c6dbd9
+	    break;
c6dbd9
+	case '\\':
c6dbd9
+	    add(c);
c6dbd9
+	    c = hgetc();
c6dbd9
+	    break;
c6dbd9
+	case '\'': {
c6dbd9
+	    int strquote = lexbuf.ptr[-1] == '$';
c6dbd9
+	    add(c);
c6dbd9
+	    STOPHIST
c6dbd9
+	    while ((c = hgetc()) != '\'' && !lexstop) {
c6dbd9
+		if (c == '\\' && strquote) {
c6dbd9
+		    add(c);
c6dbd9
+		    c = hgetc();
c6dbd9
+		}
c6dbd9
+		add(c);
c6dbd9
+	    }
c6dbd9
+	    ALLOWHIST
c6dbd9
+	    break;
c6dbd9
+	}
c6dbd9
+	case '\"':
c6dbd9
+	    add(c);
c6dbd9
+	    while ((c = hgetc()) != '\"' && !lexstop)
c6dbd9
+		if (c == '\\') {
c6dbd9
+		    add(c);
c6dbd9
+		    add(hgetc());
c6dbd9
+		} else
c6dbd9
+		    add(c);
c6dbd9
+	    break;
c6dbd9
+	case '`':
c6dbd9
+	    add(c);
c6dbd9
+	    while ((c = hgetc()) != '`' && !lexstop)
c6dbd9
+		if (c == '\\')
c6dbd9
+		    add(c), add(hgetc());
c6dbd9
+		else
c6dbd9
+		    add(c);
c6dbd9
+	    break;
c6dbd9
+	case '#':
c6dbd9
+	    if (start) {
c6dbd9
+		add(c);
c6dbd9
+		while ((c = hgetc()) != '\n' && !lexstop)
c6dbd9
+		    add(c);
c6dbd9
+		iswhite = 1;
c6dbd9
+	    }
c6dbd9
+	    break;
c6dbd9
+	}
c6dbd9
+	start = iswhite;
c6dbd9
+    }
c6dbd9
+    while (pct);
c6dbd9
+    if (!lexstop)
c6dbd9
+	SETPAREND
c6dbd9
+    cmdpop();
c6dbd9
+    return lexstop;
c6dbd9
+#else
c6dbd9
     char *new_tokstr, *new_bptr = bptr_raw;
c6dbd9
     int new_len, new_bsiz, new_lexstop, new_lex_add_raw;
c6dbd9
     int save_infor = infor;
c6dbd9
@@ -2057,6 +2137,18 @@ skipcomm(void)
c6dbd9
     bptr_raw = new_bptr;
c6dbd9
     lex_add_raw = new_lex_add_raw;
c6dbd9
     dbparens = 0;	/* restored by zcontext_restore_partial() */
c6dbd9
+    /*
c6dbd9
+     * Don't do any ZLE specials down here: they're only needed
c6dbd9
+     * when we return the string from the recursive parse.
c6dbd9
+     * (TBD: this probably means we should be initialising lexflags
c6dbd9
+     * more consistently.)
c6dbd9
+     *
c6dbd9
+     * Note that in that case we're still using the ZLE line reading
c6dbd9
+     * function at the history layer --- this is consistent with the
c6dbd9
+     * intention of maintaining the history and input layers across
c6dbd9
+     * the recursive parsing.
c6dbd9
+     */
c6dbd9
+    lexflags &= ~LEXFLAGS_ZLE;
c6dbd9
 
c6dbd9
     if (!parse_event(OUTPAR) || tok != OUTPAR)
c6dbd9
 	lexstop = 1;
c6dbd9
@@ -2111,4 +2203,5 @@ skipcomm(void)
c6dbd9
     infor = save_infor;
c6dbd9
 
c6dbd9
     return lexstop;
c6dbd9
+#endif
c6dbd9
 }
c6dbd9
-- 
c6dbd9
2.5.5
c6dbd9
c6dbd9
c6dbd9
From ad64470d3ea4190cd854aab2bc0f8d01ec6aef11 Mon Sep 17 00:00:00 2001
c6dbd9
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
c6dbd9
Date: Fri, 16 Jan 2015 20:12:40 +0000
eb5f7e
Subject: [PATCH 7/9] 32413: turn off history word marking in cmd subst
c6dbd9
c6dbd9
Upstream-commit: f2a2f28f7bde196cd1fa205ac0c20336046cf2cf
c6dbd9
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c6dbd9
---
c6dbd9
 Src/hist.c | 22 ++++++++++++++++++++--
c6dbd9
 Src/lex.c  |  2 ++
c6dbd9
 2 files changed, 22 insertions(+), 2 deletions(-)
c6dbd9
c6dbd9
diff --git a/Src/hist.c b/Src/hist.c
c6dbd9
index 561e2ac..e29a566 100644
c6dbd9
--- a/Src/hist.c
c6dbd9
+++ b/Src/hist.c
c6dbd9
@@ -131,6 +131,8 @@ mod_export int hist_skip_flags;
c6dbd9
 /* Bits of histactive variable */
c6dbd9
 #define HA_ACTIVE	(1<<0)	/* History mechanism is active */
c6dbd9
 #define HA_NOINC	(1<<1)	/* Don't store, curhist not incremented */
c6dbd9
+#define HA_INWORD       (1<<2)  /* We're inside a word, don't add
c6dbd9
+				   start and end markers */
c6dbd9
 
c6dbd9
 /* Array of word beginnings and endings in current history line. */
c6dbd9
 
c6dbd9
@@ -219,6 +221,22 @@ static int histsave_stack_pos = 0;
c6dbd9
 
c6dbd9
 static zlong histfile_linect;
c6dbd9
 
c6dbd9
+/*
c6dbd9
+ * Mark that the current level of history is or is not
c6dbd9
+ * within a word, whatever turns up.  This is used for nested
c6dbd9
+ * parsing of substitutions.
c6dbd9
+ */
c6dbd9
+
c6dbd9
+/**/
c6dbd9
+void
c6dbd9
+hist_in_word(int yesno)
c6dbd9
+{
c6dbd9
+    if (yesno)
c6dbd9
+	histactive |= HA_INWORD;
c6dbd9
+    else
c6dbd9
+	histactive &= ~HA_INWORD;
c6dbd9
+}
c6dbd9
+
c6dbd9
 /* add a character to the current history word */
c6dbd9
 
c6dbd9
 static void
c6dbd9
@@ -1329,7 +1347,7 @@ int hwgetword = -1;
c6dbd9
 void
c6dbd9
 ihwbegin(int offset)
c6dbd9
 {
c6dbd9
-    if (stophist == 2)
c6dbd9
+    if (stophist == 2 || (histactive & HA_INWORD))
c6dbd9
 	return;
c6dbd9
     if (chwordpos%2)
c6dbd9
 	chwordpos--;	/* make sure we're on a word start, not end */
c6dbd9
@@ -1349,7 +1367,7 @@ ihwbegin(int offset)
c6dbd9
 void
c6dbd9
 ihwend(void)
c6dbd9
 {
c6dbd9
-    if (stophist == 2)
c6dbd9
+    if (stophist == 2 || (histactive & HA_INWORD))
c6dbd9
 	return;
c6dbd9
     if (chwordpos%2 && chline) {
c6dbd9
 	/* end of word reached and we've already begun a word */
c6dbd9
diff --git a/Src/lex.c b/Src/lex.c
c6dbd9
index f43b92b..f1aa85d 100644
c6dbd9
--- a/Src/lex.c
c6dbd9
+++ b/Src/lex.c
c6dbd9
@@ -2114,6 +2114,7 @@ skipcomm(void)
c6dbd9
 	new_bsiz = bsiz;
c6dbd9
 
c6dbd9
 	lexsave_partial(ZCONTEXT_LEX|ZCONTEXT_PARSE);
c6dbd9
+	hist_in_word(1);
c6dbd9
     } else {
c6dbd9
 	/*
c6dbd9
 	 * Set up for nested command subsitution, however
c6dbd9
@@ -2195,6 +2196,7 @@ skipcomm(void)
c6dbd9
 	len = new_len;
c6dbd9
 	bsiz = new_bsiz;
c6dbd9
 	lexstop = new_lexstop;
c6dbd9
+	hist_in_word(0);
c6dbd9
     }
c6dbd9
 
c6dbd9
     if (!lexstop)
c6dbd9
-- 
c6dbd9
2.5.5
c6dbd9
c6dbd9
c6dbd9
From 22b063c5f2bb3350c856215e436a62d25440e605 Mon Sep 17 00:00:00 2001
c6dbd9
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
c6dbd9
Date: Sun, 18 Jan 2015 16:43:26 +0000
eb5f7e
Subject: [PATCH 8/9] 34319: fix alias expansion in history for command
c6dbd9
 substitution
c6dbd9
c6dbd9
Upstream-commit: e34ce85151dcd5ac698e116a6742d481ff64ae2c
c6dbd9
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
c6dbd9
---
c6dbd9
 Src/hist.c | 26 ++++++++++++++++++++------
c6dbd9
 1 file changed, 20 insertions(+), 6 deletions(-)
c6dbd9
c6dbd9
diff --git a/Src/hist.c b/Src/hist.c
c6dbd9
index e29a566..08763fe 100644
c6dbd9
--- a/Src/hist.c
c6dbd9
+++ b/Src/hist.c
c6dbd9
@@ -243,7 +243,16 @@ static void
c6dbd9
 ihwaddc(int c)
c6dbd9
 {
c6dbd9
     /* Only if history line exists and lexing has not finished. */
c6dbd9
-    if (chline && !(errflag || lexstop)) {
c6dbd9
+    if (chline && !(errflag || lexstop) &&
c6dbd9
+	/*
c6dbd9
+	 * If we're reading inside a word for command substitution
c6dbd9
+	 * we allow the lexer to expand aliases but don't deal
c6dbd9
+	 * with them here.  Note matching code in ihungetc().
c6dbd9
+	 * TBD: it might be neater to deal with all aliases in this
c6dbd9
+	 * fashion as we never need the expansion in the history
c6dbd9
+	 * line, only in the lexer and above.
c6dbd9
+	 */
c6dbd9
+	!((histactive & HA_INWORD) && (inbufflags & INP_ALIAS))) {
c6dbd9
 	/* Quote un-expanded bangs in the history line. */
c6dbd9
 	if (c == bangchar && stophist < 2 && qbang)
c6dbd9
 	    /* If qbang is not set, we do not escape this bangchar as it's *
c6dbd9
@@ -798,11 +807,16 @@ ihungetc(int c)
c6dbd9
 	    zlemetall--;
c6dbd9
 	    exlast++;
c6dbd9
 	}
c6dbd9
-	DPUTS(hptr <= chline, "BUG: hungetc attempted at buffer start");
c6dbd9
-	hptr--;
c6dbd9
-	DPUTS(*hptr != (char) c, "BUG: wrong character in hungetc() ");
c6dbd9
-	qbang = (c == bangchar && stophist < 2 &&
c6dbd9
-		 hptr > chline && hptr[-1] == '\\');
c6dbd9
+	if (!(histactive & HA_INWORD) || !(inbufflags & INP_ALIAS)) {
c6dbd9
+	    DPUTS(hptr <= chline, "BUG: hungetc attempted at buffer start");
c6dbd9
+	    hptr--;
c6dbd9
+	    DPUTS(*hptr != (char) c, "BUG: wrong character in hungetc() ");
c6dbd9
+	    qbang = (c == bangchar && stophist < 2 &&
c6dbd9
+		     hptr > chline && hptr[-1] == '\\');
c6dbd9
+	} else {
c6dbd9
+	    /* No active bangs in aliases */
c6dbd9
+	    qbang = 0;
c6dbd9
+	}
c6dbd9
 	if (doit)
c6dbd9
 	    inungetc(c);
c6dbd9
 	if (!qbang)
c6dbd9
-- 
c6dbd9
2.5.5
c6dbd9
eb5f7e
eb5f7e
From 155587d13060e4c7c9bbd61b7cc0a6dd17922d56 Mon Sep 17 00:00:00 2001
eb5f7e
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
eb5f7e
Date: Sun, 18 Jan 2015 22:38:57 +0000
eb5f7e
Subject: [PATCH 9/9] 34322: bug with interface to parsestr() etc.
eb5f7e
eb5f7e
Was showing up in places like ${(e)...} where command substitution
eb5f7e
could reallocate the token string, but actually there was never any
eb5f7e
guarantee that the lexer wouldn't do that, so this was always
eb5f7e
a bit iffy.
eb5f7e
eb5f7e
Upstream-commit: c6c9f5daf2e196e6ab7346dfbf5f5166a1d87f0c
eb5f7e
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
eb5f7e
---
eb5f7e
 Src/Zle/compctl.c      |  4 ++--
eb5f7e
 Src/Zle/compresult.c   |  3 ++-
eb5f7e
 Src/exec.c             |  9 +++++----
eb5f7e
 Src/init.c             | 11 +++++++----
eb5f7e
 Src/lex.c              | 30 +++++++++++++++++++++---------
eb5f7e
 Src/params.c           |  3 ++-
eb5f7e
 Src/prompt.c           |  2 +-
eb5f7e
 Src/subst.c            |  8 +++++---
eb5f7e
 Src/utils.c            |  2 +-
eb5f7e
 Test/D04parameter.ztst |  7 +++++++
eb5f7e
 10 files changed, 53 insertions(+), 26 deletions(-)
eb5f7e
eb5f7e
diff --git a/Src/Zle/compctl.c b/Src/Zle/compctl.c
eb5f7e
index 0143370..5d67137 100644
eb5f7e
--- a/Src/Zle/compctl.c
eb5f7e
+++ b/Src/Zle/compctl.c
eb5f7e
@@ -3854,7 +3854,7 @@ makecomplistflags(Compctl cc, char *s, int incmd, int compadd)
eb5f7e
 	    yaptr = get_user_var(uv);
eb5f7e
 	if ((tt = cc->explain)) {
eb5f7e
 	    tt = dupstring(tt);
eb5f7e
-	    if ((cc->mask & CC_EXPANDEXPL) && !parsestr(tt)) {
eb5f7e
+	    if ((cc->mask & CC_EXPANDEXPL) && !parsestr(&tt)) {
eb5f7e
 		singsub(&tt;;
eb5f7e
 		untokenize(tt);
eb5f7e
 	    }
eb5f7e
@@ -3874,7 +3874,7 @@ makecomplistflags(Compctl cc, char *s, int incmd, int compadd)
eb5f7e
 	}
eb5f7e
     } else if ((tt = cc->explain)) {
eb5f7e
 	tt = dupstring(tt);
eb5f7e
-	if ((cc->mask & CC_EXPANDEXPL) && !parsestr(tt)) {
eb5f7e
+	if ((cc->mask & CC_EXPANDEXPL) && !parsestr(&tt)) {
eb5f7e
 	    singsub(&tt;;
eb5f7e
 	    untokenize(tt);
eb5f7e
 	}
eb5f7e
diff --git a/Src/Zle/compresult.c b/Src/Zle/compresult.c
eb5f7e
index c0e5ff3..69d066c 100644
eb5f7e
--- a/Src/Zle/compresult.c
eb5f7e
+++ b/Src/Zle/compresult.c
eb5f7e
@@ -1090,7 +1090,8 @@ do_single(Cmatch m)
eb5f7e
 		    }
eb5f7e
 		    if (tryit) {
eb5f7e
 			noerrs = 1;
eb5f7e
-			parsestr(p);
eb5f7e
+			p = dupstring(p);
eb5f7e
+			parsestr(&p);
eb5f7e
 			singsub(&p);
eb5f7e
 			errflag = 0;
eb5f7e
 			noerrs = ne;
eb5f7e
diff --git a/Src/exec.c b/Src/exec.c
eb5f7e
index 7817a64..27e235f 100644
eb5f7e
--- a/Src/exec.c
eb5f7e
+++ b/Src/exec.c
eb5f7e
@@ -3631,17 +3631,18 @@ gethere(char **strp, int typ)
eb5f7e
 	*bptr++ = '\n';
eb5f7e
     }
eb5f7e
     *t = '\0';
eb5f7e
+    s = buf;
eb5f7e
+    buf = dupstring(buf);
eb5f7e
+    zfree(s, bsiz);
eb5f7e
     if (!qt) {
eb5f7e
 	int ef = errflag;
eb5f7e
 
eb5f7e
-	parsestr(buf);
eb5f7e
+	parsestr(&buf;;
eb5f7e
 
eb5f7e
 	if (!errflag)
eb5f7e
 	    errflag = ef;
eb5f7e
     }
eb5f7e
-    s = dupstring(buf);
eb5f7e
-    zfree(buf, bsiz);
eb5f7e
-    return s;
eb5f7e
+    return buf;
eb5f7e
 }
eb5f7e
 
eb5f7e
 /* open here string fd */
eb5f7e
diff --git a/Src/init.c b/Src/init.c
eb5f7e
index 78f171d..485fb32 100644
eb5f7e
--- a/Src/init.c
eb5f7e
+++ b/Src/init.c
eb5f7e
@@ -1143,10 +1143,13 @@ run_init_scripts(void)
eb5f7e
 	    if (islogin)
eb5f7e
 		sourcehome(".profile");
eb5f7e
 	    noerrs = 2;
eb5f7e
-	    if (s && !parsestr(s)) {
eb5f7e
-		singsub(&s);
eb5f7e
-		noerrs = 0;
eb5f7e
-		source(s);
eb5f7e
+	    if (s) {
eb5f7e
+		s = dupstring(s);
eb5f7e
+		if (!parsestr(&s)) {
eb5f7e
+		    singsub(&s);
eb5f7e
+		    noerrs = 0;
eb5f7e
+		    source(s);
eb5f7e
+		}
eb5f7e
 	    }
eb5f7e
 	    noerrs = 0;
eb5f7e
 	} else
eb5f7e
diff --git a/Src/lex.c b/Src/lex.c
eb5f7e
index b8fe332..fa920bd 100644
eb5f7e
--- a/Src/lex.c
eb5f7e
+++ b/Src/lex.c
eb5f7e
@@ -1693,17 +1693,27 @@ dquote_parse(char endchar, int sub)
eb5f7e
     return err;
eb5f7e
 }
eb5f7e
 
eb5f7e
-/* Tokenize a string given in s. Parsing is done as in double *
eb5f7e
- * quotes.  This is usually called before singsub().          */
eb5f7e
+/*
eb5f7e
+ * Tokenize a string given in s. Parsing is done as in double
eb5f7e
+ * quotes.  This is usually called before singsub().
eb5f7e
+ *
eb5f7e
+ * parsestr() is noisier, reporting an error if the parse failed.
eb5f7e
+ *
eb5f7e
+ * On entry, *s must point to a string allocated from the stack of
eb5f7e
+ * exactly the right length, i.e. strlen(*s) + 1, as the string
eb5f7e
+ * is used as the lexical token string whose memory management
eb5f7e
+ * demands this.  Usually the input string will therefore be
eb5f7e
+ * the result of an immediately preceding dupstring().
eb5f7e
+ */
eb5f7e
 
eb5f7e
 /**/
eb5f7e
 mod_export int
eb5f7e
-parsestr(char *s)
eb5f7e
+parsestr(char **s)
eb5f7e
 {
eb5f7e
     int err;
eb5f7e
 
eb5f7e
     if ((err = parsestrnoerr(s))) {
eb5f7e
-	untokenize(s);
eb5f7e
+	untokenize(*s);
eb5f7e
 	if (err > 32 && err < 127)
eb5f7e
 	    zerr("parse error near `%c'", err);
eb5f7e
 	else
eb5f7e
@@ -1714,18 +1724,20 @@ parsestr(char *s)
eb5f7e
 
eb5f7e
 /**/
eb5f7e
 mod_export int
eb5f7e
-parsestrnoerr(char *s)
eb5f7e
+parsestrnoerr(char **s)
eb5f7e
 {
eb5f7e
-    int l = strlen(s), err;
eb5f7e
+    int l = strlen(*s), err;
eb5f7e
 
eb5f7e
     lexsave();
eb5f7e
-    untokenize(s);
eb5f7e
-    inpush(dupstring(s), 0, NULL);
eb5f7e
+    untokenize(*s);
eb5f7e
+    inpush(dupstring(*s), 0, NULL);
eb5f7e
     strinbeg(0);
eb5f7e
     len = 0;
eb5f7e
-    bptr = tokstr = s;
eb5f7e
+    bptr = tokstr = *s;
eb5f7e
     bsiz = l + 1;
eb5f7e
     err = dquote_parse('\0', 1);
eb5f7e
+    if (tokstr)
eb5f7e
+	*s = tokstr;
eb5f7e
     *bptr = '\0';
eb5f7e
     strinend();
eb5f7e
     inpop();
eb5f7e
diff --git a/Src/params.c b/Src/params.c
eb5f7e
index babf6f2..f7551b2 100644
eb5f7e
--- a/Src/params.c
eb5f7e
+++ b/Src/params.c
eb5f7e
@@ -1241,7 +1241,8 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
eb5f7e
     if (ishash && (keymatch || !rev))
eb5f7e
 	remnulargs(s);
eb5f7e
     if (needtok) {
eb5f7e
-	if (parsestr(s))
eb5f7e
+	s = dupstring(s);
eb5f7e
+	if (parsestr(&s))
eb5f7e
 	    return 0;
eb5f7e
 	singsub(&s);
eb5f7e
     } else if (rev)
eb5f7e
diff --git a/Src/prompt.c b/Src/prompt.c
eb5f7e
index e51ce24..290f227 100644
eb5f7e
--- a/Src/prompt.c
eb5f7e
+++ b/Src/prompt.c
eb5f7e
@@ -183,7 +183,7 @@ promptexpand(char *s, int ns, char *rs, char *Rs, unsigned int *txtchangep)
eb5f7e
 	int oldval = lastval;
eb5f7e
 
eb5f7e
 	s = dupstring(s);
eb5f7e
-	if (!parsestr(s))
eb5f7e
+	if (!parsestr(&s))
eb5f7e
 	    singsub(&s);
eb5f7e
 	/*
eb5f7e
 	 * We don't need the special Nularg hack here and we're
eb5f7e
diff --git a/Src/subst.c b/Src/subst.c
eb5f7e
index a4df256..dcffe2f 100644
eb5f7e
--- a/Src/subst.c
eb5f7e
+++ b/Src/subst.c
eb5f7e
@@ -1306,7 +1306,7 @@ get_intarg(char **s, int *delmatchp)
eb5f7e
     p = dupstring(*s + arglen);
eb5f7e
     *s = t + arglen;
eb5f7e
     *t = sav;
eb5f7e
-    if (parsestr(p))
eb5f7e
+    if (parsestr(&p))
eb5f7e
 	return -1;
eb5f7e
     singsub(&p);
eb5f7e
     if (errflag)
eb5f7e
@@ -1329,7 +1329,8 @@ subst_parse_str(char **sp, int single, int err)
eb5f7e
 
eb5f7e
     *sp = s = dupstring(*sp);
eb5f7e
 
eb5f7e
-    if (!(err ? parsestr(s) : parsestrnoerr(s))) {
eb5f7e
+    if (!(err ? parsestr(&s) : parsestrnoerr(&s))) {
eb5f7e
+	*sp = s;
eb5f7e
 	if (!single) {
eb5f7e
             int qt = 0;
eb5f7e
 
eb5f7e
@@ -1426,7 +1427,8 @@ check_colon_subscript(char *str, char **endp)
eb5f7e
     }
eb5f7e
     sav = **endp;
eb5f7e
     **endp = '\0';
eb5f7e
-    if (parsestr(str = dupstring(str)))
eb5f7e
+    str = dupstring(str);
eb5f7e
+    if (parsestr(&str))
eb5f7e
 	return NULL;
eb5f7e
     singsub(&str);
eb5f7e
     remnulargs(str);
eb5f7e
diff --git a/Src/utils.c b/Src/utils.c
eb5f7e
index 26e2a5c..2c1d034 100644
eb5f7e
--- a/Src/utils.c
eb5f7e
+++ b/Src/utils.c
eb5f7e
@@ -1440,7 +1440,7 @@ checkmailpath(char **s)
eb5f7e
 		    setunderscore(*s);
eb5f7e
 
eb5f7e
 		    u = dupstring(u);
eb5f7e
-		    if (! parsestr(u)) {
eb5f7e
+		    if (!parsestr(&u)) {
eb5f7e
 			singsub(&u);
eb5f7e
 			zputs(u, shout);
eb5f7e
 			fputc('\n', shout);
eb5f7e
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
eb5f7e
index bea9459..fa629b2 100644
eb5f7e
--- a/Test/D04parameter.ztst
eb5f7e
+++ b/Test/D04parameter.ztst
eb5f7e
@@ -1551,3 +1551,10 @@
eb5f7e
 0:Empty parameter shouldn't cause modifiers to crash the shell
eb5f7e
 >
eb5f7e
 >
eb5f7e
+
eb5f7e
+# The following tests the return behaviour of parsestr/parsestrnoerr
eb5f7e
+  alias param-test-alias='print $'\''\x45xpanded in substitution'\'
eb5f7e
+  param='$(param-test-alias)'
eb5f7e
+  print ${(e)param}
eb5f7e
+0:Alias expansion in command substitution in parameter evaluation
eb5f7e
+>Expanded in substitution
eb5f7e
-- 
eb5f7e
2.7.4
eb5f7e