mostly fixes for new error handling
[distro-setup] / brc
diff --git a/brc b/brc
index 33e664616c36487ad7f0b426e6d3c25e303eea3f..f9439887573b4f0eed84950526c8ada10ae265f6 100644 (file)
--- a/brc
+++ b/brc
@@ -3,11 +3,14 @@
 # SPDX-License-Identifier: AGPL-3.0-or-later
 # this gets sourced. shebang is just for file mode detection
 
-# note, to catch errors in functions but not outside, do:
-# set -E -o pipefail
-# trap return ERR
-# trap 'trap ERR' RETURN
-
+# Use source ~/.bashrc instead of doing bash -l when running a script
+# so this can set extdebug and avoid the bash debugger.
+if [[ -s /a/bin/errhandle/err ]]; then
+  source /a/bin/errhandle/err
+elif [[ -s ~/.iank/err ]]; then
+  # shellcheck source=/a/bin/errhandle/err
+  source ~/.iank/err
+fi
 
 # * settings
 
@@ -62,7 +65,7 @@ if [[ $RLC_INSIDE_EMACS ]]; then
   export PAGER=cat
   export MANPAGER=cat
   # scp completion does not work, but this doesnt fix it. todo, figure this out
-  complete -r scp &> /dev/null
+  #complete -r scp &> /dev/null
   # todo, remote file completion fails, figure out how to turn it off
   export NODE_DISABLE_COLORS=1
   # This gets rid of ugly terminal escape chars in node repl
@@ -199,12 +202,13 @@ if [[ -s /usr/share/wcd/wcd-include.sh ]]; then
 fi
 
 if [[ -s /a/bin/small-misc-bash/ll-function ]]; then
-  # shellcheck source=/a/bin/small-misc-bash/ll-function
   source /a/bin/small-misc-bash/ll-function
 elif [[ -s ~/.iank/ll-function ]]; then
+  # shellcheck source=/a/bin/small-misc-bash/ll-function
   source ~/.iank/ll-function
 fi
 
+
 # * functions
 
 
@@ -299,7 +303,7 @@ caf() {
   find -L $1 -type f -not \( -name .svn -prune -o -name .git -prune \
        -o -name .hg -prune -o -name .editor-backups -prune \
        -o -name .undo-tree-history -prune \) \
-       -exec bash -lc 'hr; echo "$1"; hr; cat "$1"' _ {} \; 2>/dev/null
+       -exec bash -c '. ~/.bashrc; hr; echo "$1"; hr; cat "$1"' _ {} \; 2>/dev/null
 
 }
 
@@ -412,6 +416,39 @@ despace() {
   done
 }
 
+dig() {
+  command dig +nostats +nocmd "$@"
+}
+# Output with sections sorted, and removal of query id, so 2 dig outputs can be diffed.
+digsort() {
+  local sec
+  sec=
+  dig +nordflag "$@" | sed -r 's/^(;; ->>HEADER<<-.*), id: .*/\1/' | while read -r l; do
+    if [[ $l == [^\;]* ]]; then
+      sec+="$l"$'\n'
+    else
+      if [[ $sec ]]; then
+        printf "%s" "$sec" | sort
+        sec=
+      fi
+      printf "%s\n" "$l"
+    fi
+  done
+}
+# compare digs to the 2 servers
+# usage: digdiff @server1 @server2 DIG_ARGS
+# note: only the soa master nameserver will respond with
+# ra "recursive answer" flag. That difference is meaningless afaik.
+digdiff() {
+  local s1 s2
+  s1=$1
+  shift
+  s2=$1
+  shift
+  digsort $s1 "$@" | tee /tmp/digdiff
+  diff -u /tmp/digdiff <(digsort $s2 "$@")
+}
+
 dt() {
   date "+%A, %B %d, %r" "$@"
 }
@@ -424,10 +461,25 @@ dus() { # du, sorted, default arg of
 
 e() { echo "$@"; }
 
-# echo var. print var including escapes, etc
+# echo args
+ea() {
+  if (( ! $# )); then
+    echo no args
+  fi
+  for arg; do
+    printf "%qEOL\n" "${arg}"
+    printf "%s" "${arg}" |& hexdump -C
+  done
+}
+# echo vars. print var including escapes, etc
 ev() {
-  printf "%qEOL\n" "${!1}"
-  printf "%s" "${!1}" |& hexdump -C
+  if (( ! $# )); then
+    echo no args
+  fi
+  for arg; do
+    printf "%qEOL\n" "${!arg}"
+    printf "%s" "${!arg}" |& hexdump -C
+  done
 }
 
 
@@ -444,7 +496,7 @@ eless() {
   less /var/log/exim4/mainlog
 }
 eqcat() {
-  exiqgrep -i | while read i; do
+  exiqgrep -i | while read -r i; do
     exim -Mvh $i; hr; exim -Mvb $i; hr;
     exigrep $i /var/log/exim4/mainlog; hr
   done
@@ -580,10 +632,12 @@ gr() {
 }
 
 grr() { # grep recursive
+  # Don't return 1 on nonmatch because this is meant to be
+  # interactive, not in a conditional.
   if [[ ${#@} == 1 ]]; then
-    grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@" .
+    grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@" . || [[ $? == 1 ]]
   else
-    grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@"
+    grep --exclude-dir='*.emacs.d' --exclude-dir='*.git' -RiIP --color=auto "$@" || [[ $? == 1 ]]
   fi
 }
 rg() {
@@ -591,7 +645,8 @@ rg() {
 }
 
 hr() { # horizontal row. used to break up output
-  printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(seq ${COLUMNS:-60})
+
+  printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}")
   echo
 }
 
@@ -791,6 +846,23 @@ pk1() {
   esac
 }
 
+psg () {
+  local x y help
+  help="Usage: psg [--help] GREP_ARGS
+grep ps and output in a nice format"
+  if [[ $1 == --help ]]; then
+    echo "$help"
+    return
+  fi
+  x=$(sudo ps -eF)
+  # final grep is because some commands tend to have a lot of trailing spaces
+  y=$(echo "$x" | grep -iP "$@" | grep -o '.*[^ ]') ||:
+  if [[ $y ]]; then
+    echo "$x" | head -n 1
+    echo "$y"
+  fi
+}
+
 pubip() { curl -4s https://icanhazip.com; }
 pubip6() { curl -6s https://icanhazip.com; }
 whatismyip() { pubip; }
@@ -821,6 +893,7 @@ q() { # start / launch a program in the backround and redir output to null
 # shellcheck disable=SC2120
 r() {
   history -a # save history
+  trap ERR # this avoids a segfault
   exit ${1:0}
   # i had this redir, not sure why
   #  exit "$@" 2>/dev/null
@@ -874,6 +947,7 @@ s() {
   # with root owned files.
   #
   if [[ $EUID != 0 || $1 == -* ]]; then
+    # shellcheck disable=SC2034
     SUDOD="$PWD" sudo -i "$@"
   else
     "$@"
@@ -1197,6 +1271,7 @@ if [[ $- == *i* ]]; then
     local return=$? # this MUST COME FIRST
     local ps_char ps_color
     unset IFS
+
     history -a # save history
 
     case $return in