shithub: gemnine

Download patch

ref: 44a1cdb2993372a26ec325fccadfaa570eb00e6f
parent: afd1e85aa0dd7c71d7cd7f2cdaf18abd4e6513ae
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon May 18 08:43:00 EDT 2020

mime: handle image/*, application/pdf, audio/*

--- a/main.c
+++ b/main.c
@@ -19,7 +19,6 @@
 	char *mime;
 	char *prompt;
 	int status;
-	Biobuf body;
 	int fd;
 };
 
@@ -91,9 +90,9 @@
 {
 	Thumbprint *th;
 	Response *r;
-	char *s;
+	char *s, buf[256];
 	TLSconn conn;
-	int ok, len, oldfd;
+	int i, ok, len, oldfd;
 
 	r = calloc(1, sizeof(*r));
 	r->fd = -1;
@@ -123,13 +122,15 @@
 	}
 
 	fprint(r->fd, "%s\r\n", r->url->url);
-	Binit(&r->body, r->fd, OREAD);
-	if((s = Brdstr(&r->body, '\n', 1)) == nil){
-		werrstr("EOF");
-		goto err;
+	for(len = 0; len < sizeof(buf)-1; len++){
+		if((i = read(r->fd, buf+len, 1)) < 0)
+			goto err;
+		if(i == 0 || buf[len] == '\n')
+			break;
 	}
-	if((len = Blinelen(&r->body)) > 0)
-		s[--len] = 0;
+
+	s = buf;
+	s[len] = 0;
 	for(len--; len >= 0 && (s[len] == '\r' || s[len] == '\n'); len--)
 		s[len] = 0;
 	if(s[0] < '0' || s[0] > '9' || s[1] < '0' || s[1] > '9'){
@@ -211,6 +212,35 @@
 }
 
 void
+page(Response *r)
+{
+	if(rfork(RFPROC|RFFDG|RFNOTEG|RFNOWAIT) == 0){
+		dup(r->fd, 0); close(r->fd);
+		execl("/bin/page", "page", "-w", nil);
+	}
+}
+
+void
+play(Response *r)
+{
+	int wfd;
+	char *wsys, tmp[64];
+
+	if(rfork(RFPROC|RFFDG|RFNOTEG|RFNOWAIT) == 0){
+		snprint(tmp, sizeof(tmp), "new -pid %d -dx %d -dy %d", getpid(), 640, 480);
+		if ((wsys = getenv("wsys")) == nil)
+			exits("no wsys");
+		if ((wfd = open(wsys, ORDWR)) < 0 ||
+		    mount(wfd, -1, "/mnt/wsys", MREPL, tmp) < 0 ||
+		    bind("/mnt/wsys", "/dev", MBEFORE) < 0){
+			exits("wsys: %r");
+		}
+		dup(r->fd, 0); close(r->fd);
+		execl("/bin/play", "play", nil);
+	}
+}
+
+void
 main(int argc, char **argv)
 {
 	Response *r;
@@ -217,7 +247,7 @@
 	char *s, *t, *u, *url, *prevurl;
 	int len, wait, pl, fd;
 	Plumbmsg *m;
-	Biobuf out;
+	Biobuf out, body;
 
 	wait = 0;
 	ARGBEGIN{
@@ -244,7 +274,6 @@
 			if((m = plumbrecv(pl)) != nil){
 				url = strdup(m->data);
 				plumbfree(m);
-				close(open("/dev/text", OWRITE|OTRUNC));
 			}else{
 				exits(nil);
 			}
@@ -258,10 +287,15 @@
 nextreq:
 	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)
-				fprint(2, "MIME %s\n", r->mime);
+			if(strncmp(r->mime, "image/", 6) == 0 || strcmp(r->mime, "application/pdf") == 0)
+				page(r);
+			else if(strncmp(r->mime, "audio/", 6) == 0)
+				play(r);
+			else
+				fprint(2, "unsupported MIME %q\n", r->mime);
 		}else if(r->prompt != nil){
+			if(wait)
+				close(open("/dev/text", OWRITE|OTRUNC));
 			if((fd = open("/dev/consctl", OWRITE)) >= 0){
 				write(fd, "holdon", 6);
 				print("%s\n", r->prompt);
@@ -273,11 +307,14 @@
 				close(fd);
 				goto nextreq;
 			}else{
-				fprint(2, "INPUT %s\n", r->prompt);
+				fprint(2, "%r\n");
 			}
 		}else{
-			while((s = Brdstr(&r->body, '\n', 1)) != nil){
-				if((len = Blinelen(&r->body)) > 0)
+			if(wait)
+				close(open("/dev/text", OWRITE|OTRUNC));
+			Binit(&body, r->fd, OREAD);
+			while((s = Brdstr(&body, '\n', 1)) != nil){
+				if((len = Blinelen(&body)) > 0)
 					s[len] = 0;
 				if(s[0] == '=' && s[1] == '>'){
 					t = s + 2;
@@ -299,6 +336,10 @@
 				}
 				free(s);
 			}
+			if(prevurl != nil){
+				Bprint(&out, "\nPrevious: %s\n", prevurl);
+				free(prevurl);
+			}
 		}
 		freeresponse(r);
 	}else{
@@ -305,10 +346,6 @@
 		fprint(2, "%r\n");
 		if(!wait)
 			exits("failed");
-	}
-	if(prevurl != nil){
-		Bprint(&out, "\nPrevious: %s\n", prevurl);
-		free(prevurl);
 	}
 
 	Bflush(&out);