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 /*
616 * Load url in tab. If tab is marked as lazy, only prepare the url
617 * but don't load it. If tab is lazy and a url was already prepared,
618 * do load it!
619 */
620 void
621 load_url(struct tab *tab, const char *url)
623 int lazy;
625 lazy = tab->flags & TAB_LAZY;
627 if (lazy && tab->hist_cur != NULL) {
628 lazy = 0;
629 tab->flags &= ~TAB_LAZY;
632 if (!lazy || tab->hist_cur == NULL) {
633 if (tab->hist_cur != NULL)
634 hist_clear_forward(&tab->hist,
635 TAILQ_NEXT(tab->hist_cur, entries));
637 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
638 event_loopbreak();
639 return;
642 strlcpy(tab->buffer.page.title, url,
643 sizeof(tab->buffer.page.title));
644 hist_push(&tab->hist, tab->hist_cur);
646 if (lazy)
647 strlcpy(tab->hist_cur->h, url,
648 sizeof(tab->hist_cur->h));
651 if (!lazy && do_load_url(tab, url))
652 erase_buffer(&tab->buffer);
655 int
656 load_previous_page(struct tab *tab)
658 struct hist *h;
660 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
661 return 0;
662 tab->hist_cur = h;
663 do_load_url(tab, h->h);
664 return 1;
667 int
668 load_next_page(struct tab *tab)
670 struct hist *h;
672 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
673 return 0;
674 tab->hist_cur = h;
675 do_load_url(tab, h->h);
676 return 1;
679 void
680 stop_tab(struct tab *tab)
682 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
684 if (tab->fd != -1) {
685 close(tab->fd);
686 tab->fd = -1;
687 free(tab->path);
688 tab->path = NULL;
689 load_page_from_str(tab, "Stopped.\n");
693 void
694 add_to_bookmarks(const char *str)
696 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
697 str, strlen(str)+1);
700 void
701 save_session(void)
703 struct tab *tab;
704 int flags;
706 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
708 TAILQ_FOREACH(tab, &tabshead, tabs) {
709 flags = tab->flags;
710 if (tab == current_tab)
711 flags |= TAB_CURRENT;
712 ui_send_fs(IMSG_SESSION_TAB, flags,
713 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
716 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
719 /*
720 * Parse a line of the session file. The format is:
722 * URL [flags,...]\n
723 */
724 static void
725 parse_session_line(char *line, uint32_t *flags)
727 char *s, *ap;
729 *flags = 0;
730 if ((s = strchr(line, ' ')) == NULL)
731 return;
733 *s++ = '\0';
734 while ((ap = strsep(&s, ",")) != NULL) {
735 if (*ap == '\0')
737 else if (!strcmp(ap, "current"))
738 *flags |= TAB_CURRENT;
739 else
740 message("unknown tab flag: %s", ap);
744 static void
745 load_last_session(void)
747 char *nl, *line = NULL;
748 uint32_t flags;
749 size_t linesize = 0;
750 ssize_t linelen;
751 FILE *session;
752 struct tab *tab, *curr;
754 if ((session = fopen(session_file, "r")) == NULL) {
755 /* first time? */
756 current_tab = new_tab("about:help");
757 return;
760 while ((linelen = getline(&line, &linesize, session)) != -1) {
761 if ((nl = strchr(line, '\n')) != NULL)
762 *nl = '\0';
763 parse_session_line(line, &flags);
764 if ((tab = new_tab(line)) == NULL)
765 err(1, "new_tab");
766 if (flags & TAB_CURRENT)
767 curr = tab;
770 if (ferror(session))
771 message("error reading %s: %s",
772 session_file, strerror(errno));
773 fclose(session);
774 free(line);
776 if (curr != NULL)
777 switch_to_tab(curr);
779 return;
782 static pid_t
783 start_child(enum telescope_process p, const char *argv0, int fd)
785 const char *argv[4];
786 int argc = 0;
787 pid_t pid;
789 switch (pid = fork()) {
790 case -1:
791 die();
792 case 0:
793 break;
794 default:
795 close(fd);
796 return pid;
799 if (dup2(fd, 3) == -1)
800 err(1, "cannot setup imsg fd");
802 argv[argc++] = argv0;
803 switch (p) {
804 case PROC_UI:
805 errx(1, "Can't start ui process");
806 case PROC_FS:
807 argv[argc++] = "-Tf";
808 break;
809 case PROC_NET:
810 argv[argc++] = "-Tn";
811 break;
814 argv[argc++] = NULL;
815 execvp(argv0, (char *const *)argv);
816 err(1, "execvp(%s)", argv0);
819 static int
820 ui_send_net(int type, uint32_t peerid, const void *data,
821 uint16_t datalen)
823 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
824 datalen);
827 static int
828 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
830 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
831 datalen);
834 static void __attribute__((noreturn))
835 usage(int r)
837 fprintf(stderr, "USAGE: %s [-hnv] [-c config] [url]\n",
838 getprogname());
839 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
840 exit(r);
843 int
844 main(int argc, char * const *argv)
846 struct imsgev net_ibuf, fs_ibuf;
847 int pipe2net[2], pipe2fs[2];
848 int ch, configtest = 0, fail = 0;
849 int has_url = 0;
850 int proc = -1;
851 int sessionfd;
852 char path[PATH_MAX];
853 const char *url = NEW_TAB_URL;
854 const char *argv0;
856 argv0 = argv[0];
858 signal(SIGCHLD, SIG_IGN);
859 signal(SIGPIPE, SIG_IGN);
861 if (getenv("NO_COLOR") != NULL)
862 enable_colors = 0;
864 strlcpy(path, getenv("HOME"), sizeof(path));
865 strlcat(path, "/.telescope/config", sizeof(path));
867 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
868 switch (ch) {
869 case 'C':
870 exit(ui_print_colors());
871 case 'c':
872 fail = 1;
873 strlcpy(path, optarg, sizeof(path));
874 break;
875 case 'n':
876 configtest = 1;
877 break;
878 case 'h':
879 usage(0);
880 case 'T':
881 switch (*optarg) {
882 case 'f':
883 proc = PROC_FS;
884 break;
885 case 'n':
886 proc = PROC_NET;
887 break;
888 default:
889 errx(1, "invalid process spec %c",
890 *optarg);
892 break;
893 case 'v':
894 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
895 exit(0);
896 break;
897 default:
898 usage(1);
902 argc -= optind;
903 argv += optind;
905 if (proc != -1) {
906 if (argc > 0)
907 usage(1);
908 else if (proc == PROC_FS)
909 return fs_main();
910 else if (proc == PROC_NET)
911 return client_main();
912 else
913 usage(1);
916 if (argc != 0) {
917 has_url = 1;
918 url = argv[0];
921 /* setup keys before reading the config */
922 TAILQ_INIT(&global_map.m);
923 global_map.unhandled_input = global_key_unbound;
924 TAILQ_INIT(&minibuffer_map.m);
926 config_init();
927 parseconfig(path, fail);
928 if (configtest){
929 puts("config OK");
930 exit(0);
933 fs_init();
934 if ((sessionfd = lock_session()) == -1)
935 errx(1, "can't lock session, is another instance of "
936 "telescope already running?");
938 /* Start children. */
939 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
940 err(1, "socketpair");
941 start_child(PROC_FS, argv0, pipe2fs[1]);
942 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
943 iev_fs = &fs_ibuf;
944 iev_fs->handler = handle_dispatch_imsg;
946 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
947 err(1, "socketpair");
948 start_child(PROC_NET, argv0, pipe2net[1]);
949 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
950 iev_net = &net_ibuf;
951 iev_net->handler = handle_dispatch_imsg;
953 setproctitle("ui");
955 /* initialize tofu & load certificates */
956 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
957 load_certs(&certs);
959 event_init();
961 /* Setup event handlers for pipes to fs/net */
962 iev_fs->events = EV_READ;
963 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
964 iev_fs->handler, iev_fs);
965 event_add(&iev_fs->ev, NULL);
967 iev_net->events = EV_READ;
968 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
969 iev_net->handler, iev_net);
970 event_add(&iev_net->ev, NULL);
972 if (ui_init()) {
973 load_last_session();
974 if (has_url || TAILQ_EMPTY(&tabshead))
975 new_tab(url);
977 sandbox_ui_process();
978 ui_main_loop();
979 ui_end();
982 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
983 ui_send_net(IMSG_QUIT, 0, NULL, 0);
984 imsg_flush(&iev_fs->ibuf);
985 imsg_flush(&iev_net->ibuf);
987 close(sessionfd);
989 return 0;