#!/bin/sh # gc -- gopher client die() { echo "$*" 1>&2 exit 1 } [ -z "$1" ] && die "usage: gc URL" pretty() { awk -F" " " /^[^01i\.]/ { sub(\"^.\", \"\", \$1); printf \" ???| %s\\n\", \$1 } /^i/ { sub(\"^i\", \"\", \$1); printf \" | %s\\n\", \$1 } /^0/ { sub(\"^0\", \"\", \$1); links[n++] = sprintf(\"%s/0%s\", \$3, \$2); printf \"%2d TXT| %s\\n\", n, \$1 } /^1/ { sub(\"^1\", \"\", \$1); links[n++] = sprintf(\"%s/1%s\", \$3, \$2); printf \"%2d DIR| %s\\n\", n, \$1 } END { printf \"\" > \"$LINKS\" for (i=0; i < length(links); i++) { printf \"[%d]: %s\\n\", i + 1, links[i] >>\"$LINKS\" } } " } parseurl() { bare=${URL##gopher://} host=${bare%%/*} target=${bare##$host} } catlike() { parseurl case "$target" in /0/*) echo "${target##/0}" | nc "$host" 70 ;; /1/*) echo "${target##/1}" | nc "$host" 70 | pretty ;; *) echo "$target" | nc "$host" 70 | pretty ;; # TODO: handle other types? esac } goto() { printf "goto> " read -r URL } graburl() { N="$1" link=$(grep "^\[$N\]:" "$LINKS") URL=${link##\[*\]: } } ui() { while [ -n "$URL" ]; do parseurl case "$target" in /0/*) echo "${target##/0}" | nc "$host" 70 | ${PAGER:-less} ;; /1/*) echo "${target##/1}" | nc "$host" 70 | pretty ;; *) echo "$target" | nc "$host" 70 | pretty ;; # TODO: handle other types? esac URL="" while true; do printf "> " read -r opt case $opt in [0-9]*) graburl "$opt" && if [ -n "$URL" ]; then break; else echo "no such link"; fi ;; l*) cat "$LINKS" ;; g*) goto ; break ;; h*) cat </dev/null; then catlike else ui fi rm -f "$LINKS"