summary refs log tree commit diff
path: root/Src/builtin.c
diff options
context:
space:
mode:
authorPeter Stephenson <p.stephenson@samsung.com>2018-10-09 14:38:26 +0100
committerPeter Stephenson <p.stephenson@samsung.com>2018-10-09 14:38:26 +0100
commitbbccbe0c85887bfc15c57a0c5eb97e59f7cb9fa7 (patch)
tree01eda28efd0a0b7eff0da5b69697da61b6d0011f /Src/builtin.c
parent9ede8c657f498c584d89a8d8ff3b24104711c88c (diff)
downloadzsh-bbccbe0c85887bfc15c57a0c5eb97e59f7cb9fa7.tar.gz
zsh-bbccbe0c85887bfc15c57a0c5eb97e59f7cb9fa7.tar.xz
zsh-bbccbe0c85887bfc15c57a0c5eb97e59f7cb9fa7.zip
43660: extend 43653 when final exit is implicit.
Combine logic for case after committed to exit (shell_exiting) with
case where exit occurred in a function we nee to unwind (exit_pending).

Add sarky note for future generations to be confused at.
Diffstat (limited to 'Src/builtin.c')
-rw-r--r--Src/builtin.c40
1 files changed, 38 insertions, 2 deletions
diff --git a/Src/builtin.c b/Src/builtin.c
index ca3ef23be..e01e035cc 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -5647,8 +5647,9 @@ bin_break(char *name, char **argv, UNUSED(Options ops), int func)
 	    if (stopmsg || (zexit(0,2), !stopmsg)) {
 		retflag = 1;
 		breaks = loops;
-		exit_pending = (num << 1) | 1;
+		exit_pending = 1;
 		exit_level = locallevel;
+		exit_val = num;
 	    }
 	} else
 	    zexit(num, 0);
@@ -5698,6 +5699,42 @@ checkjobs(void)
 /**/
 int shell_exiting;
 
+/*
+ * Exit status if explicitly set by an exit command.
+ * This is complicated by the fact the exit command may be within
+ * a function whose state we need to unwind (exit_pending set
+ * and the exit will happen up the stack), or we may need to execute
+ * additional code such as a trap after we are committed to exiting
+ * (shell_exiting and the exit will happen down the stack).
+ *
+ * It's lucky this is all so obvious there is no possibility of any
+ * bugs.  (C.f. the entire rest of the shell.)
+ */
+/**/
+int exit_val;
+
+/*
+ * Actually exit the shell, working out the status locally.
+ * This is exit_val if "exit" has explicitly been called in the shell,
+ * else lastval.
+ */
+
+/**/
+void
+realexit(void)
+{
+    exit(exit_val ? exit_val : lastval);
+}
+
+/* As realexit(), but call _exit instead */
+
+/**/
+void
+_realexit(void)
+{
+    _exit(exit_val ? exit_val : lastval);
+}
+
 /* exit the shell.  val is the return value of the shell.  *
  * from_where is
  *   1   if zexit is called because of a signal
@@ -5709,7 +5746,6 @@ int shell_exiting;
 mod_export void
 zexit(int val, int from_where)
 {
-    static int exit_val;
     /*
      * Don't do anything recursively:  see below.
      * Do, however, update exit status --- there's no nesting,