Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: "var=value exec > file" ignores the assignment?



On Fri, 10 Apr 2015 01:13:50 +0900
"Jun T." <takimoto-j@xxxxxxxxxxxxxxxxx> wrote:
> 2015/04/09 02:13, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> 
> > It's possible (likely?) your patch should add a POSIX_BUILTINS test.
> 
> In the following case:
> 
> x=$(command) exec > file
> 
> we want to execute the 'command' unconditionally, but maybe do not want to
> set the value of x if POSIX_BUILTINS is unset.
> I wonder what is the easiest way (if any) to achieve this.

This (based on the previous patch) might be good enough.  It could
probably do with a couple of tests.

diff --git a/Src/exec.c b/Src/exec.c
index 2ee37d0..2a8185c 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -3305,6 +3305,20 @@ execcmd(Estate state, int input, int output, int how, int last1)
 	    closemn(mfds, i, REDIR_CLOSE);
 
     if (nullexec) {
+	/*
+	 * If nullexec is 2, we have variables to add with the redirections
+	 * in place.  If nullexec is 1, we may have variables but they
+	 * need the standard restore logic.
+	 */
+	if (varspc) {
+	    LinkList restorelist = 0, removelist = 0;
+	    if (!isset(POSIXBUILTINS) && nullexec != 2)
+		save_params(state, varspc, &restorelist, &removelist);
+	    addvars(state, varspc, 0);
+	    if (restorelist)
+		restore_params(restorelist, removelist);
+	}
+	lastval = errflag ? errflag : cmdoutval;
 	if (nullexec == 1) {
 	    /*
 	     * If nullexec is 1 we specifically *don't* restore the original
@@ -3315,13 +3329,6 @@ execcmd(Estate state, int input, int output, int how, int last1)
 		    zclose(save[i]);
 	    goto done;
 	}
-	/*
-	 * If nullexec is 2, we have variables to add with the redirections
-	 * in place.
-	 */
-	if (varspc)
-	    addvars(state, varspc, 0);
-	lastval = errflag ? errflag : cmdoutval;
 	if (isset(XTRACE)) {
 	    fputc('\n', xtrerr);
 	    fflush(xtrerr);



Messages sorted by: Reverse Date, Date, Thread, Author