+ elif echo "$ln" | grep "^$PREFIX" >/dev/null 2>&1; then
+ i=`expr $i + 1`
+ [ $i -le 9 ] && i=0$i
+ field="`echo "$ln" | cut -c1`"
+ case "$field" in
+ 0) typ='TXT';;
+ 1) typ='DIR';; # menu, actually
+ 7) typ='(?)';; # query
+ 8) typ='TEL';; # TELnet (not TELephone)
+ h) typ='WEB';; # HTML
+ I) typ='IMG';;
+ g) typ='GIF';;
+ +) typ='SVR';; # redundant server
+ *) typ='!!!';;
+ esac
+ if [ "$2" != dialog ]; then
+ echo "$ln" | sed "s:^.\\([^\t]*\\).*:$typ $i $SL\\1$EL:g"
+ else
+ echo "$ln" | sed "s:"'"'":'':g;s:^.\\([^\t]*\\).*:"'"'"$typ $i"'"'" "'"\1"'":g"
+ fi
+ fi
+ done
+}
+
+# $0 [FILE] [INDEX] [FIELD]
+# Get a field from the given-by-index link in FILE
+#
+# Fields:
+# 1 = type/name
+# 2 = selector
+# 3 = server
+# 4 = port
+getsel() {
+ cat "$1" | grep "^$PREFIX" | tail -n+"$2" | head -n 1 | cut -f"$3"
+}
+
+# Save page content to 'tmp' file
+tmp="`mktemp -t gofetch.current_page.XXXXXX`"
+finish() {
+ rm -rf "$tmp" "$tmp.jpg" "$tmp.menu" "$tmp.choice"
+}
+trap finish EXIT
+
+if [ $MODE = 1 ]; then
+ echo "$SELECTOR" | nc "$SERVER" "$PORT" | sed 's:\r::g' > "$tmp"
+else
+ echo "$SELECTOR" | nc "$SERVER" "$PORT" > "$tmp"
+fi
+
+# Process page content
+case "$MODE" in
+download)
+ # Special, fake mode, only from top-level
+ cat "$tmp"
+;;
+0)
+ cat "$tmp" | less
+;;
+1|+)
+ CHOICE=start
+ while [ "$CHOICE" != "" ]; do
+ if [ "$USE_DIALOG" = 1 ]; then
+ > "$tmp.menu"
+ cat_menu "$tmp" dialog | while read ln; do
+ echo -n " $ln" >> "$tmp.menu"
+ done
+ [ "$LINES" = "" ] && LINES=`tput lines`
+ [ "$COLUMNS" = "" ] && COLUMNS=`tput cols`
+ title="$SERVER: $SELECTOR"
+ dialog --extra-button --extra-label Back \
+ --cancel-label Exit \
+ --no-shadow \
+ --menu "$title" \
+ "$LINES" "$COLUMNS" "$LINES" \
+ --file "$tmp.menu" 2>"$tmp.choice"
+ val=$?
+ clear
+
+ if [ $val = 3 ]; then
+ CHOICE=""
+ elif [ $val = 1 ]; then
+ CHOICE="q"
+ else
+ CHOICE="`cat "$tmp.choice" | cut -c5-`"
+ fi
+
+ rm "$tmp.menu"
+ rm "$tmp.choice"
+ else
+ cat_menu "$tmp" | less
+ read -p "[$SELECTOR]: " CHOICE