File: get.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 # get [options...] [filepaths/URIs/data-URIs...]
  27 #
  28 # Get data from the named source given, whether this is a filename, a URI,
  29 # or even a base64-encoded data-URI. Data-URIs are simply decoded into the
  30 # bytes they represent, no loading/fetching required.
  31 #
  32 # The options are, available both in single and double-dash versions
  33 #
  34 #   -h, -help    show this help message
  35 
  36 
  37 # fail quits the script right after showing the message given, using the
  38 # exit code given as its 2nd arg
  39 fail() {
  40     printf "%s\n" "$1" >&2
  41     exit "${2:-1}"
  42 }
  43 
  44 
  45 case "$1" in
  46     -h|--h|-help|--help)
  47         awk '/^# +get /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  48         exit 0
  49     ;;
  50 
  51     -) ;;
  52 
  53     --) shift ;;
  54 
  55     -*)
  56         printf "unsupported option '%s'\n" "$1" >&2
  57         exit 1
  58     ;;
  59 esac
  60 
  61 dashes=0
  62 for name in "${@:--}"; do
  63     if [ "${name}" = "-" ]; then
  64         dashes="$((dashes + 1))"
  65     fi
  66 
  67     if [ "${dashes}" -gt 1 ]; then
  68         break
  69     fi
  70 done
  71 
  72 if [ "${dashes}" -gt 1 ] && [ ! -e '-' ]; then
  73     printf "can't use dash/stdin multiple times\n" >&2
  74     exit 1
  75 fi
  76 
  77 for name in "${@:--}"; do
  78     if [ -z "${name}" ]; then
  79         continue
  80     fi
  81 
  82     # if the argument given is an existing file, just use `cat`, even if the
  83     # name looks like a protocol presumably meant for `curl`
  84     if [ -e "${name}" ]; then
  85         cat "${name}" || fail "failed to read file '${name}'" $?
  86         continue
  87     fi
  88 
  89     case "${name}" in
  90         # handle the commonest kinds of URIs
  91         dict://*|ftps://*|gopher://*|gophers://*|http://*|https://*|\
  92         rtmp://*|rtsp://*|scp://*|sftp://*|telnet://*|tftp://*)
  93             curl -s -L "${name}" || fail "failed to fetch URI '${name}'" $?
  94         ;;
  95 
  96         # handle data-URIs
  97         data:,) continue;;
  98         data:*)
  99             {
 100                 echo "${name}" | sed -E 's-^data:.{0,50};base64,--' | base64 -d
 101             } || fail "failed to decode data-URI '${name}'" $?
 102         ;;
 103 
 104         # handle files
 105         file://*)
 106             cat "$(echo "${name}" | sed 's-^file://--')" 2> /dev/null ||
 107                 fail "failed to open file '${name}'" $?
 108         ;;
 109 
 110         # handle standard input
 111         -)
 112             cat "${name}" 2> /dev/null ||
 113                 fail "failed to read from the standard input" $?
 114         ;;
 115 
 116         # file not found
 117         *)
 118             fail "no file named '${name}'" 1
 119         ;;
 120     esac
 121 done