2 1fce2e75 2021-08-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 1fce2e75 2021-08-14 op * Permission to use, copy, modify, and distribute this software for any
5 1fce2e75 2021-08-14 op * purpose with or without fee is hereby granted, provided that the above
6 1fce2e75 2021-08-14 op * copyright notice and this permission notice appear in all copies.
8 1fce2e75 2021-08-14 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 1fce2e75 2021-08-14 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 1fce2e75 2021-08-14 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 1fce2e75 2021-08-14 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 1fce2e75 2021-08-14 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 1fce2e75 2021-08-14 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 1fce2e75 2021-08-14 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1fce2e75 2021-08-14 op #include "compat.h"
19 98d3e6c1 2024-02-18 op #include <sys/time.h>
21 1fce2e75 2021-08-14 op #include <errno.h>
22 64f4f8e2 2022-04-24 op #include <fcntl.h>
23 f63b8f73 2022-04-24 op #include <limits.h>
24 1fce2e75 2021-08-14 op #include <stdio.h>
25 1fce2e75 2021-08-14 op #include <stdlib.h>
26 1fce2e75 2021-08-14 op #include <string.h>
27 9e97090d 2022-02-26 op #include <time.h>
28 1fce2e75 2021-08-14 op #include <unistd.h>
30 1fce2e75 2021-08-14 op #include "defaults.h"
31 98d3e6c1 2024-02-18 op #include "ev.h"
32 b9fcc0e9 2021-10-08 op #include "fs.h"
33 65c49665 2024-01-23 op #include "hist.h"
34 87d297d1 2024-02-22 op #include "imsgev.h"
35 1fce2e75 2021-08-14 op #include "minibuffer.h"
36 1fce2e75 2021-08-14 op #include "session.h"
37 d163c210 2024-02-06 op #include "tofu.h"
38 1fce2e75 2021-08-14 op #include "ui.h"
39 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
41 9e97090d 2022-02-26 op struct history history;
43 98d3e6c1 2024-02-18 op static unsigned int autosavetimer;
46 1fce2e75 2021-08-14 op switch_to_tab(struct tab *tab)
48 1fce2e75 2021-08-14 op current_tab = tab;
49 1fce2e75 2021-08-14 op tab->flags &= ~TAB_URGENT;
51 fb8dcd1c 2022-01-18 op if (operating && tab->flags & TAB_LAZY)
52 65c49665 2024-01-23 op load_url_in_tab(tab, hist_cur(tab->hist), NULL,
53 65c49665 2024-01-23 op LU_MODE_NOHIST);
57 1fce2e75 2021-08-14 op tab_new_id(void)
59 1fce2e75 2021-08-14 op static uint32_t tab_counter;
61 1fce2e75 2021-08-14 op return tab_counter++;
65 1fce2e75 2021-08-14 op new_tab(const char *url, const char *base, struct tab *after)
67 1fce2e75 2021-08-14 op struct tab *tab;
69 87aeb47b 2021-08-18 op ui_schedule_redraw();
70 1fce2e75 2021-08-14 op autosave_hook();
72 3d89457c 2024-06-18 thomas.ad tab = xcalloc(1, sizeof(*tab));
74 65c49665 2024-01-23 op if ((tab->hist = hist_new(HIST_LINEAR)) == NULL) {
80 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.head);
81 c1d27b0e 2024-06-14 op TAILQ_INIT(&tab->buffer.vhead);
83 1fce2e75 2021-08-14 op tab->id = tab_new_id();
85 1fce2e75 2021-08-14 op if (after != NULL)
86 1fce2e75 2021-08-14 op TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
88 1fce2e75 2021-08-14 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
90 ea639250 2022-01-02 op if (!operating)
91 ea639250 2022-01-02 op tab->flags |= TAB_LAZY;
92 1fce2e75 2021-08-14 op load_url_in_tab(tab, url, base, 0);
93 ea639250 2022-01-02 op switch_to_tab(tab);
98 6c74799d 2022-01-05 op * Move a tab from the tablist to the killed tab list and erase its
99 05de8ac3 2022-01-06 op * contents. Append should always be 0 to prepend tabs so unkill_tab
100 05de8ac3 2022-01-06 op * can work correctly; appending is only useful during startup when
101 05de8ac3 2022-01-06 op * receiving the list of killed tabs to keep the correct order.
102 05de8ac3 2022-01-06 op * NB: doesn't update the current_tab.
105 05de8ac3 2022-01-06 op kill_tab(struct tab *tab, int append)
109 1fce2e75 2021-08-14 op stop_tab(tab);
110 6c74799d 2022-01-05 op erase_buffer(&tab->buffer);
111 6c74799d 2022-01-05 op TAILQ_REMOVE(&tabshead, tab, tabs);
112 87aeb47b 2021-08-18 op ui_schedule_redraw();
113 1fce2e75 2021-08-14 op autosave_hook();
115 98d3e6c1 2024-02-18 op ev_timer_cancel(tab->loading_timer);
118 05de8ac3 2022-01-06 op TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
120 05de8ac3 2022-01-06 op TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
122 6c74799d 2022-01-05 op /* gc closed tabs */
124 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &ktabshead, tabs)
126 6c74799d 2022-01-05 op while (count > max_killed_tabs) {
128 6c74799d 2022-01-05 op free_tab(TAILQ_LAST(&ktabshead, tabshead));
133 265508d0 2022-01-05 op * Resurrects the lastest killed tab and returns it. The tab is already
134 6c74799d 2022-01-05 op * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
135 6c74799d 2022-01-05 op * update current_tab.
138 6c74799d 2022-01-05 op unkill_tab(void)
140 6c74799d 2022-01-05 op struct tab *t;
142 6c74799d 2022-01-05 op if (TAILQ_EMPTY(&ktabshead))
145 b7286684 2022-01-13 op ui_schedule_redraw();
146 6c74799d 2022-01-05 op autosave_hook();
148 6c74799d 2022-01-05 op t = TAILQ_FIRST(&ktabshead);
149 6c74799d 2022-01-05 op TAILQ_REMOVE(&ktabshead, t, tabs);
150 6c74799d 2022-01-05 op TAILQ_INSERT_TAIL(&tabshead, t, tabs);
151 6c74799d 2022-01-05 op t->flags |= TAB_LAZY;
156 1b45e5df 2022-01-05 op * Free every resource linked to the tab, including the tab itself, and
157 1b45e5df 2022-01-05 op * removes it from the *killed* tablist.
160 6c74799d 2022-01-05 op free_tab(struct tab *tab)
162 6c74799d 2022-01-05 op TAILQ_REMOVE(&ktabshead, tab, tabs);
163 65c49665 2024-01-23 op hist_free(tab->hist);
168 1fce2e75 2021-08-14 op stop_tab(struct tab *tab)
170 d35e18b3 2024-02-04 op ui_send_net(IMSG_STOP, tab->id, -1, NULL, 0);
173 6c74799d 2022-01-05 op static inline void
174 f63b8f73 2022-04-24 op savetab(FILE *fp, struct tab *tab, int killed)
176 65c49665 2024-01-23 op size_t i, size, cur;
177 f63b8f73 2022-04-24 op size_t top_line, current_line;
179 f63b8f73 2022-04-24 op get_scroll_position(tab, &top_line, ¤t_line);
181 65c49665 2024-01-23 op fprintf(fp, "%s ", hist_cur(tab->hist));
182 6c74799d 2022-01-05 op if (tab == current_tab)
183 f63b8f73 2022-04-24 op fprintf(fp, "current,");
185 f63b8f73 2022-04-24 op fprintf(fp, "killed,");
187 f63b8f73 2022-04-24 op fprintf(fp, "top=%zu,cur=%zu %s\n", top_line, current_line,
188 c1d27b0e 2024-06-14 op tab->buffer.title);
190 65c49665 2024-01-23 op cur = hist_off(tab->hist);
191 65c49665 2024-01-23 op size = hist_size(tab->hist);
192 65c49665 2024-01-23 op for (i = 0; i < size; ++i) {
193 65c49665 2024-01-23 op if (i == cur)
195 65c49665 2024-01-23 op fprintf(fp, "%s %s\n", i > cur ? ">" : "<",
196 65c49665 2024-01-23 op hist_nth(tab->hist, i));
201 5bd159bd 2022-05-11 op save_tabs(void)
204 de6a6a40 2022-04-24 op struct tab *tab;
206 de6a6a40 2022-04-24 op char sfn[PATH_MAX];
208 de6a6a40 2022-04-24 op strlcpy(sfn, session_file_tmp, sizeof(sfn));
209 de6a6a40 2022-04-24 op if ((fd = mkstemp(sfn)) == -1 ||
210 5bd159bd 2022-05-11 op (fp = fdopen(fd, "w")) == NULL) {
211 de6a6a40 2022-04-24 op if (fd != -1) {
218 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &tabshead, tabs)
219 5bd159bd 2022-05-11 op savetab(fp, tab, 0);
220 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &ktabshead, tabs)
221 5bd159bd 2022-05-11 op savetab(fp, tab, 1);
223 5bd159bd 2022-05-11 op err = fflush(fp) == EOF;
226 5bd159bd 2022-05-11 op if (err || rename(sfn, session_file) == -1)
231 5bd159bd 2022-05-11 op save_all_history(void)
236 5bd159bd 2022-05-11 op char sfn[PATH_MAX];
238 5bd159bd 2022-05-11 op strlcpy(sfn, history_file_tmp, sizeof(sfn));
239 5bd159bd 2022-05-11 op if ((fd = mkstemp(sfn)) == -1 ||
240 5bd159bd 2022-05-11 op (fp = fdopen(fd, "w")) == NULL) {
241 5bd159bd 2022-05-11 op if (fd != -1) {
248 5bd159bd 2022-05-11 op for (i = 0; i < history.len; ++i) {
249 5bd159bd 2022-05-11 op history.items[i].dirty = 0;
250 5bd159bd 2022-05-11 op fprintf(fp, "%lld %s\n", (long long)history.items[i].ts,
251 5bd159bd 2022-05-11 op history.items[i].uri);
254 5bd159bd 2022-05-11 op err = fflush(fp) == EOF;
257 5bd159bd 2022-05-11 op if (err || rename(sfn, history_file) == -1) {
262 5bd159bd 2022-05-11 op history.dirty = 0;
263 5bd159bd 2022-05-11 op history.extra = 0;
267 5bd159bd 2022-05-11 op save_dirty_history(void)
272 5bd159bd 2022-05-11 op if ((fp = fopen(history_file, "a")) == NULL)
275 5bd159bd 2022-05-11 op for (i = 0; i < history.len && history.dirty > 0; ++i) {
276 5bd159bd 2022-05-11 op if (!history.items[i].dirty)
278 5bd159bd 2022-05-11 op history.dirty--;
279 5bd159bd 2022-05-11 op history.items[i].dirty = 0;
280 5bd159bd 2022-05-11 op fprintf(fp, "%lld %s\n", (long long)history.items[i].ts,
281 5bd159bd 2022-05-11 op history.items[i].uri);
283 5bd159bd 2022-05-11 op history.dirty = 0;
289 5bd159bd 2022-05-11 op save_session(void)
291 5bd159bd 2022-05-11 op if (safe_mode)
296 5bd159bd 2022-05-11 op if (history.extra > HISTORY_CAP/2)
297 5bd159bd 2022-05-11 op save_all_history();
298 5bd159bd 2022-05-11 op else if (history.dirty)
299 5bd159bd 2022-05-11 op save_dirty_history();
303 9e97090d 2022-02-26 op history_push(struct histitem *hi)
305 9e97090d 2022-02-26 op size_t i, oldest = 0;
308 9e97090d 2022-02-26 op for (i = 0; i < history.len; ++i) {
309 9e97090d 2022-02-26 op if (history.items[i].ts < history.items[oldest].ts)
312 9e97090d 2022-02-26 op /* remove duplicates */
313 9e97090d 2022-02-26 op if (!strcmp(history.items[i].uri, hi->uri))
317 3d89457c 2024-06-18 thomas.ad uri = xstrdup(hi->uri);
319 9e97090d 2022-02-26 op /* don't grow too much; replace the oldest */
320 9e97090d 2022-02-26 op if (history.len == HISTORY_CAP) {
321 9e97090d 2022-02-26 op history.items[oldest].ts = hi->ts;
322 9e97090d 2022-02-26 op free(history.items[oldest].uri);
323 9e97090d 2022-02-26 op history.items[oldest].uri = uri;
325 5bd159bd 2022-05-11 op /* Growed past the max value, signal to regen the file. */
326 5bd159bd 2022-05-11 op history.extra++;
330 9e97090d 2022-02-26 op history.items[history.len].ts = hi->ts;
331 9e97090d 2022-02-26 op history.items[history.len].uri = uri;
332 9e97090d 2022-02-26 op history.len++;
336 9e97090d 2022-02-26 op history_cmp(const void *a, const void *b)
338 9e97090d 2022-02-26 op const struct history_item *i = a, *j = b;
339 9e97090d 2022-02-26 op return strcmp(i->uri, j->uri);
343 9e97090d 2022-02-26 op history_sort(void)
345 9e97090d 2022-02-26 op qsort(history.items, history.len, sizeof(history.items[0]),
346 9e97090d 2022-02-26 op history_cmp);
350 9e97090d 2022-02-26 op history_add(const char *uri)
352 9e97090d 2022-02-26 op size_t i, j, insert = 0, oldest = 0;
356 9e97090d 2022-02-26 op for (i = 0; i < history.len; ++i) {
357 9e97090d 2022-02-26 op if (history.items[i].ts < history.items[oldest].ts)
360 9e97090d 2022-02-26 op if (insert != 0 && insert < i)
363 9e97090d 2022-02-26 op c = strcmp(uri, history.items[i].uri);
364 9e97090d 2022-02-26 op if (c == 0) {
365 9e97090d 2022-02-26 op history.items[i].ts = time(NULL);
366 9e97090d 2022-02-26 op history.items[i].dirty = 1;
367 9e97090d 2022-02-26 op history.dirty++;
368 9e97090d 2022-02-26 op autosave_hook();
376 3d89457c 2024-06-18 thomas.ad u = xstrdup(uri);
378 9e97090d 2022-02-26 op /* if history is full, replace the oldest one */
379 9e97090d 2022-02-26 op if (history.len == HISTORY_CAP) {
380 9e97090d 2022-02-26 op free(history.items[oldest].uri);
381 9e97090d 2022-02-26 op history.items[oldest].uri = u;
382 9e97090d 2022-02-26 op history.items[oldest].ts = time(NULL);
383 9e97090d 2022-02-26 op history.items[oldest].dirty = 1;
384 9e97090d 2022-02-26 op history.dirty++;
385 9e97090d 2022-02-26 op history_sort();
386 9e97090d 2022-02-26 op autosave_hook();
390 9e97090d 2022-02-26 op /* otherwise just insert in the right spot */
392 9e97090d 2022-02-26 op for (j = history.len; j > insert; --j)
393 9e97090d 2022-02-26 op memcpy(&history.items[j], &history.items[j-1],
394 9e97090d 2022-02-26 op sizeof(history.items[j]));
396 9e97090d 2022-02-26 op history.items[insert].ts = time(NULL);
397 9e97090d 2022-02-26 op history.items[insert].uri = u;
398 9e97090d 2022-02-26 op history.items[insert].dirty = 1;
399 9e97090d 2022-02-26 op history.dirty++;
400 9e97090d 2022-02-26 op history.len++;
401 9e97090d 2022-02-26 op autosave_hook();
405 1fce2e75 2021-08-14 op autosave_init(void)
411 98d3e6c1 2024-02-18 op autosave_timer(int fd, int event, void *data)
413 1fce2e75 2021-08-14 op save_session();
417 1fce2e75 2021-08-14 op * Function to be called in "interesting" places where we may want to
418 1fce2e75 2021-08-14 op * schedule an autosave (like on new tab or before loading an url.)
421 1fce2e75 2021-08-14 op autosave_hook(void)
423 1fce2e75 2021-08-14 op struct timeval tv;
425 1fce2e75 2021-08-14 op if (autosave <= 0)
428 98d3e6c1 2024-02-18 op if (!ev_timer_pending(autosavetimer)) {
429 1fce2e75 2021-08-14 op tv.tv_sec = autosave;
430 1fce2e75 2021-08-14 op tv.tv_usec = 0;
432 98d3e6c1 2024-02-18 op autosavetimer = ev_timer(&tv, autosave_timer, NULL);
436 64f4f8e2 2022-04-24 op static inline int
437 64f4f8e2 2022-04-24 op parse_khost_line(char *line, char *tmp[3])
441 64f4f8e2 2022-04-24 op for (ap = tmp; ap < &tmp[3] &&
442 64f4f8e2 2022-04-24 op (*ap = strsep(&line, " \t\n")) != NULL;) {
443 64f4f8e2 2022-04-24 op if (**ap != '\0')
447 64f4f8e2 2022-04-24 op return ap == &tmp[3] && *line == '\0';
451 64f4f8e2 2022-04-24 op load_certs(struct ohash *certs)
453 64f4f8e2 2022-04-24 op char *tmp[3], *line = NULL;
454 64f4f8e2 2022-04-24 op const char *errstr;
455 64f4f8e2 2022-04-24 op size_t lineno = 0, linesize = 0;
456 64f4f8e2 2022-04-24 op ssize_t linelen;
458 64f4f8e2 2022-04-24 op struct tofu_entry *e;
460 64f4f8e2 2022-04-24 op if ((f = fopen(known_hosts_file, "r")) == NULL)
463 3d89457c 2024-06-18 thomas.ad e = xcalloc(1, sizeof(*e));
465 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, f)) != -1) {
468 64f4f8e2 2022-04-24 op if (parse_khost_line(line, tmp)) {
469 64f4f8e2 2022-04-24 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
470 64f4f8e2 2022-04-24 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
472 64f4f8e2 2022-04-24 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
473 64f4f8e2 2022-04-24 op if (errstr != NULL)
474 64f4f8e2 2022-04-24 op errx(1, "%s:%zu verification for %s is %s: %s",
475 64f4f8e2 2022-04-24 op known_hosts_file, lineno,
476 64f4f8e2 2022-04-24 op e->domain, errstr, tmp[2]);
478 64f4f8e2 2022-04-24 op tofu_add(certs, e);
480 64f4f8e2 2022-04-24 op warnx("%s:%zu invalid entry",
481 64f4f8e2 2022-04-24 op known_hosts_file, lineno);
491 64f4f8e2 2022-04-24 op load_hist(void)
494 64f4f8e2 2022-04-24 op size_t linesize = 0;
495 64f4f8e2 2022-04-24 op ssize_t linelen;
496 64f4f8e2 2022-04-24 op char *nl, *spc, *line = NULL;
497 64f4f8e2 2022-04-24 op const char *errstr;
498 64f4f8e2 2022-04-24 op struct histitem hi;
500 64f4f8e2 2022-04-24 op if ((hist = fopen(history_file, "r")) == NULL)
503 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, hist)) != -1) {
504 64f4f8e2 2022-04-24 op if ((nl = strchr(line, '\n')) != NULL)
506 64f4f8e2 2022-04-24 op if ((spc = strchr(line, ' ')) == NULL)
511 64f4f8e2 2022-04-24 op memset(&hi, 0, sizeof(hi));
512 64f4f8e2 2022-04-24 op hi.ts = strtonum(line, INT64_MIN, INT64_MAX, &errstr);
513 64f4f8e2 2022-04-24 op if (errstr != NULL)
515 64f4f8e2 2022-04-24 op if (strlcpy(hi.uri, spc, sizeof(hi.uri)) >= sizeof(hi.uri))
518 64f4f8e2 2022-04-24 op history_push(&hi);
521 64f4f8e2 2022-04-24 op fclose(hist);
524 64f4f8e2 2022-04-24 op history_sort();
528 64f4f8e2 2022-04-24 op * Check if the last time telescope crashed. The check is done by
529 64f4f8e2 2022-04-24 op * looking at `crashed_file': if it exists then last time we crashed.
530 64f4f8e2 2022-04-24 op * Then, while here, touch the file too, it's removed during the
534 64f4f8e2 2022-04-24 op last_time_crashed(void)
536 64f4f8e2 2022-04-24 op int fd, crashed = 1;
538 64f4f8e2 2022-04-24 op if (safe_mode)
541 64f4f8e2 2022-04-24 op if (unlink(crashed_file) == -1 && errno == ENOENT)
544 64f4f8e2 2022-04-24 op if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
545 64f4f8e2 2022-04-24 op return crashed;
548 64f4f8e2 2022-04-24 op return crashed;
552 64f4f8e2 2022-04-24 op * Parse and restore a tab from the session file. The format is:
554 64f4f8e2 2022-04-24 op * URL [flags,...] [title]\n
556 64f4f8e2 2022-04-24 op static inline struct tab *
557 64f4f8e2 2022-04-24 op parse_tab_line(char *line, struct tab **ct)
559 64f4f8e2 2022-04-24 op struct tab *tab;
560 64f4f8e2 2022-04-24 op char *s, *t, *ap;
561 64f4f8e2 2022-04-24 op const char *uri, *title = "";
562 64f4f8e2 2022-04-24 op int current = 0, killed = 0;
563 64f4f8e2 2022-04-24 op size_t tline = 0, cline = 0;
566 64f4f8e2 2022-04-24 op if ((s = strchr(line, ' ')) == NULL)
570 64f4f8e2 2022-04-24 op if ((t = strchr(s, ' ')) != NULL) {
575 64f4f8e2 2022-04-24 op while ((ap = strsep(&s, ",")) != NULL) {
576 64f4f8e2 2022-04-24 op if (!strcmp(ap, "current"))
578 64f4f8e2 2022-04-24 op else if (!strcmp(ap, "killed"))
580 64f4f8e2 2022-04-24 op else if (!strncmp(ap, "top=", 4))
581 64f4f8e2 2022-04-24 op tline = strtonum(ap+4, 0, UINT32_MAX, NULL);
582 64f4f8e2 2022-04-24 op else if (!strncmp(ap, "cur=", 4))
583 64f4f8e2 2022-04-24 op cline = strtonum(ap + 4, 0, UINT32_MAX, NULL);
586 64f4f8e2 2022-04-24 op if (tline > cline) {
591 64f4f8e2 2022-04-24 op if ((tab = new_tab(uri, NULL, NULL)) == NULL)
592 64f4f8e2 2022-04-24 op err(1, "new_tab");
593 65c49665 2024-01-23 op hist_set_offs(tab->hist, tline, cline);
594 c1d27b0e 2024-06-14 op strlcpy(tab->buffer.title, title, sizeof(tab->buffer.title));
598 64f4f8e2 2022-04-24 op else if (killed)
599 64f4f8e2 2022-04-24 op kill_tab(tab, 1);
605 64f4f8e2 2022-04-24 op load_tabs(void)
607 64f4f8e2 2022-04-24 op struct tab *tab = NULL, *ct = NULL;
608 64f4f8e2 2022-04-24 op FILE *session;
609 64f4f8e2 2022-04-24 op size_t lineno = 0, linesize = 0;
610 64f4f8e2 2022-04-24 op ssize_t linelen;
611 64f4f8e2 2022-04-24 op char *uri, *line = NULL;
613 64f4f8e2 2022-04-24 op if ((session = fopen(session_file, "r")) == NULL) {
614 64f4f8e2 2022-04-24 op new_tab("about:new", NULL, NULL);
615 64f4f8e2 2022-04-24 op new_tab("about:help", NULL, NULL);
619 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, session)) != -1) {
622 64f4f8e2 2022-04-24 op if (linelen > 0 && line[linelen-1] == '\n')
623 64f4f8e2 2022-04-24 op line[linelen-1] = '\0';
625 64f4f8e2 2022-04-24 op if (*line == '<' || *line == '>') {
626 64f4f8e2 2022-04-24 op uri = line + 1;
627 64f4f8e2 2022-04-24 op if (*uri != ' ' || tab == NULL) {
628 64f4f8e2 2022-04-24 op fprintf(stderr, "%s:%zu invalid line\n",
629 64f4f8e2 2022-04-24 op session_file, lineno);
634 64f4f8e2 2022-04-24 op if (*line == '>') /* future hist */
635 65c49665 2024-01-23 op hist_append(tab->hist, uri);
637 65c49665 2024-01-23 op hist_prepend(tab->hist, uri);
639 64f4f8e2 2022-04-24 op tab = parse_tab_line(line, &ct);
642 64f4f8e2 2022-04-24 op fclose(session);
645 64f4f8e2 2022-04-24 op if (ct == NULL || TAILQ_EMPTY(&tabshead))
646 64f4f8e2 2022-04-24 op ct = new_tab("about:new", NULL, NULL);
648 64f4f8e2 2022-04-24 op switch_to_tab(ct);
650 64f4f8e2 2022-04-24 op if (last_time_crashed())
651 64f4f8e2 2022-04-24 op new_tab("about:crash", NULL, NULL);
655 64f4f8e2 2022-04-24 op load_session(struct ohash *certs)
657 64f4f8e2 2022-04-24 op load_certs(certs);
664 64f4f8e2 2022-04-24 op lock_session(void)
666 64f4f8e2 2022-04-24 op struct flock lock;
669 64f4f8e2 2022-04-24 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
672 64f4f8e2 2022-04-24 op lock.l_start = 0;
673 64f4f8e2 2022-04-24 op lock.l_len = 0;
674 64f4f8e2 2022-04-24 op lock.l_type = F_WRLCK;
675 64f4f8e2 2022-04-24 op lock.l_whence = SEEK_SET;
677 64f4f8e2 2022-04-24 op if (fcntl(fd, F_SETLK, &lock) == -1) {