Change some functions to only Bash
authorCase Duckworth <acdw@acdw.net>
Sat, 30 May 2020 19:35:17 +0000 (14:35 -0500)
committerCase Duckworth <acdw@acdw.net>
Sat, 30 May 2020 19:35:17 +0000 (14:35 -0500)
bollux

diff --git a/bollux b/bollux
index 0686c21..87c35fb 100755 (executable)
--- a/bollux
+++ b/bollux
@@ -9,6 +9,8 @@ PRGN="${0##*/}"
 VRSN=0.1
 # State
 REDIRECTS=0
+# Bash options
+# shopt -s extglob
 
 bollux_usage() {
        cat <<END
@@ -38,7 +40,12 @@ die() {
        exit "$ec"
 }
 
-trim() { sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'; }
+# pure bash bible trim_string
+trim() {
+       : "${1#"${1%%[![:space:]]*}"}"
+       : "${_%"${_##*[![:space:]]}"}"
+       printf '%s\n' "$_"
+}
 
 log() {
        [[ "$BOLLUX_LOGLEVEL" == QUIET ]] && return
@@ -125,19 +132,21 @@ blastoff() { # load a url
                URL="$(run transform_resource "$BOLLUX_URL" "$1")"
        fi
        [[ "$URL" != *://* ]] && URL="$BOLLUX_PROTO://$URL"
-       URL="$(trim <<<"$URL")"
+       URL="$(trim "$URL")"
 
        server="${URL#*://}"
        server="${server%%/*}"
 
+       log d "URL='$URL' server='$server'"
+
        run request_url "$server" "$BOLLUX_PORT" "$URL" |
                run handle_response "$URL"
 }
 
 transform_resource() { # transform_resource BASE_URL REFERENCE_URL
        declare -A R B T # reference, base url, target
-       eval "$(parse_url B "$1")"
-       eval "$(parse_url R "$2")"
+       eval "$(run parse_url B "$1")"
+       eval "$(run parse_url R "$2")"
        # A non-strict parser may ignore a scheme in the reference
        # if it is identical to the base URI's scheme.
        if ! "${STRICT:-true}" && [[ "${R[scheme]}" == "${B[scheme]}" ]]; then
@@ -149,7 +158,7 @@ transform_resource() { # transform_resource BASE_URL REFERENCE_URL
                T[scheme]="${R[scheme]}"
                isdefined "R[authority]" && T[authority]="${R[authority]}"
                isdefined R[path] &&
-                       T[path]="$(remove_dot_segments "${R[path]}")"
+                       T[path]="$(run remove_dot_segments "${R[path]}")"
                isdefined "R[query]" && T[query]="${R[query]}"
        else
                if isdefined "R[authority]"; then
@@ -239,8 +248,10 @@ remove_dot_segments() { # 5.2.4
 parse_url() { # eval "$(split_url NAME STRING)" => NAME[...]
        local name="$1"
        local string="$2"
+       # shopt -u extglob # TODO port re ^ to extglob syntax
        local re='^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?'
        [[ $string =~ $re ]] || return $?
+       # shopt -s extglob
 
        local scheme="${BASH_REMATCH[2]}"
        local authority="${BASH_REMATCH[4]}"
@@ -250,10 +261,10 @@ parse_url() { # eval "$(split_url NAME STRING)" => NAME[...]
 
        for c in scheme authority query fragment; do
                [[ "${!c}" ]] &&
-                       printf '%s[%s]=%q\n' "$name" "$c" "${!c}"
+                       run printf '%s[%s]=%q\n' "$name" "$c" "${!c}"
        done
        # unclear if the path is always set even if empty but it looks that way
-       printf '%s[path]=%q\n' "$name" "$path"
+       run printf '%s[path]=%q\n' "$name" "$path"
 }
 
 # is a NAME defined ('set' in bash)?
@@ -289,6 +300,7 @@ handle_response() {
                REDIRECTS=0
                BOLLUX_URL="$URL"
                run prompt "$meta" QUERY
+               # shellcheck disable=2153
                run blastoff "?$QUERY"
                ;;
        2*)
@@ -323,10 +335,13 @@ handle_response() {
 display() {
        case "$1" in
        *\;*)
-               mime="$(cut -d\; -f1 <<<"$1" | trim)"
-               charset="$(cut -d\; -f2 <<<"$1" | trim)"
+               mime="${1%;*}"
+               charset="${1#*;}"
+               trim "$mime"
+               trim "$charset"
+               log d "$mime $charset"
                ;;
-       *) mime="$(trim <<<"$1")" ;;
+       *) mime="$(trim "$1")" ;;
        esac
 
        [[ -z "$mime" ]] && mime="text/gemini"
@@ -384,7 +399,9 @@ mklesskey() {
 }
 
 normalize_crlf() {
-       gawk 'BEGIN{RS="\n\n"}{gsub(/\r\n?/,"\n");print;print ""}'
+       while read -r line; do
+               printf '%s\n' "${line//$'\r'?($'\n')/}"
+       done
 }
 
 typeset_gemini() {