shithub: gemnine

Download patch

ref: 786c84213d4744dd7ae1cc49eab8c69a39cf02ac
parent: 3987505e1b20d271b2838211dc2631d380dcf68c
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sun May 17 09:43:53 EDT 2020

handle plumbing

--- a/main.c
+++ b/main.c
@@ -3,6 +3,7 @@
 #include <libsec.h>
 #include <bio.h>
 #include <ctype.h>
+#include <plumb.h>
 
 typedef struct Url Url;
 typedef struct Response Response;
@@ -165,8 +166,9 @@
 main(int argc, char **argv)
 {
 	Response *r;
-	char *s, *t, *u;
-	int len, wait;
+	char *s, *t, *u, *url;
+	int len, wait, pl;
+	Plumbmsg *m;
 	Biobuf out;
 
 	wait = 0;
@@ -176,15 +178,36 @@
 		break;
 	}ARGEND;
 
-	if(argc < 1){
-		fprint(2, "usage: gemnine [-w] URL\n");
+	if(!wait && argc < 1){
+		fprint(2, "usage: gemnine [-w] [URL]\n");
 		exits("usage");
 	}
 
 	quotefmtinstall();
 	Binit(&out, 1, OWRITE);
+	pl = -1;
 
-	if((r = request(argv[0])) != nil){
+nexturl:
+	url = nil;
+	if(wait){
+		if(pl >= 0 || (pl = plumbopen("gemini", OREAD)) >= 0){
+			if((m = plumbrecv(pl)) != nil){
+				int text;
+				url = strdup(m->data);
+				plumbfree(m);
+				if((text = open("/dev/text", OWRITE|OTRUNC)) >= 0){
+					write(text, "", 0);
+					close(text);
+				}
+			}else{
+				exits(nil);
+			}
+		}
+	}else{
+		url = strdup(argv[0]);
+	}
+
+	if((r = request(url)) != nil){
 		if(r->mime != nil && strncmp(r->mime, "text/", 5) != 0){
 			/* FIXME handle in a better way */
 			if(r->mime != nil)
@@ -214,14 +237,14 @@
 		freeresponse(r);
 	}else{
 		fprint(2, "%s: %r\n", argv[1]);
-		if(wait)
-			read(0, &wait, 4);
-		exits("failed");
+		if(!wait)
+			exits("failed");
 	}
 
 	Bflush(&out);
+	free(url);
 	if(wait)
-		read(0, &wait, 4);
+		goto nexturl;
 
 	exits(nil);
 }