zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: PRINT_EXIT_VALUE with anonymous functions, again
@ 2015-02-13 10:48 Peter Stephenson
  2015-02-13 21:27 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Peter Stephenson @ 2015-02-13 10:48 UTC (permalink / raw)
  To: Zsh Hackers' List

Let's stick with an easy, unambiguously effective fix for this.  As
this is local there is no change to the simple / complex nature of the
command.

pws


diff --git a/Src/exec.c b/Src/exec.c
index 302e2b5..a60d4ff 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -3359,9 +3359,9 @@ execcmd(Estate state, int input, int output, int how, int last1)
 		zcontext_restore();
 	    } else
 		redir_prog = NULL;
-	    
+
 	    lastval = execfuncdef(state, redir_prog);
-	} 
+	}
 	else if (type >= WC_CURSH) {
 	    if (last1 == 1)
 		do_exec = 1;
@@ -4488,6 +4488,16 @@ execfuncdef(Estate state, Eprog redir_prog)
 	    execshfunc(shf, args);
 	    ret = lastval;
 
+	    if (isset(PRINTEXITVALUE) && isset(SHINSTDIN) &&
+		lastval) {
+#if defined(ZLONG_IS_LONG_LONG) && defined(PRINTF_HAS_LLD)
+		fprintf(stderr, "zsh: exit %lld\n", lastval);
+#else
+		fprintf(stderr, "zsh: exit %ld\n", (long)lastval);
+#endif
+		fflush(stderr);
+	    }
+
 	    freeeprog(shf->funcdef);
 	    if (shf->redir) /* shouldn't be */
 		freeeprog(shf->redir);
diff --git a/Test/E01options.ztst b/Test/E01options.ztst
index c6af803..3213534 100644
--- a/Test/E01options.ztst
+++ b/Test/E01options.ztst
@@ -795,12 +795,12 @@
 1:PRINT_EXIT_VALUE option
 ?zsh: exit 1
 
-#  $ZTST_testdir/../Src/zsh -f <<<'
-#      setopt printexitvalue
-#      () { false; }
-#  '
-#1:PRINT_EXIT_VALUE option for anonymous function
-#?zsh: exit 1
+  $ZTST_testdir/../Src/zsh -f <<<'
+      setopt printexitvalue
+      () { false; }
+  '
+1:PRINT_EXIT_VALUE option for anonymous function
+?zsh: exit 1
 
   setopt promptbang
   print -P !


-- 
Peter Stephenson | Principal Engineer Samsung Cambridge Solution Centre
Email: p.stephenson@samsung.com | Phone: +44 1223 434724 |
www.samsung.com


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

* Re: PATCH: PRINT_EXIT_VALUE with anonymous functions, again
  2015-02-13 10:48 PATCH: PRINT_EXIT_VALUE with anonymous functions, again Peter Stephenson
@ 2015-02-13 21:27 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2015-02-13 21:27 UTC (permalink / raw)
  To: Zsh Hackers' List

On Fri, 13 Feb 2015 10:48:49 +0000
Peter Stephenson <p.stephenson@samsung.com> wrote:
> Let's stick with an easy, unambiguously effective fix for this.  As
> this is local there is no change to the simple / complex nature of the
> command.

Test, based on the original report, fixing matters arising.

pws

diff --git a/Src/exec.c b/Src/exec.c
index 302e2b5..de6b9c5 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -2784,9 +2784,14 @@ execcmd(Estate state, int input, int output, int how, int last1)
     else
 	text = NULL;
 
-    /* Set up special parameter $_ */
-
-    setunderscore((args && nonempty(args)) ? ((char *) getdata(lastnode(args))) : "");
+    /*
+     * Set up special parameter $_
+     * For execfuncdef we may need to take account of an
+     * anonymous function with arguments.
+     */
+    if (type != WC_FUNCDEF)
+	setunderscore((args && nonempty(args)) ?
+		      ((char *) getdata(lastnode(args))) : "");
 
     /* Warn about "rm *" */
     if (type == WC_SIMPLE && interact && unset(RMSTARSILENT) &&
@@ -4374,7 +4379,7 @@ execfuncdef(Estate state, Eprog redir_prog)
     Shfunc shf;
     char *s = NULL;
     int signum, nprg, sbeg, nstrs, npats, len, plen, i, htok = 0, ret = 0;
-    int nfunc = 0;
+    int nfunc = 0, anon_func = 0;
     Wordcode beg = state->pc, end;
     Eprog prog;
     Patprog *pp;
@@ -4460,6 +4465,8 @@ execfuncdef(Estate state, Eprog redir_prog)
 	     */
 	    LinkList args;
 
+	    anon_func = 1;
+
 	    state->pc = end;
 	    end += *state->pc++;
 	    args = ecgetlist(state, *state->pc++, EC_DUPTOK, &htok);
@@ -4515,6 +4522,8 @@ execfuncdef(Estate state, Eprog redir_prog)
 	    shfunctab->addnode(shfunctab, ztrdup(s), shf);
 	}
     }
+    if (!anon_func)
+	setunderscore("");
     if (!nfunc && redir_prog) {
 	/* For completeness, shouldn't happen */
 	freeeprog(redir_prog);
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index cf639fa..42c7b4e 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -1670,3 +1670,32 @@
   print ${(e)param}
 0:Alias expansion in command substitution in parameter evaluation
 >Expanded in substitution
+
+  a=1 b=2 c=3
+  : One;
+  function {
+      : Two
+      echo $_
+      print -l $argv
+  } $_ Three
+  print -l $_ Four;
+0:$_ with anonymous function
+>Two
+>One
+>Three
+>Three
+>Four
+
+  a=1 b=2 c=3
+  : One
+  function {
+      : Two
+      echo $_
+      print -l $argv
+  }
+  print -l "$_" Four
+0:$_ with anonymous function without arguments
+>Two
+>
+>
+>Four


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

end of thread, other threads:[~2015-02-13 21:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-13 10:48 PATCH: PRINT_EXIT_VALUE with anonymous functions, again Peter Stephenson
2015-02-13 21: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).