Blame


1 5e11c00c 2021-03-02 op #include <sys/socket.h>
2 5e11c00c 2021-03-02 op
3 5e11c00c 2021-03-02 op #include <errno.h>
4 d2544989 2021-07-08 op #include <limits.h>
5 5e11c00c 2021-03-02 op #include <signal.h>
6 5e11c00c 2021-03-02 op #include <stdio.h>
7 5e11c00c 2021-03-02 op #include <stdlib.h>
8 5e11c00c 2021-03-02 op #include <string.h>
9 5e11c00c 2021-03-02 op #include <unistd.h>
10 5e11c00c 2021-03-02 op
11 395b9f4e 2021-07-12 op #include "parser.h"
12 395b9f4e 2021-07-12 op #include "telescope.h"
13 d1a0f2a3 2021-07-12 op #include "ui.h"
14 395b9f4e 2021-07-12 op
15 bc10f6a5 2021-07-12 op static struct imsgev *iev_fs, *iev_net;
16 bc10f6a5 2021-07-12 op
17 f6a429eb 2021-07-10 op struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
18 f6a429eb 2021-07-10 op struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
19 6cc5fcfe 2021-07-08 op
20 6cc5fcfe 2021-07-08 op enum telescope_process {
21 6cc5fcfe 2021-07-08 op PROC_UI,
22 6cc5fcfe 2021-07-08 op PROC_FS,
23 6cc5fcfe 2021-07-08 op PROC_NET,
24 6cc5fcfe 2021-07-08 op };
25 5e11c00c 2021-03-02 op
26 c07ac996 2021-03-12 op /* the first is also the fallback one */
27 c07ac996 2021-03-12 op static struct proto protos[] = {
28 31f1a758 2021-04-22 op { "gemini", load_gemini_url },
29 31f1a758 2021-04-22 op { "about", load_about_url },
30 4d3785b1 2021-03-09 op { NULL, NULL },
31 4d3785b1 2021-03-09 op };
32 4d3785b1 2021-03-09 op
33 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
34 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
35 2051e653 2021-03-13 op static void handle_imsg_err(struct imsg*, size_t);
36 2051e653 2021-03-13 op static void handle_imsg_check_cert(struct imsg*, size_t);
37 a2fd3805 2021-07-06 op static void handle_check_cert_user_choice(int, struct tab *);
38 a2fd3805 2021-07-06 op static void handle_maybe_save_new_cert(int, struct tab *);
39 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
40 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
41 a2fd3805 2021-07-06 op static void handle_maybe_save_page(int, struct tab *);
42 de2a69bb 2021-05-17 op static void handle_save_page_path(const char *, unsigned int);
43 de2a69bb 2021-05-17 op static void handle_imsg_file_opened(struct imsg*, size_t);
44 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
45 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
46 740f578b 2021-03-15 op static void handle_imsg_bookmark_ok(struct imsg*, size_t);
47 3a227e9a 2021-03-18 op static void handle_imsg_save_cert_ok(struct imsg*, size_t);
48 288fd238 2021-04-25 op static void handle_imsg_update_cert_ok(struct imsg *, size_t);
49 1304bbdd 2021-03-15 op static void handle_dispatch_imsg(int, short, void*);
50 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
51 2051e653 2021-03-13 op static void do_load_url(struct tab*, const char*);
52 6cc5fcfe 2021-07-08 op static pid_t start_child(enum telescope_process, const char *, int);
53 bc10f6a5 2021-07-12 op static int ui_send_net(int, uint32_t, const void *, uint16_t);
54 bc10f6a5 2021-07-12 op static int ui_send_fs(int, uint32_t, const void *, uint16_t);
55 5e11c00c 2021-03-02 op
56 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
57 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
58 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
59 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
60 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
61 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
62 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
63 740f578b 2021-03-15 op [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
64 3a227e9a 2021-03-18 op [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
65 288fd238 2021-04-25 op [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
66 de2a69bb 2021-05-17 op [IMSG_FILE_OPENED] = handle_imsg_file_opened,
67 5e11c00c 2021-03-02 op };
68 5e11c00c 2021-03-02 op
69 cbcc75fb 2021-03-17 op static struct ohash certs;
70 cbcc75fb 2021-03-17 op
71 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
72 5e11c00c 2021-03-02 op die(void)
73 5e11c00c 2021-03-02 op {
74 5e11c00c 2021-03-02 op abort(); /* TODO */
75 5e11c00c 2021-03-02 op }
76 5e11c00c 2021-03-02 op
77 5e11c00c 2021-03-02 op static struct tab *
78 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
79 5e11c00c 2021-03-02 op {
80 5e11c00c 2021-03-02 op struct tab *t;
81 5e11c00c 2021-03-02 op
82 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
83 5e11c00c 2021-03-02 op if (t->id == id)
84 5e11c00c 2021-03-02 op return t;
85 5e11c00c 2021-03-02 op }
86 5e11c00c 2021-03-02 op
87 5e11c00c 2021-03-02 op die();
88 5e11c00c 2021-03-02 op }
89 5e11c00c 2021-03-02 op
90 5e11c00c 2021-03-02 op static void
91 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
92 5e11c00c 2021-03-02 op {
93 3a9b9365 2021-03-09 op struct tab *tab;
94 3a9b9365 2021-03-09 op char *page;
95 3a9b9365 2021-03-09 op
96 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
97 3a9b9365 2021-03-09 op
98 3a9b9365 2021-03-09 op page = imsg->data;
99 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
100 3a9b9365 2021-03-09 op
101 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
102 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
103 3a9b9365 2021-03-09 op die();
104 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
105 3a9b9365 2021-03-09 op free(page);
106 5e11c00c 2021-03-02 op }
107 5e11c00c 2021-03-02 op
108 5e11c00c 2021-03-02 op static void
109 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
110 5e11c00c 2021-03-02 op {
111 984245ce 2021-06-23 op const char *hash, *host, *port;
112 cbcc75fb 2021-03-17 op int tofu_res;
113 cbcc75fb 2021-03-17 op struct tofu_entry *e;
114 cbcc75fb 2021-03-17 op struct tab *tab;
115 5e11c00c 2021-03-02 op
116 cbcc75fb 2021-03-17 op hash = imsg->data;
117 cbcc75fb 2021-03-17 op if (hash[datalen-1] != '\0')
118 cbcc75fb 2021-03-17 op abort();
119 cbcc75fb 2021-03-17 op
120 10346511 2021-03-17 op tab = tab_by_id(imsg->hdr.peerid);
121 cbcc75fb 2021-03-17 op
122 984245ce 2021-06-23 op if (tab->proxy != NULL) {
123 984245ce 2021-06-23 op host = tab->proxy->host;
124 984245ce 2021-06-23 op port = tab->proxy->port;
125 984245ce 2021-06-23 op } else {
126 984245ce 2021-06-23 op host = tab->uri.host;
127 984245ce 2021-06-23 op port = tab->uri.port;
128 984245ce 2021-06-23 op }
129 984245ce 2021-06-23 op
130 984245ce 2021-06-23 op if ((e = tofu_lookup(&certs, host, port)) == NULL) {
131 cbcc75fb 2021-03-17 op /* TODO: an update in libressl/libretls changed
132 cbcc75fb 2021-03-17 op * significantly. Find a better approach at storing
133 cbcc75fb 2021-03-17 op * the certs! */
134 cbcc75fb 2021-03-17 op if (datalen > sizeof(e->hash))
135 cbcc75fb 2021-03-17 op abort();
136 cbcc75fb 2021-03-17 op
137 cbcc75fb 2021-03-17 op tofu_res = 1; /* trust on first use */
138 cbcc75fb 2021-03-17 op if ((e = calloc(1, sizeof(*e))) == NULL)
139 cbcc75fb 2021-03-17 op abort();
140 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
141 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
142 eb4388ee 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
143 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
144 eb4388ee 2021-04-25 op }
145 cbcc75fb 2021-03-17 op strlcpy(e->hash, hash, sizeof(e->hash));
146 3768e50f 2021-04-25 op tofu_add(&certs, e);
147 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
148 cbcc75fb 2021-03-17 op } else
149 cbcc75fb 2021-03-17 op tofu_res = !strcmp(hash, e->hash);
150 cbcc75fb 2021-03-17 op
151 5d1bac73 2021-03-25 op if (tofu_res) {
152 a2fd3805 2021-07-06 op if (e->verified == -1)
153 a2fd3805 2021-07-06 op tab->trust = TS_TEMP_TRUSTED;
154 a2fd3805 2021-07-06 op else if (e->verified == 1)
155 a2fd3805 2021-07-06 op tab->trust = TS_VERIFIED;
156 a2fd3805 2021-07-06 op else
157 a2fd3805 2021-07-06 op tab->trust = TS_TRUSTED;
158 a2fd3805 2021-07-06 op
159 bc10f6a5 2021-07-12 op ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
160 5d1bac73 2021-03-25 op &tofu_res, sizeof(tofu_res));
161 5d1bac73 2021-03-25 op } else {
162 cbcc75fb 2021-03-17 op tab->trust = TS_UNTRUSTED;
163 cbcc75fb 2021-03-17 op load_page_from_str(tab, "# Certificate mismatch\n");
164 288fd238 2021-04-25 op if ((tab->cert = strdup(hash)) == NULL)
165 288fd238 2021-04-25 op die();
166 5d1bac73 2021-03-25 op ui_yornp("Certificate mismatch. Proceed?",
167 a2fd3805 2021-07-06 op handle_check_cert_user_choice, tab);
168 cbcc75fb 2021-03-17 op }
169 5d1bac73 2021-03-25 op }
170 5d1bac73 2021-03-25 op
171 5d1bac73 2021-03-25 op static void
172 a2fd3805 2021-07-06 op handle_check_cert_user_choice(int accept, struct tab *tab)
173 5d1bac73 2021-03-25 op {
174 bc10f6a5 2021-07-12 op ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
175 bc10f6a5 2021-07-12 op sizeof(accept));
176 288fd238 2021-04-25 op
177 a2fd3805 2021-07-06 op if (accept) {
178 a2fd3805 2021-07-06 op /*
179 a2fd3805 2021-07-06 op * trust the certificate for this session only. If
180 a2fd3805 2021-07-06 op * the page results in a redirect while we're asking
181 a2fd3805 2021-07-06 op * the user to save, we'll end up with an invalid
182 a2fd3805 2021-07-06 op * tabid (one request == one tab id) and crash. It
183 a2fd3805 2021-07-06 op * also makes sense to save it for the current session
184 a2fd3805 2021-07-06 op * if the user accepted it.
185 a2fd3805 2021-07-06 op */
186 a2fd3805 2021-07-06 op tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
187 a2fd3805 2021-07-06 op
188 288fd238 2021-04-25 op ui_yornp("Save the new certificate?",
189 a2fd3805 2021-07-06 op handle_maybe_save_new_cert, tab);
190 a2fd3805 2021-07-06 op } else {
191 288fd238 2021-04-25 op free(tab->cert);
192 288fd238 2021-04-25 op tab->cert = NULL;
193 288fd238 2021-04-25 op }
194 5e11c00c 2021-03-02 op }
195 5e11c00c 2021-03-02 op
196 288fd238 2021-04-25 op static void
197 a2fd3805 2021-07-06 op handle_maybe_save_new_cert(int accept, struct tab *tab)
198 288fd238 2021-04-25 op {
199 288fd238 2021-04-25 op struct tofu_entry *e;
200 984245ce 2021-06-23 op const char *host, *port;
201 288fd238 2021-04-25 op
202 984245ce 2021-06-23 op if (tab->proxy != NULL) {
203 984245ce 2021-06-23 op host = tab->proxy->host;
204 984245ce 2021-06-23 op port = tab->proxy->port;
205 984245ce 2021-06-23 op } else {
206 984245ce 2021-06-23 op host = tab->uri.host;
207 984245ce 2021-06-23 op port = tab->uri.port;
208 984245ce 2021-06-23 op }
209 984245ce 2021-06-23 op
210 288fd238 2021-04-25 op if (!accept)
211 288fd238 2021-04-25 op goto end;
212 288fd238 2021-04-25 op
213 fe2262ad 2021-05-12 op if ((e = calloc(1, sizeof(*e))) == NULL)
214 288fd238 2021-04-25 op die();
215 288fd238 2021-04-25 op
216 984245ce 2021-06-23 op strlcpy(e->domain, host, sizeof(e->domain));
217 984245ce 2021-06-23 op if (*port != '\0' && strcmp(port, "1965")) {
218 288fd238 2021-04-25 op strlcat(e->domain, ":", sizeof(e->domain));
219 984245ce 2021-06-23 op strlcat(e->domain, port, sizeof(e->domain));
220 288fd238 2021-04-25 op }
221 288fd238 2021-04-25 op strlcpy(e->hash, tab->cert, sizeof(e->hash));
222 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
223 288fd238 2021-04-25 op
224 288fd238 2021-04-25 op tofu_update(&certs, e);
225 288fd238 2021-04-25 op
226 288fd238 2021-04-25 op tab->trust = TS_TRUSTED;
227 288fd238 2021-04-25 op
228 288fd238 2021-04-25 op end:
229 288fd238 2021-04-25 op free(tab->cert);
230 288fd238 2021-04-25 op tab->cert = NULL;
231 288fd238 2021-04-25 op }
232 288fd238 2021-04-25 op
233 5cd2ebb1 2021-03-11 op static inline int
234 5cd2ebb1 2021-03-11 op normalize_code(int n)
235 5cd2ebb1 2021-03-11 op {
236 5cd2ebb1 2021-03-11 op if (n < 20) {
237 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
238 5cd2ebb1 2021-03-11 op return n;
239 5cd2ebb1 2021-03-11 op return 10;
240 5cd2ebb1 2021-03-11 op } else if (n < 30) {
241 5cd2ebb1 2021-03-11 op return 20;
242 5cd2ebb1 2021-03-11 op } else if (n < 40) {
243 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
244 5cd2ebb1 2021-03-11 op return n;
245 5cd2ebb1 2021-03-11 op return 30;
246 5cd2ebb1 2021-03-11 op } else if (n < 50) {
247 5cd2ebb1 2021-03-11 op if (n <= 44)
248 5cd2ebb1 2021-03-11 op return n;
249 5cd2ebb1 2021-03-11 op return 40;
250 5cd2ebb1 2021-03-11 op } else if (n < 60) {
251 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
252 5cd2ebb1 2021-03-11 op return n;
253 5cd2ebb1 2021-03-11 op return 50;
254 5cd2ebb1 2021-03-11 op } else if (n < 70) {
255 5cd2ebb1 2021-03-11 op if (n <= 62)
256 5cd2ebb1 2021-03-11 op return n;
257 5cd2ebb1 2021-03-11 op return 60;
258 5cd2ebb1 2021-03-11 op } else
259 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
260 5cd2ebb1 2021-03-11 op }
261 5cd2ebb1 2021-03-11 op
262 5e11c00c 2021-03-02 op static void
263 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
264 5e11c00c 2021-03-02 op {
265 0972d8b2 2021-03-02 op struct tab *tab;
266 5e11c00c 2021-03-02 op
267 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
268 0972d8b2 2021-03-02 op
269 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
270 5e11c00c 2021-03-02 op die();
271 5e11c00c 2021-03-02 op
272 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
273 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
274 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
275 0972d8b2 2021-03-02 op tab->redirect_count = 0;
276 5e11c00c 2021-03-02 op }
277 5e11c00c 2021-03-02 op
278 5e11c00c 2021-03-02 op static void
279 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
280 5e11c00c 2021-03-02 op {
281 0972d8b2 2021-03-02 op struct tab *tab;
282 0972d8b2 2021-03-02 op
283 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
284 0972d8b2 2021-03-02 op
285 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
286 0972d8b2 2021-03-02 op die();
287 5cd2ebb1 2021-03-11 op
288 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
289 0972d8b2 2021-03-02 op
290 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
291 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
292 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
293 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
294 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
295 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
296 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
297 bc10f6a5 2021-07-12 op ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
298 c07ac996 2021-03-12 op } else {
299 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
300 de2a69bb 2021-05-17 op ui_yornp("Can't display page, wanna save?",
301 a2fd3805 2021-07-06 op handle_maybe_save_page, tab);
302 c07ac996 2021-03-12 op }
303 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
304 3a9b9365 2021-03-09 op tab->redirect_count++;
305 0972d8b2 2021-03-02 op
306 3a9b9365 2021-03-09 op /* TODO: make customizable? */
307 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
308 3a9b9365 2021-03-09 op load_page_from_str(tab,
309 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
310 5cd2ebb1 2021-03-11 op } else
311 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
312 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
313 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
314 3a9b9365 2021-03-09 op }
315 de2a69bb 2021-05-17 op }
316 de2a69bb 2021-05-17 op
317 de2a69bb 2021-05-17 op static void
318 a2fd3805 2021-07-06 op handle_maybe_save_page(int dosave, struct tab *tab)
319 de2a69bb 2021-05-17 op {
320 de2a69bb 2021-05-17 op if (dosave)
321 a2fd3805 2021-07-06 op ui_read("Save to path", handle_save_page_path, tab->id);
322 de2a69bb 2021-05-17 op else
323 a2fd3805 2021-07-06 op stop_tab(tab);
324 de2a69bb 2021-05-17 op }
325 de2a69bb 2021-05-17 op
326 de2a69bb 2021-05-17 op static void
327 de2a69bb 2021-05-17 op handle_save_page_path(const char *path, unsigned int tabid)
328 de2a69bb 2021-05-17 op {
329 de2a69bb 2021-05-17 op struct tab *tab;
330 de2a69bb 2021-05-17 op
331 de2a69bb 2021-05-17 op if (path == NULL) {
332 de2a69bb 2021-05-17 op stop_tab(tab_by_id(tabid));
333 de2a69bb 2021-05-17 op return;
334 de2a69bb 2021-05-17 op }
335 de2a69bb 2021-05-17 op
336 de2a69bb 2021-05-17 op tab = tab_by_id(tabid);
337 de2a69bb 2021-05-17 op tab->path = strdup(path);
338 de2a69bb 2021-05-17 op
339 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_FILE_OPEN, tabid, path, strlen(path)+1);
340 5e11c00c 2021-03-02 op }
341 5e11c00c 2021-03-02 op
342 5e11c00c 2021-03-02 op static void
343 de2a69bb 2021-05-17 op handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
344 de2a69bb 2021-05-17 op {
345 de2a69bb 2021-05-17 op struct tab *tab;
346 de2a69bb 2021-05-17 op char *page;
347 de2a69bb 2021-05-17 op const char *e;
348 de2a69bb 2021-05-17 op int l;
349 de2a69bb 2021-05-17 op
350 de2a69bb 2021-05-17 op tab = tab_by_id(imsg->hdr.peerid);
351 de2a69bb 2021-05-17 op
352 de2a69bb 2021-05-17 op if (imsg->fd == -1) {
353 de2a69bb 2021-05-17 op stop_tab(tab);
354 de2a69bb 2021-05-17 op
355 de2a69bb 2021-05-17 op e = imsg->data;
356 de2a69bb 2021-05-17 op if (e[datalen-1] != '\0')
357 de2a69bb 2021-05-17 op die();
358 de2a69bb 2021-05-17 op l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
359 de2a69bb 2021-05-17 op tab->path, e);
360 de2a69bb 2021-05-17 op if (l == -1)
361 de2a69bb 2021-05-17 op die();
362 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
363 de2a69bb 2021-05-17 op free(page);
364 de2a69bb 2021-05-17 op } else {
365 de2a69bb 2021-05-17 op tab->fd = imsg->fd;
366 bc10f6a5 2021-07-12 op ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
367 de2a69bb 2021-05-17 op }
368 de2a69bb 2021-05-17 op }
369 de2a69bb 2021-05-17 op
370 de2a69bb 2021-05-17 op static void
371 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
372 5e11c00c 2021-03-02 op {
373 0972d8b2 2021-03-02 op struct tab *tab;
374 de2a69bb 2021-05-17 op int l;
375 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
376 5e11c00c 2021-03-02 op
377 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
378 5e11c00c 2021-03-02 op
379 de2a69bb 2021-05-17 op tab->bytes += datalen;
380 de2a69bb 2021-05-17 op if (tab->fd == -1) {
381 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page,
382 de2a69bb 2021-05-17 op imsg->data, datalen))
383 de2a69bb 2021-05-17 op die();
384 de2a69bb 2021-05-17 op } else {
385 de2a69bb 2021-05-17 op write(tab->fd, imsg->data, datalen);
386 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
387 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
388 de2a69bb 2021-05-17 op tab->path,
389 0e1eff5b 2021-06-23 op buf);
390 de2a69bb 2021-05-17 op if (l == -1)
391 de2a69bb 2021-05-17 op die();
392 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
393 de2a69bb 2021-05-17 op free(page);
394 de2a69bb 2021-05-17 op }
395 5e11c00c 2021-03-02 op
396 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
397 5e11c00c 2021-03-02 op }
398 5e11c00c 2021-03-02 op
399 5e11c00c 2021-03-02 op static void
400 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
401 5e11c00c 2021-03-02 op {
402 2ba66cea 2021-03-22 op struct tab *tab;
403 de2a69bb 2021-05-17 op int l;
404 0e1eff5b 2021-06-23 op char *page, buf[FMT_SCALED_STRSIZE] = {0};
405 a5c3e03d 2021-03-02 op
406 2ba66cea 2021-03-22 op tab = tab_by_id(imsg->hdr.peerid);
407 de2a69bb 2021-05-17 op
408 de2a69bb 2021-05-17 op if (tab->fd == -1) {
409 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
410 de2a69bb 2021-05-17 op die();
411 de2a69bb 2021-05-17 op } else {
412 0e1eff5b 2021-06-23 op fmt_scaled(tab->bytes, buf);
413 0e1eff5b 2021-06-23 op l = asprintf(&page, "Saved to \"%s\" (%s)\n",
414 de2a69bb 2021-05-17 op tab->path,
415 0e1eff5b 2021-06-23 op buf);
416 de2a69bb 2021-05-17 op if (l == -1)
417 de2a69bb 2021-05-17 op die();
418 de2a69bb 2021-05-17 op load_page_from_str(tab, page);
419 de2a69bb 2021-05-17 op free(page);
420 a5c3e03d 2021-03-02 op
421 de2a69bb 2021-05-17 op close(tab->fd);
422 de2a69bb 2021-05-17 op tab->fd = -1;
423 de2a69bb 2021-05-17 op free(tab->path);
424 de2a69bb 2021-05-17 op tab->path = NULL;
425 de2a69bb 2021-05-17 op }
426 de2a69bb 2021-05-17 op
427 2ba66cea 2021-03-22 op ui_on_tab_refresh(tab);
428 2ba66cea 2021-03-22 op ui_on_tab_loaded(tab);
429 5e11c00c 2021-03-02 op }
430 5e11c00c 2021-03-02 op
431 5e11c00c 2021-03-02 op static void
432 740f578b 2021-03-15 op handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
433 740f578b 2021-03-15 op {
434 740f578b 2021-03-15 op int res;
435 740f578b 2021-03-15 op
436 740f578b 2021-03-15 op if (datalen != sizeof(res))
437 740f578b 2021-03-15 op die();
438 740f578b 2021-03-15 op
439 740f578b 2021-03-15 op memcpy(&res, imsg->data, sizeof(res));
440 740f578b 2021-03-15 op if (res == 0)
441 7f963c41 2021-06-20 op message("Added to bookmarks!");
442 740f578b 2021-03-15 op else
443 7f963c41 2021-06-20 op message("Failed to add to bookmarks: %s",
444 740f578b 2021-03-15 op strerror(res));
445 740f578b 2021-03-15 op }
446 740f578b 2021-03-15 op
447 740f578b 2021-03-15 op static void
448 3a227e9a 2021-03-18 op handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
449 3a227e9a 2021-03-18 op {
450 3a227e9a 2021-03-18 op int res;
451 3a227e9a 2021-03-18 op
452 3a227e9a 2021-03-18 op if (datalen != sizeof(res))
453 3a227e9a 2021-03-18 op die();
454 3a227e9a 2021-03-18 op memcpy(&res, imsg->data, datalen);
455 3a227e9a 2021-03-18 op if (res != 0)
456 7f963c41 2021-06-20 op message("Failed to save the cert for: %s",
457 3a227e9a 2021-03-18 op strerror(res));
458 288fd238 2021-04-25 op }
459 288fd238 2021-04-25 op
460 288fd238 2021-04-25 op static void
461 288fd238 2021-04-25 op handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
462 288fd238 2021-04-25 op {
463 288fd238 2021-04-25 op int res;
464 288fd238 2021-04-25 op
465 288fd238 2021-04-25 op if (datalen != sizeof(res))
466 288fd238 2021-04-25 op die();
467 288fd238 2021-04-25 op memcpy(&res, imsg->data, datalen);
468 288fd238 2021-04-25 op if (!res)
469 7f963c41 2021-06-20 op message("Failed to update the certificate");
470 3a227e9a 2021-03-18 op }
471 3a227e9a 2021-03-18 op
472 3a227e9a 2021-03-18 op static void
473 1304bbdd 2021-03-15 op handle_dispatch_imsg(int fd, short ev, void *d)
474 5e11c00c 2021-03-02 op {
475 bc10f6a5 2021-07-12 op struct imsgev *iev = d;
476 bc10f6a5 2021-07-12 op dispatch_imsg(iev, ev, handlers, sizeof(handlers));
477 5e11c00c 2021-03-02 op }
478 5e11c00c 2021-03-02 op
479 0972d8b2 2021-03-02 op static void
480 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
481 0972d8b2 2021-03-02 op {
482 00ccb53d 2021-07-01 op erase_buffer(&tab->buffer);
483 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
484 46f6e974 2021-05-17 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
485 0972d8b2 2021-03-02 op die();
486 46f6e974 2021-05-17 op if (!tab->buffer.page.free(&tab->buffer.page))
487 0972d8b2 2021-03-02 op die();
488 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
489 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
490 0972d8b2 2021-03-02 op }
491 0972d8b2 2021-03-02 op
492 bcb0b073 2021-03-07 op void
493 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
494 0972d8b2 2021-03-02 op {
495 10346511 2021-03-17 op tab->trust = TS_VERIFIED;
496 2051e653 2021-03-13 op
497 46f6e974 2021-05-17 op gemtext_initparser(&tab->buffer.page);
498 35e1f40a 2021-03-14 op
499 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_GET, tab->id,
500 31f1a758 2021-04-22 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
501 4d3785b1 2021-03-09 op }
502 0972d8b2 2021-03-02 op
503 4d3785b1 2021-03-09 op void
504 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
505 4d3785b1 2021-03-09 op {
506 984245ce 2021-06-23 op struct get_req req;
507 754622a2 2021-03-15 op
508 2eef3403 2021-04-22 op stop_tab(tab);
509 2eef3403 2021-04-22 op tab->id = tab_new_id();
510 2eef3403 2021-04-22 op
511 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
512 984245ce 2021-06-23 op strlcpy(req.host, tab->uri.host, sizeof(req.host));
513 984245ce 2021-06-23 op strlcpy(req.port, tab->uri.port, sizeof(req.host));
514 984245ce 2021-06-23 op
515 984245ce 2021-06-23 op strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
516 984245ce 2021-06-23 op strlcat(req.req, "\r\n", sizeof(req.req));
517 984245ce 2021-06-23 op
518 984245ce 2021-06-23 op req.proto = PROTO_GEMINI;
519 984245ce 2021-06-23 op
520 bc10f6a5 2021-07-12 op ui_send_net(IMSG_GET_RAW, tab->id,
521 984245ce 2021-06-23 op &req, sizeof(req));
522 0972d8b2 2021-03-02 op }
523 0972d8b2 2021-03-02 op
524 984245ce 2021-06-23 op void
525 984245ce 2021-06-23 op load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
526 984245ce 2021-06-23 op {
527 984245ce 2021-06-23 op struct get_req req;
528 984245ce 2021-06-23 op
529 984245ce 2021-06-23 op stop_tab(tab);
530 984245ce 2021-06-23 op tab->id = tab_new_id();
531 984245ce 2021-06-23 op tab->proxy = p;
532 984245ce 2021-06-23 op
533 984245ce 2021-06-23 op memset(&req, 0, sizeof(req));
534 984245ce 2021-06-23 op strlcpy(req.host, p->host, sizeof(req.host));
535 984245ce 2021-06-23 op strlcpy(req.port, p->port, sizeof(req.host));
536 984245ce 2021-06-23 op
537 984245ce 2021-06-23 op strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
538 984245ce 2021-06-23 op strlcat(req.req, "\r\n", sizeof(req.req));
539 984245ce 2021-06-23 op
540 984245ce 2021-06-23 op req.proto = p->proto;
541 984245ce 2021-06-23 op
542 bc10f6a5 2021-07-12 op ui_send_net(IMSG_GET_RAW, tab->id,
543 984245ce 2021-06-23 op &req, sizeof(req));
544 984245ce 2021-06-23 op }
545 984245ce 2021-06-23 op
546 2051e653 2021-03-13 op static void
547 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
548 4d3785b1 2021-03-09 op {
549 31f1a758 2021-04-22 op struct phos_uri uri;
550 31f1a758 2021-04-22 op struct proto *p;
551 984245ce 2021-06-23 op struct proxy *proxy;
552 31f1a758 2021-04-22 op char *t;
553 7943bb81 2021-07-10 op
554 7943bb81 2021-07-10 op tab->proxy = NULL;
555 de2a69bb 2021-05-17 op
556 de2a69bb 2021-05-17 op if (tab->fd != -1) {
557 de2a69bb 2021-05-17 op close(tab->fd);
558 de2a69bb 2021-05-17 op tab->fd = -1;
559 de2a69bb 2021-05-17 op free(tab->path);
560 de2a69bb 2021-05-17 op tab->path = NULL;
561 de2a69bb 2021-05-17 op }
562 4d3785b1 2021-03-09 op
563 10346511 2021-03-17 op tab->trust = TS_UNKNOWN;
564 10346511 2021-03-17 op
565 31f1a758 2021-04-22 op memcpy(&uri, &tab->uri, sizeof(tab->uri));
566 31f1a758 2021-04-22 op if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
567 31f1a758 2021-04-22 op if (asprintf(&t, "#error loading %s\n>%s\n",
568 31f1a758 2021-04-22 op url, "Can't parse the URI") == -1)
569 31f1a758 2021-04-22 op die();
570 31f1a758 2021-04-22 op strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
571 31f1a758 2021-04-22 op load_page_from_str(tab, t);
572 31f1a758 2021-04-22 op free(t);
573 31f1a758 2021-04-22 op return;
574 31f1a758 2021-04-22 op }
575 31f1a758 2021-04-22 op
576 31f1a758 2021-04-22 op phos_serialize_uri(&tab->uri, tab->hist_cur->h,
577 31f1a758 2021-04-22 op sizeof(tab->hist_cur->h));
578 31f1a758 2021-04-22 op
579 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
580 ed383cf4 2021-04-22 op if (!strcmp(tab->uri.scheme, p->schema)) {
581 4d3785b1 2021-03-09 op p->loadfn(tab, url);
582 31f1a758 2021-04-22 op return;
583 4d3785b1 2021-03-09 op }
584 4d3785b1 2021-03-09 op }
585 4d3785b1 2021-03-09 op
586 984245ce 2021-06-23 op TAILQ_FOREACH(proxy, &proxies, proxies) {
587 984245ce 2021-06-23 op if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
588 984245ce 2021-06-23 op load_via_proxy(tab, url, proxy);
589 984245ce 2021-06-23 op return;
590 984245ce 2021-06-23 op }
591 984245ce 2021-06-23 op }
592 984245ce 2021-06-23 op
593 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
594 4d3785b1 2021-03-09 op }
595 4d3785b1 2021-03-09 op
596 9ad4627d 2021-03-10 op void
597 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
598 2051e653 2021-03-13 op {
599 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
600 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
601 2051e653 2021-03-13 op
602 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
603 2051e653 2021-03-13 op event_loopbreak();
604 2051e653 2021-03-13 op return;
605 2051e653 2021-03-13 op }
606 984245ce 2021-06-23 op
607 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
608 2051e653 2021-03-13 op do_load_url(tab, url);
609 bca92a4c 2021-07-01 op erase_buffer(&tab->buffer);
610 2051e653 2021-03-13 op }
611 2051e653 2021-03-13 op
612 2051e653 2021-03-13 op int
613 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
614 2051e653 2021-03-13 op {
615 2051e653 2021-03-13 op struct hist *h;
616 2051e653 2021-03-13 op
617 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
618 2051e653 2021-03-13 op return 0;
619 2051e653 2021-03-13 op tab->hist_cur = h;
620 2051e653 2021-03-13 op do_load_url(tab, h->h);
621 2051e653 2021-03-13 op return 1;
622 2051e653 2021-03-13 op }
623 2051e653 2021-03-13 op
624 2051e653 2021-03-13 op int
625 2051e653 2021-03-13 op load_next_page(struct tab *tab)
626 2051e653 2021-03-13 op {
627 2051e653 2021-03-13 op struct hist *h;
628 2051e653 2021-03-13 op
629 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
630 2051e653 2021-03-13 op return 0;
631 2051e653 2021-03-13 op tab->hist_cur = h;
632 2051e653 2021-03-13 op do_load_url(tab, h->h);
633 2051e653 2021-03-13 op return 1;
634 2051e653 2021-03-13 op }
635 2051e653 2021-03-13 op
636 2051e653 2021-03-13 op void
637 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
638 9ad4627d 2021-03-10 op {
639 bc10f6a5 2021-07-12 op ui_send_net(IMSG_STOP, tab->id, NULL, 0);
640 de2a69bb 2021-05-17 op
641 de2a69bb 2021-05-17 op if (tab->fd != -1) {
642 de2a69bb 2021-05-17 op close(tab->fd);
643 de2a69bb 2021-05-17 op tab->fd = -1;
644 de2a69bb 2021-05-17 op free(tab->path);
645 de2a69bb 2021-05-17 op tab->path = NULL;
646 de2a69bb 2021-05-17 op load_page_from_str(tab, "Stopped.\n");
647 de2a69bb 2021-05-17 op }
648 9ad4627d 2021-03-10 op }
649 9ad4627d 2021-03-10 op
650 740f578b 2021-03-15 op void
651 740f578b 2021-03-15 op add_to_bookmarks(const char *str)
652 740f578b 2021-03-15 op {
653 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
654 bc10f6a5 2021-07-12 op str, strlen(str)+1);
655 c7107cec 2021-04-01 op }
656 c7107cec 2021-04-01 op
657 c7107cec 2021-04-01 op void
658 c7107cec 2021-04-01 op save_session(void)
659 c7107cec 2021-04-01 op {
660 c7107cec 2021-04-01 op struct tab *tab;
661 c7107cec 2021-04-01 op
662 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
663 c7107cec 2021-04-01 op
664 c7107cec 2021-04-01 op TAILQ_FOREACH(tab, &tabshead, tabs) {
665 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_SESSION_TAB, 0,
666 c7107cec 2021-04-01 op tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
667 c7107cec 2021-04-01 op }
668 c7107cec 2021-04-01 op
669 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
670 d2544989 2021-07-08 op }
671 d2544989 2021-07-08 op
672 d2544989 2021-07-08 op static void
673 d2544989 2021-07-08 op session_new_tab_cb(const char *url)
674 d2544989 2021-07-08 op {
675 d2544989 2021-07-08 op new_tab(url);
676 d2544989 2021-07-08 op }
677 d2544989 2021-07-08 op
678 6cc5fcfe 2021-07-08 op static pid_t
679 6cc5fcfe 2021-07-08 op start_child(enum telescope_process p, const char *argv0, int fd)
680 6cc5fcfe 2021-07-08 op {
681 6cc5fcfe 2021-07-08 op const char *argv[4];
682 6cc5fcfe 2021-07-08 op int argc = 0;
683 6cc5fcfe 2021-07-08 op pid_t pid;
684 6cc5fcfe 2021-07-08 op
685 6cc5fcfe 2021-07-08 op switch (pid = fork()) {
686 6cc5fcfe 2021-07-08 op case -1:
687 6cc5fcfe 2021-07-08 op die();
688 6cc5fcfe 2021-07-08 op case 0:
689 6cc5fcfe 2021-07-08 op break;
690 6cc5fcfe 2021-07-08 op default:
691 6cc5fcfe 2021-07-08 op close(fd);
692 6cc5fcfe 2021-07-08 op return pid;
693 6cc5fcfe 2021-07-08 op }
694 6cc5fcfe 2021-07-08 op
695 6cc5fcfe 2021-07-08 op if (dup2(fd, 3) == -1)
696 6cc5fcfe 2021-07-08 op err(1, "cannot setup imsg fd");
697 6cc5fcfe 2021-07-08 op
698 6cc5fcfe 2021-07-08 op argv[argc++] = argv0;
699 6cc5fcfe 2021-07-08 op switch (p) {
700 6cc5fcfe 2021-07-08 op case PROC_UI:
701 6cc5fcfe 2021-07-08 op errx(1, "Can't start ui process");
702 6cc5fcfe 2021-07-08 op case PROC_FS:
703 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tf";
704 6cc5fcfe 2021-07-08 op break;
705 6cc5fcfe 2021-07-08 op case PROC_NET:
706 6cc5fcfe 2021-07-08 op argv[argc++] = "-Tn";
707 6cc5fcfe 2021-07-08 op break;
708 6cc5fcfe 2021-07-08 op }
709 6cc5fcfe 2021-07-08 op
710 6cc5fcfe 2021-07-08 op argv[argc++] = NULL;
711 6cc5fcfe 2021-07-08 op execvp(argv0, (char *const *)argv);
712 6cc5fcfe 2021-07-08 op err(1, "execvp(%s)", argv0);
713 6cc5fcfe 2021-07-08 op }
714 6cc5fcfe 2021-07-08 op
715 bc10f6a5 2021-07-12 op static int
716 bc10f6a5 2021-07-12 op ui_send_net(int type, uint32_t peerid, const void *data,
717 bc10f6a5 2021-07-12 op uint16_t datalen)
718 bc10f6a5 2021-07-12 op {
719 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
720 bc10f6a5 2021-07-12 op datalen);
721 bc10f6a5 2021-07-12 op }
722 bc10f6a5 2021-07-12 op
723 bc10f6a5 2021-07-12 op static int
724 bc10f6a5 2021-07-12 op ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
725 bc10f6a5 2021-07-12 op {
726 bc10f6a5 2021-07-12 op return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
727 bc10f6a5 2021-07-12 op datalen);
728 bc10f6a5 2021-07-12 op }
729 bc10f6a5 2021-07-12 op
730 6cc5fcfe 2021-07-08 op static void __attribute__((noreturn))
731 6cc5fcfe 2021-07-08 op usage(int r)
732 d2544989 2021-07-08 op {
733 d2544989 2021-07-08 op fprintf(stderr, "USAGE: %s [-hn] [-c config] [url]\n", getprogname());
734 d2544989 2021-07-08 op fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
735 6cc5fcfe 2021-07-08 op exit(r);
736 740f578b 2021-03-15 op }
737 740f578b 2021-03-15 op
738 5e11c00c 2021-03-02 op int
739 6cd6a9e1 2021-03-20 op main(int argc, char * const *argv)
740 5e11c00c 2021-03-02 op {
741 bc10f6a5 2021-07-12 op struct imsgev net_ibuf, fs_ibuf;
742 bc10f6a5 2021-07-12 op int pipe2net[2], pipe2fs[2];
743 d2544989 2021-07-08 op int ch, configtest = 0, fail = 0;
744 d2544989 2021-07-08 op int has_url = 0;
745 6cc5fcfe 2021-07-08 op int proc = -1;
746 d2544989 2021-07-08 op char path[PATH_MAX];
747 6cc5fcfe 2021-07-08 op const char *url = NEW_TAB_URL;
748 6cc5fcfe 2021-07-08 op const char *argv0;
749 5e11c00c 2021-03-02 op
750 6cc5fcfe 2021-07-08 op argv0 = argv[0];
751 6cc5fcfe 2021-07-08 op
752 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
753 a0e91173 2021-06-13 op signal(SIGPIPE, SIG_IGN);
754 5e11c00c 2021-03-02 op
755 d2544989 2021-07-08 op if (getenv("NO_COLOR") != NULL)
756 d2544989 2021-07-08 op enable_colors = 0;
757 d2544989 2021-07-08 op
758 d2544989 2021-07-08 op strlcpy(path, getenv("HOME"), sizeof(path));
759 d2544989 2021-07-08 op strlcat(path, "/.telescope/config", sizeof(path));
760 d2544989 2021-07-08 op
761 6cc5fcfe 2021-07-08 op while ((ch = getopt(argc, argv, "c:hnT:")) != -1) {
762 d2544989 2021-07-08 op switch (ch) {
763 d2544989 2021-07-08 op case 'c':
764 d2544989 2021-07-08 op fail = 1;
765 d2544989 2021-07-08 op strlcpy(path, optarg, sizeof(path));
766 d2544989 2021-07-08 op break;
767 d2544989 2021-07-08 op case 'n':
768 d2544989 2021-07-08 op configtest = 1;
769 d2544989 2021-07-08 op break;
770 d2544989 2021-07-08 op case 'h':
771 6cc5fcfe 2021-07-08 op usage(0);
772 6cc5fcfe 2021-07-08 op case 'T':
773 6cc5fcfe 2021-07-08 op switch (*optarg) {
774 6cc5fcfe 2021-07-08 op case 'f':
775 6cc5fcfe 2021-07-08 op proc = PROC_FS;
776 6cc5fcfe 2021-07-08 op break;
777 6cc5fcfe 2021-07-08 op case 'n':
778 6cc5fcfe 2021-07-08 op proc = PROC_NET;
779 6cc5fcfe 2021-07-08 op break;
780 6cc5fcfe 2021-07-08 op default:
781 6cc5fcfe 2021-07-08 op errx(1, "invalid process spec %c",
782 6cc5fcfe 2021-07-08 op *optarg);
783 6cc5fcfe 2021-07-08 op }
784 6cc5fcfe 2021-07-08 op break;
785 d2544989 2021-07-08 op default:
786 6cc5fcfe 2021-07-08 op usage(1);
787 d2544989 2021-07-08 op }
788 d2544989 2021-07-08 op }
789 d2544989 2021-07-08 op
790 d2544989 2021-07-08 op argc -= optind;
791 d2544989 2021-07-08 op argv += optind;
792 d2544989 2021-07-08 op
793 6cc5fcfe 2021-07-08 op if (proc != -1) {
794 6cc5fcfe 2021-07-08 op if (argc > 0)
795 6cc5fcfe 2021-07-08 op usage(1);
796 6cc5fcfe 2021-07-08 op else if (proc == PROC_FS)
797 6cc5fcfe 2021-07-08 op return fs_main();
798 6cc5fcfe 2021-07-08 op else if (proc == PROC_NET)
799 6cc5fcfe 2021-07-08 op return client_main();
800 6cc5fcfe 2021-07-08 op else
801 6cc5fcfe 2021-07-08 op usage(1);
802 6cc5fcfe 2021-07-08 op }
803 6cc5fcfe 2021-07-08 op
804 d2544989 2021-07-08 op if (argc != 0) {
805 d2544989 2021-07-08 op has_url = 1;
806 d2544989 2021-07-08 op url = argv[0];
807 d2544989 2021-07-08 op }
808 d2544989 2021-07-08 op
809 d5bdf203 2021-07-08 op /* setup keys before reading the config */
810 d5bdf203 2021-07-08 op TAILQ_INIT(&global_map.m);
811 d5bdf203 2021-07-08 op global_map.unhandled_input = global_key_unbound;
812 d5bdf203 2021-07-08 op TAILQ_INIT(&minibuffer_map.m);
813 d5bdf203 2021-07-08 op
814 d2544989 2021-07-08 op config_init();
815 d2544989 2021-07-08 op parseconfig(path, fail);
816 d2544989 2021-07-08 op if (configtest){
817 d2544989 2021-07-08 op puts("config OK");
818 d2544989 2021-07-08 op exit(0);
819 d2544989 2021-07-08 op }
820 d2544989 2021-07-08 op
821 6cc5fcfe 2021-07-08 op /* Start children. */
822 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
823 5e11c00c 2021-03-02 op err(1, "socketpair");
824 bc10f6a5 2021-07-12 op start_child(PROC_FS, argv0, pipe2fs[1]);
825 bc10f6a5 2021-07-12 op imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
826 bc10f6a5 2021-07-12 op iev_fs = &fs_ibuf;
827 bc10f6a5 2021-07-12 op iev_fs->handler = handle_dispatch_imsg;
828 5e11c00c 2021-03-02 op
829 bc10f6a5 2021-07-12 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
830 35e1f40a 2021-03-14 op err(1, "socketpair");
831 bc10f6a5 2021-07-12 op start_child(PROC_NET, argv0, pipe2net[1]);
832 bc10f6a5 2021-07-12 op imsg_init(&net_ibuf.ibuf, pipe2net[0]);
833 bc10f6a5 2021-07-12 op iev_net = &net_ibuf;
834 bc10f6a5 2021-07-12 op iev_net->handler = handle_dispatch_imsg;
835 5e11c00c 2021-03-02 op
836 37d429b0 2021-04-01 op setproctitle("ui");
837 35e1f40a 2021-03-14 op
838 6cc5fcfe 2021-07-08 op /* initialize tofu & load certificates */
839 6cc5fcfe 2021-07-08 op fs_init();
840 3768e50f 2021-04-25 op tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
841 3a227e9a 2021-03-18 op load_certs(&certs);
842 cbcc75fb 2021-03-17 op
843 5e11c00c 2021-03-02 op event_init();
844 5e11c00c 2021-03-02 op
845 bc10f6a5 2021-07-12 op /* Setup event handlers for pipes to fs/net */
846 bc10f6a5 2021-07-12 op iev_fs->events = EV_READ;
847 bc10f6a5 2021-07-12 op event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
848 bc10f6a5 2021-07-12 op iev_fs->handler, iev_fs);
849 bc10f6a5 2021-07-12 op event_add(&iev_fs->ev, NULL);
850 5e11c00c 2021-03-02 op
851 bc10f6a5 2021-07-12 op iev_net->events = EV_READ;
852 bc10f6a5 2021-07-12 op event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
853 bc10f6a5 2021-07-12 op iev_net->handler, iev_net);
854 bc10f6a5 2021-07-12 op event_add(&iev_net->ev, NULL);
855 35e1f40a 2021-03-14 op
856 d2544989 2021-07-08 op if (ui_init()) {
857 d2544989 2021-07-08 op load_last_session(session_new_tab_cb);
858 d2544989 2021-07-08 op if (has_url || TAILQ_EMPTY(&tabshead))
859 d2544989 2021-07-08 op new_tab(url);
860 d2544989 2021-07-08 op
861 941b3761 2021-03-18 op sandbox_ui_process();
862 941b3761 2021-03-18 op event_dispatch();
863 941b3761 2021-03-18 op ui_end();
864 941b3761 2021-03-18 op }
865 5e11c00c 2021-03-02 op
866 bc10f6a5 2021-07-12 op ui_send_fs(IMSG_QUIT, 0, NULL, 0);
867 bc10f6a5 2021-07-12 op ui_send_net(IMSG_QUIT, 0, NULL, 0);
868 5e11c00c 2021-03-02 op
869 5e11c00c 2021-03-02 op return 0;
870 5e11c00c 2021-03-02 op }