Make interactive catch function ignore bash-completion
authorIan Kelling <iank@fsf.org>
Mon, 4 Nov 2019 21:29:16 +0000 (16:29 -0500)
committerIan Kelling <iank@fsf.org>
Mon, 4 Nov 2019 21:29:16 +0000 (16:29 -0500)
And allow that to be extended based on string globing
of filenames.

err

diff --git a/err b/err
index f6a7042eb0f5b0dd644b4829b881cc6aee92a999..db7dcdb4fe1e8eb42ffd37d8f523db3e9fdb3390 100644 (file)
--- 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
 }
 
 #######################################