Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: exit status



A thread on the Austin group suggests the exit status of the shell
should be available in exit traps.  This is potentially useful,
particularly since there's no other way of getting the exit status.
It's hard to see how it can cause problems since previously the exit
status wasn't tied to any particular command (it was whatever happened
to run just before the exit) so didn't have a useful value inside the
trap.

There was one special case: on an implicit exit it always was and still
is the status of the last command before exit, however as you couldn't
tell if it was an implicit exit from the trap even that wasn't
particularly useful.

This will apply inside the next zshexit hooks, too.  It already applies
to traps on return from functions---that's a separate piece of code.
(It's preserved if multiple functions/hooks run; this is already handled
by the context used for a trap or hook.)

Obviously the status is only available until the first statement within
the trap/hook has run.

Index: Doc/Zsh/builtins.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/builtins.yo,v
retrieving revision 1.87
diff -u -r1.87 builtins.yo
--- Doc/Zsh/builtins.yo	20 Sep 2006 09:22:34 -0000	1.87
+++ Doc/Zsh/builtins.yo	10 Nov 2006 09:37:12 -0000
@@ -1252,6 +1252,8 @@
 If var(sig) is tt(0) or tt(EXIT)
 and the tt(trap) statement is executed inside the body of a function,
 then the command var(arg) is executed after the function completes.
+The value of tt($?) at the start of execution is the exit status of the
+shell or the return status of the function exiting.
 If var(sig) is tt(0) or tt(EXIT)
 and the tt(trap) statement is not executed inside the body of a function,
 then the command var(arg) is executed when the shell terminates.
Index: Doc/Zsh/func.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/func.yo,v
retrieving revision 1.14
diff -u -r1.14 func.yo
--- Doc/Zsh/func.yo	9 Nov 2006 11:04:11 -0000	1.14
+++ Doc/Zsh/func.yo	10 Nov 2006 09:37:12 -0000
@@ -248,6 +248,8 @@
 item(tt(TRAPEXIT))(
 Executed when the shell exits,
 or when the current function exits if defined inside a function.
+The value of tt($?) at the start of execution is the exit status of the
+shell or the return status of the function exiting.
 )
 findex(TRAPZERR)
 findex(TRAPERR)
Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.168
diff -u -r1.168 builtin.c
--- Src/builtin.c	9 Nov 2006 11:04:11 -0000	1.168
+++ Src/builtin.c	10 Nov 2006 09:37:14 -0000
@@ -4434,6 +4434,7 @@
 #endif
 	}
     }
+    lastval = val;
     if (sigtrapped[SIGEXIT])
 	dotrap(SIGEXIT);
     callhookfunc("zshexit", NULL, 1);


-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php



Messages sorted by: Reverse Date, Date, Thread, Author