From dc069500a00c0ba3779c5f2e1664f8f5e71586bc Mon Sep 17 00:00:00 2001 From: Ian Kelling Date: Mon, 4 Nov 2019 16:29:16 -0500 Subject: [PATCH] Make interactive catch function ignore bash-completion And allow that to be extended based on string globing of filenames. --- err | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/err b/err index f6a7042..db7dcdb 100644 --- a/err +++ b/err @@ -98,42 +98,60 @@ err-catch() { # For interactive shells: on error, print stack trace and return # # Globals: +# err_catch_ignore Array containing glob patterns to test against filenames to ignore +# errors from. Initialized to ignore bash-completion scripts on debian +# based systems. # _err_func_last Used internally. # _err_catch_err Used internally. # _err_catch_i Used internally. +# _err_catch_ignore Used internally. # # misc: All shellcheck disables for this function are false positives. ####################################### # shellcheck disable=SC2120 err-catch-interactive() { + err_catch_ignore=( + '/etc/bash_completion.d/*' + ) # shellcheck disable=SC2034 declare -i _err_func_last=0 set -E; shopt -s extdebug # shellcheck disable=SC2154 trap '_err_catch_err=$? _trap_bc="$BASH_COMMAND" - if (( ${#FUNCNAME[@]} > _err_func_last )); then - echo ERR: \`$_trap_bc'"\'"' returned $_err_catch_err - fi - _err_func_last=${#FUNCNAME[@]} - if (( _err_func_last )); then - printf " from %s:%s:in \`%s" "${BASH_SOURCE[0]}" "$(declare -F "${FUNCNAME[0]}"|awk "{print \$2}")" "${FUNCNAME[0]}" - if shopt extdebug >/dev/null; then - for ((_err_catch_i=${BASH_ARGC[0]}-1; _err_catch_i >= 0; _err_catch_i--)); do - printf " %s" "${BASH_ARGV[_err_catch_i]}" - done + _err_catch_ignore=false + for _err_catch_i in "${err_catch_ignore[@]}"; do + if [[ ${BASH_SOURCE[0]} == $_err_catch_i ]]; then + _err_catch_ignore=true + break + fi + done + if ! $_err_catch_ignore; then + if (( ${#FUNCNAME[@]} > _err_func_last )); then + echo ERR: \`$_trap_bc'"\'"' returned $_err_catch_err + fi + _err_func_last=${#FUNCNAME[@]} + if (( _err_func_last )); then + printf " from %s:%s:in \`%s" "${BASH_SOURCE[0]}" "$(declare -F "${FUNCNAME[0]}"|awk "{print \$2}")" "${FUNCNAME[0]}" + if shopt extdebug >/dev/null; then + for ((_err_catch_i=${BASH_ARGC[0]}-1; _err_catch_i >= 0; _err_catch_i--)); do + printf " %s" "${BASH_ARGV[_err_catch_i]}" + done + fi + echo '"\'"' + return $_err_catch_err fi - echo '"\'"' - return $_err_catch_err fi' ERR set -o pipefail } ####################################### -# Undoes err-catch. turns off exit and stack trace on error. +# Undoes err-catch/err-catch-interactive ####################################### err-allow() { - set +E +o pipefail; trap ERR + shopt -u extdebug + set +E +o pipefail + trap ERR } ####################################### -- 2.25.1