lib.sh: check args for errors in fx_ and dx_

check that there are at least two arguments, and ensure that
they are not empty.

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2025-09-13 11:31:43 +01:00
parent ce5127e46b
commit 333739961a
+21
View File
@@ -130,6 +130,7 @@ unpad_one_byte()
fx_()
{
xchk fx_ "$@"
fd="$(mktemp || err "can't create tmpfile")" || err
x_ rm -f "$fd" && x_ touch "$fd"
xx="$1" && shift 1
@@ -140,6 +141,7 @@ fx_()
dx_()
{
xchk dx_ "$@"
[ ! -f "$2" ] || while read -r fx; do
$1 "$fx" || return 1; :
done < "$2" || err "dx_ $*: cannot read '$2'"; :
@@ -151,6 +153,25 @@ x_()
[ $# -lt 1 ] || "$@" || err "Unhandled error for: $(echo "$@")"; :
}
xchk()
{
[ $# -lt 3 ] && err "$1 needs at least two arguments: $(xprintf "$@")"
if [ -z "$2" ] || [ -z "$3" ]; then
err "arguments must not be empty in $1: \"$2\" \"$3\" "
fi
}
xprintf()
{
xprintfargs=0
while [ $# -gt 0 ]; do
printf "\"%s\"" "$1"
xprintfargs=1
shift 1
done
[ $xprintfargs -gt 0 ] && printf "\n"; :
}
err()
{
[ $# -lt 1 ] || printf "ERROR %s: %s\n" "$0" "$1" 1>&2 || :