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"
40 static struct option longopts[] = {
41 {"colors", no_argument, NULL, 'C'},
42 {"colours", no_argument, NULL, 'C'},
43 {"help", no_argument, NULL, 'h'},
44 {"safe", no_argument, NULL, 'S'},
45 {"version", no_argument, NULL, 'v'},
49 static const char *opts = "Cc:hnST:v";
52 static char url[GEMINI_URL_LEN];
55 * Used to know when we're finished loading.
60 * "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
61 * anything to the filesystem or execute external programs.
65 static struct imsgev *iev_fs, *iev_net;
67 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
68 struct tabshead ktabshead = TAILQ_HEAD_INITIALIZER(ktabshead);
69 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
71 enum telescope_process {
77 #define CANNOT_FETCH 0
78 #define TOO_MUCH_REDIRECTS 1
79 #define MALFORMED_RESPONSE 2
80 #define UNKNOWN_TYPE_OR_CSET 3
81 #define UNKNOWN_PROTOCOL 4
83 /* XXX: keep in sync with normalize_code */
84 const char *err_pages[70] = {
85 [CANNOT_FETCH] = "# Couldn't load the page\n",
86 [TOO_MUCH_REDIRECTS] = "# Too much redirects\n",
87 [MALFORMED_RESPONSE] = "# Got a malformed response\n",
88 [UNKNOWN_TYPE_OR_CSET] = "# Unsupported type or charset\n",
89 [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
91 [10] = "# Input required\n",
92 [11] = "# Input required\n",
93 [40] = "# Temporary failure\n",
94 [41] = "# Server unavailable\n",
95 [42] = "# CGI error\n",
96 [43] = "# Proxy error\n",
97 [44] = "# Slow down\n",
98 [50] = "# Permanent failure\n",
99 [51] = "# Not found\n",
101 [53] = "# Proxy request refused\n",
102 [59] = "# Bad request\n",
103 [60] = "# Client certificate required\n",
104 [61] = "# Certificate not authorised\n",
105 [62] = "# Certificate not valid\n"
108 static void die(void) __attribute__((__noreturn__));
109 static struct tab *tab_by_id(uint32_t);
110 static void handle_imsg_err(struct imsg *, size_t);
111 static void handle_imsg_check_cert(struct imsg *, size_t);
112 static void handle_check_cert_user_choice(int, struct tab *);
113 static void handle_maybe_save_new_cert(int, struct tab *);
114 static void handle_imsg_got_code(struct imsg *, size_t);
115 static void handle_imsg_got_meta(struct imsg *, size_t);
116 static void handle_maybe_save_page(int, struct tab *);
117 static void handle_save_page_path(const char *, struct tab *);
118 static void handle_imsg_file_opened(struct imsg *, size_t);
119 static void handle_imsg_buf(struct imsg *, size_t);
120 static void handle_imsg_eof(struct imsg *, size_t);
121 static void handle_imsg_tofu(struct imsg *, size_t);
122 static void handle_imsg_bookmark_ok(struct imsg *, size_t);
123 static void handle_imsg_save_cert_ok(struct imsg *, size_t);
124 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
125 static void handle_imsg_session(struct imsg *, size_t);
126 static void handle_dispatch_imsg(int, short, void *);
127 static int load_about_url(struct tab *, const char *);
128 static int load_file_url(struct tab *, const char *);
129 static int load_finger_url(struct tab *, const char *);
130 static int load_gemini_url(struct tab *, const char *);
131 static int load_gopher_url(struct tab *, const char *);
132 static int load_via_proxy(struct tab *, const char *,
134 static int make_request(struct tab *, struct get_req *, int,
136 static int make_fs_request(struct tab *, int, const char *);
137 static int do_load_url(struct tab *, const char *, const char *, int);
138 static pid_t start_child(enum telescope_process, const char *, int);
140 static struct proto {
143 int (*loadfn)(struct tab *, const char *);
145 {"about", NULL, load_about_url},
146 {"file", NULL, load_file_url},
147 {"finger", "79", load_finger_url},
148 {"gemini", "1965", load_gemini_url},
149 {"gopher", "70", load_gopher_url},
153 static imsg_handlerfn *handlers[] = {
154 [IMSG_ERR] = handle_imsg_err,
155 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
156 [IMSG_GOT_CODE] = handle_imsg_got_code,
157 [IMSG_GOT_META] = handle_imsg_got_meta,
158 [IMSG_BUF] = handle_imsg_buf,
159 [IMSG_EOF] = handle_imsg_eof,
160 [IMSG_TOFU] = handle_imsg_tofu,
161 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
162 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
163 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
164 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
165 [IMSG_SESSION_TAB] = handle_imsg_session,
166 [IMSG_SESSION_TAB_HIST] = handle_imsg_session,
167 [IMSG_SESSION_END] = handle_imsg_session,
170 static struct ohash certs;
172 static void __attribute__((__noreturn__))
179 tab_by_id(uint32_t id)
183 TAILQ_FOREACH(t, &tabshead, tabs) {
192 handle_imsg_err(struct imsg *imsg, size_t datalen)
197 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
201 page[datalen-1] = '\0';
203 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
204 tab->hist_cur->h, page) == -1)
206 load_page_from_str(tab, page);
211 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
213 const char *hash, *host, *port;
215 struct tofu_entry *e;
219 if (hash[datalen-1] != '\0')
222 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
225 if (tab->proxy != NULL) {
226 host = tab->proxy->host;
227 port = tab->proxy->port;
229 host = tab->uri.host;
230 port = tab->uri.port;
233 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
234 /* TODO: an update in libressl/libretls changed
235 * significantly. Find a better approach at storing
237 if (datalen > sizeof(e->hash))
240 tofu_res = 1; /* trust on first use */
241 if ((e = calloc(1, sizeof(*e))) == NULL)
243 strlcpy(e->domain, host, sizeof(e->domain));
244 if (*port != '\0' && strcmp(port, "1965")) {
245 strlcat(e->domain, ":", sizeof(e->domain));
246 strlcat(e->domain, port, sizeof(e->domain));
248 strlcpy(e->hash, hash, sizeof(e->hash));
250 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
252 tofu_res = !strcmp(hash, e->hash);
255 if (e->verified == -1)
256 tab->trust = TS_TEMP_TRUSTED;
257 else if (e->verified == 1)
258 tab->trust = TS_VERIFIED;
260 tab->trust = TS_TRUSTED;
262 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
263 &tofu_res, sizeof(tofu_res));
265 tab->trust = TS_UNTRUSTED;
266 load_page_from_str(tab, "# Certificate mismatch\n");
267 if ((tab->cert = strdup(hash)) == NULL)
269 ui_yornp("Certificate mismatch. Proceed?",
270 handle_check_cert_user_choice, tab);
275 handle_check_cert_user_choice(int accept, struct tab *tab)
277 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
282 * trust the certificate for this session only. If
283 * the page results in a redirect while we're asking
284 * the user to save, we'll end up with an invalid
285 * tabid (one request == one tab id). It also makes
286 * sense to save it for the current session if the
289 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port,
293 ui_yornp("Save the new certificate?",
294 handle_maybe_save_new_cert, tab);
296 message("Certificate temporarly trusted");
304 handle_maybe_save_new_cert(int accept, struct tab *tab)
306 struct tofu_entry *e;
307 const char *host, *port;
309 if (tab->proxy != NULL) {
310 host = tab->proxy->host;
311 port = tab->proxy->port;
313 host = tab->uri.host;
314 port = tab->uri.port;
320 if ((e = calloc(1, sizeof(*e))) == NULL)
323 strlcpy(e->domain, host, sizeof(e->domain));
324 if (*port != '\0' && strcmp(port, "1965")) {
325 strlcat(e->domain, ":", sizeof(e->domain));
326 strlcat(e->domain, port, sizeof(e->domain));
328 strlcpy(e->hash, tab->cert, sizeof(e->hash));
329 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
331 tofu_update(&certs, e);
333 tab->trust = TS_TRUSTED;
341 normalize_code(int n)
344 if (n == 10 || n == 11)
350 if (n == 30 || n == 31)
358 if (n <= 53 || n == 59)
366 return MALFORMED_RESPONSE;
370 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
374 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
377 if (sizeof(tab->code) != datalen)
380 memcpy(&tab->code, imsg->data, sizeof(tab->code));
381 tab->code = normalize_code(tab->code);
382 if (tab->code != 30 && tab->code != 31)
383 tab->redirect_count = 0;
387 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
392 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
395 if (sizeof(tab->meta) <= datalen)
398 memcpy(tab->meta, imsg->data, datalen);
400 if (tab->code < 10) { /* internal errors */
401 load_page_from_str(tab, err_pages[tab->code]);
402 } else if (tab->code < 20) { /* 1x */
403 load_page_from_str(tab, err_pages[tab->code]);
404 ui_require_input(tab, tab->code == 11, PROTO_GEMINI);
405 } else if (tab->code == 20) {
406 if (setup_parser_for(tab)) {
407 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
408 } else if (safe_mode) {
409 load_page_from_str(tab,
410 err_pages[UNKNOWN_TYPE_OR_CSET]);
414 if ((h = hist_pop(&tab->hist)) != NULL)
416 snprintf(buf, sizeof(buf),
417 "Can't display \"%s\", save it?", tab->meta);
418 ui_yornp(buf, handle_maybe_save_page, tab);
420 } else if (tab->code < 40) { /* 3x */
421 tab->redirect_count++;
423 /* TODO: make customizable? */
424 if (tab->redirect_count > 5) {
425 load_page_from_str(tab,
426 err_pages[TOO_MUCH_REDIRECTS]);
428 do_load_url(tab, tab->meta, NULL, LU_MODE_NOCACHE);
429 } else { /* 4x, 5x & 6x */
430 load_page_from_str(tab, err_pages[tab->code]);
435 handle_maybe_save_page(int dosave, struct tab *tab)
438 char input[PATH_MAX];
440 /* XXX: this print a message that is confusing */
441 ui_on_tab_loaded(tab);
448 if ((f = strrchr(tab->uri.path, '/')) == NULL)
453 strlcpy(input, download_path, sizeof(input));
454 strlcat(input, f, sizeof(input));
456 ui_read("Save to path", handle_save_page_path, tab, input);
460 handle_save_page_path(const char *path, struct tab *tab)
467 ui_show_downloads_pane();
469 enqueue_download(tab->id, path);
470 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
473 * Change this tab id, the old one is associated with the
476 tab->id = tab_new_id();
480 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
486 * There are no reason we shouldn't be able to find the
489 if ((d = download_by_id(imsg->hdr.peerid)) == NULL)
492 if (imsg->fd == -1) {
494 if (e[datalen-1] != '\0')
496 message("Can't open file %s: %s", d->path, e);
499 ui_send_net(IMSG_PROCEED, d->id, NULL, 0);
504 handle_imsg_session(struct imsg *imsg, size_t datalen)
506 static struct tab *curr;
507 static struct tab *tab;
508 struct session_tab st;
509 struct session_tab_hist sth;
514 * The fs process tried to send tabs after it has announced
515 * that he's done. Something fishy is going on, better die.
520 switch (imsg->hdr.type) {
521 case IMSG_SESSION_TAB:
522 if (datalen != sizeof(st))
525 memcpy(&st, imsg->data, sizeof(st));
526 if ((tab = new_tab(st.uri, NULL, NULL)) == NULL)
528 tab->hist_cur->line_off = st.top_line;
529 tab->hist_cur->current_off = st.current_line;
530 strlcpy(tab->buffer.page.title, st.title,
531 sizeof(tab->buffer.page.title));
532 if (st.flags & TAB_CURRENT)
534 if (st.flags & TAB_KILLED)
538 case IMSG_SESSION_TAB_HIST:
539 if (tab == NULL || datalen != sizeof(sth))
542 memcpy(&sth, imsg->data, sizeof(sth));
543 if (sth.uri[sizeof(sth.uri)-1] != '\0')
546 if ((h = calloc(1, sizeof(*h))) == NULL)
548 strlcpy(h->h, sth.uri, sizeof(h->h));
551 hist_push(&tab->hist, h);
553 hist_add_before(&tab->hist, tab->hist_cur, h);
556 case IMSG_SESSION_END:
557 if (datalen != sizeof(first_time))
559 memcpy(&first_time, imsg->data, sizeof(first_time));
561 new_tab("about:new", NULL, NULL);
562 curr = new_tab("about:help", NULL, NULL);
568 if (has_url || TAILQ_EMPTY(&tabshead))
569 new_tab(url, NULL, NULL);
579 handle_imsg_buf(struct imsg *imsg, size_t datalen)
581 struct tab *tab = NULL;
582 struct download *d = NULL;
584 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
585 (d = download_by_id(imsg->hdr.peerid)) == NULL)
589 if (!parser_parse(tab, imsg->data, datalen))
591 ui_on_tab_refresh(tab);
594 write(d->fd, imsg->data, datalen);
595 ui_on_download_refresh();
600 handle_imsg_eof(struct imsg *imsg, size_t datalen)
602 struct tab *tab = NULL;
603 struct download *d = NULL;
605 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL &&
606 (d = download_by_id(imsg->hdr.peerid)) == NULL)
610 if (!parser_free(tab))
612 if (has_prefix(tab->hist_cur->h, "gemini://") ||
613 has_prefix(tab->hist_cur->h, "gopher://"))
615 ui_on_tab_refresh(tab);
616 ui_on_tab_loaded(tab);
620 ui_on_download_refresh();
625 handle_imsg_tofu(struct imsg *imsg, size_t datalen)
627 struct tofu_entry *e;
632 if ((e = calloc(1, sizeof(*e))) == NULL)
635 if (datalen != sizeof(*e))
637 memcpy(e, imsg->data, sizeof(*e));
638 if (e->domain[sizeof(e->domain)-1] != '\0' ||
639 e->hash[sizeof(e->hash)-1] != '\0')
645 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
649 if (datalen != sizeof(res))
652 memcpy(&res, imsg->data, sizeof(res));
654 message("Added to bookmarks!");
656 message("Failed to add to bookmarks: %s",
661 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
665 if (datalen != sizeof(res))
667 memcpy(&res, imsg->data, datalen);
669 message("Failed to save the cert for: %s",
674 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
678 if (datalen != sizeof(res))
680 memcpy(&res, imsg->data, datalen);
682 message("Failed to update the certificate");
686 handle_dispatch_imsg(int fd, short ev, void *d)
688 struct imsgev *iev = d;
690 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
691 err(1, "connection closed");
695 load_about_url(struct tab *tab, const char *url)
697 tab->trust = TS_UNKNOWN;
698 parser_init(tab, gemtext_initparser);
699 return make_fs_request(tab, IMSG_GET, url);
703 load_file_url(struct tab *tab, const char *url)
705 tab->trust = TS_UNKNOWN;
706 return make_fs_request(tab, IMSG_GET_FILE, tab->uri.path);
710 load_finger_url(struct tab *tab, const char *url)
716 memset(&req, 0, sizeof(req));
717 strlcpy(req.port, tab->uri.port, sizeof(req.port));
720 * Sometimes the finger url have the user as path component
721 * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
722 * userinfo (e.g. finger://cobradile@finger.farm).
724 if ((at = strchr(tab->uri.host, '@')) != NULL) {
725 len = at - tab->uri.host;
726 memcpy(req.req, tab->uri.host, len);
728 if (len >= sizeof(req.req))
732 strlcpy(req.host, at+1, sizeof(req.host));
734 strlcpy(req.host, tab->uri.host, sizeof(req.host));
736 /* +1 to skip the initial `/' */
737 strlcpy(req.req, tab->uri.path+1, sizeof(req.req));
739 strlcat(req.req, "\r\n", sizeof(req.req));
741 parser_init(tab, textplain_initparser);
742 return make_request(tab, &req, PROTO_FINGER, NULL);
746 load_gemini_url(struct tab *tab, const char *url)
750 memset(&req, 0, sizeof(req));
751 strlcpy(req.host, tab->uri.host, sizeof(req.host));
752 strlcpy(req.port, tab->uri.port, sizeof(req.port));
754 return make_request(tab, &req, PROTO_GEMINI, tab->hist_cur->h);
757 static inline const char *
758 gopher_skip_selector(const char *path, int *ret_type)
762 if (!strcmp(path, "/") || *path == '\0') {
771 switch (*ret_type = *path) {
787 load_gopher_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 path = gopher_skip_selector(tab->uri.path, &type);
800 parser_init(tab, textplain_initparser);
803 parser_init(tab, gophermap_initparser);
806 ui_require_input(tab, 0, PROTO_GOPHER);
807 return load_page_from_str(tab, err_pages[10]);
809 return load_page_from_str(tab, "Unknown gopher selector");
812 strlcpy(req.req, path, sizeof(req.req));
813 if (*tab->uri.query != '\0') {
814 strlcat(req.req, "?", sizeof(req.req));
815 strlcat(req.req, tab->uri.query, sizeof(req.req));
817 strlcat(req.req, "\r\n", sizeof(req.req));
819 return make_request(tab, &req, PROTO_GOPHER, NULL);
823 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
827 memset(&req, 0, sizeof(req));
828 strlcpy(req.host, p->host, sizeof(req.host));
829 strlcpy(req.port, p->port, sizeof(req.port));
833 return make_request(tab, &req, p->proto, tab->hist_cur->h);
837 make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
840 tab->id = tab_new_id();
844 strlcpy(req->req, r, sizeof(req->req));
845 strlcat(req->req, "\r\n", sizeof(req->req));
848 start_loading_anim(tab);
849 ui_send_net(IMSG_GET_RAW, tab->id, req, sizeof(*req));
852 * So the various load_*_url can `return make_request` and
853 * do_load_url is happy.
859 make_fs_request(struct tab *tab, int type, const char *r)
862 tab->id = tab_new_id();
864 ui_send_fs(type, tab->id, r, strlen(r)+1);
867 * So load_{about,file}_url can `return make_fs_request` and
868 * do_load_url is happy.
874 gopher_send_search_req(struct tab *tab, const char *text)
878 memset(&req, 0, sizeof(req));
879 strlcpy(req.host, tab->uri.host, sizeof(req.host));
880 strlcpy(req.port, tab->uri.port, sizeof(req.port));
883 strlcpy(req.req, tab->uri.path+2, sizeof(req.req));
884 if (*tab->uri.query != '\0') {
885 strlcat(req.req, "?", sizeof(req.req));
886 strlcat(req.req, tab->uri.query, sizeof(req.req));
889 strlcat(req.req, "\t", sizeof(req.req));
890 strlcat(req.req, text, sizeof(req.req));
891 strlcat(req.req, "\r\n", sizeof(req.req));
893 erase_buffer(&tab->buffer);
894 parser_init(tab, gophermap_initparser);
896 make_request(tab, &req, PROTO_GOPHER, NULL);
900 * Effectively load the given url in the given tab. Return 1 when
901 * loading the page asynchronously, and thus when an erase_buffer can
902 * be done right after this function return, or 0 when loading the
903 * page synchronously.
906 do_load_url(struct tab *tab, const char *url, const char *base, int mode)
911 int nocache = mode & LU_MODE_NOCACHE;
915 tab->trust = TS_UNKNOWN;
918 memcpy(&uri, &tab->uri, sizeof(tab->uri));
920 phos_parse_absolute_uri(base, &uri);
922 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
923 if (asprintf(&t, "#error loading %s\n>%s\n",
924 url, "Can't parse the URI") == -1)
926 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
927 load_page_from_str(tab, t);
932 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
933 sizeof(tab->hist_cur->h));
935 if (!nocache && mcache_lookup(tab->hist_cur->h, tab)) {
936 ui_on_tab_refresh(tab);
937 ui_on_tab_loaded(tab);
941 for (p = protos; p->schema != NULL; ++p) {
942 if (!strcmp(tab->uri.scheme, p->schema)) {
944 if (*tab->uri.port == '\0' && p->port != NULL)
945 strlcpy(tab->uri.port, p->port,
946 sizeof(tab->uri.port));
948 return p->loadfn(tab, tab->hist_cur->h);
952 TAILQ_FOREACH(proxy, &proxies, proxies) {
953 if (!strcmp(tab->uri.scheme, proxy->match_proto))
954 return load_via_proxy(tab, url, proxy);
957 return load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
961 * Load url in tab and handle history. If a tab is marked as lazy, only
962 * prepare the url but don't load it.
965 load_url(struct tab *tab, const char *url, const char *base, int mode)
967 int lazy = tab->flags & TAB_LAZY;
968 int nohist = mode & LU_MODE_NOHIST;
970 if (!nohist && (!lazy || tab->hist_cur == NULL)) {
971 if (tab->hist_cur != NULL) {
972 get_scroll_position(tab, &tab->hist_cur->line_off,
973 &tab->hist_cur->current_off);
975 hist_clear_forward(&tab->hist,
976 TAILQ_NEXT(tab->hist_cur, entries));
979 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur)))
985 strlcpy(tab->buffer.page.title, url,
986 sizeof(tab->buffer.page.title));
987 hist_push(&tab->hist, tab->hist_cur);
990 strlcpy(tab->hist_cur->h, url,
991 sizeof(tab->hist_cur->h));
995 do_load_url(tab, url, base, mode);
999 load_url_in_tab(struct tab *tab, const char *url, const char *base, int mode)
1003 message("Loading %s...", url);
1006 load_url(tab, url, base, mode);
1010 load_previous_page(struct tab *tab)
1014 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
1017 do_load_url(tab, h->h, NULL, LU_MODE_NONE);
1022 load_next_page(struct tab *tab)
1026 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
1029 do_load_url(tab, h->h, NULL, LU_MODE_NONE);
1034 add_to_bookmarks(const char *str)
1036 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
1037 str, strlen(str)+1);
1041 * Given a user-entered URL, apply some heuristics to use it:
1043 * - if it's a proper url use it
1044 * - if it starts with a `./' or a `/' assume its a file:// url
1045 * - assume it's a gemini:// url
1047 * `ret' (of which len is the size) will be filled with the resulting
1051 humanify_url(const char *raw, char *ret, size_t len)
1053 struct phos_uri uri;
1056 if (phos_parse_absolute_uri(raw, &uri)) {
1057 strlcpy(ret, raw, len);
1061 if (has_prefix(raw, "./")) {
1062 strlcpy(ret, "file://", len);
1063 getcwd(buf, sizeof(buf));
1064 strlcat(ret, buf, len);
1065 strlcat(ret, "/", len);
1066 strlcat(ret, raw+2, len);
1071 strlcpy(ret, "file://", len);
1072 strlcat(ret, raw, len);
1076 strlcpy(ret, "gemini://", len);
1077 strlcat(ret, raw, len);
1081 start_child(enum telescope_process p, const char *argv0, int fd)
1083 const char *argv[5];
1087 switch (pid = fork()) {
1097 if (dup2(fd, 3) == -1)
1098 err(1, "cannot setup imsg fd");
1100 argv[argc++] = argv0;
1103 errx(1, "Can't start ui process");
1105 argv[argc++] = "-Tf";
1108 argv[argc++] = "-Tn";
1113 argv[argc++] = "-S";
1115 argv[argc++] = NULL;
1116 execvp(argv0, (char *const *)argv);
1117 err(1, "execvp(%s)", argv0);
1121 ui_send_net(int type, uint32_t peerid, const void *data,
1124 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
1129 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
1131 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
1135 static void __attribute__((noreturn))
1138 fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1140 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1145 main(int argc, char * const *argv)
1147 struct imsgev net_ibuf, fs_ibuf;
1149 int pipe2net[2], pipe2fs[2];
1150 int ch, configtest = 0, fail = 0;
1158 signal(SIGCHLD, SIG_IGN);
1159 signal(SIGPIPE, SIG_IGN);
1161 if (getenv("NO_COLOR") != NULL)
1166 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1169 exit(ui_print_colors());
1172 strlcpy(config_path, optarg, sizeof(config_path));
1191 errx(1, "invalid process spec %c",
1196 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1210 else if (proc == PROC_FS)
1212 else if (proc == PROC_NET)
1220 humanify_url(argv[0], url, sizeof(url));
1223 /* setup keys before reading the config */
1224 TAILQ_INIT(&global_map.m);
1225 global_map.unhandled_input = global_key_unbound;
1226 TAILQ_INIT(&minibuffer_map.m);
1229 parseconfig(config_path, fail);
1235 if (download_path == NULL &&
1236 (download_path = strdup("/tmp/")) == NULL)
1239 if (!safe_mode && (sessionfd = lock_session()) == -1)
1240 errx(1, "can't lock session, is another instance of "
1241 "telescope already running?");
1243 /* Start children. */
1244 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
1245 err(1, "socketpair");
1246 start_child(PROC_FS, argv0, pipe2fs[1]);
1247 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
1249 iev_fs->handler = handle_dispatch_imsg;
1251 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1252 err(1, "socketpair");
1253 start_child(PROC_NET, argv0, pipe2net[1]);
1254 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1255 iev_net = &net_ibuf;
1256 iev_net->handler = handle_dispatch_imsg;
1260 /* initialize tofu store */
1261 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1265 /* initialize the in-memory cache store */
1268 /* Setup event handler for the autosave */
1271 /* Setup event handlers for pipes to fs/net */
1272 iev_fs->events = EV_READ;
1273 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
1274 iev_fs->handler, iev_fs);
1275 event_add(&iev_fs->ev, NULL);
1277 iev_net->events = EV_READ;
1278 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
1279 iev_net->handler, iev_net);
1280 event_add(&iev_net->ev, NULL);
1283 sandbox_ui_process();
1284 ui_send_fs(IMSG_INIT, 0, NULL, 0);
1289 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
1290 ui_send_net(IMSG_QUIT, 0, NULL, 0);
1291 imsg_flush(&iev_fs->ibuf);
1292 imsg_flush(&iev_net->ibuf);
1294 /* wait for children to terminate */
1296 pid = wait(&status);
1298 if (errno != EINTR && errno != ECHILD)
1300 } else if (WIFSIGNALED(status))
1301 warnx("child terminated; signal %d", WTERMSIG(status));
1302 } while (pid != -1 || (pid == -1 && errno == EINTR));
1304 if (!safe_mode && close(sessionfd) == -1)
1305 err(1, "close(sessionfd = %d)", sessionfd);