File: sideshow.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the "Software"), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # sideshow [options...] [command] [args...]
  27 #
  28 #
  29 # Run the command given, emitting its output lines to the standard error,
  30 # while remembering each line read from the standard input. Once the command
  31 # is done, emit all original standard input lines to the standard output.
  32 #
  33 # If not given a command, all input lines are emitted to the standard error
  34 # as they come, remembermed, and are finally emitted all at once to the
  35 # standard output at the end.
  36 #
  37 # This tool assumes the standard input is lines of text, and will almost
  38 # certainly corrupt other kinds of (binary) data.
  39 #
  40 # The options are, available both in single and double-dash versions
  41 #
  42 #   -h, -help       show this help message
  43 #   -v, -verbose    show an ANSI-styled banner for the command being run
  44 #   -0, -null       read/treat null-byte-delimited chunks as input lines;
  45 #                   standard output lines are all delimited with null bytes,
  46 #                   which may add an extra null byte at the very end
  47 
  48 
  49 nil=0
  50 verbose=0
  51 
  52 while [ $# -gt 0 ]; do
  53     case "$1" in
  54         -h|--h|-help|--help)
  55             awk '/^# +sideshow /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  56             exit 0
  57         ;;
  58 
  59         -v|--v|-verbose|--verbose) verbose=1; shift; continue ;;
  60 
  61         -0|--0|-null|--null) nil=1; shift; continue ;;
  62 
  63         --) shift; break ;;
  64 
  65         -*)
  66             printf "%s: unsupported option '%s'\n" "$0" "$1" >&2
  67             exit 1
  68         ;;
  69     esac
  70 
  71     break
  72 done
  73 
  74 if [ $# -eq 0 ]; then
  75     emit='print > "/dev/stderr"'
  76 else
  77     emit='print | cmd'
  78 fi
  79 
  80 awk -v nil="${nil}" -v verbose="${verbose}" -v cmd="$*" '
  81     BEGIN {
  82         if (nil) ORS = RS = "\000"
  83         gsub(/^ *| *$/, "", cmd)
  84         if (cmd != "") cmd = cmd " > /dev/stderr"
  85         if (verbose) printf("\x1b[7m%-80s\x1b[0m\n", cmd) > "/dev/stderr"
  86     }
  87 
  88     {
  89         lines[NR] = $0
  90         '"${emit}"'
  91     }
  92 
  93     END { for (i = 1; i <= NR; i++) print lines[i] }
  94 '