From ff83e5b0a26595cab52c0f158a85d79dfbc678e7 Mon Sep 17 00:00:00 2001
From: Case Duckworth
Date: Fri, 22 May 2020 20:16:32 -0500
Subject: Wait for responses

---
 bollux | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/bollux b/bollux
index 77f7c11..5ed99ba 100755
--- a/bollux
+++ b/bollux
@@ -4,8 +4,6 @@
 # License: MIT
 # Version: -0.7
 
-set -euo pipefail
-
 ### constants ###
 PRGN="${0##*/}"                # program name
 DLDR="${BOLLUX_DOWNDIR:-.}"    # where to download
@@ -25,7 +23,7 @@ RDRS=0                         # redirects
 
 ### utility functions ###
 # a better echo
-put() { printf '%s\n' "$*"; }
+put() { printf '%s\n' "$*" >&3; }
 
 # conditionally log events to stderr
 # lower = more important
@@ -116,20 +114,18 @@ request() { # request [-s SERVER] URL
 	log 5 "serv: $serv"
 	log 5 "addr: $addr"
 
-	t="$(mktemp)"
-
 	sslcmd=(openssl s_client -crlf -ign_eof -quiet -connect "$serv")
 	log "${sslcmd[@]}"
-	"${sslcmd[@]}" <<<"$addr" 2>"$t"
-
-	((LOGL > 4)) && cat "$t"
-	rm "$t"
+	"${sslcmd[@]}" <<<"$addr" 2>/dev/null
 }
 
 # handle the response
 # cf. gemini://gemini.circumlunar.space/docs/spec-spec.txt
-handle() { # handle < RESPONSE ## needs $URL in state
-	head="$(sed 1q)"
+handle() { # handle URL < RESPONSE
+	URL="$1"
+	while read -r head; do
+		break # wait to read the first line
+	done
 	code="$(awk '{print $1}' <<<"$head")"
 	meta="$(awk '{for(i=2;i<=NF;i++)printf "%s ",$i;printf "\n"}' <<<"$head")"
 
@@ -241,20 +237,33 @@ display() { # display MIMETYPE < DOCUMENT
 
 ### main entry point ###
 bollux() {
+	bollux_setup
+
 	if (($# == 1)); then
 		URL="$1"
 	else
-		read -rp "GO> " URL
+		read -r -u 3 -p "GO> " URL
 	fi
 
 	log 5 "URL : $URL"
-	log 5 "addr: $(_address "$URL")"
-	log 5 "serv: $(_server "$URL")"
 
-	request "$URL" | handle
+	request "$URL" >.resource &
+	handle "$URL" <.resource
+
+	bollux_cleanup
+}
+
+bollux_setup() {
+	mkfifo .resource
+	trap bollux_cleanup INT QUIT TERM EXIT
+}
+bollux_cleanup() {
+	echo
+	rm -f .resource
 }
 
 if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
+	set -euo pipefail # strict mode
 	# requirements here -- so they're only checked once
 	require awk
 	require dd
-- 
cgit 1.4.1-21-gabe81