fix bug after a bash completion
[errhandle.git] / err
diff --git a/err b/err
index 816207083ce11fdd47e9f1f2d9c0fbe18f090707..5d15407638146dedb81aa36783940e8efb8edcf4 100644 (file)
--- a/err
+++ b/err
@@ -60,9 +60,9 @@ err-catch() {
   set -E;
   # This condition avoids starting the bash debugger in the case that
   # this is sourced from a startup file, and you use a login shell to
-  # run a command. Avoid doing that if you want function arguments in
-  # your trace.
-  if [[ $- != *c* ]] || ! shopt login_shell >/dev/null; then
+  # run a command. eg: bash -l some-command. Avoid doing that if you want
+  # function arguments in your trace.
+  if ! shopt login_shell >/dev/null; then
     shopt -s extdebug
   fi
   _err-trap() {
@@ -95,7 +95,20 @@ err-catch() {
 #######################################
 
 _err-bash-trace-interactive() {
-  local ret bash_command argc pattern i
+  if (( ${#FUNCNAME[@]} <= 1 )); then
+    return 0
+  fi
+
+  for pattern in "${err_catch_ignore[@]}"; do
+    # shellcheck disable=SC2053
+    if [[ ${BASH_SOURCE[1]} == $pattern ]]; then
+      return 0
+    fi
+  done
+
+  local ret bash_command argc pattern i last
+  last=$_err_func_last
+  _err_func_last=${#FUNCNAME[@]}
   # We have these passed to us because they are lost inside the
   # function.
   ret=$1
@@ -103,26 +116,19 @@ _err-bash-trace-interactive() {
   argc=$(( $3 - 1 ))
   shift 3
   argv=("$@")
-  for pattern in "${err_catch_ignore[@]}"; do
-    # shellcheck disable=SC2053
-    if [[ ${BASH_SOURCE[0]} == $pattern ]]; then
-      return 0
-    fi
-  done
-  if (( ${#FUNCNAME[@]} > _err_func_last )); then
+  # The trap returns a nonzero, then gets called again. This condition
+  # tells us if we are the first.
+  if (( _err_func_last > last  )); then
     echo ERR: \`$bash_command\' returned $ret
   fi
-  _err_func_last=${#FUNCNAME[@]}
-  if (( _err_func_last > 1 )); then
-    printf "  from \`%s" "${FUNCNAME[1]}"
-    if shopt extdebug >/dev/null; then
-      for ((i=argc; i >= 0; i--)); do
-        printf " %s" "${argv[i]}"
-      done
-    fi
-    printf "\' defined at %s:%s\n" "${BASH_SOURCE[1]}" "$(declare -F "${FUNCNAME[1]}"|awk "{print \$2}")"
-    return $ret
+  printf "  from \`%s" "${FUNCNAME[1]}"
+  if shopt extdebug >/dev/null; then
+    for ((i=argc; i >= 0; i--)); do
+      printf " %s" "${argv[i]}"
+    done
   fi
+  printf "\' defined at %s:%s\n" "${BASH_SOURCE[1]}" "$(declare -F "${FUNCNAME[1]}"|awk "{print \$2}")"
+  return $ret
 }
 
 #######################################
@@ -148,13 +154,13 @@ err-catch-interactive() {
   declare -i _err_func_last=0
   set -E; shopt -s extdebug
   # shellcheck disable=SC2154
-  trap '_err-bash-trace-interactive $? "$BASH_COMMAND" ${BASH_ARGC[0]} "${BASH_ARGV[@]}"' ERR
+  trap '_err-bash-trace-interactive $? "$BASH_COMMAND" ${BASH_ARGC[0]} "${BASH_ARGV[@]}" || return $?' ERR
   set -o pipefail
 }
 
 
 #######################################
-# Undoes err-catch/err-catch-interactive
+# Undo err-catch/err-catch-interactive
 #######################################
 err-allow() {
   shopt -u extdebug