cmd: exit: Fix return value
authorMarek Vasut <marex@denx.de>
Tue, 6 Apr 2021 12:00:24 +0000 (14:00 +0200)
committerTom Rini <trini@konsulko.com>
Mon, 12 Apr 2021 21:45:40 +0000 (17:45 -0400)
commit8c4e3b79bd0bb76eea16869e9666e19047c0d005
tree77913e7bd9309afa6b2ddc6f3e3e49827da2025c
parent7062d4e815d255891d96aafe617a264c4c4a8a02
cmd: exit: Fix return value

In case exit is called in a script without parameter, the command
returns -2 ; in case exit is called with a numerical parameter,
the command returns -2 and lower. This leads to the following problem:
=> setenv foo 'echo bar ; exit 1' ; run foo ; echo $?
bar
0
=> setenv foo 'echo bar ; exit 0' ; run foo ; echo $?
bar
0
=> setenv foo 'echo bar ; exit -2' ; run foo ; echo $?
bar
0
That is, no matter what the 'exit' command argument is, the return
value is always 0 and so it is not possible to use script return
value in subsequent tests.

Fix this and simplify the exit command such that if exit is called with
no argument, the command returns 0, just like 'true' in cmd/test.c. In
case the command is called with any argument that is positive integer,
the argument is set as return value.
=> 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

Note that this does change ABI established in 2004 , although it is
unclear whether that ABI was originally OK or not.

Fixes: c26e454dfc6
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Pantelis Antoniou <pantelis.antoniou@konsulko.com>
Cc: Tom Rini <trini@konsulko.com>
cmd/exit.c