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 {"colours", no_argument, NULL, 'C'},
41 {"help", no_argument, NULL, 'h'},
42 {"safe", no_argument, NULL, 'S'},
43 {"version", no_argument, NULL, 'v'},
47 static const char *opts = "Cc:hnST:v";
50 static char url[GEMINI_URL_LEN];
53 * Used to know when we're finished loading.
58 * "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
59 * anything to the filesystem or execute external programs.
63 static struct imsgev *iev_fs, *iev_net;
65 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
66 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
68 enum telescope_process {
74 #define CANNOT_FETCH 0
75 #define TOO_MUCH_REDIRECTS 1
76 #define MALFORMED_RESPONSE 2
77 #define UNKNOWN_TYPE_OR_CSET 3
78 #define UNKNOWN_PROTOCOL 4
80 /* XXX: keep in sync with normalize_code */
81 const char *err_pages[70] = {
82 [CANNOT_FETCH] = "# Couldn't load the page\n",
83 [TOO_MUCH_REDIRECTS] = "# Too much redirects\n",
84 [MALFORMED_RESPONSE] = "# Got a malformed response\n",
85 [UNKNOWN_TYPE_OR_CSET] = "# Unsupported type or charset\n",
86 [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
88 [10] = "# Input required\n",
89 [11] = "# Input required\n",
90 [40] = "# Temporary failure\n",
91 [41] = "# Server unavailable\n",
92 [42] = "# CGI error\n",
93 [43] = "# Proxy error\n",
94 [44] = "# Slow down\n",
95 [50] = "# Permanent failure\n",
96 [51] = "# Not found\n",
98 [53] = "# Proxy request refused\n",
99 [59] = "# Bad request\n",
100 [60] = "# Client certificate required\n",
101 [61] = "# Certificate not authorised\n",
102 [62] = "# Certificate not valid\n"
105 static void die(void) __attribute__((__noreturn__));
106 static struct tab *tab_by_id(uint32_t);
107 static void handle_imsg_err(struct imsg *, size_t);
108 static void handle_imsg_check_cert(struct imsg *, size_t);
109 static void handle_check_cert_user_choice(int, struct tab *);
110 static void handle_maybe_save_new_cert(int, struct tab *);
111 static void handle_imsg_got_code(struct imsg *, size_t);
112 static void handle_imsg_got_meta(struct imsg *, size_t);
113 static void handle_maybe_save_page(int, struct tab *);
114 static void handle_save_page_path(const char *, struct tab *);
115 static void handle_imsg_file_opened(struct imsg *, size_t);
116 static void handle_imsg_buf(struct imsg *, size_t);
117 static void handle_imsg_eof(struct imsg *, size_t);
118 static void handle_imsg_tofu(struct imsg *, size_t);
119 static void handle_imsg_bookmark_ok(struct imsg *, size_t);
120 static void handle_imsg_save_cert_ok(struct imsg *, size_t);
121 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
122 static void handle_imsg_session(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 make_fs_request(struct tab *, int, const char *);
134 static int do_load_url(struct tab *, const char *, const char *);
135 static pid_t start_child(enum telescope_process, const char *, int);
137 static 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,
157 [IMSG_TOFU] = handle_imsg_tofu,
158 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
159 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
160 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
161 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
162 [IMSG_SESSION_TAB] = handle_imsg_session,
163 [IMSG_SESSION_TAB_HIST] = handle_imsg_session,
164 [IMSG_SESSION_END] = handle_imsg_session,
167 static struct ohash certs;
169 static void __attribute__((__noreturn__))
176 tab_by_id(uint32_t id)
180 TAILQ_FOREACH(t, &tabshead, tabs) {
189 handle_imsg_err(struct imsg *imsg, size_t datalen)
194 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
198 page[datalen-1] = '\0';
200 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
201 tab->hist_cur->h, page) == -1)
203 load_page_from_str(tab, page);
208 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
210 const char *hash, *host, *port;
212 struct tofu_entry *e;
216 if (hash[datalen-1] != '\0')
219 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
222 if (tab->proxy != NULL) {
223 host = tab->proxy->host;
224 port = tab->proxy->port;
226 host = tab->uri.host;
227 port = tab->uri.port;
230 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
231 /* TODO: an update in libressl/libretls changed
232 * significantly. Find a better approach at storing
234 if (datalen > sizeof(e->hash))
237 tofu_res = 1; /* trust on first use */
238 if ((e = calloc(1, sizeof(*e))) == NULL)
240 strlcpy(e->domain, host, sizeof(e->domain));
241 if (*port != '\0' && strcmp(port, "1965")) {
242 strlcat(e->domain, ":", sizeof(e->domain));
243 strlcat(e->domain, port, sizeof(e->domain));
245 strlcpy(e->hash, hash, sizeof(e->hash));
247 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
249 tofu_res = !strcmp(hash, e->hash);
252 if (e->verified == -1)
253 tab->trust = TS_TEMP_TRUSTED;
254 else if (e->verified == 1)
255 tab->trust = TS_VERIFIED;
257 tab->trust = TS_TRUSTED;
259 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
260 &tofu_res, sizeof(tofu_res));
262 tab->trust = TS_UNTRUSTED;
263 load_page_from_str(tab, "# Certificate mismatch\n");
264 if ((tab->cert = strdup(hash)) == NULL)
266 ui_yornp("Certificate mismatch. Proceed?",
267 handle_check_cert_user_choice, tab);
272 handle_check_cert_user_choice(int accept, struct tab *tab)
274 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
279 * trust the certificate for this session only. If
280 * the page results in a redirect while we're asking
281 * the user to save, we'll end up with an invalid
282 * tabid (one request == one tab id). It also makes
283 * sense to save it for the current session if the
286 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port,
290 ui_yornp("Save the new certificate?",
291 handle_maybe_save_new_cert, tab);
293 message("Certificate temporarly trusted");
301 handle_maybe_save_new_cert(int accept, struct tab *tab)
303 struct tofu_entry *e;
304 const char *host, *port;
306 if (tab->proxy != NULL) {
307 host = tab->proxy->host;
308 port = tab->proxy->port;
310 host = tab->uri.host;
311 port = tab->uri.port;
317 if ((e = calloc(1, sizeof(*e))) == NULL)
320 strlcpy(e->domain, host, sizeof(e->domain));
321 if (*port != '\0' && strcmp(port, "1965")) {
322 strlcat(e->domain, ":", sizeof(e->domain));
323 strlcat(e->domain, port, sizeof(e->domain));
325 strlcpy(e->hash, tab->cert, sizeof(e->hash));
326 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
328 tofu_update(&certs, e);
330 tab->trust = TS_TRUSTED;
338 normalize_code(int n)
341 if (n == 10 || n == 11)
347 if (n == 30 || n == 31)
355 if (n <= 53 || n == 59)
363 return MALFORMED_RESPONSE;
367 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
371 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
374 if (sizeof(tab->code) != datalen)
377 memcpy(&tab->code, imsg->data, sizeof(tab->code));
378 tab->code = normalize_code(tab->code);
379 if (tab->code != 30 && tab->code != 31)
380 tab->redirect_count = 0;
384 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
389 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
392 if (sizeof(tab->meta) <= datalen)
395 memcpy(tab->meta, imsg->data, datalen);
397 if (tab->code < 10) { /* internal errors */
398 load_page_from_str(tab, err_pages[tab->code]);
399 } else if (tab->code < 20) { /* 1x */
400 load_page_from_str(tab, err_pages[tab->code]);
401 ui_require_input(tab, tab->code == 11, PROTO_GEMINI);
402 } else if (tab->code == 20) {
403 if (setup_parser_for(tab)) {
404 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
405 } else if (safe_mode) {
406 load_page_from_str(tab,
407 err_pages[UNKNOWN_TYPE_OR_CSET]);
411 if ((h = hist_pop(&tab->hist)) != NULL)
413 snprintf(buf, sizeof(buf),
414 "Can't display \"%s\", save it?", tab->meta);
415 ui_yornp(buf, handle_maybe_save_page, tab);
417 } else if (tab->code < 40) { /* 3x */
418 tab->redirect_count++;
420 /* TODO: make customizable? */
421 if (tab->redirect_count > 5) {
422 load_page_from_str(tab,
423 err_pages[TOO_MUCH_REDIRECTS]);
425 do_load_url(tab, tab->meta, NULL);
426 } else { /* 4x, 5x & 6x */
427 load_page_from_str(tab, err_pages[tab->code]);
432 handle_maybe_save_page(int dosave, struct tab *tab)
435 char input[PATH_MAX];
437 /* XXX: this print a message that is confusing */
438 ui_on_tab_loaded(tab);
445 if ((f = strrchr(tab->uri.path, '/')) == NULL)
450 strlcpy(input, download_path, sizeof(input));
451 strlcat(input, f, sizeof(input));
453 ui_read("Save to path", handle_save_page_path, tab, input);
457 handle_save_page_path(const char *path, struct tab *tab)
464 ui_show_downloads_pane();
466 enqueue_download(tab->id, path);
467 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
470 * Change this tab id, the old one is associated with the
473 tab->id = tab_new_id();
477 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
483 * There are no reason we shouldn't be able to find the
486 if ((d = download_by_id(imsg->hdr.peerid)) == NULL)
489 if (imsg->fd == -1) {
491 if (e[datalen-1] != '\0')
493 message("Can't open file %s: %s", d->path, e);
496 ui_send_net(IMSG_PROCEED, d->id, NULL, 0);
501 handle_imsg_session(struct imsg *imsg, size_t datalen)
503 static struct tab *curr;
504 static struct tab *tab;
505 struct session_tab st;
506 struct session_tab_hist sth;
511 * The fs process tried to send tabs after it has announced
512 * that he's done. Something fishy is going on, better die.
517 switch (imsg->hdr.type) {
518 case IMSG_SESSION_TAB:
519 if (datalen != sizeof(st))
522 memcpy(&st, imsg->data, sizeof(st));
523 if ((tab = new_tab(st.uri, NULL, NULL)) == NULL)
525 strlcpy(tab->buffer.page.title, st.title,
526 sizeof(tab->buffer.page.title));
527 if (st.flags & TAB_CURRENT)
531 case IMSG_SESSION_TAB_HIST:
532 if (tab == NULL || datalen != sizeof(sth))
535 memcpy(&sth, imsg->data, sizeof(sth));
536 if (sth.uri[sizeof(sth.uri)-1] != '\0')
539 if ((h = calloc(1, sizeof(*h))) == NULL)
541 strlcpy(h->h, sth.uri, sizeof(h->h));
544 hist_push(&tab->hist, h);
546 hist_add_before(&tab->hist, tab->hist_cur, h);
549 case IMSG_SESSION_END:
550 if (datalen != sizeof(first_time))
552 memcpy(&first_time, imsg->data, sizeof(first_time));
554 new_tab("about:new", NULL, NULL);
555 curr = new_tab("about:help", NULL, NULL);
561 if (has_url || TAILQ_EMPTY(&tabshead))
562 new_tab(url, NULL, NULL);
572 handle_imsg_buf(struct imsg *imsg, size_t datalen)
574 struct tab *tab = NULL;
575 struct download *d = NULL;
577 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
578 (d = download_by_id(imsg->hdr.peerid)) == NULL)
582 if (!parser_parse(tab, imsg->data, datalen))
584 ui_on_tab_refresh(tab);
587 write(d->fd, imsg->data, datalen);
588 ui_on_download_refresh();
593 handle_imsg_eof(struct imsg *imsg, size_t datalen)
595 struct tab *tab = NULL;
596 struct download *d = NULL;
598 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
599 (d = download_by_id(imsg->hdr.peerid)) == NULL)
603 if (!parser_free(tab))
605 ui_on_tab_refresh(tab);
606 ui_on_tab_loaded(tab);
610 ui_on_download_refresh();
615 handle_imsg_tofu(struct imsg *imsg, size_t datalen)
617 struct tofu_entry *e;
622 if ((e = calloc(1, sizeof(*e))) == NULL)
625 if (datalen != sizeof(*e))
627 memcpy(e, imsg->data, sizeof(*e));
628 if (e->domain[sizeof(e->domain)-1] != '\0' ||
629 e->hash[sizeof(e->hash)-1] != '\0')
635 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
639 if (datalen != sizeof(res))
642 memcpy(&res, imsg->data, sizeof(res));
644 message("Added to bookmarks!");
646 message("Failed to add to bookmarks: %s",
651 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
655 if (datalen != sizeof(res))
657 memcpy(&res, imsg->data, datalen);
659 message("Failed to save the cert for: %s",
664 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
668 if (datalen != sizeof(res))
670 memcpy(&res, imsg->data, datalen);
672 message("Failed to update the certificate");
676 handle_dispatch_imsg(int fd, short ev, void *d)
678 struct imsgev *iev = d;
680 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
681 err(1, "connection closed");
685 load_about_url(struct tab *tab, const char *url)
687 tab->trust = TS_UNKNOWN;
688 parser_init(tab, gemtext_initparser);
689 return make_fs_request(tab, IMSG_GET, url);
693 load_file_url(struct tab *tab, const char *url)
695 tab->trust = TS_UNKNOWN;
696 return make_fs_request(tab, IMSG_GET_FILE, tab->uri.path);
700 load_finger_url(struct tab *tab, const char *url)
706 memset(&req, 0, sizeof(req));
707 strlcpy(req.port, tab->uri.port, sizeof(req.port));
710 * Sometimes the finger url have the user as path component
711 * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
712 * userinfo (e.g. finger://cobradile@finger.farm).
714 if ((at = strchr(tab->uri.host, '@')) != NULL) {
715 len = at - tab->uri.host;
716 memcpy(req.req, tab->uri.host, len);
718 if (len >= sizeof(req.req))
722 strlcpy(req.host, at+1, sizeof(req.host));
724 strlcpy(req.host, tab->uri.host, sizeof(req.host));
726 /* +1 to skip the initial `/' */
727 strlcpy(req.req, tab->uri.path+1, sizeof(req.req));
729 strlcat(req.req, "\r\n", sizeof(req.req));
731 parser_init(tab, textplain_initparser);
732 return make_request(tab, &req, PROTO_FINGER, NULL);
736 load_gemini_url(struct tab *tab, const char *url)
740 memset(&req, 0, sizeof(req));
741 strlcpy(req.host, tab->uri.host, sizeof(req.host));
742 strlcpy(req.port, tab->uri.port, sizeof(req.port));
744 return make_request(tab, &req, PROTO_GEMINI, tab->hist_cur->h);
747 static inline const char *
748 gopher_skip_selector(const char *path, int *ret_type)
752 if (!strcmp(path, "/") || *path == '\0') {
761 switch (*ret_type = *path) {
777 load_gopher_url(struct tab *tab, const char *url)
783 memset(&req, 0, sizeof(req));
784 strlcpy(req.host, tab->uri.host, sizeof(req.host));
785 strlcpy(req.port, tab->uri.port, sizeof(req.port));
787 path = gopher_skip_selector(tab->uri.path, &type);
790 parser_init(tab, textplain_initparser);
793 parser_init(tab, gophermap_initparser);
796 ui_require_input(tab, 0, PROTO_GOPHER);
797 return load_page_from_str(tab, err_pages[10]);
799 return load_page_from_str(tab, "Unknown gopher selector");
802 strlcpy(req.req, path, sizeof(req.req));
803 if (*tab->uri.query != '\0') {
804 strlcat(req.req, "?", sizeof(req.req));
805 strlcat(req.req, tab->uri.query, sizeof(req.req));
807 strlcat(req.req, "\r\n", sizeof(req.req));
809 return make_request(tab, &req, PROTO_GOPHER, NULL);
813 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
817 memset(&req, 0, sizeof(req));
818 strlcpy(req.host, p->host, sizeof(req.host));
819 strlcpy(req.port, p->port, sizeof(req.port));
823 return make_request(tab, &req, p->proto, tab->hist_cur->h);
827 make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
830 tab->id = tab_new_id();
834 strlcpy(req->req, r, sizeof(req->req));
835 strlcat(req->req, "\r\n", sizeof(req->req));
838 ui_send_net(IMSG_GET_RAW, tab->id, req, sizeof(*req));
841 * So the various load_*_url can `return make_request` and
842 * do_load_url is happy.
848 make_fs_request(struct tab *tab, int type, const char *r)
851 tab->id = tab_new_id();
853 ui_send_fs(type, tab->id, r, strlen(r)+1);
856 * So load_{about,file}_url can `return make_fs_request` and
857 * do_load_url is happy.
863 gopher_send_search_req(struct tab *tab, const char *text)
867 start_loading_anim(tab);
869 memset(&req, 0, sizeof(req));
870 strlcpy(req.host, tab->uri.host, sizeof(req.host));
871 strlcpy(req.port, tab->uri.port, sizeof(req.port));
874 strlcpy(req.req, tab->uri.path+2, sizeof(req.req));
875 if (*tab->uri.query != '\0') {
876 strlcat(req.req, "?", sizeof(req.req));
877 strlcat(req.req, tab->uri.query, sizeof(req.req));
880 strlcat(req.req, "\t", sizeof(req.req));
881 strlcat(req.req, text, sizeof(req.req));
882 strlcat(req.req, "\r\n", sizeof(req.req));
884 erase_buffer(&tab->buffer);
885 parser_init(tab, gophermap_initparser);
887 make_request(tab, &req, PROTO_GOPHER, NULL);
891 * Effectively load the given url in the given tab. Return 1 when
892 * loading the page asynchronously, and thus when an erase_buffer can
893 * be done right after this function return, or 0 when loading the
894 * page synchronously.
897 do_load_url(struct tab *tab, const char *url, const char *base)
906 tab->trust = TS_UNKNOWN;
909 memcpy(&uri, &tab->uri, sizeof(tab->uri));
911 phos_parse_absolute_uri(base, &uri);
913 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
914 if (asprintf(&t, "#error loading %s\n>%s\n",
915 url, "Can't parse the URI") == -1)
917 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
918 load_page_from_str(tab, t);
923 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
924 sizeof(tab->hist_cur->h));
926 for (p = protos; p->schema != NULL; ++p) {
927 if (!strcmp(tab->uri.scheme, p->schema)) {
929 if (*tab->uri.port == '\0' && p->port != NULL)
930 strlcpy(tab->uri.port, p->port,
931 sizeof(tab->uri.port));
933 return p->loadfn(tab, url);
937 TAILQ_FOREACH(proxy, &proxies, proxies) {
938 if (!strcmp(tab->uri.scheme, proxy->match_proto))
939 return load_via_proxy(tab, url, proxy);
942 return load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
946 * Load url in tab and handle history. If a tab is marked as lazy, only
947 * prepare the url but don't load it.
950 load_url(struct tab *tab, const char *url, const char *base, int nohist)
952 int lazy = tab->flags & TAB_LAZY;
954 if (!nohist && (!lazy || tab->hist_cur == NULL)) {
955 if (tab->hist_cur != NULL)
956 hist_clear_forward(&tab->hist,
957 TAILQ_NEXT(tab->hist_cur, entries));
959 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur)))
965 strlcpy(tab->buffer.page.title, url,
966 sizeof(tab->buffer.page.title));
967 hist_push(&tab->hist, tab->hist_cur);
970 strlcpy(tab->hist_cur->h, url,
971 sizeof(tab->hist_cur->h));
975 do_load_url(tab, url, base);
979 load_url_in_tab(struct tab *tab, const char *url, const char *base, int nohist)
982 load_url(tab, url, base, nohist);
988 message("Loading %s...", url);
989 start_loading_anim(tab);
990 load_url(tab, url, base, nohist);
994 load_previous_page(struct tab *tab)
998 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
1001 do_load_url(tab, h->h, NULL);
1006 load_next_page(struct tab *tab)
1010 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
1013 do_load_url(tab, h->h, NULL);
1018 add_to_bookmarks(const char *str)
1020 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
1021 str, strlen(str)+1);
1025 * Given a user-entered URL, apply some heuristics to use it:
1027 * - if it's a proper url use it
1028 * - if it starts with a `./' or a `/' assume its a file:// url
1029 * - assume it's a gemini:// url
1031 * `ret' (of which len is the size) will be filled with the resulting
1035 humanify_url(const char *raw, char *ret, size_t len)
1037 struct phos_uri uri;
1040 if (phos_parse_absolute_uri(raw, &uri)) {
1041 strlcpy(ret, raw, len);
1045 if (has_prefix(raw, "./")) {
1046 strlcpy(ret, "file://", len);
1047 getcwd(buf, sizeof(buf));
1048 strlcat(ret, buf, len);
1049 strlcat(ret, "/", len);
1050 strlcat(ret, raw+2, len);
1055 strlcpy(ret, "file://", len);
1056 strlcat(ret, raw, len);
1060 strlcpy(ret, "gemini://", len);
1061 strlcat(ret, raw, len);
1065 start_child(enum telescope_process p, const char *argv0, int fd)
1067 const char *argv[5];
1071 switch (pid = fork()) {
1081 if (dup2(fd, 3) == -1)
1082 err(1, "cannot setup imsg fd");
1084 argv[argc++] = argv0;
1087 errx(1, "Can't start ui process");
1089 argv[argc++] = "-Tf";
1092 argv[argc++] = "-Tn";
1097 argv[argc++] = "-S";
1099 argv[argc++] = NULL;
1100 execvp(argv0, (char *const *)argv);
1101 err(1, "execvp(%s)", argv0);
1105 ui_send_net(int type, uint32_t peerid, const void *data,
1108 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
1113 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
1115 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
1119 static void __attribute__((noreturn))
1122 fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1124 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1129 main(int argc, char * const *argv)
1131 struct imsgev net_ibuf, fs_ibuf;
1133 int pipe2net[2], pipe2fs[2];
1134 int ch, configtest = 0, fail = 0;
1142 signal(SIGCHLD, SIG_IGN);
1143 signal(SIGPIPE, SIG_IGN);
1145 if (getenv("NO_COLOR") != NULL)
1150 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1153 exit(ui_print_colors());
1156 strlcpy(config_path, optarg, sizeof(config_path));
1175 errx(1, "invalid process spec %c",
1180 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1194 else if (proc == PROC_FS)
1196 else if (proc == PROC_NET)
1204 humanify_url(argv[0], url, sizeof(url));
1207 /* setup keys before reading the config */
1208 TAILQ_INIT(&global_map.m);
1209 global_map.unhandled_input = global_key_unbound;
1210 TAILQ_INIT(&minibuffer_map.m);
1213 parseconfig(config_path, fail);
1219 if (download_path == NULL &&
1220 (download_path = strdup("/tmp/")) == NULL)
1223 if (!safe_mode && (sessionfd = lock_session()) == -1)
1224 errx(1, "can't lock session, is another instance of "
1225 "telescope already running?");
1227 /* Start children. */
1228 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
1229 err(1, "socketpair");
1230 start_child(PROC_FS, argv0, pipe2fs[1]);
1231 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
1233 iev_fs->handler = handle_dispatch_imsg;
1235 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1236 err(1, "socketpair");
1237 start_child(PROC_NET, argv0, pipe2net[1]);
1238 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1239 iev_net = &net_ibuf;
1240 iev_net->handler = handle_dispatch_imsg;
1244 /* initialize tofu store */
1245 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1249 /* Setup event handler for the autosave */
1252 /* Setup event handlers for pipes to fs/net */
1253 iev_fs->events = EV_READ;
1254 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
1255 iev_fs->handler, iev_fs);
1256 event_add(&iev_fs->ev, NULL);
1258 iev_net->events = EV_READ;
1259 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
1260 iev_net->handler, iev_net);
1261 event_add(&iev_net->ev, NULL);
1264 sandbox_ui_process();
1265 ui_send_fs(IMSG_INIT, 0, NULL, 0);
1270 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
1271 ui_send_net(IMSG_QUIT, 0, NULL, 0);
1272 imsg_flush(&iev_fs->ibuf);
1273 imsg_flush(&iev_net->ibuf);
1275 /* wait for children to terminate */
1277 pid = wait(&status);
1279 if (errno != EINTR && errno != ECHILD)
1281 } else if (WIFSIGNALED(status))
1282 warnx("child terminated; signal %d", WTERMSIG(status));
1283 } while (pid != -1 || (pid == -1 && errno == EINTR));
1285 if (!safe_mode && close(sessionfd) == -1)
1286 err(1, "close(sessionfd = %d)", sessionfd);