File: dog.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 # dog [options...] [paths/URIs...]
  27 #
  28 # Dog fetches data from the named sources given to it, whether these are
  29 # filenames or URIs. Single dashes stand for standard input, and can't be
  30 # used more than once. When no names are given, stdin is read by default.
  31 #
  32 # A line-mode is available via leading option `-l`, or its aliases `--l`,
  33 # `-lines`, and `--lines`. This mode turns all CRLF byte-pairs into single
  34 # LF bytes, and ensures all non-empty inputs end with a final LF byte,
  35 # which avoids accidentally joining lines across different inputs.
  36 #
  37 # Line-mode also ignores leading UTF-8 BOMs on each input's first line.
  38 
  39 
  40 # fix_lines ensures CRLF byte-pairs become single LF bytes, and that
  41 # leading UTF-8 BOMs on the first lines of each input are ignored
  42 fix_lines() {
  43     awk 'FNR == 1 { gsub(/^\xef\xbb\xbf/, "") } 1' | sed -E 's-\r$--'
  44 }
  45 
  46 # fail quits the script right after showing the message given, using
  47 # exit code given as its 2nd arg
  48 fail() {
  49     printf "\x1b[31m%s\x1b[0m\n" "$1" > /dev/stderr
  50     exit "$2"
  51 }
  52 
  53 # when no args are given, just show the help message and quit
  54 if [ $# -eq 0 ]; then
  55     fix_lines
  56     exit $?
  57 fi
  58 
  59 # handle leading options
  60 lines=0
  61 case "$1" in
  62     -h|--h|-help|--help)
  63         # show help message, extracting the info-comment at the start
  64         # of this file, and quit
  65         awk '/^# +dog/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  66         exit 0
  67     ;;
  68     -l|--l|-lines|--lines)
  69         # enable line-mode
  70         lines=1
  71         # don't confuse this option as a named input, later on
  72         shift
  73     ;;
  74 esac
  75 
  76 # ensure single dashes aren't used multiple times
  77 dashes=0
  78 for a in "$@"; do
  79     if [ "$a" = "-" ]; then
  80         if [ "$dashes" -gt 0 ]; then
  81             fail "can't use stdin (single-dash) more than once" 1
  82         fi
  83         dashes=1
  84     fi
  85 done
  86 
  87 # ensure errors propagate correctly, even when in line-mode
  88 # set -o pipefail
  89 
  90 for a in "$@"; do
  91     case "$a" in
  92         -)
  93             # handle a dash by reading from stdin
  94             if [ "$lines" -eq 1 ]; then
  95                 fix_lines || exit $?
  96             else
  97                 cat || exit $?
  98             fi
  99         ;;
 100 
 101         dict://*|file://*|ftp://*|ftps://*|gopher://*|gophers://*|\
 102         http://*|https://*|rtmp://*|rtsp://*|scp://*|sftp://*|\
 103         smb://*|smbs://*|telnet://*|tftp://*)
 104             # handle URIs
 105             if [ "$lines" -eq 1 ]; then
 106                 (curl -s -L "$a" | fix_lines) || \
 107                     fail "failed to fetch URI $a" $?
 108             else
 109                 curl -s -L "$a" || fail "failed to fetch URI $a" $?
 110             fi
 111         ;;
 112 
 113         data:*)
 114             # handle data-URIs
 115             if [ "$lines" -eq 1 ]; then
 116                 (printf "%s" "$a" | sed 's-^.*,--1' | base64 -d | awk 1) || \
 117                     fail "failed to decode data-URI $a" $?
 118             else
 119                 (printf "%s" "$a" | sed 's-^.*,--1' | base64 -d) || \
 120                     fail "failed to decode data-URI $a" $?
 121             fi
 122         ;;
 123 
 124         *)
 125             # handle files and bytes-mode data-URIs
 126             if [ "$lines" -eq 1 ]; then
 127                 (cat "$a" | fix_lines) || fail "failed to open file $a" $?
 128             else
 129                 cat "$a" || fail "failed to open file $a" $?
 130             fi
 131         ;;
 132     esac
 133 done