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>
35 #include "minibuffer.h"
38 #include "telescope.h"
42 static const struct option longopts[] = {
43 {"colors", no_argument, NULL, 'C'},
44 {"colours", no_argument, NULL, 'C'},
45 {"help", no_argument, NULL, 'h'},
46 {"safe", no_argument, NULL, 'S'},
47 {"version", no_argument, NULL, 'v'},
51 static const char *opts = "Cc:hnST:v";
54 static char url[GEMINI_URL_LEN];
57 * Used to know when we're finished loading.
62 * "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
63 * anything to the filesystem or execute external programs.
67 static struct imsgev *iev_fs, *iev_net;
69 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
70 struct tabshead ktabshead = TAILQ_HEAD_INITIALIZER(ktabshead);
71 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
73 enum telescope_process {
79 #define CANNOT_FETCH 0
80 #define TOO_MUCH_REDIRECTS 1
81 #define MALFORMED_RESPONSE 2
82 #define UNKNOWN_TYPE_OR_CSET 3
83 #define UNKNOWN_PROTOCOL 4
85 /* XXX: keep in sync with normalize_code */
86 const char *err_pages[70] = {
87 [CANNOT_FETCH] = "# Couldn't load the page\n",
88 [TOO_MUCH_REDIRECTS] = "# Too much redirects\n",
89 [MALFORMED_RESPONSE] = "# Got a malformed response\n",
90 [UNKNOWN_TYPE_OR_CSET] = "# Unsupported type or charset\n",
91 [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
93 [10] = "# Input required\n",
94 [11] = "# Input required\n",
95 [40] = "# Temporary failure\n",
96 [41] = "# Server unavailable\n",
97 [42] = "# CGI error\n",
98 [43] = "# Proxy error\n",
99 [44] = "# Slow down\n",
100 [50] = "# Permanent failure\n",
101 [51] = "# Not found\n",
103 [53] = "# Proxy request refused\n",
104 [59] = "# Bad request\n",
105 [60] = "# Client certificate required\n",
106 [61] = "# Certificate not authorised\n",
107 [62] = "# Certificate not valid\n"
110 static void die(void) __attribute__((__noreturn__));
111 static struct tab *tab_by_id(uint32_t);
112 static void handle_imsg_err(struct imsg *, size_t);
113 static void handle_imsg_check_cert(struct imsg *, size_t);
114 static void handle_check_cert_user_choice(int, struct tab *);
115 static void handle_maybe_save_new_cert(int, struct tab *);
116 static void handle_imsg_got_code(struct imsg *, size_t);
117 static void handle_imsg_got_meta(struct imsg *, size_t);
118 static void handle_maybe_save_page(int, struct tab *);
119 static void handle_save_page_path(const char *, struct tab *);
120 static void handle_imsg_file_opened(struct imsg *, size_t);
121 static void handle_imsg_buf(struct imsg *, size_t);
122 static void handle_imsg_eof(struct imsg *, size_t);
123 static void handle_imsg_tofu(struct imsg *, size_t);
124 static void handle_imsg_bookmark_ok(struct imsg *, size_t);
125 static void handle_imsg_save_cert_ok(struct imsg *, size_t);
126 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
127 static void handle_imsg_session(struct imsg *, size_t);
128 static void handle_imsg_history(struct imsg *, size_t);
129 static void handle_dispatch_imsg(int, short, void *);
130 static int load_about_url(struct tab *, const char *);
131 static int load_file_url(struct tab *, const char *);
132 static int load_finger_url(struct tab *, const char *);
133 static int load_gemini_url(struct tab *, const char *);
134 static int load_gopher_url(struct tab *, const char *);
135 static int load_via_proxy(struct tab *, const char *,
137 static int make_request(struct tab *, struct get_req *, int,
139 static int make_fs_request(struct tab *, int, const char *);
140 static int do_load_url(struct tab *, const char *, const char *, int);
141 static pid_t start_child(enum telescope_process, const char *, int);
142 static void send_url(const char *);
144 static const struct proto {
147 int (*loadfn)(struct tab *, const char *);
149 {"about", NULL, load_about_url},
150 {"file", NULL, load_file_url},
151 {"finger", "79", load_finger_url},
152 {"gemini", "1965", load_gemini_url},
153 {"gopher", "70", load_gopher_url},
157 static imsg_handlerfn *handlers[] = {
158 [IMSG_ERR] = handle_imsg_err,
159 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
160 [IMSG_GOT_CODE] = handle_imsg_got_code,
161 [IMSG_GOT_META] = handle_imsg_got_meta,
162 [IMSG_BUF] = handle_imsg_buf,
163 [IMSG_EOF] = handle_imsg_eof,
164 [IMSG_TOFU] = handle_imsg_tofu,
165 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
166 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
167 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
168 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
169 [IMSG_SESSION_TAB] = handle_imsg_session,
170 [IMSG_SESSION_TAB_HIST] = handle_imsg_session,
171 [IMSG_SESSION_END] = handle_imsg_session,
172 [IMSG_HIST_ITEM] = handle_imsg_history,
173 [IMSG_HIST_END] = handle_imsg_history,
176 static struct ohash certs;
178 static void __attribute__((__noreturn__))
185 tab_by_id(uint32_t id)
189 TAILQ_FOREACH(t, &tabshead, tabs) {
198 handle_imsg_err(struct imsg *imsg, size_t datalen)
203 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
207 page[datalen-1] = '\0';
209 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
210 tab->hist_cur->h, page) == -1)
212 load_page_from_str(tab, page);
217 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
219 const char *hash, *host, *port;
221 struct tofu_entry *e;
225 if (hash[datalen-1] != '\0')
228 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
231 if (tab->proxy != NULL) {
232 host = tab->proxy->host;
233 port = tab->proxy->port;
235 host = tab->uri.host;
236 port = tab->uri.port;
239 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
240 /* TODO: an update in libressl/libretls changed
241 * significantly. Find a better approach at storing
243 if (datalen > sizeof(e->hash))
246 tofu_res = 1; /* trust on first use */
247 if ((e = calloc(1, sizeof(*e))) == NULL)
249 strlcpy(e->domain, host, sizeof(e->domain));
250 if (*port != '\0' && strcmp(port, "1965")) {
251 strlcat(e->domain, ":", sizeof(e->domain));
252 strlcat(e->domain, port, sizeof(e->domain));
254 strlcpy(e->hash, hash, sizeof(e->hash));
256 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
258 tofu_res = !strcmp(hash, e->hash);
261 if (e->verified == -1)
262 tab->trust = TS_TEMP_TRUSTED;
263 else if (e->verified == 1)
264 tab->trust = TS_VERIFIED;
266 tab->trust = TS_TRUSTED;
268 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
269 &tofu_res, sizeof(tofu_res));
271 tab->trust = TS_UNTRUSTED;
272 load_page_from_str(tab, "# Certificate mismatch\n");
273 if ((tab->cert = strdup(hash)) == NULL)
275 ui_yornp("Certificate mismatch. Proceed?",
276 handle_check_cert_user_choice, tab);
281 handle_check_cert_user_choice(int accept, struct tab *tab)
283 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
288 * trust the certificate for this session only. If
289 * the page results in a redirect while we're asking
290 * the user to save, we'll end up with an invalid
291 * tabid (one request == one tab id). It also makes
292 * sense to save it for the current session if the
295 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port,
299 ui_yornp("Save the new certificate?",
300 handle_maybe_save_new_cert, tab);
302 message("Certificate temporarly trusted");
310 handle_maybe_save_new_cert(int accept, struct tab *tab)
312 struct tofu_entry *e;
313 const char *host, *port;
315 if (tab->proxy != NULL) {
316 host = tab->proxy->host;
317 port = tab->proxy->port;
319 host = tab->uri.host;
320 port = tab->uri.port;
326 if ((e = calloc(1, sizeof(*e))) == NULL)
329 strlcpy(e->domain, host, sizeof(e->domain));
330 if (*port != '\0' && strcmp(port, "1965")) {
331 strlcat(e->domain, ":", sizeof(e->domain));
332 strlcat(e->domain, port, sizeof(e->domain));
334 strlcpy(e->hash, tab->cert, sizeof(e->hash));
335 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
337 tofu_update(&certs, e);
339 tab->trust = TS_TRUSTED;
347 normalize_code(int n)
350 if (n == 10 || n == 11)
356 if (n == 30 || n == 31)
364 if (n <= 53 || n == 59)
372 return MALFORMED_RESPONSE;
376 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
380 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
383 if (sizeof(tab->code) != datalen)
386 memcpy(&tab->code, imsg->data, sizeof(tab->code));
387 tab->code = normalize_code(tab->code);
388 if (tab->code != 30 && tab->code != 31)
389 tab->redirect_count = 0;
393 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
398 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
401 if (sizeof(tab->meta) <= datalen)
404 memcpy(tab->meta, imsg->data, datalen);
406 if (tab->code < 10) { /* internal errors */
407 load_page_from_str(tab, err_pages[tab->code]);
408 } else if (tab->code < 20) { /* 1x */
409 free(tab->last_input_url);
410 tab->last_input_url = strdup(tab->hist_cur->h);
411 if (tab->last_input_url == NULL)
414 load_page_from_str(tab, err_pages[tab->code]);
415 ui_require_input(tab, tab->code == 11, ir_select_gemini);
416 } else if (tab->code == 20) {
417 history_add(tab->hist_cur->h);
418 if (setup_parser_for(tab)) {
419 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
420 } else if (safe_mode) {
421 load_page_from_str(tab,
422 err_pages[UNKNOWN_TYPE_OR_CSET]);
426 if ((h = hist_pop(&tab->hist)) != NULL)
428 snprintf(buf, sizeof(buf),
429 "Can't display \"%s\", save it?", tab->meta);
430 ui_yornp(buf, handle_maybe_save_page, tab);
432 } else if (tab->code < 40) { /* 3x */
433 tab->redirect_count++;
435 /* TODO: make customizable? */
436 if (tab->redirect_count > 5) {
437 load_page_from_str(tab,
438 err_pages[TOO_MUCH_REDIRECTS]);
440 do_load_url(tab, tab->meta, NULL, LU_MODE_NOCACHE);
441 } else { /* 4x, 5x & 6x */
442 load_page_from_str(tab, err_pages[tab->code]);
447 handle_maybe_save_page(int dosave, struct tab *tab)
450 char input[PATH_MAX];
452 /* XXX: this print a message that is confusing */
453 ui_on_tab_loaded(tab);
460 if ((f = strrchr(tab->uri.path, '/')) == NULL)
465 strlcpy(input, download_path, sizeof(input));
466 strlcat(input, f, sizeof(input));
468 ui_read("Save to path", handle_save_page_path, tab, input);
472 handle_save_page_path(const char *path, struct tab *tab)
479 ui_show_downloads_pane();
481 enqueue_download(tab->id, path);
482 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
485 * Change this tab id, the old one is associated with the
488 tab->id = tab_new_id();
492 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
498 * There are no reason we shouldn't be able to find the
501 if ((d = download_by_id(imsg->hdr.peerid)) == NULL)
504 if (imsg->fd == -1) {
506 if (e[datalen-1] != '\0')
508 message("Can't open file %s: %s", d->path, e);
511 ui_send_net(IMSG_PROCEED, d->id, NULL, 0);
516 handle_imsg_session(struct imsg *imsg, size_t datalen)
518 static struct tab *curr;
519 static struct tab *tab;
520 struct session_tab st;
521 struct session_tab_hist sth;
526 * The fs process tried to send tabs after it has announced
527 * that he's done. Something fishy is going on, better die.
532 switch (imsg->hdr.type) {
533 case IMSG_SESSION_TAB:
534 if (datalen != sizeof(st))
537 memcpy(&st, imsg->data, sizeof(st));
538 if ((tab = new_tab(st.uri, NULL, NULL)) == NULL)
540 tab->hist_cur->line_off = st.top_line;
541 tab->hist_cur->current_off = st.current_line;
542 strlcpy(tab->buffer.page.title, st.title,
543 sizeof(tab->buffer.page.title));
544 if (st.flags & TAB_CURRENT)
546 if (st.flags & TAB_KILLED)
550 case IMSG_SESSION_TAB_HIST:
551 if (tab == NULL || datalen != sizeof(sth))
554 memcpy(&sth, imsg->data, sizeof(sth));
555 if (sth.uri[sizeof(sth.uri)-1] != '\0')
558 if ((h = calloc(1, sizeof(*h))) == NULL)
560 strlcpy(h->h, sth.uri, sizeof(h->h));
563 hist_push(&tab->hist, h);
565 hist_add_before(&tab->hist, tab->hist_cur, h);
568 case IMSG_SESSION_END:
569 if (datalen != sizeof(first_time))
571 memcpy(&first_time, imsg->data, sizeof(first_time));
573 new_tab("about:new", NULL, NULL);
574 curr = new_tab("about:help", NULL, NULL);
580 if (has_url || TAILQ_EMPTY(&tabshead))
581 new_tab(url, NULL, NULL);
591 handle_imsg_history(struct imsg *imsg, size_t datalen)
596 * The fs process tried to send history item after it
597 * has announced that it's done. Something fishy is
598 * going on, better die
603 switch (imsg->hdr.type) {
605 if (datalen != sizeof(hi))
608 memcpy(&hi, imsg->data, sizeof(hi));
622 handle_imsg_buf(struct imsg *imsg, size_t datalen)
624 struct tab *tab = NULL;
625 struct download *d = NULL;
627 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
628 (d = download_by_id(imsg->hdr.peerid)) == NULL)
632 if (!parser_parse(tab, imsg->data, datalen))
634 ui_on_tab_refresh(tab);
637 write(d->fd, imsg->data, datalen);
638 ui_on_download_refresh();
643 handle_imsg_eof(struct imsg *imsg, size_t datalen)
645 struct tab *tab = NULL;
646 struct download *d = NULL;
648 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
649 (d = download_by_id(imsg->hdr.peerid)) == NULL)
653 if (!parser_free(tab))
655 if (has_prefix(tab->hist_cur->h, "gemini://") ||
656 has_prefix(tab->hist_cur->h, "gopher://"))
658 ui_on_tab_refresh(tab);
659 ui_on_tab_loaded(tab);
663 ui_on_download_refresh();
668 handle_imsg_tofu(struct imsg *imsg, size_t datalen)
670 struct tofu_entry *e;
675 if ((e = calloc(1, sizeof(*e))) == NULL)
678 if (datalen != sizeof(*e))
680 memcpy(e, imsg->data, sizeof(*e));
681 if (e->domain[sizeof(e->domain)-1] != '\0' ||
682 e->hash[sizeof(e->hash)-1] != '\0')
688 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
692 if (datalen != sizeof(res))
695 memcpy(&res, imsg->data, sizeof(res));
697 message("Added to bookmarks!");
699 message("Failed to add to bookmarks: %s",
704 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
708 if (datalen != sizeof(res))
710 memcpy(&res, imsg->data, datalen);
712 message("Failed to save the cert for: %s",
717 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
721 if (datalen != sizeof(res))
723 memcpy(&res, imsg->data, datalen);
725 message("Failed to update the certificate");
729 handle_dispatch_imsg(int fd, short ev, void *d)
731 struct imsgev *iev = d;
733 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
734 err(1, "connection closed");
738 load_about_url(struct tab *tab, const char *url)
740 tab->trust = TS_UNKNOWN;
741 parser_init(tab, gemtext_initparser);
742 return make_fs_request(tab, IMSG_GET, url);
746 load_file_url(struct tab *tab, const char *url)
748 tab->trust = TS_UNKNOWN;
749 return make_fs_request(tab, IMSG_GET_FILE, tab->uri.path);
753 load_finger_url(struct tab *tab, const char *url)
759 memset(&req, 0, sizeof(req));
760 strlcpy(req.port, tab->uri.port, sizeof(req.port));
763 * Sometimes the finger url have the user as path component
764 * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
765 * userinfo (e.g. finger://cobradile@finger.farm).
767 if ((at = strchr(tab->uri.host, '@')) != NULL) {
768 len = at - tab->uri.host;
769 memcpy(req.req, tab->uri.host, len);
771 if (len >= sizeof(req.req))
775 strlcpy(req.host, at+1, sizeof(req.host));
777 strlcpy(req.host, tab->uri.host, sizeof(req.host));
779 /* +1 to skip the initial `/' */
780 strlcpy(req.req, tab->uri.path+1, sizeof(req.req));
782 strlcat(req.req, "\r\n", sizeof(req.req));
784 parser_init(tab, textplain_initparser);
785 return make_request(tab, &req, PROTO_FINGER, NULL);
789 load_gemini_url(struct tab *tab, const char *url)
793 memset(&req, 0, sizeof(req));
794 strlcpy(req.host, tab->uri.host, sizeof(req.host));
795 strlcpy(req.port, tab->uri.port, sizeof(req.port));
797 return make_request(tab, &req, PROTO_GEMINI, tab->hist_cur->h);
800 static inline const char *
801 gopher_skip_selector(const char *path, int *ret_type)
805 if (!strcmp(path, "/") || *path == '\0') {
814 switch (*ret_type = *path) {
830 load_gopher_url(struct tab *tab, const char *url)
836 memset(&req, 0, sizeof(req));
837 strlcpy(req.host, tab->uri.host, sizeof(req.host));
838 strlcpy(req.port, tab->uri.port, sizeof(req.port));
840 path = gopher_skip_selector(tab->uri.path, &type);
843 parser_init(tab, textplain_initparser);
846 parser_init(tab, gophermap_initparser);
849 free(tab->last_input_url);
850 tab->last_input_url = strdup(url);
851 if (tab->last_input_url == NULL)
853 ui_require_input(tab, 0, ir_select_gopher);
854 return load_page_from_str(tab, err_pages[10]);
856 return load_page_from_str(tab, "Unknown gopher selector");
859 strlcpy(req.req, path, sizeof(req.req));
860 if (*tab->uri.query != '\0') {
861 strlcat(req.req, "?", sizeof(req.req));
862 strlcat(req.req, tab->uri.query, sizeof(req.req));
864 strlcat(req.req, "\r\n", sizeof(req.req));
866 return make_request(tab, &req, PROTO_GOPHER, NULL);
870 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
874 memset(&req, 0, sizeof(req));
875 strlcpy(req.host, p->host, sizeof(req.host));
876 strlcpy(req.port, p->port, sizeof(req.port));
880 return make_request(tab, &req, p->proto, tab->hist_cur->h);
884 make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
887 tab->id = tab_new_id();
891 strlcpy(req->req, r, sizeof(req->req));
892 strlcat(req->req, "\r\n", sizeof(req->req));
895 start_loading_anim(tab);
896 ui_send_net(IMSG_GET_RAW, tab->id, req, sizeof(*req));
899 * So the various load_*_url can `return make_request` and
900 * do_load_url is happy.
906 make_fs_request(struct tab *tab, int type, const char *r)
909 tab->id = tab_new_id();
911 ui_send_fs(type, tab->id, r, strlen(r)+1);
914 * So load_{about,file}_url can `return make_fs_request` and
915 * do_load_url is happy.
921 gopher_send_search_req(struct tab *tab, const char *text)
925 memset(&req, 0, sizeof(req));
926 strlcpy(req.host, tab->uri.host, sizeof(req.host));
927 strlcpy(req.port, tab->uri.port, sizeof(req.port));
930 strlcpy(req.req, tab->uri.path+2, sizeof(req.req));
931 if (*tab->uri.query != '\0') {
932 strlcat(req.req, "?", sizeof(req.req));
933 strlcat(req.req, tab->uri.query, sizeof(req.req));
936 strlcat(req.req, "\t", sizeof(req.req));
937 strlcat(req.req, text, sizeof(req.req));
938 strlcat(req.req, "\r\n", sizeof(req.req));
940 erase_buffer(&tab->buffer);
941 parser_init(tab, gophermap_initparser);
943 make_request(tab, &req, PROTO_GOPHER, NULL);
947 load_page_from_str(struct tab *tab, const char *page)
949 parser_init(tab, gemtext_initparser);
950 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
952 if (!tab->buffer.page.free(&tab->buffer.page))
954 ui_on_tab_refresh(tab);
955 ui_on_tab_loaded(tab);
960 * Effectively load the given url in the given tab. Return 1 when
961 * loading the page asynchronously, and thus when an erase_buffer can
962 * be done right after this function return, or 0 when loading the
963 * page synchronously.
966 do_load_url(struct tab *tab, const char *url, const char *base, int mode)
968 const struct proto *p;
971 int nocache = mode & LU_MODE_NOCACHE;
975 tab->trust = TS_UNKNOWN;
978 memcpy(&uri, &tab->uri, sizeof(tab->uri));
980 phos_parse_absolute_uri(base, &uri);
982 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
983 if (asprintf(&t, "#error loading %s\n>%s\n",
984 url, "Can't parse the URI") == -1)
986 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
987 load_page_from_str(tab, t);
992 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
993 sizeof(tab->hist_cur->h));
995 if (!nocache && mcache_lookup(tab->hist_cur->h, tab)) {
996 ui_on_tab_refresh(tab);
997 ui_on_tab_loaded(tab);
1001 for (p = protos; p->schema != NULL; ++p) {
1002 if (!strcmp(tab->uri.scheme, p->schema)) {
1003 /* patch the port */
1004 if (*tab->uri.port == '\0' && p->port != NULL)
1005 strlcpy(tab->uri.port, p->port,
1006 sizeof(tab->uri.port));
1008 return p->loadfn(tab, tab->hist_cur->h);
1012 TAILQ_FOREACH(proxy, &proxies, proxies) {
1013 if (!strcmp(tab->uri.scheme, proxy->match_proto))
1014 return load_via_proxy(tab, url, proxy);
1017 return load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
1021 * Load url in tab and handle history. If a tab is marked as lazy, only
1022 * prepare the url but don't load it.
1025 load_url(struct tab *tab, const char *url, const char *base, int mode)
1027 int lazy = tab->flags & TAB_LAZY;
1028 int nohist = mode & LU_MODE_NOHIST;
1030 if (operating && lazy) {
1031 tab->flags ^= TAB_LAZY;
1033 } else if (tab->hist_cur != NULL)
1034 get_scroll_position(tab, &tab->hist_cur->line_off,
1035 &tab->hist_cur->current_off);
1037 if (!nohist && (!lazy || tab->hist_cur == NULL)) {
1038 if (tab->hist_cur != NULL)
1039 hist_clear_forward(&tab->hist,
1040 TAILQ_NEXT(tab->hist_cur, entries));
1042 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur)))
1048 strlcpy(tab->buffer.page.title, url,
1049 sizeof(tab->buffer.page.title));
1050 hist_push(&tab->hist, tab->hist_cur);
1053 strlcpy(tab->hist_cur->h, url,
1054 sizeof(tab->hist_cur->h));
1058 do_load_url(tab, url, base, mode);
1062 load_url_in_tab(struct tab *tab, const char *url, const char *base, int mode)
1066 message("Loading %s...", url);
1069 load_url(tab, url, base, mode);
1073 load_previous_page(struct tab *tab)
1077 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
1080 do_load_url(tab, h->h, NULL, LU_MODE_NONE);
1085 load_next_page(struct tab *tab)
1089 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
1092 do_load_url(tab, h->h, NULL, LU_MODE_NONE);
1097 add_to_bookmarks(const char *str)
1099 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
1100 str, strlen(str)+1);
1104 * Given a user-entered URL, apply some heuristics to use it:
1106 * - if it's a proper url use it
1107 * - if it starts with a `./' or a `/' assume its a file:// url
1108 * - assume it's a gemini:// url
1110 * `ret' (of which len is the size) will be filled with the resulting
1114 humanify_url(const char *raw, char *ret, size_t len)
1116 struct phos_uri uri;
1119 if (phos_parse_absolute_uri(raw, &uri)) {
1120 strlcpy(ret, raw, len);
1124 if (has_prefix(raw, "./")) {
1125 strlcpy(ret, "file://", len);
1126 getcwd(buf, sizeof(buf));
1127 strlcat(ret, buf, len);
1128 strlcat(ret, "/", len);
1129 strlcat(ret, raw+2, len);
1134 strlcpy(ret, "file://", len);
1135 strlcat(ret, raw, len);
1139 strlcpy(ret, "gemini://", len);
1140 strlcat(ret, raw, len);
1144 start_child(enum telescope_process p, const char *argv0, int fd)
1146 const char *argv[5];
1150 switch (pid = fork()) {
1160 if (dup2(fd, 3) == -1)
1161 err(1, "cannot setup imsg fd");
1163 argv[argc++] = argv0;
1166 errx(1, "Can't start ui process");
1168 argv[argc++] = "-Tf";
1171 argv[argc++] = "-Tn";
1176 argv[argc++] = "-S";
1178 argv[argc++] = NULL;
1179 execvp(argv0, (char *const *)argv);
1180 err(1, "execvp(%s)", argv0);
1184 send_url(const char *url)
1186 struct sockaddr_un sun;
1187 struct imsgbuf ibuf;
1190 if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1193 memset(&sun, 0, sizeof(sun));
1194 sun.sun_family = AF_UNIX;
1195 strlcpy(sun.sun_path, ctlsock_path, sizeof(sun.sun_path));
1197 if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
1198 err(1, "connect: %s", ctlsock_path);
1200 imsg_init(&ibuf, ctl_sock);
1201 imsg_compose(&ibuf, IMSG_CTL_OPEN_URL, 0, 0, -1, url,
1208 ui_send_net(int type, uint32_t peerid, const void *data,
1211 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
1216 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
1218 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
1222 static void __attribute__((noreturn))
1225 fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1227 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1232 main(int argc, char * const *argv)
1234 struct imsgev net_ibuf, fs_ibuf;
1237 int pipe2net[2], pipe2fs[2];
1238 int ch, configtest = 0, fail = 0;
1246 signal(SIGCHLD, SIG_IGN);
1247 signal(SIGPIPE, SIG_IGN);
1249 if (getenv("NO_COLOR") != NULL)
1254 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1257 exit(ui_print_colors());
1260 strlcpy(config_path, optarg, sizeof(config_path));
1279 errx(1, "invalid process spec %c",
1284 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1298 else if (proc == PROC_FS)
1300 else if (proc == PROC_NET)
1308 humanify_url(argv[0], url, sizeof(url));
1311 /* setup keys before reading the config */
1312 TAILQ_INIT(&global_map.m);
1313 global_map.unhandled_input = global_key_unbound;
1314 TAILQ_INIT(&minibuffer_map.m);
1317 parseconfig(config_path, fail);
1323 if (download_path == NULL &&
1324 (download_path = strdup("/tmp/")) == NULL)
1327 if (!safe_mode && (sessionfd = lock_session()) == -1) {
1333 errx(1, "can't lock session, is another instance of "
1334 "telescope already running?");
1337 /* Start children. */
1338 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
1339 err(1, "socketpair");
1340 start_child(PROC_FS, argv0, pipe2fs[1]);
1341 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
1343 iev_fs->handler = handle_dispatch_imsg;
1345 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1346 err(1, "socketpair");
1347 start_child(PROC_NET, argv0, pipe2net[1]);
1348 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1349 iev_net = &net_ibuf;
1350 iev_net->handler = handle_dispatch_imsg;
1354 /* initialize tofu store */
1355 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1360 if ((control_fd = control_init(ctlsock_path)) == -1)
1361 err(1, "control_init %s", ctlsock_path);
1362 control_listen(control_fd);
1365 /* initialize the in-memory cache store */
1368 /* Setup event handler for the autosave */
1371 /* Setup event handlers for pipes to fs/net */
1372 iev_fs->events = EV_READ;
1373 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
1374 iev_fs->handler, iev_fs);
1375 event_add(&iev_fs->ev, NULL);
1377 iev_net->events = EV_READ;
1378 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
1379 iev_net->handler, iev_net);
1380 event_add(&iev_net->ev, NULL);
1383 sandbox_ui_process();
1384 ui_send_fs(IMSG_INIT, 0, NULL, 0);
1389 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
1390 ui_send_net(IMSG_QUIT, 0, NULL, 0);
1391 imsg_flush(&iev_fs->ibuf);
1392 imsg_flush(&iev_net->ibuf);
1394 /* wait for children to terminate */
1396 pid = wait(&status);
1398 if (errno != EINTR && errno != ECHILD)
1400 } else if (WIFSIGNALED(status))
1401 warnx("child terminated; signal %d", WTERMSIG(status));
1402 } while (pid != -1 || (pid == -1 && errno == EINTR));
1404 if (!safe_mode && close(sessionfd) == -1)
1405 err(1, "close(sessionfd = %d)", sessionfd);