File: nh.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2024 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 # nh [options...] [files...]
  27 #
  28 #
  29 # Nice Hexadecimals is a byte-viewer which shows bytes as base-16 values,
  30 # using various ANSI styles to color-code output.
  31 #
  32 # Output lines end with a panel showing all ASCII sequences detected along.
  33 #
  34 # Options, where leading double-dashes are also allowed:
  35 #
  36 #     -h         show this help message
  37 #     -help      same as -h
  38 
  39 
  40 # handle help options
  41 case "$1" in
  42     -h|--h|-help|--help)
  43         awk '/^# +nh/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  44         exit 0
  45     ;;
  46 esac
  47 
  48 # if [ $# -gt 1 ]; then
  49 #     printf "\e[31mmultiple inputs not allowed\e[0m\n" > /dev/stderr
  50 #     exit 1
  51 # fi
  52 
  53 dashes=0
  54 for name in "${@:--}"; do
  55     if [ "${name}" = "-" ]; then
  56         dashes="$((dashes + 1))"
  57     fi
  58     if [ "${dashes}" -gt 1 ]; then
  59         printf "\e[31mcan't use dash/stdin multiple times\e[0m\n" > /dev/stderr
  60         exit 1
  61     fi
  62 done
  63 
  64 first=1
  65 
  66 for name in "${@:--}"; do
  67     od -A none -t u1 --width=16 "${name}" | awk '
  68         NR > 1 {
  69             for (i = 1; i <= pnf; i++) printf " %s", p[i]
  70             for (i = 1; i <= pnf; i++) printf " %s", p[i]
  71             for (i = 1; i <= NF; i++) printf " %s", $i
  72             printf "\n"
  73         }
  74 
  75         {
  76             pnf = NF
  77             for (i = 1; i <= NF; i++) p[i] = $i
  78         }
  79 
  80         END {
  81             if (NR > 0) {
  82                 for (i = 1; i <= pnf; i++) printf " %s", p[i]
  83                 for (i = 1; i <= pnf; i++) printf " %s", p[i]
  84                 if (pnf > 0) printf "\n"
  85             }
  86         }' | awk -v first="${first}" -v name="${name}" '
  87     BEGIN {
  88         vis1 = " !\"#$%& ()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  89         vis2 = "[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
  90         split(vis1 vis2, visible, "")
  91         l = length(visible)
  92         for (i = 1; i < l; i++) a[i + 32 - 1] = visible[i]
  93         a[39] = "'"'"'"
  94         a[126] = "~"
  95 
  96         if (name == "-") name = "<stdin>"
  97         if (!first) printf "\n\n"
  98         printf "• %s\n\n", name
  99         fflush()
 100     }
 101 
 102     function show_offset(n, s, len, lead, alt) {
 103         s = sprintf("%8d", n)
 104 
 105         while (match(s, /[0-9]{4,}/)) {
 106             len = RLENGTH
 107             lead = len % 3
 108             alt = lead > 0
 109 
 110             printf "%s", substr(s, 1, RSTART + lead - 1)
 111             s = substr(s, RSTART + lead)
 112             len -= lead
 113 
 114             while (len > 0) {
 115                 if (alt == 1) {
 116                     printf "\x1b[38;5;248m%s\x1b[0m", substr(s, 1, 3)
 117                 } else {
 118                     printf "%s", substr(s, 1, 3)
 119                 }
 120 
 121                 alt = 1 - alt
 122                 s = substr(s, 4)
 123                 len -= 3
 124             }
 125         }
 126 
 127         printf "%s", s
 128     }
 129 
 130     function show_byte(n) {
 131         if (n == 0) {
 132             printf "\x1b[38;5;111m%02x ", n
 133             return
 134         }
 135 
 136         if (n == 255) {
 137             printf "\x1b[38;5;209m%x ", n
 138             return
 139         }
 140 
 141         if (32 <= n && n <= 126) {
 142             printf "\x1b[38;5;72m%x\x1b[0m\x1b[48;5;254m%s", n, a[n]
 143             return
 144         }
 145 
 146         printf "\x1b[38;5;246m%02x ", n
 147     }
 148 
 149     NR % 5 == 1 && NR > 1 {
 150         printf "%8s  ", ""
 151         printf "           ·           ·           ·           ·\n"
 152     }
 153 
 154     {
 155         # printf "%8d  \x1b[48;5;254m", offset
 156         show_offset(offset)
 157         printf "  \x1b[48;5;254m"
 158 
 159         for (i = 1; i <= NF; i++) {
 160             if (i > 16) break
 161             show_byte($i)
 162         }
 163 
 164         printf "\x1b[0m"
 165 
 166         runs = 0
 167         asciirun = 0
 168         short = (NF < 2 * 16)
 169         pad = short ? sprintf("%*s", 3 * (NF - 16) + 2, "") : "  "
 170 
 171         for (i = short ? NF / 2 : 16 + 1; i <= NF; i++) {
 172             n = $i
 173 
 174             if (32 < n && n <= 126) {
 175                 if (!asciirun) {
 176                     asciirun = 1
 177                     runs++
 178                     printf (runs > 1) ? " " : pad
 179                 }
 180 
 181                 printf "%s", a[n]
 182                 continue
 183             }
 184 
 185             asciirun = 0
 186         }
 187 
 188         printf "\n"
 189         fflush()
 190 
 191         offset += 16
 192     }'
 193 
 194     first=0
 195 done