Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Zsh does not follow POSIX when return is called during the action of a trap
On Wed, 12 Mar 2014 13:49:18 -0400
Chet Ramey <chet.ramey@xxxxxxxx> 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author