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>
33 #include "minibuffer.h"
36 #include "telescope.h"
39 static struct option longopts[] = {
40 {"colors", no_argument, NULL, 'C'},
41 {"colours", no_argument, NULL, 'C'},
42 {"help", no_argument, NULL, 'h'},
43 {"safe", no_argument, NULL, 'S'},
44 {"version", no_argument, NULL, 'v'},
48 static const char *opts = "Cc:hnST:v";
51 static char url[GEMINI_URL_LEN];
54 * Used to know when we're finished loading.
59 * "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
60 * anything to the filesystem or execute external programs.
64 static struct imsgev *iev_fs, *iev_net;
66 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
67 struct tabshead ktabshead = TAILQ_HEAD_INITIALIZER(ktabshead);
68 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
70 enum telescope_process {
76 #define CANNOT_FETCH 0
77 #define TOO_MUCH_REDIRECTS 1
78 #define MALFORMED_RESPONSE 2
79 #define UNKNOWN_TYPE_OR_CSET 3
80 #define UNKNOWN_PROTOCOL 4
82 /* XXX: keep in sync with normalize_code */
83 const char *err_pages[70] = {
84 [CANNOT_FETCH] = "# Couldn't load the page\n",
85 [TOO_MUCH_REDIRECTS] = "# Too much redirects\n",
86 [MALFORMED_RESPONSE] = "# Got a malformed response\n",
87 [UNKNOWN_TYPE_OR_CSET] = "# Unsupported type or charset\n",
88 [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
90 [10] = "# Input required\n",
91 [11] = "# Input required\n",
92 [40] = "# Temporary failure\n",
93 [41] = "# Server unavailable\n",
94 [42] = "# CGI error\n",
95 [43] = "# Proxy error\n",
96 [44] = "# Slow down\n",
97 [50] = "# Permanent failure\n",
98 [51] = "# Not found\n",
100 [53] = "# Proxy request refused\n",
101 [59] = "# Bad request\n",
102 [60] = "# Client certificate required\n",
103 [61] = "# Certificate not authorised\n",
104 [62] = "# Certificate not valid\n"
107 static void die(void) __attribute__((__noreturn__));
108 static struct tab *tab_by_id(uint32_t);
109 static void handle_imsg_err(struct imsg *, size_t);
110 static void handle_imsg_check_cert(struct imsg *, size_t);
111 static void handle_check_cert_user_choice(int, struct tab *);
112 static void handle_maybe_save_new_cert(int, struct tab *);
113 static void handle_imsg_got_code(struct imsg *, size_t);
114 static void handle_imsg_got_meta(struct imsg *, size_t);
115 static void handle_maybe_save_page(int, struct tab *);
116 static void handle_save_page_path(const char *, struct tab *);
117 static void handle_imsg_file_opened(struct imsg *, size_t);
118 static void handle_imsg_buf(struct imsg *, size_t);
119 static void handle_imsg_eof(struct imsg *, size_t);
120 static void handle_imsg_tofu(struct imsg *, size_t);
121 static void handle_imsg_bookmark_ok(struct imsg *, size_t);
122 static void handle_imsg_save_cert_ok(struct imsg *, size_t);
123 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
124 static void handle_imsg_session(struct imsg *, size_t);
125 static void handle_dispatch_imsg(int, short, void *);
126 static int load_about_url(struct tab *, const char *);
127 static int load_file_url(struct tab *, const char *);
128 static int load_finger_url(struct tab *, const char *);
129 static int load_gemini_url(struct tab *, const char *);
130 static int load_gopher_url(struct tab *, const char *);
131 static int load_via_proxy(struct tab *, const char *,
133 static int make_request(struct tab *, struct get_req *, int,
135 static int make_fs_request(struct tab *, int, const char *);
136 static int do_load_url(struct tab *, const char *, const char *);
137 static pid_t start_child(enum telescope_process, const char *, int);
139 static struct proto {
142 int (*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 imsg_handlerfn *handlers[] = {
153 [IMSG_ERR] = handle_imsg_err,
154 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
155 [IMSG_GOT_CODE] = handle_imsg_got_code,
156 [IMSG_GOT_META] = handle_imsg_got_meta,
157 [IMSG_BUF] = handle_imsg_buf,
158 [IMSG_EOF] = handle_imsg_eof,
159 [IMSG_TOFU] = handle_imsg_tofu,
160 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
161 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
162 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
163 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
164 [IMSG_SESSION_TAB] = handle_imsg_session,
165 [IMSG_SESSION_TAB_HIST] = handle_imsg_session,
166 [IMSG_SESSION_END] = handle_imsg_session,
169 static struct ohash certs;
171 static void __attribute__((__noreturn__))
178 tab_by_id(uint32_t id)
182 TAILQ_FOREACH(t, &tabshead, tabs) {
191 handle_imsg_err(struct imsg *imsg, size_t datalen)
196 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
200 page[datalen-1] = '\0';
202 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
203 tab->hist_cur->h, page) == -1)
205 load_page_from_str(tab, page);
210 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
212 const char *hash, *host, *port;
214 struct tofu_entry *e;
218 if (hash[datalen-1] != '\0')
221 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
224 if (tab->proxy != NULL) {
225 host = tab->proxy->host;
226 port = tab->proxy->port;
228 host = tab->uri.host;
229 port = tab->uri.port;
232 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
233 /* TODO: an update in libressl/libretls changed
234 * significantly. Find a better approach at storing
236 if (datalen > sizeof(e->hash))
239 tofu_res = 1; /* trust on first use */
240 if ((e = calloc(1, sizeof(*e))) == NULL)
242 strlcpy(e->domain, host, sizeof(e->domain));
243 if (*port != '\0' && strcmp(port, "1965")) {
244 strlcat(e->domain, ":", sizeof(e->domain));
245 strlcat(e->domain, port, sizeof(e->domain));
247 strlcpy(e->hash, hash, sizeof(e->hash));
249 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
251 tofu_res = !strcmp(hash, e->hash);
254 if (e->verified == -1)
255 tab->trust = TS_TEMP_TRUSTED;
256 else if (e->verified == 1)
257 tab->trust = TS_VERIFIED;
259 tab->trust = TS_TRUSTED;
261 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
262 &tofu_res, sizeof(tofu_res));
264 tab->trust = TS_UNTRUSTED;
265 load_page_from_str(tab, "# Certificate mismatch\n");
266 if ((tab->cert = strdup(hash)) == NULL)
268 ui_yornp("Certificate mismatch. Proceed?",
269 handle_check_cert_user_choice, tab);
274 handle_check_cert_user_choice(int accept, struct tab *tab)
276 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
281 * trust the certificate for this session only. If
282 * the page results in a redirect while we're asking
283 * the user to save, we'll end up with an invalid
284 * tabid (one request == one tab id). It also makes
285 * sense to save it for the current session if the
288 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port,
292 ui_yornp("Save the new certificate?",
293 handle_maybe_save_new_cert, tab);
295 message("Certificate temporarly trusted");
303 handle_maybe_save_new_cert(int accept, struct tab *tab)
305 struct tofu_entry *e;
306 const char *host, *port;
308 if (tab->proxy != NULL) {
309 host = tab->proxy->host;
310 port = tab->proxy->port;
312 host = tab->uri.host;
313 port = tab->uri.port;
319 if ((e = calloc(1, sizeof(*e))) == NULL)
322 strlcpy(e->domain, host, sizeof(e->domain));
323 if (*port != '\0' && strcmp(port, "1965")) {
324 strlcat(e->domain, ":", sizeof(e->domain));
325 strlcat(e->domain, port, sizeof(e->domain));
327 strlcpy(e->hash, tab->cert, sizeof(e->hash));
328 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
330 tofu_update(&certs, e);
332 tab->trust = TS_TRUSTED;
340 normalize_code(int n)
343 if (n == 10 || n == 11)
349 if (n == 30 || n == 31)
357 if (n <= 53 || n == 59)
365 return MALFORMED_RESPONSE;
369 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
373 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
376 if (sizeof(tab->code) != datalen)
379 memcpy(&tab->code, imsg->data, sizeof(tab->code));
380 tab->code = normalize_code(tab->code);
381 if (tab->code != 30 && tab->code != 31)
382 tab->redirect_count = 0;
386 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
391 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
394 if (sizeof(tab->meta) <= datalen)
397 memcpy(tab->meta, imsg->data, datalen);
399 if (tab->code < 10) { /* internal errors */
400 load_page_from_str(tab, err_pages[tab->code]);
401 } else if (tab->code < 20) { /* 1x */
402 load_page_from_str(tab, err_pages[tab->code]);
403 ui_require_input(tab, tab->code == 11, PROTO_GEMINI);
404 } else if (tab->code == 20) {
405 if (setup_parser_for(tab)) {
406 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
407 } else if (safe_mode) {
408 load_page_from_str(tab,
409 err_pages[UNKNOWN_TYPE_OR_CSET]);
413 if ((h = hist_pop(&tab->hist)) != NULL)
415 snprintf(buf, sizeof(buf),
416 "Can't display \"%s\", save it?", tab->meta);
417 ui_yornp(buf, handle_maybe_save_page, tab);
419 } else if (tab->code < 40) { /* 3x */
420 tab->redirect_count++;
422 /* TODO: make customizable? */
423 if (tab->redirect_count > 5) {
424 load_page_from_str(tab,
425 err_pages[TOO_MUCH_REDIRECTS]);
427 do_load_url(tab, tab->meta, NULL);
428 } else { /* 4x, 5x & 6x */
429 load_page_from_str(tab, err_pages[tab->code]);
434 handle_maybe_save_page(int dosave, struct tab *tab)
437 char input[PATH_MAX];
439 /* XXX: this print a message that is confusing */
440 ui_on_tab_loaded(tab);
447 if ((f = strrchr(tab->uri.path, '/')) == NULL)
452 strlcpy(input, download_path, sizeof(input));
453 strlcat(input, f, sizeof(input));
455 ui_read("Save to path", handle_save_page_path, tab, input);
459 handle_save_page_path(const char *path, struct tab *tab)
466 ui_show_downloads_pane();
468 enqueue_download(tab->id, path);
469 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
472 * Change this tab id, the old one is associated with the
475 tab->id = tab_new_id();
479 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
485 * There are no reason we shouldn't be able to find the
488 if ((d = download_by_id(imsg->hdr.peerid)) == NULL)
491 if (imsg->fd == -1) {
493 if (e[datalen-1] != '\0')
495 message("Can't open file %s: %s", d->path, e);
498 ui_send_net(IMSG_PROCEED, d->id, NULL, 0);
503 handle_imsg_session(struct imsg *imsg, size_t datalen)
505 static struct tab *curr;
506 static struct tab *tab;
507 struct session_tab st;
508 struct session_tab_hist sth;
513 * The fs process tried to send tabs after it has announced
514 * that he's done. Something fishy is going on, better die.
519 switch (imsg->hdr.type) {
520 case IMSG_SESSION_TAB:
521 if (datalen != sizeof(st))
524 memcpy(&st, imsg->data, sizeof(st));
525 if ((tab = new_tab(st.uri, NULL, NULL)) == NULL)
527 strlcpy(tab->buffer.page.title, st.title,
528 sizeof(tab->buffer.page.title));
529 if (st.flags & TAB_CURRENT)
531 if (st.flags & TAB_KILLED)
535 case IMSG_SESSION_TAB_HIST:
536 if (tab == NULL || datalen != sizeof(sth))
539 memcpy(&sth, imsg->data, sizeof(sth));
540 if (sth.uri[sizeof(sth.uri)-1] != '\0')
543 if ((h = calloc(1, sizeof(*h))) == NULL)
545 strlcpy(h->h, sth.uri, sizeof(h->h));
548 hist_push(&tab->hist, h);
550 hist_add_before(&tab->hist, tab->hist_cur, h);
553 case IMSG_SESSION_END:
554 if (datalen != sizeof(first_time))
556 memcpy(&first_time, imsg->data, sizeof(first_time));
558 new_tab("about:new", NULL, NULL);
559 curr = new_tab("about:help", NULL, NULL);
565 if (has_url || TAILQ_EMPTY(&tabshead))
566 new_tab(url, NULL, NULL);
576 handle_imsg_buf(struct imsg *imsg, size_t datalen)
578 struct tab *tab = NULL;
579 struct download *d = NULL;
581 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
582 (d = download_by_id(imsg->hdr.peerid)) == NULL)
586 if (!parser_parse(tab, imsg->data, datalen))
588 ui_on_tab_refresh(tab);
591 write(d->fd, imsg->data, datalen);
592 ui_on_download_refresh();
597 handle_imsg_eof(struct imsg *imsg, size_t datalen)
599 struct tab *tab = NULL;
600 struct download *d = NULL;
602 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
603 (d = download_by_id(imsg->hdr.peerid)) == NULL)
607 if (!parser_free(tab))
609 if (has_prefix(tab->hist_cur->h, "gemini://") ||
610 has_prefix(tab->hist_cur->h, "gopher://"))
611 mcache_buffer(tab->hist_cur->h, &tab->buffer, tab->trust);
612 ui_on_tab_refresh(tab);
613 ui_on_tab_loaded(tab);
617 ui_on_download_refresh();
622 handle_imsg_tofu(struct imsg *imsg, size_t datalen)
624 struct tofu_entry *e;
629 if ((e = calloc(1, sizeof(*e))) == NULL)
632 if (datalen != sizeof(*e))
634 memcpy(e, imsg->data, sizeof(*e));
635 if (e->domain[sizeof(e->domain)-1] != '\0' ||
636 e->hash[sizeof(e->hash)-1] != '\0')
642 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
646 if (datalen != sizeof(res))
649 memcpy(&res, imsg->data, sizeof(res));
651 message("Added to bookmarks!");
653 message("Failed to add to bookmarks: %s",
658 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
662 if (datalen != sizeof(res))
664 memcpy(&res, imsg->data, datalen);
666 message("Failed to save the cert for: %s",
671 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
675 if (datalen != sizeof(res))
677 memcpy(&res, imsg->data, datalen);
679 message("Failed to update the certificate");
683 handle_dispatch_imsg(int fd, short ev, void *d)
685 struct imsgev *iev = d;
687 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
688 err(1, "connection closed");
692 load_about_url(struct tab *tab, const char *url)
694 tab->trust = TS_UNKNOWN;
695 parser_init(tab, gemtext_initparser);
696 return make_fs_request(tab, IMSG_GET, url);
700 load_file_url(struct tab *tab, const char *url)
702 tab->trust = TS_UNKNOWN;
703 return make_fs_request(tab, IMSG_GET_FILE, tab->uri.path);
707 load_finger_url(struct tab *tab, const char *url)
713 memset(&req, 0, sizeof(req));
714 strlcpy(req.port, tab->uri.port, sizeof(req.port));
717 * Sometimes the finger url have the user as path component
718 * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
719 * userinfo (e.g. finger://cobradile@finger.farm).
721 if ((at = strchr(tab->uri.host, '@')) != NULL) {
722 len = at - tab->uri.host;
723 memcpy(req.req, tab->uri.host, len);
725 if (len >= sizeof(req.req))
729 strlcpy(req.host, at+1, sizeof(req.host));
731 strlcpy(req.host, tab->uri.host, sizeof(req.host));
733 /* +1 to skip the initial `/' */
734 strlcpy(req.req, tab->uri.path+1, sizeof(req.req));
736 strlcat(req.req, "\r\n", sizeof(req.req));
738 parser_init(tab, textplain_initparser);
739 return make_request(tab, &req, PROTO_FINGER, NULL);
743 load_gemini_url(struct tab *tab, const char *url)
747 memset(&req, 0, sizeof(req));
748 strlcpy(req.host, tab->uri.host, sizeof(req.host));
749 strlcpy(req.port, tab->uri.port, sizeof(req.port));
751 return make_request(tab, &req, PROTO_GEMINI, tab->hist_cur->h);
754 static inline const char *
755 gopher_skip_selector(const char *path, int *ret_type)
759 if (!strcmp(path, "/") || *path == '\0') {
768 switch (*ret_type = *path) {
784 load_gopher_url(struct tab *tab, const char *url)
790 memset(&req, 0, sizeof(req));
791 strlcpy(req.host, tab->uri.host, sizeof(req.host));
792 strlcpy(req.port, tab->uri.port, sizeof(req.port));
794 path = gopher_skip_selector(tab->uri.path, &type);
797 parser_init(tab, textplain_initparser);
800 parser_init(tab, gophermap_initparser);
803 ui_require_input(tab, 0, PROTO_GOPHER);
804 return load_page_from_str(tab, err_pages[10]);
806 return load_page_from_str(tab, "Unknown gopher selector");
809 strlcpy(req.req, path, sizeof(req.req));
810 if (*tab->uri.query != '\0') {
811 strlcat(req.req, "?", sizeof(req.req));
812 strlcat(req.req, tab->uri.query, sizeof(req.req));
814 strlcat(req.req, "\r\n", sizeof(req.req));
816 return make_request(tab, &req, PROTO_GOPHER, NULL);
820 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
824 memset(&req, 0, sizeof(req));
825 strlcpy(req.host, p->host, sizeof(req.host));
826 strlcpy(req.port, p->port, sizeof(req.port));
830 return make_request(tab, &req, p->proto, tab->hist_cur->h);
834 make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
837 tab->id = tab_new_id();
841 strlcpy(req->req, r, sizeof(req->req));
842 strlcat(req->req, "\r\n", sizeof(req->req));
845 ui_send_net(IMSG_GET_RAW, tab->id, req, sizeof(*req));
848 * So the various load_*_url can `return make_request` and
849 * do_load_url is happy.
855 make_fs_request(struct tab *tab, int type, const char *r)
858 tab->id = tab_new_id();
860 ui_send_fs(type, tab->id, r, strlen(r)+1);
863 * So load_{about,file}_url can `return make_fs_request` and
864 * do_load_url is happy.
870 gopher_send_search_req(struct tab *tab, const char *text)
874 start_loading_anim(tab);
876 memset(&req, 0, sizeof(req));
877 strlcpy(req.host, tab->uri.host, sizeof(req.host));
878 strlcpy(req.port, tab->uri.port, sizeof(req.port));
881 strlcpy(req.req, tab->uri.path+2, sizeof(req.req));
882 if (*tab->uri.query != '\0') {
883 strlcat(req.req, "?", sizeof(req.req));
884 strlcat(req.req, tab->uri.query, sizeof(req.req));
887 strlcat(req.req, "\t", sizeof(req.req));
888 strlcat(req.req, text, sizeof(req.req));
889 strlcat(req.req, "\r\n", sizeof(req.req));
891 erase_buffer(&tab->buffer);
892 parser_init(tab, gophermap_initparser);
894 make_request(tab, &req, PROTO_GOPHER, NULL);
898 try_load_cache(struct tab *tab)
900 struct evbuffer *evb;
903 if (!mcache_lookup(tab->hist_cur->h, &evb, &trust))
906 parser_init(tab, gemtext_initparser);
907 if (!parser_parse(tab, EVBUFFER_DATA(evb), EVBUFFER_LENGTH(evb)))
911 ui_on_tab_refresh(tab);
912 ui_on_tab_loaded(tab);
917 erase_buffer(&tab->buffer);
922 * Effectively load the given url in the given tab. Return 1 when
923 * loading the page asynchronously, and thus when an erase_buffer can
924 * be done right after this function return, or 0 when loading the
925 * page synchronously.
928 do_load_url(struct tab *tab, const char *url, const char *base)
937 tab->trust = TS_UNKNOWN;
940 memcpy(&uri, &tab->uri, sizeof(tab->uri));
942 phos_parse_absolute_uri(base, &uri);
944 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
945 if (asprintf(&t, "#error loading %s\n>%s\n",
946 url, "Can't parse the URI") == -1)
948 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
949 load_page_from_str(tab, t);
954 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
955 sizeof(tab->hist_cur->h));
957 if (try_load_cache(tab))
960 for (p = protos; p->schema != NULL; ++p) {
961 if (!strcmp(tab->uri.scheme, p->schema)) {
963 if (*tab->uri.port == '\0' && p->port != NULL)
964 strlcpy(tab->uri.port, p->port,
965 sizeof(tab->uri.port));
967 return p->loadfn(tab, url);
971 TAILQ_FOREACH(proxy, &proxies, proxies) {
972 if (!strcmp(tab->uri.scheme, proxy->match_proto))
973 return load_via_proxy(tab, url, proxy);
976 return load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
980 * Load url in tab and handle history. If a tab is marked as lazy, only
981 * prepare the url but don't load it.
984 load_url(struct tab *tab, const char *url, const char *base, int nohist)
986 int lazy = tab->flags & TAB_LAZY;
988 if (!nohist && (!lazy || tab->hist_cur == NULL)) {
989 if (tab->hist_cur != NULL)
990 hist_clear_forward(&tab->hist,
991 TAILQ_NEXT(tab->hist_cur, entries));
993 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur)))
999 strlcpy(tab->buffer.page.title, url,
1000 sizeof(tab->buffer.page.title));
1001 hist_push(&tab->hist, tab->hist_cur);
1004 strlcpy(tab->hist_cur->h, url,
1005 sizeof(tab->hist_cur->h));
1009 do_load_url(tab, url, base);
1013 load_url_in_tab(struct tab *tab, const char *url, const char *base, int nohist)
1016 load_url(tab, url, base, nohist);
1022 message("Loading %s...", url);
1023 start_loading_anim(tab);
1024 load_url(tab, url, base, nohist);
1028 load_previous_page(struct tab *tab)
1032 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
1035 do_load_url(tab, h->h, NULL);
1040 load_next_page(struct tab *tab)
1044 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
1047 do_load_url(tab, h->h, NULL);
1052 add_to_bookmarks(const char *str)
1054 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
1055 str, strlen(str)+1);
1059 * Given a user-entered URL, apply some heuristics to use it:
1061 * - if it's a proper url use it
1062 * - if it starts with a `./' or a `/' assume its a file:// url
1063 * - assume it's a gemini:// url
1065 * `ret' (of which len is the size) will be filled with the resulting
1069 humanify_url(const char *raw, char *ret, size_t len)
1071 struct phos_uri uri;
1074 if (phos_parse_absolute_uri(raw, &uri)) {
1075 strlcpy(ret, raw, len);
1079 if (has_prefix(raw, "./")) {
1080 strlcpy(ret, "file://", len);
1081 getcwd(buf, sizeof(buf));
1082 strlcat(ret, buf, len);
1083 strlcat(ret, "/", len);
1084 strlcat(ret, raw+2, len);
1089 strlcpy(ret, "file://", len);
1090 strlcat(ret, raw, len);
1094 strlcpy(ret, "gemini://", len);
1095 strlcat(ret, raw, len);
1099 start_child(enum telescope_process p, const char *argv0, int fd)
1101 const char *argv[5];
1105 switch (pid = fork()) {
1115 if (dup2(fd, 3) == -1)
1116 err(1, "cannot setup imsg fd");
1118 argv[argc++] = argv0;
1121 errx(1, "Can't start ui process");
1123 argv[argc++] = "-Tf";
1126 argv[argc++] = "-Tn";
1131 argv[argc++] = "-S";
1133 argv[argc++] = NULL;
1134 execvp(argv0, (char *const *)argv);
1135 err(1, "execvp(%s)", argv0);
1139 ui_send_net(int type, uint32_t peerid, const void *data,
1142 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
1147 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
1149 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
1153 static void __attribute__((noreturn))
1156 fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1158 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1163 main(int argc, char * const *argv)
1165 struct imsgev net_ibuf, fs_ibuf;
1167 int pipe2net[2], pipe2fs[2];
1168 int ch, configtest = 0, fail = 0;
1176 signal(SIGCHLD, SIG_IGN);
1177 signal(SIGPIPE, SIG_IGN);
1179 if (getenv("NO_COLOR") != NULL)
1184 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1187 exit(ui_print_colors());
1190 strlcpy(config_path, optarg, sizeof(config_path));
1209 errx(1, "invalid process spec %c",
1214 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1228 else if (proc == PROC_FS)
1230 else if (proc == PROC_NET)
1238 humanify_url(argv[0], url, sizeof(url));
1241 /* setup keys before reading the config */
1242 TAILQ_INIT(&global_map.m);
1243 global_map.unhandled_input = global_key_unbound;
1244 TAILQ_INIT(&minibuffer_map.m);
1247 parseconfig(config_path, fail);
1253 if (download_path == NULL &&
1254 (download_path = strdup("/tmp/")) == NULL)
1257 if (!safe_mode && (sessionfd = lock_session()) == -1)
1258 errx(1, "can't lock session, is another instance of "
1259 "telescope already running?");
1261 /* Start children. */
1262 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
1263 err(1, "socketpair");
1264 start_child(PROC_FS, argv0, pipe2fs[1]);
1265 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
1267 iev_fs->handler = handle_dispatch_imsg;
1269 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1270 err(1, "socketpair");
1271 start_child(PROC_NET, argv0, pipe2net[1]);
1272 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1273 iev_net = &net_ibuf;
1274 iev_net->handler = handle_dispatch_imsg;
1278 /* initialize tofu store */
1279 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1283 /* initialize the in-memory cache store */
1286 /* Setup event handler for the autosave */
1289 /* Setup event handlers for pipes to fs/net */
1290 iev_fs->events = EV_READ;
1291 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
1292 iev_fs->handler, iev_fs);
1293 event_add(&iev_fs->ev, NULL);
1295 iev_net->events = EV_READ;
1296 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
1297 iev_net->handler, iev_net);
1298 event_add(&iev_net->ev, NULL);
1301 sandbox_ui_process();
1302 ui_send_fs(IMSG_INIT, 0, NULL, 0);
1307 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
1308 ui_send_net(IMSG_QUIT, 0, NULL, 0);
1309 imsg_flush(&iev_fs->ibuf);
1310 imsg_flush(&iev_net->ibuf);
1312 /* wait for children to terminate */
1314 pid = wait(&status);
1316 if (errno != EINTR && errno != ECHILD)
1318 } else if (WIFSIGNALED(status))
1319 warnx("child terminated; signal %d", WTERMSIG(status));
1320 } while (pid != -1 || (pid == -1 && errno == EINTR));
1322 if (!safe_mode && close(sessionfd) == -1)
1323 err(1, "close(sessionfd = %d)", sessionfd);