diff options
-rwxr-xr-x | gophed | 93 |
1 files changed, 45 insertions, 48 deletions
@@ -1,81 +1,78 @@ #!/bin/sh -PROMPT="==" -SHOW="show" +PROMPT='==' +SHOW='show' cleanup () { - rm -f $TMPFILE; + rm -f "$TMPFILE"; } go () { - printf "${1}\r\n" | nc $2 $3 + printf '%s\r\n' "$1" | nc "$2" "$3" } usage() { - printf "usage: \n" - printf " visit a resource:\n" - printf " $0 <path> <server> <port>\n" - printf " init links:\n" - printf " $0 -i \n" - printf " show this help:\n" - printf " $0 -h\n" - printf "\n" - printf "In a gopher page, just type:\n" - printf " !./g %% LINE\n" - printf "to visit the resource at LINE, or:\n" - printf " !./d %% LINE\n" - printf "to download the resource at LINE\n\n" + printf '%s\n' \ + 'usage:' \ + ' visit a resource:' \ + " $0 <path> <server> <port>" \ + ' init links:' \ + " $0 -i" \ + ' show this help:' \ + " $0 -h" \ + '' \ + 'In a gopher page, just type:' \ + " !./g %% LINE" \ + 'to visit the resource at LINE, or:' \ + " !./d %% LINE" \ + 'to download the resource at LINE\n' exit 0 } trap cleanup INT QUIT HUP KILL ABRT TERM -if [ $# -lt 1 ]; then - usage -fi - -if [ "$1" = "-h" ]; then usage; fi +[ "$#" -lt 1 ] || [ "$1" = -h ] && usage -if [ "$0" = "gophed" -a "$1" = "-i" ]; then - ln -sf $0 g - ln -sf $0 d - ln -sf $0 v +if [ "$0" = gophed ] && [ "$1" = -i ]; then + ln -sf "$0" g + ln -sf "$0" d + ln -sf "$0" v exit 0 fi -script_name=$(basename $0) +script_name="$(basename "$0")" -if [ "${script_name}" = "gophed" -o "${script_name}" = "v" ]; then - sel=$(echo $1 | sed -r 's:/:+:g') - TMPFILE=$(mktemp /tmp/gophed_$2_${sel}_$3.XXXXXXXXXXXXXXXXXXX) - go "$1" "$2" "$3" > $TMPFILE - cat $SHOW - | ed -p ${PROMPT} $TMPFILE +if [ "$script_name" = gophed ] || [ "$script_name" = v ]; then + sel="$(printf '%s' "$1" | sed -r 's:/:+:g')" + TMPFILE="$(mktemp "/tmp/gophed_${2}_${sel}_$3.XXXXXXXXXXXXXXXXXXX")" + go "$1" "$2" "$3" > "$TMPFILE" + cat "$SHOW" - | ed -p "$PROMPT" "$TMPFILE" cleanup exit 0 fi -if [ ${script_name} = "g" -o ${script_name} = "d" ]; then - LINE=$(awk "{if (NR == $2) print \$0;}" $1) - RESOURCE=$(echo "$LINE" | cut -d ' ' -f 2) - HOST=$(echo "$LINE" | cut -d ' ' -f 3) - PORT=$(echo "$LINE" | awk -F ' ' '{ match($4,/[[:digit:]]+/); print substr($4,RSTART,RLENGTH) }') +if [ "$script_name" = g ] || [ "$script_name" = d ]; then + LINE="$(awk "{if (NR == '$2') print '$0';}" "$1")" + RESOURCE="$(printf '%s' "$LINE" | cut -d ' ' -f 2)" + HOST="$(printf '%s' "$LINE" | cut -d ' ' -f 3)" + PORT="$(printf '%s' "$LINE" | awk -F ' ' '{ match($4,/[[:digit:]]+/); print substr($4,RSTART,RLENGTH) }')" - if [ "${script_name}" = "g" ]; then - SEL=$(echo $RESOURCE | sed -r 's:/:+:g') - TMPFILE=$(mktemp /tmp/gophed_${HOST}_${SEL}_${PORT}.XXXXXXXXXXXXXXXXXXX) - go "$RESOURCE" "$HOST" "$PORT" > $TMPFILE - cat $SHOW - | ed -p ${PROMPT} $TMPFILE + if [ "$script_name" = g ]; then + SEL="$(printf '%s' "$RESOURCE" | sed -r 's:/:+:g')" + TMPFILE="$(mktemp "/tmp/gophed_${HOST}_${SEL}_$PORT.XXXXXXXXXXXXXXXXXXX")" + go "$RESOURCE" "$HOST" "$PORT" > "$TMPFILE" + cat "$SHOW" - | ed -p "$PROMPT" "$TMPFILE" fi - if [ "${script_name}" = "d" ]; then - DIR_RESOURCE=$(dirname $RESOURCE) - echo "Download $RESOURCE in ${HOST}${RESOURCE}" - mkdir -p "$HOST/$DIR_RESOURCE" - go "$RESOURCE" "$HOST" "$PORT" > "${HOST}${RESOURCE}" + if [ "$script_name" = d ]; then + DIR_RESOURCE="$(dirname "$RESOURCE")" + printf 'Download %s in %s\n' "$RESOURCE" "$HOST$RESOURCE" + mkdir -p "$HOST/$DIR_RESOURCE" + go "$RESOURCE" "$HOST" "$PORT" > "$HOST$RESOURCE" fi cleanup exit 0 fi -echo "${scriptname}: command not found" +printf '%s: command not found\n' "$scriptname" cleanup |