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 int do_load_url(struct tab*, const char*);
64 static void parse_session_line(char *, uint32_t *);
65 static void load_last_session(void);
66 static pid_t start_child(enum telescope_process, const char *, int);
67 static int ui_send_net(int, uint32_t, const void *, uint16_t);
68 static int ui_send_fs(int, uint32_t, const void *, uint16_t);
70 static imsg_handlerfn *handlers[] = {
71 [IMSG_ERR] = handle_imsg_err,
72 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
73 [IMSG_GOT_CODE] = handle_imsg_got_code,
74 [IMSG_GOT_META] = handle_imsg_got_meta,
75 [IMSG_BUF] = handle_imsg_buf,
76 [IMSG_EOF] = handle_imsg_eof,
77 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
78 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
79 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
80 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
81 };
83 static struct ohash certs;
85 static void __attribute__((__noreturn__))
86 die(void)
87 {
88 abort(); /* TODO */
89 }
91 static struct tab *
92 tab_by_id(uint32_t id)
93 {
94 struct tab *t;
96 TAILQ_FOREACH(t, &tabshead, tabs) {
97 if (t->id == id)
98 return t;
99 }
101 die();
104 static void
105 handle_imsg_err(struct imsg *imsg, size_t datalen)
107 struct tab *tab;
108 char *page;
110 tab = tab_by_id(imsg->hdr.peerid);
112 page = imsg->data;
113 page[datalen-1] = '\0';
115 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
116 tab->hist_cur->h, page) == -1)
117 die();
118 load_page_from_str(tab, page);
119 free(page);
122 static void
123 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
125 const char *hash, *host, *port;
126 int tofu_res;
127 struct tofu_entry *e;
128 struct tab *tab;
130 hash = imsg->data;
131 if (hash[datalen-1] != '\0')
132 abort();
134 tab = tab_by_id(imsg->hdr.peerid);
136 if (tab->proxy != NULL) {
137 host = tab->proxy->host;
138 port = tab->proxy->port;
139 } else {
140 host = tab->uri.host;
141 port = tab->uri.port;
144 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
145 /* TODO: an update in libressl/libretls changed
146 * significantly. Find a better approach at storing
147 * the certs! */
148 if (datalen > sizeof(e->hash))
149 abort();
151 tofu_res = 1; /* trust on first use */
152 if ((e = calloc(1, sizeof(*e))) == NULL)
153 abort();
154 strlcpy(e->domain, host, sizeof(e->domain));
155 if (*port != '\0' && strcmp(port, "1965")) {
156 strlcat(e->domain, ":", sizeof(e->domain));
157 strlcat(e->domain, port, sizeof(e->domain));
159 strlcpy(e->hash, hash, sizeof(e->hash));
160 tofu_add(&certs, e);
161 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
162 } else
163 tofu_res = !strcmp(hash, e->hash);
165 if (tofu_res) {
166 if (e->verified == -1)
167 tab->trust = TS_TEMP_TRUSTED;
168 else if (e->verified == 1)
169 tab->trust = TS_VERIFIED;
170 else
171 tab->trust = TS_TRUSTED;
173 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
174 &tofu_res, sizeof(tofu_res));
175 } else {
176 tab->trust = TS_UNTRUSTED;
177 load_page_from_str(tab, "# Certificate mismatch\n");
178 if ((tab->cert = strdup(hash)) == NULL)
179 die();
180 ui_yornp("Certificate mismatch. Proceed?",
181 handle_check_cert_user_choice, tab);
185 static void
186 handle_check_cert_user_choice(int accept, struct tab *tab)
188 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
189 sizeof(accept));
191 if (accept) {
192 /*
193 * trust the certificate for this session only. If
194 * the page results in a redirect while we're asking
195 * the user to save, we'll end up with an invalid
196 * tabid (one request == one tab id) and crash. It
197 * also makes sense to save it for the current session
198 * if the user accepted it.
199 */
200 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
202 ui_yornp("Save the new certificate?",
203 handle_maybe_save_new_cert, tab);
204 } else {
205 free(tab->cert);
206 tab->cert = NULL;
210 static void
211 handle_maybe_save_new_cert(int accept, struct tab *tab)
213 struct tofu_entry *e;
214 const char *host, *port;
216 if (tab->proxy != NULL) {
217 host = tab->proxy->host;
218 port = tab->proxy->port;
219 } else {
220 host = tab->uri.host;
221 port = tab->uri.port;
224 if (!accept)
225 goto end;
227 if ((e = calloc(1, sizeof(*e))) == NULL)
228 die();
230 strlcpy(e->domain, host, sizeof(e->domain));
231 if (*port != '\0' && strcmp(port, "1965")) {
232 strlcat(e->domain, ":", sizeof(e->domain));
233 strlcat(e->domain, port, sizeof(e->domain));
235 strlcpy(e->hash, tab->cert, sizeof(e->hash));
236 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
238 tofu_update(&certs, e);
240 tab->trust = TS_TRUSTED;
242 end:
243 free(tab->cert);
244 tab->cert = NULL;
247 static inline int
248 normalize_code(int n)
250 if (n < 20) {
251 if (n == 10 || n == 11)
252 return n;
253 return 10;
254 } else if (n < 30) {
255 return 20;
256 } else if (n < 40) {
257 if (n == 30 || n == 31)
258 return n;
259 return 30;
260 } else if (n < 50) {
261 if (n <= 44)
262 return n;
263 return 40;
264 } else if (n < 60) {
265 if (n <= 53 || n == 59)
266 return n;
267 return 50;
268 } else if (n < 70) {
269 if (n <= 62)
270 return n;
271 return 60;
272 } else
273 return MALFORMED_RESPONSE;
276 static void
277 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
279 struct tab *tab;
281 tab = tab_by_id(imsg->hdr.peerid);
283 if (sizeof(tab->code) != datalen)
284 die();
286 memcpy(&tab->code, imsg->data, sizeof(tab->code));
287 tab->code = normalize_code(tab->code);
288 if (tab->code != 30 && tab->code != 31)
289 tab->redirect_count = 0;
292 static void
293 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
295 struct tab *tab;
297 tab = tab_by_id(imsg->hdr.peerid);
299 if (sizeof(tab->meta) <= datalen)
300 die();
302 memcpy(tab->meta, imsg->data, datalen);
304 if (tab->code < 10) { /* internal errors */
305 load_page_from_str(tab, err_pages[tab->code]);
306 } else if (tab->code < 20) { /* 1x */
307 load_page_from_str(tab, err_pages[tab->code]);
308 ui_require_input(tab, tab->code == 11);
309 } else if (tab->code == 20) {
310 if (setup_parser_for(tab)) {
311 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
312 } else {
313 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
314 ui_yornp("Can't display page, wanna save?",
315 handle_maybe_save_page, tab);
317 } else if (tab->code < 40) { /* 3x */
318 tab->redirect_count++;
320 /* TODO: make customizable? */
321 if (tab->redirect_count > 5) {
322 load_page_from_str(tab,
323 err_pages[TOO_MUCH_REDIRECTS]);
324 } else
325 do_load_url(tab, tab->meta);
326 } else { /* 4x, 5x & 6x */
327 load_page_from_str(tab, err_pages[tab->code]);
331 static void
332 handle_maybe_save_page(int dosave, struct tab *tab)
334 if (dosave)
335 ui_read("Save to path", handle_save_page_path, tab);
336 else
337 stop_tab(tab);
340 static void
341 handle_save_page_path(const char *path, struct tab *tab)
343 if (path == NULL) {
344 stop_tab(tab);
345 return;
348 tab->path = strdup(path);
350 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
353 static void
354 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
356 struct tab *tab;
357 char *page;
358 const char *e;
359 int l;
361 tab = tab_by_id(imsg->hdr.peerid);
363 if (imsg->fd == -1) {
364 stop_tab(tab);
366 e = imsg->data;
367 if (e[datalen-1] != '\0')
368 die();
369 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
370 tab->path, e);
371 if (l == -1)
372 die();
373 load_page_from_str(tab, page);
374 free(page);
375 } else {
376 tab->fd = imsg->fd;
377 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
381 static void
382 handle_imsg_buf(struct imsg *imsg, size_t datalen)
384 struct tab *tab;
385 int l;
386 char *page, buf[FMT_SCALED_STRSIZE] = {0};
388 tab = tab_by_id(imsg->hdr.peerid);
390 tab->bytes += datalen;
391 if (tab->fd == -1) {
392 if (!tab->buffer.page.parse(&tab->buffer.page,
393 imsg->data, datalen))
394 die();
395 } else {
396 write(tab->fd, imsg->data, datalen);
397 fmt_scaled(tab->bytes, buf);
398 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
399 tab->path,
400 buf);
401 if (l == -1)
402 die();
403 load_page_from_str(tab, page);
404 free(page);
407 ui_on_tab_refresh(tab);
410 static void
411 handle_imsg_eof(struct imsg *imsg, size_t datalen)
413 struct tab *tab;
414 int l;
415 char *page, buf[FMT_SCALED_STRSIZE] = {0};
417 tab = tab_by_id(imsg->hdr.peerid);
419 if (tab->fd == -1) {
420 if (!tab->buffer.page.free(&tab->buffer.page))
421 die();
422 } else {
423 fmt_scaled(tab->bytes, buf);
424 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
425 tab->path,
426 buf);
427 if (l == -1)
428 die();
429 load_page_from_str(tab, page);
430 free(page);
432 close(tab->fd);
433 tab->fd = -1;
434 free(tab->path);
435 tab->path = NULL;
438 ui_on_tab_refresh(tab);
439 ui_on_tab_loaded(tab);
442 static void
443 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
445 int res;
447 if (datalen != sizeof(res))
448 die();
450 memcpy(&res, imsg->data, sizeof(res));
451 if (res == 0)
452 message("Added to bookmarks!");
453 else
454 message("Failed to add to bookmarks: %s",
455 strerror(res));
458 static void
459 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
461 int res;
463 if (datalen != sizeof(res))
464 die();
465 memcpy(&res, imsg->data, datalen);
466 if (res != 0)
467 message("Failed to save the cert for: %s",
468 strerror(res));
471 static void
472 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
474 int res;
476 if (datalen != sizeof(res))
477 die();
478 memcpy(&res, imsg->data, datalen);
479 if (!res)
480 message("Failed to update the certificate");
483 static void
484 handle_dispatch_imsg(int fd, short ev, void *d)
486 struct imsgev *iev = d;
487 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
490 static void
491 load_page_from_str(struct tab *tab, const char *page)
493 erase_buffer(&tab->buffer);
494 gemtext_initparser(&tab->buffer.page);
495 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
496 die();
497 if (!tab->buffer.page.free(&tab->buffer.page))
498 die();
499 ui_on_tab_refresh(tab);
500 ui_on_tab_loaded(tab);
503 void
504 load_about_url(struct tab *tab, const char *url)
506 tab->trust = TS_VERIFIED;
508 gemtext_initparser(&tab->buffer.page);
510 ui_send_fs(IMSG_GET, tab->id,
511 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
514 void
515 load_gemini_url(struct tab *tab, const char *url)
517 struct get_req req;
519 stop_tab(tab);
520 tab->id = tab_new_id();
522 memset(&req, 0, sizeof(req));
523 strlcpy(req.host, tab->uri.host, sizeof(req.host));
524 strlcpy(req.port, tab->uri.port, sizeof(req.host));
526 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
527 strlcat(req.req, "\r\n", sizeof(req.req));
529 req.proto = PROTO_GEMINI;
531 ui_send_net(IMSG_GET_RAW, tab->id,
532 &req, sizeof(req));
535 void
536 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
538 struct get_req req;
540 stop_tab(tab);
541 tab->id = tab_new_id();
542 tab->proxy = p;
544 memset(&req, 0, sizeof(req));
545 strlcpy(req.host, p->host, sizeof(req.host));
546 strlcpy(req.port, p->port, sizeof(req.host));
548 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
549 strlcat(req.req, "\r\n", sizeof(req.req));
551 req.proto = p->proto;
553 ui_send_net(IMSG_GET_RAW, tab->id,
554 &req, sizeof(req));
557 /*
558 * Effectively load the given url in the given tab. Return 1 when
559 * loading the page asynchronously, and thus when an erase_buffer can
560 * be done right after this function return, or 0 when loading the
561 * page synchronously. In this last case, erase_buffer *MUST* be
562 * called by the handling function (such as load_page_from_str).
563 */
564 static int
565 do_load_url(struct tab *tab, const char *url)
567 struct phos_uri uri;
568 struct proto *p;
569 struct proxy *proxy;
570 char *t;
572 tab->proxy = NULL;
574 if (tab->fd != -1) {
575 close(tab->fd);
576 tab->fd = -1;
577 free(tab->path);
578 tab->path = NULL;
581 tab->trust = TS_UNKNOWN;
583 memcpy(&uri, &tab->uri, sizeof(tab->uri));
584 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
585 if (asprintf(&t, "#error loading %s\n>%s\n",
586 url, "Can't parse the URI") == -1)
587 die();
588 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
589 load_page_from_str(tab, t);
590 free(t);
591 return 0;
594 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
595 sizeof(tab->hist_cur->h));
597 for (p = protos; p->schema != NULL; ++p) {
598 if (!strcmp(tab->uri.scheme, p->schema)) {
599 p->loadfn(tab, url);
600 return 1;
604 TAILQ_FOREACH(proxy, &proxies, proxies) {
605 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
606 load_via_proxy(tab, url, proxy);
607 return 1;
611 load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
612 return 0;
615 void
616 load_url(struct tab *tab, const char *url)
618 if (tab->hist_cur != NULL)
619 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
621 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
622 event_loopbreak();
623 return;
626 hist_push(&tab->hist, tab->hist_cur);
627 if (do_load_url(tab, url))
628 erase_buffer(&tab->buffer);
631 int
632 load_previous_page(struct tab *tab)
634 struct hist *h;
636 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
637 return 0;
638 tab->hist_cur = h;
639 do_load_url(tab, h->h);
640 return 1;
643 int
644 load_next_page(struct tab *tab)
646 struct hist *h;
648 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
649 return 0;
650 tab->hist_cur = h;
651 do_load_url(tab, h->h);
652 return 1;
655 void
656 stop_tab(struct tab *tab)
658 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
660 if (tab->fd != -1) {
661 close(tab->fd);
662 tab->fd = -1;
663 free(tab->path);
664 tab->path = NULL;
665 load_page_from_str(tab, "Stopped.\n");
669 void
670 add_to_bookmarks(const char *str)
672 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
673 str, strlen(str)+1);
676 void
677 save_session(void)
679 struct tab *tab;
680 int flags;
682 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
684 TAILQ_FOREACH(tab, &tabshead, tabs) {
685 flags = tab->flags;
686 if (tab == current_tab)
687 flags |= TAB_CURRENT;
688 ui_send_fs(IMSG_SESSION_TAB, flags,
689 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
692 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
695 /*
696 * Parse a line of the session file. The format is:
698 * URL [flags,...]\n
699 */
700 static void
701 parse_session_line(char *line, uint32_t *flags)
703 char *s, *ap;
705 *flags = 0;
706 if ((s = strchr(line, ' ')) == NULL)
707 return;
709 *s++ = '\0';
710 while ((ap = strsep(&s, ",")) != NULL) {
711 if (*ap == '\0')
713 else if (!strcmp(ap, "current"))
714 *flags |= TAB_CURRENT;
715 else
716 message("unknown tab flag: %s", ap);
720 static void
721 load_last_session(void)
723 char *nl, *line = NULL;
724 uint32_t flags;
725 size_t linesize = 0;
726 ssize_t linelen;
727 FILE *session;
728 struct tab *tab, *curr;
730 if ((session = fopen(session_file, "r")) == NULL) {
731 /* first time? */
732 current_tab = new_tab("about:help");
733 return;
736 while ((linelen = getline(&line, &linesize, session)) != -1) {
737 if ((nl = strchr(line, '\n')) != NULL)
738 *nl = '\0';
739 parse_session_line(line, &flags);
740 if ((tab = new_tab(line)) == NULL)
741 err(1, "new_tab");
742 if (flags & TAB_CURRENT)
743 curr = tab;
746 if (ferror(session))
747 message("error reading %s: %s",
748 session_file, strerror(errno));
749 fclose(session);
750 free(line);
752 if (curr != NULL)
753 switch_to_tab(curr);
755 return;
758 static pid_t
759 start_child(enum telescope_process p, const char *argv0, int fd)
761 const char *argv[4];
762 int argc = 0;
763 pid_t pid;
765 switch (pid = fork()) {
766 case -1:
767 die();
768 case 0:
769 break;
770 default:
771 close(fd);
772 return pid;
775 if (dup2(fd, 3) == -1)
776 err(1, "cannot setup imsg fd");
778 argv[argc++] = argv0;
779 switch (p) {
780 case PROC_UI:
781 errx(1, "Can't start ui process");
782 case PROC_FS:
783 argv[argc++] = "-Tf";
784 break;
785 case PROC_NET:
786 argv[argc++] = "-Tn";
787 break;
790 argv[argc++] = NULL;
791 execvp(argv0, (char *const *)argv);
792 err(1, "execvp(%s)", argv0);
795 static int
796 ui_send_net(int type, uint32_t peerid, const void *data,
797 uint16_t datalen)
799 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
800 datalen);
803 static int
804 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
806 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
807 datalen);
810 static void __attribute__((noreturn))
811 usage(int r)
813 fprintf(stderr, "USAGE: %s [-hnv] [-c config] [url]\n",
814 getprogname());
815 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
816 exit(r);
819 int
820 main(int argc, char * const *argv)
822 struct imsgev net_ibuf, fs_ibuf;
823 int pipe2net[2], pipe2fs[2];
824 int ch, configtest = 0, fail = 0;
825 int has_url = 0;
826 int proc = -1;
827 int sessionfd;
828 char path[PATH_MAX];
829 const char *url = NEW_TAB_URL;
830 const char *argv0;
832 argv0 = argv[0];
834 signal(SIGCHLD, SIG_IGN);
835 signal(SIGPIPE, SIG_IGN);
837 if (getenv("NO_COLOR") != NULL)
838 enable_colors = 0;
840 strlcpy(path, getenv("HOME"), sizeof(path));
841 strlcat(path, "/.telescope/config", sizeof(path));
843 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
844 switch (ch) {
845 case 'C':
846 exit(ui_print_colors());
847 case 'c':
848 fail = 1;
849 strlcpy(path, optarg, sizeof(path));
850 break;
851 case 'n':
852 configtest = 1;
853 break;
854 case 'h':
855 usage(0);
856 case 'T':
857 switch (*optarg) {
858 case 'f':
859 proc = PROC_FS;
860 break;
861 case 'n':
862 proc = PROC_NET;
863 break;
864 default:
865 errx(1, "invalid process spec %c",
866 *optarg);
868 break;
869 case 'v':
870 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
871 exit(0);
872 break;
873 default:
874 usage(1);
878 argc -= optind;
879 argv += optind;
881 if (proc != -1) {
882 if (argc > 0)
883 usage(1);
884 else if (proc == PROC_FS)
885 return fs_main();
886 else if (proc == PROC_NET)
887 return client_main();
888 else
889 usage(1);
892 if (argc != 0) {
893 has_url = 1;
894 url = argv[0];
897 /* setup keys before reading the config */
898 TAILQ_INIT(&global_map.m);
899 global_map.unhandled_input = global_key_unbound;
900 TAILQ_INIT(&minibuffer_map.m);
902 config_init();
903 parseconfig(path, fail);
904 if (configtest){
905 puts("config OK");
906 exit(0);
909 fs_init();
910 if ((sessionfd = lock_session()) == -1)
911 errx(1, "can't lock session, is another instance of "
912 "telescope already running?");
914 /* Start children. */
915 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
916 err(1, "socketpair");
917 start_child(PROC_FS, argv0, pipe2fs[1]);
918 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
919 iev_fs = &fs_ibuf;
920 iev_fs->handler = handle_dispatch_imsg;
922 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
923 err(1, "socketpair");
924 start_child(PROC_NET, argv0, pipe2net[1]);
925 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
926 iev_net = &net_ibuf;
927 iev_net->handler = handle_dispatch_imsg;
929 setproctitle("ui");
931 /* initialize tofu & load certificates */
932 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
933 load_certs(&certs);
935 event_init();
937 /* Setup event handlers for pipes to fs/net */
938 iev_fs->events = EV_READ;
939 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
940 iev_fs->handler, iev_fs);
941 event_add(&iev_fs->ev, NULL);
943 iev_net->events = EV_READ;
944 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
945 iev_net->handler, iev_net);
946 event_add(&iev_net->ev, NULL);
948 if (ui_init()) {
949 load_last_session();
950 if (has_url || TAILQ_EMPTY(&tabshead))
951 new_tab(url);
953 sandbox_ui_process();
954 ui_refresh();
955 event_dispatch();
956 ui_end();
959 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
960 ui_send_net(IMSG_QUIT, 0, NULL, 0);
961 imsg_flush(&iev_fs->ibuf);
962 imsg_flush(&iev_net->ibuf);
964 close(sessionfd);
966 return 0;