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>
37 #include "minibuffer.h"
40 #include "telescope.h"
44 static const struct option longopts[] = {
45 {"colors", no_argument, NULL, 'C'},
46 {"colours", no_argument, NULL, 'C'},
47 {"help", no_argument, NULL, 'h'},
48 {"safe", no_argument, NULL, 'S'},
49 {"version", no_argument, NULL, 'v'},
53 static const char *opts = "Cc:hnST:v";
56 static char url[GEMINI_URL_LEN];
59 * Used to know when we're finished loading.
64 * "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
65 * anything to the filesystem or execute external programs.
69 static struct imsgev *iev_net;
71 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
72 struct tabshead ktabshead = TAILQ_HEAD_INITIALIZER(ktabshead);
73 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
75 enum telescope_process {
80 #define CANNOT_FETCH 0
81 #define TOO_MUCH_REDIRECTS 1
82 #define MALFORMED_RESPONSE 2
83 #define UNKNOWN_TYPE_OR_CSET 3
84 #define UNKNOWN_PROTOCOL 4
86 /* XXX: keep in sync with normalize_code */
87 const char *err_pages[70] = {
88 [CANNOT_FETCH] = "# Couldn't load the page\n",
89 [TOO_MUCH_REDIRECTS] = "# Too much redirects\n",
90 [MALFORMED_RESPONSE] = "# Got a malformed response\n",
91 [UNKNOWN_TYPE_OR_CSET] = "# Unsupported type or charset\n",
92 [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
94 [10] = "# Input required\n",
95 [11] = "# Input required\n",
96 [40] = "# Temporary failure\n",
97 [41] = "# Server unavailable\n",
98 [42] = "# CGI error\n",
99 [43] = "# Proxy error\n",
100 [44] = "# Slow down\n",
101 [50] = "# Permanent failure\n",
102 [51] = "# Not found\n",
104 [53] = "# Proxy request refused\n",
105 [59] = "# Bad request\n",
106 [60] = "# Client certificate required\n",
107 [61] = "# Certificate not authorised\n",
108 [62] = "# Certificate not valid\n"
111 static void die(void) __attribute__((__noreturn__));
112 static struct tab *tab_by_id(uint32_t);
113 static void handle_imsg_err(struct imsg *, size_t);
114 static void handle_imsg_check_cert(struct imsg *, size_t);
115 static void handle_check_cert_user_choice(int, struct tab *);
116 static void handle_maybe_save_new_cert(int, struct tab *);
117 static void handle_imsg_got_code(struct imsg *, size_t);
118 static void handle_imsg_got_meta(struct imsg *, size_t);
119 static void handle_maybe_save_page(int, struct tab *);
120 static void handle_save_page_path(const char *, struct tab *);
121 static void handle_imsg_buf(struct imsg *, size_t);
122 static void handle_imsg_eof(struct imsg *, size_t);
123 static void handle_dispatch_imsg(int, short, void *);
124 static int load_about_url(struct tab *, const char *);
125 static int load_file_url(struct tab *, const char *);
126 static int load_finger_url(struct tab *, const char *);
127 static int load_gemini_url(struct tab *, const char *);
128 static int load_gopher_url(struct tab *, const char *);
129 static int load_via_proxy(struct tab *, const char *,
131 static int make_request(struct tab *, struct get_req *, int,
133 static int do_load_url(struct tab *, const char *, const char *, int);
134 static pid_t start_child(enum telescope_process, const char *, int);
135 static void send_url(const char *);
137 static const struct proto {
140 int (*loadfn)(struct tab *, const char *);
142 {"about", NULL, load_about_url},
143 {"file", NULL, load_file_url},
144 {"finger", "79", load_finger_url},
145 {"gemini", "1965", load_gemini_url},
146 {"gopher", "70", load_gopher_url},
150 static imsg_handlerfn *handlers[] = {
151 [IMSG_ERR] = handle_imsg_err,
152 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
153 [IMSG_GOT_CODE] = handle_imsg_got_code,
154 [IMSG_GOT_META] = handle_imsg_got_meta,
155 [IMSG_BUF] = handle_imsg_buf,
156 [IMSG_EOF] = handle_imsg_eof,
159 static struct ohash certs;
161 static void __attribute__((__noreturn__))
168 tab_by_id(uint32_t id)
172 TAILQ_FOREACH(t, &tabshead, tabs) {
181 handle_imsg_err(struct imsg *imsg, size_t datalen)
186 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
190 page[datalen-1] = '\0';
192 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
193 tab->hist_cur->h, page) == -1)
195 load_page_from_str(tab, page);
200 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
202 const char *hash, *host, *port;
204 struct tofu_entry *e;
208 if (hash[datalen-1] != '\0')
211 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
214 if (tab->proxy != NULL) {
215 host = tab->proxy->host;
216 port = tab->proxy->port;
218 host = tab->uri.host;
219 port = tab->uri.port;
222 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
223 /* TODO: an update in libressl/libretls changed
224 * significantly. Find a better approach at storing
226 if (datalen > sizeof(e->hash))
229 tofu_res = 1; /* trust on first use */
230 if ((e = calloc(1, sizeof(*e))) == NULL)
232 strlcpy(e->domain, host, sizeof(e->domain));
233 if (*port != '\0' && strcmp(port, "1965")) {
234 strlcat(e->domain, ":", sizeof(e->domain));
235 strlcat(e->domain, port, sizeof(e->domain));
237 strlcpy(e->hash, hash, sizeof(e->hash));
238 tofu_save(&certs, e);
240 tofu_res = !strcmp(hash, e->hash);
243 if (e->verified == -1)
244 tab->trust = TS_TEMP_TRUSTED;
245 else if (e->verified == 1)
246 tab->trust = TS_VERIFIED;
248 tab->trust = TS_TRUSTED;
250 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
251 &tofu_res, sizeof(tofu_res));
253 tab->trust = TS_UNTRUSTED;
254 load_page_from_str(tab, "# Certificate mismatch\n");
255 if ((tab->cert = strdup(hash)) == NULL)
257 ui_yornp("Certificate mismatch. Proceed?",
258 handle_check_cert_user_choice, tab);
263 handle_check_cert_user_choice(int accept, struct tab *tab)
265 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
270 * trust the certificate for this session only. If
271 * the page results in a redirect while we're asking
272 * the user to save, we'll end up with an invalid
273 * tabid (one request == one tab id). It also makes
274 * sense to save it for the current session if the
277 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port,
281 ui_yornp("Save the new certificate?",
282 handle_maybe_save_new_cert, tab);
284 message("Certificate temporarly trusted");
292 handle_maybe_save_new_cert(int accept, struct tab *tab)
294 struct tofu_entry *e;
295 const char *host, *port;
297 if (tab->proxy != NULL) {
298 host = tab->proxy->host;
299 port = tab->proxy->port;
301 host = tab->uri.host;
302 port = tab->uri.port;
308 if ((e = calloc(1, sizeof(*e))) == NULL)
311 strlcpy(e->domain, host, sizeof(e->domain));
312 if (*port != '\0' && strcmp(port, "1965")) {
313 strlcat(e->domain, ":", sizeof(e->domain));
314 strlcat(e->domain, port, sizeof(e->domain));
316 strlcpy(e->hash, tab->cert, sizeof(e->hash));
319 tofu_update(&certs, e);
321 tab->trust = TS_TRUSTED;
329 normalize_code(int n)
332 if (n == 10 || n == 11)
338 if (n == 30 || n == 31)
346 if (n <= 53 || n == 59)
354 return MALFORMED_RESPONSE;
358 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
362 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
365 if (sizeof(tab->code) != datalen)
368 memcpy(&tab->code, imsg->data, sizeof(tab->code));
369 tab->code = normalize_code(tab->code);
370 if (tab->code != 30 && tab->code != 31)
371 tab->redirect_count = 0;
375 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
380 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
383 if (sizeof(tab->meta) <= datalen)
386 memcpy(tab->meta, imsg->data, datalen);
388 if (tab->code < 10) { /* internal errors */
389 load_page_from_str(tab, err_pages[tab->code]);
390 } else if (tab->code < 20) { /* 1x */
391 free(tab->last_input_url);
392 tab->last_input_url = strdup(tab->hist_cur->h);
393 if (tab->last_input_url == NULL)
396 load_page_from_str(tab, err_pages[tab->code]);
397 ui_require_input(tab, tab->code == 11, ir_select_gemini);
398 } else if (tab->code == 20) {
399 history_add(tab->hist_cur->h);
400 if (setup_parser_for(tab)) {
401 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
402 } else if (safe_mode) {
403 load_page_from_str(tab,
404 err_pages[UNKNOWN_TYPE_OR_CSET]);
408 if ((h = hist_pop(&tab->hist)) != NULL)
410 snprintf(buf, sizeof(buf),
411 "Can't display \"%s\", save it?", tab->meta);
412 ui_yornp(buf, handle_maybe_save_page, tab);
414 } else if (tab->code < 40) { /* 3x */
415 tab->redirect_count++;
417 /* TODO: make customizable? */
418 if (tab->redirect_count > 5) {
419 load_page_from_str(tab,
420 err_pages[TOO_MUCH_REDIRECTS]);
422 do_load_url(tab, tab->meta, NULL, LU_MODE_NOCACHE);
423 } else { /* 4x, 5x & 6x */
424 load_page_from_str(tab, err_pages[tab->code]);
429 handle_maybe_save_page(int dosave, struct tab *tab)
432 char input[PATH_MAX];
434 /* XXX: this print a message that is confusing */
435 ui_on_tab_loaded(tab);
442 if ((f = strrchr(tab->uri.path, '/')) == NULL)
447 strlcpy(input, download_path, sizeof(input));
448 strlcat(input, f, sizeof(input));
450 ui_read("Save to path", handle_save_page_path, tab, input);
454 handle_save_page_path(const char *path, struct tab *tab)
464 ui_show_downloads_pane();
466 d = enqueue_download(tab->id, path, 0);
469 * Change this tab id, the old one is associated with the
472 tab->id = tab_new_id();
474 if ((fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, 0644)) == -1)
475 message("Can't open file %s: %s", d->path, strerror(errno));
476 else if (d->buffer) {
480 if ((fp = fdopen(fd, "w")) != NULL) {
481 r = parser_serialize(current_tab, fp);
483 message("Failed to save the page.");
487 dequeue_first_download();
490 ui_send_net(IMSG_PROCEED, d->id, NULL, 0);
495 handle_imsg_buf(struct imsg *imsg, size_t datalen)
497 struct tab *tab = NULL;
498 struct download *d = NULL;
500 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
501 (d = download_by_id(imsg->hdr.peerid)) == NULL)
505 if (!parser_parse(tab, imsg->data, datalen))
507 ui_on_tab_refresh(tab);
510 write(d->fd, imsg->data, datalen);
511 ui_on_download_refresh();
516 handle_imsg_eof(struct imsg *imsg, size_t datalen)
518 struct tab *tab = NULL;
519 struct download *d = NULL;
521 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
522 (d = download_by_id(imsg->hdr.peerid)) == NULL)
526 if (!parser_free(tab))
528 if (has_prefix(tab->hist_cur->h, "gemini://") ||
529 has_prefix(tab->hist_cur->h, "gopher://"))
531 ui_on_tab_refresh(tab);
532 ui_on_tab_loaded(tab);
536 ui_on_download_refresh();
541 handle_dispatch_imsg(int fd, short ev, void *d)
543 struct imsgev *iev = d;
545 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
546 err(1, "connection closed");
550 load_about_url(struct tab *tab, const char *url)
552 tab->trust = TS_TRUSTED;
553 fs_load_url(tab, url);
554 ui_on_tab_refresh(tab);
555 ui_on_tab_loaded(tab);
560 load_file_url(struct tab *tab, const char *url)
562 tab->trust = TS_TRUSTED;
563 fs_load_url(tab, url);
564 ui_on_tab_refresh(tab);
565 ui_on_tab_loaded(tab);
570 load_finger_url(struct tab *tab, const char *url)
576 memset(&req, 0, sizeof(req));
577 strlcpy(req.port, tab->uri.port, sizeof(req.port));
580 * Sometimes the finger url have the user as path component
581 * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
582 * userinfo (e.g. finger://cobradile@finger.farm).
584 if ((at = strchr(tab->uri.host, '@')) != NULL) {
585 len = at - tab->uri.host;
586 memcpy(req.req, tab->uri.host, len);
588 if (len >= sizeof(req.req))
592 strlcpy(req.host, at+1, sizeof(req.host));
594 strlcpy(req.host, tab->uri.host, sizeof(req.host));
596 /* +1 to skip the initial `/' */
597 strlcpy(req.req, tab->uri.path+1, sizeof(req.req));
599 strlcat(req.req, "\r\n", sizeof(req.req));
601 parser_init(tab, textplain_initparser);
602 return make_request(tab, &req, PROTO_FINGER, NULL);
606 load_gemini_url(struct tab *tab, const char *url)
610 memset(&req, 0, sizeof(req));
611 strlcpy(req.host, tab->uri.host, sizeof(req.host));
612 strlcpy(req.port, tab->uri.port, sizeof(req.port));
614 return make_request(tab, &req, PROTO_GEMINI, tab->hist_cur->h);
617 static inline const char *
618 gopher_skip_selector(const char *path, int *ret_type)
622 if (!strcmp(path, "/") || *path == '\0') {
631 switch (*ret_type = *path) {
647 load_gopher_url(struct tab *tab, const char *url)
653 memset(&req, 0, sizeof(req));
654 strlcpy(req.host, tab->uri.host, sizeof(req.host));
655 strlcpy(req.port, tab->uri.port, sizeof(req.port));
657 path = gopher_skip_selector(tab->uri.path, &type);
660 parser_init(tab, textplain_initparser);
663 parser_init(tab, gophermap_initparser);
666 free(tab->last_input_url);
667 tab->last_input_url = strdup(url);
668 if (tab->last_input_url == NULL)
670 ui_require_input(tab, 0, ir_select_gopher);
671 return load_page_from_str(tab, err_pages[10]);
673 return load_page_from_str(tab, "Unknown gopher selector");
676 strlcpy(req.req, path, sizeof(req.req));
677 if (*tab->uri.query != '\0') {
678 strlcat(req.req, "?", sizeof(req.req));
679 strlcat(req.req, tab->uri.query, sizeof(req.req));
681 strlcat(req.req, "\r\n", sizeof(req.req));
683 return make_request(tab, &req, PROTO_GOPHER, NULL);
687 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
691 memset(&req, 0, sizeof(req));
692 strlcpy(req.host, p->host, sizeof(req.host));
693 strlcpy(req.port, p->port, sizeof(req.port));
697 return make_request(tab, &req, p->proto, tab->hist_cur->h);
701 make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
704 tab->id = tab_new_id();
708 strlcpy(req->req, r, sizeof(req->req));
709 strlcat(req->req, "\r\n", sizeof(req->req));
712 start_loading_anim(tab);
713 ui_send_net(IMSG_GET_RAW, tab->id, req, sizeof(*req));
716 * So the various load_*_url can `return make_request` and
717 * do_load_url is happy.
723 gopher_send_search_req(struct tab *tab, const char *text)
727 memset(&req, 0, sizeof(req));
728 strlcpy(req.host, tab->uri.host, sizeof(req.host));
729 strlcpy(req.port, tab->uri.port, sizeof(req.port));
732 strlcpy(req.req, tab->uri.path+2, sizeof(req.req));
733 if (*tab->uri.query != '\0') {
734 strlcat(req.req, "?", sizeof(req.req));
735 strlcat(req.req, tab->uri.query, sizeof(req.req));
738 strlcat(req.req, "\t", sizeof(req.req));
739 strlcat(req.req, text, sizeof(req.req));
740 strlcat(req.req, "\r\n", sizeof(req.req));
742 erase_buffer(&tab->buffer);
743 parser_init(tab, gophermap_initparser);
745 make_request(tab, &req, PROTO_GOPHER, NULL);
749 load_page_from_str(struct tab *tab, const char *page)
751 parser_init(tab, gemtext_initparser);
752 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
754 if (!tab->buffer.page.free(&tab->buffer.page))
756 ui_on_tab_refresh(tab);
757 ui_on_tab_loaded(tab);
762 * Effectively load the given url in the given tab. Return 1 when
763 * loading the page asynchronously, and thus when an erase_buffer can
764 * be done right after this function return, or 0 when loading the
765 * page synchronously.
768 do_load_url(struct tab *tab, const char *url, const char *base, int mode)
770 const struct proto *p;
773 int nocache = mode & LU_MODE_NOCACHE;
777 tab->trust = TS_UNKNOWN;
780 memcpy(&uri, &tab->uri, sizeof(tab->uri));
782 phos_parse_absolute_uri(base, &uri);
784 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
785 if (asprintf(&t, "#error loading %s\n>%s\n",
786 url, "Can't parse the URI") == -1)
788 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
789 load_page_from_str(tab, t);
794 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
795 sizeof(tab->hist_cur->h));
797 if (!nocache && mcache_lookup(tab->hist_cur->h, tab)) {
798 ui_on_tab_refresh(tab);
799 ui_on_tab_loaded(tab);
803 for (p = protos; p->schema != NULL; ++p) {
804 if (!strcmp(tab->uri.scheme, p->schema)) {
806 if (*tab->uri.port == '\0' && p->port != NULL)
807 strlcpy(tab->uri.port, p->port,
808 sizeof(tab->uri.port));
810 return p->loadfn(tab, tab->hist_cur->h);
814 TAILQ_FOREACH(proxy, &proxies, proxies) {
815 if (!strcmp(tab->uri.scheme, proxy->match_proto))
816 return load_via_proxy(tab, url, proxy);
819 return load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
823 * Load url in tab and handle history. If a tab is marked as lazy, only
824 * prepare the url but don't load it.
827 load_url(struct tab *tab, const char *url, const char *base, int mode)
829 int lazy = tab->flags & TAB_LAZY;
830 int nohist = mode & LU_MODE_NOHIST;
832 if (operating && lazy) {
833 tab->flags ^= TAB_LAZY;
835 } else if (tab->hist_cur != NULL)
836 get_scroll_position(tab, &tab->hist_cur->line_off,
837 &tab->hist_cur->current_off);
839 if (!nohist && (!lazy || tab->hist_cur == NULL)) {
840 if (tab->hist_cur != NULL)
841 hist_clear_forward(&tab->hist,
842 TAILQ_NEXT(tab->hist_cur, entries));
844 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur)))
850 strlcpy(tab->buffer.page.title, url,
851 sizeof(tab->buffer.page.title));
852 hist_push(&tab->hist, tab->hist_cur);
855 strlcpy(tab->hist_cur->h, url,
856 sizeof(tab->hist_cur->h));
860 do_load_url(tab, url, base, mode);
864 load_url_in_tab(struct tab *tab, const char *url, const char *base, int mode)
868 message("Loading %s...", url);
871 load_url(tab, url, base, mode);
875 load_previous_page(struct tab *tab)
879 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
882 do_load_url(tab, h->h, NULL, LU_MODE_NONE);
887 load_next_page(struct tab *tab)
891 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
894 do_load_url(tab, h->h, NULL, LU_MODE_NONE);
899 write_buffer(const char *path, struct tab *tab)
906 if ((fp = fopen(path, "w")) == NULL)
908 if (!parser_serialize(tab, fp))
909 message("Failed to save the page.");
914 * Given a user-entered URL, apply some heuristics to use it:
916 * - if it's a proper url use it
917 * - if it starts with a `./' or a `/' assume its a file:// url
918 * - assume it's a gemini:// url
920 * `ret' (of which len is the size) will be filled with the resulting
924 humanify_url(const char *raw, char *ret, size_t len)
929 if (phos_parse_absolute_uri(raw, &uri)) {
930 strlcpy(ret, raw, len);
934 if (has_prefix(raw, "./")) {
935 strlcpy(ret, "file://", len);
936 getcwd(buf, sizeof(buf));
937 strlcat(ret, buf, len);
938 strlcat(ret, "/", len);
939 strlcat(ret, raw+2, len);
944 strlcpy(ret, "file://", len);
945 strlcat(ret, raw, len);
949 strlcpy(ret, "gemini://", len);
950 strlcat(ret, raw, len);
954 bookmark_page(const char *url)
958 if ((fp = fopen(bookmark_file, "a")) == NULL)
960 fprintf(fp, "=> %s\n", url);
966 start_child(enum telescope_process p, const char *argv0, int fd)
972 switch (pid = fork()) {
982 if (dup2(fd, 3) == -1)
983 err(1, "cannot setup imsg fd");
985 argv[argc++] = argv0;
988 errx(1, "Can't start ui process");
990 argv[argc++] = "-Tn";
998 execvp(argv0, (char *const *)argv);
999 err(1, "execvp(%s)", argv0);
1003 send_url(const char *url)
1005 struct sockaddr_un sun;
1006 struct imsgbuf ibuf;
1009 if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1012 memset(&sun, 0, sizeof(sun));
1013 sun.sun_family = AF_UNIX;
1014 strlcpy(sun.sun_path, ctlsock_path, sizeof(sun.sun_path));
1016 if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
1017 err(1, "connect: %s", ctlsock_path);
1019 imsg_init(&ibuf, ctl_sock);
1020 imsg_compose(&ibuf, IMSG_CTL_OPEN_URL, 0, 0, -1, url,
1027 ui_send_net(int type, uint32_t peerid, const void *data,
1030 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
1034 static void __attribute__((noreturn))
1037 fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1039 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1044 main(int argc, char * const *argv)
1046 struct imsgev net_ibuf;
1050 int ch, configtest = 0, fail = 0;
1058 signal(SIGCHLD, SIG_IGN);
1059 signal(SIGPIPE, SIG_IGN);
1061 if (getenv("NO_COLOR") != NULL)
1064 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1067 exit(ui_print_colors());
1070 strlcpy(config_path, optarg, sizeof(config_path));
1086 errx(1, "invalid process spec %c",
1091 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1105 else if (proc == PROC_NET)
1113 humanify_url(argv[0], url, sizeof(url));
1118 /* setup keys before reading the config */
1119 TAILQ_INIT(&global_map.m);
1120 global_map.unhandled_input = global_key_unbound;
1121 TAILQ_INIT(&minibuffer_map.m);
1124 parseconfig(config_path, fail);
1130 if (download_path == NULL &&
1131 (download_path = strdup("/tmp/")) == NULL)
1134 if (!safe_mode && (sessionfd = lock_session()) == -1) {
1140 errx(1, "can't lock session, is another instance of "
1141 "telescope already running?");
1144 /* Start children. */
1145 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1146 err(1, "socketpair");
1147 start_child(PROC_NET, argv0, pipe2net[1]);
1148 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1149 iev_net = &net_ibuf;
1150 iev_net->handler = handle_dispatch_imsg;
1154 /* initialize tofu store */
1155 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1160 if ((control_fd = control_init(ctlsock_path)) == -1)
1161 err(1, "control_init %s", ctlsock_path);
1162 control_listen(control_fd);
1165 /* initialize the in-memory cache store */
1168 /* Setup event handler for the autosave */
1171 /* Setup event handlers for pipes to net */
1172 iev_net->events = EV_READ;
1173 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
1174 iev_net->handler, iev_net);
1175 event_add(&iev_net->ev, NULL);
1178 sandbox_ui_process();
1179 fs_load_state(&certs);
1181 switch_to_tab(current_tab);
1186 ui_send_net(IMSG_QUIT, 0, NULL, 0);
1187 imsg_flush(&iev_net->ibuf);
1189 /* wait for children to terminate */
1191 pid = wait(&status);
1193 if (errno != EINTR && errno != ECHILD)
1195 } else if (WIFSIGNALED(status))
1196 warnx("child terminated; signal %d", WTERMSIG(status));
1197 } while (pid != -1 || (pid == -1 && errno == EINTR));
1200 unlink(crashed_file);
1202 if (!safe_mode && close(sessionfd) == -1)
1203 err(1, "close(sessionfd = %d)", sessionfd);