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

PATCH: POSIX_TRAPS option



It's high time we were able to treat EXIT traps the same way as other
shells.

Oddly, POSIX doesn't seem to specify when EXIT traps are actually run,
but the implication must be it refers to exiting the shell, not
returning from shell functions.

Index: Doc/Zsh/options.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/options.yo,v
retrieving revision 1.94
diff -p -u -r1.94 options.yo
--- Doc/Zsh/options.yo	23 Aug 2010 13:40:43 -0000	1.94
+++ Doc/Zsh/options.yo	10 Sep 2010 19:29:48 -0000
@@ -1876,6 +1876,19 @@ If multibyte character support is not co
 ignored; all octets with the top bit set may be used in identifiers.
 This is non-standard but is the traditional zsh behaviour.
 )
+pindex(POSIX_TRAPS)
+pindex(NO_POSIX_TRAPS)
+pindex(POSIXTRAPS)
+pindex(NOPOSIXTRAPS)
+cindex(traps, on function exit)
+cindex(traps, POSIX compatibility)
+item(tt(POSIX_TRAPS) <K> <S>)(
+When the is option is set, the usual zsh behaviour of executing
+traps for tt(EXIT) on exit from shell functions is suppressed.
+In that case, manipulating tt(EXIT) traps always alters the global
+trap for exiting the shell; the tt(LOCAL_TRAPS) option is
+ignored for the tt(EXIT) trap.
+)
 pindex(SH_FILE_EXPANSION)
 pindex(NO_SH_FILE_EXPANSION)
 pindex(SHFILEEXPANSION)
Index: Src/options.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/options.c,v
retrieving revision 1.54
diff -p -u -r1.54 options.c
--- Src/options.c	16 Mar 2010 09:44:55 -0000	1.54
+++ Src/options.c	10 Sep 2010 19:29:48 -0000
@@ -204,6 +204,7 @@ static struct optname optns[] = {
 {{NULL, "posixcd",            OPT_EMULATE|OPT_BOURNE},	 POSIXCD},
 {{NULL, "posixidentifiers",   OPT_EMULATE|OPT_BOURNE},	 POSIXIDENTIFIERS},
 {{NULL, "posixjobs",          OPT_EMULATE|OPT_BOURNE},	 POSIXJOBS},
+{{NULL, "posixtraps",          OPT_EMULATE|OPT_BOURNE},	 POSIXTRAPS},
 {{NULL, "printeightbit",      0},                        PRINTEIGHTBIT},
 {{NULL, "printexitvalue",     0},			 PRINTEXITVALUE},
 {{NULL, "privileged",	      OPT_SPECIAL},		 PRIVILEGED},
Index: Src/signals.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/signals.c,v
retrieving revision 1.59
diff -p -u -r1.59 signals.c
--- Src/signals.c	22 Aug 2010 20:08:57 -0000	1.59
+++ Src/signals.c	10 Sep 2010 19:29:49 -0000
@@ -864,7 +864,8 @@ removetrap(int sig)
      * one, to aid in removing this one.  However, if there's
      * already one at the current locallevel we just overwrite it.
      */
-    if (!dontsavetrap && (isset(LOCALTRAPS) || sig == SIGEXIT) &&
+    if (!dontsavetrap &&
+	(sig == SIGEXIT ? !isset(POSIXTRAPS) : isset(LOCALTRAPS)) &&
 	locallevel &&
 	(!trapped || locallevel > (sigtrapped[sig] >> ZSIG_SHIFT)))
 	dosavetrap(sig, locallevel);
@@ -932,7 +933,7 @@ starttrapscope(void)
      * so give it the next higher one. dosavetrap() is called
      * automatically where necessary.
      */
-    if (sigtrapped[SIGEXIT]) {
+    if (sigtrapped[SIGEXIT] && !isset(POSIXTRAPS)) {
 	locallevel++;
 	unsettrap(SIGEXIT);
 	locallevel--;
@@ -960,7 +961,7 @@ endtrapscope(void)
      */
     if (intrap)
 	exittr = 0;
-    else if ((exittr = sigtrapped[SIGEXIT])) {
+    else if (!isset(POSIXTRAPS) && (exittr = sigtrapped[SIGEXIT])) {
 	if (exittr & ZSIG_FUNC) {
 	    exitfn = removehashnode(shfunctab, "TRAPEXIT");
 	} else {
@@ -1005,7 +1006,8 @@ endtrapscope(void)
     }
 
     if (exittr) {
-	dotrapargs(SIGEXIT, &exittr, exitfn);
+	if (!isset(POSIXTRAPS))
+	    dotrapargs(SIGEXIT, &exittr, exitfn);
 	if (exittr & ZSIG_FUNC)
 	    shfunctab->freenode((HashNode)exitfn);
 	else
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.166
diff -p -u -r1.166 zsh.h
--- Src/zsh.h	27 May 2010 18:57:34 -0000	1.166
+++ Src/zsh.h	10 Sep 2010 19:29:49 -0000
@@ -1987,6 +1987,7 @@ enum {
     POSIXCD,
     POSIXIDENTIFIERS,
     POSIXJOBS,
+    POSIXTRAPS,
     PRINTEIGHTBIT,
     PRINTEXITVALUE,
     PRIVILEGED,
Index: Test/C03traps.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/C03traps.ztst,v
retrieving revision 1.19
diff -p -u -r1.19 C03traps.ztst
--- Test/C03traps.ztst	10 May 2010 12:31:49 -0000	1.19
+++ Test/C03traps.ztst	10 Sep 2010 19:29:49 -0000
@@ -371,6 +371,23 @@
 0: EXIT trap set in command substitution
 >command substitution exited
 
+   (cd ..; $ZTST_exe -fc 'setopt posixtraps;
+   TRAPEXIT() { print Exited; }
+   fn1() { trap; }
+   setopt localtraps # should be ignored by EXIT
+   fn2() { TRAPEXIT() { print No, really exited; } }
+   fn1
+   fn2
+   fn1')
+0:POSIX_TRAPS option
+>TRAPEXIT () {
+>	print Exited
+>}
+>TRAPEXIT () {
+>	print No, really exited
+>}
+>No, really exited
+
 %clean
 
   rm -f TRAPEXIT

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



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