diff options
author | Case Duckworth | 2020-05-22 20:34:27 -0500 |
---|---|---|
committer | Case Duckworth | 2020-05-22 20:34:27 -0500 |
commit | 1b4c540448d2212bf33f610fb40209854b32221b (patch) | |
tree | 0c6252000d8b1ef0225073e526bbfd07d58db9d1 | |
parent | Wait for responses (diff) | |
download | bollux-1b4c540448d2212bf33f610fb40209854b32221b.tar.gz bollux-1b4c540448d2212bf33f610fb40209854b32221b.zip |
Change file descriptors and add functions
-rwxr-xr-x | bollux | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/bollux b/bollux index 5ed99ba..b027eab 100755 --- a/bollux +++ b/bollux | |||
@@ -65,6 +65,13 @@ die() { # die [EXIT-CODE] MESSAGE | |||
65 | exit "$ec" | 65 | exit "$ec" |
66 | } | 66 | } |
67 | 67 | ||
68 | # ask the user for input | ||
69 | ask() { # ask PROMPT [READ_OPT...] | ||
70 | prompt="$1" | ||
71 | shift | ||
72 | read -e -r -u 3 -p "$prompt> " "$@" | ||
73 | } | ||
74 | |||
68 | # fail if something isn't installed | 75 | # fail if something isn't installed |
69 | require() { hash "$1" 2>/dev/null || die 127 "Requirement '$1' not found."; } | 76 | require() { hash "$1" 2>/dev/null || die 127 "Requirement '$1' not found."; } |
70 | 77 | ||
@@ -135,7 +142,7 @@ handle() { # handle URL < RESPONSE | |||
135 | 1*) # INPUT | 142 | 1*) # INPUT |
136 | log 3 "Input" | 143 | log 3 "Input" |
137 | put "$meta" | 144 | put "$meta" |
138 | read -rep "? " | 145 | ask "?" |
139 | bollux "$URL?$REPLY" | 146 | bollux "$URL?$REPLY" |
140 | ;; | 147 | ;; |
141 | 2*) # SUCCESS | 148 | 2*) # SUCCESS |
@@ -219,44 +226,48 @@ display() { # display MIMETYPE < DOCUMENT | |||
219 | # ($LESSOPEN, $LESSCLOSE) | 226 | # ($LESSOPEN, $LESSCLOSE) |
220 | ;; | 227 | ;; |
221 | *) | 228 | *) |
222 | tn="$(mktemp)" | 229 | download "$URL" |
223 | dd status=progress >"$tn" | ||
224 | fn="$DLDR/${URL##*/}" | ||
225 | if [[ -f "$fn" ]]; then | ||
226 | log - "Saved '$tn'." | ||
227 | else | ||
228 | if mv "$tn" "$fn"; then | ||
229 | log - "Saved '$fn'." | ||
230 | else | ||
231 | log 0 "Error saving '$fn'." | ||
232 | fi | ||
233 | fi | ||
234 | ;; | 230 | ;; |
235 | esac | 231 | esac |
236 | } | 232 | } |
237 | 233 | ||
234 | download() { # download URL < FILE | ||
235 | tn="$(mktemp)" | ||
236 | dd status=progress >"$tn" | ||
237 | fn="$DLDR/${URL##*/}" | ||
238 | if [[ -f "$fn" ]]; then | ||
239 | log - "Saved '$tn'." | ||
240 | else | ||
241 | if mv "$tn" "$fn"; then | ||
242 | log - "Saved '$fn'." | ||
243 | else | ||
244 | log 0 "Error saving '$fn'." | ||
245 | log - "Saved '$tn'." | ||
246 | fi | ||
247 | fi | ||
248 | } | ||
249 | |||
238 | ### main entry point ### | 250 | ### main entry point ### |
239 | bollux() { | 251 | bollux() { |
240 | bollux_setup | 252 | # use &3 for user input |
253 | exec 3<>/dev/tty | ||
241 | 254 | ||
242 | if (($# == 1)); then | 255 | if (($# == 1)); then |
243 | URL="$1" | 256 | URL="$1" |
244 | else | 257 | else |
245 | read -r -u 3 -p "GO> " URL | 258 | ask GO URL |
246 | fi | 259 | fi |
247 | 260 | ||
248 | log 5 "URL : $URL" | 261 | log 5 "URL : $URL" |
249 | 262 | ||
250 | request "$URL" >.resource & | 263 | request "$URL" | handle "$URL" |
251 | handle "$URL" <.resource | ||
252 | |||
253 | bollux_cleanup | ||
254 | } | 264 | } |
255 | 265 | ||
256 | bollux_setup() { | 266 | bollux_setup() { |
257 | mkfifo .resource | 267 | mkfifo .resource |
258 | trap bollux_cleanup INT QUIT TERM EXIT | 268 | trap bollux_cleanup INT QUIT TERM EXIT |
259 | } | 269 | } |
270 | |||
260 | bollux_cleanup() { | 271 | bollux_cleanup() { |
261 | echo | 272 | echo |
262 | rm -f .resource | 273 | rm -f .resource |
@@ -272,4 +283,5 @@ if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then | |||
272 | require sed | 283 | require sed |
273 | 284 | ||
274 | bollux "$@" | 285 | bollux "$@" |
286 | echo | ||
275 | fi | 287 | fi |