accept URLs

master
KatolaZ 6 years ago
parent acd8a08910
commit aa624aff7c
  1. 4
      README.md
  2. 37
      gophed

@ -35,13 +35,13 @@ and `d`, which will be used as `gophed` commands. Once `gophed` has
been initialised, you can start visiting some gopherholes: been initialised, you can start visiting some gopherholes:
``` ```
./gophed / kalos.mine.nu 70 ./gophed gopher://kalos.mine.nu
``` ```
or equivalently: or equivalently:
``` ```
./v / kalos.mine.nu 70 ./v gopher://kalos.mine.nu
``` ```
This command will download the corresponding resource, and print it on This command will download the corresponding resource, and print it on

@ -14,8 +14,8 @@ go () {
usage() { usage() {
printf '%s\n' \ printf '%s\n' \
'usage:' \ 'usage:' \
' visit a resource:' \ ' visit a Gopher URL:' \
" $0 <path> <server> <port>" \ " $0 <URL>" \
' init links:' \ ' init links:' \
" $0 -i" \ " $0 -i" \
' show this help:' \ ' show this help:' \
@ -25,10 +25,35 @@ usage() {
" !./g % LINE" \ " !./g % LINE" \
'to visit the resource at LINE, or:' \ 'to visit the resource at LINE, or:' \
" !./d % LINE" \ " !./d % LINE" \
'to download the resource at LINE\n' 'to download the resource at LINE\n'\
" !./v URL" \
'to visit a Gopher URL'
exit 0 exit 0
} }
goto_url(){
URL="$(echo $1 | sed 's,gopher://,,g')"
hostport=$(echo "$URL" | cut -d "/" -f 1)
host="$(echo $hostport | cut -d ":" -f 1)"
port="$(echo $hostport | cut -s -d ":" -f 2)"
[ -z "$port" ] && port='70'
type=$(echo "$URL" | cut -s -d "/" -f 2)
[ -z "$type" ] && {
type='1'
sel="/"
go $sel $host $port
return
}
[ -n "${type#?}" ] && echo "Invalid Gopher URL" >&2 && exit 1
## Check if type is a valid one
type="$(echo $type | sed -n '/^[0-9ITghis+]$/p')"
[ -z "${type}" ] && echo "Invalid Gopher URL" >&2 && exit 1
sel=/$(echo "$URL" | cut -s -d "/" -f 3-)
go $sel $host $port
}
trap cleanup INT QUIT HUP KILL ABRT TERM trap cleanup INT QUIT HUP KILL ABRT TERM
[ "$#" -lt 1 ] || [ "$1" = "-h" ] && usage [ "$#" -lt 1 ] || [ "$1" = "-h" ] && usage
@ -46,8 +71,8 @@ fi
if [ "$script_name" = "gophed" ] || [ "$script_name" = "v" ]; then if [ "$script_name" = "gophed" ] || [ "$script_name" = "v" ]; then
sel="$(printf '%s' "$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" goto_url "$1" > "$TMPFILE"
cat "$SHOW" - | ed -p "$PROMPT" "$TMPFILE" while read line; do echo $line; done | ed -p "$PROMPT" "$TMPFILE"
cleanup cleanup
exit 0 exit 0
fi fi
@ -62,7 +87,7 @@ if [ "$script_name" = "g" ] || [ "$script_name" = "d" ]; then
SEL="$(printf '%s' "$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" while read line; do echo $line; done | ed -p "$PROMPT" "$TMPFILE"
fi fi
if [ "$script_name" = "d" ]; then if [ "$script_name" = "d" ]; then
DIR_RESOURCE="$(dirname "$RESOURCE")" DIR_RESOURCE="$(dirname "$RESOURCE")"

Loading…
Cancel
Save