2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/socket.h>
31 #include "minibuffer.h"
34 #include "telescope.h"
37 static struct option longopts[] = {
38 {"colors", no_argument, NULL, 'c'},
39 {"help", no_argument, NULL, 'h'},
40 {"version", no_argument, NULL, 'v'},
44 static const char *opts = "Cc:hnT:v";
47 * Used to know when we're finished loading.
51 static struct imsgev *iev_fs, *iev_net;
53 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
54 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
56 enum telescope_process {
62 #define CANNOT_FETCH 0
63 #define TOO_MUCH_REDIRECTS 1
64 #define MALFORMED_RESPONSE 2
65 #define UNKNOWN_TYPE_OR_CSET 3
66 #define UNKNOWN_PROTOCOL 4
68 /* XXX: keep in sync with normalize_code */
69 const char *err_pages[70] = {
70 [CANNOT_FETCH] = "# Couldn't load the page\n",
71 [TOO_MUCH_REDIRECTS] = "# Too much redirects\n",
72 [MALFORMED_RESPONSE] = "# Got a malformed response\n",
73 [UNKNOWN_TYPE_OR_CSET] = "# Unsupported type or charset\n",
74 [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
76 [10] = "# Input required\n",
77 [11] = "# Input required\n",
78 [40] = "# Temporary failure\n",
79 [41] = "# Server unavailable\n",
80 [42] = "# CGI error\n",
81 [43] = "# Proxy error\n",
82 [44] = "# Slow down\n",
83 [50] = "# Permanent failure\n",
84 [51] = "# Not found\n",
86 [53] = "# Proxy request refused\n",
87 [59] = "# Bad request\n",
88 [60] = "# Client certificate required\n",
89 [61] = "# Certificate not authorised\n",
90 [62] = "# Certificate not valid\n"
93 static void die(void) __attribute__((__noreturn__));
94 static struct tab *tab_by_id(uint32_t);
95 static void handle_imsg_err(struct imsg *, size_t);
96 static void handle_imsg_check_cert(struct imsg *, size_t);
97 static void handle_check_cert_user_choice(int, struct tab *);
98 static void handle_maybe_save_new_cert(int, struct tab *);
99 static void handle_imsg_got_code(struct imsg *, size_t);
100 static void handle_imsg_got_meta(struct imsg *, size_t);
101 static void handle_maybe_save_page(int, struct tab *);
102 static void handle_save_page_path(const char *, struct tab *);
103 static void handle_imsg_file_opened(struct imsg *, size_t);
104 static void handle_imsg_buf(struct imsg *, size_t);
105 static void handle_imsg_eof(struct imsg *, size_t);
106 static void handle_imsg_bookmark_ok(struct imsg *, size_t);
107 static void handle_imsg_save_cert_ok(struct imsg *, size_t);
108 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
109 static void handle_dispatch_imsg(int, short, void *);
110 static int load_about_url(struct tab *, const char *);
111 static int load_file_url(struct tab *, const char *);
112 static int load_finger_url(struct tab *, const char *);
113 static int load_gemini_url(struct tab *, const char *);
114 static int load_gopher_url(struct tab *, const char *);
115 static int load_via_proxy(struct tab *, const char *,
117 static int make_request(struct tab *, struct get_req *, int,
119 static int make_fs_request(struct tab *, int, const char *);
120 static int do_load_url(struct tab *, const char *, const char *);
121 static pid_t start_child(enum telescope_process, const char *, int);
123 static struct proto {
126 int (*loadfn)(struct tab *, const char *);
128 {"about", NULL, load_about_url},
129 {"file", NULL, load_file_url},
130 {"finger", "79", load_finger_url},
131 {"gemini", "1965", load_gemini_url},
132 {"gopher", "70", load_gopher_url},
136 static imsg_handlerfn *handlers[] = {
137 [IMSG_ERR] = handle_imsg_err,
138 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
139 [IMSG_GOT_CODE] = handle_imsg_got_code,
140 [IMSG_GOT_META] = handle_imsg_got_meta,
141 [IMSG_BUF] = handle_imsg_buf,
142 [IMSG_EOF] = handle_imsg_eof,
143 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
144 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
145 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
146 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
149 static struct ohash certs;
151 static void __attribute__((__noreturn__))
158 tab_by_id(uint32_t id)
162 TAILQ_FOREACH(t, &tabshead, tabs) {
171 handle_imsg_err(struct imsg *imsg, size_t datalen)
176 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
180 page[datalen-1] = '\0';
182 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
183 tab->hist_cur->h, page) == -1)
185 load_page_from_str(tab, page);
190 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
192 const char *hash, *host, *port;
194 struct tofu_entry *e;
198 if (hash[datalen-1] != '\0')
201 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
204 if (tab->proxy != NULL) {
205 host = tab->proxy->host;
206 port = tab->proxy->port;
208 host = tab->uri.host;
209 port = tab->uri.port;
212 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
213 /* TODO: an update in libressl/libretls changed
214 * significantly. Find a better approach at storing
216 if (datalen > sizeof(e->hash))
219 tofu_res = 1; /* trust on first use */
220 if ((e = calloc(1, sizeof(*e))) == NULL)
222 strlcpy(e->domain, host, sizeof(e->domain));
223 if (*port != '\0' && strcmp(port, "1965")) {
224 strlcat(e->domain, ":", sizeof(e->domain));
225 strlcat(e->domain, port, sizeof(e->domain));
227 strlcpy(e->hash, hash, sizeof(e->hash));
229 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
231 tofu_res = !strcmp(hash, e->hash);
234 if (e->verified == -1)
235 tab->trust = TS_TEMP_TRUSTED;
236 else if (e->verified == 1)
237 tab->trust = TS_VERIFIED;
239 tab->trust = TS_TRUSTED;
241 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
242 &tofu_res, sizeof(tofu_res));
244 tab->trust = TS_UNTRUSTED;
245 load_page_from_str(tab, "# Certificate mismatch\n");
246 if ((tab->cert = strdup(hash)) == NULL)
248 ui_yornp("Certificate mismatch. Proceed?",
249 handle_check_cert_user_choice, tab);
254 handle_check_cert_user_choice(int accept, struct tab *tab)
256 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
261 * trust the certificate for this session only. If
262 * the page results in a redirect while we're asking
263 * the user to save, we'll end up with an invalid
264 * tabid (one request == one tab id). It also makes
265 * sense to save it for the current session if the
268 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port,
271 ui_yornp("Save the new certificate?",
272 handle_maybe_save_new_cert, tab);
280 handle_maybe_save_new_cert(int accept, struct tab *tab)
282 struct tofu_entry *e;
283 const char *host, *port;
285 if (tab->proxy != NULL) {
286 host = tab->proxy->host;
287 port = tab->proxy->port;
289 host = tab->uri.host;
290 port = tab->uri.port;
296 if ((e = calloc(1, sizeof(*e))) == NULL)
299 strlcpy(e->domain, host, sizeof(e->domain));
300 if (*port != '\0' && strcmp(port, "1965")) {
301 strlcat(e->domain, ":", sizeof(e->domain));
302 strlcat(e->domain, port, sizeof(e->domain));
304 strlcpy(e->hash, tab->cert, sizeof(e->hash));
305 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
307 tofu_update(&certs, e);
309 tab->trust = TS_TRUSTED;
317 normalize_code(int n)
320 if (n == 10 || n == 11)
326 if (n == 30 || n == 31)
334 if (n <= 53 || n == 59)
342 return MALFORMED_RESPONSE;
346 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
350 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
353 if (sizeof(tab->code) != datalen)
356 memcpy(&tab->code, imsg->data, sizeof(tab->code));
357 tab->code = normalize_code(tab->code);
358 if (tab->code != 30 && tab->code != 31)
359 tab->redirect_count = 0;
363 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
367 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
370 if (sizeof(tab->meta) <= datalen)
373 memcpy(tab->meta, imsg->data, datalen);
375 if (tab->code < 10) { /* internal errors */
376 load_page_from_str(tab, err_pages[tab->code]);
377 } else if (tab->code < 20) { /* 1x */
378 load_page_from_str(tab, err_pages[tab->code]);
379 ui_require_input(tab, tab->code == 11, PROTO_GEMINI);
380 } else if (tab->code == 20) {
381 if (setup_parser_for(tab)) {
382 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
384 load_page_from_str(tab,
385 err_pages[UNKNOWN_TYPE_OR_CSET]);
386 ui_yornp("Can't display page, save it?",
387 handle_maybe_save_page, tab);
389 } else if (tab->code < 40) { /* 3x */
390 tab->redirect_count++;
392 /* TODO: make customizable? */
393 if (tab->redirect_count > 5) {
394 load_page_from_str(tab,
395 err_pages[TOO_MUCH_REDIRECTS]);
397 do_load_url(tab, tab->meta, NULL);
398 } else { /* 4x, 5x & 6x */
399 load_page_from_str(tab, err_pages[tab->code]);
404 handle_maybe_save_page(int dosave, struct tab *tab)
407 ui_read("Save to path", handle_save_page_path, tab);
413 handle_save_page_path(const char *path, struct tab *tab)
420 tab->path = strdup(path);
422 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
426 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
433 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL) {
439 if (imsg->fd == -1) {
443 if (e[datalen-1] != '\0')
445 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
449 load_page_from_str(tab, page);
453 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
458 handle_imsg_buf(struct imsg *imsg, size_t datalen)
462 char *page, buf[FMT_SCALED_STRSIZE] = {0};
464 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
467 tab->bytes += datalen;
469 if (!parser_parse(tab, imsg->data, datalen))
472 write(tab->fd, imsg->data, datalen);
473 fmt_scaled(tab->bytes, buf);
474 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
479 load_page_from_str(tab, page);
483 ui_on_tab_refresh(tab);
487 handle_imsg_eof(struct imsg *imsg, size_t datalen)
491 char *page, buf[FMT_SCALED_STRSIZE] = {0};
493 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
497 if (!parser_free(tab))
500 fmt_scaled(tab->bytes, buf);
501 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
506 load_page_from_str(tab, page);
515 ui_on_tab_refresh(tab);
516 ui_on_tab_loaded(tab);
520 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
524 if (datalen != sizeof(res))
527 memcpy(&res, imsg->data, sizeof(res));
529 message("Added to bookmarks!");
531 message("Failed to add to bookmarks: %s",
536 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
540 if (datalen != sizeof(res))
542 memcpy(&res, imsg->data, datalen);
544 message("Failed to save the cert for: %s",
549 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
553 if (datalen != sizeof(res))
555 memcpy(&res, imsg->data, datalen);
557 message("Failed to update the certificate");
561 handle_dispatch_imsg(int fd, short ev, void *d)
563 struct imsgev *iev = d;
565 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
566 err(1, "connection closed");
570 load_about_url(struct tab *tab, const char *url)
572 tab->trust = TS_UNKNOWN;
573 parser_init(tab, gemtext_initparser);
574 return make_fs_request(tab, IMSG_GET, url);
578 load_file_url(struct tab *tab, const char *url)
580 tab->trust = TS_UNKNOWN;
581 return make_fs_request(tab, IMSG_GET_FILE, tab->uri.path);
585 load_finger_url(struct tab *tab, const char *url)
591 memset(&req, 0, sizeof(req));
592 strlcpy(req.port, tab->uri.port, sizeof(req.port));
595 * Sometimes the finger url have the user as path component
596 * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
597 * userinfo (e.g. finger://cobradile@finger.farm).
599 if ((at = strchr(tab->uri.host, '@')) != NULL) {
600 len = at - tab->uri.host;
601 memcpy(req.req, tab->uri.host, len);
603 if (len >= sizeof(req.req))
607 strlcpy(req.host, at+1, sizeof(req.host));
609 strlcpy(req.host, tab->uri.host, sizeof(req.host));
611 /* +1 to skip the initial `/' */
612 strlcpy(req.req, tab->uri.path+1, sizeof(req.req));
614 strlcat(req.req, "\r\n", sizeof(req.req));
616 parser_init(tab, textplain_initparser);
617 return make_request(tab, &req, PROTO_FINGER, NULL);
621 load_gemini_url(struct tab *tab, const char *url)
625 memset(&req, 0, sizeof(req));
626 strlcpy(req.host, tab->uri.host, sizeof(req.host));
627 strlcpy(req.port, tab->uri.port, sizeof(req.host));
629 return make_request(tab, &req, PROTO_GEMINI, tab->hist_cur->h);
632 static inline const char *
633 gopher_skip_selector(const char *path, int *ret_type)
637 if (!strcmp(path, "/") || *path == '\0') {
646 switch (*ret_type = *path) {
662 load_gopher_url(struct tab *tab, const char *url)
668 memset(&req, 0, sizeof(req));
669 strlcpy(req.host, tab->uri.host, sizeof(req.host));
670 strlcpy(req.port, tab->uri.port, sizeof(req.host));
672 path = gopher_skip_selector(tab->uri.path, &type);
675 parser_init(tab, textplain_initparser);
678 parser_init(tab, gophermap_initparser);
681 ui_require_input(tab, 0, PROTO_GOPHER);
682 return load_page_from_str(tab, err_pages[10]);
684 return load_page_from_str(tab, "Unknown gopher selector");
687 strlcpy(req.req, path, sizeof(req.req));
688 if (*tab->uri.query != '\0') {
689 strlcat(req.req, "?", sizeof(req.req));
690 strlcat(req.req, tab->uri.query, sizeof(req.req));
692 strlcat(req.req, "\r\n", sizeof(req.req));
694 return make_request(tab, &req, PROTO_GOPHER, NULL);
698 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
702 memset(&req, 0, sizeof(req));
703 strlcpy(req.host, p->host, sizeof(req.host));
704 strlcpy(req.port, p->port, sizeof(req.host));
708 return make_request(tab, &req, p->proto, tab->hist_cur->h);
712 make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
715 tab->id = tab_new_id();
719 strlcpy(req->req, r, sizeof(req->req));
720 strlcat(req->req, "\r\n", sizeof(req->req));
723 ui_send_net(IMSG_GET_RAW, tab->id, req, sizeof(*req));
726 * So the various load_*_url can `return make_request` and
727 * do_load_url is happy.
733 make_fs_request(struct tab *tab, int type, const char *r)
736 tab->id = tab_new_id();
738 ui_send_fs(type, tab->id, r, strlen(r)+1);
741 * So load_{about,file}_url can `return make_fs_request` and
742 * do_load_url is happy.
748 gopher_send_search_req(struct tab *tab, const char *text)
752 start_loading_anim(tab);
754 memset(&req, 0, sizeof(req));
755 strlcpy(req.host, tab->uri.host, sizeof(req.host));
756 strlcpy(req.port, tab->uri.port, sizeof(req.host));
759 strlcpy(req.req, tab->uri.path+2, sizeof(req.req));
760 if (*tab->uri.query != '\0') {
761 strlcat(req.req, "?", sizeof(req.req));
762 strlcat(req.req, tab->uri.query, sizeof(req.req));
765 strlcat(req.req, "\t", sizeof(req.req));
766 strlcat(req.req, text, sizeof(req.req));
767 strlcat(req.req, "\r\n", sizeof(req.req));
769 erase_buffer(&tab->buffer);
770 parser_init(tab, gophermap_initparser);
772 make_request(tab, &req, PROTO_GOPHER, NULL);
776 * Effectively load the given url in the given tab. Return 1 when
777 * loading the page asynchronously, and thus when an erase_buffer can
778 * be done right after this function return, or 0 when loading the
779 * page synchronously. In this last case, erase_buffer *MUST* be
780 * called by the handling function (such as load_page_from_str).
783 do_load_url(struct tab *tab, const char *url, const char *base)
799 tab->trust = TS_UNKNOWN;
802 memcpy(&uri, &tab->uri, sizeof(tab->uri));
804 phos_parse_absolute_uri(base, &uri);
806 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
807 if (asprintf(&t, "#error loading %s\n>%s\n",
808 url, "Can't parse the URI") == -1)
810 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
811 load_page_from_str(tab, t);
816 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
817 sizeof(tab->hist_cur->h));
819 for (p = protos; p->schema != NULL; ++p) {
820 if (!strcmp(tab->uri.scheme, p->schema)) {
822 if (*tab->uri.port == '\0' && p->port != NULL)
823 strlcpy(tab->uri.port, p->port,
824 sizeof(tab->uri.port));
826 return p->loadfn(tab, url);
830 TAILQ_FOREACH(proxy, &proxies, proxies) {
831 if (!strcmp(tab->uri.scheme, proxy->match_proto))
832 return load_via_proxy(tab, url, proxy);
835 return load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
839 * Load url in tab. If tab is marked as lazy, only prepare the url
840 * but don't load it. If tab is lazy and a url was already prepared,
844 load_url(struct tab *tab, const char *url, const char *base, int nohist)
848 lazy = tab->flags & TAB_LAZY;
850 if (lazy && tab->hist_cur != NULL) {
852 tab->flags &= ~TAB_LAZY;
855 /* can't have both nohist and lazy at the same time. */
859 if (!nohist && (!lazy || tab->hist_cur == NULL)) {
860 if (tab->hist_cur != NULL)
861 hist_clear_forward(&tab->hist,
862 TAILQ_NEXT(tab->hist_cur, entries));
864 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur)))
870 strlcpy(tab->buffer.page.title, url,
871 sizeof(tab->buffer.page.title));
872 hist_push(&tab->hist, tab->hist_cur);
875 strlcpy(tab->hist_cur->h, url,
876 sizeof(tab->hist_cur->h));
879 if (!lazy && do_load_url(tab, url, base))
880 erase_buffer(&tab->buffer);
884 load_url_in_tab(struct tab *tab, const char *url, const char *base, int nohist)
887 load_url(tab, url, base, nohist);
893 message("Loading %s...", url);
894 start_loading_anim(tab);
895 load_url(tab, url, base, nohist);
899 load_previous_page(struct tab *tab)
903 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
906 do_load_url(tab, h->h, NULL);
911 load_next_page(struct tab *tab)
915 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
918 do_load_url(tab, h->h, NULL);
923 add_to_bookmarks(const char *str)
925 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
930 * Given a user-entered URL, apply some heuristics to use it:
932 * - if it's a proper url use it
933 * - if it starts with a `./' or a `/' assume its a file:// url
934 * - assume it's a gemini:// url
936 * `ret' (of which len is the size) will be filled with the resulting
940 humanify_url(const char *raw, char *ret, size_t len)
945 if (phos_parse_absolute_uri(raw, &uri)) {
946 strlcpy(ret, raw, len);
950 if (has_prefix(raw, "./")) {
951 strlcpy(ret, "file://", len);
952 getcwd(buf, sizeof(buf));
953 strlcat(ret, buf, len);
954 strlcat(ret, "/", len);
955 strlcat(ret, raw+2, len);
960 strlcpy(ret, "file://", len);
961 strlcat(ret, raw, len);
965 strlcpy(ret, "gemini://", len);
966 strlcat(ret, raw, len);
970 start_child(enum telescope_process p, const char *argv0, int fd)
976 switch (pid = fork()) {
986 if (dup2(fd, 3) == -1)
987 err(1, "cannot setup imsg fd");
989 argv[argc++] = argv0;
992 errx(1, "Can't start ui process");
994 argv[argc++] = "-Tf";
997 argv[argc++] = "-Tn";
1001 argv[argc++] = NULL;
1002 execvp(argv0, (char *const *)argv);
1003 err(1, "execvp(%s)", argv0);
1007 ui_send_net(int type, uint32_t peerid, const void *data,
1010 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
1015 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
1017 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
1021 static void __attribute__((noreturn))
1024 fprintf(stderr, "USAGE: %s [-hnv] [-c config] [url]\n",
1026 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1031 main(int argc, char * const *argv)
1033 struct imsgev net_ibuf, fs_ibuf;
1035 int pipe2net[2], pipe2fs[2];
1036 int ch, configtest = 0, fail = 0;
1041 char path[PATH_MAX], url[GEMINI_URL_LEN+1];
1046 signal(SIGCHLD, SIG_IGN);
1047 signal(SIGPIPE, SIG_IGN);
1049 if (getenv("NO_COLOR") != NULL)
1052 strlcpy(path, getenv("HOME"), sizeof(path));
1053 strlcat(path, "/.telescope/config", sizeof(path));
1055 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1058 exit(ui_print_colors());
1061 strlcpy(path, optarg, sizeof(path));
1077 errx(1, "invalid process spec %c",
1082 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1096 else if (proc == PROC_FS)
1098 else if (proc == PROC_NET)
1106 humanify_url(argv[0], url, sizeof(url));
1109 /* setup keys before reading the config */
1110 TAILQ_INIT(&global_map.m);
1111 global_map.unhandled_input = global_key_unbound;
1112 TAILQ_INIT(&minibuffer_map.m);
1115 parseconfig(path, fail);
1122 if ((sessionfd = lock_session()) == -1)
1123 errx(1, "can't lock session, is another instance of "
1124 "telescope already running?");
1126 /* Start children. */
1127 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
1128 err(1, "socketpair");
1129 start_child(PROC_FS, argv0, pipe2fs[1]);
1130 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
1132 iev_fs->handler = handle_dispatch_imsg;
1134 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1135 err(1, "socketpair");
1136 start_child(PROC_NET, argv0, pipe2net[1]);
1137 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1138 iev_net = &net_ibuf;
1139 iev_net->handler = handle_dispatch_imsg;
1143 /* initialize tofu & load certificates */
1144 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1149 /* Setup event handler for the autosave */
1152 /* Setup event handlers for pipes to fs/net */
1153 iev_fs->events = EV_READ;
1154 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
1155 iev_fs->handler, iev_fs);
1156 event_add(&iev_fs->ev, NULL);
1158 iev_net->events = EV_READ;
1159 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
1160 iev_net->handler, iev_net);
1161 event_add(&iev_net->ev, NULL);
1164 load_last_session();
1165 if (has_url || TAILQ_EMPTY(&tabshead))
1166 new_tab(url, NULL, NULL);
1168 sandbox_ui_process();
1174 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
1175 ui_send_net(IMSG_QUIT, 0, NULL, 0);
1176 imsg_flush(&iev_fs->ibuf);
1177 imsg_flush(&iev_net->ibuf);
1179 /* wait for children to terminate */
1181 pid = wait(&status);
1183 if (errno != EINTR && errno != ECHILD)
1185 } else if (WIFSIGNALED(status))
1186 warnx("child terminated; signal %d", WTERMSIG(status));
1187 } while (pid != -1 || (pid == -1 && errno == EINTR));