summaryrefslogtreecommitdiff
path: root/include/lib.sh
diff options
context:
space:
mode:
authorLeah Rowe <leah@libreboot.org>2025-09-13 11:31:43 +0100
committerLeah Rowe <leah@libreboot.org>2025-09-13 11:47:10 +0100
commit333739961aba2920405fb5399679bc7a9db81796 (patch)
tree21eee7d3a12c7f38d061b5e40ec935dd69179d82 /include/lib.sh
parentce5127e46b89c2e5716668911bc82834d06660ff (diff)
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>
Diffstat (limited to 'include/lib.sh')
-rw-r--r--include/lib.sh21
1 files changed, 21 insertions, 0 deletions
diff --git a/include/lib.sh b/include/lib.sh
index 1f8687fb..7728a4c6 100644
--- a/include/lib.sh
+++ b/include/lib.sh
@@ -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 || :