Add an f_eval_catch() function for debugging individual commands in a

series of commands. Accepts the `-d' flag as a first argument to skip
displaying messages in a dialog box. The command is logged as it
appears to the shell prior to the first pass of parameter expansion to
allow copy/pasting into a real shell (opposed to simply echo'ing the
command which would produce debug output that has undergone at least one
pass of parameter expansion, thus no-longer copacetic for copy/paste).
Takes printf(1) style syntax and a utility identifier for error messages.
This commit is contained in:
Devin Teske 2013-11-07 10:28:12 +00:00
parent 8a316c0005
commit bdf4b1764e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=257784

View file

@ -777,6 +777,88 @@ f_mounted()
mount | grep -Eq " on $dir \([^)]+\)$"
}
# f_eval_catch [-d] $funcname $utility $format [$arguments ...]
#
# Silently evaluate a command in a sub-shell and test for error. If debugging
# is enabled a copy of the command and its output is sent to debug (either
# stdout or file depending on environment). If an error occurs, output of the
# command is displayed in a dialog(1) msgbox using the [above] f_show_err()
# function (unless optional `-d' flag is the first argument, then no dialog).
# The $funcname argument is sent to debugging while the $utility argument is
# used in the title of the dialog box. The command that is sent to debugging
# along with $funcname is the product of the printf(1) syntax produced by
# $format with optional $arguments.
#
# Example 1:
#
# debug=1
# f_eval_catch myfunc cat 'contents=$( cat "%s" )' /some/file
# # Error displayed ``cat: /some/file: No such file or directory''
#
# Produces the following debug output:
#
# DEBUG: myfunc: cat "/some/file"
# DEBUG: myfunc: retval=1 <output below>
# cat: /some/file: No such file or directory
#
# Example 2:
#
# debug=1
# f_eval_catch myfunc echo 'echo "%s"' "Hello, World!"
# # No error displayed
#
# Produces the following debug output:
#
# DEBUG: myfunc: echo "Hello, World!"
# DEBUG: myfunc: retval=0 <output below>
# Hello, World!
#
# Example 3:
#
# debug=1
# echo 123 | f_eval_catch myfunc rev rev
# # No error displayed
#
# Produces the following debug output:
#
# DEBUG: myfunc: rev
# DEBUG: myfunc: retval=0 <output below>
# 321
#
# Example 4:
#
# debug=1
# f_eval_catch myfunc true true
# # No error displayed
#
# Produces the following debug output:
#
# DEBUG: myfunc: true
# DEBUG: myfunc: retval=0 <no output>
#
f_eval_catch()
{
local no_dialog=
[ "$1" = "-d" ] && no_dialog=1 && shift 1
local funcname="$1" utility="$2"; shift 2
local cmd output retval
cmd=$( printf -- "$@" )
f_dprintf "%s: %s" "$funcname" "$cmd" # Log command *before* eval
output=$( exec 2>&1; eval "$cmd" )
retval=$?
if [ "$output" ]; then
f_dprintf "%s: retval=%i <output below>\n%s" "$funcname" \
$retval "$output"
else
f_dprintf "%s: retval=%i <no output>" "$funcname" $retval
fi
! [ "$no_dialog" -o "$nonInteractive" -o $retval -eq $SUCCESS ] &&
msg_error="${msg_error:-Error}${utility:+: $utility}" \
f_show_err "%s" "$output"
# NB: f_show_err will handle NULL output appropriately
return $retval
}
############################################################ MAIN
#