zsh-workers
 help / color / mirror / code / Atom feed
* "var=value exec > file" ignores the assignment?
@ 2015-04-07 15:51 Jun T.
  2015-04-08 17:13 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Jun T. @ 2015-04-07 15:51 UTC (permalink / raw)
  To: zsh-workers

If 'exec' has no command arg but has both parameter assignment
and redirection, the assignment seems to be ignored.

The following script outputs "x = 0" into test.log:

#!/usr/local/bin/zsh
setopt POSIX_BUILTINS 
x=0
x=1 exec > test.log
echo "x = $x"

'ksh' and 'bash -posix' (or /bin/sh) output "x = 1", which I
guess what POSIX requires.

Moreover, assuming there is no command named 'junk',

x=$(junk) exec > test.log

does NOT issue "command not found: junk" error.

The following patch *seems* to fix these, but I believe this
is at most a partial fix (or regression at the worst).

Either with or without the patch, and either POSIX_BUILTINS is
set or unset, the following outputs "x = 1" (/bin/sh outputs
"x = 0"). But I don't know whether this need be fixed (or how
to fix it if it need be).

x=0
true | x=1 exec | true
echo "x = $x"


diff --git a/Src/exec.c b/Src/exec.c
index 1a6149a..fdb2470 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -3305,6 +3305,13 @@ 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 (varspc)
+	    addvars(state, varspc, 0);
+	lastval = errflag ? errflag : cmdoutval;
 	if (nullexec == 1) {
 	    /*
 	     * If nullexec is 1 we specifically *don't* restore the original
@@ -3315,13 +3322,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);



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: "var=value exec > file" ignores the assignment?
  2015-04-07 15:51 "var=value exec > file" ignores the assignment? Jun T.
@ 2015-04-08 17:13 ` Bart Schaefer
  2015-04-09 16:13   ` Jun T.
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2015-04-08 17:13 UTC (permalink / raw)
  To: zsh-workers

On Apr 8, 12:51am, Jun T. wrote:
}
} If 'exec' has no command arg but has both parameter assignment
} and redirection, the assignment seems to be ignored.

Hm.  This probably comes down to the behavior of "special" built-ins
which are supposed to retain the results of environment assignments.
It's possible (likely?) your patch should add a POSIX_BUILTINS test.

I'm not sure why we skip the XTRACE when nullexec == 1.  If your patch
is otherwise correct, there doesn't seem to be any other reason to
distinguish the 1 and 2 cases.

} Either with or without the patch, and either POSIX_BUILTINS is
} set or unset, the following outputs "x = 1" (/bin/sh outputs
} "x = 0"). But I don't know whether this need be fixed (or how
} to fix it if it need be).
} 
} x=0
} true | x=1 exec | true
} echo "x = $x"

This is a curious one.  Normally zsh would fork off "x=1 exec" so the
assignment would only affect a subshell, but that is apparently short-
circuited because the subshell has nothing else to do.  This:

x=0
true | x=1 | true

also assigns so $x == 1 whereas this:

true | { x=1 } | true

which should otherwise be equivalent, leaves $x == 0.  Anyway the point
is I don't think the presence of "exec" has anything to do with this
particular behavior.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: "var=value exec > file" ignores the assignment?
  2015-04-08 17:13 ` Bart Schaefer
@ 2015-04-09 16:13   ` Jun T.
  2015-04-15 19:52     ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Jun T. @ 2015-04-09 16:13 UTC (permalink / raw)
  To: zsh-workers


2015/04/09 02:13, Bart Schaefer <schaefer@brasslantern.com> 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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: "var=value exec > file" ignores the assignment?
  2015-04-09 16:13   ` Jun T.
@ 2015-04-15 19:52     ` Peter Stephenson
  2015-04-15 20:27       ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2015-04-15 19:52 UTC (permalink / raw)
  To: zsh-workers

On Fri, 10 Apr 2015 01:13:50 +0900
"Jun T." <takimoto-j@kba.biglobe.ne.jp> wrote:
> 2015/04/09 02:13, Bart Schaefer <schaefer@brasslantern.com> 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);


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: "var=value exec > file" ignores the assignment?
  2015-04-15 19:52     ` Peter Stephenson
@ 2015-04-15 20:27       ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2015-04-15 20:27 UTC (permalink / raw)
  To: zsh-workers

On Wed, 15 Apr 2015 20:52:18 +0100
Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> This (based on the previous patch) might be good enough.  It could
> probably do with a couple of tests.

Let me know if you've heard this one...  That's not what I meant.  You
know what I meant.

pws

diff --git a/Test/A04redirect.ztst b/Test/A04redirect.ztst
index 13f1f7c..602341d 100644
--- a/Test/A04redirect.ztst
+++ b/Test/A04redirect.ztst
@@ -538,3 +538,20 @@
   print $functions[noredirfn]
 0:Output from $functions[] for definition with no redirection
 >	print This rather boring function has no redirection.
+
+  (x=43
+   x=$(print This should appear, really >&2; print Not used) exec >test.log
+   print x=$x)
+   cat test.log
+0:Assignment with exec used for redirection: no POSIX_BUILTINS
+>x=43
+?This should appear, really
+
+  (setopt POSIX_BUILTINS
+   x=45
+   x=$(print This should appear, too >&2; print And this) exec >test.log
+   print x=$x)
+   cat test.log
+0:Assignment with exec used for redirection: POSIX_BUILTINS
+>x=And this
+?This should appear, too


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-04-15 20:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-07 15:51 "var=value exec > file" ignores the assignment? Jun T.
2015-04-08 17:13 ` Bart Schaefer
2015-04-09 16:13   ` Jun T.
2015-04-15 19:52     ` Peter Stephenson
2015-04-15 20:27       ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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).