Tidy up quiet wrappers

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
Dave Rodgman 2024-02-26 11:43:11 +00:00
parent 219006329d
commit 00bc790d79
2 changed files with 39 additions and 41 deletions

View File

@ -16,15 +16,13 @@ NO_SILENCE=" --version "
TOOL=$(basename "$0") TOOL=$(basename "$0")
# Locate original tool # Locate original tool
ORIGINAL_TOOL=$(type -ap ${TOOL} | grep -v "$0" | head -n1 ) ORIGINAL_TOOL=$(type -ap "${TOOL}" | grep -v -Fx "$0" | head -n1 )
quote_args() { print_quoted_args() {
# similar to printf '%q' "$@" # similar to printf '%q' "$@"
# but produce more human-readable results for common/simple cases like "a b" # but produce more human-readable results for common/simple cases like "a b"
local args=("$@") for a in "$@"; do
s="" simple_pattern='^([[:alnum:]_+-]+=)?([[:alnum:] _=+-./:@]*)$'
for a in "${args[@]}"; do
simple_pattern='^([[:alnum:]_+-]+=)?([[:alnum:] _=+-]*)$'
if [[ $a =~ ' ' && $a =~ $simple_pattern ]]; then if [[ $a =~ ' ' && $a =~ $simple_pattern ]]; then
# a has spaces, but no other special characters that need escaping # a has spaces, but no other special characters that need escaping
# (quoting after removing spaces yields no backslashes) # (quoting after removing spaces yields no backslashes)
@ -33,39 +31,40 @@ quote_args() {
# CFLAGS=a b -> CFLAGS="a b" # CFLAGS=a b -> CFLAGS="a b"
q="${BASH_REMATCH[1]}\"${BASH_REMATCH[2]}\"" q="${BASH_REMATCH[1]}\"${BASH_REMATCH[2]}\""
else else
# get bash to do the quoting # get bash to do the quoting (which may result in no quotes or escaping,
# if none is needed).
q=$(printf '%q' "$a") q=$(printf '%q' "$a")
fi fi
s="$s $q" printf "%s " "$q"
done done
echo $s
} }
if [[ ! " $@ " =~ " --version " ]]; then if [[ ! " $* " =~ " --version " ]]; then
# Display the command being invoked - if it succeeds, this is all that will # Display the command being invoked - if it succeeds, this is all that will
# be displayed. Don't do this for invocations with --version, because # be displayed. Don't do this for invocations with --version, because
# this output is often parsed by scripts, so we don't want to modify it. # this output is often parsed by scripts, so we don't want to modify it.
echo "${TOOL} $(quote_args "$@")" printf %s "${TOOL} "
print_quoted_args "$@"
echo
fi fi
if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then
# Run original command with no output supression # Run original command with no output supression
${ORIGINAL_TOOL} "$@" exec "${ORIGINAL_TOOL}" "$@"
EXIT_STATUS=$?
else else
# Run original command and capture output & exit status # Run original command and capture output & exit status
TMPFILE=$(mktemp /tmp/quiet-${TOOL}.XXXXXX) TMPFILE=$(mktemp "quiet-${TOOL}.XXXXXX")
${ORIGINAL_TOOL} "$@" > ${TMPFILE} 2>&1 "${ORIGINAL_TOOL}" "$@" > "${TMPFILE}" 2>&1
EXIT_STATUS=$? EXIT_STATUS=$?
if [[ $EXIT_STATUS -ne 0 ]]; then if [[ $EXIT_STATUS -ne 0 ]]; then
# On error, display the full output # On error, display the full output
cat ${TMPFILE} cat "${TMPFILE}"
fi fi
# Remove tmpfile # Remove tmpfile
rm ${TMPFILE} rm "${TMPFILE}"
fi
# Propagate the exit status # Propagate the exit status
exit $EXIT_STATUS exit $EXIT_STATUS
fi

View File

@ -11,20 +11,18 @@
# VERBOSE_LOGS=1 # VERBOSE_LOGS=1
# don't silence invocations containing these arguments # don't silence invocations containing these arguments
NO_SILENCE=" --version | test " NO_SILENCE=" --version | test"
TOOL=$(basename "$0") TOOL=$(basename "$0")
# Locate original tool # Locate original tool
ORIGINAL_TOOL=$(type -ap ${TOOL} | grep -v "$0" | head -n1 ) ORIGINAL_TOOL=$(type -ap "${TOOL}" | grep -v -Fx "$0" | head -n1 )
quote_args() { print_quoted_args() {
# similar to printf '%q' "$@" # similar to printf '%q' "$@"
# but produce more human-readable results for common/simple cases like "a b" # but produce more human-readable results for common/simple cases like "a b"
local args=("$@") for a in "$@"; do
s="" simple_pattern='^([[:alnum:]_+-]+=)?([[:alnum:] _=+-./:@]*)$'
for a in "${args[@]}"; do
simple_pattern='^([[:alnum:]_+-]+=)?([[:alnum:] _=+-]*)$'
if [[ $a =~ ' ' && $a =~ $simple_pattern ]]; then if [[ $a =~ ' ' && $a =~ $simple_pattern ]]; then
# a has spaces, but no other special characters that need escaping # a has spaces, but no other special characters that need escaping
# (quoting after removing spaces yields no backslashes) # (quoting after removing spaces yields no backslashes)
@ -33,39 +31,40 @@ quote_args() {
# CFLAGS=a b -> CFLAGS="a b" # CFLAGS=a b -> CFLAGS="a b"
q="${BASH_REMATCH[1]}\"${BASH_REMATCH[2]}\"" q="${BASH_REMATCH[1]}\"${BASH_REMATCH[2]}\""
else else
# get bash to do the quoting # get bash to do the quoting (which may result in no quotes or escaping,
# if none is needed).
q=$(printf '%q' "$a") q=$(printf '%q' "$a")
fi fi
s="$s $q" printf "%s " "$q"
done done
echo $s
} }
if [[ ! " $@ " =~ " --version " ]]; then if [[ ! " $* " =~ " --version " ]]; then
# Display the command being invoked - if it succeeds, this is all that will # Display the command being invoked - if it succeeds, this is all that will
# be displayed. Don't do this for invocations with --version, because # be displayed. Don't do this for invocations with --version, because
# this output is often parsed by scripts, so we don't want to modify it. # this output is often parsed by scripts, so we don't want to modify it.
echo "${TOOL} $(quote_args "$@")" printf %s "${TOOL} "
print_quoted_args "$@"
echo
fi fi
if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then if [[ " $@ " =~ $NO_SILENCE || -n "${VERBOSE_LOGS}" ]]; then
# Run original command with no output supression # Run original command with no output supression
${ORIGINAL_TOOL} "$@" exec "${ORIGINAL_TOOL}" "$@"
EXIT_STATUS=$?
else else
# Run original command and capture output & exit status # Run original command and capture output & exit status
TMPFILE=$(mktemp /tmp/quiet-${TOOL}.XXXXXX) TMPFILE=$(mktemp "quiet-${TOOL}.XXXXXX")
${ORIGINAL_TOOL} "$@" > ${TMPFILE} 2>&1 "${ORIGINAL_TOOL}" "$@" > "${TMPFILE}" 2>&1
EXIT_STATUS=$? EXIT_STATUS=$?
if [[ $EXIT_STATUS -ne 0 ]]; then if [[ $EXIT_STATUS -ne 0 ]]; then
# On error, display the full output # On error, display the full output
cat ${TMPFILE} cat "${TMPFILE}"
fi fi
# Remove tmpfile # Remove tmpfile
rm ${TMPFILE} rm "${TMPFILE}"
fi
# Propagate the exit status # Propagate the exit status
exit $EXIT_STATUS exit $EXIT_STATUS
fi