Blame


1 5e11c00c 2021-03-02 op #include "telescope.h"
2 5e11c00c 2021-03-02 op
3 5e11c00c 2021-03-02 op #include <sys/socket.h>
4 5e11c00c 2021-03-02 op
5 5e11c00c 2021-03-02 op #include <err.h>
6 5e11c00c 2021-03-02 op #include <errno.h>
7 5e11c00c 2021-03-02 op #include <signal.h>
8 5e11c00c 2021-03-02 op #include <stdio.h>
9 5e11c00c 2021-03-02 op #include <stdlib.h>
10 5e11c00c 2021-03-02 op #include <string.h>
11 5e11c00c 2021-03-02 op #include <unistd.h>
12 5e11c00c 2021-03-02 op
13 35e1f40a 2021-03-14 op struct event netev, fsev;
14 5e11c00c 2021-03-02 op struct tabshead tabshead;
15 5e11c00c 2021-03-02 op
16 c07ac996 2021-03-12 op /* the first is also the fallback one */
17 c07ac996 2021-03-12 op static struct proto protos[] = {
18 4d3785b1 2021-03-09 op { "gemini:", load_gemini_url },
19 4d3785b1 2021-03-09 op { "about:", load_about_url },
20 4d3785b1 2021-03-09 op { NULL, NULL },
21 4d3785b1 2021-03-09 op };
22 4d3785b1 2021-03-09 op
23 35e1f40a 2021-03-14 op static struct imsgbuf *netibuf, *fsibuf;
24 5e11c00c 2021-03-02 op
25 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
26 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
27 2051e653 2021-03-13 op static void handle_imsg_err(struct imsg*, size_t);
28 2051e653 2021-03-13 op static void handle_imsg_check_cert(struct imsg*, size_t);
29 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
30 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
31 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
32 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
33 740f578b 2021-03-15 op static void handle_imsg_bookmark_ok(struct imsg*, size_t);
34 2051e653 2021-03-13 op static void dispatch_imsg(int, short, void*);
35 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
36 2051e653 2021-03-13 op static void do_load_url(struct tab*, const char*);
37 5e11c00c 2021-03-02 op
38 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
39 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
40 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
41 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
42 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
43 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
44 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
45 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
46 5e11c00c 2021-03-02 op };
47 5e11c00c 2021-03-02 op
48 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
49 5e11c00c 2021-03-02 op die(void)
50 5e11c00c 2021-03-02 op {
51 5e11c00c 2021-03-02 op abort(); /* TODO */
52 5e11c00c 2021-03-02 op }
53 5e11c00c 2021-03-02 op
54 5e11c00c 2021-03-02 op static struct tab *
55 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
56 5e11c00c 2021-03-02 op {
57 5e11c00c 2021-03-02 op struct tab *t;
58 5e11c00c 2021-03-02 op
59 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
60 5e11c00c 2021-03-02 op if (t->id == id)
61 5e11c00c 2021-03-02 op return t;
62 5e11c00c 2021-03-02 op }
63 5e11c00c 2021-03-02 op
64 5e11c00c 2021-03-02 op die();
65 5e11c00c 2021-03-02 op }
66 5e11c00c 2021-03-02 op
67 5e11c00c 2021-03-02 op static void
68 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
69 5e11c00c 2021-03-02 op {
70 3a9b9365 2021-03-09 op struct tab *tab;
71 3a9b9365 2021-03-09 op char *page;
72 3a9b9365 2021-03-09 op
73 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
74 3a9b9365 2021-03-09 op
75 3a9b9365 2021-03-09 op page = imsg->data;
76 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
77 3a9b9365 2021-03-09 op
78 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
79 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
80 3a9b9365 2021-03-09 op die();
81 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
82 3a9b9365 2021-03-09 op free(page);
83 5e11c00c 2021-03-02 op }
84 5e11c00c 2021-03-02 op
85 5e11c00c 2021-03-02 op static void
86 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
87 5e11c00c 2021-03-02 op {
88 5e11c00c 2021-03-02 op int tofu_res = 1;
89 5e11c00c 2021-03-02 op
90 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1, &tofu_res, sizeof(tofu_res));
91 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
92 5e11c00c 2021-03-02 op }
93 5e11c00c 2021-03-02 op
94 5cd2ebb1 2021-03-11 op static inline int
95 5cd2ebb1 2021-03-11 op normalize_code(int n)
96 5cd2ebb1 2021-03-11 op {
97 5cd2ebb1 2021-03-11 op if (n < 20) {
98 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
99 5cd2ebb1 2021-03-11 op return n;
100 5cd2ebb1 2021-03-11 op return 10;
101 5cd2ebb1 2021-03-11 op } else if (n < 30) {
102 5cd2ebb1 2021-03-11 op return 20;
103 5cd2ebb1 2021-03-11 op } else if (n < 40) {
104 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
105 5cd2ebb1 2021-03-11 op return n;
106 5cd2ebb1 2021-03-11 op return 30;
107 5cd2ebb1 2021-03-11 op } else if (n < 50) {
108 5cd2ebb1 2021-03-11 op if (n <= 44)
109 5cd2ebb1 2021-03-11 op return n;
110 5cd2ebb1 2021-03-11 op return 40;
111 5cd2ebb1 2021-03-11 op } else if (n < 60) {
112 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
113 5cd2ebb1 2021-03-11 op return n;
114 5cd2ebb1 2021-03-11 op return 50;
115 5cd2ebb1 2021-03-11 op } else if (n < 70) {
116 5cd2ebb1 2021-03-11 op if (n <= 62)
117 5cd2ebb1 2021-03-11 op return n;
118 5cd2ebb1 2021-03-11 op return 60;
119 5cd2ebb1 2021-03-11 op } else
120 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
121 5cd2ebb1 2021-03-11 op }
122 5cd2ebb1 2021-03-11 op
123 5e11c00c 2021-03-02 op static void
124 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
125 5e11c00c 2021-03-02 op {
126 0972d8b2 2021-03-02 op struct tab *tab;
127 5e11c00c 2021-03-02 op
128 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
129 0972d8b2 2021-03-02 op
130 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
131 5e11c00c 2021-03-02 op die();
132 5e11c00c 2021-03-02 op
133 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
134 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
135 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
136 0972d8b2 2021-03-02 op tab->redirect_count = 0;
137 5e11c00c 2021-03-02 op }
138 5e11c00c 2021-03-02 op
139 5e11c00c 2021-03-02 op static void
140 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
141 5e11c00c 2021-03-02 op {
142 0972d8b2 2021-03-02 op struct tab *tab;
143 0972d8b2 2021-03-02 op
144 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
145 0972d8b2 2021-03-02 op
146 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
147 0972d8b2 2021-03-02 op die();
148 5cd2ebb1 2021-03-11 op
149 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
150 0972d8b2 2021-03-02 op
151 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
152 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
153 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
154 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
155 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
156 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
157 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
158 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
159 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
160 c07ac996 2021-03-12 op } else {
161 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
162 c07ac996 2021-03-12 op }
163 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
164 3a9b9365 2021-03-09 op tab->redirect_count++;
165 0972d8b2 2021-03-02 op
166 3a9b9365 2021-03-09 op /* TODO: make customizable? */
167 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
168 3a9b9365 2021-03-09 op load_page_from_str(tab,
169 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
170 5cd2ebb1 2021-03-11 op } else
171 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
172 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
173 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
174 3a9b9365 2021-03-09 op }
175 5e11c00c 2021-03-02 op }
176 5e11c00c 2021-03-02 op
177 5e11c00c 2021-03-02 op static void
178 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
179 5e11c00c 2021-03-02 op {
180 0972d8b2 2021-03-02 op struct tab *tab;
181 5e11c00c 2021-03-02 op
182 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
183 5e11c00c 2021-03-02 op
184 0972d8b2 2021-03-02 op if (!tab->page.parse(&tab->page, imsg->data, datalen))
185 5e11c00c 2021-03-02 op die();
186 5e11c00c 2021-03-02 op
187 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
188 5e11c00c 2021-03-02 op }
189 5e11c00c 2021-03-02 op
190 5e11c00c 2021-03-02 op static void
191 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
192 5e11c00c 2021-03-02 op {
193 a5c3e03d 2021-03-02 op struct tab *t;
194 a5c3e03d 2021-03-02 op
195 a5c3e03d 2021-03-02 op t = tab_by_id(imsg->hdr.peerid);
196 a5c3e03d 2021-03-02 op if (!t->page.free(&t->page))
197 a5c3e03d 2021-03-02 op die();
198 a5c3e03d 2021-03-02 op
199 a5c3e03d 2021-03-02 op ui_on_tab_refresh(t);
200 8af5e5ed 2021-03-08 op ui_on_tab_loaded(t);
201 5e11c00c 2021-03-02 op }
202 5e11c00c 2021-03-02 op
203 5e11c00c 2021-03-02 op static void
204 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
205 740f578b 2021-03-15 op {
206 740f578b 2021-03-15 op int res;
207 740f578b 2021-03-15 op
208 740f578b 2021-03-15 op if (datalen != sizeof(res))
209 740f578b 2021-03-15 op die();
210 740f578b 2021-03-15 op
211 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
212 740f578b 2021-03-15 op if (res == 0)
213 740f578b 2021-03-15 op ui_notify("Added to bookmarks!");
214 740f578b 2021-03-15 op else
215 740f578b 2021-03-15 op ui_notify("Failed to add to bookmarks: %s",
216 740f578b 2021-03-15 op strerror(res));
217 740f578b 2021-03-15 op }
218 740f578b 2021-03-15 op
219 740f578b 2021-03-15 op static void
220 5e11c00c 2021-03-02 op dispatch_imsg(int fd, short ev, void *d)
221 5e11c00c 2021-03-02 op {
222 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
223 35e1f40a 2021-03-14 op struct imsg imsg;
224 35e1f40a 2021-03-14 op size_t datalen;
225 35e1f40a 2021-03-14 op ssize_t n;
226 5e11c00c 2021-03-02 op
227 35e1f40a 2021-03-14 op if ((n = imsg_read(ibuf)) == -1) {
228 5e11c00c 2021-03-02 op if (errno == EAGAIN || errno == EWOULDBLOCK)
229 5e11c00c 2021-03-02 op return;
230 5e11c00c 2021-03-02 op die();
231 5e11c00c 2021-03-02 op }
232 5e11c00c 2021-03-02 op
233 35e1f40a 2021-03-14 op if (n == 0)
234 35e1f40a 2021-03-14 op _exit(1);
235 5e11c00c 2021-03-02 op
236 5e11c00c 2021-03-02 op for (;;) {
237 35e1f40a 2021-03-14 op if ((n = imsg_get(ibuf, &imsg)) == -1)
238 5e11c00c 2021-03-02 op die();
239 5e11c00c 2021-03-02 op if (n == 0)
240 5e11c00c 2021-03-02 op return;
241 5e11c00c 2021-03-02 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
242 5e11c00c 2021-03-02 op handlers[imsg.hdr.type](&imsg, datalen);
243 5e11c00c 2021-03-02 op imsg_free(&imsg);
244 5e11c00c 2021-03-02 op }
245 5e11c00c 2021-03-02 op }
246 5e11c00c 2021-03-02 op
247 0972d8b2 2021-03-02 op static void
248 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
249 0972d8b2 2021-03-02 op {
250 0972d8b2 2021-03-02 op gemtext_initparser(&tab->page);
251 3a9b9365 2021-03-09 op if (!tab->page.parse(&tab->page, page, strlen(page)))
252 0972d8b2 2021-03-02 op die();
253 0972d8b2 2021-03-02 op if (!tab->page.free(&tab->page))
254 0972d8b2 2021-03-02 op die();
255 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
256 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
257 0972d8b2 2021-03-02 op }
258 0972d8b2 2021-03-02 op
259 bcb0b073 2021-03-07 op void
260 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
261 0972d8b2 2021-03-02 op {
262 2051e653 2021-03-13 op char *m;
263 2051e653 2021-03-13 op size_t len;
264 8af5e5ed 2021-03-08 op
265 2051e653 2021-03-13 op
266 4d3785b1 2021-03-09 op memset(&tab->url, 0, sizeof(tab->url));
267 4d3785b1 2021-03-09 op
268 4d3785b1 2021-03-09 op m = strchr(url, ':');
269 4d3785b1 2021-03-09 op strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
270 4d3785b1 2021-03-09 op strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
271 097ff997 2021-03-09 op
272 2051e653 2021-03-13 op len = sizeof(tab->hist_cur->h)-1;
273 2051e653 2021-03-13 op strlcpy(tab->hist_cur->h, url, len);
274 4d3785b1 2021-03-09 op
275 35e1f40a 2021-03-14 op gemtext_initparser(&tab->page);
276 35e1f40a 2021-03-14 op
277 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
278 35e1f40a 2021-03-14 op tab->hist_cur->h, len+1);
279 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
280 4d3785b1 2021-03-09 op }
281 0972d8b2 2021-03-02 op
282 4d3785b1 2021-03-09 op void
283 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
284 4d3785b1 2021-03-09 op {
285 4d3785b1 2021-03-09 op const char *err;
286 4d3785b1 2021-03-09 op char *p;
287 2051e653 2021-03-13 op size_t len;
288 3a9b9365 2021-03-09 op
289 4d3785b1 2021-03-09 op if (has_prefix(url, "gemini:")) {
290 4d3785b1 2021-03-09 op if (!url_parse(url, &tab->url, &err))
291 3a9b9365 2021-03-09 op goto err;
292 4d3785b1 2021-03-09 op } else {
293 4d3785b1 2021-03-09 op if (!url_resolve_from(&tab->url, url, &err))
294 4d3785b1 2021-03-09 op goto err;
295 3a9b9365 2021-03-09 op }
296 3a9b9365 2021-03-09 op
297 2051e653 2021-03-13 op len = sizeof(tab->hist_cur->h)-1;
298 2051e653 2021-03-13 op url_unparse(&tab->url, tab->hist_cur->h, len);
299 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
300 2051e653 2021-03-13 op tab->hist_cur->h, len+1);
301 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
302 3a9b9365 2021-03-09 op return;
303 3a9b9365 2021-03-09 op
304 3a9b9365 2021-03-09 op err:
305 4d3785b1 2021-03-09 op if (asprintf(&p, "#error loading %s\n>%s\n",
306 4d3785b1 2021-03-09 op url, err) == -1)
307 3a9b9365 2021-03-09 op die();
308 2051e653 2021-03-13 op strlcpy(tab->hist_cur->h, url, len);
309 4d3785b1 2021-03-09 op load_page_from_str(tab, p);
310 4d3785b1 2021-03-09 op free(p);
311 0972d8b2 2021-03-02 op }
312 0972d8b2 2021-03-02 op
313 2051e653 2021-03-13 op static void
314 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
315 4d3785b1 2021-03-09 op {
316 4d3785b1 2021-03-09 op struct proto *p;
317 4d3785b1 2021-03-09 op
318 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
319 4d3785b1 2021-03-09 op if (has_prefix(url, p->schema)) {
320 4d3785b1 2021-03-09 op p->loadfn(tab, url);
321 4d3785b1 2021-03-09 op return;
322 4d3785b1 2021-03-09 op }
323 4d3785b1 2021-03-09 op }
324 4d3785b1 2021-03-09 op
325 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
326 4d3785b1 2021-03-09 op }
327 4d3785b1 2021-03-09 op
328 9ad4627d 2021-03-10 op void
329 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
330 2051e653 2021-03-13 op {
331 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
332 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
333 2051e653 2021-03-13 op
334 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
335 2051e653 2021-03-13 op event_loopbreak();
336 2051e653 2021-03-13 op return;
337 2051e653 2021-03-13 op }
338 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
339 2051e653 2021-03-13 op do_load_url(tab, url);
340 2051e653 2021-03-13 op }
341 2051e653 2021-03-13 op
342 2051e653 2021-03-13 op int
343 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
344 2051e653 2021-03-13 op {
345 2051e653 2021-03-13 op struct hist *h;
346 2051e653 2021-03-13 op
347 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
348 2051e653 2021-03-13 op return 0;
349 2051e653 2021-03-13 op tab->hist_cur = h;
350 2051e653 2021-03-13 op do_load_url(tab, h->h);
351 2051e653 2021-03-13 op return 1;
352 2051e653 2021-03-13 op }
353 2051e653 2021-03-13 op
354 2051e653 2021-03-13 op int
355 2051e653 2021-03-13 op load_next_page(struct tab *tab)
356 2051e653 2021-03-13 op {
357 2051e653 2021-03-13 op struct hist *h;
358 2051e653 2021-03-13 op
359 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
360 2051e653 2021-03-13 op return 0;
361 2051e653 2021-03-13 op tab->hist_cur = h;
362 2051e653 2021-03-13 op do_load_url(tab, h->h);
363 2051e653 2021-03-13 op return 1;
364 2051e653 2021-03-13 op }
365 2051e653 2021-03-13 op
366 2051e653 2021-03-13 op void
367 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
368 9ad4627d 2021-03-10 op {
369 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
370 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
371 9ad4627d 2021-03-10 op }
372 9ad4627d 2021-03-10 op
373 740f578b 2021-03-15 op void
374 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
375 740f578b 2021-03-15 op {
376 740f578b 2021-03-15 op imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
377 740f578b 2021-03-15 op imsg_flush(fsibuf);
378 740f578b 2021-03-15 op }
379 740f578b 2021-03-15 op
380 5e11c00c 2021-03-02 op int
381 5e11c00c 2021-03-02 op main(void)
382 5e11c00c 2021-03-02 op {
383 35e1f40a 2021-03-14 op struct imsgbuf network_ibuf, fs_ibuf;
384 35e1f40a 2021-03-14 op int net_fds[2], fs_fds[2];
385 35e1f40a 2021-03-14 op pid_t pid;
386 5e11c00c 2021-03-02 op
387 35e1f40a 2021-03-14 op pid = getpid();
388 35e1f40a 2021-03-14 op
389 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
390 5e11c00c 2021-03-02 op
391 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
392 5e11c00c 2021-03-02 op err(1, "socketpair");
393 5e11c00c 2021-03-02 op
394 5e11c00c 2021-03-02 op switch (fork()) {
395 5e11c00c 2021-03-02 op case -1:
396 5e11c00c 2021-03-02 op err(1, "fork");
397 5e11c00c 2021-03-02 op case 0:
398 5e11c00c 2021-03-02 op /* child */
399 35e1f40a 2021-03-14 op setproctitle("(%d) fs", pid);
400 35e1f40a 2021-03-14 op close(fs_fds[0]);
401 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[1]);
402 35e1f40a 2021-03-14 op exit(fs_main(&fs_ibuf));
403 35e1f40a 2021-03-14 op default:
404 35e1f40a 2021-03-14 op close(fs_fds[1]);
405 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[0]);
406 35e1f40a 2021-03-14 op fsibuf = &fs_ibuf;
407 5e11c00c 2021-03-02 op }
408 b1d4d01b 2021-03-14 op
409 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
410 35e1f40a 2021-03-14 op err(1, "socketpair");
411 5e11c00c 2021-03-02 op
412 35e1f40a 2021-03-14 op switch (fork()) {
413 35e1f40a 2021-03-14 op case -1:
414 35e1f40a 2021-03-14 op err(1, "fork");
415 35e1f40a 2021-03-14 op case 0:
416 35e1f40a 2021-03-14 op /* child */
417 35e1f40a 2021-03-14 op setproctitle("(%d) client", pid);
418 35e1f40a 2021-03-14 op close(net_fds[0]);
419 35e1f40a 2021-03-14 op close(fs_fds[0]);
420 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[1]);
421 35e1f40a 2021-03-14 op exit(client_main(&network_ibuf));
422 35e1f40a 2021-03-14 op default:
423 35e1f40a 2021-03-14 op close(net_fds[1]);
424 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[0]);
425 35e1f40a 2021-03-14 op netibuf = &network_ibuf;
426 35e1f40a 2021-03-14 op }
427 5e11c00c 2021-03-02 op
428 35e1f40a 2021-03-14 op setproctitle("(%d) ui", pid);
429 35e1f40a 2021-03-14 op
430 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
431 5e11c00c 2021-03-02 op
432 5e11c00c 2021-03-02 op event_init();
433 5e11c00c 2021-03-02 op
434 35e1f40a 2021-03-14 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, netibuf);
435 35e1f40a 2021-03-14 op event_add(&netev, NULL);
436 5e11c00c 2021-03-02 op
437 35e1f40a 2021-03-14 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, fsibuf);
438 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
439 35e1f40a 2021-03-14 op
440 5e11c00c 2021-03-02 op ui_init();
441 5e11c00c 2021-03-02 op
442 b1d4d01b 2021-03-14 op sandbox_ui_process();
443 b1d4d01b 2021-03-14 op
444 5e11c00c 2021-03-02 op event_dispatch();
445 5e11c00c 2021-03-02 op
446 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
447 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
448 5e11c00c 2021-03-02 op
449 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
450 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
451 35e1f40a 2021-03-14 op
452 5e11c00c 2021-03-02 op ui_end();
453 5e11c00c 2021-03-02 op
454 5e11c00c 2021-03-02 op return 0;
455 5e11c00c 2021-03-02 op }