zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] typeset: set $? on incidental error
@ 2016-01-14  0:13 Daniel Shahaf
  2016-01-14  4:58 ` Bart Schaefer
  2016-01-14  5:24 ` Eric Cook
  0 siblings, 2 replies; 29+ messages in thread
From: Daniel Shahaf @ 2016-01-14  0:13 UTC (permalink / raw)
  To: zsh-workers

The 'typeset' family of builtins doesn't set $? when one would expect it
to do so:

    % x=$(true) y=$(exit 42); echo $?
    42
    % local x=$(true) y=$(exit 42); echo $?
    0

This patch makes 'typeset' behave as ordiary assignment does.

It's a one-liner but still, I'd appreciate a second pair of eyes over it.

Cheers,

Daniel

diff --git a/Src/builtin.c b/Src/builtin.c
index d8974eb..c4af66f 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -475,9 +475,13 @@ execbuiltin(LinkList args, LinkList assigns, Builtin bn)
 	{
 	    /*
 	     * Takes two sets of arguments.
+	     *
+	     * Return assignfunc's return value, or if it succeeded return any
+	     * preexisting error.
 	     */
 	    HandlerFuncAssign assignfunc = (HandlerFuncAssign)bn->handlerfunc;
-	    return (*(assignfunc)) (name, argv, assigns, &ops, bn->funcid);
+	    int retval = (*(assignfunc)) (name, argv, assigns, &ops, bn->funcid);
+	    return retval ? retval : lastval;
 	}
 	else
 	{
diff --git a/Test/B02typeset.ztst b/Test/B02typeset.ztst
index 7d65cc8..a87ecfb 100644
--- a/Test/B02typeset.ztst
+++ b/Test/B02typeset.ztst
@@ -706,3 +706,13 @@
 >typeset -a array=( '' two '' four )
 >typeset -a array=( one '' three )
 >no really nothing here
+
+  s='local x=$(exit 42); echo $?'
+  eval $s
+  (
+    disable -r local
+    eval $s
+  )
+0:typeset reports errors like parameter assignment does
+>42
+>42


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

end of thread, other threads:[~2016-02-01  5:47 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-14  0:13 [PATCH] typeset: set $? on incidental error Daniel Shahaf
2016-01-14  4:58 ` Bart Schaefer
2016-01-14  5:24 ` Eric Cook
2016-01-15  6:26   ` Daniel Shahaf
2016-01-15 14:46     ` Mikael Magnusson
2016-01-15 14:54       ` Eric Cook
2016-01-15 15:49         ` Peter Stephenson
     [not found]       ` <20160118022557.GE3979@tarsus.local2>
2016-01-18  4:38         ` Mikael Magnusson
2016-01-18 13:33           ` Mikael Magnusson
2016-01-20  7:47           ` Daniel Shahaf
2016-01-20 15:00             ` Eric Cook
2016-01-23 23:53               ` Daniel Shahaf
2016-01-24  3:32                 ` Eric Cook
2016-01-26 22:50                   ` Daniel Shahaf
2016-01-27  4:15                     ` Error status of "repeat" (was Re: [PATCH] typeset: set $? on incidental error) Bart Schaefer
2016-01-27  4:38                       ` Bart Schaefer
2016-01-27  9:52                       ` Peter Stephenson
2016-01-29  9:18                         ` Daniel Shahaf
2016-01-29  9:29                           ` Peter Stephenson
2016-01-29 10:25                             ` Daniel Shahaf
2016-01-27  4:17                     ` [PATCH] typeset: set $? on incidental error Bart Schaefer
2016-01-29  9:18                       ` Daniel Shahaf
2016-01-30  7:46                         ` typeset docs flow " Daniel Shahaf
2016-01-30 19:47                           ` Bart Schaefer
2016-01-31  0:49                             ` Jun T.
2016-01-31 17:03                               ` Bart Schaefer
2016-02-01  3:23                                 ` Jun T.
2016-02-01  5:41                                   ` Bart Schaefer
2016-01-21 14:22             ` Vincent Lefevre

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