diff options
-rw-r--r-- | shell/hush.c | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/shell/hush.c b/shell/hush.c index 6172f2285..b881b001a 100644 --- a/shell/hush.c +++ b/shell/hush.c | |||
@@ -988,6 +988,7 @@ struct globals { | |||
988 | # define G_fatal_sig_mask 0 | 988 | # define G_fatal_sig_mask 0 |
989 | #endif | 989 | #endif |
990 | #if ENABLE_HUSH_TRAP | 990 | #if ENABLE_HUSH_TRAP |
991 | int pre_trap_exitcode; | ||
991 | # if ENABLE_HUSH_FUNCTIONS | 992 | # if ENABLE_HUSH_FUNCTIONS |
992 | int return_exitcode; | 993 | int return_exitcode; |
993 | # endif | 994 | # endif |
@@ -2111,10 +2112,11 @@ static int check_and_run_traps(void) | |||
2111 | argv[1] = xstrdup(G_traps[sig]); | 2112 | argv[1] = xstrdup(G_traps[sig]); |
2112 | /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */ | 2113 | /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */ |
2113 | argv[2] = NULL; | 2114 | argv[2] = NULL; |
2114 | save_rcode = G.last_exitcode; | 2115 | G.pre_trap_exitcode = save_rcode = G.last_exitcode; |
2115 | builtin_eval(argv); | 2116 | builtin_eval(argv); |
2116 | free(argv[1]); | 2117 | free(argv[1]); |
2117 | G.last_exitcode = save_rcode; | 2118 | G.last_exitcode = save_rcode; |
2119 | G.pre_trap_exitcode = -1; | ||
2118 | # if ENABLE_HUSH_FUNCTIONS | 2120 | # if ENABLE_HUSH_FUNCTIONS |
2119 | if (G.return_exitcode >= 0) { | 2121 | if (G.return_exitcode >= 0) { |
2120 | debug_printf_exec("trap exitcode:%d\n", G.return_exitcode); | 2122 | debug_printf_exec("trap exitcode:%d\n", G.return_exitcode); |
@@ -9927,8 +9929,11 @@ int hush_main(int argc, char **argv) | |||
9927 | INIT_G(); | 9929 | INIT_G(); |
9928 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ | 9930 | if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */ |
9929 | G.last_exitcode = EXIT_SUCCESS; | 9931 | G.last_exitcode = EXIT_SUCCESS; |
9930 | #if ENABLE_HUSH_TRAP && ENABLE_HUSH_FUNCTIONS | 9932 | #if ENABLE_HUSH_TRAP |
9933 | # if ENABLE_HUSH_FUNCTIONS | ||
9931 | G.return_exitcode = -1; | 9934 | G.return_exitcode = -1; |
9935 | # endif | ||
9936 | G.pre_trap_exitcode = -1; | ||
9932 | #endif | 9937 | #endif |
9933 | 9938 | ||
9934 | #if ENABLE_HUSH_FAST | 9939 | #if ENABLE_HUSH_FAST |
@@ -10575,8 +10580,13 @@ static int FAST_FUNC builtin_exit(char **argv) | |||
10575 | 10580 | ||
10576 | /* note: EXIT trap is run by hush_exit */ | 10581 | /* note: EXIT trap is run by hush_exit */ |
10577 | argv = skip_dash_dash(argv); | 10582 | argv = skip_dash_dash(argv); |
10578 | if (argv[0] == NULL) | 10583 | if (argv[0] == NULL) { |
10584 | #if ENABLE_HUSH_TRAP | ||
10585 | if (G.pre_trap_exitcode >= 0) /* "exit" in trap uses $? from before the trap */ | ||
10586 | hush_exit(G.pre_trap_exitcode); | ||
10587 | #endif | ||
10579 | hush_exit(G.last_exitcode); | 10588 | hush_exit(G.last_exitcode); |
10589 | } | ||
10580 | /* mimic bash: exit 123abc == exit 255 + error msg */ | 10590 | /* mimic bash: exit 123abc == exit 255 + error msg */ |
10581 | xfunc_error_retval = 255; | 10591 | xfunc_error_retval = 255; |
10582 | /* bash: exit -2 == exit 254, no error msg */ | 10592 | /* bash: exit -2 == exit 254, no error msg */ |