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>
32 #include "minibuffer.h"
35 #include "telescope.h"
38 static struct option longopts[] = {
39 {"colors", no_argument, NULL, 'c'},
40 {"help", no_argument, NULL, 'h'},
41 {"safe", no_argument, NULL, 'S'},
42 {"version", no_argument, NULL, 'v'},
46 static const char *opts = "Cc:hnST:v";
49 * Used to know when we're finished loading.
54 * "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
55 * anything to the filesystem or execute external programs.
59 static struct imsgev *iev_fs, *iev_net;
61 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
62 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
64 enum telescope_process {
70 #define CANNOT_FETCH 0
71 #define TOO_MUCH_REDIRECTS 1
72 #define MALFORMED_RESPONSE 2
73 #define UNKNOWN_TYPE_OR_CSET 3
74 #define UNKNOWN_PROTOCOL 4
76 /* XXX: keep in sync with normalize_code */
77 const char *err_pages[70] = {
78 [CANNOT_FETCH] = "# Couldn't load the page\n",
79 [TOO_MUCH_REDIRECTS] = "# Too much redirects\n",
80 [MALFORMED_RESPONSE] = "# Got a malformed response\n",
81 [UNKNOWN_TYPE_OR_CSET] = "# Unsupported type or charset\n",
82 [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
84 [10] = "# Input required\n",
85 [11] = "# Input required\n",
86 [40] = "# Temporary failure\n",
87 [41] = "# Server unavailable\n",
88 [42] = "# CGI error\n",
89 [43] = "# Proxy error\n",
90 [44] = "# Slow down\n",
91 [50] = "# Permanent failure\n",
92 [51] = "# Not found\n",
94 [53] = "# Proxy request refused\n",
95 [59] = "# Bad request\n",
96 [60] = "# Client certificate required\n",
97 [61] = "# Certificate not authorised\n",
98 [62] = "# Certificate not valid\n"
101 static void die(void) __attribute__((__noreturn__));
102 static struct tab *tab_by_id(uint32_t);
103 static void handle_imsg_err(struct imsg *, size_t);
104 static void handle_imsg_check_cert(struct imsg *, size_t);
105 static void handle_check_cert_user_choice(int, struct tab *);
106 static void handle_maybe_save_new_cert(int, struct tab *);
107 static void handle_imsg_got_code(struct imsg *, size_t);
108 static void handle_imsg_got_meta(struct imsg *, size_t);
109 static void handle_maybe_save_page(int, struct tab *);
110 static void handle_save_page_path(const char *, struct tab *);
111 static void handle_imsg_file_opened(struct imsg *, size_t);
112 static void handle_imsg_buf(struct imsg *, size_t);
113 static void handle_imsg_eof(struct imsg *, size_t);
114 static void handle_imsg_bookmark_ok(struct imsg *, size_t);
115 static void handle_imsg_save_cert_ok(struct imsg *, size_t);
116 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
117 static void handle_dispatch_imsg(int, short, void *);
118 static int load_about_url(struct tab *, const char *);
119 static int load_file_url(struct tab *, const char *);
120 static int load_finger_url(struct tab *, const char *);
121 static int load_gemini_url(struct tab *, const char *);
122 static int load_gopher_url(struct tab *, const char *);
123 static int load_via_proxy(struct tab *, const char *,
125 static int make_request(struct tab *, struct get_req *, int,
127 static int make_fs_request(struct tab *, int, const char *);
128 static int do_load_url(struct tab *, const char *, const char *);
129 static pid_t start_child(enum telescope_process, const char *, int);
131 static struct proto {
134 int (*loadfn)(struct tab *, const char *);
136 {"about", NULL, load_about_url},
137 {"file", NULL, load_file_url},
138 {"finger", "79", load_finger_url},
139 {"gemini", "1965", load_gemini_url},
140 {"gopher", "70", load_gopher_url},
144 static imsg_handlerfn *handlers[] = {
145 [IMSG_ERR] = handle_imsg_err,
146 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
147 [IMSG_GOT_CODE] = handle_imsg_got_code,
148 [IMSG_GOT_META] = handle_imsg_got_meta,
149 [IMSG_BUF] = handle_imsg_buf,
150 [IMSG_EOF] = handle_imsg_eof,
151 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
152 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
153 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
154 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
157 static struct ohash certs;
159 static void __attribute__((__noreturn__))
166 tab_by_id(uint32_t id)
170 TAILQ_FOREACH(t, &tabshead, tabs) {
179 handle_imsg_err(struct imsg *imsg, size_t datalen)
184 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
188 page[datalen-1] = '\0';
190 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
191 tab->hist_cur->h, page) == -1)
193 load_page_from_str(tab, page);
198 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
200 const char *hash, *host, *port;
202 struct tofu_entry *e;
206 if (hash[datalen-1] != '\0')
209 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
212 if (tab->proxy != NULL) {
213 host = tab->proxy->host;
214 port = tab->proxy->port;
216 host = tab->uri.host;
217 port = tab->uri.port;
220 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
221 /* TODO: an update in libressl/libretls changed
222 * significantly. Find a better approach at storing
224 if (datalen > sizeof(e->hash))
227 tofu_res = 1; /* trust on first use */
228 if ((e = calloc(1, sizeof(*e))) == NULL)
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, hash, sizeof(e->hash));
237 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
239 tofu_res = !strcmp(hash, e->hash);
242 if (e->verified == -1)
243 tab->trust = TS_TEMP_TRUSTED;
244 else if (e->verified == 1)
245 tab->trust = TS_VERIFIED;
247 tab->trust = TS_TRUSTED;
249 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
250 &tofu_res, sizeof(tofu_res));
252 tab->trust = TS_UNTRUSTED;
253 load_page_from_str(tab, "# Certificate mismatch\n");
254 if ((tab->cert = strdup(hash)) == NULL)
256 ui_yornp("Certificate mismatch. Proceed?",
257 handle_check_cert_user_choice, tab);
262 handle_check_cert_user_choice(int accept, struct tab *tab)
264 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
269 * trust the certificate for this session only. If
270 * the page results in a redirect while we're asking
271 * the user to save, we'll end up with an invalid
272 * tabid (one request == one tab id). It also makes
273 * sense to save it for the current session if the
276 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port,
280 ui_yornp("Save the new certificate?",
281 handle_maybe_save_new_cert, tab);
283 message("Certificate temporarly trusted");
291 handle_maybe_save_new_cert(int accept, struct tab *tab)
293 struct tofu_entry *e;
294 const char *host, *port;
296 if (tab->proxy != NULL) {
297 host = tab->proxy->host;
298 port = tab->proxy->port;
300 host = tab->uri.host;
301 port = tab->uri.port;
307 if ((e = calloc(1, sizeof(*e))) == NULL)
310 strlcpy(e->domain, host, sizeof(e->domain));
311 if (*port != '\0' && strcmp(port, "1965")) {
312 strlcat(e->domain, ":", sizeof(e->domain));
313 strlcat(e->domain, port, sizeof(e->domain));
315 strlcpy(e->hash, tab->cert, sizeof(e->hash));
316 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
318 tofu_update(&certs, e);
320 tab->trust = TS_TRUSTED;
328 normalize_code(int n)
331 if (n == 10 || n == 11)
337 if (n == 30 || n == 31)
345 if (n <= 53 || n == 59)
353 return MALFORMED_RESPONSE;
357 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
361 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
364 if (sizeof(tab->code) != datalen)
367 memcpy(&tab->code, imsg->data, sizeof(tab->code));
368 tab->code = normalize_code(tab->code);
369 if (tab->code != 30 && tab->code != 31)
370 tab->redirect_count = 0;
374 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
379 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
382 if (sizeof(tab->meta) <= datalen)
385 memcpy(tab->meta, imsg->data, datalen);
387 if (tab->code < 10) { /* internal errors */
388 load_page_from_str(tab, err_pages[tab->code]);
389 } else if (tab->code < 20) { /* 1x */
390 load_page_from_str(tab, err_pages[tab->code]);
391 ui_require_input(tab, tab->code == 11, PROTO_GEMINI);
392 } else if (tab->code == 20) {
393 if (setup_parser_for(tab)) {
394 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
395 } else if (safe_mode) {
396 load_page_from_str(tab,
397 err_pages[UNKNOWN_TYPE_OR_CSET]);
401 if ((h = hist_pop(&tab->hist)) != NULL)
403 snprintf(buf, sizeof(buf),
404 "Can't display \"%s\", save it?", tab->meta);
405 ui_yornp(buf, handle_maybe_save_page, tab);
407 } else if (tab->code < 40) { /* 3x */
408 tab->redirect_count++;
410 /* TODO: make customizable? */
411 if (tab->redirect_count > 5) {
412 load_page_from_str(tab,
413 err_pages[TOO_MUCH_REDIRECTS]);
415 do_load_url(tab, tab->meta, NULL);
416 } else { /* 4x, 5x & 6x */
417 load_page_from_str(tab, err_pages[tab->code]);
422 handle_maybe_save_page(int dosave, struct tab *tab)
425 char input[PATH_MAX];
427 /* XXX: this print a message that is confusing */
428 ui_on_tab_loaded(tab);
435 if ((f = strrchr(tab->uri.path, '/')) == NULL)
440 strlcpy(input, download_path, sizeof(input));
441 strlcat(input, f, sizeof(input));
443 ui_read("Save to path", handle_save_page_path, tab, input);
447 handle_save_page_path(const char *path, struct tab *tab)
454 ui_show_downloads_pane();
456 enqueue_download(tab->id, path);
457 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
460 * Change this tab id, the old one is associated with the
463 tab->id = tab_new_id();
467 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
473 * There are no reason we shouldn't be able to find the
476 if ((d = download_by_id(imsg->hdr.peerid)) == NULL)
479 if (imsg->fd == -1) {
481 if (e[datalen-1] != '\0')
483 message("Can't open file %s: %s", d->path, e);
486 ui_send_net(IMSG_PROCEED, d->id, NULL, 0);
491 handle_imsg_buf(struct imsg *imsg, size_t datalen)
493 struct tab *tab = NULL;
494 struct download *d = NULL;
496 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
497 (d = download_by_id(imsg->hdr.peerid)) == NULL)
501 if (!parser_parse(tab, imsg->data, datalen))
503 ui_on_tab_refresh(tab);
506 write(d->fd, imsg->data, datalen);
507 ui_on_download_refresh();
512 handle_imsg_eof(struct imsg *imsg, size_t datalen)
514 struct tab *tab = NULL;
515 struct download *d = NULL;
517 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
518 (d = download_by_id(imsg->hdr.peerid)) == NULL)
522 if (!parser_free(tab))
524 ui_on_tab_refresh(tab);
525 ui_on_tab_loaded(tab);
529 ui_on_download_refresh();
534 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
538 if (datalen != sizeof(res))
541 memcpy(&res, imsg->data, sizeof(res));
543 message("Added to bookmarks!");
545 message("Failed to add to bookmarks: %s",
550 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
554 if (datalen != sizeof(res))
556 memcpy(&res, imsg->data, datalen);
558 message("Failed to save the cert for: %s",
563 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
567 if (datalen != sizeof(res))
569 memcpy(&res, imsg->data, datalen);
571 message("Failed to update the certificate");
575 handle_dispatch_imsg(int fd, short ev, void *d)
577 struct imsgev *iev = d;
579 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
580 err(1, "connection closed");
584 load_about_url(struct tab *tab, const char *url)
586 tab->trust = TS_UNKNOWN;
587 parser_init(tab, gemtext_initparser);
588 return make_fs_request(tab, IMSG_GET, url);
592 load_file_url(struct tab *tab, const char *url)
594 tab->trust = TS_UNKNOWN;
595 return make_fs_request(tab, IMSG_GET_FILE, tab->uri.path);
599 load_finger_url(struct tab *tab, const char *url)
605 memset(&req, 0, sizeof(req));
606 strlcpy(req.port, tab->uri.port, sizeof(req.port));
609 * Sometimes the finger url have the user as path component
610 * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
611 * userinfo (e.g. finger://cobradile@finger.farm).
613 if ((at = strchr(tab->uri.host, '@')) != NULL) {
614 len = at - tab->uri.host;
615 memcpy(req.req, tab->uri.host, len);
617 if (len >= sizeof(req.req))
621 strlcpy(req.host, at+1, sizeof(req.host));
623 strlcpy(req.host, tab->uri.host, sizeof(req.host));
625 /* +1 to skip the initial `/' */
626 strlcpy(req.req, tab->uri.path+1, sizeof(req.req));
628 strlcat(req.req, "\r\n", sizeof(req.req));
630 parser_init(tab, textplain_initparser);
631 return make_request(tab, &req, PROTO_FINGER, NULL);
635 load_gemini_url(struct tab *tab, const char *url)
639 memset(&req, 0, sizeof(req));
640 strlcpy(req.host, tab->uri.host, sizeof(req.host));
641 strlcpy(req.port, tab->uri.port, sizeof(req.host));
643 return make_request(tab, &req, PROTO_GEMINI, tab->hist_cur->h);
646 static inline const char *
647 gopher_skip_selector(const char *path, int *ret_type)
651 if (!strcmp(path, "/") || *path == '\0') {
660 switch (*ret_type = *path) {
676 load_gopher_url(struct tab *tab, const char *url)
682 memset(&req, 0, sizeof(req));
683 strlcpy(req.host, tab->uri.host, sizeof(req.host));
684 strlcpy(req.port, tab->uri.port, sizeof(req.host));
686 path = gopher_skip_selector(tab->uri.path, &type);
689 parser_init(tab, textplain_initparser);
692 parser_init(tab, gophermap_initparser);
695 ui_require_input(tab, 0, PROTO_GOPHER);
696 return load_page_from_str(tab, err_pages[10]);
698 return load_page_from_str(tab, "Unknown gopher selector");
701 strlcpy(req.req, path, sizeof(req.req));
702 if (*tab->uri.query != '\0') {
703 strlcat(req.req, "?", sizeof(req.req));
704 strlcat(req.req, tab->uri.query, sizeof(req.req));
706 strlcat(req.req, "\r\n", sizeof(req.req));
708 return make_request(tab, &req, PROTO_GOPHER, NULL);
712 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
716 memset(&req, 0, sizeof(req));
717 strlcpy(req.host, p->host, sizeof(req.host));
718 strlcpy(req.port, p->port, sizeof(req.host));
722 return make_request(tab, &req, p->proto, tab->hist_cur->h);
726 make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
729 tab->id = tab_new_id();
733 strlcpy(req->req, r, sizeof(req->req));
734 strlcat(req->req, "\r\n", sizeof(req->req));
737 ui_send_net(IMSG_GET_RAW, tab->id, req, sizeof(*req));
740 * So the various load_*_url can `return make_request` and
741 * do_load_url is happy.
747 make_fs_request(struct tab *tab, int type, const char *r)
750 tab->id = tab_new_id();
752 ui_send_fs(type, tab->id, r, strlen(r)+1);
755 * So load_{about,file}_url can `return make_fs_request` and
756 * do_load_url is happy.
762 gopher_send_search_req(struct tab *tab, const char *text)
766 start_loading_anim(tab);
768 memset(&req, 0, sizeof(req));
769 strlcpy(req.host, tab->uri.host, sizeof(req.host));
770 strlcpy(req.port, tab->uri.port, sizeof(req.host));
773 strlcpy(req.req, tab->uri.path+2, sizeof(req.req));
774 if (*tab->uri.query != '\0') {
775 strlcat(req.req, "?", sizeof(req.req));
776 strlcat(req.req, tab->uri.query, sizeof(req.req));
779 strlcat(req.req, "\t", sizeof(req.req));
780 strlcat(req.req, text, sizeof(req.req));
781 strlcat(req.req, "\r\n", sizeof(req.req));
783 erase_buffer(&tab->buffer);
784 parser_init(tab, gophermap_initparser);
786 make_request(tab, &req, PROTO_GOPHER, NULL);
790 * Effectively load the given url in the given tab. Return 1 when
791 * loading the page asynchronously, and thus when an erase_buffer can
792 * be done right after this function return, or 0 when loading the
793 * page synchronously.
796 do_load_url(struct tab *tab, const char *url, const char *base)
805 tab->trust = TS_UNKNOWN;
808 memcpy(&uri, &tab->uri, sizeof(tab->uri));
810 phos_parse_absolute_uri(base, &uri);
812 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
813 if (asprintf(&t, "#error loading %s\n>%s\n",
814 url, "Can't parse the URI") == -1)
816 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
817 load_page_from_str(tab, t);
822 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
823 sizeof(tab->hist_cur->h));
825 for (p = protos; p->schema != NULL; ++p) {
826 if (!strcmp(tab->uri.scheme, p->schema)) {
828 if (*tab->uri.port == '\0' && p->port != NULL)
829 strlcpy(tab->uri.port, p->port,
830 sizeof(tab->uri.port));
832 return p->loadfn(tab, url);
836 TAILQ_FOREACH(proxy, &proxies, proxies) {
837 if (!strcmp(tab->uri.scheme, proxy->match_proto))
838 return load_via_proxy(tab, url, proxy);
841 return load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
845 * Load url in tab. If tab is marked as lazy, only prepare the url
846 * but don't load it. If tab is lazy and a url was already prepared,
850 load_url(struct tab *tab, const char *url, const char *base, int nohist)
854 lazy = tab->flags & TAB_LAZY;
856 if (lazy && tab->hist_cur != NULL) {
858 tab->flags &= ~TAB_LAZY;
861 /* can't have both nohist and lazy at the same time. */
865 if (!nohist && (!lazy || tab->hist_cur == NULL)) {
866 if (tab->hist_cur != NULL)
867 hist_clear_forward(&tab->hist,
868 TAILQ_NEXT(tab->hist_cur, entries));
870 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur)))
876 strlcpy(tab->buffer.page.title, url,
877 sizeof(tab->buffer.page.title));
878 hist_push(&tab->hist, tab->hist_cur);
881 strlcpy(tab->hist_cur->h, url,
882 sizeof(tab->hist_cur->h));
886 do_load_url(tab, url, base);
890 load_url_in_tab(struct tab *tab, const char *url, const char *base, int nohist)
893 load_url(tab, url, base, nohist);
899 message("Loading %s...", url);
900 start_loading_anim(tab);
901 load_url(tab, url, base, nohist);
905 load_previous_page(struct tab *tab)
909 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
912 do_load_url(tab, h->h, NULL);
917 load_next_page(struct tab *tab)
921 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
924 do_load_url(tab, h->h, NULL);
929 add_to_bookmarks(const char *str)
931 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
936 * Given a user-entered URL, apply some heuristics to use it:
938 * - if it's a proper url use it
939 * - if it starts with a `./' or a `/' assume its a file:// url
940 * - assume it's a gemini:// url
942 * `ret' (of which len is the size) will be filled with the resulting
946 humanify_url(const char *raw, char *ret, size_t len)
951 if (phos_parse_absolute_uri(raw, &uri)) {
952 strlcpy(ret, raw, len);
956 if (has_prefix(raw, "./")) {
957 strlcpy(ret, "file://", len);
958 getcwd(buf, sizeof(buf));
959 strlcat(ret, buf, len);
960 strlcat(ret, "/", len);
961 strlcat(ret, raw+2, len);
966 strlcpy(ret, "file://", len);
967 strlcat(ret, raw, len);
971 strlcpy(ret, "gemini://", len);
972 strlcat(ret, raw, len);
976 start_child(enum telescope_process p, const char *argv0, int fd)
982 switch (pid = fork()) {
992 if (dup2(fd, 3) == -1)
993 err(1, "cannot setup imsg fd");
995 argv[argc++] = argv0;
998 errx(1, "Can't start ui process");
1000 argv[argc++] = "-Tf";
1003 argv[argc++] = "-Tn";
1008 argv[argc++] = "-S";
1010 argv[argc++] = NULL;
1011 execvp(argv0, (char *const *)argv);
1012 err(1, "execvp(%s)", argv0);
1016 ui_send_net(int type, uint32_t peerid, const void *data,
1019 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
1024 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
1026 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
1030 static void __attribute__((noreturn))
1033 fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1035 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1040 main(int argc, char * const *argv)
1042 struct imsgev net_ibuf, fs_ibuf;
1044 int pipe2net[2], pipe2fs[2];
1045 int ch, configtest = 0, fail = 0;
1050 char url[GEMINI_URL_LEN+1];
1055 signal(SIGCHLD, SIG_IGN);
1056 signal(SIGPIPE, SIG_IGN);
1058 if (getenv("NO_COLOR") != NULL)
1063 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1066 exit(ui_print_colors());
1069 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_FS)
1109 else if (proc == PROC_NET)
1117 humanify_url(argv[0], url, sizeof(url));
1120 /* setup keys before reading the config */
1121 TAILQ_INIT(&global_map.m);
1122 global_map.unhandled_input = global_key_unbound;
1123 TAILQ_INIT(&minibuffer_map.m);
1126 parseconfig(config_path, fail);
1132 if (download_path == NULL &&
1133 (download_path = strdup("/tmp/")) == NULL)
1136 if (!safe_mode && (sessionfd = lock_session()) == -1)
1137 errx(1, "can't lock session, is another instance of "
1138 "telescope already running?");
1140 /* Start children. */
1141 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
1142 err(1, "socketpair");
1143 start_child(PROC_FS, argv0, pipe2fs[1]);
1144 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
1146 iev_fs->handler = handle_dispatch_imsg;
1148 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1149 err(1, "socketpair");
1150 start_child(PROC_NET, argv0, pipe2net[1]);
1151 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1152 iev_net = &net_ibuf;
1153 iev_net->handler = handle_dispatch_imsg;
1157 /* initialize tofu & load certificates */
1158 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1163 /* Setup event handler for the autosave */
1166 /* Setup event handlers for pipes to fs/net */
1167 iev_fs->events = EV_READ;
1168 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
1169 iev_fs->handler, iev_fs);
1170 event_add(&iev_fs->ev, NULL);
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 load_last_session();
1179 if (has_url || TAILQ_EMPTY(&tabshead))
1180 new_tab(url, NULL, NULL);
1182 sandbox_ui_process();
1188 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
1189 ui_send_net(IMSG_QUIT, 0, NULL, 0);
1190 imsg_flush(&iev_fs->ibuf);
1191 imsg_flush(&iev_net->ibuf);
1193 /* wait for children to terminate */
1195 pid = wait(&status);
1197 if (errno != EINTR && errno != ECHILD)
1199 } else if (WIFSIGNALED(status))
1200 warnx("child terminated; signal %d", WTERMSIG(status));
1201 } while (pid != -1 || (pid == -1 && errno == EINTR));
1203 if (!safe_mode && close(sessionfd) == -1)
1204 err(1, "close(sessionfd = %d)", sessionfd);