Blob


1 #include <sys/socket.h>
3 #include <errno.h>
4 #include <limits.h>
5 #include <signal.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
11 #include "parser.h"
12 #include "telescope.h"
13 #include "ui.h"
15 struct event netev, fsev;
16 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
17 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
19 enum telescope_process {
20 PROC_UI,
21 PROC_FS,
22 PROC_NET,
23 };
25 /* the first is also the fallback one */
26 static struct proto protos[] = {
27 { "gemini", load_gemini_url },
28 { "about", load_about_url },
29 { NULL, NULL },
30 };
32 static struct imsgbuf *netibuf, *fsibuf;
34 static void die(void) __attribute__((__noreturn__));
35 static struct tab *tab_by_id(uint32_t);
36 static void handle_imsg_err(struct imsg*, size_t);
37 static void handle_imsg_check_cert(struct imsg*, size_t);
38 static void handle_check_cert_user_choice(int, struct tab *);
39 static void handle_maybe_save_new_cert(int, struct tab *);
40 static void handle_imsg_got_code(struct imsg*, size_t);
41 static void handle_imsg_got_meta(struct imsg*, size_t);
42 static void handle_maybe_save_page(int, struct tab *);
43 static void handle_save_page_path(const char *, unsigned int);
44 static void handle_imsg_file_opened(struct imsg*, size_t);
45 static void handle_imsg_buf(struct imsg*, size_t);
46 static void handle_imsg_eof(struct imsg*, size_t);
47 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
48 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
49 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
50 static void handle_dispatch_imsg(int, short, void*);
51 static void load_page_from_str(struct tab*, const char*);
52 static void do_load_url(struct tab*, const char*);
53 static pid_t start_child(enum telescope_process, const char *, int);
55 static imsg_handlerfn *handlers[] = {
56 [IMSG_ERR] = handle_imsg_err,
57 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
58 [IMSG_GOT_CODE] = handle_imsg_got_code,
59 [IMSG_GOT_META] = handle_imsg_got_meta,
60 [IMSG_BUF] = handle_imsg_buf,
61 [IMSG_EOF] = handle_imsg_eof,
62 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
63 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
64 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
65 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
66 };
68 static struct ohash certs;
70 static void __attribute__((__noreturn__))
71 die(void)
72 {
73 abort(); /* TODO */
74 }
76 static struct tab *
77 tab_by_id(uint32_t id)
78 {
79 struct tab *t;
81 TAILQ_FOREACH(t, &tabshead, tabs) {
82 if (t->id == id)
83 return t;
84 }
86 die();
87 }
89 static void
90 handle_imsg_err(struct imsg *imsg, size_t datalen)
91 {
92 struct tab *tab;
93 char *page;
95 tab = tab_by_id(imsg->hdr.peerid);
97 page = imsg->data;
98 page[datalen-1] = '\0';
100 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
101 tab->hist_cur->h, page) == -1)
102 die();
103 load_page_from_str(tab, page);
104 free(page);
107 static void
108 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
110 const char *hash, *host, *port;
111 int tofu_res;
112 struct tofu_entry *e;
113 struct tab *tab;
115 hash = imsg->data;
116 if (hash[datalen-1] != '\0')
117 abort();
119 tab = tab_by_id(imsg->hdr.peerid);
121 if (tab->proxy != NULL) {
122 host = tab->proxy->host;
123 port = tab->proxy->port;
124 } else {
125 host = tab->uri.host;
126 port = tab->uri.port;
129 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
130 /* TODO: an update in libressl/libretls changed
131 * significantly. Find a better approach at storing
132 * the certs! */
133 if (datalen > sizeof(e->hash))
134 abort();
136 tofu_res = 1; /* trust on first use */
137 if ((e = calloc(1, sizeof(*e))) == NULL)
138 abort();
139 strlcpy(e->domain, host, sizeof(e->domain));
140 if (*port != '\0' && strcmp(port, "1965")) {
141 strlcat(e->domain, ":", sizeof(e->domain));
142 strlcat(e->domain, port, sizeof(e->domain));
144 strlcpy(e->hash, hash, sizeof(e->hash));
145 tofu_add(&certs, e);
146 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
147 e, sizeof(*e));
148 imsg_flush(fsibuf);
149 } else
150 tofu_res = !strcmp(hash, e->hash);
152 if (tofu_res) {
153 if (e->verified == -1)
154 tab->trust = TS_TEMP_TRUSTED;
155 else if (e->verified == 1)
156 tab->trust = TS_VERIFIED;
157 else
158 tab->trust = TS_TRUSTED;
160 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
161 &tofu_res, sizeof(tofu_res));
162 imsg_flush(netibuf);
163 } else {
164 tab->trust = TS_UNTRUSTED;
165 load_page_from_str(tab, "# Certificate mismatch\n");
166 if ((tab->cert = strdup(hash)) == NULL)
167 die();
168 ui_yornp("Certificate mismatch. Proceed?",
169 handle_check_cert_user_choice, tab);
173 static void
174 handle_check_cert_user_choice(int accept, struct tab *tab)
176 imsg_compose(netibuf, IMSG_CERT_STATUS, tab->id, 0, -1,
177 &accept, sizeof(accept));
178 imsg_flush(netibuf);
180 if (accept) {
181 /*
182 * trust the certificate for this session only. If
183 * the page results in a redirect while we're asking
184 * the user to save, we'll end up with an invalid
185 * tabid (one request == one tab id) and crash. It
186 * also makes sense to save it for the current session
187 * if the user accepted it.
188 */
189 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
191 ui_yornp("Save the new certificate?",
192 handle_maybe_save_new_cert, tab);
193 } else {
194 free(tab->cert);
195 tab->cert = NULL;
199 static void
200 handle_maybe_save_new_cert(int accept, struct tab *tab)
202 struct tofu_entry *e;
203 const char *host, *port;
205 if (tab->proxy != NULL) {
206 host = tab->proxy->host;
207 port = tab->proxy->port;
208 } else {
209 host = tab->uri.host;
210 port = tab->uri.port;
213 if (!accept)
214 goto end;
216 if ((e = calloc(1, sizeof(*e))) == NULL)
217 die();
219 strlcpy(e->domain, host, sizeof(e->domain));
220 if (*port != '\0' && strcmp(port, "1965")) {
221 strlcat(e->domain, ":", sizeof(e->domain));
222 strlcat(e->domain, port, sizeof(e->domain));
224 strlcpy(e->hash, tab->cert, sizeof(e->hash));
225 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
226 imsg_flush(fsibuf);
228 tofu_update(&certs, e);
230 tab->trust = TS_TRUSTED;
232 end:
233 free(tab->cert);
234 tab->cert = NULL;
237 static inline int
238 normalize_code(int n)
240 if (n < 20) {
241 if (n == 10 || n == 11)
242 return n;
243 return 10;
244 } else if (n < 30) {
245 return 20;
246 } else if (n < 40) {
247 if (n == 30 || n == 31)
248 return n;
249 return 30;
250 } else if (n < 50) {
251 if (n <= 44)
252 return n;
253 return 40;
254 } else if (n < 60) {
255 if (n <= 53 || n == 59)
256 return n;
257 return 50;
258 } else if (n < 70) {
259 if (n <= 62)
260 return n;
261 return 60;
262 } else
263 return MALFORMED_RESPONSE;
266 static void
267 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
269 struct tab *tab;
271 tab = tab_by_id(imsg->hdr.peerid);
273 if (sizeof(tab->code) != datalen)
274 die();
276 memcpy(&tab->code, imsg->data, sizeof(tab->code));
277 tab->code = normalize_code(tab->code);
278 if (tab->code != 30 && tab->code != 31)
279 tab->redirect_count = 0;
282 static void
283 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
285 struct tab *tab;
287 tab = tab_by_id(imsg->hdr.peerid);
289 if (sizeof(tab->meta) <= datalen)
290 die();
292 memcpy(tab->meta, imsg->data, datalen);
294 if (tab->code < 10) { /* internal errors */
295 load_page_from_str(tab, err_pages[tab->code]);
296 } else if (tab->code < 20) { /* 1x */
297 load_page_from_str(tab, err_pages[tab->code]);
298 ui_require_input(tab, tab->code == 11);
299 } else if (tab->code == 20) {
300 if (setup_parser_for(tab)) {
301 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
302 imsg_flush(netibuf);
303 } else {
304 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
305 ui_yornp("Can't display page, wanna save?",
306 handle_maybe_save_page, tab);
308 } else if (tab->code < 40) { /* 3x */
309 tab->redirect_count++;
311 /* TODO: make customizable? */
312 if (tab->redirect_count > 5) {
313 load_page_from_str(tab,
314 err_pages[TOO_MUCH_REDIRECTS]);
315 } else
316 do_load_url(tab, tab->meta);
317 } else { /* 4x, 5x & 6x */
318 load_page_from_str(tab, err_pages[tab->code]);
322 static void
323 handle_maybe_save_page(int dosave, struct tab *tab)
325 if (dosave)
326 ui_read("Save to path", handle_save_page_path, tab->id);
327 else
328 stop_tab(tab);
331 static void
332 handle_save_page_path(const char *path, unsigned int tabid)
334 struct tab *tab;
336 if (path == NULL) {
337 stop_tab(tab_by_id(tabid));
338 return;
341 tab = tab_by_id(tabid);
342 tab->path = strdup(path);
344 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
345 imsg_flush(fsibuf);
348 static void
349 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
351 struct tab *tab;
352 char *page;
353 const char *e;
354 int l;
356 tab = tab_by_id(imsg->hdr.peerid);
358 if (imsg->fd == -1) {
359 stop_tab(tab);
361 e = imsg->data;
362 if (e[datalen-1] != '\0')
363 die();
364 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
365 tab->path, e);
366 if (l == -1)
367 die();
368 load_page_from_str(tab, page);
369 free(page);
370 } else {
371 tab->fd = imsg->fd;
372 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
373 imsg_flush(netibuf);
377 static void
378 handle_imsg_buf(struct imsg *imsg, size_t datalen)
380 struct tab *tab;
381 int l;
382 char *page, buf[FMT_SCALED_STRSIZE] = {0};
384 tab = tab_by_id(imsg->hdr.peerid);
386 tab->bytes += datalen;
387 if (tab->fd == -1) {
388 if (!tab->buffer.page.parse(&tab->buffer.page,
389 imsg->data, datalen))
390 die();
391 } else {
392 write(tab->fd, imsg->data, datalen);
393 fmt_scaled(tab->bytes, buf);
394 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
395 tab->path,
396 buf);
397 if (l == -1)
398 die();
399 load_page_from_str(tab, page);
400 free(page);
403 ui_on_tab_refresh(tab);
406 static void
407 handle_imsg_eof(struct imsg *imsg, size_t datalen)
409 struct tab *tab;
410 int l;
411 char *page, buf[FMT_SCALED_STRSIZE] = {0};
413 tab = tab_by_id(imsg->hdr.peerid);
415 if (tab->fd == -1) {
416 if (!tab->buffer.page.free(&tab->buffer.page))
417 die();
418 } else {
419 fmt_scaled(tab->bytes, buf);
420 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
421 tab->path,
422 buf);
423 if (l == -1)
424 die();
425 load_page_from_str(tab, page);
426 free(page);
428 close(tab->fd);
429 tab->fd = -1;
430 free(tab->path);
431 tab->path = NULL;
434 ui_on_tab_refresh(tab);
435 ui_on_tab_loaded(tab);
438 static void
439 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
441 int res;
443 if (datalen != sizeof(res))
444 die();
446 memcpy(&res, imsg->data, sizeof(res));
447 if (res == 0)
448 message("Added to bookmarks!");
449 else
450 message("Failed to add to bookmarks: %s",
451 strerror(res));
454 static void
455 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
457 int res;
459 if (datalen != sizeof(res))
460 die();
461 memcpy(&res, imsg->data, datalen);
462 if (res != 0)
463 message("Failed to save the cert for: %s",
464 strerror(res));
467 static void
468 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
470 int res;
472 if (datalen != sizeof(res))
473 die();
474 memcpy(&res, imsg->data, datalen);
475 if (!res)
476 message("Failed to update the certificate");
479 static void
480 handle_dispatch_imsg(int fd, short ev, void *d)
482 struct imsgbuf *ibuf = d;
483 dispatch_imsg(ibuf, handlers, sizeof(handlers));
486 static void
487 load_page_from_str(struct tab *tab, const char *page)
489 erase_buffer(&tab->buffer);
490 gemtext_initparser(&tab->buffer.page);
491 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
492 die();
493 if (!tab->buffer.page.free(&tab->buffer.page))
494 die();
495 ui_on_tab_refresh(tab);
496 ui_on_tab_loaded(tab);
499 void
500 load_about_url(struct tab *tab, const char *url)
502 tab->trust = TS_VERIFIED;
504 gemtext_initparser(&tab->buffer.page);
506 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
507 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
508 imsg_flush(fsibuf);
511 void
512 load_gemini_url(struct tab *tab, const char *url)
514 struct get_req req;
516 stop_tab(tab);
517 tab->id = tab_new_id();
519 memset(&req, 0, sizeof(req));
520 strlcpy(req.host, tab->uri.host, sizeof(req.host));
521 strlcpy(req.port, tab->uri.port, sizeof(req.host));
523 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
524 strlcat(req.req, "\r\n", sizeof(req.req));
526 req.proto = PROTO_GEMINI;
528 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
529 &req, sizeof(req));
530 imsg_flush(netibuf);
533 void
534 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
536 struct get_req req;
538 stop_tab(tab);
539 tab->id = tab_new_id();
540 tab->proxy = p;
542 memset(&req, 0, sizeof(req));
543 strlcpy(req.host, p->host, sizeof(req.host));
544 strlcpy(req.port, p->port, sizeof(req.host));
546 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
547 strlcat(req.req, "\r\n", sizeof(req.req));
549 req.proto = p->proto;
551 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
552 &req, sizeof(req));
553 imsg_flush(netibuf);
556 static void
557 do_load_url(struct tab *tab, const char *url)
559 struct phos_uri uri;
560 struct proto *p;
561 struct proxy *proxy;
562 char *t;
564 tab->proxy = NULL;
566 if (tab->fd != -1) {
567 close(tab->fd);
568 tab->fd = -1;
569 free(tab->path);
570 tab->path = NULL;
573 tab->trust = TS_UNKNOWN;
575 memcpy(&uri, &tab->uri, sizeof(tab->uri));
576 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
577 if (asprintf(&t, "#error loading %s\n>%s\n",
578 url, "Can't parse the URI") == -1)
579 die();
580 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
581 load_page_from_str(tab, t);
582 free(t);
583 return;
586 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
587 sizeof(tab->hist_cur->h));
589 for (p = protos; p->schema != NULL; ++p) {
590 if (!strcmp(tab->uri.scheme, p->schema)) {
591 p->loadfn(tab, url);
592 return;
596 TAILQ_FOREACH(proxy, &proxies, proxies) {
597 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
598 load_via_proxy(tab, url, proxy);
599 return;
603 protos[0].loadfn(tab, url);
606 void
607 load_url(struct tab *tab, const char *url)
609 if (tab->hist_cur != NULL)
610 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
612 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
613 event_loopbreak();
614 return;
617 hist_push(&tab->hist, tab->hist_cur);
618 do_load_url(tab, url);
619 erase_buffer(&tab->buffer);
622 int
623 load_previous_page(struct tab *tab)
625 struct hist *h;
627 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
628 return 0;
629 tab->hist_cur = h;
630 do_load_url(tab, h->h);
631 return 1;
634 int
635 load_next_page(struct tab *tab)
637 struct hist *h;
639 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
640 return 0;
641 tab->hist_cur = h;
642 do_load_url(tab, h->h);
643 return 1;
646 void
647 stop_tab(struct tab *tab)
649 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
650 imsg_flush(netibuf);
652 if (tab->fd != -1) {
653 close(tab->fd);
654 tab->fd = -1;
655 free(tab->path);
656 tab->path = NULL;
657 load_page_from_str(tab, "Stopped.\n");
661 void
662 add_to_bookmarks(const char *str)
664 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
665 imsg_flush(fsibuf);
668 void
669 save_session(void)
671 struct tab *tab;
673 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
674 imsg_flush(fsibuf);
676 TAILQ_FOREACH(tab, &tabshead, tabs) {
677 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
678 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
679 imsg_flush(fsibuf);
682 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
683 imsg_flush(fsibuf);
686 static void
687 session_new_tab_cb(const char *url)
689 new_tab(url);
692 static pid_t
693 start_child(enum telescope_process p, const char *argv0, int fd)
695 const char *argv[4];
696 int argc = 0;
697 pid_t pid;
699 switch (pid = fork()) {
700 case -1:
701 die();
702 case 0:
703 break;
704 default:
705 close(fd);
706 return pid;
709 if (dup2(fd, 3) == -1)
710 err(1, "cannot setup imsg fd");
712 argv[argc++] = argv0;
713 switch (p) {
714 case PROC_UI:
715 errx(1, "Can't start ui process");
716 case PROC_FS:
717 argv[argc++] = "-Tf";
718 break;
719 case PROC_NET:
720 argv[argc++] = "-Tn";
721 break;
724 argv[argc++] = NULL;
725 execvp(argv0, (char *const *)argv);
726 err(1, "execvp(%s)", argv0);
729 static void __attribute__((noreturn))
730 usage(int r)
732 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
733 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
734 exit(r);
737 int
738 main(int argc, char * const *argv)
740 struct imsgbuf net_ibuf, fs_ibuf;
741 int net_fds[2], fs_fds[2];
742 int ch, configtest = 0, fail = 0;
743 int has_url = 0;
744 int proc = -1;
745 char path[PATH_MAX];
746 const char *url = NEW_TAB_URL;
747 const char *argv0;
749 argv0 = argv[0];
751 signal(SIGCHLD, SIG_IGN);
752 signal(SIGPIPE, SIG_IGN);
754 if (getenv("NO_COLOR") != NULL)
755 enable_colors = 0;
757 strlcpy(path, getenv("HOME"), sizeof(path));
758 strlcat(path, "/.telescope/config", sizeof(path));
760 while ((ch = getopt(argc, argv, "c:hnT:")) != -1) {
761 switch (ch) {
762 case 'c':
763 fail = 1;
764 strlcpy(path, optarg, sizeof(path));
765 break;
766 case 'n':
767 configtest = 1;
768 break;
769 case 'h':
770 usage(0);
771 case 'T':
772 switch (*optarg) {
773 case 'f':
774 proc = PROC_FS;
775 break;
776 case 'n':
777 proc = PROC_NET;
778 break;
779 default:
780 errx(1, "invalid process spec %c",
781 *optarg);
783 break;
784 default:
785 usage(1);
789 argc -= optind;
790 argv += optind;
792 if (proc != -1) {
793 if (argc > 0)
794 usage(1);
795 else if (proc == PROC_FS)
796 return fs_main();
797 else if (proc == PROC_NET)
798 return client_main();
799 else
800 usage(1);
803 if (argc != 0) {
804 has_url = 1;
805 url = argv[0];
808 /* setup keys before reading the config */
809 TAILQ_INIT(&global_map.m);
810 global_map.unhandled_input = global_key_unbound;
811 TAILQ_INIT(&minibuffer_map.m);
813 config_init();
814 parseconfig(path, fail);
815 if (configtest){
816 puts("config OK");
817 exit(0);
820 /* Start children. */
821 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
822 err(1, "socketpair");
823 start_child(PROC_FS, argv0, fs_fds[1]);
824 imsg_init(&fs_ibuf, fs_fds[0]);
825 fsibuf = &fs_ibuf;
827 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
828 err(1, "socketpair");
829 start_child(PROC_NET, argv0, net_fds[1]);
830 imsg_init(&net_ibuf, net_fds[0]);
831 netibuf = &net_ibuf;
833 setproctitle("ui");
835 /* initialize tofu & load certificates */
836 fs_init();
837 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
838 load_certs(&certs);
840 event_init();
842 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
843 handle_dispatch_imsg, netibuf);
844 event_add(&netev, NULL);
846 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
847 handle_dispatch_imsg, fsibuf);
848 event_add(&fsev, NULL);
850 if (ui_init()) {
851 load_last_session(session_new_tab_cb);
852 if (has_url || TAILQ_EMPTY(&tabshead))
853 new_tab(url);
855 sandbox_ui_process();
856 event_dispatch();
857 ui_end();
860 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
861 imsg_flush(netibuf);
863 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
864 imsg_flush(fsibuf);
866 return 0;