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 1fce2e75 2021-08-14 op #include <errno.h>
20 64f4f8e2 2022-04-24 op #include <fcntl.h>
21 f63b8f73 2022-04-24 op #include <limits.h>
22 1fce2e75 2021-08-14 op #include <stdio.h>
23 1fce2e75 2021-08-14 op #include <stdlib.h>
24 1fce2e75 2021-08-14 op #include <string.h>
25 9e97090d 2022-02-26 op #include <time.h>
26 1fce2e75 2021-08-14 op #include <unistd.h>
28 1fce2e75 2021-08-14 op #include "defaults.h"
29 b9fcc0e9 2021-10-08 op #include "fs.h"
30 65c49665 2024-01-23 op #include "hist.h"
31 1fce2e75 2021-08-14 op #include "minibuffer.h"
32 1fce2e75 2021-08-14 op #include "session.h"
33 d163c210 2024-02-06 op #include "tofu.h"
34 1fce2e75 2021-08-14 op #include "ui.h"
36 9e97090d 2022-02-26 op struct history history;
38 1fce2e75 2021-08-14 op static struct event autosaveev;
41 1fce2e75 2021-08-14 op switch_to_tab(struct tab *tab)
43 1fce2e75 2021-08-14 op current_tab = tab;
44 1fce2e75 2021-08-14 op tab->flags &= ~TAB_URGENT;
46 fb8dcd1c 2022-01-18 op if (operating && tab->flags & TAB_LAZY)
47 65c49665 2024-01-23 op load_url_in_tab(tab, hist_cur(tab->hist), NULL,
48 65c49665 2024-01-23 op LU_MODE_NOHIST);
52 1fce2e75 2021-08-14 op tab_new_id(void)
54 1fce2e75 2021-08-14 op static uint32_t tab_counter;
56 1fce2e75 2021-08-14 op return tab_counter++;
60 1fce2e75 2021-08-14 op new_tab(const char *url, const char *base, struct tab *after)
62 1fce2e75 2021-08-14 op struct tab *tab;
64 87aeb47b 2021-08-18 op ui_schedule_redraw();
65 1fce2e75 2021-08-14 op autosave_hook();
67 1fce2e75 2021-08-14 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
68 1fce2e75 2021-08-14 op event_loopbreak();
72 65c49665 2024-01-23 op if ((tab->hist = hist_new(HIST_LINEAR)) == NULL) {
74 65c49665 2024-01-23 op event_loopbreak();
78 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.head);
79 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.page.head);
80 3b30597e 2022-02-11 op evtimer_set(&tab->loadingev, NULL, NULL);
82 1fce2e75 2021-08-14 op tab->id = tab_new_id();
84 1fce2e75 2021-08-14 op if (after != NULL)
85 1fce2e75 2021-08-14 op TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
87 1fce2e75 2021-08-14 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
89 ea639250 2021-01-02 op if (!operating)
90 ea639250 2021-01-02 op tab->flags |= TAB_LAZY;
91 1fce2e75 2021-08-14 op load_url_in_tab(tab, url, base, 0);
92 ea639250 2021-01-02 op switch_to_tab(tab);
97 6c74799d 2022-01-05 op * Move a tab from the tablist to the killed tab list and erase its
98 05de8ac3 2022-01-06 op * contents. Append should always be 0 to prepend tabs so unkill_tab
99 05de8ac3 2022-01-06 op * can work correctly; appending is only useful during startup when
100 05de8ac3 2022-01-06 op * receiving the list of killed tabs to keep the correct order.
101 05de8ac3 2022-01-06 op * NB: doesn't update the current_tab.
104 05de8ac3 2022-01-06 op kill_tab(struct tab *tab, int append)
108 1fce2e75 2021-08-14 op stop_tab(tab);
109 6c74799d 2022-01-05 op erase_buffer(&tab->buffer);
110 6c74799d 2022-01-05 op TAILQ_REMOVE(&tabshead, tab, tabs);
111 87aeb47b 2021-08-18 op ui_schedule_redraw();
112 1fce2e75 2021-08-14 op autosave_hook();
114 1fce2e75 2021-08-14 op if (evtimer_pending(&tab->loadingev, NULL))
115 1fce2e75 2021-08-14 op evtimer_del(&tab->loadingev);
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 f63b8f73 2022-04-24 op tab->buffer.page.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 9e97090d 2022-02-26 op if ((uri = strdup(hi->uri)) == NULL)
320 9e97090d 2022-02-26 op /* don't grow too much; replace the oldest */
321 9e97090d 2022-02-26 op if (history.len == HISTORY_CAP) {
322 9e97090d 2022-02-26 op history.items[oldest].ts = hi->ts;
323 9e97090d 2022-02-26 op free(history.items[oldest].uri);
324 9e97090d 2022-02-26 op history.items[oldest].uri = uri;
326 5bd159bd 2022-05-11 op /* Growed past the max value, signal to regen the file. */
327 5bd159bd 2022-05-11 op history.extra++;
331 9e97090d 2022-02-26 op history.items[history.len].ts = hi->ts;
332 9e97090d 2022-02-26 op history.items[history.len].uri = uri;
333 9e97090d 2022-02-26 op history.len++;
337 9e97090d 2022-02-26 op history_cmp(const void *a, const void *b)
339 9e97090d 2022-02-26 op const struct history_item *i = a, *j = b;
340 9e97090d 2022-02-26 op return strcmp(i->uri, j->uri);
344 9e97090d 2022-02-26 op history_sort(void)
346 9e97090d 2022-02-26 op qsort(history.items, history.len, sizeof(history.items[0]),
347 9e97090d 2022-02-26 op history_cmp);
351 9e97090d 2022-02-26 op history_add(const char *uri)
353 9e97090d 2022-02-26 op size_t i, j, insert = 0, oldest = 0;
357 9e97090d 2022-02-26 op for (i = 0; i < history.len; ++i) {
358 9e97090d 2022-02-26 op if (history.items[i].ts < history.items[oldest].ts)
361 9e97090d 2022-02-26 op if (insert != 0 && insert < i)
364 9e97090d 2022-02-26 op c = strcmp(uri, history.items[i].uri);
365 9e97090d 2022-02-26 op if (c == 0) {
366 9e97090d 2022-02-26 op history.items[i].ts = time(NULL);
367 9e97090d 2022-02-26 op history.items[i].dirty = 1;
368 9e97090d 2022-02-26 op history.dirty++;
369 9e97090d 2022-02-26 op autosave_hook();
377 9e97090d 2022-02-26 op if ((u = strdup(uri)) == NULL)
380 9e97090d 2022-02-26 op /* if history is full, replace the oldest one */
381 9e97090d 2022-02-26 op if (history.len == HISTORY_CAP) {
382 9e97090d 2022-02-26 op free(history.items[oldest].uri);
383 9e97090d 2022-02-26 op history.items[oldest].uri = u;
384 9e97090d 2022-02-26 op history.items[oldest].ts = time(NULL);
385 9e97090d 2022-02-26 op history.items[oldest].dirty = 1;
386 9e97090d 2022-02-26 op history.dirty++;
387 9e97090d 2022-02-26 op history_sort();
388 9e97090d 2022-02-26 op autosave_hook();
392 9e97090d 2022-02-26 op /* otherwise just insert in the right spot */
394 9e97090d 2022-02-26 op for (j = history.len; j > insert; --j)
395 9e97090d 2022-02-26 op memcpy(&history.items[j], &history.items[j-1],
396 9e97090d 2022-02-26 op sizeof(history.items[j]));
398 9e97090d 2022-02-26 op history.items[insert].ts = time(NULL);
399 9e97090d 2022-02-26 op history.items[insert].uri = u;
400 9e97090d 2022-02-26 op history.items[insert].dirty = 1;
401 9e97090d 2022-02-26 op history.dirty++;
402 9e97090d 2022-02-26 op history.len++;
403 9e97090d 2022-02-26 op autosave_hook();
407 1fce2e75 2021-08-14 op autosave_init(void)
409 1fce2e75 2021-08-14 op evtimer_set(&autosaveev, autosave_timer, NULL);
413 1fce2e75 2021-08-14 op autosave_timer(int fd, short event, void *data)
415 1fce2e75 2021-08-14 op save_session();
419 1fce2e75 2021-08-14 op * Function to be called in "interesting" places where we may want to
420 1fce2e75 2021-08-14 op * schedule an autosave (like on new tab or before loading an url.)
423 1fce2e75 2021-08-14 op autosave_hook(void)
425 1fce2e75 2021-08-14 op struct timeval tv;
427 1fce2e75 2021-08-14 op if (autosave <= 0)
430 1fce2e75 2021-08-14 op if (!evtimer_pending(&autosaveev, NULL)) {
431 1fce2e75 2021-08-14 op tv.tv_sec = autosave;
432 1fce2e75 2021-08-14 op tv.tv_usec = 0;
434 1fce2e75 2021-08-14 op evtimer_add(&autosaveev, &tv);
438 64f4f8e2 2022-04-24 op static inline int
439 64f4f8e2 2022-04-24 op parse_khost_line(char *line, char *tmp[3])
443 64f4f8e2 2022-04-24 op for (ap = tmp; ap < &tmp[3] &&
444 64f4f8e2 2022-04-24 op (*ap = strsep(&line, " \t\n")) != NULL;) {
445 64f4f8e2 2022-04-24 op if (**ap != '\0')
449 64f4f8e2 2022-04-24 op return ap == &tmp[3] && *line == '\0';
453 64f4f8e2 2022-04-24 op load_certs(struct ohash *certs)
455 64f4f8e2 2022-04-24 op char *tmp[3], *line = NULL;
456 64f4f8e2 2022-04-24 op const char *errstr;
457 64f4f8e2 2022-04-24 op size_t lineno = 0, linesize = 0;
458 64f4f8e2 2022-04-24 op ssize_t linelen;
460 64f4f8e2 2022-04-24 op struct tofu_entry *e;
462 64f4f8e2 2022-04-24 op if ((f = fopen(known_hosts_file, "r")) == NULL)
465 64f4f8e2 2022-04-24 op if ((e = calloc(1, sizeof(*e))) == NULL) {
470 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, f)) != -1) {
473 64f4f8e2 2022-04-24 op if (parse_khost_line(line, tmp)) {
474 64f4f8e2 2022-04-24 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
475 64f4f8e2 2022-04-24 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
477 64f4f8e2 2022-04-24 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
478 64f4f8e2 2022-04-24 op if (errstr != NULL)
479 64f4f8e2 2022-04-24 op errx(1, "%s:%zu verification for %s is %s: %s",
480 64f4f8e2 2022-04-24 op known_hosts_file, lineno,
481 64f4f8e2 2022-04-24 op e->domain, errstr, tmp[2]);
483 64f4f8e2 2022-04-24 op tofu_add(certs, e);
485 64f4f8e2 2022-04-24 op warnx("%s:%zu invalid entry",
486 64f4f8e2 2022-04-24 op known_hosts_file, lineno);
496 64f4f8e2 2022-04-24 op load_hist(void)
499 64f4f8e2 2022-04-24 op size_t linesize = 0;
500 64f4f8e2 2022-04-24 op ssize_t linelen;
501 64f4f8e2 2022-04-24 op char *nl, *spc, *line = NULL;
502 64f4f8e2 2022-04-24 op const char *errstr;
503 64f4f8e2 2022-04-24 op struct histitem hi;
505 64f4f8e2 2022-04-24 op if ((hist = fopen(history_file, "r")) == NULL)
508 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, hist)) != -1) {
509 64f4f8e2 2022-04-24 op if ((nl = strchr(line, '\n')) != NULL)
511 64f4f8e2 2022-04-24 op if ((spc = strchr(line, ' ')) == NULL)
516 64f4f8e2 2022-04-24 op memset(&hi, 0, sizeof(hi));
517 64f4f8e2 2022-04-24 op hi.ts = strtonum(line, INT64_MIN, INT64_MAX, &errstr);
518 64f4f8e2 2022-04-24 op if (errstr != NULL)
520 64f4f8e2 2022-04-24 op if (strlcpy(hi.uri, spc, sizeof(hi.uri)) >= sizeof(hi.uri))
523 64f4f8e2 2022-04-24 op history_push(&hi);
526 64f4f8e2 2022-04-24 op fclose(hist);
529 64f4f8e2 2022-04-24 op history_sort();
533 64f4f8e2 2022-04-24 op * Check if the last time telescope crashed. The check is done by
534 64f4f8e2 2022-04-24 op * looking at `crashed_file': if it exists then last time we crashed.
535 64f4f8e2 2022-04-24 op * Then, while here, touch the file too, it's removed during the
539 64f4f8e2 2022-04-24 op last_time_crashed(void)
541 64f4f8e2 2022-04-24 op int fd, crashed = 1;
543 64f4f8e2 2022-04-24 op if (safe_mode)
546 64f4f8e2 2022-04-24 op if (unlink(crashed_file) == -1 && errno == ENOENT)
549 64f4f8e2 2022-04-24 op if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
550 64f4f8e2 2022-04-24 op return crashed;
553 64f4f8e2 2022-04-24 op return crashed;
557 64f4f8e2 2022-04-24 op * Parse and restore a tab from the session file. The format is:
559 64f4f8e2 2022-04-24 op * URL [flags,...] [title]\n
561 64f4f8e2 2022-04-24 op static inline struct tab *
562 64f4f8e2 2022-04-24 op parse_tab_line(char *line, struct tab **ct)
564 64f4f8e2 2022-04-24 op struct tab *tab;
565 64f4f8e2 2022-04-24 op char *s, *t, *ap;
566 64f4f8e2 2022-04-24 op const char *uri, *title = "";
567 64f4f8e2 2022-04-24 op int current = 0, killed = 0;
568 64f4f8e2 2022-04-24 op size_t tline = 0, cline = 0;
571 64f4f8e2 2022-04-24 op if ((s = strchr(line, ' ')) == NULL)
575 64f4f8e2 2022-04-24 op if ((t = strchr(s, ' ')) != NULL) {
580 64f4f8e2 2022-04-24 op while ((ap = strsep(&s, ",")) != NULL) {
581 64f4f8e2 2022-04-24 op if (!strcmp(ap, "current"))
583 64f4f8e2 2022-04-24 op else if (!strcmp(ap, "killed"))
585 64f4f8e2 2022-04-24 op else if (!strncmp(ap, "top=", 4))
586 64f4f8e2 2022-04-24 op tline = strtonum(ap+4, 0, UINT32_MAX, NULL);
587 64f4f8e2 2022-04-24 op else if (!strncmp(ap, "cur=", 4))
588 64f4f8e2 2022-04-24 op cline = strtonum(ap + 4, 0, UINT32_MAX, NULL);
591 64f4f8e2 2022-04-24 op if (tline > cline) {
596 64f4f8e2 2022-04-24 op if ((tab = new_tab(uri, NULL, NULL)) == NULL)
597 64f4f8e2 2022-04-24 op err(1, "new_tab");
598 65c49665 2024-01-23 op hist_set_offs(tab->hist, tline, cline);
599 64f4f8e2 2022-04-24 op strlcpy(tab->buffer.page.title, title, sizeof(tab->buffer.page.title));
603 64f4f8e2 2022-04-24 op else if (killed)
604 64f4f8e2 2022-04-24 op kill_tab(tab, 1);
610 64f4f8e2 2022-04-24 op load_tabs(void)
612 64f4f8e2 2022-04-24 op struct tab *tab = NULL, *ct = NULL;
613 64f4f8e2 2022-04-24 op FILE *session;
614 64f4f8e2 2022-04-24 op size_t lineno = 0, linesize = 0;
615 64f4f8e2 2022-04-24 op ssize_t linelen;
616 64f4f8e2 2022-04-24 op char *uri, *line = NULL;
618 64f4f8e2 2022-04-24 op if ((session = fopen(session_file, "r")) == NULL) {
619 64f4f8e2 2022-04-24 op new_tab("about:new", NULL, NULL);
620 64f4f8e2 2022-04-24 op new_tab("about:help", NULL, NULL);
624 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, session)) != -1) {
627 64f4f8e2 2022-04-24 op if (linelen > 0 && line[linelen-1] == '\n')
628 64f4f8e2 2022-04-24 op line[linelen-1] = '\0';
630 64f4f8e2 2022-04-24 op if (*line == '<' || *line == '>') {
631 64f4f8e2 2022-04-24 op uri = line + 1;
632 64f4f8e2 2022-04-24 op if (*uri != ' ' || tab == NULL) {
633 64f4f8e2 2022-04-24 op fprintf(stderr, "%s:%zu invalid line\n",
634 64f4f8e2 2022-04-24 op session_file, lineno);
639 64f4f8e2 2022-04-24 op if (*line == '>') /* future hist */
640 65c49665 2024-01-23 op hist_append(tab->hist, uri);
642 65c49665 2024-01-23 op hist_prepend(tab->hist, uri);
644 64f4f8e2 2022-04-24 op tab = parse_tab_line(line, &ct);
647 64f4f8e2 2022-04-24 op fclose(session);
650 64f4f8e2 2022-04-24 op if (ct == NULL || TAILQ_EMPTY(&tabshead))
651 64f4f8e2 2022-04-24 op ct = new_tab("about:new", NULL, NULL);
653 64f4f8e2 2022-04-24 op switch_to_tab(ct);
655 64f4f8e2 2022-04-24 op if (last_time_crashed())
656 64f4f8e2 2022-04-24 op new_tab("about:crash", NULL, NULL);
660 64f4f8e2 2022-04-24 op load_session(struct ohash *certs)
662 64f4f8e2 2022-04-24 op load_certs(certs);
669 64f4f8e2 2022-04-24 op lock_session(void)
671 64f4f8e2 2022-04-24 op struct flock lock;
674 64f4f8e2 2022-04-24 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
677 64f4f8e2 2022-04-24 op lock.l_start = 0;
678 64f4f8e2 2022-04-24 op lock.l_len = 0;
679 64f4f8e2 2022-04-24 op lock.l_type = F_WRLCK;
680 64f4f8e2 2022-04-24 op lock.l_whence = SEEK_SET;
682 64f4f8e2 2022-04-24 op if (fcntl(fd, F_SETLK, &lock) == -1) {