From 7bed68f5b7096349bc8a6f48d2f4394db7a581af Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sat, 13 Sep 2025 12:05:37 +0100 Subject: 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 --- include/lib.sh | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/include/lib.sh b/include/lib.sh index 63aea2b7..d221a472 100644 --- a/include/lib.sh +++ b/include/lib.sh @@ -149,18 +149,33 @@ dx_() x_() { - [ $# -lt 1 ] || [ -n "$1" ] || err "Empty first arg: x_ $(xprintf "$@")" - [ $# -lt 1 ] || "$@" || err "Unhandled error for: $(xprintf "$@")"; : + [ $# -lt 1 ] || [ -n "$1" ] || err "Empty first arg" "$@" + [ $# -lt 1 ] || "$@" || err "Unhandled error" "$@" } 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 err "arguments must not be empty in $1: \"$2\" \"$3\" " 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() { xprintfargs=0 @@ -172,9 +187,3 @@ xprintf() done [ $xprintfargs -gt 0 ] && printf "\n"; : } - -err() -{ - [ $# -lt 1 ] || printf "ERROR %s: %s\n" "$0" "$1" 1>&2 || : - exit 1 -} -- cgit v1.2.1