diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2020-02-21 02:18:06 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2020-02-21 02:18:06 +0100 |
commit | cc9ecd9af13f04a4814ebdec60942962c3e9f14f (patch) | |
tree | 700e8aa0d2e23df90312d224f42a76733c9b248e /shell/hush.c | |
parent | f977e004cefe46e8010c3d29681d77da50e380a9 (diff) | |
download | busybox-w32-cc9ecd9af13f04a4814ebdec60942962c3e9f14f.tar.gz busybox-w32-cc9ecd9af13f04a4814ebdec60942962c3e9f14f.tar.bz2 busybox-w32-cc9ecd9af13f04a4814ebdec60942962c3e9f14f.zip |
hush: make "exit" in trap use pre-trap exitcode
function old new delta
check_and_run_traps 259 276 +17
builtin_exit 42 53 +11
hush_main 1086 1096 +10
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 38/0) Total: 38 bytes
Fixes exitcode_trap2.tests.
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell/hush.c')
-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 */ |