2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 #include "minibuffer.h"
34 struct history history;
36 static struct event autosaveev;
39 switch_to_tab(struct tab *tab)
42 tab->flags &= ~TAB_URGENT;
44 if (operating && tab->flags & TAB_LAZY)
45 load_url_in_tab(tab, tab->hist_cur->h, NULL, LU_MODE_NOHIST);
51 static uint32_t tab_counter;
57 new_tab(const char *url, const char *base, struct tab *after)
64 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
69 TAILQ_INIT(&tab->hist.head);
70 TAILQ_INIT(&tab->buffer.head);
71 TAILQ_INIT(&tab->buffer.page.head);
72 evtimer_set(&tab->loadingev, NULL, NULL);
74 tab->id = tab_new_id();
77 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
79 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
82 tab->flags |= TAB_LAZY;
83 load_url_in_tab(tab, url, base, 0);
89 * Move a tab from the tablist to the killed tab list and erase its
90 * contents. Append should always be 0 to prepend tabs so unkill_tab
91 * can work correctly; appending is only useful during startup when
92 * receiving the list of killed tabs to keep the correct order.
93 * NB: doesn't update the current_tab.
96 kill_tab(struct tab *tab, int append)
101 erase_buffer(&tab->buffer);
102 TAILQ_REMOVE(&tabshead, tab, tabs);
103 ui_schedule_redraw();
106 if (evtimer_pending(&tab->loadingev, NULL))
107 evtimer_del(&tab->loadingev);
110 TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
112 TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
116 TAILQ_FOREACH(tab, &ktabshead, tabs)
118 while (count > max_killed_tabs) {
120 free_tab(TAILQ_LAST(&ktabshead, tabshead));
125 * Resurrects the lastest killed tab and returns it. The tab is already
126 * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
127 * update current_tab.
134 if (TAILQ_EMPTY(&ktabshead))
137 ui_schedule_redraw();
140 t = TAILQ_FIRST(&ktabshead);
141 TAILQ_REMOVE(&ktabshead, t, tabs);
142 TAILQ_INSERT_TAIL(&tabshead, t, tabs);
143 t->flags |= TAB_LAZY;
148 * Free every resource linked to the tab, including the tab itself, and
149 * removes it from the *killed* tablist.
152 free_tab(struct tab *tab)
154 TAILQ_REMOVE(&ktabshead, tab, tabs);
155 hist_clear(&tab->hist);
160 stop_tab(struct tab *tab)
162 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
166 savetab(FILE *fp, struct tab *tab, int killed)
169 size_t top_line, current_line;
172 get_scroll_position(tab, &top_line, ¤t_line);
174 fprintf(fp, "%s ", tab->hist_cur->h);
175 if (tab == current_tab)
176 fprintf(fp, "current,");
178 fprintf(fp, "killed,");
180 fprintf(fp, "top=%zu,cur=%zu %s\n", top_line, current_line,
181 tab->buffer.page.title);
184 TAILQ_FOREACH(h, &tab->hist.head, entries) {
185 if (h == tab->hist_cur) {
190 fprintf(fp, "%s %s\n", future ? ">" : "<", h->h);
206 strlcpy(sfn, session_file_tmp, sizeof(sfn));
207 if ((fd = mkstemp(sfn)) == -1 ||
208 (tmp = fdopen(fd, "w")) == NULL) {
216 TAILQ_FOREACH(tab, &tabshead, tabs)
217 savetab(tmp, tab, 0);
218 TAILQ_FOREACH(tab, &ktabshead, tabs)
219 savetab(tmp, tab, 1);
229 if (rename(sfn, session_file))
232 strlcpy(sfn, history_file_tmp, sizeof(sfn));
233 if ((fd = mkstemp(sfn)) == -1 ||
234 (tmp = fdopen(fd, "w")) == NULL) {
243 for (i = 0; i < history.len && history.dirty > 0; ++i) {
244 if (!history.items[i].dirty)
247 history.items[i].dirty = 0;
249 fprintf(tmp, "%lld %s\n",
250 (long long)history.items[i].ts,
251 history.items[i].uri);
264 rename(sfn, history_file);
268 history_push(struct histitem *hi)
270 size_t i, oldest = 0;
273 for (i = 0; i < history.len; ++i) {
274 if (history.items[i].ts < history.items[oldest].ts)
277 /* remove duplicates */
278 if (!strcmp(history.items[i].uri, hi->uri))
282 if ((uri = strdup(hi->uri)) == NULL)
285 /* don't grow too much; replace the oldest */
286 if (history.len == HISTORY_CAP) {
287 history.items[oldest].ts = hi->ts;
288 free(history.items[oldest].uri);
289 history.items[oldest].uri = uri;
293 history.items[history.len].ts = hi->ts;
294 history.items[history.len].uri = uri;
299 history_cmp(const void *a, const void *b)
301 const struct history_item *i = a, *j = b;
302 return strcmp(i->uri, j->uri);
308 qsort(history.items, history.len, sizeof(history.items[0]),
313 history_add(const char *uri)
315 size_t i, j, insert = 0, oldest = 0;
319 for (i = 0; i < history.len; ++i) {
320 if (history.items[i].ts < history.items[oldest].ts)
323 if (insert != 0 && insert < i)
326 c = strcmp(uri, history.items[i].uri);
328 history.items[i].ts = time(NULL);
329 history.items[i].dirty = 1;
339 if ((u = strdup(uri)) == NULL)
342 /* if history is full, replace the oldest one */
343 if (history.len == HISTORY_CAP) {
344 free(history.items[oldest].uri);
345 history.items[oldest].uri = u;
346 history.items[oldest].ts = time(NULL);
347 history.items[oldest].dirty = 1;
354 /* otherwise just insert in the right spot */
356 for (j = history.len; j > insert; --j)
357 memcpy(&history.items[j], &history.items[j-1],
358 sizeof(history.items[j]));
360 history.items[insert].ts = time(NULL);
361 history.items[insert].uri = u;
362 history.items[insert].dirty = 1;
371 evtimer_set(&autosaveev, autosave_timer, NULL);
375 autosave_timer(int fd, short event, void *data)
381 * Function to be called in "interesting" places where we may want to
382 * schedule an autosave (like on new tab or before loading an url.)
392 if (!evtimer_pending(&autosaveev, NULL)) {
393 tv.tv_sec = autosave;
396 evtimer_add(&autosaveev, &tv);
401 parse_khost_line(char *line, char *tmp[3])
405 for (ap = tmp; ap < &tmp[3] &&
406 (*ap = strsep(&line, " \t\n")) != NULL;) {
411 return ap == &tmp[3] && *line == '\0';
415 load_certs(struct ohash *certs)
417 char *tmp[3], *line = NULL;
419 size_t lineno = 0, linesize = 0;
422 struct tofu_entry *e;
424 if ((f = fopen(known_hosts_file, "r")) == NULL)
427 if ((e = calloc(1, sizeof(*e))) == NULL) {
432 while ((linelen = getline(&line, &linesize, f)) != -1) {
435 if (parse_khost_line(line, tmp)) {
436 strlcpy(e->domain, tmp[0], sizeof(e->domain));
437 strlcpy(e->hash, tmp[1], sizeof(e->hash));
439 e->verified = strtonum(tmp[2], 0, 1, &errstr);
441 errx(1, "%s:%zu verification for %s is %s: %s",
442 known_hosts_file, lineno,
443 e->domain, errstr, tmp[2]);
447 warnx("%s:%zu invalid entry",
448 known_hosts_file, lineno);
463 char *nl, *spc, *line = NULL;
467 if ((hist = fopen(history_file, "r")) == NULL)
470 while ((linelen = getline(&line, &linesize, hist)) != -1) {
471 if ((nl = strchr(line, '\n')) != NULL)
473 if ((spc = strchr(line, ' ')) == NULL)
478 memset(&hi, 0, sizeof(hi));
479 hi.ts = strtonum(line, INT64_MIN, INT64_MAX, &errstr);
482 if (strlcpy(hi.uri, spc, sizeof(hi.uri)) >= sizeof(hi.uri))
495 * Check if the last time telescope crashed. The check is done by
496 * looking at `crashed_file': if it exists then last time we crashed.
497 * Then, while here, touch the file too, it's removed during the
501 last_time_crashed(void)
508 if (unlink(crashed_file) == -1 && errno == ENOENT)
511 if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
519 * Parse and restore a tab from the session file. The format is:
521 * URL [flags,...] [title]\n
523 static inline struct tab *
524 parse_tab_line(char *line, struct tab **ct)
528 const char *uri, *title = "";
529 int current = 0, killed = 0;
530 size_t tline = 0, cline = 0;
533 if ((s = strchr(line, ' ')) == NULL)
537 if ((t = strchr(s, ' ')) != NULL) {
542 while ((ap = strsep(&s, ",")) != NULL) {
543 if (!strcmp(ap, "current"))
545 else if (!strcmp(ap, "killed"))
547 else if (!strncmp(ap, "top=", 4))
548 tline = strtonum(ap+4, 0, UINT32_MAX, NULL);
549 else if (!strncmp(ap, "cur=", 4))
550 cline = strtonum(ap + 4, 0, UINT32_MAX, NULL);
558 if ((tab = new_tab(uri, NULL, NULL)) == NULL)
560 tab->hist_cur->line_off = tline;
561 tab->hist_cur->current_off = cline;
562 strlcpy(tab->buffer.page.title, title, sizeof(tab->buffer.page.title));
575 struct tab *tab = NULL, *ct = NULL;
578 size_t lineno = 0, linesize = 0;
580 char *uri, *line = NULL;
582 if ((session = fopen(session_file, "r")) == NULL) {
583 new_tab("about:new", NULL, NULL);
584 new_tab("about:help", NULL, NULL);
588 while ((linelen = getline(&line, &linesize, session)) != -1) {
591 if (linelen > 0 && line[linelen-1] == '\n')
592 line[linelen-1] = '\0';
594 if (*line == '<' || *line == '>') {
596 if (*uri != ' ' || tab == NULL) {
597 fprintf(stderr, "%s:%zu invalid line\n",
598 session_file, lineno);
603 if ((h = calloc(1, sizeof(*h))) == NULL)
605 strlcpy(h->h, uri, sizeof(h->h));
607 if (*line == '>') /* future hist */
608 hist_push(&tab->hist, h);
610 hist_add_before(&tab->hist, tab->hist_cur, h);
612 tab = parse_tab_line(line, &ct);
618 if (ct == NULL || TAILQ_EMPTY(&tabshead))
619 ct = new_tab("about:new", NULL, NULL);
623 if (last_time_crashed())
624 new_tab("about:crash", NULL, NULL);
628 load_session(struct ohash *certs)
642 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
647 lock.l_type = F_WRLCK;
648 lock.l_whence = SEEK_SET;
650 if (fcntl(fd, F_SETLK, &lock) == -1) {