aboutsummaryrefslogtreecommitdiff
path: root/common/cli_hush.c
diff options
context:
space:
mode:
authorMarek Vasut <marex@denx.de>2022-12-20 07:25:59 +0100
committerTom Rini <trini@konsulko.com>2023-01-11 15:02:24 -0500
commit721307eba0e7d94241698936c58352ee3c6da748 (patch)
tree7252c2fc1aad94b0c013ac8e092d40dac2093623 /common/cli_hush.c
parentf3d914cfdd5ac611d99f04096497f4cd13dd6aaa (diff)
downloadu-boot-721307eba0e7d94241698936c58352ee3c6da748.zip
u-boot-721307eba0e7d94241698936c58352ee3c6da748.tar.gz
u-boot-721307eba0e7d94241698936c58352ee3c6da748.tar.bz2
cmd: exit: Fix return value propagation out of environment scripts
Make sure the 'exit' command as well as 'exit $val' command exits from environment scripts immediately and propagates return value out of those scripts fully. That means the following behavior is expected: " => setenv foo 'echo bar ; exit 1' ; run foo ; echo $? bar 1 => setenv foo 'echo bar ; exit 0' ; run foo ; echo $? bar 0 => setenv foo 'echo bar ; exit -2' ; run foo ; echo $? bar 0 " As well as the followin behavior: " => setenv foo 'echo bar ; exit 3 ; echo fail'; run foo; echo $? bar 3 => setenv foo 'echo bar ; exit 1 ; echo fail'; run foo; echo $? bar 1 => setenv foo 'echo bar ; exit 0 ; echo fail'; run foo; echo $? bar 0 => setenv foo 'echo bar ; exit -1 ; echo fail'; run foo; echo $? bar 0 => setenv foo 'echo bar ; exit -2 ; echo fail'; run foo; echo $? bar 0 => setenv foo 'echo bar ; exit ; echo fail'; run foo; echo $? bar 0 " Fixes: 8c4e3b79bd0 ("cmd: exit: Fix return value") Reviewed-by: Hector Palacios <hector.palacios@digi.com> Signed-off-by: Marek Vasut <marex@denx.de>
Diffstat (limited to 'common/cli_hush.c')
-rw-r--r--common/cli_hush.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/common/cli_hush.c b/common/cli_hush.c
index a80b847..1ad7a50 100644
--- a/common/cli_hush.c
+++ b/common/cli_hush.c
@@ -1901,7 +1901,7 @@ static int run_list_real(struct pipe *pi)
last_return_code = -rcode - 2;
return -2; /* exit */
}
- last_return_code=(rcode == 0) ? 0 : 1;
+ last_return_code = rcode;
#endif
#ifndef __U_BOOT__
pi->num_progs = save_num_progs; /* restore number of programs */
@@ -3211,7 +3211,15 @@ static int parse_stream_outer(struct in_str *inp, int flag)
printf("exit not allowed from main input shell.\n");
continue;
}
- break;
+ /*
+ * DANGER
+ * Return code -2 is special in this context,
+ * it indicates exit from inner pipe instead
+ * of return code itself, the return code is
+ * stored in 'last_return_code' variable!
+ * DANGER
+ */
+ return -2;
}
if (code == -1)
flag_repeat = 0;
@@ -3248,9 +3256,9 @@ int parse_string_outer(const char *s, int flag)
#endif /* __U_BOOT__ */
{
struct in_str input;
+ int rcode;
#ifdef __U_BOOT__
char *p = NULL;
- int rcode;
if (!s)
return 1;
if (!*s)
@@ -3262,11 +3270,12 @@ int parse_string_outer(const char *s, int flag)
setup_string_in_str(&input, p);
rcode = parse_stream_outer(&input, flag);
free(p);
- return rcode;
+ return rcode == -2 ? last_return_code : rcode;
} else {
#endif
setup_string_in_str(&input, s);
- return parse_stream_outer(&input, flag);
+ rcode = parse_stream_outer(&input, flag);
+ return rcode == -2 ? last_return_code : rcode;
#ifdef __U_BOOT__
}
#endif
@@ -3286,7 +3295,7 @@ int parse_file_outer(void)
setup_file_in_str(&input);
#endif
rcode = parse_stream_outer(&input, FLAG_PARSE_SEMICOLON);
- return rcode;
+ return rcode == -2 ? last_return_code : rcode;
}
#ifdef __U_BOOT__