2 44008d59 2024-01-16 op * Copyright (c) 2021, 2024 Omar Polo <op@omarpolo.com>
4 4540067e 2021-07-18 op * Permission to use, copy, modify, and distribute this software for any
5 4540067e 2021-07-18 op * purpose with or without fee is hereby granted, provided that the above
6 4540067e 2021-07-18 op * copyright notice and this permission notice appear in all copies.
8 4540067e 2021-07-18 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 4540067e 2021-07-18 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 4540067e 2021-07-18 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 4540067e 2021-07-18 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 4540067e 2021-07-18 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 4540067e 2021-07-18 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 4540067e 2021-07-18 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1f496e7b 2021-07-21 op #include "compat.h"
19 5e11c00c 2021-03-02 op #include <sys/socket.h>
20 98d3e6c1 2024-02-18 op #include <sys/time.h>
21 4cf6ba13 2022-02-11 op #include <sys/un.h>
22 be97d6e6 2021-08-15 op #include <sys/wait.h>
24 be97d6e6 2021-08-15 op #include <errno.h>
25 f63b8f73 2022-04-24 op #include <fcntl.h>
26 dc761924 2021-07-15 op #include <getopt.h>
27 f63b8f73 2022-04-24 op #include <limits.h>
28 5e11c00c 2021-03-02 op #include <signal.h>
29 5e11c00c 2021-03-02 op #include <stdio.h>
30 5e11c00c 2021-03-02 op #include <stdlib.h>
31 5e11c00c 2021-03-02 op #include <string.h>
32 5e11c00c 2021-03-02 op #include <unistd.h>
34 d35e18b3 2024-02-04 op #include "certs.h"
35 d252d87c 2024-02-05 op #include "cmd.h"
36 4cf6ba13 2022-02-11 op #include "control.h"
37 e659558c 2021-07-13 op #include "defaults.h"
38 98d3e6c1 2024-02-18 op #include "ev.h"
39 eb2ed626 2021-10-07 op #include "fs.h"
40 65c49665 2024-01-23 op #include "hist.h"
41 87d297d1 2024-02-22 op #include "imsgev.h"
42 44008d59 2024-01-16 op #include "iri.h"
43 f7db6d13 2024-02-06 op #include "keymap.h"
44 d5af38cc 2022-01-10 op #include "mcache.h"
45 4bc446b9 2021-07-21 op #include "minibuffer.h"
46 395b9f4e 2021-07-12 op #include "parser.h"
47 1fce2e75 2021-08-14 op #include "session.h"
48 395b9f4e 2021-07-12 op #include "telescope.h"
49 d163c210 2024-02-06 op #include "tofu.h"
50 d1a0f2a3 2021-07-12 op #include "ui.h"
51 9d65b1d9 2022-01-11 op #include "utils.h"
53 0d877486 2022-02-24 op static const struct option longopts[] = {
54 dc761924 2021-07-15 op {"help", no_argument, NULL, 'h'},
55 d0971653 2021-09-15 op {"safe", no_argument, NULL, 'S'},
56 dc761924 2021-07-15 op {"version", no_argument, NULL, 'v'},
57 dc761924 2021-07-15 op {NULL, 0, NULL, 0},
60 d08fccd2 2024-01-23 op static const char *opts = "c:hnST:v";
62 bb28f1c2 2021-12-30 op static int has_url;
63 bb28f1c2 2021-12-30 op static char url[GEMINI_URL_LEN];
66 a37d1a1c 2021-08-14 op * Used to know when we're finished loading.
68 1fce2e75 2021-08-14 op int operating;
71 d0971653 2021-09-15 op * "Safe" (or "sandobox") mode. If enabled, Telescope shouldn't write
72 d0971653 2021-09-15 op * anything to the filesystem or execute external programs.
74 d0971653 2021-09-15 op int safe_mode;
76 f63b8f73 2022-04-24 op static struct imsgev *iev_net;
78 f6a429eb 2021-07-10 op struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
79 6c74799d 2022-01-05 op struct tabshead ktabshead = TAILQ_HEAD_INITIALIZER(ktabshead);
80 f6a429eb 2021-07-10 op struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
82 6cc5fcfe 2021-07-08 op enum telescope_process {
87 9a8a7402 2021-07-18 op #define CANNOT_FETCH 0
88 ef88dc16 2024-02-02 op #define TOO_MANY_REDIRECTS 1
89 9a8a7402 2021-07-18 op #define MALFORMED_RESPONSE 2
90 9a8a7402 2021-07-18 op #define UNKNOWN_TYPE_OR_CSET 3
91 9a8a7402 2021-07-18 op #define UNKNOWN_PROTOCOL 4
93 a21e0756 2021-08-14 op /* XXX: keep in sync with normalize_code */
94 9a8a7402 2021-07-18 op const char *err_pages[70] = {
95 9a8a7402 2021-07-18 op [CANNOT_FETCH] = "# Couldn't load the page\n",
96 ef88dc16 2024-02-02 op [TOO_MANY_REDIRECTS] = "# Too many redirects\n",
97 9a8a7402 2021-07-18 op [MALFORMED_RESPONSE] = "# Got a malformed response\n",
98 5014b6a9 2024-02-02 op [UNKNOWN_TYPE_OR_CSET] = "# Unsupported document format or charset\n",
99 9a8a7402 2021-07-18 op [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
101 9a8a7402 2021-07-18 op [10] = "# Input required\n",
102 9a8a7402 2021-07-18 op [11] = "# Input required\n",
103 9a8a7402 2021-07-18 op [40] = "# Temporary failure\n",
104 9a8a7402 2021-07-18 op [41] = "# Server unavailable\n",
105 9a8a7402 2021-07-18 op [42] = "# CGI error\n",
106 9a8a7402 2021-07-18 op [43] = "# Proxy error\n",
107 9a8a7402 2021-07-18 op [44] = "# Slow down\n",
108 9a8a7402 2021-07-18 op [50] = "# Permanent failure\n",
109 9a8a7402 2021-07-18 op [51] = "# Not found\n",
110 9a8a7402 2021-07-18 op [52] = "# Gone\n",
111 9a8a7402 2021-07-18 op [53] = "# Proxy request refused\n",
112 9a8a7402 2021-07-18 op [59] = "# Bad request\n",
113 9a8a7402 2021-07-18 op [60] = "# Client certificate required\n",
114 9a8a7402 2021-07-18 op [61] = "# Certificate not authorised\n",
115 9a8a7402 2021-07-18 op [62] = "# Certificate not valid\n"
118 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
119 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
120 b148be71 2024-01-31 op static void handle_imsg_check_cert(struct imsg *);
121 a2fd3805 2021-07-06 op static void handle_check_cert_user_choice(int, struct tab *);
122 a2fd3805 2021-07-06 op static void handle_maybe_save_new_cert(int, struct tab *);
123 a2fd3805 2021-07-06 op static void handle_maybe_save_page(int, struct tab *);
124 d1353324 2021-07-13 op static void handle_save_page_path(const char *, struct tab *);
125 98d3e6c1 2024-02-18 op static void handle_dispatch_imsg(int, int, void *);
126 cd1d043d 2024-01-13 op static void load_about_url(struct tab *, const char *);
127 cd1d043d 2024-01-13 op static void load_file_url(struct tab *, const char *);
128 cd1d043d 2024-01-13 op static void load_finger_url(struct tab *, const char *);
129 cd1d043d 2024-01-13 op static void load_gemini_url(struct tab *, const char *);
130 cd1d043d 2024-01-13 op static void load_gopher_url(struct tab *, const char *);
131 cd1d043d 2024-01-13 op static void load_via_proxy(struct tab *, const char *,
132 cd1d043d 2024-01-13 op struct proxy *);
133 cd1d043d 2024-01-13 op static void make_request(struct tab *, struct get_req *, int,
134 71bc1636 2024-02-06 op const char *);
135 cd1d043d 2024-01-13 op static void do_load_url(struct tab *, const char *, const char *, int);
136 6cc5fcfe 2021-07-08 op static pid_t start_child(enum telescope_process, const char *, int);
137 4cf6ba13 2022-02-11 op static void send_url(const char *);
139 0d877486 2022-02-24 op static const struct proto {
140 352e0355 2021-07-25 op const char *schema;
141 ba782cb8 2021-07-25 op const char *port;
142 cd1d043d 2024-01-13 op void (*loadfn)(struct tab *, const char *);
143 352e0355 2021-07-25 op } protos[] = {
144 ba782cb8 2021-07-25 op {"about", NULL, load_about_url},
145 0b1053d2 2021-08-13 op {"file", NULL, load_file_url},
146 ba782cb8 2021-07-25 op {"finger", "79", load_finger_url},
147 ba782cb8 2021-07-25 op {"gemini", "1965", load_gemini_url},
148 1663bf1e 2021-07-25 op {"gopher", "70", load_gopher_url},
149 ba782cb8 2021-07-25 op {NULL, NULL, NULL},
152 cbcc75fb 2021-03-17 op static struct ohash certs;
154 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
157 5e11c00c 2021-03-02 op abort(); /* TODO */
160 5e11c00c 2021-03-02 op static struct tab *
161 f6243012 2021-07-19 op tab_by_id(uint32_t id)
163 5e11c00c 2021-03-02 op struct tab *t;
165 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
166 5e11c00c 2021-03-02 op if (t->id == id)
174 b148be71 2024-01-31 op handle_imsg_check_cert(struct imsg *imsg)
177 62cbcdae 2024-02-02 op const char *host, *port;
178 cbcc75fb 2021-03-17 op int tofu_res;
179 62cbcdae 2024-02-02 op struct ibuf ibuf;
180 cbcc75fb 2021-03-17 op struct tofu_entry *e;
181 cbcc75fb 2021-03-17 op struct tab *tab;
182 b148be71 2024-01-31 op size_t datalen;
184 62cbcdae 2024-02-02 op if (imsg_get_ibuf(imsg, &ibuf) == -1 ||
185 62cbcdae 2024-02-02 op ibuf_borrow_str(&ibuf, &hash) == -1)
187 b148be71 2024-01-31 op datalen = strlen(hash);
189 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(imsg))) == NULL)
192 984245ce 2021-06-23 op if (tab->proxy != NULL) {
193 984245ce 2021-06-23 op host = tab->proxy->host;
194 984245ce 2021-06-23 op port = tab->proxy->port;
196 25905047 2024-01-16 op host = tab->iri.iri_host;
197 25905047 2024-01-16 op port = tab->iri.iri_portstr;
200 984245ce 2021-06-23 op if ((e = tofu_lookup(&certs, host, port)) == NULL) {
201 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
202 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
203 cbcc75fb 2021-03-17 op * the certs! */
204 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
207 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
208 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
210 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
211 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
212 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
213 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
215 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
216 4fa88f57 2022-04-24 op tofu_save(&certs, e);
218 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
220 5d1bac73 2021-03-25 op if (tofu_res) {
221 a2fd3805 2021-07-06 op if (e->verified == -1)
222 a2fd3805 2021-07-06 op tab->trust = TS_TEMP_TRUSTED;
223 a2fd3805 2021-07-06 op else if (e->verified == 1)
224 a2fd3805 2021-07-06 op tab->trust = TS_VERIFIED;
226 a2fd3805 2021-07-06 op tab->trust = TS_TRUSTED;
228 d35e18b3 2024-02-04 op ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid, -1,
229 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
231 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
232 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
233 288fd238 2021-04-25 op if ((tab->cert = strdup(hash)) == NULL)
235 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
236 a2fd3805 2021-07-06 op handle_check_cert_user_choice, tab);
241 a2fd3805 2021-07-06 op handle_check_cert_user_choice(int accept, struct tab *tab)
243 d35e18b3 2024-02-04 op ui_send_net(IMSG_CERT_STATUS, tab->id, -1, &accept,
244 bc10f6a5 2021-07-12 op sizeof(accept));
246 a2fd3805 2021-07-06 op if (accept) {
247 25905047 2024-01-16 op const char *host, *port;
249 25905047 2024-01-16 op host = tab->iri.iri_host;
250 25905047 2024-01-16 op port = tab->iri.iri_portstr;
253 a2fd3805 2021-07-06 op * trust the certificate for this session only. If
254 a2fd3805 2021-07-06 op * the page results in a redirect while we're asking
255 a2fd3805 2021-07-06 op * the user to save, we'll end up with an invalid
256 7b4e4feb 2021-08-15 op * tabid (one request == one tab id). It also makes
257 7b4e4feb 2021-08-15 op * sense to save it for the current session if the
258 7b4e4feb 2021-08-15 op * user accepted it.
260 25905047 2024-01-16 op tofu_temp_trust(&certs, host, port, tab->cert);
262 d0971653 2021-09-15 op if (!safe_mode)
263 d0971653 2021-09-15 op ui_yornp("Save the new certificate?",
264 d0971653 2021-09-15 op handle_maybe_save_new_cert, tab);
266 d0971653 2021-09-15 op message("Certificate temporarly trusted");
268 288fd238 2021-04-25 op free(tab->cert);
269 288fd238 2021-04-25 op tab->cert = NULL;
274 a2fd3805 2021-07-06 op handle_maybe_save_new_cert(int accept, struct tab *tab)
276 288fd238 2021-04-25 op struct tofu_entry *e;
277 984245ce 2021-06-23 op const char *host, *port;
279 984245ce 2021-06-23 op if (tab->proxy != NULL) {
280 984245ce 2021-06-23 op host = tab->proxy->host;
281 984245ce 2021-06-23 op port = tab->proxy->port;
283 25905047 2024-01-16 op host = tab->iri.iri_host;
284 25905047 2024-01-16 op port = tab->iri.iri_portstr;
290 fe2262ad 2021-05-12 op if ((e = calloc(1, sizeof(*e))) == NULL)
293 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
294 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
295 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
296 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
298 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
300 3078e1bc 2022-04-24 op tofu_update_persist(&certs, e);
302 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
305 288fd238 2021-04-25 op free(tab->cert);
306 288fd238 2021-04-25 op tab->cert = NULL;
309 5cd2ebb1 2021-03-11 op static inline int
310 5cd2ebb1 2021-03-11 op normalize_code(int n)
312 5cd2ebb1 2021-03-11 op if (n < 20) {
313 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
316 5cd2ebb1 2021-03-11 op } else if (n < 30) {
318 5cd2ebb1 2021-03-11 op } else if (n < 40) {
319 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
322 5cd2ebb1 2021-03-11 op } else if (n < 50) {
326 5cd2ebb1 2021-03-11 op } else if (n < 60) {
327 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
330 5cd2ebb1 2021-03-11 op } else if (n < 70) {
335 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
339 b148be71 2024-01-31 op handle_request_response(struct tab *tab)
341 fcd99a0d 2021-11-05 op char buf[128];
343 54764e41 2024-02-02 op if (tab->code != 30 && tab->code != 31)
344 54764e41 2024-02-02 op tab->redirect_count = 0;
346 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
347 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
348 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
349 ed504b9e 2022-02-07 op free(tab->last_input_url);
350 65c49665 2024-01-23 op tab->last_input_url = strdup(hist_cur(tab->hist));
351 ed504b9e 2022-02-07 op if (tab->last_input_url == NULL)
354 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
355 693188cb 2022-02-07 op ui_require_input(tab, tab->code == 11, ir_select_gemini);
356 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
357 65c49665 2024-01-23 op history_add(hist_cur(tab->hist));
358 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
359 d35e18b3 2024-02-04 op ui_send_net(IMSG_PROCEED, tab->id, -1, NULL, 0);
360 fcd99a0d 2021-11-05 op } else if (safe_mode) {
361 69f32f56 2021-08-16 op load_page_from_str(tab,
362 69f32f56 2021-08-16 op err_pages[UNKNOWN_TYPE_OR_CSET]);
364 65c49665 2024-01-23 op hist_prev(tab->hist);
365 fcd99a0d 2021-11-05 op snprintf(buf, sizeof(buf),
366 fcd99a0d 2021-11-05 op "Can't display \"%s\", save it?", tab->meta);
367 fcd99a0d 2021-11-05 op ui_yornp(buf, handle_maybe_save_page, tab);
369 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
370 3a9b9365 2021-03-09 op tab->redirect_count++;
372 3a9b9365 2021-03-09 op /* TODO: make customizable? */
373 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
374 3a9b9365 2021-03-09 op load_page_from_str(tab,
375 ef88dc16 2024-02-02 op err_pages[TOO_MANY_REDIRECTS]);
377 65c49665 2024-01-23 op do_load_url(tab, tab->meta, hist_cur(tab->hist),
378 33133ab1 2024-01-16 op LU_MODE_NOCACHE);
379 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
380 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
381 5a39f593 2024-02-05 op if (tab->code >= 60)
382 5a39f593 2024-02-05 op cmd_use_certificate(&tab->buffer);
387 a2fd3805 2021-07-06 op handle_maybe_save_page(int dosave, struct tab *tab)
389 bb9912eb 2021-08-29 op const char *f;
390 bb9912eb 2021-08-29 op char input[PATH_MAX];
392 fcd99a0d 2021-11-05 op /* XXX: this print a message that is confusing */
393 fcd99a0d 2021-11-05 op ui_on_tab_loaded(tab);
395 bb9912eb 2021-08-29 op if (!dosave) {
396 a2fd3805 2021-07-06 op stop_tab(tab);
400 25905047 2024-01-16 op if ((f = strrchr(tab->iri.iri_path, '/')) == NULL)
405 112b9046 2021-08-29 op strlcpy(input, download_path, sizeof(input));
406 bb9912eb 2021-08-29 op strlcat(input, f, sizeof(input));
408 bb9912eb 2021-08-29 op ui_read("Save to path", handle_save_page_path, tab, input);
412 d1353324 2021-07-13 op handle_save_page_path(const char *path, struct tab *tab)
414 f63b8f73 2022-04-24 op struct download *d;
417 de2a69bb 2021-05-17 op if (path == NULL) {
418 d1353324 2021-07-13 op stop_tab(tab);
422 ff48dbfd 2022-05-05 op if ((fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, 0644)) == -1) {
423 ff48dbfd 2022-05-05 op message("Can't open file %s: %s", path, strerror(errno));
424 ff48dbfd 2022-05-05 op stop_tab(tab);
428 ff48dbfd 2022-05-05 op ui_show_downloads_pane();
429 fd984e76 2022-05-05 op d = enqueue_download(tab->id, path);
431 d35e18b3 2024-02-04 op ui_send_net(IMSG_PROCEED, d->id, -1, NULL, 0);
434 89e46508 2022-05-05 op * Change this tab id, the old one is associated with the
435 89e46508 2022-05-05 op * download now.
437 89e46508 2022-05-05 op tab->id = tab_new_id();
441 98d3e6c1 2024-02-18 op handle_dispatch_imsg(int fd, int event, void *data)
443 b148be71 2024-01-31 op struct imsgev *iev = data;
444 b148be71 2024-01-31 op struct imsgbuf *imsgbuf = &iev->ibuf;
445 b148be71 2024-01-31 op struct imsg imsg;
446 54764e41 2024-02-02 op struct ibuf ibuf;
447 b148be71 2024-01-31 op struct tab *tab;
448 b148be71 2024-01-31 op struct download *d;
449 54764e41 2024-02-02 op const char *h;
450 54764e41 2024-02-02 op char *str, *page;
454 b148be71 2024-01-31 op if (event & EV_READ) {
455 b148be71 2024-01-31 op if ((n = imsg_read(imsgbuf)) == -1 && errno != EAGAIN)
456 b148be71 2024-01-31 op err(1, "imsg_read");
458 b148be71 2024-01-31 op err(1, "connection closed");
460 b148be71 2024-01-31 op if (event & EV_WRITE) {
461 b148be71 2024-01-31 op if ((n = msgbuf_write(&imsgbuf->w)) == -1 && errno != EAGAIN)
462 b148be71 2024-01-31 op err(1, "msgbuf_write");
464 b148be71 2024-01-31 op err(1, "connection closed");
468 b148be71 2024-01-31 op if ((n = imsg_get(imsgbuf, &imsg)) == -1)
469 b148be71 2024-01-31 op err(1, "imsg_get");
473 b148be71 2024-01-31 op switch (imsg_get_type(&imsg)) {
474 b148be71 2024-01-31 op case IMSG_ERR:
475 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL)
477 62cbcdae 2024-02-02 op if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
478 62cbcdae 2024-02-02 op ibuf_borrow_str(&ibuf, &str) == -1)
480 b148be71 2024-01-31 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
481 b148be71 2024-01-31 op hist_cur(tab->hist), str) == -1)
483 b148be71 2024-01-31 op load_page_from_str(tab, page);
486 b148be71 2024-01-31 op case IMSG_CHECK_CERT:
487 b148be71 2024-01-31 op handle_imsg_check_cert(&imsg);
489 54764e41 2024-02-02 op case IMSG_REPLY:
490 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL)
492 54764e41 2024-02-02 op if (imsg_get_ibuf(&imsg, &ibuf) == -1 ||
493 54764e41 2024-02-02 op ibuf_get(&ibuf, &code, sizeof(code)) == -1 ||
494 54764e41 2024-02-02 op ibuf_borrow_str(&ibuf, &str) == -1)
496 b148be71 2024-01-31 op if (strlcpy(tab->meta, str, sizeof(tab->meta)) >=
497 b148be71 2024-01-31 op sizeof(tab->meta))
499 54764e41 2024-02-02 op tab->code = normalize_code(code);
500 b148be71 2024-01-31 op handle_request_response(tab);
502 b148be71 2024-01-31 op case IMSG_BUF:
503 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
504 b148be71 2024-01-31 op ((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
508 b148be71 2024-01-31 op if (!parser_parse(tab, imsg.data,
509 b148be71 2024-01-31 op imsg_get_len(&imsg)))
511 b148be71 2024-01-31 op ui_on_tab_refresh(tab);
513 b148be71 2024-01-31 op size_t datalen = imsg_get_len(&imsg);
515 b148be71 2024-01-31 op d->bytes += datalen;
516 b148be71 2024-01-31 op write(d->fd, imsg.data, datalen);
517 b148be71 2024-01-31 op ui_on_download_refresh();
520 b148be71 2024-01-31 op case IMSG_EOF:
521 b148be71 2024-01-31 op if ((tab = tab_by_id(imsg_get_id(&imsg))) == NULL &&
522 b148be71 2024-01-31 op ((d = download_by_id(imsg_get_id(&imsg)))) == NULL)
525 b148be71 2024-01-31 op if (tab != NULL) {
526 b148be71 2024-01-31 op if (!parser_free(tab))
528 b148be71 2024-01-31 op h = hist_cur(tab->hist);
529 b148be71 2024-01-31 op if (!strncmp(h, "gemini://", 9) ||
530 b148be71 2024-01-31 op !strncmp(h, "gopher://", 9) ||
531 b148be71 2024-01-31 op !strncmp(h, "finger://", 9))
532 b148be71 2024-01-31 op mcache_tab(tab);
535 b148be71 2024-01-31 op * Gemini is handled as soon as a 2x
536 b148be71 2024-01-31 op * reply is got.
538 b148be71 2024-01-31 op if (!strncmp(h, "finger://", 9) ||
539 b148be71 2024-01-31 op !strncmp(h, "gopher://", 9))
540 b148be71 2024-01-31 op history_add(h);
542 b148be71 2024-01-31 op ui_on_tab_refresh(tab);
543 b148be71 2024-01-31 op ui_on_tab_loaded(tab);
545 b148be71 2024-01-31 op close(d->fd);
547 b148be71 2024-01-31 op ui_on_download_refresh();
551 b148be71 2024-01-31 op errx(1, "got unknown imsg %d", imsg_get_type(&imsg));
554 b148be71 2024-01-31 op imsg_free(&imsg);
557 b148be71 2024-01-31 op imsg_event_add(iev);
561 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
563 f63b8f73 2022-04-24 op tab->trust = TS_TRUSTED;
564 f63b8f73 2022-04-24 op fs_load_url(tab, url);
565 f63b8f73 2022-04-24 op ui_on_tab_refresh(tab);
566 f63b8f73 2022-04-24 op ui_on_tab_loaded(tab);
570 0b1053d2 2021-08-13 op load_file_url(struct tab *tab, const char *url)
572 f63b8f73 2022-04-24 op tab->trust = TS_TRUSTED;
573 f63b8f73 2022-04-24 op fs_load_url(tab, url);
574 f63b8f73 2022-04-24 op ui_on_tab_refresh(tab);
575 f63b8f73 2022-04-24 op ui_on_tab_loaded(tab);
579 5fb52fc0 2021-07-25 op load_finger_url(struct tab *tab, const char *url)
581 5fb52fc0 2021-07-25 op struct get_req req;
582 25905047 2024-01-16 op const char *path;
584 5fb52fc0 2021-07-25 op memset(&req, 0, sizeof(req));
585 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
586 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
589 5fb52fc0 2021-07-25 op * Sometimes the finger url have the user as path component
590 5fb52fc0 2021-07-25 op * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
591 5fb52fc0 2021-07-25 op * userinfo (e.g. finger://cobradile@finger.farm).
593 25905047 2024-01-16 op if (tab->iri.iri_flags & IH_UINFO) {
594 25905047 2024-01-16 op strlcpy(req.req, tab->iri.iri_uinfo, sizeof(req.req));
596 25905047 2024-01-16 op path = tab->iri.iri_path;
597 25905047 2024-01-16 op while (*path == '/')
599 25905047 2024-01-16 op strlcpy(req.req, path, sizeof(req.req));
601 5fb52fc0 2021-07-25 op strlcat(req.req, "\r\n", sizeof(req.req));
603 8c1b0837 2021-07-25 op parser_init(tab, textplain_initparser);
604 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_FINGER, NULL);
608 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
610 984245ce 2021-06-23 op struct get_req req;
612 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
613 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
614 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
616 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GEMINI, hist_cur(tab->hist));
619 ca190c99 2021-08-14 op static inline const char *
620 ca190c99 2021-08-14 op gopher_skip_selector(const char *path, int *ret_type)
622 ca190c99 2021-08-14 op *ret_type = 0;
624 ca190c99 2021-08-14 op if (!strcmp(path, "/") || *path == '\0') {
625 ca190c99 2021-08-14 op *ret_type = '1';
629 ca190c99 2021-08-14 op if (*path != '/')
633 ca190c99 2021-08-14 op switch (*ret_type = *path) {
640 ca190c99 2021-08-14 op *ret_type = 0;
645 95bb01e2 2021-08-28 op return ++path;
649 1663bf1e 2021-07-25 op load_gopher_url(struct tab *tab, const char *url)
651 81839fee 2021-07-25 op struct get_req req;
653 81839fee 2021-07-25 op const char *path;
655 1663bf1e 2021-07-25 op memset(&req, 0, sizeof(req));
656 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
657 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
659 25905047 2024-01-16 op path = gopher_skip_selector(tab->iri.iri_path, &type);
660 ca190c99 2021-08-14 op switch (type) {
662 8c1b0837 2021-07-25 op parser_init(tab, textplain_initparser);
665 ca190c99 2021-08-14 op parser_init(tab, gophermap_initparser);
668 acf9defe 2022-02-08 op free(tab->last_input_url);
669 acf9defe 2022-02-08 op tab->last_input_url = strdup(url);
670 acf9defe 2022-02-08 op if (tab->last_input_url == NULL)
672 693188cb 2022-02-07 op ui_require_input(tab, 0, ir_select_gopher);
673 cd1d043d 2024-01-13 op load_page_from_str(tab, err_pages[10]);
676 cd1d043d 2024-01-13 op load_page_from_str(tab, "Unknown gopher selector");
680 62c0f697 2024-02-21 op if (iri_urlunescape(path, req.req, sizeof(req.req)) == -1)
681 62c0f697 2024-02-21 op strlcpy(req.req, path, sizeof(req.req));
682 25905047 2024-01-16 op if (tab->iri.iri_flags & IH_QUERY) {
683 9a28e7e5 2021-08-03 op strlcat(req.req, "?", sizeof(req.req));
684 25905047 2024-01-16 op strlcat(req.req, tab->iri.iri_query, sizeof(req.req));
686 767da05c 2021-08-13 op strlcat(req.req, "\r\n", sizeof(req.req));
688 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GOPHER, NULL);
692 984245ce 2021-06-23 op load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
694 984245ce 2021-06-23 op struct get_req req;
696 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
697 984245ce 2021-06-23 op strlcpy(req.host, p->host, sizeof(req.host));
698 f94c7ac1 2021-11-24 op strlcpy(req.port, p->port, sizeof(req.port));
700 cf1195ce 2021-07-25 op tab->proxy = p;
702 71bc1636 2024-02-06 op make_request(tab, &req, p->proto, hist_cur(tab->hist));
706 71bc1636 2024-02-06 op make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
708 71bc1636 2024-02-06 op int use_cert = 0, fd = -1;
710 71bc1636 2024-02-06 op if (proto == PROTO_GEMINI) {
711 71bc1636 2024-02-06 op tab->client_cert = cert_for(&tab->iri, &tab->client_cert_temp);
712 71bc1636 2024-02-06 op use_cert = (tab->client_cert != NULL);
715 cf1195ce 2021-07-25 op stop_tab(tab);
716 cf1195ce 2021-07-25 op tab->id = tab_new_id();
717 cf1195ce 2021-07-25 op req->proto = proto;
719 cf1195ce 2021-07-25 op if (r != NULL) {
720 cf1195ce 2021-07-25 op strlcpy(req->req, r, sizeof(req->req));
721 cf1195ce 2021-07-25 op strlcat(req->req, "\r\n", sizeof(req->req));
724 5df02e0d 2022-01-11 op start_loading_anim(tab);
726 d35e18b3 2024-02-04 op if (!use_cert)
727 d35e18b3 2024-02-04 op tab->client_cert = NULL;
728 d35e18b3 2024-02-04 op if (use_cert && (fd = cert_open(tab->client_cert)) == -1) {
729 d35e18b3 2024-02-04 op tab->client_cert = NULL;
730 d35e18b3 2024-02-04 op message("failed to open certificate: %s", strerror(errno));
733 d35e18b3 2024-02-04 op ui_send_net(IMSG_GET, tab->id, fd, req, sizeof(*req));
737 9a28e7e5 2021-08-03 op gopher_send_search_req(struct tab *tab, const char *text)
739 9a28e7e5 2021-08-03 op struct get_req req;
741 9a28e7e5 2021-08-03 op memset(&req, 0, sizeof(req));
742 25905047 2024-01-16 op strlcpy(req.host, tab->iri.iri_host, sizeof(req.host));
743 25905047 2024-01-16 op strlcpy(req.port, tab->iri.iri_portstr, sizeof(req.port));
745 9a28e7e5 2021-08-03 op /* +2 to skip /7 */
746 25905047 2024-01-16 op strlcpy(req.req, tab->iri.iri_path+2, sizeof(req.req));
747 25905047 2024-01-16 op if (tab->iri.iri_flags & IH_QUERY) {
748 9a28e7e5 2021-08-03 op strlcat(req.req, "?", sizeof(req.req));
749 25905047 2024-01-16 op strlcat(req.req, tab->iri.iri_query, sizeof(req.req));
752 9a28e7e5 2021-08-03 op strlcat(req.req, "\t", sizeof(req.req));
753 9a28e7e5 2021-08-03 op strlcat(req.req, text, sizeof(req.req));
754 9a28e7e5 2021-08-03 op strlcat(req.req, "\r\n", sizeof(req.req));
756 9a28e7e5 2021-08-03 op erase_buffer(&tab->buffer);
757 9a28e7e5 2021-08-03 op parser_init(tab, gophermap_initparser);
759 71bc1636 2024-02-06 op make_request(tab, &req, PROTO_GOPHER, NULL);
763 1bcb8303 2022-01-19 op load_page_from_str(struct tab *tab, const char *page)
765 1bcb8303 2022-01-19 op parser_init(tab, gemtext_initparser);
766 1bcb8303 2022-01-19 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
768 1bcb8303 2022-01-19 op if (!tab->buffer.page.free(&tab->buffer.page))
770 1bcb8303 2022-01-19 op ui_on_tab_refresh(tab);
771 1bcb8303 2022-01-19 op ui_on_tab_loaded(tab);
775 cd1d043d 2024-01-13 op * Effectively load the given url in the given tab.
778 ed21a9a1 2022-01-11 op do_load_url(struct tab *tab, const char *url, const char *base, int mode)
780 0d877486 2022-02-24 op const struct proto *p;
781 984245ce 2021-06-23 op struct proxy *proxy;
782 8f3c9af8 2022-01-11 op int nocache = mode & LU_MODE_NOCACHE;
784 65c49665 2024-01-23 op char buf[1025];
786 7943bb81 2021-07-10 op tab->proxy = NULL;
787 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
789 25905047 2024-01-16 op if (iri_parse(base, url, &tab->iri) == -1) {
790 25905047 2024-01-16 op if (asprintf(&t, "# error loading %s\n>%s\n",
791 25905047 2024-01-16 op url, "Can't parse the IRI") == -1)
793 65c49665 2024-01-23 op hist_set_cur(tab->hist, url);
794 31f1a758 2021-04-22 op load_page_from_str(tab, t);
799 65c49665 2024-01-23 op iri_unparse(&tab->iri, buf, sizeof(buf));
800 65c49665 2024-01-23 op hist_set_cur(tab->hist, buf);
802 65c49665 2024-01-23 op if (!nocache && mcache_lookup(buf, tab)) {
803 8f3c9af8 2022-01-11 op ui_on_tab_refresh(tab);
804 8f3c9af8 2022-01-11 op ui_on_tab_loaded(tab);
808 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
809 25905047 2024-01-16 op if (!strcmp(tab->iri.iri_scheme, p->schema)) {
810 3b726df7 2024-01-16 op /* patch the port */
811 25905047 2024-01-16 op if (*tab->iri.iri_portstr == '\0' &&
812 25905047 2024-01-16 op p->port != NULL)
813 3b726df7 2024-01-16 op iri_setport(&tab->iri, p->port);
815 65c49665 2024-01-23 op p->loadfn(tab, buf);
820 984245ce 2021-06-23 op TAILQ_FOREACH(proxy, &proxies, proxies) {
821 25905047 2024-01-16 op if (!strcmp(tab->iri.iri_scheme, proxy->match_proto)) {
822 cd1d043d 2024-01-13 op load_via_proxy(tab, url, proxy);
827 cd1d043d 2024-01-13 op load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
831 ea639250 2021-01-02 op * Load url in tab and handle history. If a tab is marked as lazy, only
832 ea639250 2021-01-02 op * prepare the url but don't load it.
835 ed21a9a1 2022-01-11 op load_url(struct tab *tab, const char *url, const char *base, int mode)
837 65c49665 2024-01-23 op size_t line_off, curr_off;
838 ea639250 2021-01-02 op int lazy = tab->flags & TAB_LAZY;
839 65c49665 2024-01-23 op int dohist = !(mode & LU_MODE_NOHIST);
841 fb8dcd1c 2022-01-18 op if (operating && lazy) {
842 fb8dcd1c 2022-01-18 op tab->flags ^= TAB_LAZY;
844 65c49665 2024-01-23 op } else if (hist_size(tab->hist) != 0) {
845 65c49665 2024-01-23 op get_scroll_position(tab, &line_off, &curr_off);
846 65c49665 2024-01-23 op hist_set_offs(tab->hist, line_off, curr_off);
849 65c49665 2024-01-23 op if (dohist) {
850 65c49665 2024-01-23 op if (hist_push(tab->hist, url) == -1) {
855 2ddbda6b 2021-07-17 op strlcpy(tab->buffer.page.title, url,
856 2ddbda6b 2021-07-17 op sizeof(tab->buffer.page.title));
860 ed21a9a1 2022-01-11 op do_load_url(tab, url, base, mode);
864 ed21a9a1 2022-01-11 op load_url_in_tab(struct tab *tab, const char *url, const char *base, int mode)
866 0259f38d 2022-01-11 op if (operating) {
867 0259f38d 2022-01-11 op autosave_hook();
868 0259f38d 2022-01-11 op message("Loading %s...", url);
871 0f7d8271 2024-01-22 op if (base == NULL)
872 65c49665 2024-01-23 op base = hist_cur(tab->hist);
874 ed21a9a1 2022-01-11 op load_url(tab, url, base, mode);
878 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
880 65c49665 2024-01-23 op const char *h;
882 65c49665 2024-01-23 op if ((h = hist_prev(tab->hist)) == NULL)
884 65c49665 2024-01-23 op do_load_url(tab, h, NULL, LU_MODE_NONE);
889 2051e653 2021-03-13 op load_next_page(struct tab *tab)
891 65c49665 2024-01-23 op const char *h;
893 65c49665 2024-01-23 op if ((h = hist_next(tab->hist)) == NULL)
895 65c49665 2024-01-23 op do_load_url(tab, h, NULL, LU_MODE_NONE);
900 868b3a8f 2022-04-13 op write_buffer(const char *path, struct tab *tab)
904 868b3a8f 2022-04-13 op if (path == NULL)
907 f63b8f73 2022-04-24 op if ((fp = fopen(path, "w")) == NULL)
909 f63b8f73 2022-04-24 op if (!parser_serialize(tab, fp))
910 f63b8f73 2022-04-24 op message("Failed to save the page.");
915 449ea6fe 2024-01-16 op * Given a user-entered URL, apply some heuristics to use it if
916 449ea6fe 2024-01-16 op * load-url-use-heuristic allows it.
918 f6d7d047 2021-12-30 op * - if it's a proper url use it
919 f6d7d047 2021-12-30 op * - if it starts with a `./' or a `/' assume its a file:// url
920 cfcbc23c 2024-01-16 op * - assume it's a default-protocol:// url
922 f6d7d047 2021-12-30 op * `ret' (of which len is the size) will be filled with the resulting
926 449ea6fe 2024-01-16 op humanify_url(const char *raw, const char *base, char *ret, size_t len)
928 44008d59 2024-01-16 op static struct iri iri;
930 449ea6fe 2024-01-16 op if (load_url_use_heuristic)
933 449ea6fe 2024-01-16 op if (iri_parse(base, raw, &iri) == 0) {
934 44008d59 2024-01-16 op iri_unparse(&iri, ret, len);
938 d89eb764 2022-04-24 op if (!strncmp(raw, "./", 2)) {
939 c6efff96 2021-08-16 op strlcpy(ret, "file://", len);
940 449ea6fe 2024-01-16 op strlcat(ret, cwd, len);
941 c6efff96 2021-08-16 op strlcat(ret, "/", len);
942 c6efff96 2021-08-16 op strlcat(ret, raw+2, len);
946 c6efff96 2021-08-16 op if (*raw == '/') {
947 c6efff96 2021-08-16 op strlcpy(ret, "file://", len);
948 c6efff96 2021-08-16 op strlcat(ret, raw, len);
952 cfcbc23c 2024-01-16 op strlcpy(ret, default_protocol, len);
953 cfcbc23c 2024-01-16 op strlcat(ret, "://", len);
954 c6efff96 2021-08-16 op strlcat(ret, raw, len);
958 486bde7d 2022-04-24 op bookmark_page(const char *url)
962 486bde7d 2022-04-24 op if ((fp = fopen(bookmark_file, "a")) == NULL)
964 486bde7d 2022-04-24 op fprintf(fp, "=> %s\n", url);
970 6cc5fcfe 2021-07-08 op start_child(enum telescope_process p, const char *argv0, int fd)
972 55ccd6d0 2021-09-15 op const char *argv[5];
973 6cc5fcfe 2021-07-08 op int argc = 0;
976 6cc5fcfe 2021-07-08 op switch (pid = fork()) {
986 6cc5fcfe 2021-07-08 op if (dup2(fd, 3) == -1)
987 6cc5fcfe 2021-07-08 op err(1, "cannot setup imsg fd");
989 6cc5fcfe 2021-07-08 op argv[argc++] = argv0;
991 6cc5fcfe 2021-07-08 op case PROC_UI:
992 6cc5fcfe 2021-07-08 op errx(1, "Can't start ui process");
993 6cc5fcfe 2021-07-08 op case PROC_NET:
994 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tn";
998 55ccd6d0 2021-09-15 op if (safe_mode)
999 55ccd6d0 2021-09-15 op argv[argc++] = "-S";
1001 6cc5fcfe 2021-07-08 op argv[argc++] = NULL;
1002 6cc5fcfe 2021-07-08 op execvp(argv0, (char *const *)argv);
1003 6cc5fcfe 2021-07-08 op err(1, "execvp(%s)", argv0);
1007 4cf6ba13 2022-02-11 op send_url(const char *url)
1009 4cf6ba13 2022-02-11 op struct sockaddr_un sun;
1010 4cf6ba13 2022-02-11 op struct imsgbuf ibuf;
1011 4cf6ba13 2022-02-11 op int ctl_sock;
1013 4cf6ba13 2022-02-11 op if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1014 4cf6ba13 2022-02-11 op err(1, "socket");
1016 4cf6ba13 2022-02-11 op memset(&sun, 0, sizeof(sun));
1017 4cf6ba13 2022-02-11 op sun.sun_family = AF_UNIX;
1018 4cf6ba13 2022-02-11 op strlcpy(sun.sun_path, ctlsock_path, sizeof(sun.sun_path));
1020 4cf6ba13 2022-02-11 op if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
1021 4cf6ba13 2022-02-11 op err(1, "connect: %s", ctlsock_path);
1023 4cf6ba13 2022-02-11 op imsg_init(&ibuf, ctl_sock);
1024 4cf6ba13 2022-02-11 op imsg_compose(&ibuf, IMSG_CTL_OPEN_URL, 0, 0, -1, url,
1025 4cf6ba13 2022-02-11 op strlen(url) + 1);
1026 4cf6ba13 2022-02-11 op imsg_flush(&ibuf);
1027 4cf6ba13 2022-02-11 op close(ctl_sock);
1031 d35e18b3 2024-02-04 op ui_send_net(int type, uint32_t peerid, int fd, const void *data,
1032 bc10f6a5 2021-07-12 op uint16_t datalen)
1034 d35e18b3 2024-02-04 op return imsg_compose_event(iev_net, type, peerid, 0, fd, data,
1038 6cc5fcfe 2021-07-08 op static void __attribute__((noreturn))
1039 6cc5fcfe 2021-07-08 op usage(int r)
1041 335cfaec 2021-09-15 op fprintf(stderr, "USAGE: %s [-ChnSv] [-c config] [url]\n",
1042 dc761924 2021-07-15 op getprogname());
1043 d2544989 2021-07-08 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
1048 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
1050 f63b8f73 2022-04-24 op struct imsgev net_ibuf;
1052 4cf6ba13 2022-02-11 op int control_fd;
1053 f63b8f73 2022-04-24 op int pipe2net[2];
1054 d2544989 2021-07-08 op int ch, configtest = 0, fail = 0;
1055 6cc5fcfe 2021-07-08 op int proc = -1;
1056 5d7c642a 2021-09-15 op int sessionfd = -1;
1058 6cc5fcfe 2021-07-08 op const char *argv0;
1060 6cc5fcfe 2021-07-08 op argv0 = argv[0];
1062 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
1063 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
1065 d2544989 2021-07-08 op if (getenv("NO_COLOR") != NULL)
1066 d2544989 2021-07-08 op enable_colors = 0;
1068 dc761924 2021-07-15 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1069 d2544989 2021-07-08 op switch (ch) {
1072 eb2ed626 2021-10-07 op strlcpy(config_path, optarg, sizeof(config_path));
1075 d2544989 2021-07-08 op configtest = 1;
1080 d0971653 2021-09-15 op safe_mode = 1;
1083 6cc5fcfe 2021-07-08 op switch (*optarg) {
1085 6cc5fcfe 2021-07-08 op proc = PROC_NET;
1088 6cc5fcfe 2021-07-08 op errx(1, "invalid process spec %c",
1093 dc761924 2021-07-15 op printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1101 d2544989 2021-07-08 op argc -= optind;
1102 d2544989 2021-07-08 op argv += optind;
1104 6cc5fcfe 2021-07-08 op if (proc != -1) {
1105 6cc5fcfe 2021-07-08 op if (argc > 0)
1107 6cc5fcfe 2021-07-08 op else if (proc == PROC_NET)
1108 f45bd2e3 2021-07-24 op return net_main();
1113 d5bdf203 2021-07-08 op /* setup keys before reading the config */
1114 d5bdf203 2021-07-08 op TAILQ_INIT(&global_map.m);
1115 d5bdf203 2021-07-08 op global_map.unhandled_input = global_key_unbound;
1116 d5bdf203 2021-07-08 op TAILQ_INIT(&minibuffer_map.m);
1118 b8ec3578 2024-02-14 op if (fs_init() == -1)
1119 b8ec3578 2024-02-14 op err(1, "fs_init failed");
1120 b8ec3578 2024-02-14 op if (certs_init(certs_file) == -1)
1121 b8ec3578 2024-02-14 op err(1, "certs_init failed");
1122 d2544989 2021-07-08 op config_init();
1123 eb2ed626 2021-10-07 op parseconfig(config_path, fail);
1124 69f32f56 2021-08-16 op if (configtest) {
1125 d2544989 2021-07-08 op puts("config OK");
1129 cfcbc23c 2024-01-16 op if (default_protocol == NULL &&
1130 cfcbc23c 2024-01-16 op (default_protocol = strdup("gemini")) == NULL)
1131 cfcbc23c 2024-01-16 op err(1, "strdup");
1133 112b9046 2021-08-29 op if (download_path == NULL &&
1134 112b9046 2021-08-29 op (download_path = strdup("/tmp/")) == NULL)
1135 112b9046 2021-08-29 op errx(1, "strdup");
1137 449ea6fe 2024-01-16 op if (argc != 0) {
1140 449ea6fe 2024-01-16 op if (asprintf(&base, "file://%s/", cwd) == -1)
1141 449ea6fe 2024-01-16 op err(1, "asprintf");
1143 449ea6fe 2024-01-16 op has_url = 1;
1144 449ea6fe 2024-01-16 op humanify_url(argv[0], base, url, sizeof(url));
1149 4cf6ba13 2022-02-11 op if (!safe_mode && (sessionfd = lock_session()) == -1) {
1150 4cf6ba13 2022-02-11 op if (has_url) {
1151 4cf6ba13 2022-02-11 op send_url(url);
1155 d0fd368a 2021-07-15 op errx(1, "can't lock session, is another instance of "
1156 d0fd368a 2021-07-15 op "telescope already running?");
1159 6cc5fcfe 2021-07-08 op /* Start children. */
1160 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1161 35e1f40a 2021-03-14 op err(1, "socketpair");
1162 bc10f6a5 2021-07-12 op start_child(PROC_NET, argv0, pipe2net[1]);
1163 bc10f6a5 2021-07-12 op imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1164 bc10f6a5 2021-07-12 op iev_net = &net_ibuf;
1165 bc10f6a5 2021-07-12 op iev_net->handler = handle_dispatch_imsg;
1167 37d429b0 2021-04-01 op setproctitle("ui");
1169 eb722b50 2022-01-03 op /* initialize tofu store */
1170 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1172 98d3e6c1 2024-02-18 op if (ev_init() == -1)
1173 98d3e6c1 2024-02-18 op err(1, "ev_init");
1175 4cf6ba13 2022-02-11 op if (!safe_mode) {
1176 4cf6ba13 2022-02-11 op if ((control_fd = control_init(ctlsock_path)) == -1)
1177 4cf6ba13 2022-02-11 op err(1, "control_init %s", ctlsock_path);
1178 4cf6ba13 2022-02-11 op control_listen(control_fd);
1181 d5af38cc 2022-01-10 op /* initialize the in-memory cache store */
1182 d5af38cc 2022-01-10 op mcache_init();
1184 ecfbb1d4 2021-08-14 op /* Setup event handler for the autosave */
1185 1fce2e75 2021-08-14 op autosave_init();
1187 f63b8f73 2022-04-24 op /* Setup event handlers for pipes to net */
1188 bc10f6a5 2021-07-12 op iev_net->events = EV_READ;
1189 98d3e6c1 2024-02-18 op ev_add(iev_net->ibuf.fd, iev_net->events, iev_net->handler, iev_net);
1191 bb28f1c2 2021-12-30 op if (ui_init()) {
1192 941b3761 2021-03-18 op sandbox_ui_process();
1193 64f4f8e2 2022-04-24 op load_session(&certs);
1194 d521a6a4 2022-05-11 op if (has_url)
1195 d521a6a4 2022-05-11 op new_tab(url, NULL, NULL);
1196 f63b8f73 2022-04-24 op operating = 1;
1197 f63b8f73 2022-04-24 op switch_to_tab(current_tab);
1198 f63b8f73 2022-04-24 op ui_main_loop();
1202 d35e18b3 2024-02-04 op ui_send_net(IMSG_QUIT, 0, -1, NULL, 0);
1203 a9925134 2021-07-15 op imsg_flush(&iev_net->ibuf);
1205 be97d6e6 2021-08-15 op /* wait for children to terminate */
1207 be97d6e6 2021-08-15 op pid = wait(&status);
1208 be97d6e6 2021-08-15 op if (pid == -1) {
1209 be97d6e6 2021-08-15 op if (errno != EINTR && errno != ECHILD)
1210 be97d6e6 2021-08-15 op err(1, "wait");
1211 be97d6e6 2021-08-15 op } else if (WIFSIGNALED(status))
1212 be97d6e6 2021-08-15 op warnx("child terminated; signal %d", WTERMSIG(status));
1213 be97d6e6 2021-08-15 op } while (pid != -1 || (pid == -1 && errno == EINTR));
1215 f63b8f73 2022-04-24 op if (!safe_mode)
1216 f63b8f73 2022-04-24 op unlink(crashed_file);
1218 5d7c642a 2021-09-15 op if (!safe_mode && close(sessionfd) == -1)
1219 5d7c642a 2021-09-15 op err(1, "close(sessionfd = %d)", sessionfd);