2 35e1f40a 2021-03-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 35e1f40a 2021-03-14 op * Permission to use, copy, modify, and distribute this software for any
5 35e1f40a 2021-03-14 op * purpose with or without fee is hereby granted, provided that the above
6 35e1f40a 2021-03-14 op * copyright notice and this permission notice appear in all copies.
8 35e1f40a 2021-03-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 35e1f40a 2021-03-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 35e1f40a 2021-03-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 35e1f40a 2021-03-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 35e1f40a 2021-03-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 35e1f40a 2021-03-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 35e1f40a 2021-03-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 eb2ed626 2021-10-07 op * Handles config and runtime files
21 786e6deb 2021-07-21 op #include "compat.h"
23 6cd6a9e1 2021-03-20 op #include <sys/stat.h>
24 65124267 2021-08-13 op #include <sys/types.h>
26 65124267 2021-08-13 op #include <dirent.h>
27 35e1f40a 2021-03-14 op #include <errno.h>
28 de2a69bb 2021-05-17 op #include <fcntl.h>
29 35e1f40a 2021-03-14 op #include <limits.h>
30 eb2ed626 2021-10-07 op #include <libgen.h>
31 35e1f40a 2021-03-14 op #include <stdio.h>
32 35e1f40a 2021-03-14 op #include <stdlib.h>
33 35e1f40a 2021-03-14 op #include <string.h>
34 35e1f40a 2021-03-14 op #include <unistd.h>
36 5a824be4 2021-07-13 op #include "pages.h"
37 f63b8f73 2022-04-24 op #include "parser.h"
38 eb9cbcba 2022-01-03 op #include "session.h"
39 f63b8f73 2022-04-24 op #include "telescope.h"
40 9d65b1d9 2022-01-11 op #include "utils.h"
42 f63b8f73 2022-04-24 op #include "fs.h"
44 f63b8f73 2022-04-24 op #ifndef nitems
45 f63b8f73 2022-04-24 op #define nitems(x) (sizeof(x) / sizeof(x[0]))
48 35e1f40a 2021-03-14 op static void die(void) __attribute__((__noreturn__));
49 5cbe1763 2021-08-13 op static int select_non_dot(const struct dirent *);
50 5cbe1763 2021-08-13 op static int select_non_dotdot(const struct dirent *);
51 eb2ed626 2021-10-07 op static size_t join_path(char*, const char*, const char*, size_t);
52 eb2ed626 2021-10-07 op static void getenv_default(char*, const char*, const char*, size_t);
53 eb2ed626 2021-10-07 op static void mkdirs(const char*, mode_t);
54 6c8ddace 2022-01-03 op static void init_paths(void);
55 50f03682 2022-01-03 op static void load_last_session(void);
56 9e97090d 2022-02-26 op static void load_hist(void);
57 83ed72f1 2022-01-03 op static int last_time_crashed(void);
58 f63b8f73 2022-04-24 op static void load_certs(struct ohash *);
61 f88fbabc 2021-11-27 op * Where to store user data. These are all equal to ~/.telescope if
64 fb3d08c1 2021-10-07 op char config_path_base[PATH_MAX];
65 fb3d08c1 2021-10-07 op char data_path_base[PATH_MAX];
66 fb3d08c1 2021-10-07 op char cache_path_base[PATH_MAX];
68 4cf6ba13 2022-02-11 op char ctlsock_path[PATH_MAX];
69 eb2ed626 2021-10-07 op char config_path[PATH_MAX];
70 fb3d08c1 2021-10-07 op char lockfile_path[PATH_MAX];
71 fb3d08c1 2021-10-07 op char bookmark_file[PATH_MAX];
72 fb3d08c1 2021-10-07 op char known_hosts_file[PATH_MAX], known_hosts_tmp[PATH_MAX];
73 fb3d08c1 2021-10-07 op char crashed_file[PATH_MAX];
74 de6a6a40 2022-04-24 op char session_file[PATH_MAX], session_file_tmp[PATH_MAX];
75 de6a6a40 2022-04-24 op char history_file[PATH_MAX], history_file_tmp[PATH_MAX];
77 35e1f40a 2021-03-14 op static void __attribute__((__noreturn__))
80 35e1f40a 2021-03-14 op abort(); /* TODO */
84 f63b8f73 2022-04-24 op select_non_dot(const struct dirent *d)
86 f63b8f73 2022-04-24 op return strcmp(d->d_name, ".");
90 f63b8f73 2022-04-24 op select_non_dotdot(const struct dirent *d)
92 f63b8f73 2022-04-24 op return strcmp(d->d_name, ".") && strcmp(d->d_name, "..");
96 f63b8f73 2022-04-24 op send_dir(struct tab *tab, const char *path)
98 f63b8f73 2022-04-24 op struct dirent **names;
99 f63b8f73 2022-04-24 op int (*selector)(const struct dirent *) = select_non_dot;
104 f63b8f73 2022-04-24 op * need something to fake a redirect
107 f63b8f73 2022-04-24 op if (!has_suffix(path, "/")) {
108 f63b8f73 2022-04-24 op if (asprintf(&s, "%s/", path) == -1)
110 f63b8f73 2022-04-24 op send_hdr(peerid, 30, s);
116 f63b8f73 2022-04-24 op if (!strcmp(path, "/"))
117 f63b8f73 2022-04-24 op selector = select_non_dotdot;
119 f63b8f73 2022-04-24 op if ((len = scandir(path, &names, selector, alphasort)) == -1) {
120 f63b8f73 2022-04-24 op load_page_from_str(tab, "# failure reading the directory\n");
124 f63b8f73 2022-04-24 op parser_init(tab, gemtext_initparser);
125 f63b8f73 2022-04-24 op parser_parsef(tab, "# Index of %s\n\n", path);
127 f63b8f73 2022-04-24 op for (i = 0; i < len; ++i) {
128 f63b8f73 2022-04-24 op const char *sufx = "";
130 f63b8f73 2022-04-24 op if (names[i]->d_type == DT_DIR)
133 f63b8f73 2022-04-24 op parser_parsef(tab, "=> %s%s\n", names[i]->d_name, sufx);
136 f63b8f73 2022-04-24 op parser_free(tab);
141 f63b8f73 2022-04-24 op is_dir(FILE *fp)
143 f63b8f73 2022-04-24 op struct stat sb;
145 f63b8f73 2022-04-24 op if (fstat(fileno(fp), &sb) == -1)
148 f63b8f73 2022-04-24 op return S_ISDIR(sb.st_mode);
151 f63b8f73 2022-04-24 op static parserinit
152 65124267 2021-08-13 op file_type(const char *path)
154 fb4dc49f 2021-08-13 op struct mapping {
155 fb4dc49f 2021-08-13 op const char *ext;
156 f63b8f73 2022-04-24 op parserinit fn;
158 f63b8f73 2022-04-24 op {"diff", textpatch_initparser},
159 f63b8f73 2022-04-24 op {"gemini", gemtext_initparser},
160 f63b8f73 2022-04-24 op {"gmi", gemtext_initparser},
161 f63b8f73 2022-04-24 op {"markdown", textplain_initparser},
162 f63b8f73 2022-04-24 op {"md", textplain_initparser},
163 f63b8f73 2022-04-24 op {"patch", gemtext_initparser},
164 fb4dc49f 2021-08-13 op {NULL, NULL},
168 65124267 2021-08-13 op if ((dot = strrchr(path, '.')) == NULL)
169 f63b8f73 2022-04-24 op return textplain_initparser;
173 65124267 2021-08-13 op for (m = ms; m->ext != NULL; ++m)
174 65124267 2021-08-13 op if (!strcmp(m->ext, dot))
175 f63b8f73 2022-04-24 op return m->fn;
177 f63b8f73 2022-04-24 op return textplain_initparser;
181 f63b8f73 2022-04-24 op fs_load_url(struct tab *tab, const char *url)
183 f63b8f73 2022-04-24 op const char *bpath = "bookmarks.gmi", *fallback = "# Not found\n";
184 f63b8f73 2022-04-24 op parserinit initfn = gemtext_initparser;
185 f63b8f73 2022-04-24 op char path[PATH_MAX];
186 f63b8f73 2022-04-24 op FILE *fp = NULL;
188 f63b8f73 2022-04-24 op char buf[BUFSIZ];
189 f63b8f73 2022-04-24 op struct page {
190 f63b8f73 2022-04-24 op const char *name;
191 f63b8f73 2022-04-24 op const char *path;
192 f63b8f73 2022-04-24 op const uint8_t *data;
194 f63b8f73 2022-04-24 op } pages[] = {
195 f63b8f73 2022-04-24 op {"about", NULL, about_about, about_about_len},
196 f63b8f73 2022-04-24 op {"blank", NULL, about_blank, about_blank_len},
197 f63b8f73 2022-04-24 op {"bookmarks", bpath, bookmarks, bookmarks_len},
198 f63b8f73 2022-04-24 op {"crash", NULL, about_crash, about_crash_len},
199 f63b8f73 2022-04-24 op {"help", NULL, about_help, about_help_len},
200 f63b8f73 2022-04-24 op {"license", NULL, about_license, about_license_len},
201 f63b8f73 2022-04-24 op {"new", NULL, about_new, about_new_len},
202 f63b8f73 2022-04-24 op }, *page = NULL;
204 f63b8f73 2022-04-24 op if (!strncmp(url, "about:", 6)) {
207 f63b8f73 2022-04-24 op for (i = 0; page == NULL && i < nitems(pages); ++i) {
208 f63b8f73 2022-04-24 op if (!strcmp(url, pages[i].name))
209 f63b8f73 2022-04-24 op page = &pages[i];
212 f63b8f73 2022-04-24 op if (page == NULL)
215 f63b8f73 2022-04-24 op strlcpy(path, data_path_base, sizeof(path));
216 f63b8f73 2022-04-24 op strlcat(path, "/", sizeof(path));
217 f63b8f73 2022-04-24 op if (page->path != NULL)
218 f63b8f73 2022-04-24 op strlcat(path, page->path, sizeof(path));
220 f63b8f73 2022-04-24 op strlcat(path, "page/about_", sizeof(path));
221 f63b8f73 2022-04-24 op strlcat(path, page->name, sizeof(path));
222 f63b8f73 2022-04-24 op strlcat(path, ".gmi", sizeof(path));
225 f63b8f73 2022-04-24 op fallback = page->data;
226 f63b8f73 2022-04-24 op } else if (!strncmp(url, "file://", 7)) {
228 f63b8f73 2022-04-24 op strlcpy(path, url, sizeof(path));
229 f63b8f73 2022-04-24 op initfn = file_type(url);
233 f63b8f73 2022-04-24 op if ((fp = fopen(path, "r")) == NULL)
236 f63b8f73 2022-04-24 op if (is_dir(fp)) {
238 f63b8f73 2022-04-24 op send_dir(tab, path);
242 f63b8f73 2022-04-24 op parser_init(tab, initfn);
246 f63b8f73 2022-04-24 op r = fread(buf, 1, sizeof(buf), fp);
247 f63b8f73 2022-04-24 op if (!parser_parse(tab, buf, r))
249 f63b8f73 2022-04-24 op if (r != sizeof(buf))
252 f63b8f73 2022-04-24 op parser_free(tab);
255 f63b8f73 2022-04-24 op if (fp != NULL)
258 f63b8f73 2022-04-24 op load_page_from_str(tab, fallback);
262 f63b8f73 2022-04-24 op save_cert(const struct tofu_entry *e)
266 f63b8f73 2022-04-24 op if ((f = fopen(known_hosts_file, "a")) == NULL)
268 f63b8f73 2022-04-24 op fprintf(f, "%s %s %d\n", e->domain, e->hash, e->verified);
274 f63b8f73 2022-04-24 op update_cert(const struct tofu_entry *e)
276 288fd238 2021-04-25 op FILE *tmp, *f;
277 288fd238 2021-04-25 op char sfn[PATH_MAX], *line = NULL, *t;
278 288fd238 2021-04-25 op size_t l, linesize = 0;
279 288fd238 2021-04-25 op ssize_t linelen;
282 288fd238 2021-04-25 op strlcpy(sfn, known_hosts_tmp, sizeof(sfn));
283 288fd238 2021-04-25 op if ((fd = mkstemp(sfn)) == -1 ||
284 288fd238 2021-04-25 op (tmp = fdopen(fd, "w")) == NULL) {
285 288fd238 2021-04-25 op if (fd != -1) {
292 288fd238 2021-04-25 op if ((f = fopen(known_hosts_file, "r")) == NULL) {
298 f63b8f73 2022-04-24 op l = strlen(e->domain);
299 288fd238 2021-04-25 op while ((linelen = getline(&line, &linesize, f)) != -1) {
300 f63b8f73 2022-04-24 op if ((t = strstr(line, e->domain)) != NULL &&
301 288fd238 2021-04-25 op (line[l] == ' ' || line[l] == '\t'))
303 288fd238 2021-04-25 op /* line has a trailing \n */
304 288fd238 2021-04-25 op fprintf(tmp, "%s", line);
306 f63b8f73 2022-04-24 op fprintf(tmp, "%s %s %d\n", e->domain, e->hash, e->verified);
309 f63b8f73 2022-04-24 op err = ferror(tmp);
319 f63b8f73 2022-04-24 op if (rename(sfn, known_hosts_file))
324 eb2ed626 2021-10-07 op static size_t
325 eb2ed626 2021-10-07 op join_path(char *buf, const char *lhs, const char *rhs, size_t buflen)
327 eb2ed626 2021-10-07 op strlcpy(buf, lhs, buflen);
328 eb2ed626 2021-10-07 op return strlcat(buf, rhs, buflen);
332 eb2ed626 2021-10-07 op getenv_default(char *buf, const char *name, const char *def, size_t buflen)
335 eb2ed626 2021-10-07 op char *home, *env;
337 eb2ed626 2021-10-07 op if ((home = getenv("HOME")) == NULL)
338 eb2ed626 2021-10-07 op errx(1, "HOME is not defined");
340 eb2ed626 2021-10-07 op if ((env = getenv(name)) != NULL)
341 eb2ed626 2021-10-07 op ret = strlcpy(buf, env, buflen);
343 eb2ed626 2021-10-07 op ret = join_path(buf, home, def, buflen);
345 eb2ed626 2021-10-07 op if (ret >= buflen)
346 eb2ed626 2021-10-07 op errx(1, "buffer too small for %s", name);
350 eb2ed626 2021-10-07 op mkdirs(const char *path, mode_t mode)
352 444dad86 2021-10-07 op char copy[PATH_MAX+1], orig[PATH_MAX+1], *parent;
354 eb2ed626 2021-10-07 op strlcpy(copy, path, sizeof(copy));
355 444dad86 2021-10-07 op strlcpy(orig, path, sizeof(orig));
356 eb2ed626 2021-10-07 op parent = dirname(copy);
357 eb2ed626 2021-10-07 op if (!strcmp(parent, "/"))
359 eb2ed626 2021-10-07 op mkdirs(parent, mode);
361 444dad86 2021-10-07 op if (mkdir(orig, mode) != 0) {
362 eb2ed626 2021-10-07 op if (errno == EEXIST)
364 444dad86 2021-10-07 op err(1, "can't mkdir %s", orig);
369 6c8ddace 2022-01-03 op init_paths(void)
371 7e60a21a 2022-01-03 op char xdg_config_base[PATH_MAX];
372 7e60a21a 2022-01-03 op char xdg_data_base[PATH_MAX];
373 7e60a21a 2022-01-03 op char xdg_cache_base[PATH_MAX];
374 7e60a21a 2022-01-03 op char old_path[PATH_MAX];
376 7e60a21a 2022-01-03 op struct stat info;
378 eb2ed626 2021-10-07 op /* old path */
379 eb2ed626 2021-10-07 op if ((home = getenv("HOME")) == NULL)
380 eb2ed626 2021-10-07 op errx(1, "HOME is not defined");
381 eb2ed626 2021-10-07 op join_path(old_path, home, "/.telescope", sizeof(old_path));
383 eb2ed626 2021-10-07 op /* if ~/.telescope exists, use that instead of xdg dirs */
384 eb2ed626 2021-10-07 op if (stat(old_path, &info) == 0 && S_ISDIR(info.st_mode)) {
385 eb2ed626 2021-10-07 op join_path(config_path_base, home, "/.telescope",
386 eb2ed626 2021-10-07 op sizeof(config_path_base));
387 eb2ed626 2021-10-07 op join_path(data_path_base, home, "/.telescope",
388 eb2ed626 2021-10-07 op sizeof(data_path_base));
389 eb2ed626 2021-10-07 op join_path(cache_path_base, home, "/.telescope",
390 eb2ed626 2021-10-07 op sizeof(cache_path_base));
394 eb2ed626 2021-10-07 op /* xdg paths */
395 eb2ed626 2021-10-07 op getenv_default(xdg_config_base, "XDG_CONFIG_HOME", "/.config",
396 eb2ed626 2021-10-07 op sizeof(xdg_config_base));
397 eb2ed626 2021-10-07 op getenv_default(xdg_data_base, "XDG_DATA_HOME", "/.local/share",
398 eb2ed626 2021-10-07 op sizeof(xdg_data_base));
399 eb2ed626 2021-10-07 op getenv_default(xdg_cache_base, "XDG_CACHE_HOME", "/.cache",
400 eb2ed626 2021-10-07 op sizeof(xdg_cache_base));
402 eb2ed626 2021-10-07 op join_path(config_path_base, xdg_config_base, "/telescope",
403 eb2ed626 2021-10-07 op sizeof(config_path_base));
404 eb2ed626 2021-10-07 op join_path(data_path_base, xdg_data_base, "/telescope",
405 eb2ed626 2021-10-07 op sizeof(data_path_base));
406 eb2ed626 2021-10-07 op join_path(cache_path_base, xdg_cache_base, "/telescope",
407 eb2ed626 2021-10-07 op sizeof(cache_path_base));
409 eb2ed626 2021-10-07 op mkdirs(xdg_config_base, S_IRWXU);
410 eb2ed626 2021-10-07 op mkdirs(xdg_data_base, S_IRWXU);
411 eb2ed626 2021-10-07 op mkdirs(xdg_cache_base, S_IRWXU);
413 eb2ed626 2021-10-07 op mkdirs(config_path_base, S_IRWXU);
414 eb2ed626 2021-10-07 op mkdirs(data_path_base, S_IRWXU);
415 eb2ed626 2021-10-07 op mkdirs(cache_path_base, S_IRWXU);
419 eb2ed626 2021-10-07 op fs_init(void)
421 6c8ddace 2022-01-03 op init_paths();
423 4cf6ba13 2022-02-11 op join_path(ctlsock_path, cache_path_base, "/ctl",
424 4cf6ba13 2022-02-11 op sizeof(ctlsock_path));
425 eb2ed626 2021-10-07 op join_path(config_path, config_path_base, "/config",
426 eb2ed626 2021-10-07 op sizeof(config_path));
427 eb2ed626 2021-10-07 op join_path(lockfile_path, cache_path_base, "/lock",
428 eb2ed626 2021-10-07 op sizeof(lockfile_path));
429 eb2ed626 2021-10-07 op join_path(bookmark_file, data_path_base, "/bookmarks.gmi",
430 eb2ed626 2021-10-07 op sizeof(bookmark_file));
431 eb2ed626 2021-10-07 op join_path(known_hosts_file, data_path_base, "/known_hosts",
432 eb2ed626 2021-10-07 op sizeof(known_hosts_file));
433 eb2ed626 2021-10-07 op join_path(known_hosts_tmp, cache_path_base,
434 eb2ed626 2021-10-07 op "/known_hosts.tmp.XXXXXXXXXX", sizeof(known_hosts_tmp));
435 eb2ed626 2021-10-07 op join_path(session_file, cache_path_base, "/session",
436 de6a6a40 2022-04-24 op sizeof(session_file));
437 de6a6a40 2022-04-24 op join_path(session_file_tmp, cache_path_base, "/session.XXXXXXXXXX",
438 eb2ed626 2021-10-07 op sizeof(session_file));
439 9e97090d 2022-02-26 op join_path(history_file, cache_path_base, "/history",
440 de6a6a40 2022-04-24 op sizeof(history_file));
441 de6a6a40 2022-04-24 op join_path(history_file_tmp, cache_path_base, "/history.XXXXXXXXXX",
442 9e97090d 2022-02-26 op sizeof(history_file));
443 eb2ed626 2021-10-07 op join_path(crashed_file, cache_path_base, "/crashed",
444 eb2ed626 2021-10-07 op sizeof(crashed_file));
450 f63b8f73 2022-04-24 op * Parse a line of the session file and restores it. The format is:
452 bb28f1c2 2021-12-30 op * URL [flags,...] [title]\n
454 f63b8f73 2022-04-24 op static inline struct tab *
455 f63b8f73 2022-04-24 op parse_session_line(char *line, struct tab **ct)
457 f63b8f73 2022-04-24 op struct tab *tab;
458 bb28f1c2 2021-12-30 op char *s, *t, *ap;
459 f63b8f73 2022-04-24 op const char *uri, *title = "";
460 f63b8f73 2022-04-24 op int current = 0, killed = 0;
461 f63b8f73 2022-04-24 op size_t top_line = 0, current_line = 0;
464 bb28f1c2 2021-12-30 op if ((s = strchr(line, ' ')) == NULL)
469 bb28f1c2 2021-12-30 op if ((t = strchr(s, ' ')) != NULL) {
474 bb28f1c2 2021-12-30 op while ((ap = strsep(&s, ",")) != NULL) {
475 bb28f1c2 2021-12-30 op if (!strcmp(ap, "current"))
477 6c74799d 2022-01-05 op else if (!strcmp(ap, "killed"))
479 e795e935 2022-01-18 op else if (has_prefix(ap, "top="))
480 f63b8f73 2022-04-24 op top_line = strtonum(ap+4, 0, UINT32_MAX, NULL);
481 e795e935 2022-01-18 op else if (has_prefix(ap, "cur="))
482 f63b8f73 2022-04-24 op current_line = strtonum(ap+4, 0, UINT32_MAX, NULL);
485 f63b8f73 2022-04-24 op if (top_line > current_line) {
486 f63b8f73 2022-04-24 op top_line = 0;
487 f63b8f73 2022-04-24 op current_line = 0;
490 f63b8f73 2022-04-24 op if ((tab = new_tab(uri, NULL, NULL)) == NULL)
492 f63b8f73 2022-04-24 op tab->hist_cur->line_off = top_line;
493 f63b8f73 2022-04-24 op tab->hist_cur->current_off = current_line;
494 f63b8f73 2022-04-24 op strlcpy(tab->buffer.page.title, title, sizeof(tab->buffer.page.title));
498 f63b8f73 2022-04-24 op else if (killed)
499 f63b8f73 2022-04-24 op kill_tab(tab, 1);
504 1040cc7f 2021-01-02 op static inline void
505 f63b8f73 2022-04-24 op sendhist(struct tab *tab, const char *uri, int future)
507 f63b8f73 2022-04-24 op struct hist *h;
509 f63b8f73 2022-04-24 op if ((h = calloc(1, sizeof(*h))) == NULL)
511 f63b8f73 2022-04-24 op strlcpy(h->h, uri, sizeof(h->h));
514 f63b8f73 2022-04-24 op hist_push(&tab->hist, h);
516 f63b8f73 2022-04-24 op hist_add_before(&tab->hist, tab->hist_cur, h);
520 50f03682 2022-01-03 op load_last_session(void)
522 f63b8f73 2022-04-24 op struct tab *tab = NULL, *ct = NULL;
523 bb28f1c2 2021-12-30 op FILE *session;
524 bb28f1c2 2021-12-30 op size_t linesize = 0;
525 bb28f1c2 2021-12-30 op ssize_t linelen;
527 1040cc7f 2021-01-02 op char *nl, *s, *line = NULL;
529 bb28f1c2 2021-12-30 op if ((session = fopen(session_file, "r")) == NULL) {
530 f63b8f73 2022-04-24 op new_tab("about:new", NULL, NULL);
531 f63b8f73 2022-04-24 op switch_to_tab(new_tab("about:help", NULL, NULL));
535 bb28f1c2 2021-12-30 op while ((linelen = getline(&line, &linesize, session)) != -1) {
536 bb28f1c2 2021-12-30 op if ((nl = strchr(line, '\n')) != NULL)
539 1040cc7f 2021-01-02 op if (*line == '<' || *line == '>') {
540 1040cc7f 2021-01-02 op future = *line == '>';
542 f63b8f73 2022-04-24 op if (*s != ' ' || tab == NULL)
544 f63b8f73 2022-04-24 op sendhist(tab, ++s, future);
546 f63b8f73 2022-04-24 op tab = parse_session_line(line, &ct);
550 bb28f1c2 2021-12-30 op fclose(session);
553 f63b8f73 2022-04-24 op if (ct != NULL)
554 f63b8f73 2022-04-24 op switch_to_tab(ct);
556 f63b8f73 2022-04-24 op if (last_time_crashed())
557 f63b8f73 2022-04-24 op switch_to_tab(new_tab("about:crash", NULL, NULL));
561 9e97090d 2022-02-26 op load_hist(void)
564 9e97090d 2022-02-26 op size_t linesize = 0;
565 9e97090d 2022-02-26 op ssize_t linelen;
566 9e97090d 2022-02-26 op char *nl, *spc, *line = NULL;
567 9e97090d 2022-02-26 op const char *errstr;
568 9e97090d 2022-02-26 op struct histitem hi;
570 9e97090d 2022-02-26 op if ((hist = fopen(history_file, "r")) == NULL)
573 9e97090d 2022-02-26 op while ((linelen = getline(&line, &linesize, hist)) != -1) {
574 9e97090d 2022-02-26 op if ((nl = strchr(line, '\n')) != NULL)
576 9e97090d 2022-02-26 op if ((spc = strchr(line, ' ')) == NULL)
581 9e97090d 2022-02-26 op memset(&hi, 0, sizeof(hi));
582 9e97090d 2022-02-26 op hi.ts = strtonum(line, INT64_MIN, INT64_MAX, &errstr);
583 9e97090d 2022-02-26 op if (errstr != NULL)
585 9e97090d 2022-02-26 op if (strlcpy(hi.uri, spc, sizeof(hi.uri)) >= sizeof(hi.uri))
588 f63b8f73 2022-04-24 op history_push(&hi);
591 9e97090d 2022-02-26 op fclose(hist);
594 f63b8f73 2022-04-24 op history_sort();
598 f63b8f73 2022-04-24 op fs_load_state(struct ohash *certs)
600 f63b8f73 2022-04-24 op load_certs(certs);
602 f63b8f73 2022-04-24 op load_last_session();
607 be97d6e6 2021-08-15 op * Check if the last time telescope crashed. The check is done by
608 be97d6e6 2021-08-15 op * looking at `crashed_file': if it exists then last time we crashed.
609 be97d6e6 2021-08-15 op * Then, while here, touch the file too. During IMSG_QUIT we'll
613 b6171794 2021-07-20 op last_time_crashed(void)
615 be97d6e6 2021-08-15 op int fd, crashed = 1;
617 2b409042 2021-09-15 op if (safe_mode)
620 be97d6e6 2021-08-15 op if (unlink(crashed_file) == -1 && errno == ENOENT)
623 be97d6e6 2021-08-15 op if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
624 be97d6e6 2021-08-15 op return crashed;
627 be97d6e6 2021-08-15 op return crashed;
631 d0fd368a 2021-07-15 op lock_session(void)
633 d0fd368a 2021-07-15 op struct flock lock;
636 d0fd368a 2021-07-15 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
639 d0fd368a 2021-07-15 op lock.l_start = 0;
640 d0fd368a 2021-07-15 op lock.l_len = 0;
641 d0fd368a 2021-07-15 op lock.l_type = F_WRLCK;
642 d0fd368a 2021-07-15 op lock.l_whence = SEEK_SET;
644 d0fd368a 2021-07-15 op if (fcntl(fd, F_SETLK, &lock) == -1) {
652 6400962b 2022-01-03 op static inline int
653 c6d03cf5 2021-04-25 op parse_khost_line(char *line, char *tmp[3])
657 c6d03cf5 2021-04-25 op for (ap = tmp; ap < &tmp[3] &&
658 c6d03cf5 2021-04-25 op (*ap = strsep(&line, " \t\n")) != NULL;) {
659 c6d03cf5 2021-04-25 op if (**ap != '\0')
663 c6d03cf5 2021-04-25 op return ap == &tmp[3] && *line == '\0';
667 f63b8f73 2022-04-24 op load_certs(struct ohash *certs)
669 c6d03cf5 2021-04-25 op char *tmp[3], *line = NULL;
670 6cd6a9e1 2021-03-20 op const char *errstr;
671 ec1fa0b0 2021-04-25 op size_t lineno = 0, linesize = 0;
672 3a227e9a 2021-03-18 op ssize_t linelen;
674 f63b8f73 2022-04-24 op struct tofu_entry *e;
676 3a227e9a 2021-03-18 op if ((f = fopen(known_hosts_file, "r")) == NULL)
679 f63b8f73 2022-04-24 op if ((e = calloc(1, sizeof(*e))) == NULL) {
684 3a227e9a 2021-03-18 op while ((linelen = getline(&line, &linesize, f)) != -1) {
687 eb722b50 2022-01-03 op if (parse_khost_line(line, tmp)) {
688 f63b8f73 2022-04-24 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
689 f63b8f73 2022-04-24 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
691 f63b8f73 2022-04-24 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
692 c6d03cf5 2021-04-25 op if (errstr != NULL)
693 c6d03cf5 2021-04-25 op errx(1, "%s:%zu verification for %s is %s: %s",
694 c6d03cf5 2021-04-25 op known_hosts_file, lineno,
695 f63b8f73 2022-04-24 op e->domain, errstr, tmp[2]);
697 f63b8f73 2022-04-24 op tofu_add(certs, e);
699 ec1fa0b0 2021-04-25 op warnx("%s:%zu invalid entry",
700 ec1fa0b0 2021-04-25 op known_hosts_file, lineno);