diff options
author | Leah Rowe <leah@libreboot.org> | 2024-03-27 01:19:39 +0000 |
---|---|---|
committer | Leah Rowe <leah@libreboot.org> | 2024-03-27 01:50:31 +0000 |
commit | 6ebab10caa5be6fc1cfd244e745851687d4bd70d (patch) | |
tree | 0b983459c0cbc1b8998186975f9a571decb05592 /script/build/serprog | |
parent | 6b11f1b055466982747f65665506173a0b22557b (diff) |
safer, simpler error handling in lbmk
in shell scripts, a function named the same as a program included in
the $PATH will override that program. for example, you could make a
function called ls() and this would override the standand "ls".
in lbmk, a part of it was first trying to run the "fail" command,
deferring to "err", because some scripts call fail() which does
some minor cleanup before calling err.
in most cases, fail() is not defined, and it's possible that the user
could have a program called "fail" in their $PATH, the behaviour of
which we could not determine, and it could have disastrous effects.
lbmk error handling has been re-engineered in such a way that the
err function is defined in a variable, which defaults to err_ which
calls err_, so defined under include/err.sh.
in functions that require cleanup prior to error handling, a fail()
function is still defined, and err is overridden, thus:
err="fail"
this change has made xx_() obsolete, so now only x_ is used. the x_
function is a wrapper that can be used to run a command and exit with
non-zero status (from lbmk) if the command fails. the xx_ command
did the same thing, but called fail() which would have called err();
now everything is $err
example:
rm -f "$filename" || err "could not delete file"
this would now be:
rm -f "$filename" || $err "could not delete file"
overriding of err= must be done *after* including err.sh. for
example:
err="fail"
. "include/err.sh"
^ this is wrong. instead, one must do:
. "include/err.sh"
err="fail"
this is because err is set as a global variable under err.sh
the new error handling is much cleaner, and safer. it also reduces
the chance of mistakes such as: calling err when you meant to
call fail. this is because the standard way is now to call $err,
so you set err="fail" at the top of the script and all is well.
Signed-off-by: Leah Rowe <leah@libreboot.org>
Diffstat (limited to 'script/build/serprog')
-rwxr-xr-x | script/build/serprog | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/script/build/serprog b/script/build/serprog index 8b003add..ea2c02bf 100755 --- a/script/build/serprog +++ b/script/build/serprog @@ -17,8 +17,8 @@ usage="usage: ./build firmware serprog <rp2040|stm32> [board]" main() { - [ -z "${1+x}" ] && err "${usage}" - [ "$1" != "rp2040" ] && [ "$1" != "stm32" ] && err "$usage" + [ -z "${1+x}" ] && $err "${usage}" + [ "$1" != "rp2040" ] && [ "$1" != "stm32" ] && $err "$usage" if [ "${1}" = "rp2040" ]; then boards_dir=${pico_sdk_dir}/src/boards/include/boards [ -d "$pico_src_dir" ] || x_ ./update trees -f "pico-serprog" @@ -71,7 +71,7 @@ print_boards() list_boards() { - basename -a -s .h "${1}/"*.h || err "list_boards $1: can't list boards" + basename -a -s .h "${1}/"*.h || $err "list_boards $1: can't list boards" } main $@ |