2 * Copyright (c) 2021, 2024 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>
45 #include "minibuffer.h"
48 #include "telescope.h"
53 static const struct option longopts[] = {
54 {"help", no_argument, NULL, 'h'},
55 {"safe", no_argument, NULL, 'S'},
56 {"version", no_argument, NULL, 'v'},
60 static const char *opts = "c:hnST:v";
63 static char url[GEMINI_URL_LEN];
66 * Used to know when we're finished loading.
71 * "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
72 * anything to the filesystem or execute external programs.
76 static struct imsgev *iev_net;
78 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
79 struct tabshead ktabshead = TAILQ_HEAD_INITIALIZER(ktabshead);
80 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
82 enum telescope_process {
87 #define CANNOT_FETCH 0
88 #define TOO_MANY_REDIRECTS 1
89 #define MALFORMED_RESPONSE 2
90 #define UNKNOWN_TYPE_OR_CSET 3
91 #define UNKNOWN_PROTOCOL 4
93 /* XXX: keep in sync with normalize_code */
94 const char *err_pages[70] = {
95 [CANNOT_FETCH] = "# Couldn't load the page\n",
96 [TOO_MANY_REDIRECTS] = "# Too many redirects\n",
97 [MALFORMED_RESPONSE] = "# Got a malformed response\n",
98 [UNKNOWN_TYPE_OR_CSET] = "# Unsupported document format or charset\n",
99 [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
101 [10] = "# Input required\n",
102 [11] = "# Input required\n",
103 [40] = "# Temporary failure\n",
104 [41] = "# Server unavailable\n",
105 [42] = "# CGI error\n",
106 [43] = "# Proxy error\n",
107 [44] = "# Slow down\n",
108 [50] = "# Permanent failure\n",
109 [51] = "# Not found\n",
111 [53] = "# Proxy request refused\n",
112 [59] = "# Bad request\n",
113 [60] = "# Client certificate required\n",
114 [61] = "# Certificate not authorised\n",
115 [62] = "# Certificate not valid\n"
118 static void die(void) __attribute__((__noreturn__));
119 static struct tab *tab_by_id(uint32_t);
120 static void handle_imsg_check_cert(struct imsg *);
121 static void handle_check_cert_user_choice(int, struct tab *);
122 static void handle_maybe_save_new_cert(int, struct tab *);
123 static void handle_maybe_save_page(int, struct tab *);
124 static void handle_save_page_path(const char *, struct tab *);
125 static void handle_dispatch_imsg(int, int, void *);
126 static void load_about_url(struct tab *, const char *);
127 static void load_file_url(struct tab *, const char *);
128 static void load_finger_url(struct tab *, const char *);
129 static void load_gemini_url(struct tab *, const char *);
130 static void load_gopher_url(struct tab *, const char *);
131 static void load_via_proxy(struct tab *, const char *,
133 static void make_request(struct tab *, struct get_req *, int,
135 static void do_load_url(struct tab *, const char *, const char *, int);
136 static pid_t start_child(enum telescope_process, const char *, int);
137 static void send_url(const char *);
139 static const struct proto {
142 void (*loadfn)(struct tab *, const char *);
144 {"about", NULL, load_about_url},
145 {"file", NULL, load_file_url},
146 {"finger", "79", load_finger_url},
147 {"gemini", "1965", load_gemini_url},
148 {"gopher", "70", load_gopher_url},
152 static struct ohash certs;
154 static void __attribute__((__noreturn__))
161 tab_by_id(uint32_t id)
165 TAILQ_FOREACH(t, &tabshead, tabs) {
174 handle_imsg_check_cert(struct imsg *imsg)
177 const char *host, *port;
180 struct tofu_entry *e;
184 if (imsg_get_ibuf(imsg, &ibuf) == -1 ||
185 ibuf_borrow_str(&ibuf, &hash) == -1)
187 datalen = strlen(hash);
189 if ((tab = tab_by_id(imsg_get_id(imsg))) == NULL)
192 if (tab->proxy != NULL) {
193 host = tab->proxy->host;
194 port = tab->proxy->port;
196 host = tab->iri.iri_host;
197 port = tab->iri.iri_portstr;
200 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
201 /* TODO: an update in libressl/libretls changed
202 * significantly. Find a better approach at storing
204 if (datalen > sizeof(e->hash))
207 tofu_res = 1; /* trust on first use */
208 if ((e = calloc(1, sizeof(*e))) == NULL)
210 strlcpy(e->domain, host, sizeof(e->domain));
211 if (*port != '\0' && strcmp(port, "1965")) {
212 strlcat(e->domain, ":", sizeof(e->domain));
213 strlcat(e->domain, port, sizeof(e->domain));
215 strlcpy(e->hash, hash, sizeof(e->hash));
216 tofu_save(&certs, e);
218 tofu_res = !strcmp(hash, e->hash);
221 if (e->verified == -1)
222 tab->trust = TS_TEMP_TRUSTED;
223 else if (e->verified == 1)
224 tab->trust = TS_VERIFIED;
226 tab->trust = TS_TRUSTED;
228 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid, -1,
229 &tofu_res, sizeof(tofu_res));
231 tab->trust = TS_UNTRUSTED;
232 load_page_from_str(tab, "# Certificate mismatch\n");
233 if ((tab->cert = strdup(hash)) == NULL)
235 ui_yornp("Certificate mismatch. Proceed?",
236 handle_check_cert_user_choice, tab);
241 handle_check_cert_user_choice(int accept, struct tab *tab)
243 ui_send_net(IMSG_CERT_STATUS, tab->id, -1, &accept,
247 const char *host, *port;
249 host = tab->iri.iri_host;
250 port = tab->iri.iri_portstr;
253 * trust the certificate for this session only. If
254 * the page results in a redirect while we're asking
255 * the user to save, we'll end up with an invalid
256 * tabid (one request == one tab id). It also makes
257 * sense to save it for the current session if the
260 tofu_temp_trust(&certs, host, port, tab->cert);
263 ui_yornp("Save the new certificate?",
264 handle_maybe_save_new_cert, tab);
266 message("Certificate temporarly trusted");
274 handle_maybe_save_new_cert(int accept, struct tab *tab)
276 struct tofu_entry *e;
277 const char *host, *port;
279 if (tab->proxy != NULL) {
280 host = tab->proxy->host;
281 port = tab->proxy->port;
283 host = tab->iri.iri_host;
284 port = tab->iri.iri_portstr;
290 if ((e = calloc(1, sizeof(*e))) == NULL)
293 strlcpy(e->domain, host, sizeof(e->domain));
294 if (*port != '\0' && strcmp(port, "1965")) {
295 strlcat(e->domain, ":", sizeof(e->domain));
296 strlcat(e->domain, port, sizeof(e->domain));
298 strlcpy(e->hash, tab->cert, sizeof(e->hash));
300 tofu_update_persist(&certs, e);
302 tab->trust = TS_TRUSTED;
310 normalize_code(int n)
313 if (n == 10 || n == 11)
319 if (n == 30 || n == 31)
327 if (n <= 53 || n == 59)
335 return MALFORMED_RESPONSE;
339 handle_request_response(struct tab *tab)
343 if (tab->code != 30 && tab->code != 31)
344 tab->redirect_count = 0;
346 if (tab->code < 10) { /* internal errors */
347 load_page_from_str(tab, err_pages[tab->code]);
348 } else if (tab->code < 20) { /* 1x */
349 free(tab->last_input_url);
350 tab->last_input_url = strdup(hist_cur(tab->hist));
351 if (tab->last_input_url == NULL)
354 load_page_from_str(tab, err_pages[tab->code]);
355 ui_require_input(tab, tab->code == 11, ir_select_gemini);
356 } else if (tab->code == 20) {
357 history_add(hist_cur(tab->hist));
358 if (setup_parser_for(tab)) {
359 ui_send_net(IMSG_PROCEED, tab->id, -1, NULL, 0);
360 } else if (safe_mode) {
361 load_page_from_str(tab,
362 err_pages[UNKNOWN_TYPE_OR_CSET]);
364 hist_prev(tab->hist);
365 snprintf(buf, sizeof(buf),
366 "Can't display \"%s\", save it?", tab->meta);
367 ui_yornp(buf, handle_maybe_save_page, tab);
369 } else if (tab->code < 40) { /* 3x */
370 tab->redirect_count++;
372 /* TODO: make customizable? */
373 if (tab->redirect_count > 5) {
374 load_page_from_str(tab,
375 err_pages[TOO_MANY_REDIRECTS]);
377 do_load_url(tab, tab->meta, hist_cur(tab->hist),
379 } else { /* 4x, 5x & 6x */
380 load_page_from_str(tab, err_pages[tab->code]);
382 cmd_use_certificate(&tab->buffer);
387 handle_maybe_save_page(int dosave, struct tab *tab)
390 char input[PATH_MAX];
392 /* XXX: this print a message that is confusing */
393 ui_on_tab_loaded(tab);
400 if ((f = strrchr(tab->iri.iri_path, '/')) == NULL)
405 strlcpy(input, download_path, sizeof(input));
406 strlcat(input, f, sizeof(input));
408 ui_read("Save to path", handle_save_page_path, tab, input);
412 handle_save_page_path(const char *path, struct tab *tab)
422 if ((fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, 0644)) == -1) {
423 message("Can't open file %s: %s", path, strerror(errno));
428 ui_show_downloads_pane();
429 d = enqueue_download(tab->id, path);
431 ui_send_net(IMSG_PROCEED, d->id, -1, NULL, 0);
434 * Change this tab id, the old one is associated with the
437 tab->id = tab_new_id();
441 handle_dispatch_imsg(int fd, int event, void *data)
443 struct imsgev *iev = data;
444 struct imsgbuf *imsgbuf = &iev->ibuf;
454 if (event & EV_READ) {
455 if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
458 err(1, "connection closed");
460 if (event & EV_WRITE) {
461 if ((n = msgbuf_write(&imsgbuf->w)) == -1 && errno != EAGAIN)
462 err(1, "msgbuf_write");
464 err(1, "connection closed");
468 if ((n = imsg_get(imsgbuf, &imsg)) == -1)
473 switch (imsg_get_type(&imsg)) {
475 if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL)
477 if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
478 ibuf_borrow_str(&ibuf, &str) == -1)
480 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
481 hist_cur(tab->hist), str) == -1)
483 load_page_from_str(tab, page);
486 case IMSG_CHECK_CERT:
487 handle_imsg_check_cert(&imsg);
490 if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL)
492 if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
493 ibuf_get(&ibuf, &code, sizeof(code)) == -1 ||
494 ibuf_borrow_str(&ibuf, &str) == -1)
496 if (strlcpy(tab->meta, str, sizeof(tab->meta)) >=
499 tab->code = normalize_code(code);
500 handle_request_response(tab);
503 if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
504 ((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
508 if (!parser_parse(tab, imsg.data,
509 imsg_get_len(&imsg)))
511 ui_on_tab_refresh(tab);
513 size_t datalen = imsg_get_len(&imsg);
516 write(d->fd, imsg.data, datalen);
517 ui_on_download_refresh();
521 if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
522 ((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
526 if (!parser_free(tab))
528 h = hist_cur(tab->hist);
529 if (!strncmp(h, "gemini://", 9) ||
530 !strncmp(h, "gopher://", 9) ||
531 !strncmp(h, "finger://", 9))
535 * Gemini is handled as soon as a 2x
538 if (!strncmp(h, "finger://", 9) ||
539 !strncmp(h, "gopher://", 9))
542 ui_on_tab_refresh(tab);
543 ui_on_tab_loaded(tab);
547 ui_on_download_refresh();
551 errx(1, "got unknown imsg %d", imsg_get_type(&imsg));
561 load_about_url(struct tab *tab, const char *url)
563 tab->trust = TS_TRUSTED;
564 fs_load_url(tab, url);
565 ui_on_tab_refresh(tab);
566 ui_on_tab_loaded(tab);
570 load_file_url(struct tab *tab, const char *url)
572 tab->trust = TS_TRUSTED;
573 fs_load_url(tab, url);
574 ui_on_tab_refresh(tab);
575 ui_on_tab_loaded(tab);
579 load_finger_url(struct tab *tab, const char *url)
584 memset(&req, 0, sizeof(req));
585 strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
586 strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
589 * Sometimes the finger url have the user as path component
590 * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
591 * userinfo (e.g. finger://cobradile@finger.farm).
593 if (tab->iri.iri_flags & IH_UINFO) {
594 strlcpy(req.req, tab->iri.iri_uinfo, sizeof(req.req));
596 path = tab->iri.iri_path;
599 strlcpy(req.req, path, sizeof(req.req));
601 strlcat(req.req, "\r\n", sizeof(req.req));
603 parser_init(tab, textplain_initparser);
604 make_request(tab, &req, PROTO_FINGER, NULL);
608 load_gemini_url(struct tab *tab, const char *url)
612 memset(&req, 0, sizeof(req));
613 strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
614 strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
616 make_request(tab, &req, PROTO_GEMINI, hist_cur(tab->hist));
619 static inline const char *
620 gopher_skip_selector(const char *path, int *ret_type)
624 if (!strcmp(path, "/") || *path == '\0') {
633 switch (*ret_type = *path) {
649 load_gopher_url(struct tab *tab, const char *url)
655 memset(&req, 0, sizeof(req));
656 strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
657 strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
659 path = gopher_skip_selector(tab->iri.iri_path, &type);
662 parser_init(tab, textplain_initparser);
665 parser_init(tab, gophermap_initparser);
668 free(tab->last_input_url);
669 tab->last_input_url = strdup(url);
670 if (tab->last_input_url == NULL)
672 ui_require_input(tab, 0, ir_select_gopher);
673 load_page_from_str(tab, err_pages[10]);
676 load_page_from_str(tab, "Unknown gopher selector");
680 if (iri_urlunescape(path, req.req, sizeof(req.req)) == -1)
681 strlcpy(req.req, path, sizeof(req.req));
682 if (tab->iri.iri_flags & IH_QUERY) {
683 strlcat(req.req, "?", sizeof(req.req));
684 strlcat(req.req, tab->iri.iri_query, sizeof(req.req));
686 strlcat(req.req, "\r\n", sizeof(req.req));
688 make_request(tab, &req, PROTO_GOPHER, NULL);
692 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
696 memset(&req, 0, sizeof(req));
697 strlcpy(req.host, p->host, sizeof(req.host));
698 strlcpy(req.port, p->port, sizeof(req.port));
702 make_request(tab, &req, p->proto, hist_cur(tab->hist));
706 make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
708 int use_cert = 0, fd = -1;
710 if (proto == PROTO_GEMINI) {
711 tab->client_cert = cert_for(&tab->iri, &tab->client_cert_temp);
712 use_cert = (tab->client_cert != NULL);
716 tab->id = tab_new_id();
720 strlcpy(req->req, r, sizeof(req->req));
721 strlcat(req->req, "\r\n", sizeof(req->req));
724 start_loading_anim(tab);
727 tab->client_cert = NULL;
728 if (use_cert && (fd = cert_open(tab->client_cert)) == -1) {
729 tab->client_cert = NULL;
730 message("failed to open certificate: %s", strerror(errno));
733 ui_send_net(IMSG_GET, tab->id, fd, req, sizeof(*req));
737 gopher_send_search_req(struct tab *tab, const char *text)
741 memset(&req, 0, sizeof(req));
742 strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
743 strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
746 strlcpy(req.req, tab->iri.iri_path+2, sizeof(req.req));
747 if (tab->iri.iri_flags & IH_QUERY) {
748 strlcat(req.req, "?", sizeof(req.req));
749 strlcat(req.req, tab->iri.iri_query, sizeof(req.req));
752 strlcat(req.req, "\t", sizeof(req.req));
753 strlcat(req.req, text, sizeof(req.req));
754 strlcat(req.req, "\r\n", sizeof(req.req));
756 erase_buffer(&tab->buffer);
757 parser_init(tab, gophermap_initparser);
759 make_request(tab, &req, PROTO_GOPHER, NULL);
763 load_page_from_str(struct tab *tab, const char *page)
765 parser_init(tab, gemtext_initparser);
766 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
768 if (!tab->buffer.page.free(&tab->buffer.page))
770 ui_on_tab_refresh(tab);
771 ui_on_tab_loaded(tab);
775 * Effectively load the given url in the given tab.
778 do_load_url(struct tab *tab, const char *url, const char *base, int mode)
780 const struct proto *p;
782 int nocache = mode & LU_MODE_NOCACHE;
787 tab->trust = TS_UNKNOWN;
789 if (iri_parse(base, url, &tab->iri) == -1) {
790 if (asprintf(&t, "# error loading %s\n>%s\n",
791 url, "Can't parse the IRI") == -1)
793 hist_set_cur(tab->hist, url);
794 load_page_from_str(tab, t);
799 iri_unparse(&tab->iri, buf, sizeof(buf));
800 hist_set_cur(tab->hist, buf);
802 if (!nocache && mcache_lookup(buf, tab)) {
803 ui_on_tab_refresh(tab);
804 ui_on_tab_loaded(tab);
808 for (p = protos; p->schema != NULL; ++p) {
809 if (!strcmp(tab->iri.iri_scheme, p->schema)) {
811 if (*tab->iri.iri_portstr == '\0' &&
813 iri_setport(&tab->iri, p->port);
820 TAILQ_FOREACH(proxy, &proxies, proxies) {
821 if (!strcmp(tab->iri.iri_scheme, proxy->match_proto)) {
822 load_via_proxy(tab, url, proxy);
827 load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
831 * Load url in tab and handle history. If a tab is marked as lazy, only
832 * prepare the url but don't load it.
835 load_url(struct tab *tab, const char *url, const char *base, int mode)
837 size_t line_off, curr_off;
838 int lazy = tab->flags & TAB_LAZY;
839 int dohist = !(mode & LU_MODE_NOHIST);
841 if (operating && lazy) {
842 tab->flags ^= TAB_LAZY;
844 } else if (hist_size(tab->hist) != 0) {
845 get_scroll_position(tab, &line_off, &curr_off);
846 hist_set_offs(tab->hist, line_off, curr_off);
850 if (hist_push(tab->hist, url) == -1) {
855 strlcpy(tab->buffer.page.title, url,
856 sizeof(tab->buffer.page.title));
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);
872 base = hist_cur(tab->hist);
874 load_url(tab, url, base, mode);
878 load_previous_page(struct tab *tab)
882 if ((h = hist_prev(tab->hist)) == NULL)
884 do_load_url(tab, h, NULL, LU_MODE_NONE);
889 load_next_page(struct tab *tab)
893 if ((h = hist_next(tab->hist)) == NULL)
895 do_load_url(tab, h, NULL, LU_MODE_NONE);
900 write_buffer(const char *path, struct tab *tab)
907 if ((fp = fopen(path, "w")) == NULL)
909 if (!parser_serialize(tab, fp))
910 message("Failed to save the page.");
915 * Given a user-entered URL, apply some heuristics to use it if
916 * load-url-use-heuristic allows it.
918 * - if it's a proper url use it
919 * - if it starts with a `./' or a `/' assume its a file:// url
920 * - assume it's a default-protocol:// url
922 * `ret' (of which len is the size) will be filled with the resulting
926 humanify_url(const char *raw, const char *base, char *ret, size_t len)
928 static struct iri iri;
930 if (load_url_use_heuristic)
933 if (iri_parse(base, raw, &iri) == 0) {
934 iri_unparse(&iri, ret, len);
938 if (!strncmp(raw, "./", 2)) {
939 strlcpy(ret, "file://", len);
940 strlcat(ret, cwd, len);
941 strlcat(ret, "/", len);
942 strlcat(ret, raw+2, len);
947 strlcpy(ret, "file://", len);
948 strlcat(ret, raw, len);
952 strlcpy(ret, default_protocol, len);
953 strlcat(ret, "://", len);
954 strlcat(ret, raw, len);
958 bookmark_page(const char *url)
962 if ((fp = fopen(bookmark_file, "a")) == NULL)
964 fprintf(fp, "=> %s\n", url);
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++] = "-Tn";
1001 argv[argc++] = NULL;
1002 execvp(argv0, (char *const *)argv);
1003 err(1, "execvp(%s)", argv0);
1007 send_url(const char *url)
1009 struct sockaddr_un sun;
1010 struct imsgbuf ibuf;
1013 if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1016 memset(&sun, 0, sizeof(sun));
1017 sun.sun_family = AF_UNIX;
1018 strlcpy(sun.sun_path, ctlsock_path, sizeof(sun.sun_path));
1020 if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
1021 err(1, "connect: %s", ctlsock_path);
1023 imsg_init(&ibuf, ctl_sock);
1024 imsg_compose(&ibuf, IMSG_CTL_OPEN_URL, 0, 0, -1, url,
1031 ui_send_net(int type, uint32_t peerid, int fd, const void *data,
1034 return imsg_compose_event(iev_net, type, peerid, 0, fd, data,
1038 static void __attribute__((noreturn))
1041 fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1043 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1048 main(int argc, char * const *argv)
1050 struct imsgev net_ibuf;
1054 int ch, configtest = 0, fail = 0;
1062 signal(SIGCHLD, SIG_IGN);
1063 signal(SIGPIPE, SIG_IGN);
1065 if (getenv("NO_COLOR") != NULL)
1068 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1072 strlcpy(config_path, optarg, sizeof(config_path));
1088 errx(1, "invalid process spec %c",
1093 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1107 else if (proc == PROC_NET)
1113 /* setup keys before reading the config */
1114 TAILQ_INIT(&global_map.m);
1115 global_map.unhandled_input = global_key_unbound;
1116 TAILQ_INIT(&minibuffer_map.m);
1118 if (fs_init() == -1)
1119 err(1, "fs_init failed");
1120 if (certs_init(certs_file) == -1)
1121 err(1, "certs_init failed");
1123 parseconfig(config_path, fail);
1129 if (default_protocol == NULL &&
1130 (default_protocol = strdup("gemini")) == NULL)
1133 if (download_path == NULL &&
1134 (download_path = strdup("/tmp/")) == NULL)
1140 if (asprintf(&base, "file://%s/", cwd) == -1)
1144 humanify_url(argv[0], base, url, sizeof(url));
1149 if (!safe_mode && (sessionfd = lock_session()) == -1) {
1155 errx(1, "can't lock session, is another instance of "
1156 "telescope already running?");
1159 /* Start children. */
1160 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1161 err(1, "socketpair");
1162 start_child(PROC_NET, argv0, pipe2net[1]);
1163 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1164 iev_net = &net_ibuf;
1165 iev_net->handler = handle_dispatch_imsg;
1169 /* initialize tofu store */
1170 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1172 if (ev_init() == -1)
1176 if ((control_fd = control_init(ctlsock_path)) == -1)
1177 err(1, "control_init %s", ctlsock_path);
1178 control_listen(control_fd);
1181 /* initialize the in-memory cache store */
1184 /* Setup event handler for the autosave */
1187 /* Setup event handlers for pipes to net */
1188 iev_net->events = EV_READ;
1189 ev_add(iev_net->ibuf.fd, iev_net->events, iev_net->handler, iev_net);
1192 sandbox_ui_process();
1193 load_session(&certs);
1195 new_tab(url, NULL, NULL);
1197 switch_to_tab(current_tab);
1202 ui_send_net(IMSG_QUIT, 0, -1, NULL, 0);
1203 imsg_flush(&iev_net->ibuf);
1205 /* wait for children to terminate */
1207 pid = wait(&status);
1209 if (errno != EINTR && errno != ECHILD)
1211 } else if (WIFSIGNALED(status))
1212 warnx("child terminated; signal %d", WTERMSIG(status));
1213 } while (pid != -1 || (pid == -1 && errno == EINTR));
1216 unlink(crashed_file);
1218 if (!safe_mode && close(sessionfd) == -1)
1219 err(1, "close(sessionfd = %d)", sessionfd);