~ft/gemnine

ref: 7eb61c82c8f04b7f4a93ecbed287b48b51583620 gemnine/main.c -rw-r--r-- 3.6 KiB
7eb61c82 — Sigrid Solveig Haflínudóttir differentiate between network errors and error responses 1 year, 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <plumb.h>
#include <ctype.h>
#include "gemnine.h"

void
page(Response *r)
{
	if(rfork(RFPROC|RFFDG|RFNOTEG|RFNOWAIT) == 0){
		char tmp[32] = "/tmp/gem9XXXXXXXXXXX", *cmd;
		mktemp(tmp);
		cmd = smprint("cat >%s >[2]/dev/null; page -w %s; rm %s", tmp, tmp, tmp);
		dup(r->fd, 0); close(r->fd);
		execl("/bin/rc", "rc", "-c", cmd, 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 *url, *x;
	int len, wait, pl, fd;
	Plumbmsg *m;
	Biobuf out, body;

	wait = 0;
	ARGBEGIN{
	case 'w':
		wait = 1;
		break;
	}ARGEND;

	if(!wait && argc < 1){
		fprint(2, "usage: gemnine [-w] [URL]\n");
		exits("usage");
	}

	quotefmtinstall();
	fmtinstall('U', Ufmt);
	fmtinstall('N', Nfmt);
	fmtinstall(']', Mfmt);
	fmtinstall('E', Efmt);
	fmtinstall('[', encodefmt);
	fmtinstall('H', encodefmt);

	Binit(&out, 1, OWRITE);
	pl = -1;

nexturl:
	url = nil;
	if(wait){
		if(pl >= 0 || (pl = plumbopen("gemini", OREAD)) >= 0){
			if((m = plumbrecv(pl)) != nil){
				url = urlparse(nil, estrdup(m->data));
				plumbfree(m);
			}else{
				exits(nil);
			}
		}else{
			sysfatal("plumbopen: %r");
		}
	}else{
		url = urlparse(nil, estrdup(argv[0]));
	}

nextreq:
	if((r = request(url)) != nil){
		if(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)
				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);
				Binit(&body, 0, OREAD);
				t = nil;
				if((s = Brdstr(&body, 0, 1)) != nil)
					t = smprint("%s?%E", r->url->full, (Str2){s, "/:@ \n"});
				Bterm(&body);
				free(s);
				if(t != nil){
					freeresponse(r);
					freeurl(url);
					url = urlparse(nil, t);
					free(t);
				}else{
					fprint(2, "%r\n");
				}
				close(fd);
				goto nextreq;
			}else{
				fprint(2, "%r\n");
			}
		}else{
			if(wait)
				close(open("/dev/text", OWRITE|OTRUNC));
			if(r->code < 20 || r->code >= 30){
				if(r->code > 0)
					fprint(2, "%U: %d %s\n", url, r->code, (r->meta && r->meta[0]) ? r->meta : r->status);
				else
					fprint(2, "%U: %r\n", url);
				if(!wait)
					exits("failed");
			}else{
				Binit(&body, r->fd, OREAD);
				while((s = Brdstr(&body, '\n', 1)) != nil){
					if((len = Blinelen(&body)) > 0)
						s[len] = 0;
					for(len--; len >= 0 && (s[len] == '\r' || s[len] == '\n'); len--)
						s[len] = 0;
					if(s[0] == '=' && s[1] == '>'){
						u = s + 2;
						while(isspace(*u))
							u++;
						if((t = strpbrk(u, " \t")) != nil)
							*t++ = 0;
						else
							t = "";
						x = urlparse(r->url, u);
						Bprint(&out, "→ %U %s\n", x, t);
						freeurl(x);
					}else{
						Bprint(&out, "%s\n", s);
					}
					free(s);
				}
				Bterm(&body);
			}
		}
		freeresponse(r);
		freeurl(url);
	}else{
		fprint(2, "%U: %r\n", url);
		if(!wait)
			exits("failed");
	}

	Bflush(&out);
	if(wait)
		goto nexturl;

	exits(nil);
}