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.
35 #include "minibuffer.h"
41 struct history history;
43 static unsigned int autosavetimer;
46 switch_to_tab(struct tab *tab)
49 tab->flags &= ~TAB_URGENT;
51 if (operating && tab->flags & TAB_LAZY)
52 load_url_in_tab(tab, hist_cur(tab->hist), NULL,
59 static uint32_t tab_counter;
65 new_tab(const char *url, const char *base, struct tab *after)
72 tab = xcalloc(1, sizeof(*tab));
74 if ((tab->hist = hist_new(HIST_LINEAR)) == NULL) {
80 TAILQ_INIT(&tab->buffer.head);
81 TAILQ_INIT(&tab->buffer.vhead);
83 tab->id = tab_new_id();
86 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
88 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
91 tab->flags |= TAB_LAZY;
92 load_url_in_tab(tab, url, base, 0);
98 * Move a tab from the tablist to the killed tab list and erase its
99 * contents. Append should always be 0 to prepend tabs so unkill_tab
100 * can work correctly; appending is only useful during startup when
101 * receiving the list of killed tabs to keep the correct order.
102 * NB: doesn't update the current_tab.
105 kill_tab(struct tab *tab, int append)
110 erase_buffer(&tab->buffer);
111 TAILQ_REMOVE(&tabshead, tab, tabs);
112 ui_schedule_redraw();
115 ev_timer_cancel(tab->loading_timer);
118 TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
120 TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
124 TAILQ_FOREACH(tab, &ktabshead, tabs)
126 while (count > max_killed_tabs) {
128 free_tab(TAILQ_LAST(&ktabshead, tabshead));
133 * Resurrects the lastest killed tab and returns it. The tab is already
134 * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
135 * update current_tab.
142 if (TAILQ_EMPTY(&ktabshead))
145 ui_schedule_redraw();
148 t = TAILQ_FIRST(&ktabshead);
149 TAILQ_REMOVE(&ktabshead, t, tabs);
150 TAILQ_INSERT_TAIL(&tabshead, t, tabs);
151 t->flags |= TAB_LAZY;
156 * Free every resource linked to the tab, including the tab itself, and
157 * removes it from the *killed* tablist.
160 free_tab(struct tab *tab)
162 TAILQ_REMOVE(&ktabshead, tab, tabs);
163 hist_free(tab->hist);
168 stop_tab(struct tab *tab)
170 ui_send_net(IMSG_STOP, tab->id, -1, NULL, 0);
174 savetab(FILE *fp, struct tab *tab, int killed)
177 size_t top_line, current_line;
179 get_scroll_position(tab, &top_line, ¤t_line);
181 fprintf(fp, "%s ", hist_cur(tab->hist));
182 if (tab == current_tab)
183 fprintf(fp, "current,");
185 fprintf(fp, "killed,");
187 fprintf(fp, "top=%zu,cur=%zu %s\n", top_line, current_line,
190 cur = hist_off(tab->hist);
191 size = hist_size(tab->hist);
192 for (i = 0; i < size; ++i) {
195 fprintf(fp, "%s %s\n", i > cur ? ">" : "<",
196 hist_nth(tab->hist, i));
208 strlcpy(sfn, session_file_tmp, sizeof(sfn));
209 if ((fd = mkstemp(sfn)) == -1 ||
210 (fp = fdopen(fd, "w")) == NULL) {
218 TAILQ_FOREACH(tab, &tabshead, tabs)
220 TAILQ_FOREACH(tab, &ktabshead, tabs)
223 err = fflush(fp) == EOF;
226 if (err || rename(sfn, session_file) == -1)
231 save_all_history(void)
238 strlcpy(sfn, history_file_tmp, sizeof(sfn));
239 if ((fd = mkstemp(sfn)) == -1 ||
240 (fp = fdopen(fd, "w")) == NULL) {
248 for (i = 0; i < history.len; ++i) {
249 history.items[i].dirty = 0;
250 fprintf(fp, "%lld %s\n", (long long)history.items[i].ts,
251 history.items[i].uri);
254 err = fflush(fp) == EOF;
257 if (err || rename(sfn, history_file) == -1) {
267 save_dirty_history(void)
272 if ((fp = fopen(history_file, "a")) == NULL)
275 for (i = 0; i < history.len && history.dirty > 0; ++i) {
276 if (!history.items[i].dirty)
279 history.items[i].dirty = 0;
280 fprintf(fp, "%lld %s\n", (long long)history.items[i].ts,
281 history.items[i].uri);
296 if (history.extra > HISTORY_CAP/2)
298 else if (history.dirty)
299 save_dirty_history();
303 history_push(struct histitem *hi)
305 size_t i, oldest = 0;
308 for (i = 0; i < history.len; ++i) {
309 if (history.items[i].ts < history.items[oldest].ts)
312 /* remove duplicates */
313 if (!strcmp(history.items[i].uri, hi->uri))
317 uri = xstrdup(hi->uri);
319 /* don't grow too much; replace the oldest */
320 if (history.len == HISTORY_CAP) {
321 history.items[oldest].ts = hi->ts;
322 free(history.items[oldest].uri);
323 history.items[oldest].uri = uri;
325 /* Growed past the max value, signal to regen the file. */
330 history.items[history.len].ts = hi->ts;
331 history.items[history.len].uri = uri;
336 history_cmp(const void *a, const void *b)
338 const struct history_item *i = a, *j = b;
339 return strcmp(i->uri, j->uri);
345 qsort(history.items, history.len, sizeof(history.items[0]),
350 history_add(const char *uri)
352 size_t i, j, insert = 0, oldest = 0;
356 for (i = 0; i < history.len; ++i) {
357 if (history.items[i].ts < history.items[oldest].ts)
360 if (insert != 0 && insert < i)
363 c = strcmp(uri, history.items[i].uri);
365 history.items[i].ts = time(NULL);
366 history.items[i].dirty = 1;
378 /* if history is full, replace the oldest one */
379 if (history.len == HISTORY_CAP) {
380 free(history.items[oldest].uri);
381 history.items[oldest].uri = u;
382 history.items[oldest].ts = time(NULL);
383 history.items[oldest].dirty = 1;
390 /* otherwise just insert in the right spot */
392 for (j = history.len; j > insert; --j)
393 memcpy(&history.items[j], &history.items[j-1],
394 sizeof(history.items[j]));
396 history.items[insert].ts = time(NULL);
397 history.items[insert].uri = u;
398 history.items[insert].dirty = 1;
411 autosave_timer(int fd, int event, void *data)
417 * Function to be called in "interesting" places where we may want to
418 * schedule an autosave (like on new tab or before loading an url.)
428 if (!ev_timer_pending(autosavetimer)) {
429 tv.tv_sec = autosave;
432 autosavetimer = ev_timer(&tv, autosave_timer, NULL);
437 parse_khost_line(char *line, char *tmp[3])
441 for (ap = tmp; ap < &tmp[3] &&
442 (*ap = strsep(&line, " \t\n")) != NULL;) {
447 return ap == &tmp[3] && *line == '\0';
451 load_certs(struct ohash *certs)
453 char *tmp[3], *line = NULL;
455 size_t lineno = 0, linesize = 0;
458 struct tofu_entry *e;
460 if ((f = fopen(known_hosts_file, "r")) == NULL)
463 e = xcalloc(1, sizeof(*e));
465 while ((linelen = getline(&line, &linesize, f)) != -1) {
468 if (parse_khost_line(line, tmp)) {
469 strlcpy(e->domain, tmp[0], sizeof(e->domain));
470 strlcpy(e->hash, tmp[1], sizeof(e->hash));
472 e->verified = strtonum(tmp[2], 0, 1, &errstr);
474 errx(1, "%s:%zu verification for %s is %s: %s",
475 known_hosts_file, lineno,
476 e->domain, errstr, tmp[2]);
480 warnx("%s:%zu invalid entry",
481 known_hosts_file, lineno);
496 char *nl, *spc, *line = NULL;
500 if ((hist = fopen(history_file, "r")) == NULL)
503 while ((linelen = getline(&line, &linesize, hist)) != -1) {
504 if ((nl = strchr(line, '\n')) != NULL)
506 if ((spc = strchr(line, ' ')) == NULL)
511 memset(&hi, 0, sizeof(hi));
512 hi.ts = strtonum(line, INT64_MIN, INT64_MAX, &errstr);
515 if (strlcpy(hi.uri, spc, sizeof(hi.uri)) >= sizeof(hi.uri))
528 * Check if the last time telescope crashed. The check is done by
529 * looking at `crashed_file': if it exists then last time we crashed.
530 * Then, while here, touch the file too, it's removed during the
534 last_time_crashed(void)
541 if (unlink(crashed_file) == -1 && errno == ENOENT)
544 if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
552 * Parse and restore a tab from the session file. The format is:
554 * URL [flags,...] [title]\n
556 static inline struct tab *
557 parse_tab_line(char *line, struct tab **ct)
561 const char *uri, *title = "";
562 int current = 0, killed = 0;
563 size_t tline = 0, cline = 0;
566 if ((s = strchr(line, ' ')) == NULL)
570 if ((t = strchr(s, ' ')) != NULL) {
575 while ((ap = strsep(&s, ",")) != NULL) {
576 if (!strcmp(ap, "current"))
578 else if (!strcmp(ap, "killed"))
580 else if (!strncmp(ap, "top=", 4))
581 tline = strtonum(ap+4, 0, UINT32_MAX, NULL);
582 else if (!strncmp(ap, "cur=", 4))
583 cline = strtonum(ap + 4, 0, UINT32_MAX, NULL);
591 if ((tab = new_tab(uri, NULL, NULL)) == NULL)
593 hist_set_offs(tab->hist, tline, cline);
594 strlcpy(tab->buffer.title, title, sizeof(tab->buffer.title));
607 struct tab *tab = NULL, *ct = NULL;
609 size_t lineno = 0, linesize = 0;
611 char *uri, *line = NULL;
613 if ((session = fopen(session_file, "r")) == NULL) {
614 new_tab("about:new", NULL, NULL);
615 new_tab("about:help", NULL, NULL);
619 while ((linelen = getline(&line, &linesize, session)) != -1) {
622 if (linelen > 0 && line[linelen-1] == '\n')
623 line[linelen-1] = '\0';
625 if (*line == '<' || *line == '>') {
627 if (*uri != ' ' || tab == NULL) {
628 fprintf(stderr, "%s:%zu invalid line\n",
629 session_file, lineno);
634 if (*line == '>') /* future hist */
635 hist_append(tab->hist, uri);
637 hist_prepend(tab->hist, uri);
639 tab = parse_tab_line(line, &ct);
645 if (ct == NULL || TAILQ_EMPTY(&tabshead))
646 ct = new_tab("about:new", NULL, NULL);
650 if (last_time_crashed())
651 new_tab("about:crash", NULL, NULL);
655 load_session(struct ohash *certs)
669 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
674 lock.l_type = F_WRLCK;
675 lock.l_whence = SEEK_SET;
677 if (fcntl(fd, F_SETLK, &lock) == -1) {