~ft/gemnine

44a1cdb2993372a26ec325fccadfaa570eb00e6f — Sigrid Haflínudóttir 2 years ago afd1e85
mime: handle image/*, application/pdf, audio/*
1 files changed, 58 insertions(+), 21 deletions(-)

M main.c
M main.c => main.c +58 -21
@@ 19,7 19,6 @@ struct Response {
	char *mime;
	char *prompt;
	int status;
	Biobuf body;
	int fd;
};



@@ 91,9 90,9 @@ request(char *url)
{
	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 @@ request(char *url)
	}

	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,13 212,42 @@ readall(int fd)
}

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;
	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 @@ nexturl:
			if((m = plumbrecv(pl)) != nil){
				url = strdup(m->data);
				plumbfree(m);
				close(open("/dev/text", OWRITE|OTRUNC));
			}else{
				exits(nil);
			}


@@ 258,10 287,15 @@ nexturl:
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 @@ nextreq:
				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 @@ nextreq:
				}
				free(s);
			}
			if(prevurl != nil){
				Bprint(&out, "\nPrevious: %s\n", prevurl);
				free(prevurl);
			}
		}
		freeresponse(r);
	}else{


@@ 306,10 347,6 @@ nextreq:
		if(!wait)
			exits("failed");
	}
	if(prevurl != nil){
		Bprint(&out, "\nPrevious: %s\n", prevurl);
		free(prevurl);
	}

	Bflush(&out);
	prevurl = url;