Arrange quoting, use printf, small fixes

master
Quentin Rameau 6 years ago committed by KatolaZ
parent 5e28c04711
commit 6179fc6738

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

Loading…
Cancel
Save