File: c.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-2025 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 # c [options...] [filenames/URIs...]
  27 #
  28 # Concatenate bytes from all the named sources given, in the order given.
  29 # Each named source can be a file, a web (HTTP/HTTPS) resource, or even a
  30 # base64-encoded data-URI. Data-URIs are simply decoded into the bytes they
  31 # represent, no loading/fetching required.
  32 #
  33 # The name `-` (a minus, or ASCII dash) means read all bytes from standatd
  34 # input. When not given any named source, the standard input is also read
  35 # in full, by default.
  36 #
  37 # The only options show this help message, via any of `-h`, `--h`, `-help`,
  38 # or `--help`.
  39 
  40 
  41 case "$1" in
  42     -h|--h|-help|--help)
  43         awk '/^# +c /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  44         exit 0
  45     ;;
  46 esac
  47 
  48 if [ $# -eq 0 ]; then
  49     cat
  50     exit $?
  51 fi
  52 
  53 names=0
  54 dashes=0
  55 for name in "${@:--}"; do
  56     if [ -z "${name}" ]; then
  57         continue
  58     fi
  59     names="$((names + 1))"
  60 
  61     if [ "${name}" = "-" ]; then
  62         dashes="$((dashes + 1))"
  63     fi
  64 
  65     if [ "${dashes}" -gt 1 ]; then
  66         printf "\e[31mcan't use dash/stdin multiple times\e[0m\n" > /dev/stderr
  67         exit 1
  68     fi
  69 done
  70 
  71 for name in "${@:--}"; do
  72     if [ -z "${name}" ]; then
  73         continue
  74     fi
  75 
  76     # if the argument given is an existing file, just use `cat`, even if the
  77     # name looks like a protocol presumably meant for `curl`
  78     if [ -e "${name}" ]; then
  79         cat "${name}" || fail "failed to read file '${name}'" $?
  80         continue
  81     fi
  82 
  83     case "${name}" in
  84         -h|--h|-help|--help)
  85             awk '/^# +get /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  86             exit 0
  87         ;;
  88 
  89         http://*|https://*|ftp://*)
  90             # handle the commonest kinds of URIs
  91             # wget -O - "${name}" || fail "failed to fetch URI '${name}'" $?
  92             curl -s -L "${name}" || fail "failed to fetch URI '${name}'" $?
  93         ;;
  94 
  95         dict://*|ftps://*|gopher://*|gophers://*|rtmp://*|rtsp://*|scp://*|\
  96         sftp://*|smb://*|smbs://*|telnet://*|tftp://*)
  97             # handle more kinds of URIs
  98             curl -s -L "${name}" || fail "failed to fetch URI '${name}'" $?
  99         ;;
 100 
 101         data:*)
 102             # handle data-URIs
 103             {
 104                 echo "${name}" | sed -E 's-^data:.{0,50};base64,--' | base64 -d
 105             } || fail "failed to decode data-URI '${name}'" $?
 106         ;;
 107 
 108         file://*)
 109             # handle files
 110             cat "$(echo "${name}" | sed 's-^file://--')" 2> /dev/null ||
 111                 fail "failed to open file '${name}'" $?
 112         ;;
 113 
 114         -)
 115             # handle standard input
 116             cat "${name}" 2> /dev/null ||
 117                 fail "failed to read from the standard input" $?
 118         ;;
 119 
 120         *)
 121             # handle files
 122             cat "${name}" 2> /dev/null ||
 123                 fail "failed to open file '${name}'" $?
 124         ;;
 125     esac
 126 done
 127 
 128 if [ "${names}" -eq 0 ]; then
 129     cat
 130     exit $?
 131 fi