#!/bin/sh # The MIT License (MIT) # # Copyright © 2020-2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # get [options...] [filepaths/URIs/data-URIs...] # # Get data from the named source given, whether this is a filename, a URI, # or even a base64-encoded data-URI. Data-URIs are simply decoded into the # bytes they represent, no loading/fetching required. # # The only options show this help message, via any of `-h`, `--h`, `-help`, # or `--help`. # set -o pipefail # fail quits the script right after showing the message given, using # exit code given as its 2nd arg fail() { printf "\e[31m%s\e[0m\n" "$1" > /dev/stderr exit "${2:-1}" } if [ $# -eq 0 ]; then cat exit $? fi dashes=0 for name in "${@:--}"; do if [ "${name}" = "-" ]; then dashes="$((dashes + 1))" fi if [ "${dashes}" -gt 1 ]; then printf "\e[31mcan't use dash/stdin multiple times\e[0m\n" > /dev/stderr exit 1 fi done for name in "${@:--}"; do if [ -z "${name}" ]; then continue fi # if the argument given is an existing file, just use `cat`, even if the # name looks like a protocol presumably meant for `curl` if [ -e "${name}" ]; then cat "${name}" || fail "failed to read file '${name}'" $? continue fi case "${name}" in -h|--h|-help|--help) awk '/^# +get /, /^$/ { gsub(/^# ?/, ""); print }' "$0" exit 0 ;; http://*|https://*|ftp://*) # handle the commonest kinds of URIs # wget -O - "${name}" || fail "failed to fetch URI '${name}'" $? curl -s -L "${name}" || fail "failed to fetch URI '${name}'" $? ;; dict://*|ftps://*|gopher://*|gophers://*|rtmp://*|rtsp://*|scp://*|\ sftp://*|smb://*|smbs://*|telnet://*|tftp://*) # handle more kinds of URIs curl -s -L "${name}" || fail "failed to fetch URI '${name}'" $? ;; data:*) # handle data-URIs { echo "${name}" | sed -E 's-^data:.{0,50};base64,--' | base64 -d } || fail "failed to decode data-URI '${name}'" $? ;; file://*) # handle files cat "$(echo "${name}" | sed 's-^file://--')" 2> /dev/null || fail "failed to open file '${name}'" $? ;; -) # handle standard input cat "${name}" 2> /dev/null || fail "failed to read from the standard input" $? ;; *) # handle files cat "${name}" 2> /dev/null || fail "failed to open file '${name}'" $? ;; esac done