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 "defaults.h"
12 #include "pages.h"
13 #include "parser.h"
14 #include "telescope.h"
15 #include "ui.h"
17 static struct imsgev *iev_fs, *iev_net;
19 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
20 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
22 enum telescope_process {
23 PROC_UI,
24 PROC_FS,
25 PROC_NET,
26 };
28 /* the first is also the fallback one */
29 static struct proto protos[] = {
30 { "gemini", load_gemini_url },
31 { "about", load_about_url },
32 { NULL, NULL },
33 };
35 static void die(void) __attribute__((__noreturn__));
36 static struct tab *tab_by_id(uint32_t);
37 static void handle_imsg_err(struct imsg*, size_t);
38 static void handle_imsg_check_cert(struct imsg*, size_t);
39 static void handle_check_cert_user_choice(int, struct tab *);
40 static void handle_maybe_save_new_cert(int, struct tab *);
41 static void handle_imsg_got_code(struct imsg*, size_t);
42 static void handle_imsg_got_meta(struct imsg*, size_t);
43 static void handle_maybe_save_page(int, struct tab *);
44 static void handle_save_page_path(const char *, struct tab *);
45 static void handle_imsg_file_opened(struct imsg*, size_t);
46 static void handle_imsg_buf(struct imsg*, size_t);
47 static void handle_imsg_eof(struct imsg*, size_t);
48 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
49 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
50 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
51 static void handle_dispatch_imsg(int, short, void*);
52 static void load_page_from_str(struct tab*, const char*);
53 static void do_load_url(struct tab*, const char*);
54 static pid_t start_child(enum telescope_process, const char *, int);
55 static int ui_send_net(int, uint32_t, const void *, uint16_t);
56 static int ui_send_fs(int, uint32_t, const void *, uint16_t);
58 static imsg_handlerfn *handlers[] = {
59 [IMSG_ERR] = handle_imsg_err,
60 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
61 [IMSG_GOT_CODE] = handle_imsg_got_code,
62 [IMSG_GOT_META] = handle_imsg_got_meta,
63 [IMSG_BUF] = handle_imsg_buf,
64 [IMSG_EOF] = handle_imsg_eof,
65 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
66 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
67 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
68 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
69 };
71 static struct ohash certs;
73 static void __attribute__((__noreturn__))
74 die(void)
75 {
76 abort(); /* TODO */
77 }
79 static struct tab *
80 tab_by_id(uint32_t id)
81 {
82 struct tab *t;
84 TAILQ_FOREACH(t, &tabshead, tabs) {
85 if (t->id == id)
86 return t;
87 }
89 die();
90 }
92 static void
93 handle_imsg_err(struct imsg *imsg, size_t datalen)
94 {
95 struct tab *tab;
96 char *page;
98 tab = tab_by_id(imsg->hdr.peerid);
100 page = imsg->data;
101 page[datalen-1] = '\0';
103 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
104 tab->hist_cur->h, page) == -1)
105 die();
106 load_page_from_str(tab, page);
107 free(page);
110 static void
111 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
113 const char *hash, *host, *port;
114 int tofu_res;
115 struct tofu_entry *e;
116 struct tab *tab;
118 hash = imsg->data;
119 if (hash[datalen-1] != '\0')
120 abort();
122 tab = tab_by_id(imsg->hdr.peerid);
124 if (tab->proxy != NULL) {
125 host = tab->proxy->host;
126 port = tab->proxy->port;
127 } else {
128 host = tab->uri.host;
129 port = tab->uri.port;
132 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
133 /* TODO: an update in libressl/libretls changed
134 * significantly. Find a better approach at storing
135 * the certs! */
136 if (datalen > sizeof(e->hash))
137 abort();
139 tofu_res = 1; /* trust on first use */
140 if ((e = calloc(1, sizeof(*e))) == NULL)
141 abort();
142 strlcpy(e->domain, host, sizeof(e->domain));
143 if (*port != '\0' && strcmp(port, "1965")) {
144 strlcat(e->domain, ":", sizeof(e->domain));
145 strlcat(e->domain, port, sizeof(e->domain));
147 strlcpy(e->hash, hash, sizeof(e->hash));
148 tofu_add(&certs, e);
149 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
150 } else
151 tofu_res = !strcmp(hash, e->hash);
153 if (tofu_res) {
154 if (e->verified == -1)
155 tab->trust = TS_TEMP_TRUSTED;
156 else if (e->verified == 1)
157 tab->trust = TS_VERIFIED;
158 else
159 tab->trust = TS_TRUSTED;
161 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
162 &tofu_res, sizeof(tofu_res));
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 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
177 sizeof(accept));
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 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
226 tofu_update(&certs, e);
228 tab->trust = TS_TRUSTED;
230 end:
231 free(tab->cert);
232 tab->cert = NULL;
235 static inline int
236 normalize_code(int n)
238 if (n < 20) {
239 if (n == 10 || n == 11)
240 return n;
241 return 10;
242 } else if (n < 30) {
243 return 20;
244 } else if (n < 40) {
245 if (n == 30 || n == 31)
246 return n;
247 return 30;
248 } else if (n < 50) {
249 if (n <= 44)
250 return n;
251 return 40;
252 } else if (n < 60) {
253 if (n <= 53 || n == 59)
254 return n;
255 return 50;
256 } else if (n < 70) {
257 if (n <= 62)
258 return n;
259 return 60;
260 } else
261 return MALFORMED_RESPONSE;
264 static void
265 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
267 struct tab *tab;
269 tab = tab_by_id(imsg->hdr.peerid);
271 if (sizeof(tab->code) != datalen)
272 die();
274 memcpy(&tab->code, imsg->data, sizeof(tab->code));
275 tab->code = normalize_code(tab->code);
276 if (tab->code != 30 && tab->code != 31)
277 tab->redirect_count = 0;
280 static void
281 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
283 struct tab *tab;
285 tab = tab_by_id(imsg->hdr.peerid);
287 if (sizeof(tab->meta) <= datalen)
288 die();
290 memcpy(tab->meta, imsg->data, datalen);
292 if (tab->code < 10) { /* internal errors */
293 load_page_from_str(tab, err_pages[tab->code]);
294 } else if (tab->code < 20) { /* 1x */
295 load_page_from_str(tab, err_pages[tab->code]);
296 ui_require_input(tab, tab->code == 11);
297 } else if (tab->code == 20) {
298 if (setup_parser_for(tab)) {
299 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
300 } else {
301 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
302 ui_yornp("Can't display page, wanna save?",
303 handle_maybe_save_page, tab);
305 } else if (tab->code < 40) { /* 3x */
306 tab->redirect_count++;
308 /* TODO: make customizable? */
309 if (tab->redirect_count > 5) {
310 load_page_from_str(tab,
311 err_pages[TOO_MUCH_REDIRECTS]);
312 } else
313 do_load_url(tab, tab->meta);
314 } else { /* 4x, 5x & 6x */
315 load_page_from_str(tab, err_pages[tab->code]);
319 static void
320 handle_maybe_save_page(int dosave, struct tab *tab)
322 if (dosave)
323 ui_read("Save to path", handle_save_page_path, tab);
324 else
325 stop_tab(tab);
328 static void
329 handle_save_page_path(const char *path, struct tab *tab)
331 if (path == NULL) {
332 stop_tab(tab);
333 return;
336 tab->path = strdup(path);
338 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
341 static void
342 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
344 struct tab *tab;
345 char *page;
346 const char *e;
347 int l;
349 tab = tab_by_id(imsg->hdr.peerid);
351 if (imsg->fd == -1) {
352 stop_tab(tab);
354 e = imsg->data;
355 if (e[datalen-1] != '\0')
356 die();
357 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
358 tab->path, e);
359 if (l == -1)
360 die();
361 load_page_from_str(tab, page);
362 free(page);
363 } else {
364 tab->fd = imsg->fd;
365 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
369 static void
370 handle_imsg_buf(struct imsg *imsg, size_t datalen)
372 struct tab *tab;
373 int l;
374 char *page, buf[FMT_SCALED_STRSIZE] = {0};
376 tab = tab_by_id(imsg->hdr.peerid);
378 tab->bytes += datalen;
379 if (tab->fd == -1) {
380 if (!tab->buffer.page.parse(&tab->buffer.page,
381 imsg->data, datalen))
382 die();
383 } else {
384 write(tab->fd, imsg->data, datalen);
385 fmt_scaled(tab->bytes, buf);
386 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
387 tab->path,
388 buf);
389 if (l == -1)
390 die();
391 load_page_from_str(tab, page);
392 free(page);
395 ui_on_tab_refresh(tab);
398 static void
399 handle_imsg_eof(struct imsg *imsg, size_t datalen)
401 struct tab *tab;
402 int l;
403 char *page, buf[FMT_SCALED_STRSIZE] = {0};
405 tab = tab_by_id(imsg->hdr.peerid);
407 if (tab->fd == -1) {
408 if (!tab->buffer.page.free(&tab->buffer.page))
409 die();
410 } else {
411 fmt_scaled(tab->bytes, buf);
412 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
413 tab->path,
414 buf);
415 if (l == -1)
416 die();
417 load_page_from_str(tab, page);
418 free(page);
420 close(tab->fd);
421 tab->fd = -1;
422 free(tab->path);
423 tab->path = NULL;
426 ui_on_tab_refresh(tab);
427 ui_on_tab_loaded(tab);
430 static void
431 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
433 int res;
435 if (datalen != sizeof(res))
436 die();
438 memcpy(&res, imsg->data, sizeof(res));
439 if (res == 0)
440 message("Added to bookmarks!");
441 else
442 message("Failed to add to bookmarks: %s",
443 strerror(res));
446 static void
447 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
449 int res;
451 if (datalen != sizeof(res))
452 die();
453 memcpy(&res, imsg->data, datalen);
454 if (res != 0)
455 message("Failed to save the cert for: %s",
456 strerror(res));
459 static void
460 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
462 int res;
464 if (datalen != sizeof(res))
465 die();
466 memcpy(&res, imsg->data, datalen);
467 if (!res)
468 message("Failed to update the certificate");
471 static void
472 handle_dispatch_imsg(int fd, short ev, void *d)
474 struct imsgev *iev = d;
475 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
478 static void
479 load_page_from_str(struct tab *tab, const char *page)
481 erase_buffer(&tab->buffer);
482 gemtext_initparser(&tab->buffer.page);
483 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
484 die();
485 if (!tab->buffer.page.free(&tab->buffer.page))
486 die();
487 ui_on_tab_refresh(tab);
488 ui_on_tab_loaded(tab);
491 void
492 load_about_url(struct tab *tab, const char *url)
494 tab->trust = TS_VERIFIED;
496 gemtext_initparser(&tab->buffer.page);
498 ui_send_fs(IMSG_GET, tab->id,
499 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
502 void
503 load_gemini_url(struct tab *tab, const char *url)
505 struct get_req req;
507 stop_tab(tab);
508 tab->id = tab_new_id();
510 memset(&req, 0, sizeof(req));
511 strlcpy(req.host, tab->uri.host, sizeof(req.host));
512 strlcpy(req.port, tab->uri.port, sizeof(req.host));
514 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
515 strlcat(req.req, "\r\n", sizeof(req.req));
517 req.proto = PROTO_GEMINI;
519 ui_send_net(IMSG_GET_RAW, tab->id,
520 &req, sizeof(req));
523 void
524 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
526 struct get_req req;
528 stop_tab(tab);
529 tab->id = tab_new_id();
530 tab->proxy = p;
532 memset(&req, 0, sizeof(req));
533 strlcpy(req.host, p->host, sizeof(req.host));
534 strlcpy(req.port, p->port, sizeof(req.host));
536 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
537 strlcat(req.req, "\r\n", sizeof(req.req));
539 req.proto = p->proto;
541 ui_send_net(IMSG_GET_RAW, tab->id,
542 &req, sizeof(req));
545 static void
546 do_load_url(struct tab *tab, const char *url)
548 struct phos_uri uri;
549 struct proto *p;
550 struct proxy *proxy;
551 char *t;
553 tab->proxy = NULL;
555 if (tab->fd != -1) {
556 close(tab->fd);
557 tab->fd = -1;
558 free(tab->path);
559 tab->path = NULL;
562 tab->trust = TS_UNKNOWN;
564 memcpy(&uri, &tab->uri, sizeof(tab->uri));
565 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
566 if (asprintf(&t, "#error loading %s\n>%s\n",
567 url, "Can't parse the URI") == -1)
568 die();
569 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
570 load_page_from_str(tab, t);
571 free(t);
572 return;
575 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
576 sizeof(tab->hist_cur->h));
578 for (p = protos; p->schema != NULL; ++p) {
579 if (!strcmp(tab->uri.scheme, p->schema)) {
580 p->loadfn(tab, url);
581 return;
585 TAILQ_FOREACH(proxy, &proxies, proxies) {
586 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
587 load_via_proxy(tab, url, proxy);
588 return;
592 protos[0].loadfn(tab, url);
595 void
596 load_url(struct tab *tab, const char *url)
598 if (tab->hist_cur != NULL)
599 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
601 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
602 event_loopbreak();
603 return;
606 hist_push(&tab->hist, tab->hist_cur);
607 do_load_url(tab, url);
608 erase_buffer(&tab->buffer);
611 int
612 load_previous_page(struct tab *tab)
614 struct hist *h;
616 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
617 return 0;
618 tab->hist_cur = h;
619 do_load_url(tab, h->h);
620 return 1;
623 int
624 load_next_page(struct tab *tab)
626 struct hist *h;
628 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
629 return 0;
630 tab->hist_cur = h;
631 do_load_url(tab, h->h);
632 return 1;
635 void
636 stop_tab(struct tab *tab)
638 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
640 if (tab->fd != -1) {
641 close(tab->fd);
642 tab->fd = -1;
643 free(tab->path);
644 tab->path = NULL;
645 load_page_from_str(tab, "Stopped.\n");
649 void
650 add_to_bookmarks(const char *str)
652 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
653 str, strlen(str)+1);
656 void
657 save_session(void)
659 struct tab *tab;
661 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
663 TAILQ_FOREACH(tab, &tabshead, tabs) {
664 ui_send_fs(IMSG_SESSION_TAB, 0,
665 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
668 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
671 static void
672 session_new_tab_cb(const char *url)
674 new_tab(url);
677 static pid_t
678 start_child(enum telescope_process p, const char *argv0, int fd)
680 const char *argv[4];
681 int argc = 0;
682 pid_t pid;
684 switch (pid = fork()) {
685 case -1:
686 die();
687 case 0:
688 break;
689 default:
690 close(fd);
691 return pid;
694 if (dup2(fd, 3) == -1)
695 err(1, "cannot setup imsg fd");
697 argv[argc++] = argv0;
698 switch (p) {
699 case PROC_UI:
700 errx(1, "Can't start ui process");
701 case PROC_FS:
702 argv[argc++] = "-Tf";
703 break;
704 case PROC_NET:
705 argv[argc++] = "-Tn";
706 break;
709 argv[argc++] = NULL;
710 execvp(argv0, (char *const *)argv);
711 err(1, "execvp(%s)", argv0);
714 static int
715 ui_send_net(int type, uint32_t peerid, const void *data,
716 uint16_t datalen)
718 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
719 datalen);
722 static int
723 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
725 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
726 datalen);
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 imsgev net_ibuf, fs_ibuf;
741 int pipe2net[2], pipe2fs[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, pipe2fs) == -1)
822 err(1, "socketpair");
823 start_child(PROC_FS, argv0, pipe2fs[1]);
824 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
825 iev_fs = &fs_ibuf;
826 iev_fs->handler = handle_dispatch_imsg;
828 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
829 err(1, "socketpair");
830 start_child(PROC_NET, argv0, pipe2net[1]);
831 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
832 iev_net = &net_ibuf;
833 iev_net->handler = handle_dispatch_imsg;
835 setproctitle("ui");
837 /* initialize tofu & load certificates */
838 fs_init();
839 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
840 load_certs(&certs);
842 event_init();
844 /* Setup event handlers for pipes to fs/net */
845 iev_fs->events = EV_READ;
846 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
847 iev_fs->handler, iev_fs);
848 event_add(&iev_fs->ev, NULL);
850 iev_net->events = EV_READ;
851 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
852 iev_net->handler, iev_net);
853 event_add(&iev_net->ev, NULL);
855 if (ui_init()) {
856 load_last_session(session_new_tab_cb);
857 if (has_url || TAILQ_EMPTY(&tabshead))
858 new_tab(url);
860 sandbox_ui_process();
861 event_dispatch();
862 ui_end();
865 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
866 ui_send_net(IMSG_QUIT, 0, NULL, 0);
867 imsg_flush(&iev_fs->ibuf);
869 return 0;