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.
31 #include "minibuffer.h"
36 struct history history;
38 static struct event autosaveev;
41 switch_to_tab(struct tab *tab)
44 tab->flags &= ~TAB_URGENT;
46 if (operating && tab->flags & TAB_LAZY)
47 load_url_in_tab(tab, hist_cur(tab->hist), NULL,
54 static uint32_t tab_counter;
60 new_tab(const char *url, const char *base, struct tab *after)
67 if ((tab = calloc(1, sizeof(*tab))) == NULL) {
72 if ((tab->hist = hist_new(HIST_LINEAR)) == NULL) {
78 TAILQ_INIT(&tab->buffer.head);
79 TAILQ_INIT(&tab->buffer.page.head);
80 evtimer_set(&tab->loadingev, NULL, NULL);
82 tab->id = tab_new_id();
85 TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
87 TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
90 tab->flags |= TAB_LAZY;
91 load_url_in_tab(tab, url, base, 0);
97 * Move a tab from the tablist to the killed tab list and erase its
98 * contents. Append should always be 0 to prepend tabs so unkill_tab
99 * can work correctly; appending is only useful during startup when
100 * receiving the list of killed tabs to keep the correct order.
101 * NB: doesn't update the current_tab.
104 kill_tab(struct tab *tab, int append)
109 erase_buffer(&tab->buffer);
110 TAILQ_REMOVE(&tabshead, tab, tabs);
111 ui_schedule_redraw();
114 if (evtimer_pending(&tab->loadingev, NULL))
115 evtimer_del(&tab->loadingev);
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,
188 tab->buffer.page.title);
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 if ((uri = strdup(hi->uri)) == NULL)
320 /* don't grow too much; replace the oldest */
321 if (history.len == HISTORY_CAP) {
322 history.items[oldest].ts = hi->ts;
323 free(history.items[oldest].uri);
324 history.items[oldest].uri = uri;
326 /* Growed past the max value, signal to regen the file. */
331 history.items[history.len].ts = hi->ts;
332 history.items[history.len].uri = uri;
337 history_cmp(const void *a, const void *b)
339 const struct history_item *i = a, *j = b;
340 return strcmp(i->uri, j->uri);
346 qsort(history.items, history.len, sizeof(history.items[0]),
351 history_add(const char *uri)
353 size_t i, j, insert = 0, oldest = 0;
357 for (i = 0; i < history.len; ++i) {
358 if (history.items[i].ts < history.items[oldest].ts)
361 if (insert != 0 && insert < i)
364 c = strcmp(uri, history.items[i].uri);
366 history.items[i].ts = time(NULL);
367 history.items[i].dirty = 1;
377 if ((u = strdup(uri)) == NULL)
380 /* if history is full, replace the oldest one */
381 if (history.len == HISTORY_CAP) {
382 free(history.items[oldest].uri);
383 history.items[oldest].uri = u;
384 history.items[oldest].ts = time(NULL);
385 history.items[oldest].dirty = 1;
392 /* otherwise just insert in the right spot */
394 for (j = history.len; j > insert; --j)
395 memcpy(&history.items[j], &history.items[j-1],
396 sizeof(history.items[j]));
398 history.items[insert].ts = time(NULL);
399 history.items[insert].uri = u;
400 history.items[insert].dirty = 1;
409 evtimer_set(&autosaveev, autosave_timer, NULL);
413 autosave_timer(int fd, short event, void *data)
419 * Function to be called in "interesting" places where we may want to
420 * schedule an autosave (like on new tab or before loading an url.)
430 if (!evtimer_pending(&autosaveev, NULL)) {
431 tv.tv_sec = autosave;
434 evtimer_add(&autosaveev, &tv);
439 parse_khost_line(char *line, char *tmp[3])
443 for (ap = tmp; ap < &tmp[3] &&
444 (*ap = strsep(&line, " \t\n")) != NULL;) {
449 return ap == &tmp[3] && *line == '\0';
453 load_certs(struct ohash *certs)
455 char *tmp[3], *line = NULL;
457 size_t lineno = 0, linesize = 0;
460 struct tofu_entry *e;
462 if ((f = fopen(known_hosts_file, "r")) == NULL)
465 if ((e = calloc(1, sizeof(*e))) == NULL) {
470 while ((linelen = getline(&line, &linesize, f)) != -1) {
473 if (parse_khost_line(line, tmp)) {
474 strlcpy(e->domain, tmp[0], sizeof(e->domain));
475 strlcpy(e->hash, tmp[1], sizeof(e->hash));
477 e->verified = strtonum(tmp[2], 0, 1, &errstr);
479 errx(1, "%s:%zu verification for %s is %s: %s",
480 known_hosts_file, lineno,
481 e->domain, errstr, tmp[2]);
485 warnx("%s:%zu invalid entry",
486 known_hosts_file, lineno);
501 char *nl, *spc, *line = NULL;
505 if ((hist = fopen(history_file, "r")) == NULL)
508 while ((linelen = getline(&line, &linesize, hist)) != -1) {
509 if ((nl = strchr(line, '\n')) != NULL)
511 if ((spc = strchr(line, ' ')) == NULL)
516 memset(&hi, 0, sizeof(hi));
517 hi.ts = strtonum(line, INT64_MIN, INT64_MAX, &errstr);
520 if (strlcpy(hi.uri, spc, sizeof(hi.uri)) >= sizeof(hi.uri))
533 * Check if the last time telescope crashed. The check is done by
534 * looking at `crashed_file': if it exists then last time we crashed.
535 * Then, while here, touch the file too, it's removed during the
539 last_time_crashed(void)
546 if (unlink(crashed_file) == -1 && errno == ENOENT)
549 if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
557 * Parse and restore a tab from the session file. The format is:
559 * URL [flags,...] [title]\n
561 static inline struct tab *
562 parse_tab_line(char *line, struct tab **ct)
566 const char *uri, *title = "";
567 int current = 0, killed = 0;
568 size_t tline = 0, cline = 0;
571 if ((s = strchr(line, ' ')) == NULL)
575 if ((t = strchr(s, ' ')) != NULL) {
580 while ((ap = strsep(&s, ",")) != NULL) {
581 if (!strcmp(ap, "current"))
583 else if (!strcmp(ap, "killed"))
585 else if (!strncmp(ap, "top=", 4))
586 tline = strtonum(ap+4, 0, UINT32_MAX, NULL);
587 else if (!strncmp(ap, "cur=", 4))
588 cline = strtonum(ap + 4, 0, UINT32_MAX, NULL);
596 if ((tab = new_tab(uri, NULL, NULL)) == NULL)
598 hist_set_offs(tab->hist, tline, cline);
599 strlcpy(tab->buffer.page.title, title, sizeof(tab->buffer.page.title));
612 struct tab *tab = NULL, *ct = NULL;
614 size_t lineno = 0, linesize = 0;
616 char *uri, *line = NULL;
618 if ((session = fopen(session_file, "r")) == NULL) {
619 new_tab("about:new", NULL, NULL);
620 new_tab("about:help", NULL, NULL);
624 while ((linelen = getline(&line, &linesize, session)) != -1) {
627 if (linelen > 0 && line[linelen-1] == '\n')
628 line[linelen-1] = '\0';
630 if (*line == '<' || *line == '>') {
632 if (*uri != ' ' || tab == NULL) {
633 fprintf(stderr, "%s:%zu invalid line\n",
634 session_file, lineno);
639 if (*line == '>') /* future hist */
640 hist_append(tab->hist, uri);
642 hist_prepend(tab->hist, uri);
644 tab = parse_tab_line(line, &ct);
650 if (ct == NULL || TAILQ_EMPTY(&tabshead))
651 ct = new_tab("about:new", NULL, NULL);
655 if (last_time_crashed())
656 new_tab("about:crash", NULL, NULL);
660 load_session(struct ohash *certs)
674 if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
679 lock.l_type = F_WRLCK;
680 lock.l_whence = SEEK_SET;
682 if (fcntl(fd, F_SETLK, &lock) == -1) {