Package: mksh
Version: 39.3.20100725-1
Severity: normal
Justification: posix [1]

It is handy to be able to use the old exit status from an EXIT handler
by using bare "exit":

 $ sh -c 'trap "echo hi; exit" EXIT; exit 9'; echo $?
 hi
 9

Maybe something like the following would do the trick?  Untested, does
not handle nested traps.

[1] 
http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_21_14
http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_24_14
---
diff --git a/funcs.c b/funcs.c
index 4ec5dab..859c7a4 100644
--- a/funcs.c
+++ b/funcs.c
@@ -2247,6 +2247,9 @@ c_exitreturn(const char **wp)
                        warningf(true, "%s: bad number", arg);
                } else
                        exstat = n;
+       } else {
+               if (trap_exstat != -1)
+                       exstat = trap_exstat;
        }
        if (wp[0][0] == 'r') { /* return */
                struct env *ep;
diff --git a/histrap.c b/histrap.c
index 2ac4c38..9bcfb98 100644
--- a/histrap.c
+++ b/histrap.c
@@ -1048,6 +1048,8 @@ inittraps(void)
        int i;
        const char *cs;
 
+       trap_exstat = -1;
+
        /* Populate sigtraps based on sys_signame and sys_siglist. */
        for (i = 0; i <= NSIG; i++) {
                sigtraps[i].signal = i;
@@ -1263,7 +1265,6 @@ runtrap(Trap *p)
 {
        int     i = p->signal;
        char    *trapstr = p->trap;
-       int     oexstat;
        int     old_changed = 0;
 
        p->set = 0;
@@ -1287,13 +1288,14 @@ runtrap(Trap *p)
                p->flags &= ~TF_CHANGED;
                p->trap = NULL;
        }
-       oexstat = exstat;
+       trap_exstat = exstat;
        /*
         * Note: trapstr is fully parsed before anything is executed, thus
         * no problem with afree(p->trap) in settrap() while still in use.
         */
        command(trapstr, current_lineno);
-       exstat = oexstat;
+       exstat = trap_exstat;
+       trap_exstat = -1;
        if (i == SIGEXIT_ || i == SIGERR_) {
                if (p->flags & TF_CHANGED)
                        /* don't clear TF_CHANGED */
diff --git a/sh.h b/sh.h
index 640add0..cfa7460 100644
--- a/sh.h
+++ b/sh.h
@@ -557,6 +557,7 @@ EXTERN struct mksh_kshstate_v {
        /* global state */
        pid_t procpid_;         /* PID of executing process */
        int exstat_;            /* exit status */
+       int trap_exstat_;       /* exit status that triggered trap */
        int subst_exstat_;      /* exit status of last $(..)/`..` */
        struct env env_;        /* top-level parsing & execution env. */
        uint8_t shell_flags_[FNFLAGS];
@@ -576,6 +577,7 @@ EXTERN struct mksh_kshstate_f {
 #define ksheuid                kshstate_f.ksheuid_
 #define kshppid                kshstate_f.kshppid_
 #define exstat         kshstate_v.exstat_
+#define trap_exstat    kshstate_v.trap_exstat_
 #define subst_exstat   kshstate_v.subst_exstat_
 
 /* evil hack: return hash(kshstate_f concat (kshstate_f'.h:=hash(arg))) */



-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to