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 1304bbdd 2021-03-15 op static void handle_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 1304bbdd 2021-03-15 op handle_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 1304bbdd 2021-03-15 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
224 5e11c00c 2021-03-02 op }
225 5e11c00c 2021-03-02 op
226 0972d8b2 2021-03-02 op static void
227 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
228 0972d8b2 2021-03-02 op {
229 0972d8b2 2021-03-02 op gemtext_initparser(&tab->page);
230 3a9b9365 2021-03-09 op if (!tab->page.parse(&tab->page, page, strlen(page)))
231 0972d8b2 2021-03-02 op die();
232 0972d8b2 2021-03-02 op if (!tab->page.free(&tab->page))
233 0972d8b2 2021-03-02 op die();
234 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
235 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
236 0972d8b2 2021-03-02 op }
237 0972d8b2 2021-03-02 op
238 bcb0b073 2021-03-07 op void
239 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
240 0972d8b2 2021-03-02 op {
241 2051e653 2021-03-13 op char *m;
242 2051e653 2021-03-13 op size_t len;
243 8af5e5ed 2021-03-08 op
244 2051e653 2021-03-13 op
245 4d3785b1 2021-03-09 op memset(&tab->url, 0, sizeof(tab->url));
246 4d3785b1 2021-03-09 op
247 4d3785b1 2021-03-09 op m = strchr(url, ':');
248 4d3785b1 2021-03-09 op strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
249 4d3785b1 2021-03-09 op strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
250 097ff997 2021-03-09 op
251 2051e653 2021-03-13 op len = sizeof(tab->hist_cur->h)-1;
252 2051e653 2021-03-13 op strlcpy(tab->hist_cur->h, url, len);
253 4d3785b1 2021-03-09 op
254 35e1f40a 2021-03-14 op gemtext_initparser(&tab->page);
255 35e1f40a 2021-03-14 op
256 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
257 35e1f40a 2021-03-14 op tab->hist_cur->h, len+1);
258 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
259 4d3785b1 2021-03-09 op }
260 0972d8b2 2021-03-02 op
261 4d3785b1 2021-03-09 op void
262 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
263 4d3785b1 2021-03-09 op {
264 4d3785b1 2021-03-09 op const char *err;
265 4d3785b1 2021-03-09 op char *p;
266 2051e653 2021-03-13 op size_t len;
267 754622a2 2021-03-15 op
268 754622a2 2021-03-15 op len = sizeof(tab->hist_cur->h)-1;
269 3a9b9365 2021-03-09 op
270 4d3785b1 2021-03-09 op if (has_prefix(url, "gemini:")) {
271 4d3785b1 2021-03-09 op if (!url_parse(url, &tab->url, &err))
272 3a9b9365 2021-03-09 op goto err;
273 4d3785b1 2021-03-09 op } else {
274 4d3785b1 2021-03-09 op if (!url_resolve_from(&tab->url, url, &err))
275 4d3785b1 2021-03-09 op goto err;
276 3a9b9365 2021-03-09 op }
277 3a9b9365 2021-03-09 op
278 2051e653 2021-03-13 op url_unparse(&tab->url, tab->hist_cur->h, len);
279 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
280 2051e653 2021-03-13 op tab->hist_cur->h, len+1);
281 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
282 3a9b9365 2021-03-09 op return;
283 3a9b9365 2021-03-09 op
284 3a9b9365 2021-03-09 op err:
285 4d3785b1 2021-03-09 op if (asprintf(&p, "#error loading %s\n>%s\n",
286 4d3785b1 2021-03-09 op url, err) == -1)
287 3a9b9365 2021-03-09 op die();
288 2051e653 2021-03-13 op strlcpy(tab->hist_cur->h, url, len);
289 4d3785b1 2021-03-09 op load_page_from_str(tab, p);
290 4d3785b1 2021-03-09 op free(p);
291 0972d8b2 2021-03-02 op }
292 0972d8b2 2021-03-02 op
293 2051e653 2021-03-13 op static void
294 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
295 4d3785b1 2021-03-09 op {
296 4d3785b1 2021-03-09 op struct proto *p;
297 4d3785b1 2021-03-09 op
298 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
299 4d3785b1 2021-03-09 op if (has_prefix(url, p->schema)) {
300 4d3785b1 2021-03-09 op p->loadfn(tab, url);
301 4d3785b1 2021-03-09 op return;
302 4d3785b1 2021-03-09 op }
303 4d3785b1 2021-03-09 op }
304 4d3785b1 2021-03-09 op
305 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
306 4d3785b1 2021-03-09 op }
307 4d3785b1 2021-03-09 op
308 9ad4627d 2021-03-10 op void
309 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
310 2051e653 2021-03-13 op {
311 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
312 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
313 2051e653 2021-03-13 op
314 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
315 2051e653 2021-03-13 op event_loopbreak();
316 2051e653 2021-03-13 op return;
317 2051e653 2021-03-13 op }
318 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
319 2051e653 2021-03-13 op do_load_url(tab, url);
320 2051e653 2021-03-13 op }
321 2051e653 2021-03-13 op
322 2051e653 2021-03-13 op int
323 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
324 2051e653 2021-03-13 op {
325 2051e653 2021-03-13 op struct hist *h;
326 2051e653 2021-03-13 op
327 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
328 2051e653 2021-03-13 op return 0;
329 2051e653 2021-03-13 op tab->hist_cur = h;
330 2051e653 2021-03-13 op do_load_url(tab, h->h);
331 2051e653 2021-03-13 op return 1;
332 2051e653 2021-03-13 op }
333 2051e653 2021-03-13 op
334 2051e653 2021-03-13 op int
335 2051e653 2021-03-13 op load_next_page(struct tab *tab)
336 2051e653 2021-03-13 op {
337 2051e653 2021-03-13 op struct hist *h;
338 2051e653 2021-03-13 op
339 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
340 2051e653 2021-03-13 op return 0;
341 2051e653 2021-03-13 op tab->hist_cur = h;
342 2051e653 2021-03-13 op do_load_url(tab, h->h);
343 2051e653 2021-03-13 op return 1;
344 2051e653 2021-03-13 op }
345 2051e653 2021-03-13 op
346 2051e653 2021-03-13 op void
347 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
348 9ad4627d 2021-03-10 op {
349 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
350 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
351 9ad4627d 2021-03-10 op }
352 9ad4627d 2021-03-10 op
353 740f578b 2021-03-15 op void
354 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
355 740f578b 2021-03-15 op {
356 740f578b 2021-03-15 op imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
357 740f578b 2021-03-15 op imsg_flush(fsibuf);
358 740f578b 2021-03-15 op }
359 740f578b 2021-03-15 op
360 5e11c00c 2021-03-02 op int
361 5e11c00c 2021-03-02 op main(void)
362 5e11c00c 2021-03-02 op {
363 35e1f40a 2021-03-14 op struct imsgbuf network_ibuf, fs_ibuf;
364 35e1f40a 2021-03-14 op int net_fds[2], fs_fds[2];
365 35e1f40a 2021-03-14 op pid_t pid;
366 5e11c00c 2021-03-02 op
367 35e1f40a 2021-03-14 op pid = getpid();
368 35e1f40a 2021-03-14 op
369 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
370 5e11c00c 2021-03-02 op
371 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
372 5e11c00c 2021-03-02 op err(1, "socketpair");
373 5e11c00c 2021-03-02 op
374 5e11c00c 2021-03-02 op switch (fork()) {
375 5e11c00c 2021-03-02 op case -1:
376 5e11c00c 2021-03-02 op err(1, "fork");
377 5e11c00c 2021-03-02 op case 0:
378 5e11c00c 2021-03-02 op /* child */
379 35e1f40a 2021-03-14 op setproctitle("(%d) fs", pid);
380 35e1f40a 2021-03-14 op close(fs_fds[0]);
381 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[1]);
382 35e1f40a 2021-03-14 op exit(fs_main(&fs_ibuf));
383 35e1f40a 2021-03-14 op default:
384 35e1f40a 2021-03-14 op close(fs_fds[1]);
385 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[0]);
386 35e1f40a 2021-03-14 op fsibuf = &fs_ibuf;
387 5e11c00c 2021-03-02 op }
388 b1d4d01b 2021-03-14 op
389 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
390 35e1f40a 2021-03-14 op err(1, "socketpair");
391 5e11c00c 2021-03-02 op
392 35e1f40a 2021-03-14 op switch (fork()) {
393 35e1f40a 2021-03-14 op case -1:
394 35e1f40a 2021-03-14 op err(1, "fork");
395 35e1f40a 2021-03-14 op case 0:
396 35e1f40a 2021-03-14 op /* child */
397 35e1f40a 2021-03-14 op setproctitle("(%d) client", pid);
398 35e1f40a 2021-03-14 op close(net_fds[0]);
399 35e1f40a 2021-03-14 op close(fs_fds[0]);
400 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[1]);
401 35e1f40a 2021-03-14 op exit(client_main(&network_ibuf));
402 35e1f40a 2021-03-14 op default:
403 35e1f40a 2021-03-14 op close(net_fds[1]);
404 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[0]);
405 35e1f40a 2021-03-14 op netibuf = &network_ibuf;
406 35e1f40a 2021-03-14 op }
407 5e11c00c 2021-03-02 op
408 35e1f40a 2021-03-14 op setproctitle("(%d) ui", pid);
409 35e1f40a 2021-03-14 op
410 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
411 5e11c00c 2021-03-02 op
412 5e11c00c 2021-03-02 op event_init();
413 5e11c00c 2021-03-02 op
414 1304bbdd 2021-03-15 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, netibuf);
415 35e1f40a 2021-03-14 op event_add(&netev, NULL);
416 5e11c00c 2021-03-02 op
417 1304bbdd 2021-03-15 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, fsibuf);
418 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
419 35e1f40a 2021-03-14 op
420 5e11c00c 2021-03-02 op ui_init();
421 5e11c00c 2021-03-02 op
422 b1d4d01b 2021-03-14 op sandbox_ui_process();
423 b1d4d01b 2021-03-14 op
424 5e11c00c 2021-03-02 op event_dispatch();
425 5e11c00c 2021-03-02 op
426 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
427 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
428 5e11c00c 2021-03-02 op
429 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
430 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
431 35e1f40a 2021-03-14 op
432 5e11c00c 2021-03-02 op ui_end();
433 5e11c00c 2021-03-02 op
434 5e11c00c 2021-03-02 op return 0;
435 5e11c00c 2021-03-02 op }