shithub: gemnine

Download patch

ref: 456b709807fdeb7c725731fd1469f7181b342893
parent: 11e93db76eefebd94f03bace14526c5218a3cfc6
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed May 19 06:48:47 EDT 2021

dump raw to stdout if not /dev/cons

--- a/main.c
+++ b/main.c
@@ -5,7 +5,7 @@
 #include <ctype.h>
 #include "gemnine.h"
 
-void
+static void
 page(Response *r)
 {
 	if(rfork(RFPROC|RFFDG|RFNOTEG|RFNOWAIT) == 0){
@@ -21,7 +21,7 @@
 	}
 }
 
-void
+static void
 play(Response *r)
 {
 	if(rfork(RFPROC|RFFDG|RFNOTEG|RFNOWAIT) == 0){
@@ -42,6 +42,13 @@
 	}
 }
 
+static void
+usage(void)
+{
+	fprint(2, "usage: %s [-w] [URL]\n", argv0);
+	exits("usage");
+}
+
 void
 main(int argc, char **argv)
 {
@@ -48,9 +55,10 @@
 	Response *r;
 	char *s, *t, *u, *tmp;
 	Url *url, *x;
-	int len, wait, pl, fd;
+	int len, raw, wait, pl, fd;
 	Plumbmsg *m;
 	Biobuf out, body;
+	char buf[8192];
 
 	wait = 0;
 	ARGBEGIN{
@@ -57,13 +65,15 @@
 	case 'w':
 		wait = 1;
 		break;
+	default:
+		usage();
 	}ARGEND;
 
-	if(!wait && argc < 1){
-		fprint(2, "usage: gemnine [-w] [URL]\n");
-		exits("usage");
-	}
+	if(!wait && argc < 1)
+		usage();
 
+	raw = fd2path(1, buf, sizeof(buf)) == 0 && strcmp(buf, "/dev/cons") != 0;
+
 	quotefmtinstall();
 	fmtinstall('U', Ufmt);
 	fmtinstall('N', Nfmt);
@@ -94,7 +104,7 @@
 
 nextreq:
 	if((r = request(url)) != nil){
-		if(r->mime != nil && strncmp(r->mime, "text/", 5) != 0){
+		if(!raw && r->mime != nil && strncmp(r->mime, "text/", 5) != 0){
 			if(strncmp(r->mime, "image/", 6) == 0 || strcmp(r->mime, "application/pdf") == 0)
 				page(r);
 			else if(strncmp(r->mime, "audio/", 6) == 0)
@@ -136,6 +146,15 @@
 					fprint(2, "%U: %r\n", url);
 				if(!wait)
 					exits("failed");
+			}else if(raw){
+				for(;;){
+					if((len = read(r->fd, buf, sizeof(buf))) == 0)
+						break;
+					if(len < 0)
+						goto err;
+					if(write(1, buf, len) <= 0)
+						break;
+				}
 			}else{
 				Binit(&body, r->fd, OREAD);
 				while((s = Brdstr(&body, '\n', 1)) != nil){
@@ -169,6 +188,7 @@
 		freeresponse(r);
 		freeurl(url);
 	}else{
+err:
 		fprint(2, "%U: %r\n", url);
 		if(!wait)
 			exits("failed");