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 # fail quits the script right after showing the message given, using 54 # exit code given as its 2nd arg 55 fail() { 56 printf "\e[31m%s\e[0m\n" "$1" > /dev/stderr 57 exit "${2:-1}" 58 } 59 60 names=0 61 dashes=0 62 for name in "${@:--}"; do 63 if [ -z "${name}" ]; then 64 continue 65 fi 66 names="$((names + 1))" 67 68 if [ "${name}" = "-" ]; then 69 dashes="$((dashes + 1))" 70 fi 71 72 if [ "${dashes}" -gt 1 ]; then 73 printf "\e[31mcan't use dash/stdin multiple times\e[0m\n" > /dev/stderr 74 exit 1 75 fi 76 done 77 78 for name in "${@:--}"; do 79 if [ -z "${name}" ]; then 80 continue 81 fi 82 83 # if the argument given is an existing file, just use `cat`, even if the 84 # name looks like a protocol presumably meant for `curl` 85 if [ -e "${name}" ]; then 86 cat "${name}" || fail "failed to read file '${name}'" $? 87 continue 88 fi 89 90 case "${name}" in 91 -h|--h|-help|--help) 92 awk '/^# +get /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 93 exit 0 94 ;; 95 96 http://*|https://*|ftp://*) 97 # wget -O - "${name}" || fail "failed to fetch URI '${name}'" $? 98 curl -s -L "${name}" || fail "failed to fetch URI '${name}'" $? 99 ;; 100 101 dict://*|ftps://*|gopher://*|gophers://*|rtmp://*|rtsp://*|scp://*|\ 102 sftp://*|smb://*|smbs://*|telnet://*|tftp://*) 103 curl -s -L "${name}" || fail "failed to fetch URI '${name}'" $? 104 ;; 105 106 data:*) 107 # handle data-URIs 108 { 109 echo "${name}" | sed -E 's-^data:.{0,50};base64,--' | base64 -d 110 } || fail "failed to decode data-URI '${name}'" $? 111 ;; 112 113 file://*) 114 cat "$(echo "${name}" | sed 's-^file://--')" 2> /dev/null || 115 fail "failed to open file '${name}'" $? 116 ;; 117 118 -) 119 # handle standard input 120 cat "${name}" 2> /dev/null || 121 fail "failed to read from the standard input" $? 122 ;; 123 124 *) 125 # handle files 126 cat "${name}" 2> /dev/null || 127 fail "failed to open file '${name}'" $? 128 ;; 129 esac 130 done 131 132 if [ "${names}" -eq 0 ]; then 133 cat 134 exit $? 135 fi