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

Re: 'set -e' with '!' POSIX issue



On Wed, 5 Oct 2016 12:18:26 +0200
Martijn Dekker <martijn@xxxxxxxx> wrote:
> On Fri, Mar 13, 2009 at 03:51:34PM +0100, Vincent Lefevre wrote:
> > According to the new "set -e" proposal
> >
> >   http://www.opengroup.org/austin/mailarchives/ag/msg18258.html
> >
> > zsh -c 'set -e; ! if true; then false; fi; echo $?'
> >
> > should output 0, i.e. "false" should not make the shell exit, because
> > it is under a "!" context (even though "!" doesn't apply on the "false"
> > command directly).

This one appears to be a straightforward extension.

diff --git a/Src/exec.c b/Src/exec.c
index f248ca2..741c80e 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -1317,8 +1317,13 @@ execlist(Estate state, int dont_change_job, int exiting)
 	    next = state->pc + WC_SUBLIST_SKIP(code);
 	    if (!oldnoerrexit)
 		noerrexit = !isend;
-	    if ((WC_SUBLIST_FLAGS(code) & WC_SUBLIST_NOT) && isend)
-		this_noerrexit = 1;
+	    if (WC_SUBLIST_FLAGS(code) & WC_SUBLIST_NOT) {
+		/* suppress errexit for "! this_command" */
+		if (isend)
+		    this_noerrexit = 1;
+		/* suppress errexit for ! <list-of-shell-commands> */
+		noerrexit = 1;
+	    }
 	    switch (WC_SUBLIST_TYPE(code)) {
 	    case WC_SUBLIST_END:
 		/* End of sublist; just execute, ignoring status. */
diff --git a/Test/C03traps.ztst b/Test/C03traps.ztst
index 3a65b28..0faec02 100644
--- a/Test/C03traps.ztst
+++ b/Test/C03traps.ztst
@@ -556,6 +556,33 @@ F:Must be tested with a top-level script rather than source or function
 1:ERRETURN with "!" and a following false
 >before
 
+  fn() {
+      emulate -L zsh
+      setopt errreturn
+      print before
+      ! if true; then
+        false
+      fi
+      print after
+  }
+  fn
+0:ERRETURN with "!" suppressed inside complex structure
+>before
+>after
+
+  fn() {
+      emulate -L zsh
+      setopt errreturn
+      print before
+      if true; then
+        false
+      fi
+      print after
+  }
+  fn
+1:ERRETURN with no "!" suppression (control case)
+>before
+
 %clean
 
   rm -f TRAPEXIT



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