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"
14 struct event netev, fsev;
15 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
16 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
18 enum telescope_process {
19 PROC_UI,
20 PROC_FS,
21 PROC_NET,
22 };
24 /* the first is also the fallback one */
25 static struct proto protos[] = {
26 { "gemini", load_gemini_url },
27 { "about", load_about_url },
28 { NULL, NULL },
29 };
31 static struct imsgbuf *netibuf, *fsibuf;
33 static void die(void) __attribute__((__noreturn__));
34 static struct tab *tab_by_id(uint32_t);
35 static void handle_imsg_err(struct imsg*, size_t);
36 static void handle_imsg_check_cert(struct imsg*, size_t);
37 static void handle_check_cert_user_choice(int, struct tab *);
38 static void handle_maybe_save_new_cert(int, struct tab *);
39 static void handle_imsg_got_code(struct imsg*, size_t);
40 static void handle_imsg_got_meta(struct imsg*, size_t);
41 static void handle_maybe_save_page(int, struct tab *);
42 static void handle_save_page_path(const char *, unsigned int);
43 static void handle_imsg_file_opened(struct imsg*, size_t);
44 static void handle_imsg_buf(struct imsg*, size_t);
45 static void handle_imsg_eof(struct imsg*, size_t);
46 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
47 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
48 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
49 static void handle_dispatch_imsg(int, short, void*);
50 static void load_page_from_str(struct tab*, const char*);
51 static void do_load_url(struct tab*, const char*);
52 static pid_t start_child(enum telescope_process, const char *, int);
54 static imsg_handlerfn *handlers[] = {
55 [IMSG_ERR] = handle_imsg_err,
56 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
57 [IMSG_GOT_CODE] = handle_imsg_got_code,
58 [IMSG_GOT_META] = handle_imsg_got_meta,
59 [IMSG_BUF] = handle_imsg_buf,
60 [IMSG_EOF] = handle_imsg_eof,
61 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
62 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
63 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
64 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
65 };
67 static struct ohash certs;
69 static void __attribute__((__noreturn__))
70 die(void)
71 {
72 abort(); /* TODO */
73 }
75 static struct tab *
76 tab_by_id(uint32_t id)
77 {
78 struct tab *t;
80 TAILQ_FOREACH(t, &tabshead, tabs) {
81 if (t->id == id)
82 return t;
83 }
85 die();
86 }
88 static void
89 handle_imsg_err(struct imsg *imsg, size_t datalen)
90 {
91 struct tab *tab;
92 char *page;
94 tab = tab_by_id(imsg->hdr.peerid);
96 page = imsg->data;
97 page[datalen-1] = '\0';
99 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
100 tab->hist_cur->h, page) == -1)
101 die();
102 load_page_from_str(tab, page);
103 free(page);
106 static void
107 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
109 const char *hash, *host, *port;
110 int tofu_res;
111 struct tofu_entry *e;
112 struct tab *tab;
114 hash = imsg->data;
115 if (hash[datalen-1] != '\0')
116 abort();
118 tab = tab_by_id(imsg->hdr.peerid);
120 if (tab->proxy != NULL) {
121 host = tab->proxy->host;
122 port = tab->proxy->port;
123 } else {
124 host = tab->uri.host;
125 port = tab->uri.port;
128 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
129 /* TODO: an update in libressl/libretls changed
130 * significantly. Find a better approach at storing
131 * the certs! */
132 if (datalen > sizeof(e->hash))
133 abort();
135 tofu_res = 1; /* trust on first use */
136 if ((e = calloc(1, sizeof(*e))) == NULL)
137 abort();
138 strlcpy(e->domain, host, sizeof(e->domain));
139 if (*port != '\0' && strcmp(port, "1965")) {
140 strlcat(e->domain, ":", sizeof(e->domain));
141 strlcat(e->domain, port, sizeof(e->domain));
143 strlcpy(e->hash, hash, sizeof(e->hash));
144 tofu_add(&certs, e);
145 imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
146 e, sizeof(*e));
147 imsg_flush(fsibuf);
148 } else
149 tofu_res = !strcmp(hash, e->hash);
151 if (tofu_res) {
152 if (e->verified == -1)
153 tab->trust = TS_TEMP_TRUSTED;
154 else if (e->verified == 1)
155 tab->trust = TS_VERIFIED;
156 else
157 tab->trust = TS_TRUSTED;
159 imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
160 &tofu_res, sizeof(tofu_res));
161 imsg_flush(netibuf);
162 } else {
163 tab->trust = TS_UNTRUSTED;
164 load_page_from_str(tab, "# Certificate mismatch\n");
165 if ((tab->cert = strdup(hash)) == NULL)
166 die();
167 ui_yornp("Certificate mismatch. Proceed?",
168 handle_check_cert_user_choice, tab);
172 static void
173 handle_check_cert_user_choice(int accept, struct tab *tab)
175 imsg_compose(netibuf, IMSG_CERT_STATUS, tab->id, 0, -1,
176 &accept, sizeof(accept));
177 imsg_flush(netibuf);
179 if (accept) {
180 /*
181 * trust the certificate for this session only. If
182 * the page results in a redirect while we're asking
183 * the user to save, we'll end up with an invalid
184 * tabid (one request == one tab id) and crash. It
185 * also makes sense to save it for the current session
186 * if the user accepted it.
187 */
188 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
190 ui_yornp("Save the new certificate?",
191 handle_maybe_save_new_cert, tab);
192 } else {
193 free(tab->cert);
194 tab->cert = NULL;
198 static void
199 handle_maybe_save_new_cert(int accept, struct tab *tab)
201 struct tofu_entry *e;
202 const char *host, *port;
204 if (tab->proxy != NULL) {
205 host = tab->proxy->host;
206 port = tab->proxy->port;
207 } else {
208 host = tab->uri.host;
209 port = tab->uri.port;
212 if (!accept)
213 goto end;
215 if ((e = calloc(1, sizeof(*e))) == NULL)
216 die();
218 strlcpy(e->domain, host, sizeof(e->domain));
219 if (*port != '\0' && strcmp(port, "1965")) {
220 strlcat(e->domain, ":", sizeof(e->domain));
221 strlcat(e->domain, port, sizeof(e->domain));
223 strlcpy(e->hash, tab->cert, sizeof(e->hash));
224 imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
225 imsg_flush(fsibuf);
227 tofu_update(&certs, e);
229 tab->trust = TS_TRUSTED;
231 end:
232 free(tab->cert);
233 tab->cert = NULL;
236 static inline int
237 normalize_code(int n)
239 if (n < 20) {
240 if (n == 10 || n == 11)
241 return n;
242 return 10;
243 } else if (n < 30) {
244 return 20;
245 } else if (n < 40) {
246 if (n == 30 || n == 31)
247 return n;
248 return 30;
249 } else if (n < 50) {
250 if (n <= 44)
251 return n;
252 return 40;
253 } else if (n < 60) {
254 if (n <= 53 || n == 59)
255 return n;
256 return 50;
257 } else if (n < 70) {
258 if (n <= 62)
259 return n;
260 return 60;
261 } else
262 return MALFORMED_RESPONSE;
265 static void
266 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
268 struct tab *tab;
270 tab = tab_by_id(imsg->hdr.peerid);
272 if (sizeof(tab->code) != datalen)
273 die();
275 memcpy(&tab->code, imsg->data, sizeof(tab->code));
276 tab->code = normalize_code(tab->code);
277 if (tab->code != 30 && tab->code != 31)
278 tab->redirect_count = 0;
281 static void
282 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
284 struct tab *tab;
286 tab = tab_by_id(imsg->hdr.peerid);
288 if (sizeof(tab->meta) <= datalen)
289 die();
291 memcpy(tab->meta, imsg->data, datalen);
293 if (tab->code < 10) { /* internal errors */
294 load_page_from_str(tab, err_pages[tab->code]);
295 } else if (tab->code < 20) { /* 1x */
296 load_page_from_str(tab, err_pages[tab->code]);
297 ui_require_input(tab, tab->code == 11);
298 } else if (tab->code == 20) {
299 if (setup_parser_for(tab)) {
300 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
301 imsg_flush(netibuf);
302 } else {
303 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
304 ui_yornp("Can't display page, wanna save?",
305 handle_maybe_save_page, tab);
307 } else if (tab->code < 40) { /* 3x */
308 tab->redirect_count++;
310 /* TODO: make customizable? */
311 if (tab->redirect_count > 5) {
312 load_page_from_str(tab,
313 err_pages[TOO_MUCH_REDIRECTS]);
314 } else
315 do_load_url(tab, tab->meta);
316 } else { /* 4x, 5x & 6x */
317 load_page_from_str(tab, err_pages[tab->code]);
321 static void
322 handle_maybe_save_page(int dosave, struct tab *tab)
324 if (dosave)
325 ui_read("Save to path", handle_save_page_path, tab->id);
326 else
327 stop_tab(tab);
330 static void
331 handle_save_page_path(const char *path, unsigned int tabid)
333 struct tab *tab;
335 if (path == NULL) {
336 stop_tab(tab_by_id(tabid));
337 return;
340 tab = tab_by_id(tabid);
341 tab->path = strdup(path);
343 imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
344 imsg_flush(fsibuf);
347 static void
348 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
350 struct tab *tab;
351 char *page;
352 const char *e;
353 int l;
355 tab = tab_by_id(imsg->hdr.peerid);
357 if (imsg->fd == -1) {
358 stop_tab(tab);
360 e = imsg->data;
361 if (e[datalen-1] != '\0')
362 die();
363 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
364 tab->path, e);
365 if (l == -1)
366 die();
367 load_page_from_str(tab, page);
368 free(page);
369 } else {
370 tab->fd = imsg->fd;
371 imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
372 imsg_flush(netibuf);
376 static void
377 handle_imsg_buf(struct imsg *imsg, size_t datalen)
379 struct tab *tab;
380 int l;
381 char *page, buf[FMT_SCALED_STRSIZE] = {0};
383 tab = tab_by_id(imsg->hdr.peerid);
385 tab->bytes += datalen;
386 if (tab->fd == -1) {
387 if (!tab->buffer.page.parse(&tab->buffer.page,
388 imsg->data, datalen))
389 die();
390 } else {
391 write(tab->fd, imsg->data, datalen);
392 fmt_scaled(tab->bytes, buf);
393 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
394 tab->path,
395 buf);
396 if (l == -1)
397 die();
398 load_page_from_str(tab, page);
399 free(page);
402 ui_on_tab_refresh(tab);
405 static void
406 handle_imsg_eof(struct imsg *imsg, size_t datalen)
408 struct tab *tab;
409 int l;
410 char *page, buf[FMT_SCALED_STRSIZE] = {0};
412 tab = tab_by_id(imsg->hdr.peerid);
414 if (tab->fd == -1) {
415 if (!tab->buffer.page.free(&tab->buffer.page))
416 die();
417 } else {
418 fmt_scaled(tab->bytes, buf);
419 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
420 tab->path,
421 buf);
422 if (l == -1)
423 die();
424 load_page_from_str(tab, page);
425 free(page);
427 close(tab->fd);
428 tab->fd = -1;
429 free(tab->path);
430 tab->path = NULL;
433 ui_on_tab_refresh(tab);
434 ui_on_tab_loaded(tab);
437 static void
438 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
440 int res;
442 if (datalen != sizeof(res))
443 die();
445 memcpy(&res, imsg->data, sizeof(res));
446 if (res == 0)
447 message("Added to bookmarks!");
448 else
449 message("Failed to add to bookmarks: %s",
450 strerror(res));
453 static void
454 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
456 int res;
458 if (datalen != sizeof(res))
459 die();
460 memcpy(&res, imsg->data, datalen);
461 if (res != 0)
462 message("Failed to save the cert for: %s",
463 strerror(res));
466 static void
467 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
469 int res;
471 if (datalen != sizeof(res))
472 die();
473 memcpy(&res, imsg->data, datalen);
474 if (!res)
475 message("Failed to update the certificate");
478 static void
479 handle_dispatch_imsg(int fd, short ev, void *d)
481 struct imsgbuf *ibuf = d;
482 dispatch_imsg(ibuf, handlers, sizeof(handlers));
485 static void
486 load_page_from_str(struct tab *tab, const char *page)
488 erase_buffer(&tab->buffer);
489 gemtext_initparser(&tab->buffer.page);
490 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
491 die();
492 if (!tab->buffer.page.free(&tab->buffer.page))
493 die();
494 ui_on_tab_refresh(tab);
495 ui_on_tab_loaded(tab);
498 void
499 load_about_url(struct tab *tab, const char *url)
501 tab->trust = TS_VERIFIED;
503 gemtext_initparser(&tab->buffer.page);
505 imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
506 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
507 imsg_flush(fsibuf);
510 void
511 load_gemini_url(struct tab *tab, const char *url)
513 struct get_req req;
515 stop_tab(tab);
516 tab->id = tab_new_id();
518 memset(&req, 0, sizeof(req));
519 strlcpy(req.host, tab->uri.host, sizeof(req.host));
520 strlcpy(req.port, tab->uri.port, sizeof(req.host));
522 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
523 strlcat(req.req, "\r\n", sizeof(req.req));
525 req.proto = PROTO_GEMINI;
527 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
528 &req, sizeof(req));
529 imsg_flush(netibuf);
532 void
533 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
535 struct get_req req;
537 stop_tab(tab);
538 tab->id = tab_new_id();
539 tab->proxy = p;
541 memset(&req, 0, sizeof(req));
542 strlcpy(req.host, p->host, sizeof(req.host));
543 strlcpy(req.port, p->port, sizeof(req.host));
545 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
546 strlcat(req.req, "\r\n", sizeof(req.req));
548 req.proto = p->proto;
550 imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
551 &req, sizeof(req));
552 imsg_flush(netibuf);
555 static void
556 do_load_url(struct tab *tab, const char *url)
558 struct phos_uri uri;
559 struct proto *p;
560 struct proxy *proxy;
561 char *t;
563 tab->proxy = NULL;
565 if (tab->fd != -1) {
566 close(tab->fd);
567 tab->fd = -1;
568 free(tab->path);
569 tab->path = NULL;
572 tab->trust = TS_UNKNOWN;
574 memcpy(&uri, &tab->uri, sizeof(tab->uri));
575 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
576 if (asprintf(&t, "#error loading %s\n>%s\n",
577 url, "Can't parse the URI") == -1)
578 die();
579 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
580 load_page_from_str(tab, t);
581 free(t);
582 return;
585 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
586 sizeof(tab->hist_cur->h));
588 for (p = protos; p->schema != NULL; ++p) {
589 if (!strcmp(tab->uri.scheme, p->schema)) {
590 p->loadfn(tab, url);
591 return;
595 TAILQ_FOREACH(proxy, &proxies, proxies) {
596 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
597 load_via_proxy(tab, url, proxy);
598 return;
602 protos[0].loadfn(tab, url);
605 void
606 load_url(struct tab *tab, const char *url)
608 if (tab->hist_cur != NULL)
609 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
611 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
612 event_loopbreak();
613 return;
616 hist_push(&tab->hist, tab->hist_cur);
617 do_load_url(tab, url);
618 erase_buffer(&tab->buffer);
621 int
622 load_previous_page(struct tab *tab)
624 struct hist *h;
626 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
627 return 0;
628 tab->hist_cur = h;
629 do_load_url(tab, h->h);
630 return 1;
633 int
634 load_next_page(struct tab *tab)
636 struct hist *h;
638 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
639 return 0;
640 tab->hist_cur = h;
641 do_load_url(tab, h->h);
642 return 1;
645 void
646 stop_tab(struct tab *tab)
648 imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
649 imsg_flush(netibuf);
651 if (tab->fd != -1) {
652 close(tab->fd);
653 tab->fd = -1;
654 free(tab->path);
655 tab->path = NULL;
656 load_page_from_str(tab, "Stopped.\n");
660 void
661 add_to_bookmarks(const char *str)
663 imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
664 imsg_flush(fsibuf);
667 void
668 save_session(void)
670 struct tab *tab;
672 imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
673 imsg_flush(fsibuf);
675 TAILQ_FOREACH(tab, &tabshead, tabs) {
676 imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
677 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
678 imsg_flush(fsibuf);
681 imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
682 imsg_flush(fsibuf);
685 static void
686 session_new_tab_cb(const char *url)
688 new_tab(url);
691 static pid_t
692 start_child(enum telescope_process p, const char *argv0, int fd)
694 const char *argv[4];
695 int argc = 0;
696 pid_t pid;
698 switch (pid = fork()) {
699 case -1:
700 die();
701 case 0:
702 break;
703 default:
704 close(fd);
705 return pid;
708 if (dup2(fd, 3) == -1)
709 err(1, "cannot setup imsg fd");
711 argv[argc++] = argv0;
712 switch (p) {
713 case PROC_UI:
714 errx(1, "Can't start ui process");
715 case PROC_FS:
716 argv[argc++] = "-Tf";
717 break;
718 case PROC_NET:
719 argv[argc++] = "-Tn";
720 break;
723 argv[argc++] = NULL;
724 execvp(argv0, (char *const *)argv);
725 err(1, "execvp(%s)", argv0);
728 static void __attribute__((noreturn))
729 usage(int r)
731 fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
732 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
733 exit(r);
736 int
737 main(int argc, char * const *argv)
739 struct imsgbuf network_ibuf, fs_ibuf;
740 int net_fds[2], fs_fds[2];
741 int ch, configtest = 0, fail = 0;
742 int has_url = 0;
743 int proc = -1;
744 char path[PATH_MAX];
745 const char *url = NEW_TAB_URL;
746 const char *argv0;
748 argv0 = argv[0];
750 signal(SIGCHLD, SIG_IGN);
751 signal(SIGPIPE, SIG_IGN);
753 if (getenv("NO_COLOR") != NULL)
754 enable_colors = 0;
756 strlcpy(path, getenv("HOME"), sizeof(path));
757 strlcat(path, "/.telescope/config", sizeof(path));
759 while ((ch = getopt(argc, argv, "c:hnT:")) != -1) {
760 switch (ch) {
761 case 'c':
762 fail = 1;
763 strlcpy(path, optarg, sizeof(path));
764 break;
765 case 'n':
766 configtest = 1;
767 break;
768 case 'h':
769 usage(0);
770 case 'T':
771 switch (*optarg) {
772 case 'f':
773 proc = PROC_FS;
774 break;
775 case 'n':
776 proc = PROC_NET;
777 break;
778 default:
779 errx(1, "invalid process spec %c",
780 *optarg);
782 break;
783 default:
784 usage(1);
788 argc -= optind;
789 argv += optind;
791 if (proc != -1) {
792 if (argc > 0)
793 usage(1);
794 else if (proc == PROC_FS)
795 return fs_main();
796 else if (proc == PROC_NET)
797 return client_main();
798 else
799 usage(1);
802 if (argc != 0) {
803 has_url = 1;
804 url = argv[0];
807 /* setup keys before reading the config */
808 TAILQ_INIT(&global_map.m);
809 global_map.unhandled_input = global_key_unbound;
810 TAILQ_INIT(&minibuffer_map.m);
812 config_init();
813 parseconfig(path, fail);
814 if (configtest){
815 puts("config OK");
816 exit(0);
819 /* Start children. */
820 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
821 err(1, "socketpair");
822 start_child(PROC_FS, argv0, fs_fds[1]);
823 imsg_init(&fs_ibuf, fs_fds[0]);
824 fsibuf = &fs_ibuf;
826 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
827 err(1, "socketpair");
828 start_child(PROC_NET, argv0, net_fds[1]);
829 imsg_init(&network_ibuf, net_fds[0]);
830 netibuf = &network_ibuf;
832 setproctitle("ui");
834 /* initialize tofu & load certificates */
835 fs_init();
836 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
837 load_certs(&certs);
839 event_init();
841 event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
842 handle_dispatch_imsg, netibuf);
843 event_add(&netev, NULL);
845 event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
846 handle_dispatch_imsg, fsibuf);
847 event_add(&fsev, NULL);
849 if (ui_init()) {
850 load_last_session(session_new_tab_cb);
851 if (has_url || TAILQ_EMPTY(&tabshead))
852 new_tab(url);
854 sandbox_ui_process();
855 event_dispatch();
856 ui_end();
859 imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
860 imsg_flush(netibuf);
862 imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
863 imsg_flush(fsibuf);
865 return 0;