File: dog.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 # 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 33 # fail quits the script right after showing the message given, using the 34 # exit code given as its 2nd arg 35 fail() { 36 printf "%s\n" "$1" >&2 37 exit "${2:-1}" 38 } 39 40 # when no args are given, just show the help message and quit 41 if [ $# -eq 0 ]; then 42 awk '/^# +dog /, /^$/ { gsub(/^# ?/, ""); print }' "$0" >&2 43 exit 1 44 fi 45 46 case "$1" in 47 -h|--h|-help|--help) 48 awk '/^# +dog /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 49 exit 0 50 ;; 51 esac 52 53 [ "$1" = '--' ] && shift 54 55 # ensure single dashes aren't used multiple times 56 dashes=0 57 for name in "$@"; do 58 if [ "${name}" = "-" ]; then 59 if [ "$dashes" -gt 0 ]; then 60 fail "can't use stdin (single-dash) more than once" 1 61 fi 62 dashes=1 63 fi 64 done 65 66 for name in "$@"; do 67 case "${name}" in 68 # handle a dash by reading from stdin 69 -) cat || exit $?;; 70 71 # handle URIs 72 dict://*|file://*|ftp://*|ftps://*|gopher://*|gophers://*|http://*\ 73 |https://*|rtmp://*|rtsp://*|scp://*|sftp://*|telnet://*|tftp://*) 74 curl -s -L "${name}" || fail "failed to fetch URI ${name}" $?;; 75 76 # handle data-URIs 77 data:,) continue;; 78 data:*) { 79 printf "%s" "${name}" | sed -E 's-^data:.{0,50};base64,--' | 80 base64 -d 81 } || fail "failed to decode data-URI ${name}" $?;; 82 83 # handle files and bytes-mode data-URIs 84 *) cat "${name}" || fail "failed to open file ${name}" $?;; 85 esac 86 done