lib.sh: use xprintf in err()

if more than one argument is provided, it is interpreted
as a command, and the command is outputted.

this means that now for example, where you have:

ls -l foo | err "could not list directory"

you could do:

ls -l foo | err "could not list directory" "$@"

this would show all the arguments given to the calling
function that tried to run "ls"

let's say that function was called bar, you might do:

ls -l foo | err "could not list directory" bar "$@"

right now, it's not easy to provide good debug info
where err is used, unless it was called with x_, which
provides the command/arguments that was bugging out.

with this, we now have an easy and readable/maintainable
way to do the same thing everywhere in xbmk.

this will now be done, in a follow-up commit.

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2025-09-13 12:05:37 +01:00
parent edcf8cead8
commit 7bed68f5b7
+18 -9
View File
@@ -149,18 +149,33 @@ dx_()
x_() x_()
{ {
[ $# -lt 1 ] || [ -n "$1" ] || err "Empty first arg: x_ $(xprintf "$@")" [ $# -lt 1 ] || [ -n "$1" ] || err "Empty first arg" "$@"
[ $# -lt 1 ] || "$@" || err "Unhandled error for: $(xprintf "$@")"; : [ $# -lt 1 ] || "$@" || err "Unhandled error" "$@"
} }
xchk() xchk()
{ {
[ $# -lt 3 ] && err "$1 needs at least two arguments: $(xprintf "$@")" [ $# -lt 3 ] && err "$1 needs at least two arguments" "$@"
if [ -z "$2" ] || [ -z "$3" ]; then if [ -z "$2" ] || [ -z "$3" ]; then
err "arguments must not be empty in $1: \"$2\" \"$3\" " err "arguments must not be empty in $1: \"$2\" \"$3\" "
fi fi
} }
err()
{
if [ $# -eq 1 ]; then
printf "ERROR %s: %s\n" "$0" "$1" 1>&2 || :
elif [ $# -gt 1 ]; then
printf "ERROR %s: %s: in command/function with arguments: " \
"$0" "$1" 1>&2
shift 1
xprintf "$@" 1>&2
else
printf "ERROR, but no arguments provided to err\n" 1>&2
fi
exit 1
}
xprintf() xprintf()
{ {
xprintfargs=0 xprintfargs=0
@@ -172,9 +187,3 @@ xprintf()
done done
[ $xprintfargs -gt 0 ] && printf "\n"; : [ $xprintfargs -gt 0 ] && printf "\n"; :
} }
err()
{
[ $# -lt 1 ] || printf "ERROR %s: %s\n" "$0" "$1" 1>&2 || :
exit 1
}