Blame


1 1fce2e75 2021-08-14 op /*
2 1fce2e75 2021-08-14 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 1fce2e75 2021-08-14 op *
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.
7 1fce2e75 2021-08-14 op *
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.
15 1fce2e75 2021-08-14 op */
16 1fce2e75 2021-08-14 op
17 1fce2e75 2021-08-14 op #include "compat.h"
18 1fce2e75 2021-08-14 op
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>
27 1fce2e75 2021-08-14 op
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"
35 1fce2e75 2021-08-14 op
36 9e97090d 2022-02-26 op struct history history;
37 9e97090d 2022-02-26 op
38 1fce2e75 2021-08-14 op static struct event autosaveev;
39 1fce2e75 2021-08-14 op
40 1fce2e75 2021-08-14 op void
41 1fce2e75 2021-08-14 op switch_to_tab(struct tab *tab)
42 1fce2e75 2021-08-14 op {
43 1fce2e75 2021-08-14 op current_tab = tab;
44 1fce2e75 2021-08-14 op tab->flags &= ~TAB_URGENT;
45 1fce2e75 2021-08-14 op
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);
49 1fce2e75 2021-08-14 op }
50 1fce2e75 2021-08-14 op
51 1fce2e75 2021-08-14 op unsigned int
52 1fce2e75 2021-08-14 op tab_new_id(void)
53 1fce2e75 2021-08-14 op {
54 1fce2e75 2021-08-14 op static uint32_t tab_counter;
55 1fce2e75 2021-08-14 op
56 1fce2e75 2021-08-14 op return tab_counter++;
57 1fce2e75 2021-08-14 op }
58 1fce2e75 2021-08-14 op
59 1fce2e75 2021-08-14 op struct tab *
60 1fce2e75 2021-08-14 op new_tab(const char *url, const char *base, struct tab *after)
61 1fce2e75 2021-08-14 op {
62 1fce2e75 2021-08-14 op struct tab *tab;
63 1fce2e75 2021-08-14 op
64 87aeb47b 2021-08-18 op ui_schedule_redraw();
65 1fce2e75 2021-08-14 op autosave_hook();
66 1fce2e75 2021-08-14 op
67 1fce2e75 2021-08-14 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
68 1fce2e75 2021-08-14 op event_loopbreak();
69 1fce2e75 2021-08-14 op return NULL;
70 1fce2e75 2021-08-14 op }
71 1fce2e75 2021-08-14 op
72 65c49665 2024-01-23 op if ((tab->hist = hist_new(HIST_LINEAR)) == NULL) {
73 65c49665 2024-01-23 op free(tab);
74 65c49665 2024-01-23 op event_loopbreak();
75 65c49665 2024-01-23 op return NULL;
76 65c49665 2024-01-23 op }
77 65c49665 2024-01-23 op
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);
81 1fce2e75 2021-08-14 op
82 1fce2e75 2021-08-14 op tab->id = tab_new_id();
83 1fce2e75 2021-08-14 op
84 1fce2e75 2021-08-14 op if (after != NULL)
85 1fce2e75 2021-08-14 op TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
86 1fce2e75 2021-08-14 op else
87 1fce2e75 2021-08-14 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
88 1fce2e75 2021-08-14 op
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);
93 1fce2e75 2021-08-14 op return tab;
94 1fce2e75 2021-08-14 op }
95 1fce2e75 2021-08-14 op
96 1fce2e75 2021-08-14 op /*
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.
102 1fce2e75 2021-08-14 op */
103 1fce2e75 2021-08-14 op void
104 05de8ac3 2022-01-06 op kill_tab(struct tab *tab, int append)
105 1fce2e75 2021-08-14 op {
106 6c74799d 2022-01-05 op int count;
107 6c74799d 2022-01-05 op
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();
113 1fce2e75 2021-08-14 op
114 1fce2e75 2021-08-14 op if (evtimer_pending(&tab->loadingev, NULL))
115 1fce2e75 2021-08-14 op evtimer_del(&tab->loadingev);
116 1fce2e75 2021-08-14 op
117 05de8ac3 2022-01-06 op if (append)
118 05de8ac3 2022-01-06 op TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
119 05de8ac3 2022-01-06 op else
120 05de8ac3 2022-01-06 op TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
121 6c74799d 2022-01-05 op
122 6c74799d 2022-01-05 op /* gc closed tabs */
123 6c74799d 2022-01-05 op count = 0;
124 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &ktabshead, tabs)
125 6c74799d 2022-01-05 op count++;
126 6c74799d 2022-01-05 op while (count > max_killed_tabs) {
127 6c74799d 2022-01-05 op count--;
128 6c74799d 2022-01-05 op free_tab(TAILQ_LAST(&ktabshead, tabshead));
129 6c74799d 2022-01-05 op }
130 6c74799d 2022-01-05 op }
131 6c74799d 2022-01-05 op
132 6c74799d 2022-01-05 op /*
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.
136 6c74799d 2022-01-05 op */
137 6c74799d 2022-01-05 op struct tab *
138 6c74799d 2022-01-05 op unkill_tab(void)
139 6c74799d 2022-01-05 op {
140 6c74799d 2022-01-05 op struct tab *t;
141 6c74799d 2022-01-05 op
142 6c74799d 2022-01-05 op if (TAILQ_EMPTY(&ktabshead))
143 6c74799d 2022-01-05 op return NULL;
144 6c74799d 2022-01-05 op
145 b7286684 2022-01-13 op ui_schedule_redraw();
146 6c74799d 2022-01-05 op autosave_hook();
147 6c74799d 2022-01-05 op
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;
152 6c74799d 2022-01-05 op return t;
153 6c74799d 2022-01-05 op }
154 6c74799d 2022-01-05 op
155 6c74799d 2022-01-05 op /*
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.
158 6c74799d 2022-01-05 op */
159 6c74799d 2022-01-05 op void
160 6c74799d 2022-01-05 op free_tab(struct tab *tab)
161 6c74799d 2022-01-05 op {
162 6c74799d 2022-01-05 op TAILQ_REMOVE(&ktabshead, tab, tabs);
163 65c49665 2024-01-23 op hist_free(tab->hist);
164 1fce2e75 2021-08-14 op free(tab);
165 1fce2e75 2021-08-14 op }
166 1fce2e75 2021-08-14 op
167 1fce2e75 2021-08-14 op void
168 1fce2e75 2021-08-14 op stop_tab(struct tab *tab)
169 1fce2e75 2021-08-14 op {
170 d35e18b3 2024-02-04 op ui_send_net(IMSG_STOP, tab->id, -1, NULL, 0);
171 1fce2e75 2021-08-14 op }
172 1fce2e75 2021-08-14 op
173 6c74799d 2022-01-05 op static inline void
174 f63b8f73 2022-04-24 op savetab(FILE *fp, struct tab *tab, int killed)
175 1fce2e75 2021-08-14 op {
176 65c49665 2024-01-23 op size_t i, size, cur;
177 f63b8f73 2022-04-24 op size_t top_line, current_line;
178 1fce2e75 2021-08-14 op
179 f63b8f73 2022-04-24 op get_scroll_position(tab, &top_line, &current_line);
180 d0971653 2021-09-15 op
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,");
184 6c74799d 2022-01-05 op if (killed)
185 f63b8f73 2022-04-24 op fprintf(fp, "killed,");
186 1fce2e75 2021-08-14 op
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);
189 e795e935 2022-01-18 op
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)
194 6c74799d 2022-01-05 op continue;
195 65c49665 2024-01-23 op fprintf(fp, "%s %s\n", i > cur ? ">" : "<",
196 65c49665 2024-01-23 op hist_nth(tab->hist, i));
197 6c74799d 2022-01-05 op }
198 6c74799d 2022-01-05 op }
199 1040cc7f 2021-01-02 op
200 5bd159bd 2022-05-11 op static void
201 5bd159bd 2022-05-11 op save_tabs(void)
202 6c74799d 2022-01-05 op {
203 5bd159bd 2022-05-11 op FILE *fp;
204 de6a6a40 2022-04-24 op struct tab *tab;
205 5bd159bd 2022-05-11 op int fd, err;
206 de6a6a40 2022-04-24 op char sfn[PATH_MAX];
207 1fce2e75 2021-08-14 op
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) {
212 de6a6a40 2022-04-24 op unlink(sfn);
213 de6a6a40 2022-04-24 op close(fd);
214 de6a6a40 2022-04-24 op }
215 f63b8f73 2022-04-24 op return;
216 de6a6a40 2022-04-24 op }
217 6c74799d 2022-01-05 op
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);
222 6c74799d 2022-01-05 op
223 5bd159bd 2022-05-11 op err = fflush(fp) == EOF;
224 5bd159bd 2022-05-11 op fclose(fp);
225 9e97090d 2022-02-26 op
226 5bd159bd 2022-05-11 op if (err || rename(sfn, session_file) == -1)
227 de6a6a40 2022-04-24 op unlink(sfn);
228 5bd159bd 2022-05-11 op }
229 f63b8f73 2022-04-24 op
230 5bd159bd 2022-05-11 op static void
231 5bd159bd 2022-05-11 op save_all_history(void)
232 5bd159bd 2022-05-11 op {
233 5bd159bd 2022-05-11 op FILE *fp;
234 5bd159bd 2022-05-11 op size_t i;
235 5bd159bd 2022-05-11 op int fd, err;
236 5bd159bd 2022-05-11 op char sfn[PATH_MAX];
237 de6a6a40 2022-04-24 op
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) {
242 5bd159bd 2022-05-11 op unlink(sfn);
243 5bd159bd 2022-05-11 op close(fd);
244 5bd159bd 2022-05-11 op }
245 de6a6a40 2022-04-24 op return;
246 5bd159bd 2022-05-11 op }
247 de6a6a40 2022-04-24 op
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);
252 5bd159bd 2022-05-11 op }
253 9e97090d 2022-02-26 op
254 5bd159bd 2022-05-11 op err = fflush(fp) == EOF;
255 5bd159bd 2022-05-11 op fclose(fp);
256 5bd159bd 2022-05-11 op
257 5bd159bd 2022-05-11 op if (err || rename(sfn, history_file) == -1) {
258 5bd159bd 2022-05-11 op unlink(sfn);
259 5bd159bd 2022-05-11 op return;
260 9e97090d 2022-02-26 op }
261 f63b8f73 2022-04-24 op
262 5bd159bd 2022-05-11 op history.dirty = 0;
263 5bd159bd 2022-05-11 op history.extra = 0;
264 5bd159bd 2022-05-11 op }
265 5bd159bd 2022-05-11 op
266 5bd159bd 2022-05-11 op static void
267 5bd159bd 2022-05-11 op save_dirty_history(void)
268 5bd159bd 2022-05-11 op {
269 5bd159bd 2022-05-11 op FILE *fp;
270 5bd159bd 2022-05-11 op size_t i;
271 5bd159bd 2022-05-11 op
272 5bd159bd 2022-05-11 op if ((fp = fopen(history_file, "a")) == NULL)
273 5bd159bd 2022-05-11 op return;
274 5bd159bd 2022-05-11 op
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)
277 5bd159bd 2022-05-11 op continue;
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);
282 5bd159bd 2022-05-11 op }
283 5bd159bd 2022-05-11 op history.dirty = 0;
284 5bd159bd 2022-05-11 op
285 5bd159bd 2022-05-11 op fclose(fp);
286 5bd159bd 2022-05-11 op }
287 5bd159bd 2022-05-11 op
288 5bd159bd 2022-05-11 op void
289 5bd159bd 2022-05-11 op save_session(void)
290 5bd159bd 2022-05-11 op {
291 5bd159bd 2022-05-11 op if (safe_mode)
292 5bd159bd 2022-05-11 op return;
293 5bd159bd 2022-05-11 op
294 5bd159bd 2022-05-11 op save_tabs();
295 5bd159bd 2022-05-11 op
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();
300 9e97090d 2022-02-26 op }
301 9e97090d 2022-02-26 op
302 9e97090d 2022-02-26 op void
303 9e97090d 2022-02-26 op history_push(struct histitem *hi)
304 9e97090d 2022-02-26 op {
305 9e97090d 2022-02-26 op size_t i, oldest = 0;
306 9e97090d 2022-02-26 op char *uri;
307 9e97090d 2022-02-26 op
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)
310 9e97090d 2022-02-26 op oldest = i;
311 9e97090d 2022-02-26 op
312 9e97090d 2022-02-26 op /* remove duplicates */
313 9e97090d 2022-02-26 op if (!strcmp(history.items[i].uri, hi->uri))
314 9e97090d 2022-02-26 op return;
315 9e97090d 2022-02-26 op }
316 9e97090d 2022-02-26 op
317 9e97090d 2022-02-26 op if ((uri = strdup(hi->uri)) == NULL)
318 9e97090d 2022-02-26 op abort();
319 9e97090d 2022-02-26 op
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;
325 5bd159bd 2022-05-11 op
326 5bd159bd 2022-05-11 op /* Growed past the max value, signal to regen the file. */
327 5bd159bd 2022-05-11 op history.extra++;
328 9e97090d 2022-02-26 op return;
329 9e97090d 2022-02-26 op }
330 9e97090d 2022-02-26 op
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++;
334 9e97090d 2022-02-26 op }
335 9e97090d 2022-02-26 op
336 9e97090d 2022-02-26 op static int
337 9e97090d 2022-02-26 op history_cmp(const void *a, const void *b)
338 9e97090d 2022-02-26 op {
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);
341 9e97090d 2022-02-26 op }
342 9e97090d 2022-02-26 op
343 9e97090d 2022-02-26 op void
344 9e97090d 2022-02-26 op history_sort(void)
345 9e97090d 2022-02-26 op {
346 9e97090d 2022-02-26 op qsort(history.items, history.len, sizeof(history.items[0]),
347 9e97090d 2022-02-26 op history_cmp);
348 1fce2e75 2021-08-14 op }
349 1fce2e75 2021-08-14 op
350 1fce2e75 2021-08-14 op void
351 9e97090d 2022-02-26 op history_add(const char *uri)
352 9e97090d 2022-02-26 op {
353 9e97090d 2022-02-26 op size_t i, j, insert = 0, oldest = 0;
354 9e97090d 2022-02-26 op char *u;
355 9e97090d 2022-02-26 op int c;
356 9e97090d 2022-02-26 op
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)
359 9e97090d 2022-02-26 op oldest = i;
360 9e97090d 2022-02-26 op
361 9e97090d 2022-02-26 op if (insert != 0 && insert < i)
362 9e97090d 2022-02-26 op continue;
363 9e97090d 2022-02-26 op
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();
370 9e97090d 2022-02-26 op return;
371 9e97090d 2022-02-26 op }
372 9e97090d 2022-02-26 op
373 9e97090d 2022-02-26 op if (c > 0)
374 9e97090d 2022-02-26 op insert = i;
375 9e97090d 2022-02-26 op }
376 9e97090d 2022-02-26 op
377 9e97090d 2022-02-26 op if ((u = strdup(uri)) == NULL)
378 9e97090d 2022-02-26 op return;
379 9e97090d 2022-02-26 op
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();
389 9e97090d 2022-02-26 op return;
390 9e97090d 2022-02-26 op }
391 9e97090d 2022-02-26 op
392 9e97090d 2022-02-26 op /* otherwise just insert in the right spot */
393 9e97090d 2022-02-26 op
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]));
397 9e97090d 2022-02-26 op
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();
404 9e97090d 2022-02-26 op }
405 9e97090d 2022-02-26 op
406 9e97090d 2022-02-26 op void
407 1fce2e75 2021-08-14 op autosave_init(void)
408 1fce2e75 2021-08-14 op {
409 1fce2e75 2021-08-14 op evtimer_set(&autosaveev, autosave_timer, NULL);
410 1fce2e75 2021-08-14 op }
411 1fce2e75 2021-08-14 op
412 1fce2e75 2021-08-14 op void
413 1fce2e75 2021-08-14 op autosave_timer(int fd, short event, void *data)
414 1fce2e75 2021-08-14 op {
415 1fce2e75 2021-08-14 op save_session();
416 1fce2e75 2021-08-14 op }
417 1fce2e75 2021-08-14 op
418 1fce2e75 2021-08-14 op /*
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.)
421 1fce2e75 2021-08-14 op */
422 1fce2e75 2021-08-14 op void
423 1fce2e75 2021-08-14 op autosave_hook(void)
424 1fce2e75 2021-08-14 op {
425 1fce2e75 2021-08-14 op struct timeval tv;
426 1fce2e75 2021-08-14 op
427 1fce2e75 2021-08-14 op if (autosave <= 0)
428 1fce2e75 2021-08-14 op return;
429 1fce2e75 2021-08-14 op
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;
433 1fce2e75 2021-08-14 op
434 1fce2e75 2021-08-14 op evtimer_add(&autosaveev, &tv);
435 64f4f8e2 2022-04-24 op }
436 64f4f8e2 2022-04-24 op }
437 64f4f8e2 2022-04-24 op
438 64f4f8e2 2022-04-24 op static inline int
439 64f4f8e2 2022-04-24 op parse_khost_line(char *line, char *tmp[3])
440 64f4f8e2 2022-04-24 op {
441 64f4f8e2 2022-04-24 op char **ap;
442 64f4f8e2 2022-04-24 op
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')
446 64f4f8e2 2022-04-24 op ap++;
447 64f4f8e2 2022-04-24 op }
448 64f4f8e2 2022-04-24 op
449 64f4f8e2 2022-04-24 op return ap == &tmp[3] && *line == '\0';
450 64f4f8e2 2022-04-24 op }
451 64f4f8e2 2022-04-24 op
452 64f4f8e2 2022-04-24 op static void
453 64f4f8e2 2022-04-24 op load_certs(struct ohash *certs)
454 64f4f8e2 2022-04-24 op {
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;
459 64f4f8e2 2022-04-24 op FILE *f;
460 64f4f8e2 2022-04-24 op struct tofu_entry *e;
461 64f4f8e2 2022-04-24 op
462 64f4f8e2 2022-04-24 op if ((f = fopen(known_hosts_file, "r")) == NULL)
463 64f4f8e2 2022-04-24 op return;
464 64f4f8e2 2022-04-24 op
465 64f4f8e2 2022-04-24 op if ((e = calloc(1, sizeof(*e))) == NULL) {
466 64f4f8e2 2022-04-24 op fclose(f);
467 64f4f8e2 2022-04-24 op return;
468 1fce2e75 2021-08-14 op }
469 64f4f8e2 2022-04-24 op
470 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, f)) != -1) {
471 64f4f8e2 2022-04-24 op lineno++;
472 64f4f8e2 2022-04-24 op
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));
476 64f4f8e2 2022-04-24 op
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]);
482 64f4f8e2 2022-04-24 op
483 64f4f8e2 2022-04-24 op tofu_add(certs, e);
484 64f4f8e2 2022-04-24 op } else
485 64f4f8e2 2022-04-24 op warnx("%s:%zu invalid entry",
486 64f4f8e2 2022-04-24 op known_hosts_file, lineno);
487 64f4f8e2 2022-04-24 op }
488 64f4f8e2 2022-04-24 op
489 64f4f8e2 2022-04-24 op free(line);
490 64f4f8e2 2022-04-24 op fclose(f);
491 64f4f8e2 2022-04-24 op return;
492 1fce2e75 2021-08-14 op }
493 64f4f8e2 2022-04-24 op
494 64f4f8e2 2022-04-24 op
495 64f4f8e2 2022-04-24 op static void
496 64f4f8e2 2022-04-24 op load_hist(void)
497 64f4f8e2 2022-04-24 op {
498 64f4f8e2 2022-04-24 op FILE *hist;
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;
504 64f4f8e2 2022-04-24 op
505 64f4f8e2 2022-04-24 op if ((hist = fopen(history_file, "r")) == NULL)
506 64f4f8e2 2022-04-24 op return;
507 64f4f8e2 2022-04-24 op
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)
510 64f4f8e2 2022-04-24 op *nl = '\0';
511 64f4f8e2 2022-04-24 op if ((spc = strchr(line, ' ')) == NULL)
512 64f4f8e2 2022-04-24 op continue;
513 64f4f8e2 2022-04-24 op *spc = '\0';
514 64f4f8e2 2022-04-24 op spc++;
515 64f4f8e2 2022-04-24 op
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)
519 64f4f8e2 2022-04-24 op continue;
520 64f4f8e2 2022-04-24 op if (strlcpy(hi.uri, spc, sizeof(hi.uri)) >= sizeof(hi.uri))
521 64f4f8e2 2022-04-24 op continue;
522 64f4f8e2 2022-04-24 op
523 64f4f8e2 2022-04-24 op history_push(&hi);
524 64f4f8e2 2022-04-24 op }
525 64f4f8e2 2022-04-24 op
526 64f4f8e2 2022-04-24 op fclose(hist);
527 64f4f8e2 2022-04-24 op free(line);
528 64f4f8e2 2022-04-24 op
529 64f4f8e2 2022-04-24 op history_sort();
530 64f4f8e2 2022-04-24 op }
531 64f4f8e2 2022-04-24 op
532 64f4f8e2 2022-04-24 op /*
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
536 64f4f8e2 2022-04-24 op * teardown.
537 64f4f8e2 2022-04-24 op */
538 64f4f8e2 2022-04-24 op static int
539 64f4f8e2 2022-04-24 op last_time_crashed(void)
540 64f4f8e2 2022-04-24 op {
541 64f4f8e2 2022-04-24 op int fd, crashed = 1;
542 64f4f8e2 2022-04-24 op
543 64f4f8e2 2022-04-24 op if (safe_mode)
544 64f4f8e2 2022-04-24 op return 0;
545 64f4f8e2 2022-04-24 op
546 64f4f8e2 2022-04-24 op if (unlink(crashed_file) == -1 && errno == ENOENT)
547 64f4f8e2 2022-04-24 op crashed = 0;
548 64f4f8e2 2022-04-24 op
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;
551 64f4f8e2 2022-04-24 op close(fd);
552 64f4f8e2 2022-04-24 op
553 64f4f8e2 2022-04-24 op return crashed;
554 64f4f8e2 2022-04-24 op }
555 64f4f8e2 2022-04-24 op
556 64f4f8e2 2022-04-24 op /*
557 64f4f8e2 2022-04-24 op * Parse and restore a tab from the session file. The format is:
558 64f4f8e2 2022-04-24 op *
559 64f4f8e2 2022-04-24 op * URL [flags,...] [title]\n
560 64f4f8e2 2022-04-24 op */
561 64f4f8e2 2022-04-24 op static inline struct tab *
562 64f4f8e2 2022-04-24 op parse_tab_line(char *line, struct tab **ct)
563 64f4f8e2 2022-04-24 op {
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;
569 64f4f8e2 2022-04-24 op
570 64f4f8e2 2022-04-24 op uri = line;
571 64f4f8e2 2022-04-24 op if ((s = strchr(line, ' ')) == NULL)
572 64f4f8e2 2022-04-24 op return NULL;
573 64f4f8e2 2022-04-24 op *s++ = '\0';
574 64f4f8e2 2022-04-24 op
575 64f4f8e2 2022-04-24 op if ((t = strchr(s, ' ')) != NULL) {
576 64f4f8e2 2022-04-24 op *t++ = '\0';
577 64f4f8e2 2022-04-24 op title = t;
578 64f4f8e2 2022-04-24 op }
579 64f4f8e2 2022-04-24 op
580 64f4f8e2 2022-04-24 op while ((ap = strsep(&s, ",")) != NULL) {
581 64f4f8e2 2022-04-24 op if (!strcmp(ap, "current"))
582 64f4f8e2 2022-04-24 op current = 1;
583 64f4f8e2 2022-04-24 op else if (!strcmp(ap, "killed"))
584 64f4f8e2 2022-04-24 op killed = 1;
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);
589 64f4f8e2 2022-04-24 op }
590 64f4f8e2 2022-04-24 op
591 64f4f8e2 2022-04-24 op if (tline > cline) {
592 64f4f8e2 2022-04-24 op tline = 0;
593 64f4f8e2 2022-04-24 op cline = 0;
594 64f4f8e2 2022-04-24 op }
595 64f4f8e2 2022-04-24 op
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));
600 64f4f8e2 2022-04-24 op
601 64f4f8e2 2022-04-24 op if (current)
602 64f4f8e2 2022-04-24 op *ct = tab;
603 64f4f8e2 2022-04-24 op else if (killed)
604 64f4f8e2 2022-04-24 op kill_tab(tab, 1);
605 64f4f8e2 2022-04-24 op
606 64f4f8e2 2022-04-24 op return tab;
607 64f4f8e2 2022-04-24 op }
608 64f4f8e2 2022-04-24 op
609 64f4f8e2 2022-04-24 op static void
610 64f4f8e2 2022-04-24 op load_tabs(void)
611 64f4f8e2 2022-04-24 op {
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;
617 64f4f8e2 2022-04-24 op
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);
621 64f4f8e2 2022-04-24 op return;
622 64f4f8e2 2022-04-24 op }
623 64f4f8e2 2022-04-24 op
624 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, session)) != -1) {
625 64f4f8e2 2022-04-24 op lineno++;
626 64f4f8e2 2022-04-24 op
627 64f4f8e2 2022-04-24 op if (linelen > 0 && line[linelen-1] == '\n')
628 64f4f8e2 2022-04-24 op line[linelen-1] = '\0';
629 64f4f8e2 2022-04-24 op
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);
635 64f4f8e2 2022-04-24 op continue;
636 64f4f8e2 2022-04-24 op }
637 64f4f8e2 2022-04-24 op uri++;
638 64f4f8e2 2022-04-24 op
639 64f4f8e2 2022-04-24 op if (*line == '>') /* future hist */
640 65c49665 2024-01-23 op hist_append(tab->hist, uri);
641 64f4f8e2 2022-04-24 op else
642 65c49665 2024-01-23 op hist_prepend(tab->hist, uri);
643 64f4f8e2 2022-04-24 op } else
644 64f4f8e2 2022-04-24 op tab = parse_tab_line(line, &ct);
645 64f4f8e2 2022-04-24 op }
646 64f4f8e2 2022-04-24 op
647 64f4f8e2 2022-04-24 op fclose(session);
648 64f4f8e2 2022-04-24 op free(line);
649 64f4f8e2 2022-04-24 op
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);
652 64f4f8e2 2022-04-24 op
653 64f4f8e2 2022-04-24 op switch_to_tab(ct);
654 64f4f8e2 2022-04-24 op
655 64f4f8e2 2022-04-24 op if (last_time_crashed())
656 64f4f8e2 2022-04-24 op new_tab("about:crash", NULL, NULL);
657 64f4f8e2 2022-04-24 op }
658 64f4f8e2 2022-04-24 op
659 64f4f8e2 2022-04-24 op int
660 64f4f8e2 2022-04-24 op load_session(struct ohash *certs)
661 64f4f8e2 2022-04-24 op {
662 64f4f8e2 2022-04-24 op load_certs(certs);
663 64f4f8e2 2022-04-24 op load_hist();
664 64f4f8e2 2022-04-24 op load_tabs();
665 64f4f8e2 2022-04-24 op return 0;
666 64f4f8e2 2022-04-24 op }
667 64f4f8e2 2022-04-24 op
668 64f4f8e2 2022-04-24 op int
669 64f4f8e2 2022-04-24 op lock_session(void)
670 64f4f8e2 2022-04-24 op {
671 64f4f8e2 2022-04-24 op struct flock lock;
672 64f4f8e2 2022-04-24 op int fd;
673 64f4f8e2 2022-04-24 op
674 64f4f8e2 2022-04-24 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
675 64f4f8e2 2022-04-24 op return -1;
676 64f4f8e2 2022-04-24 op
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;
681 64f4f8e2 2022-04-24 op
682 64f4f8e2 2022-04-24 op if (fcntl(fd, F_SETLK, &lock) == -1) {
683 64f4f8e2 2022-04-24 op close(fd);
684 64f4f8e2 2022-04-24 op return -1;
685 64f4f8e2 2022-04-24 op }
686 64f4f8e2 2022-04-24 op
687 64f4f8e2 2022-04-24 op return fd;
688 64f4f8e2 2022-04-24 op }