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

PATCH: ERR_RETURN option



I just discovered I needed this --- it works like ERR_EXIT but performs
a `return' instead.  Is this OK?  Have I missed something more obvious?

Index: Doc/Zsh/options.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/options.yo,v
retrieving revision 1.26
diff -u -r1.26 options.yo
--- Doc/Zsh/options.yo	20 Jun 2002 16:39:58 -0000	1.26
+++ Doc/Zsh/options.yo	2 Dec 2002 11:30:35 -0000
@@ -380,6 +380,16 @@
 trap, if set, and exit.  This is disabled while running initialization
 scripts.
 )
+pindex(ERR_RETURN)
+cindex(function return, on error)
+cidnex(return from function, on error)
+item(tt(ERR_RETURN))(
+If a command has a non-zero exit status, return immediately from the
+enclosing function.  The logic is identical to that for tt(ERR_EXIT),
+except that an implicit tt(return) statement is executed instead of an
+tt(exit).  This will trigger an exit at the outermost level of a
+non-interactive script.
+)
 pindex(EXEC)
 cindex(command execution, enabling)
 item(tt(EXEC) (tt(PLUS()n), ksh: tt(PLUS()n)) <D>)(
Index: Src/exec.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/exec.c,v
retrieving revision 1.46
diff -u -r1.46 exec.c
--- Src/exec.c	17 Sep 2002 10:42:20 -0000	1.46
+++ Src/exec.c	2 Dec 2002 11:30:35 -0000
@@ -924,13 +924,23 @@
 		dotrap(SIGZERR);
 		donetrap = 1;
 	    }
-	    if (lastval && isset(ERREXIT)) {
-		if (sigtrapped[SIGEXIT])
-		    dotrap(SIGEXIT);
-		if (mypid != getpid())
-		    _exit(lastval);
-		else
-		    exit(lastval);
+	    if (lastval) {
+		int errreturn = isset(ERRRETURN) && 
+		    (isset(INTERACTIVE) || locallevel || sourcelevel);
+		int errexit = isset(ERREXIT) || 
+		    (isset(ERRRETURN) && !errreturn);
+		if (errexit) {
+		    if (sigtrapped[SIGEXIT])
+			dotrap(SIGEXIT);
+		    if (mypid != getpid())
+			_exit(lastval);
+		    else
+			exit(lastval);
+		}
+		if (errreturn) {
+		    retflag = 1;
+		    breaks = loops;
+		}
 	    }
 	}
 	if (ltype & Z_END)
Index: Src/options.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/options.c,v
retrieving revision 1.13
diff -u -r1.13 options.c
--- Src/options.c	27 Aug 2002 21:10:34 -0000	1.13
+++ Src/options.c	2 Dec 2002 11:30:35 -0000
@@ -109,6 +109,7 @@
 {NULL, "cshnullglob",	      OPT_EMULATE|OPT_CSH,	 CSHNULLGLOB},
 {NULL, "equals",	      OPT_EMULATE|OPT_ZSH,	 EQUALS},
 {NULL, "errexit",	      OPT_EMULATE,		 ERREXIT},
+{NULL, "errreturn",	      OPT_EMULATE,		 ERRRETURN},
 {NULL, "exec",		      OPT_ALL,			 EXECOPT},
 {NULL, "extendedglob",	      OPT_EMULATE,		 EXTENDEDGLOB},
 {NULL, "extendedhistory",     OPT_CSH,			 EXTENDEDHISTORY},
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.42
diff -u -r1.42 zsh.h
--- Src/zsh.h	3 Sep 2002 09:33:37 -0000	1.42
+++ Src/zsh.h	2 Dec 2002 11:30:35 -0000
@@ -1419,6 +1419,7 @@
     CSHNULLGLOB,
     EQUALS,
     ERREXIT,
+    ERRRETURN,
     EXECOPT,
     EXTENDEDGLOB,
     EXTENDEDHISTORY,

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, UK                          Tel: +44 (0)1223 692070


**********************************************************************
The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential 
and/or privileged material. 
Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by 
persons or entities other than the intended recipient is 
prohibited.  
If you received this in error, please contact the sender and 
delete the material from any computer.
**********************************************************************



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