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