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 98d3e6c1 2024-02-18 op #include <sys/time.h>
20 98d3e6c1 2024-02-18 op
21 1fce2e75 2021-08-14 op #include <errno.h>
22 64f4f8e2 2022-04-24 op #include <fcntl.h>
23 f63b8f73 2022-04-24 op #include <limits.h>
24 1fce2e75 2021-08-14 op #include <stdio.h>
25 1fce2e75 2021-08-14 op #include <stdlib.h>
26 1fce2e75 2021-08-14 op #include <string.h>
27 9e97090d 2022-02-26 op #include <time.h>
28 1fce2e75 2021-08-14 op #include <unistd.h>
29 1fce2e75 2021-08-14 op
30 1fce2e75 2021-08-14 op #include "defaults.h"
31 98d3e6c1 2024-02-18 op #include "ev.h"
32 b9fcc0e9 2021-10-08 op #include "fs.h"
33 65c49665 2024-01-23 op #include "hist.h"
34 87d297d1 2024-02-22 op #include "imsgev.h"
35 1fce2e75 2021-08-14 op #include "minibuffer.h"
36 1fce2e75 2021-08-14 op #include "session.h"
37 d163c210 2024-02-06 op #include "tofu.h"
38 1fce2e75 2021-08-14 op #include "ui.h"
39 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
40 1fce2e75 2021-08-14 op
41 9e97090d 2022-02-26 op struct history history;
42 9e97090d 2022-02-26 op
43 98d3e6c1 2024-02-18 op static unsigned int autosavetimer;
44 1fce2e75 2021-08-14 op
45 1fce2e75 2021-08-14 op void
46 1fce2e75 2021-08-14 op switch_to_tab(struct tab *tab)
47 1fce2e75 2021-08-14 op {
48 1fce2e75 2021-08-14 op current_tab = tab;
49 1fce2e75 2021-08-14 op tab->flags &= ~TAB_URGENT;
50 1fce2e75 2021-08-14 op
51 fb8dcd1c 2022-01-18 op if (operating && tab->flags & TAB_LAZY)
52 65c49665 2024-01-23 op load_url_in_tab(tab, hist_cur(tab->hist), NULL,
53 65c49665 2024-01-23 op LU_MODE_NOHIST);
54 1fce2e75 2021-08-14 op }
55 1fce2e75 2021-08-14 op
56 1fce2e75 2021-08-14 op unsigned int
57 1fce2e75 2021-08-14 op tab_new_id(void)
58 1fce2e75 2021-08-14 op {
59 1fce2e75 2021-08-14 op static uint32_t tab_counter;
60 1fce2e75 2021-08-14 op
61 1fce2e75 2021-08-14 op return tab_counter++;
62 1fce2e75 2021-08-14 op }
63 1fce2e75 2021-08-14 op
64 1fce2e75 2021-08-14 op struct tab *
65 1fce2e75 2021-08-14 op new_tab(const char *url, const char *base, struct tab *after)
66 1fce2e75 2021-08-14 op {
67 1fce2e75 2021-08-14 op struct tab *tab;
68 1fce2e75 2021-08-14 op
69 87aeb47b 2021-08-18 op ui_schedule_redraw();
70 1fce2e75 2021-08-14 op autosave_hook();
71 1fce2e75 2021-08-14 op
72 3d89457c 2024-06-18 thomas.ad tab = xcalloc(1, sizeof(*tab));
73 1fce2e75 2021-08-14 op
74 65c49665 2024-01-23 op if ((tab->hist = hist_new(HIST_LINEAR)) == NULL) {
75 65c49665 2024-01-23 op free(tab);
76 98d3e6c1 2024-02-18 op ev_break();
77 65c49665 2024-01-23 op return NULL;
78 65c49665 2024-01-23 op }
79 65c49665 2024-01-23 op
80 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.head);
81 c1d27b0e 2024-06-14 op TAILQ_INIT(&tab->buffer.vhead);
82 1fce2e75 2021-08-14 op
83 1fce2e75 2021-08-14 op tab->id = tab_new_id();
84 1fce2e75 2021-08-14 op
85 1fce2e75 2021-08-14 op if (after != NULL)
86 1fce2e75 2021-08-14 op TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
87 1fce2e75 2021-08-14 op else
88 1fce2e75 2021-08-14 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
89 1fce2e75 2021-08-14 op
90 ea639250 2022-01-02 op if (!operating)
91 ea639250 2022-01-02 op tab->flags |= TAB_LAZY;
92 1fce2e75 2021-08-14 op load_url_in_tab(tab, url, base, 0);
93 ea639250 2022-01-02 op switch_to_tab(tab);
94 1fce2e75 2021-08-14 op return tab;
95 1fce2e75 2021-08-14 op }
96 1fce2e75 2021-08-14 op
97 1fce2e75 2021-08-14 op /*
98 6c74799d 2022-01-05 op * Move a tab from the tablist to the killed tab list and erase its
99 05de8ac3 2022-01-06 op * contents. Append should always be 0 to prepend tabs so unkill_tab
100 05de8ac3 2022-01-06 op * can work correctly; appending is only useful during startup when
101 05de8ac3 2022-01-06 op * receiving the list of killed tabs to keep the correct order.
102 05de8ac3 2022-01-06 op * NB: doesn't update the current_tab.
103 1fce2e75 2021-08-14 op */
104 1fce2e75 2021-08-14 op void
105 05de8ac3 2022-01-06 op kill_tab(struct tab *tab, int append)
106 1fce2e75 2021-08-14 op {
107 6c74799d 2022-01-05 op int count;
108 6c74799d 2022-01-05 op
109 1fce2e75 2021-08-14 op stop_tab(tab);
110 6c74799d 2022-01-05 op erase_buffer(&tab->buffer);
111 6c74799d 2022-01-05 op TAILQ_REMOVE(&tabshead, tab, tabs);
112 87aeb47b 2021-08-18 op ui_schedule_redraw();
113 1fce2e75 2021-08-14 op autosave_hook();
114 1fce2e75 2021-08-14 op
115 98d3e6c1 2024-02-18 op ev_timer_cancel(tab->loading_timer);
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 c1d27b0e 2024-06-14 op tab->buffer.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 2022-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 3d89457c 2024-06-18 thomas.ad uri = xstrdup(hi->uri);
318 9e97090d 2022-02-26 op
319 9e97090d 2022-02-26 op /* don't grow too much; replace the oldest */
320 9e97090d 2022-02-26 op if (history.len == HISTORY_CAP) {
321 9e97090d 2022-02-26 op history.items[oldest].ts = hi->ts;
322 9e97090d 2022-02-26 op free(history.items[oldest].uri);
323 9e97090d 2022-02-26 op history.items[oldest].uri = uri;
324 5bd159bd 2022-05-11 op
325 5bd159bd 2022-05-11 op /* Growed past the max value, signal to regen the file. */
326 5bd159bd 2022-05-11 op history.extra++;
327 9e97090d 2022-02-26 op return;
328 9e97090d 2022-02-26 op }
329 9e97090d 2022-02-26 op
330 9e97090d 2022-02-26 op history.items[history.len].ts = hi->ts;
331 9e97090d 2022-02-26 op history.items[history.len].uri = uri;
332 9e97090d 2022-02-26 op history.len++;
333 9e97090d 2022-02-26 op }
334 9e97090d 2022-02-26 op
335 9e97090d 2022-02-26 op static int
336 9e97090d 2022-02-26 op history_cmp(const void *a, const void *b)
337 9e97090d 2022-02-26 op {
338 9e97090d 2022-02-26 op const struct history_item *i = a, *j = b;
339 9e97090d 2022-02-26 op return strcmp(i->uri, j->uri);
340 9e97090d 2022-02-26 op }
341 9e97090d 2022-02-26 op
342 9e97090d 2022-02-26 op void
343 9e97090d 2022-02-26 op history_sort(void)
344 9e97090d 2022-02-26 op {
345 9e97090d 2022-02-26 op qsort(history.items, history.len, sizeof(history.items[0]),
346 9e97090d 2022-02-26 op history_cmp);
347 1fce2e75 2021-08-14 op }
348 1fce2e75 2021-08-14 op
349 1fce2e75 2021-08-14 op void
350 9e97090d 2022-02-26 op history_add(const char *uri)
351 9e97090d 2022-02-26 op {
352 9e97090d 2022-02-26 op size_t i, j, insert = 0, oldest = 0;
353 9e97090d 2022-02-26 op char *u;
354 9e97090d 2022-02-26 op int c;
355 9e97090d 2022-02-26 op
356 9e97090d 2022-02-26 op for (i = 0; i < history.len; ++i) {
357 9e97090d 2022-02-26 op if (history.items[i].ts < history.items[oldest].ts)
358 9e97090d 2022-02-26 op oldest = i;
359 9e97090d 2022-02-26 op
360 9e97090d 2022-02-26 op if (insert != 0 && insert < i)
361 9e97090d 2022-02-26 op continue;
362 9e97090d 2022-02-26 op
363 9e97090d 2022-02-26 op c = strcmp(uri, history.items[i].uri);
364 9e97090d 2022-02-26 op if (c == 0) {
365 9e97090d 2022-02-26 op history.items[i].ts = time(NULL);
366 9e97090d 2022-02-26 op history.items[i].dirty = 1;
367 9e97090d 2022-02-26 op history.dirty++;
368 9e97090d 2022-02-26 op autosave_hook();
369 9e97090d 2022-02-26 op return;
370 9e97090d 2022-02-26 op }
371 9e97090d 2022-02-26 op
372 9e97090d 2022-02-26 op if (c > 0)
373 9e97090d 2022-02-26 op insert = i;
374 9e97090d 2022-02-26 op }
375 9e97090d 2022-02-26 op
376 3d89457c 2024-06-18 thomas.ad u = xstrdup(uri);
377 9e97090d 2022-02-26 op
378 9e97090d 2022-02-26 op /* if history is full, replace the oldest one */
379 9e97090d 2022-02-26 op if (history.len == HISTORY_CAP) {
380 9e97090d 2022-02-26 op free(history.items[oldest].uri);
381 9e97090d 2022-02-26 op history.items[oldest].uri = u;
382 9e97090d 2022-02-26 op history.items[oldest].ts = time(NULL);
383 9e97090d 2022-02-26 op history.items[oldest].dirty = 1;
384 9e97090d 2022-02-26 op history.dirty++;
385 9e97090d 2022-02-26 op history_sort();
386 9e97090d 2022-02-26 op autosave_hook();
387 9e97090d 2022-02-26 op return;
388 9e97090d 2022-02-26 op }
389 9e97090d 2022-02-26 op
390 9e97090d 2022-02-26 op /* otherwise just insert in the right spot */
391 9e97090d 2022-02-26 op
392 9e97090d 2022-02-26 op for (j = history.len; j > insert; --j)
393 9e97090d 2022-02-26 op memcpy(&history.items[j], &history.items[j-1],
394 9e97090d 2022-02-26 op sizeof(history.items[j]));
395 9e97090d 2022-02-26 op
396 9e97090d 2022-02-26 op history.items[insert].ts = time(NULL);
397 9e97090d 2022-02-26 op history.items[insert].uri = u;
398 9e97090d 2022-02-26 op history.items[insert].dirty = 1;
399 9e97090d 2022-02-26 op history.dirty++;
400 9e97090d 2022-02-26 op history.len++;
401 9e97090d 2022-02-26 op autosave_hook();
402 9e97090d 2022-02-26 op }
403 9e97090d 2022-02-26 op
404 9e97090d 2022-02-26 op void
405 1fce2e75 2021-08-14 op autosave_init(void)
406 1fce2e75 2021-08-14 op {
407 98d3e6c1 2024-02-18 op return;
408 1fce2e75 2021-08-14 op }
409 1fce2e75 2021-08-14 op
410 1fce2e75 2021-08-14 op void
411 98d3e6c1 2024-02-18 op autosave_timer(int fd, int event, void *data)
412 1fce2e75 2021-08-14 op {
413 1fce2e75 2021-08-14 op save_session();
414 1fce2e75 2021-08-14 op }
415 1fce2e75 2021-08-14 op
416 1fce2e75 2021-08-14 op /*
417 1fce2e75 2021-08-14 op * Function to be called in "interesting" places where we may want to
418 1fce2e75 2021-08-14 op * schedule an autosave (like on new tab or before loading an url.)
419 1fce2e75 2021-08-14 op */
420 1fce2e75 2021-08-14 op void
421 1fce2e75 2021-08-14 op autosave_hook(void)
422 1fce2e75 2021-08-14 op {
423 1fce2e75 2021-08-14 op struct timeval tv;
424 1fce2e75 2021-08-14 op
425 1fce2e75 2021-08-14 op if (autosave <= 0)
426 1fce2e75 2021-08-14 op return;
427 1fce2e75 2021-08-14 op
428 98d3e6c1 2024-02-18 op if (!ev_timer_pending(autosavetimer)) {
429 1fce2e75 2021-08-14 op tv.tv_sec = autosave;
430 1fce2e75 2021-08-14 op tv.tv_usec = 0;
431 1fce2e75 2021-08-14 op
432 98d3e6c1 2024-02-18 op autosavetimer = ev_timer(&tv, autosave_timer, NULL);
433 64f4f8e2 2022-04-24 op }
434 64f4f8e2 2022-04-24 op }
435 64f4f8e2 2022-04-24 op
436 64f4f8e2 2022-04-24 op static inline int
437 64f4f8e2 2022-04-24 op parse_khost_line(char *line, char *tmp[3])
438 64f4f8e2 2022-04-24 op {
439 64f4f8e2 2022-04-24 op char **ap;
440 64f4f8e2 2022-04-24 op
441 64f4f8e2 2022-04-24 op for (ap = tmp; ap < &tmp[3] &&
442 64f4f8e2 2022-04-24 op (*ap = strsep(&line, " \t\n")) != NULL;) {
443 64f4f8e2 2022-04-24 op if (**ap != '\0')
444 64f4f8e2 2022-04-24 op ap++;
445 64f4f8e2 2022-04-24 op }
446 64f4f8e2 2022-04-24 op
447 64f4f8e2 2022-04-24 op return ap == &tmp[3] && *line == '\0';
448 64f4f8e2 2022-04-24 op }
449 64f4f8e2 2022-04-24 op
450 64f4f8e2 2022-04-24 op static void
451 64f4f8e2 2022-04-24 op load_certs(struct ohash *certs)
452 64f4f8e2 2022-04-24 op {
453 64f4f8e2 2022-04-24 op char *tmp[3], *line = NULL;
454 64f4f8e2 2022-04-24 op const char *errstr;
455 64f4f8e2 2022-04-24 op size_t lineno = 0, linesize = 0;
456 64f4f8e2 2022-04-24 op ssize_t linelen;
457 64f4f8e2 2022-04-24 op FILE *f;
458 64f4f8e2 2022-04-24 op struct tofu_entry *e;
459 64f4f8e2 2022-04-24 op
460 64f4f8e2 2022-04-24 op if ((f = fopen(known_hosts_file, "r")) == NULL)
461 64f4f8e2 2022-04-24 op return;
462 64f4f8e2 2022-04-24 op
463 3d89457c 2024-06-18 thomas.ad e = xcalloc(1, sizeof(*e));
464 64f4f8e2 2022-04-24 op
465 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, f)) != -1) {
466 64f4f8e2 2022-04-24 op lineno++;
467 64f4f8e2 2022-04-24 op
468 64f4f8e2 2022-04-24 op if (parse_khost_line(line, tmp)) {
469 64f4f8e2 2022-04-24 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
470 64f4f8e2 2022-04-24 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
471 64f4f8e2 2022-04-24 op
472 64f4f8e2 2022-04-24 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
473 64f4f8e2 2022-04-24 op if (errstr != NULL)
474 64f4f8e2 2022-04-24 op errx(1, "%s:%zu verification for %s is %s: %s",
475 64f4f8e2 2022-04-24 op known_hosts_file, lineno,
476 64f4f8e2 2022-04-24 op e->domain, errstr, tmp[2]);
477 64f4f8e2 2022-04-24 op
478 64f4f8e2 2022-04-24 op tofu_add(certs, e);
479 64f4f8e2 2022-04-24 op } else
480 64f4f8e2 2022-04-24 op warnx("%s:%zu invalid entry",
481 64f4f8e2 2022-04-24 op known_hosts_file, lineno);
482 64f4f8e2 2022-04-24 op }
483 64f4f8e2 2022-04-24 op
484 64f4f8e2 2022-04-24 op free(line);
485 64f4f8e2 2022-04-24 op fclose(f);
486 64f4f8e2 2022-04-24 op return;
487 1fce2e75 2021-08-14 op }
488 64f4f8e2 2022-04-24 op
489 64f4f8e2 2022-04-24 op
490 64f4f8e2 2022-04-24 op static void
491 64f4f8e2 2022-04-24 op load_hist(void)
492 64f4f8e2 2022-04-24 op {
493 64f4f8e2 2022-04-24 op FILE *hist;
494 64f4f8e2 2022-04-24 op size_t linesize = 0;
495 64f4f8e2 2022-04-24 op ssize_t linelen;
496 64f4f8e2 2022-04-24 op char *nl, *spc, *line = NULL;
497 64f4f8e2 2022-04-24 op const char *errstr;
498 64f4f8e2 2022-04-24 op struct histitem hi;
499 64f4f8e2 2022-04-24 op
500 64f4f8e2 2022-04-24 op if ((hist = fopen(history_file, "r")) == NULL)
501 64f4f8e2 2022-04-24 op return;
502 64f4f8e2 2022-04-24 op
503 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, hist)) != -1) {
504 64f4f8e2 2022-04-24 op if ((nl = strchr(line, '\n')) != NULL)
505 64f4f8e2 2022-04-24 op *nl = '\0';
506 64f4f8e2 2022-04-24 op if ((spc = strchr(line, ' ')) == NULL)
507 64f4f8e2 2022-04-24 op continue;
508 64f4f8e2 2022-04-24 op *spc = '\0';
509 64f4f8e2 2022-04-24 op spc++;
510 64f4f8e2 2022-04-24 op
511 64f4f8e2 2022-04-24 op memset(&hi, 0, sizeof(hi));
512 64f4f8e2 2022-04-24 op hi.ts = strtonum(line, INT64_MIN, INT64_MAX, &errstr);
513 64f4f8e2 2022-04-24 op if (errstr != NULL)
514 64f4f8e2 2022-04-24 op continue;
515 64f4f8e2 2022-04-24 op if (strlcpy(hi.uri, spc, sizeof(hi.uri)) >= sizeof(hi.uri))
516 64f4f8e2 2022-04-24 op continue;
517 64f4f8e2 2022-04-24 op
518 64f4f8e2 2022-04-24 op history_push(&hi);
519 64f4f8e2 2022-04-24 op }
520 64f4f8e2 2022-04-24 op
521 64f4f8e2 2022-04-24 op fclose(hist);
522 64f4f8e2 2022-04-24 op free(line);
523 64f4f8e2 2022-04-24 op
524 64f4f8e2 2022-04-24 op history_sort();
525 64f4f8e2 2022-04-24 op }
526 64f4f8e2 2022-04-24 op
527 64f4f8e2 2022-04-24 op /*
528 64f4f8e2 2022-04-24 op * Check if the last time telescope crashed. The check is done by
529 64f4f8e2 2022-04-24 op * looking at `crashed_file': if it exists then last time we crashed.
530 64f4f8e2 2022-04-24 op * Then, while here, touch the file too, it's removed during the
531 64f4f8e2 2022-04-24 op * teardown.
532 64f4f8e2 2022-04-24 op */
533 64f4f8e2 2022-04-24 op static int
534 64f4f8e2 2022-04-24 op last_time_crashed(void)
535 64f4f8e2 2022-04-24 op {
536 64f4f8e2 2022-04-24 op int fd, crashed = 1;
537 64f4f8e2 2022-04-24 op
538 64f4f8e2 2022-04-24 op if (safe_mode)
539 64f4f8e2 2022-04-24 op return 0;
540 64f4f8e2 2022-04-24 op
541 64f4f8e2 2022-04-24 op if (unlink(crashed_file) == -1 && errno == ENOENT)
542 64f4f8e2 2022-04-24 op crashed = 0;
543 64f4f8e2 2022-04-24 op
544 64f4f8e2 2022-04-24 op if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
545 64f4f8e2 2022-04-24 op return crashed;
546 64f4f8e2 2022-04-24 op close(fd);
547 64f4f8e2 2022-04-24 op
548 64f4f8e2 2022-04-24 op return crashed;
549 64f4f8e2 2022-04-24 op }
550 64f4f8e2 2022-04-24 op
551 64f4f8e2 2022-04-24 op /*
552 64f4f8e2 2022-04-24 op * Parse and restore a tab from the session file. The format is:
553 64f4f8e2 2022-04-24 op *
554 64f4f8e2 2022-04-24 op * URL [flags,...] [title]\n
555 64f4f8e2 2022-04-24 op */
556 64f4f8e2 2022-04-24 op static inline struct tab *
557 64f4f8e2 2022-04-24 op parse_tab_line(char *line, struct tab **ct)
558 64f4f8e2 2022-04-24 op {
559 64f4f8e2 2022-04-24 op struct tab *tab;
560 64f4f8e2 2022-04-24 op char *s, *t, *ap;
561 64f4f8e2 2022-04-24 op const char *uri, *title = "";
562 64f4f8e2 2022-04-24 op int current = 0, killed = 0;
563 64f4f8e2 2022-04-24 op size_t tline = 0, cline = 0;
564 64f4f8e2 2022-04-24 op
565 64f4f8e2 2022-04-24 op uri = line;
566 64f4f8e2 2022-04-24 op if ((s = strchr(line, ' ')) == NULL)
567 64f4f8e2 2022-04-24 op return NULL;
568 64f4f8e2 2022-04-24 op *s++ = '\0';
569 64f4f8e2 2022-04-24 op
570 64f4f8e2 2022-04-24 op if ((t = strchr(s, ' ')) != NULL) {
571 64f4f8e2 2022-04-24 op *t++ = '\0';
572 64f4f8e2 2022-04-24 op title = t;
573 64f4f8e2 2022-04-24 op }
574 64f4f8e2 2022-04-24 op
575 64f4f8e2 2022-04-24 op while ((ap = strsep(&s, ",")) != NULL) {
576 64f4f8e2 2022-04-24 op if (!strcmp(ap, "current"))
577 64f4f8e2 2022-04-24 op current = 1;
578 64f4f8e2 2022-04-24 op else if (!strcmp(ap, "killed"))
579 64f4f8e2 2022-04-24 op killed = 1;
580 64f4f8e2 2022-04-24 op else if (!strncmp(ap, "top=", 4))
581 64f4f8e2 2022-04-24 op tline = strtonum(ap+4, 0, UINT32_MAX, NULL);
582 64f4f8e2 2022-04-24 op else if (!strncmp(ap, "cur=", 4))
583 64f4f8e2 2022-04-24 op cline = strtonum(ap + 4, 0, UINT32_MAX, NULL);
584 64f4f8e2 2022-04-24 op }
585 64f4f8e2 2022-04-24 op
586 64f4f8e2 2022-04-24 op if (tline > cline) {
587 64f4f8e2 2022-04-24 op tline = 0;
588 64f4f8e2 2022-04-24 op cline = 0;
589 64f4f8e2 2022-04-24 op }
590 64f4f8e2 2022-04-24 op
591 64f4f8e2 2022-04-24 op if ((tab = new_tab(uri, NULL, NULL)) == NULL)
592 64f4f8e2 2022-04-24 op err(1, "new_tab");
593 65c49665 2024-01-23 op hist_set_offs(tab->hist, tline, cline);
594 c1d27b0e 2024-06-14 op strlcpy(tab->buffer.title, title, sizeof(tab->buffer.title));
595 64f4f8e2 2022-04-24 op
596 64f4f8e2 2022-04-24 op if (current)
597 64f4f8e2 2022-04-24 op *ct = tab;
598 64f4f8e2 2022-04-24 op else if (killed)
599 64f4f8e2 2022-04-24 op kill_tab(tab, 1);
600 64f4f8e2 2022-04-24 op
601 64f4f8e2 2022-04-24 op return tab;
602 64f4f8e2 2022-04-24 op }
603 64f4f8e2 2022-04-24 op
604 64f4f8e2 2022-04-24 op static void
605 64f4f8e2 2022-04-24 op load_tabs(void)
606 64f4f8e2 2022-04-24 op {
607 64f4f8e2 2022-04-24 op struct tab *tab = NULL, *ct = NULL;
608 64f4f8e2 2022-04-24 op FILE *session;
609 64f4f8e2 2022-04-24 op size_t lineno = 0, linesize = 0;
610 64f4f8e2 2022-04-24 op ssize_t linelen;
611 64f4f8e2 2022-04-24 op char *uri, *line = NULL;
612 64f4f8e2 2022-04-24 op
613 64f4f8e2 2022-04-24 op if ((session = fopen(session_file, "r")) == NULL) {
614 64f4f8e2 2022-04-24 op new_tab("about:new", NULL, NULL);
615 64f4f8e2 2022-04-24 op new_tab("about:help", NULL, NULL);
616 64f4f8e2 2022-04-24 op return;
617 64f4f8e2 2022-04-24 op }
618 64f4f8e2 2022-04-24 op
619 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, session)) != -1) {
620 64f4f8e2 2022-04-24 op lineno++;
621 64f4f8e2 2022-04-24 op
622 64f4f8e2 2022-04-24 op if (linelen > 0 && line[linelen-1] == '\n')
623 64f4f8e2 2022-04-24 op line[linelen-1] = '\0';
624 64f4f8e2 2022-04-24 op
625 64f4f8e2 2022-04-24 op if (*line == '<' || *line == '>') {
626 64f4f8e2 2022-04-24 op uri = line + 1;
627 64f4f8e2 2022-04-24 op if (*uri != ' ' || tab == NULL) {
628 64f4f8e2 2022-04-24 op fprintf(stderr, "%s:%zu invalid line\n",
629 64f4f8e2 2022-04-24 op session_file, lineno);
630 64f4f8e2 2022-04-24 op continue;
631 64f4f8e2 2022-04-24 op }
632 64f4f8e2 2022-04-24 op uri++;
633 64f4f8e2 2022-04-24 op
634 64f4f8e2 2022-04-24 op if (*line == '>') /* future hist */
635 65c49665 2024-01-23 op hist_append(tab->hist, uri);
636 64f4f8e2 2022-04-24 op else
637 65c49665 2024-01-23 op hist_prepend(tab->hist, uri);
638 64f4f8e2 2022-04-24 op } else
639 64f4f8e2 2022-04-24 op tab = parse_tab_line(line, &ct);
640 64f4f8e2 2022-04-24 op }
641 64f4f8e2 2022-04-24 op
642 64f4f8e2 2022-04-24 op fclose(session);
643 64f4f8e2 2022-04-24 op free(line);
644 64f4f8e2 2022-04-24 op
645 64f4f8e2 2022-04-24 op if (ct == NULL || TAILQ_EMPTY(&tabshead))
646 64f4f8e2 2022-04-24 op ct = new_tab("about:new", NULL, NULL);
647 64f4f8e2 2022-04-24 op
648 64f4f8e2 2022-04-24 op switch_to_tab(ct);
649 64f4f8e2 2022-04-24 op
650 64f4f8e2 2022-04-24 op if (last_time_crashed())
651 64f4f8e2 2022-04-24 op new_tab("about:crash", NULL, NULL);
652 64f4f8e2 2022-04-24 op }
653 64f4f8e2 2022-04-24 op
654 64f4f8e2 2022-04-24 op int
655 64f4f8e2 2022-04-24 op load_session(struct ohash *certs)
656 64f4f8e2 2022-04-24 op {
657 64f4f8e2 2022-04-24 op load_certs(certs);
658 64f4f8e2 2022-04-24 op load_hist();
659 64f4f8e2 2022-04-24 op load_tabs();
660 64f4f8e2 2022-04-24 op return 0;
661 64f4f8e2 2022-04-24 op }
662 64f4f8e2 2022-04-24 op
663 64f4f8e2 2022-04-24 op int
664 64f4f8e2 2022-04-24 op lock_session(void)
665 64f4f8e2 2022-04-24 op {
666 64f4f8e2 2022-04-24 op struct flock lock;
667 64f4f8e2 2022-04-24 op int fd;
668 64f4f8e2 2022-04-24 op
669 64f4f8e2 2022-04-24 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
670 64f4f8e2 2022-04-24 op return -1;
671 64f4f8e2 2022-04-24 op
672 64f4f8e2 2022-04-24 op lock.l_start = 0;
673 64f4f8e2 2022-04-24 op lock.l_len = 0;
674 64f4f8e2 2022-04-24 op lock.l_type = F_WRLCK;
675 64f4f8e2 2022-04-24 op lock.l_whence = SEEK_SET;
676 64f4f8e2 2022-04-24 op
677 64f4f8e2 2022-04-24 op if (fcntl(fd, F_SETLK, &lock) == -1) {
678 64f4f8e2 2022-04-24 op close(fd);
679 64f4f8e2 2022-04-24 op return -1;
680 64f4f8e2 2022-04-24 op }
681 64f4f8e2 2022-04-24 op
682 64f4f8e2 2022-04-24 op return fd;
683 64f4f8e2 2022-04-24 op }