From: Peter Stephenson <p.stephenson@samsung.com>
To: zsh-workers@zsh.org
Cc: "Eduardo A. Bustamante Lopez" <dualbus@gmail.com>
Subject: Re: Zsh does not follow POSIX when return is called during the action of a trap
Date: Wed, 12 Mar 2014 18:23:16 +0000 [thread overview]
Message-ID: <20140312182316.56159d56@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <53209E1E.2090007@case.edu>
On Wed, 12 Mar 2014 13:49:18 -0400
Chet Ramey <chet.ramey@case.edu> wrote:
> On 3/12/14 1:03 PM, Peter Stephenson wrote:
>
> > However, I'm trying to work out what happens with the case
> >
> > | trap '(exit BEFORE-RETURN); return EXPLICIT-RETURN-VALUE' SIGNAL
> > |
> > | fn() {
> > | (exit BEFORE-ACTION); -block here waiting for signal-
> > | }
> >
> > Should this exit with EXPLICIT-RETURN_VALUE or BEFORE-ACTION? The
> > latter is easier to implement but my guess (without ploughing through
> > the standard) is EXPLICIT-RETURN-VALUE is right here.
>
> The correct exit value is EXPLICIT-RETURN-VALUE.
OK, so I think the answer is as follows... I'm out of time, I'll give
it a check with explicit return values tomorrow.
I'll also add some additional documentation for POSIX_TRAPS.
diff --git a/Src/builtin.c b/Src/builtin.c
index 9bcbcf7..0419c39 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -4688,9 +4688,10 @@ exit_pending;
int
bin_break(char *name, char **argv, UNUSED(Options ops), int func)
{
- int num = lastval, nump = 0;
+ int num = lastval, nump = 0, implicit;
/* handle one optional numeric argument */
+ implicit = !*argv;
if (*argv) {
num = mathevali(*argv++);
nump = 1;
@@ -4721,7 +4722,12 @@ bin_break(char *name, char **argv, UNUSED(Options ops), int func)
retflag = 1;
breaks = loops;
lastval = num;
- if (trap_state == TRAP_STATE_PRIMED && trap_return == -2) {
+ if (trap_state == TRAP_STATE_PRIMED && trap_return == -2 &&
+ /*
+ * With POSIX, "return" on its own in a trap doesn't
+ * update $? --- we keep the status from before the trap.
+ */
+ !(isset(POSIXTRAPS) && implicit)) {
trap_state = TRAP_STATE_FORCE_RETURN;
trap_return = lastval;
}
diff --git a/Src/signals.c b/Src/signals.c
index c8f5fbc..a6eb803 100644
--- a/Src/signals.c
+++ b/Src/signals.c
@@ -1155,6 +1155,7 @@ dotrapargs(int sig, int *sigtr, void *sigfn)
char *name, num[4];
int obreaks = breaks;
int oretflag = retflag;
+ int olastval = lastval;
int isfunc;
int traperr, new_trap_state, new_trap_return;
@@ -1261,6 +1262,13 @@ dotrapargs(int sig, int *sigtr, void *sigfn)
} else {
if (traperr && !EMULATION(EMULATE_SH))
lastval = 1;
+ else {
+ /*
+ * With no explicit forced return, we keep the
+ * lastval from before the trap ran.
+ */
+ lastval = olastval;
+ }
if (try_tryflag)
errflag = traperr;
breaks += obreaks;
pws
prev parent reply other threads:[~2014-03-12 18:33 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-12 15:36 Eduardo A. Bustamante López
2014-03-12 16:52 ` Bart Schaefer
2014-03-12 17:03 ` Peter Stephenson
2014-03-12 17:47 ` Eduardo A. Bustamante López
2014-03-12 17:49 ` Chet Ramey
2014-03-12 18:23 ` Peter Stephenson [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140312182316.56159d56@pwslap01u.europe.root.pri \
--to=p.stephenson@samsung.com \
--cc=dualbus@gmail.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).