Blob


1 #include <sys/socket.h>
3 #include <errno.h>
4 #include <getopt.h>
5 #include <limits.h>
6 #include <signal.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
12 #include "defaults.h"
13 #include "pages.h"
14 #include "parser.h"
15 #include "telescope.h"
16 #include "ui.h"
18 static struct option longopts[] = {
19 {"colors", no_argument, NULL, 'c'},
20 {"help", no_argument, NULL, 'h'},
21 {"version", no_argument, NULL, 'v'},
22 {NULL, 0, NULL, 0},
23 };
25 static const char *opts = "Cc:hnT:v";
27 static struct imsgev *iev_fs, *iev_net;
29 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
30 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
32 enum telescope_process {
33 PROC_UI,
34 PROC_FS,
35 PROC_NET,
36 };
38 /* the first is also the fallback one */
39 static struct proto protos[] = {
40 { "gemini", load_gemini_url },
41 { "about", load_about_url },
42 { NULL, NULL },
43 };
45 static void die(void) __attribute__((__noreturn__));
46 static struct tab *tab_by_id(uint32_t);
47 static void handle_imsg_err(struct imsg*, size_t);
48 static void handle_imsg_check_cert(struct imsg*, size_t);
49 static void handle_check_cert_user_choice(int, struct tab *);
50 static void handle_maybe_save_new_cert(int, struct tab *);
51 static void handle_imsg_got_code(struct imsg*, size_t);
52 static void handle_imsg_got_meta(struct imsg*, size_t);
53 static void handle_maybe_save_page(int, struct tab *);
54 static void handle_save_page_path(const char *, struct tab *);
55 static void handle_imsg_file_opened(struct imsg*, size_t);
56 static void handle_imsg_buf(struct imsg*, size_t);
57 static void handle_imsg_eof(struct imsg*, size_t);
58 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
59 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
60 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
61 static void handle_dispatch_imsg(int, short, void*);
62 static void load_page_from_str(struct tab*, const char*);
63 static void do_load_url(struct tab*, const char*);
64 static pid_t start_child(enum telescope_process, const char *, int);
65 static int ui_send_net(int, uint32_t, const void *, uint16_t);
66 static int ui_send_fs(int, uint32_t, const void *, uint16_t);
68 static imsg_handlerfn *handlers[] = {
69 [IMSG_ERR] = handle_imsg_err,
70 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
71 [IMSG_GOT_CODE] = handle_imsg_got_code,
72 [IMSG_GOT_META] = handle_imsg_got_meta,
73 [IMSG_BUF] = handle_imsg_buf,
74 [IMSG_EOF] = handle_imsg_eof,
75 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
76 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
77 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
78 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
79 };
81 static struct ohash certs;
83 static void __attribute__((__noreturn__))
84 die(void)
85 {
86 abort(); /* TODO */
87 }
89 static struct tab *
90 tab_by_id(uint32_t id)
91 {
92 struct tab *t;
94 TAILQ_FOREACH(t, &tabshead, tabs) {
95 if (t->id == id)
96 return t;
97 }
99 die();
102 static void
103 handle_imsg_err(struct imsg *imsg, size_t datalen)
105 struct tab *tab;
106 char *page;
108 tab = tab_by_id(imsg->hdr.peerid);
110 page = imsg->data;
111 page[datalen-1] = '\0';
113 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
114 tab->hist_cur->h, page) == -1)
115 die();
116 load_page_from_str(tab, page);
117 free(page);
120 static void
121 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
123 const char *hash, *host, *port;
124 int tofu_res;
125 struct tofu_entry *e;
126 struct tab *tab;
128 hash = imsg->data;
129 if (hash[datalen-1] != '\0')
130 abort();
132 tab = tab_by_id(imsg->hdr.peerid);
134 if (tab->proxy != NULL) {
135 host = tab->proxy->host;
136 port = tab->proxy->port;
137 } else {
138 host = tab->uri.host;
139 port = tab->uri.port;
142 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
143 /* TODO: an update in libressl/libretls changed
144 * significantly. Find a better approach at storing
145 * the certs! */
146 if (datalen > sizeof(e->hash))
147 abort();
149 tofu_res = 1; /* trust on first use */
150 if ((e = calloc(1, sizeof(*e))) == NULL)
151 abort();
152 strlcpy(e->domain, host, sizeof(e->domain));
153 if (*port != '\0' && strcmp(port, "1965")) {
154 strlcat(e->domain, ":", sizeof(e->domain));
155 strlcat(e->domain, port, sizeof(e->domain));
157 strlcpy(e->hash, hash, sizeof(e->hash));
158 tofu_add(&certs, e);
159 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
160 } else
161 tofu_res = !strcmp(hash, e->hash);
163 if (tofu_res) {
164 if (e->verified == -1)
165 tab->trust = TS_TEMP_TRUSTED;
166 else if (e->verified == 1)
167 tab->trust = TS_VERIFIED;
168 else
169 tab->trust = TS_TRUSTED;
171 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
172 &tofu_res, sizeof(tofu_res));
173 } else {
174 tab->trust = TS_UNTRUSTED;
175 load_page_from_str(tab, "# Certificate mismatch\n");
176 if ((tab->cert = strdup(hash)) == NULL)
177 die();
178 ui_yornp("Certificate mismatch. Proceed?",
179 handle_check_cert_user_choice, tab);
183 static void
184 handle_check_cert_user_choice(int accept, struct tab *tab)
186 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
187 sizeof(accept));
189 if (accept) {
190 /*
191 * trust the certificate for this session only. If
192 * the page results in a redirect while we're asking
193 * the user to save, we'll end up with an invalid
194 * tabid (one request == one tab id) and crash. It
195 * also makes sense to save it for the current session
196 * if the user accepted it.
197 */
198 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
200 ui_yornp("Save the new certificate?",
201 handle_maybe_save_new_cert, tab);
202 } else {
203 free(tab->cert);
204 tab->cert = NULL;
208 static void
209 handle_maybe_save_new_cert(int accept, struct tab *tab)
211 struct tofu_entry *e;
212 const char *host, *port;
214 if (tab->proxy != NULL) {
215 host = tab->proxy->host;
216 port = tab->proxy->port;
217 } else {
218 host = tab->uri.host;
219 port = tab->uri.port;
222 if (!accept)
223 goto end;
225 if ((e = calloc(1, sizeof(*e))) == NULL)
226 die();
228 strlcpy(e->domain, host, sizeof(e->domain));
229 if (*port != '\0' && strcmp(port, "1965")) {
230 strlcat(e->domain, ":", sizeof(e->domain));
231 strlcat(e->domain, port, sizeof(e->domain));
233 strlcpy(e->hash, tab->cert, sizeof(e->hash));
234 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
236 tofu_update(&certs, e);
238 tab->trust = TS_TRUSTED;
240 end:
241 free(tab->cert);
242 tab->cert = NULL;
245 static inline int
246 normalize_code(int n)
248 if (n < 20) {
249 if (n == 10 || n == 11)
250 return n;
251 return 10;
252 } else if (n < 30) {
253 return 20;
254 } else if (n < 40) {
255 if (n == 30 || n == 31)
256 return n;
257 return 30;
258 } else if (n < 50) {
259 if (n <= 44)
260 return n;
261 return 40;
262 } else if (n < 60) {
263 if (n <= 53 || n == 59)
264 return n;
265 return 50;
266 } else if (n < 70) {
267 if (n <= 62)
268 return n;
269 return 60;
270 } else
271 return MALFORMED_RESPONSE;
274 static void
275 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
277 struct tab *tab;
279 tab = tab_by_id(imsg->hdr.peerid);
281 if (sizeof(tab->code) != datalen)
282 die();
284 memcpy(&tab->code, imsg->data, sizeof(tab->code));
285 tab->code = normalize_code(tab->code);
286 if (tab->code != 30 && tab->code != 31)
287 tab->redirect_count = 0;
290 static void
291 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
293 struct tab *tab;
295 tab = tab_by_id(imsg->hdr.peerid);
297 if (sizeof(tab->meta) <= datalen)
298 die();
300 memcpy(tab->meta, imsg->data, datalen);
302 if (tab->code < 10) { /* internal errors */
303 load_page_from_str(tab, err_pages[tab->code]);
304 } else if (tab->code < 20) { /* 1x */
305 load_page_from_str(tab, err_pages[tab->code]);
306 ui_require_input(tab, tab->code == 11);
307 } else if (tab->code == 20) {
308 if (setup_parser_for(tab)) {
309 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
310 } else {
311 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
312 ui_yornp("Can't display page, wanna save?",
313 handle_maybe_save_page, tab);
315 } else if (tab->code < 40) { /* 3x */
316 tab->redirect_count++;
318 /* TODO: make customizable? */
319 if (tab->redirect_count > 5) {
320 load_page_from_str(tab,
321 err_pages[TOO_MUCH_REDIRECTS]);
322 } else
323 do_load_url(tab, tab->meta);
324 } else { /* 4x, 5x & 6x */
325 load_page_from_str(tab, err_pages[tab->code]);
329 static void
330 handle_maybe_save_page(int dosave, struct tab *tab)
332 if (dosave)
333 ui_read("Save to path", handle_save_page_path, tab);
334 else
335 stop_tab(tab);
338 static void
339 handle_save_page_path(const char *path, struct tab *tab)
341 if (path == NULL) {
342 stop_tab(tab);
343 return;
346 tab->path = strdup(path);
348 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
351 static void
352 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
354 struct tab *tab;
355 char *page;
356 const char *e;
357 int l;
359 tab = tab_by_id(imsg->hdr.peerid);
361 if (imsg->fd == -1) {
362 stop_tab(tab);
364 e = imsg->data;
365 if (e[datalen-1] != '\0')
366 die();
367 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
368 tab->path, e);
369 if (l == -1)
370 die();
371 load_page_from_str(tab, page);
372 free(page);
373 } else {
374 tab->fd = imsg->fd;
375 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
379 static void
380 handle_imsg_buf(struct imsg *imsg, size_t datalen)
382 struct tab *tab;
383 int l;
384 char *page, buf[FMT_SCALED_STRSIZE] = {0};
386 tab = tab_by_id(imsg->hdr.peerid);
388 tab->bytes += datalen;
389 if (tab->fd == -1) {
390 if (!tab->buffer.page.parse(&tab->buffer.page,
391 imsg->data, datalen))
392 die();
393 } else {
394 write(tab->fd, imsg->data, datalen);
395 fmt_scaled(tab->bytes, buf);
396 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
397 tab->path,
398 buf);
399 if (l == -1)
400 die();
401 load_page_from_str(tab, page);
402 free(page);
405 ui_on_tab_refresh(tab);
408 static void
409 handle_imsg_eof(struct imsg *imsg, size_t datalen)
411 struct tab *tab;
412 int l;
413 char *page, buf[FMT_SCALED_STRSIZE] = {0};
415 tab = tab_by_id(imsg->hdr.peerid);
417 if (tab->fd == -1) {
418 if (!tab->buffer.page.free(&tab->buffer.page))
419 die();
420 } else {
421 fmt_scaled(tab->bytes, buf);
422 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
423 tab->path,
424 buf);
425 if (l == -1)
426 die();
427 load_page_from_str(tab, page);
428 free(page);
430 close(tab->fd);
431 tab->fd = -1;
432 free(tab->path);
433 tab->path = NULL;
436 ui_on_tab_refresh(tab);
437 ui_on_tab_loaded(tab);
440 static void
441 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
443 int res;
445 if (datalen != sizeof(res))
446 die();
448 memcpy(&res, imsg->data, sizeof(res));
449 if (res == 0)
450 message("Added to bookmarks!");
451 else
452 message("Failed to add to bookmarks: %s",
453 strerror(res));
456 static void
457 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
459 int res;
461 if (datalen != sizeof(res))
462 die();
463 memcpy(&res, imsg->data, datalen);
464 if (res != 0)
465 message("Failed to save the cert for: %s",
466 strerror(res));
469 static void
470 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
472 int res;
474 if (datalen != sizeof(res))
475 die();
476 memcpy(&res, imsg->data, datalen);
477 if (!res)
478 message("Failed to update the certificate");
481 static void
482 handle_dispatch_imsg(int fd, short ev, void *d)
484 struct imsgev *iev = d;
485 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
488 static void
489 load_page_from_str(struct tab *tab, const char *page)
491 erase_buffer(&tab->buffer);
492 gemtext_initparser(&tab->buffer.page);
493 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
494 die();
495 if (!tab->buffer.page.free(&tab->buffer.page))
496 die();
497 ui_on_tab_refresh(tab);
498 ui_on_tab_loaded(tab);
501 void
502 load_about_url(struct tab *tab, const char *url)
504 tab->trust = TS_VERIFIED;
506 gemtext_initparser(&tab->buffer.page);
508 ui_send_fs(IMSG_GET, tab->id,
509 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
512 void
513 load_gemini_url(struct tab *tab, const char *url)
515 struct get_req req;
517 stop_tab(tab);
518 tab->id = tab_new_id();
520 memset(&req, 0, sizeof(req));
521 strlcpy(req.host, tab->uri.host, sizeof(req.host));
522 strlcpy(req.port, tab->uri.port, sizeof(req.host));
524 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
525 strlcat(req.req, "\r\n", sizeof(req.req));
527 req.proto = PROTO_GEMINI;
529 ui_send_net(IMSG_GET_RAW, tab->id,
530 &req, sizeof(req));
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 ui_send_net(IMSG_GET_RAW, tab->id,
552 &req, sizeof(req));
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 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
650 if (tab->fd != -1) {
651 close(tab->fd);
652 tab->fd = -1;
653 free(tab->path);
654 tab->path = NULL;
655 load_page_from_str(tab, "Stopped.\n");
659 void
660 add_to_bookmarks(const char *str)
662 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
663 str, strlen(str)+1);
666 void
667 save_session(void)
669 struct tab *tab;
671 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
673 TAILQ_FOREACH(tab, &tabshead, tabs) {
674 ui_send_fs(IMSG_SESSION_TAB, 0,
675 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
678 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
681 static void
682 session_new_tab_cb(const char *url)
684 new_tab(url);
687 static pid_t
688 start_child(enum telescope_process p, const char *argv0, int fd)
690 const char *argv[4];
691 int argc = 0;
692 pid_t pid;
694 switch (pid = fork()) {
695 case -1:
696 die();
697 case 0:
698 break;
699 default:
700 close(fd);
701 return pid;
704 if (dup2(fd, 3) == -1)
705 err(1, "cannot setup imsg fd");
707 argv[argc++] = argv0;
708 switch (p) {
709 case PROC_UI:
710 errx(1, "Can't start ui process");
711 case PROC_FS:
712 argv[argc++] = "-Tf";
713 break;
714 case PROC_NET:
715 argv[argc++] = "-Tn";
716 break;
719 argv[argc++] = NULL;
720 execvp(argv0, (char *const *)argv);
721 err(1, "execvp(%s)", argv0);
724 static int
725 ui_send_net(int type, uint32_t peerid, const void *data,
726 uint16_t datalen)
728 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
729 datalen);
732 static int
733 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
735 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
736 datalen);
739 static void __attribute__((noreturn))
740 usage(int r)
742 fprintf(stderr, "USAGE: %s [-hnv] [-c config] [url]\n",
743 getprogname());
744 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
745 exit(r);
748 int
749 main(int argc, char * const *argv)
751 struct imsgev net_ibuf, fs_ibuf;
752 int pipe2net[2], pipe2fs[2];
753 int ch, configtest = 0, fail = 0;
754 int has_url = 0;
755 int proc = -1;
756 int sessionfd;
757 char path[PATH_MAX];
758 const char *url = NEW_TAB_URL;
759 const char *argv0;
761 argv0 = argv[0];
763 signal(SIGCHLD, SIG_IGN);
764 signal(SIGPIPE, SIG_IGN);
766 if (getenv("NO_COLOR") != NULL)
767 enable_colors = 0;
769 strlcpy(path, getenv("HOME"), sizeof(path));
770 strlcat(path, "/.telescope/config", sizeof(path));
772 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
773 switch (ch) {
774 case 'C':
775 exit(ui_print_colors());
776 case 'c':
777 fail = 1;
778 strlcpy(path, optarg, sizeof(path));
779 break;
780 case 'n':
781 configtest = 1;
782 break;
783 case 'h':
784 usage(0);
785 case 'T':
786 switch (*optarg) {
787 case 'f':
788 proc = PROC_FS;
789 break;
790 case 'n':
791 proc = PROC_NET;
792 break;
793 default:
794 errx(1, "invalid process spec %c",
795 *optarg);
797 break;
798 case 'v':
799 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
800 exit(0);
801 break;
802 default:
803 usage(1);
807 argc -= optind;
808 argv += optind;
810 if (proc != -1) {
811 if (argc > 0)
812 usage(1);
813 else if (proc == PROC_FS)
814 return fs_main();
815 else if (proc == PROC_NET)
816 return client_main();
817 else
818 usage(1);
821 if (argc != 0) {
822 has_url = 1;
823 url = argv[0];
826 /* setup keys before reading the config */
827 TAILQ_INIT(&global_map.m);
828 global_map.unhandled_input = global_key_unbound;
829 TAILQ_INIT(&minibuffer_map.m);
831 config_init();
832 parseconfig(path, fail);
833 if (configtest){
834 puts("config OK");
835 exit(0);
838 fs_init();
839 if ((sessionfd = lock_session()) == -1)
840 errx(1, "can't lock session, is another instance of "
841 "telescope already running?");
843 /* Start children. */
844 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
845 err(1, "socketpair");
846 start_child(PROC_FS, argv0, pipe2fs[1]);
847 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
848 iev_fs = &fs_ibuf;
849 iev_fs->handler = handle_dispatch_imsg;
851 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
852 err(1, "socketpair");
853 start_child(PROC_NET, argv0, pipe2net[1]);
854 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
855 iev_net = &net_ibuf;
856 iev_net->handler = handle_dispatch_imsg;
858 setproctitle("ui");
860 /* initialize tofu & load certificates */
861 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
862 load_certs(&certs);
864 event_init();
866 /* Setup event handlers for pipes to fs/net */
867 iev_fs->events = EV_READ;
868 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
869 iev_fs->handler, iev_fs);
870 event_add(&iev_fs->ev, NULL);
872 iev_net->events = EV_READ;
873 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
874 iev_net->handler, iev_net);
875 event_add(&iev_net->ev, NULL);
877 if (ui_init()) {
878 load_last_session(session_new_tab_cb);
879 if (has_url || TAILQ_EMPTY(&tabshead))
880 new_tab(url);
882 sandbox_ui_process();
883 event_dispatch();
884 ui_end();
887 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
888 ui_send_net(IMSG_QUIT, 0, NULL, 0);
889 imsg_flush(&iev_fs->ibuf);
890 imsg_flush(&iev_net->ibuf);
892 close(sessionfd);
894 return 0;