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 1fce2e75 2021-08-14 op
40 9e97090d 2022-02-26 op struct history history;
41 9e97090d 2022-02-26 op
42 98d3e6c1 2024-02-18 op static unsigned int autosavetimer;
43 1fce2e75 2021-08-14 op
44 1fce2e75 2021-08-14 op void
45 1fce2e75 2021-08-14 op switch_to_tab(struct tab *tab)
46 1fce2e75 2021-08-14 op {
47 1fce2e75 2021-08-14 op current_tab = tab;
48 1fce2e75 2021-08-14 op tab->flags &= ~TAB_URGENT;
49 1fce2e75 2021-08-14 op
50 fb8dcd1c 2022-01-18 op if (operating && tab->flags & TAB_LAZY)
51 65c49665 2024-01-23 op load_url_in_tab(tab, hist_cur(tab->hist), NULL,
52 65c49665 2024-01-23 op LU_MODE_NOHIST);
53 1fce2e75 2021-08-14 op }
54 1fce2e75 2021-08-14 op
55 1fce2e75 2021-08-14 op unsigned int
56 1fce2e75 2021-08-14 op tab_new_id(void)
57 1fce2e75 2021-08-14 op {
58 1fce2e75 2021-08-14 op static uint32_t tab_counter;
59 1fce2e75 2021-08-14 op
60 1fce2e75 2021-08-14 op return tab_counter++;
61 1fce2e75 2021-08-14 op }
62 1fce2e75 2021-08-14 op
63 1fce2e75 2021-08-14 op struct tab *
64 1fce2e75 2021-08-14 op new_tab(const char *url, const char *base, struct tab *after)
65 1fce2e75 2021-08-14 op {
66 1fce2e75 2021-08-14 op struct tab *tab;
67 1fce2e75 2021-08-14 op
68 87aeb47b 2021-08-18 op ui_schedule_redraw();
69 1fce2e75 2021-08-14 op autosave_hook();
70 1fce2e75 2021-08-14 op
71 1fce2e75 2021-08-14 op if ((tab = calloc(1, sizeof(*tab))) == NULL) {
72 98d3e6c1 2024-02-18 op ev_break();
73 1fce2e75 2021-08-14 op return NULL;
74 1fce2e75 2021-08-14 op }
75 1fce2e75 2021-08-14 op
76 65c49665 2024-01-23 op if ((tab->hist = hist_new(HIST_LINEAR)) == NULL) {
77 65c49665 2024-01-23 op free(tab);
78 98d3e6c1 2024-02-18 op ev_break();
79 65c49665 2024-01-23 op return NULL;
80 65c49665 2024-01-23 op }
81 65c49665 2024-01-23 op
82 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.head);
83 1fce2e75 2021-08-14 op TAILQ_INIT(&tab->buffer.page.head);
84 1fce2e75 2021-08-14 op
85 1fce2e75 2021-08-14 op tab->id = tab_new_id();
86 1fce2e75 2021-08-14 op
87 1fce2e75 2021-08-14 op if (after != NULL)
88 1fce2e75 2021-08-14 op TAILQ_INSERT_AFTER(&tabshead, after, tab, tabs);
89 1fce2e75 2021-08-14 op else
90 1fce2e75 2021-08-14 op TAILQ_INSERT_TAIL(&tabshead, tab, tabs);
91 1fce2e75 2021-08-14 op
92 ea639250 2021-01-02 op if (!operating)
93 ea639250 2021-01-02 op tab->flags |= TAB_LAZY;
94 1fce2e75 2021-08-14 op load_url_in_tab(tab, url, base, 0);
95 ea639250 2021-01-02 op switch_to_tab(tab);
96 1fce2e75 2021-08-14 op return tab;
97 1fce2e75 2021-08-14 op }
98 1fce2e75 2021-08-14 op
99 1fce2e75 2021-08-14 op /*
100 6c74799d 2022-01-05 op * Move a tab from the tablist to the killed tab list and erase its
101 05de8ac3 2022-01-06 op * contents. Append should always be 0 to prepend tabs so unkill_tab
102 05de8ac3 2022-01-06 op * can work correctly; appending is only useful during startup when
103 05de8ac3 2022-01-06 op * receiving the list of killed tabs to keep the correct order.
104 05de8ac3 2022-01-06 op * NB: doesn't update the current_tab.
105 1fce2e75 2021-08-14 op */
106 1fce2e75 2021-08-14 op void
107 05de8ac3 2022-01-06 op kill_tab(struct tab *tab, int append)
108 1fce2e75 2021-08-14 op {
109 6c74799d 2022-01-05 op int count;
110 6c74799d 2022-01-05 op
111 1fce2e75 2021-08-14 op stop_tab(tab);
112 6c74799d 2022-01-05 op erase_buffer(&tab->buffer);
113 6c74799d 2022-01-05 op TAILQ_REMOVE(&tabshead, tab, tabs);
114 87aeb47b 2021-08-18 op ui_schedule_redraw();
115 1fce2e75 2021-08-14 op autosave_hook();
116 1fce2e75 2021-08-14 op
117 98d3e6c1 2024-02-18 op ev_timer_cancel(tab->loading_timer);
118 1fce2e75 2021-08-14 op
119 05de8ac3 2022-01-06 op if (append)
120 05de8ac3 2022-01-06 op TAILQ_INSERT_TAIL(&ktabshead, tab, tabs);
121 05de8ac3 2022-01-06 op else
122 05de8ac3 2022-01-06 op TAILQ_INSERT_HEAD(&ktabshead, tab, tabs);
123 6c74799d 2022-01-05 op
124 6c74799d 2022-01-05 op /* gc closed tabs */
125 6c74799d 2022-01-05 op count = 0;
126 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &ktabshead, tabs)
127 6c74799d 2022-01-05 op count++;
128 6c74799d 2022-01-05 op while (count > max_killed_tabs) {
129 6c74799d 2022-01-05 op count--;
130 6c74799d 2022-01-05 op free_tab(TAILQ_LAST(&ktabshead, tabshead));
131 6c74799d 2022-01-05 op }
132 6c74799d 2022-01-05 op }
133 6c74799d 2022-01-05 op
134 6c74799d 2022-01-05 op /*
135 265508d0 2022-01-05 op * Resurrects the lastest killed tab and returns it. The tab is already
136 6c74799d 2022-01-05 op * added to the tab list with the TAB_LAZY flag set. NB: this doesn't
137 6c74799d 2022-01-05 op * update current_tab.
138 6c74799d 2022-01-05 op */
139 6c74799d 2022-01-05 op struct tab *
140 6c74799d 2022-01-05 op unkill_tab(void)
141 6c74799d 2022-01-05 op {
142 6c74799d 2022-01-05 op struct tab *t;
143 6c74799d 2022-01-05 op
144 6c74799d 2022-01-05 op if (TAILQ_EMPTY(&ktabshead))
145 6c74799d 2022-01-05 op return NULL;
146 6c74799d 2022-01-05 op
147 b7286684 2022-01-13 op ui_schedule_redraw();
148 6c74799d 2022-01-05 op autosave_hook();
149 6c74799d 2022-01-05 op
150 6c74799d 2022-01-05 op t = TAILQ_FIRST(&ktabshead);
151 6c74799d 2022-01-05 op TAILQ_REMOVE(&ktabshead, t, tabs);
152 6c74799d 2022-01-05 op TAILQ_INSERT_TAIL(&tabshead, t, tabs);
153 6c74799d 2022-01-05 op t->flags |= TAB_LAZY;
154 6c74799d 2022-01-05 op return t;
155 6c74799d 2022-01-05 op }
156 6c74799d 2022-01-05 op
157 6c74799d 2022-01-05 op /*
158 1b45e5df 2022-01-05 op * Free every resource linked to the tab, including the tab itself, and
159 1b45e5df 2022-01-05 op * removes it from the *killed* tablist.
160 6c74799d 2022-01-05 op */
161 6c74799d 2022-01-05 op void
162 6c74799d 2022-01-05 op free_tab(struct tab *tab)
163 6c74799d 2022-01-05 op {
164 6c74799d 2022-01-05 op TAILQ_REMOVE(&ktabshead, tab, tabs);
165 65c49665 2024-01-23 op hist_free(tab->hist);
166 1fce2e75 2021-08-14 op free(tab);
167 1fce2e75 2021-08-14 op }
168 1fce2e75 2021-08-14 op
169 1fce2e75 2021-08-14 op void
170 1fce2e75 2021-08-14 op stop_tab(struct tab *tab)
171 1fce2e75 2021-08-14 op {
172 d35e18b3 2024-02-04 op ui_send_net(IMSG_STOP, tab->id, -1, NULL, 0);
173 1fce2e75 2021-08-14 op }
174 1fce2e75 2021-08-14 op
175 6c74799d 2022-01-05 op static inline void
176 f63b8f73 2022-04-24 op savetab(FILE *fp, struct tab *tab, int killed)
177 1fce2e75 2021-08-14 op {
178 65c49665 2024-01-23 op size_t i, size, cur;
179 f63b8f73 2022-04-24 op size_t top_line, current_line;
180 1fce2e75 2021-08-14 op
181 f63b8f73 2022-04-24 op get_scroll_position(tab, &top_line, &current_line);
182 d0971653 2021-09-15 op
183 65c49665 2024-01-23 op fprintf(fp, "%s ", hist_cur(tab->hist));
184 6c74799d 2022-01-05 op if (tab == current_tab)
185 f63b8f73 2022-04-24 op fprintf(fp, "current,");
186 6c74799d 2022-01-05 op if (killed)
187 f63b8f73 2022-04-24 op fprintf(fp, "killed,");
188 1fce2e75 2021-08-14 op
189 f63b8f73 2022-04-24 op fprintf(fp, "top=%zu,cur=%zu %s\n", top_line, current_line,
190 f63b8f73 2022-04-24 op tab->buffer.page.title);
191 e795e935 2022-01-18 op
192 65c49665 2024-01-23 op cur = hist_off(tab->hist);
193 65c49665 2024-01-23 op size = hist_size(tab->hist);
194 65c49665 2024-01-23 op for (i = 0; i < size; ++i) {
195 65c49665 2024-01-23 op if (i == cur)
196 6c74799d 2022-01-05 op continue;
197 65c49665 2024-01-23 op fprintf(fp, "%s %s\n", i > cur ? ">" : "<",
198 65c49665 2024-01-23 op hist_nth(tab->hist, i));
199 6c74799d 2022-01-05 op }
200 6c74799d 2022-01-05 op }
201 1040cc7f 2021-01-02 op
202 5bd159bd 2022-05-11 op static void
203 5bd159bd 2022-05-11 op save_tabs(void)
204 6c74799d 2022-01-05 op {
205 5bd159bd 2022-05-11 op FILE *fp;
206 de6a6a40 2022-04-24 op struct tab *tab;
207 5bd159bd 2022-05-11 op int fd, err;
208 de6a6a40 2022-04-24 op char sfn[PATH_MAX];
209 1fce2e75 2021-08-14 op
210 de6a6a40 2022-04-24 op strlcpy(sfn, session_file_tmp, sizeof(sfn));
211 de6a6a40 2022-04-24 op if ((fd = mkstemp(sfn)) == -1 ||
212 5bd159bd 2022-05-11 op (fp = fdopen(fd, "w")) == NULL) {
213 de6a6a40 2022-04-24 op if (fd != -1) {
214 de6a6a40 2022-04-24 op unlink(sfn);
215 de6a6a40 2022-04-24 op close(fd);
216 de6a6a40 2022-04-24 op }
217 f63b8f73 2022-04-24 op return;
218 de6a6a40 2022-04-24 op }
219 6c74799d 2022-01-05 op
220 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &tabshead, tabs)
221 5bd159bd 2022-05-11 op savetab(fp, tab, 0);
222 6c74799d 2022-01-05 op TAILQ_FOREACH(tab, &ktabshead, tabs)
223 5bd159bd 2022-05-11 op savetab(fp, tab, 1);
224 6c74799d 2022-01-05 op
225 5bd159bd 2022-05-11 op err = fflush(fp) == EOF;
226 5bd159bd 2022-05-11 op fclose(fp);
227 9e97090d 2022-02-26 op
228 5bd159bd 2022-05-11 op if (err || rename(sfn, session_file) == -1)
229 de6a6a40 2022-04-24 op unlink(sfn);
230 5bd159bd 2022-05-11 op }
231 f63b8f73 2022-04-24 op
232 5bd159bd 2022-05-11 op static void
233 5bd159bd 2022-05-11 op save_all_history(void)
234 5bd159bd 2022-05-11 op {
235 5bd159bd 2022-05-11 op FILE *fp;
236 5bd159bd 2022-05-11 op size_t i;
237 5bd159bd 2022-05-11 op int fd, err;
238 5bd159bd 2022-05-11 op char sfn[PATH_MAX];
239 de6a6a40 2022-04-24 op
240 5bd159bd 2022-05-11 op strlcpy(sfn, history_file_tmp, sizeof(sfn));
241 5bd159bd 2022-05-11 op if ((fd = mkstemp(sfn)) == -1 ||
242 5bd159bd 2022-05-11 op (fp = fdopen(fd, "w")) == NULL) {
243 5bd159bd 2022-05-11 op if (fd != -1) {
244 5bd159bd 2022-05-11 op unlink(sfn);
245 5bd159bd 2022-05-11 op close(fd);
246 5bd159bd 2022-05-11 op }
247 de6a6a40 2022-04-24 op return;
248 5bd159bd 2022-05-11 op }
249 de6a6a40 2022-04-24 op
250 5bd159bd 2022-05-11 op for (i = 0; i < history.len; ++i) {
251 5bd159bd 2022-05-11 op history.items[i].dirty = 0;
252 5bd159bd 2022-05-11 op fprintf(fp, "%lld %s\n", (long long)history.items[i].ts,
253 5bd159bd 2022-05-11 op history.items[i].uri);
254 5bd159bd 2022-05-11 op }
255 9e97090d 2022-02-26 op
256 5bd159bd 2022-05-11 op err = fflush(fp) == EOF;
257 5bd159bd 2022-05-11 op fclose(fp);
258 5bd159bd 2022-05-11 op
259 5bd159bd 2022-05-11 op if (err || rename(sfn, history_file) == -1) {
260 5bd159bd 2022-05-11 op unlink(sfn);
261 5bd159bd 2022-05-11 op return;
262 9e97090d 2022-02-26 op }
263 f63b8f73 2022-04-24 op
264 5bd159bd 2022-05-11 op history.dirty = 0;
265 5bd159bd 2022-05-11 op history.extra = 0;
266 5bd159bd 2022-05-11 op }
267 5bd159bd 2022-05-11 op
268 5bd159bd 2022-05-11 op static void
269 5bd159bd 2022-05-11 op save_dirty_history(void)
270 5bd159bd 2022-05-11 op {
271 5bd159bd 2022-05-11 op FILE *fp;
272 5bd159bd 2022-05-11 op size_t i;
273 5bd159bd 2022-05-11 op
274 5bd159bd 2022-05-11 op if ((fp = fopen(history_file, "a")) == NULL)
275 5bd159bd 2022-05-11 op return;
276 5bd159bd 2022-05-11 op
277 5bd159bd 2022-05-11 op for (i = 0; i < history.len && history.dirty > 0; ++i) {
278 5bd159bd 2022-05-11 op if (!history.items[i].dirty)
279 5bd159bd 2022-05-11 op continue;
280 5bd159bd 2022-05-11 op history.dirty--;
281 5bd159bd 2022-05-11 op history.items[i].dirty = 0;
282 5bd159bd 2022-05-11 op fprintf(fp, "%lld %s\n", (long long)history.items[i].ts,
283 5bd159bd 2022-05-11 op history.items[i].uri);
284 5bd159bd 2022-05-11 op }
285 5bd159bd 2022-05-11 op history.dirty = 0;
286 5bd159bd 2022-05-11 op
287 5bd159bd 2022-05-11 op fclose(fp);
288 5bd159bd 2022-05-11 op }
289 5bd159bd 2022-05-11 op
290 5bd159bd 2022-05-11 op void
291 5bd159bd 2022-05-11 op save_session(void)
292 5bd159bd 2022-05-11 op {
293 5bd159bd 2022-05-11 op if (safe_mode)
294 5bd159bd 2022-05-11 op return;
295 5bd159bd 2022-05-11 op
296 5bd159bd 2022-05-11 op save_tabs();
297 5bd159bd 2022-05-11 op
298 5bd159bd 2022-05-11 op if (history.extra > HISTORY_CAP/2)
299 5bd159bd 2022-05-11 op save_all_history();
300 5bd159bd 2022-05-11 op else if (history.dirty)
301 5bd159bd 2022-05-11 op save_dirty_history();
302 9e97090d 2022-02-26 op }
303 9e97090d 2022-02-26 op
304 9e97090d 2022-02-26 op void
305 9e97090d 2022-02-26 op history_push(struct histitem *hi)
306 9e97090d 2022-02-26 op {
307 9e97090d 2022-02-26 op size_t i, oldest = 0;
308 9e97090d 2022-02-26 op char *uri;
309 9e97090d 2022-02-26 op
310 9e97090d 2022-02-26 op for (i = 0; i < history.len; ++i) {
311 9e97090d 2022-02-26 op if (history.items[i].ts < history.items[oldest].ts)
312 9e97090d 2022-02-26 op oldest = i;
313 9e97090d 2022-02-26 op
314 9e97090d 2022-02-26 op /* remove duplicates */
315 9e97090d 2022-02-26 op if (!strcmp(history.items[i].uri, hi->uri))
316 9e97090d 2022-02-26 op return;
317 9e97090d 2022-02-26 op }
318 9e97090d 2022-02-26 op
319 9e97090d 2022-02-26 op if ((uri = strdup(hi->uri)) == NULL)
320 9e97090d 2022-02-26 op abort();
321 9e97090d 2022-02-26 op
322 9e97090d 2022-02-26 op /* don't grow too much; replace the oldest */
323 9e97090d 2022-02-26 op if (history.len == HISTORY_CAP) {
324 9e97090d 2022-02-26 op history.items[oldest].ts = hi->ts;
325 9e97090d 2022-02-26 op free(history.items[oldest].uri);
326 9e97090d 2022-02-26 op history.items[oldest].uri = uri;
327 5bd159bd 2022-05-11 op
328 5bd159bd 2022-05-11 op /* Growed past the max value, signal to regen the file. */
329 5bd159bd 2022-05-11 op history.extra++;
330 9e97090d 2022-02-26 op return;
331 9e97090d 2022-02-26 op }
332 9e97090d 2022-02-26 op
333 9e97090d 2022-02-26 op history.items[history.len].ts = hi->ts;
334 9e97090d 2022-02-26 op history.items[history.len].uri = uri;
335 9e97090d 2022-02-26 op history.len++;
336 9e97090d 2022-02-26 op }
337 9e97090d 2022-02-26 op
338 9e97090d 2022-02-26 op static int
339 9e97090d 2022-02-26 op history_cmp(const void *a, const void *b)
340 9e97090d 2022-02-26 op {
341 9e97090d 2022-02-26 op const struct history_item *i = a, *j = b;
342 9e97090d 2022-02-26 op return strcmp(i->uri, j->uri);
343 9e97090d 2022-02-26 op }
344 9e97090d 2022-02-26 op
345 9e97090d 2022-02-26 op void
346 9e97090d 2022-02-26 op history_sort(void)
347 9e97090d 2022-02-26 op {
348 9e97090d 2022-02-26 op qsort(history.items, history.len, sizeof(history.items[0]),
349 9e97090d 2022-02-26 op history_cmp);
350 1fce2e75 2021-08-14 op }
351 1fce2e75 2021-08-14 op
352 1fce2e75 2021-08-14 op void
353 9e97090d 2022-02-26 op history_add(const char *uri)
354 9e97090d 2022-02-26 op {
355 9e97090d 2022-02-26 op size_t i, j, insert = 0, oldest = 0;
356 9e97090d 2022-02-26 op char *u;
357 9e97090d 2022-02-26 op int c;
358 9e97090d 2022-02-26 op
359 9e97090d 2022-02-26 op for (i = 0; i < history.len; ++i) {
360 9e97090d 2022-02-26 op if (history.items[i].ts < history.items[oldest].ts)
361 9e97090d 2022-02-26 op oldest = i;
362 9e97090d 2022-02-26 op
363 9e97090d 2022-02-26 op if (insert != 0 && insert < i)
364 9e97090d 2022-02-26 op continue;
365 9e97090d 2022-02-26 op
366 9e97090d 2022-02-26 op c = strcmp(uri, history.items[i].uri);
367 9e97090d 2022-02-26 op if (c == 0) {
368 9e97090d 2022-02-26 op history.items[i].ts = time(NULL);
369 9e97090d 2022-02-26 op history.items[i].dirty = 1;
370 9e97090d 2022-02-26 op history.dirty++;
371 9e97090d 2022-02-26 op autosave_hook();
372 9e97090d 2022-02-26 op return;
373 9e97090d 2022-02-26 op }
374 9e97090d 2022-02-26 op
375 9e97090d 2022-02-26 op if (c > 0)
376 9e97090d 2022-02-26 op insert = i;
377 9e97090d 2022-02-26 op }
378 9e97090d 2022-02-26 op
379 9e97090d 2022-02-26 op if ((u = strdup(uri)) == NULL)
380 9e97090d 2022-02-26 op return;
381 9e97090d 2022-02-26 op
382 9e97090d 2022-02-26 op /* if history is full, replace the oldest one */
383 9e97090d 2022-02-26 op if (history.len == HISTORY_CAP) {
384 9e97090d 2022-02-26 op free(history.items[oldest].uri);
385 9e97090d 2022-02-26 op history.items[oldest].uri = u;
386 9e97090d 2022-02-26 op history.items[oldest].ts = time(NULL);
387 9e97090d 2022-02-26 op history.items[oldest].dirty = 1;
388 9e97090d 2022-02-26 op history.dirty++;
389 9e97090d 2022-02-26 op history_sort();
390 9e97090d 2022-02-26 op autosave_hook();
391 9e97090d 2022-02-26 op return;
392 9e97090d 2022-02-26 op }
393 9e97090d 2022-02-26 op
394 9e97090d 2022-02-26 op /* otherwise just insert in the right spot */
395 9e97090d 2022-02-26 op
396 9e97090d 2022-02-26 op for (j = history.len; j > insert; --j)
397 9e97090d 2022-02-26 op memcpy(&history.items[j], &history.items[j-1],
398 9e97090d 2022-02-26 op sizeof(history.items[j]));
399 9e97090d 2022-02-26 op
400 9e97090d 2022-02-26 op history.items[insert].ts = time(NULL);
401 9e97090d 2022-02-26 op history.items[insert].uri = u;
402 9e97090d 2022-02-26 op history.items[insert].dirty = 1;
403 9e97090d 2022-02-26 op history.dirty++;
404 9e97090d 2022-02-26 op history.len++;
405 9e97090d 2022-02-26 op autosave_hook();
406 9e97090d 2022-02-26 op }
407 9e97090d 2022-02-26 op
408 9e97090d 2022-02-26 op void
409 1fce2e75 2021-08-14 op autosave_init(void)
410 1fce2e75 2021-08-14 op {
411 98d3e6c1 2024-02-18 op return;
412 1fce2e75 2021-08-14 op }
413 1fce2e75 2021-08-14 op
414 1fce2e75 2021-08-14 op void
415 98d3e6c1 2024-02-18 op autosave_timer(int fd, int event, void *data)
416 1fce2e75 2021-08-14 op {
417 1fce2e75 2021-08-14 op save_session();
418 1fce2e75 2021-08-14 op }
419 1fce2e75 2021-08-14 op
420 1fce2e75 2021-08-14 op /*
421 1fce2e75 2021-08-14 op * Function to be called in "interesting" places where we may want to
422 1fce2e75 2021-08-14 op * schedule an autosave (like on new tab or before loading an url.)
423 1fce2e75 2021-08-14 op */
424 1fce2e75 2021-08-14 op void
425 1fce2e75 2021-08-14 op autosave_hook(void)
426 1fce2e75 2021-08-14 op {
427 1fce2e75 2021-08-14 op struct timeval tv;
428 1fce2e75 2021-08-14 op
429 1fce2e75 2021-08-14 op if (autosave <= 0)
430 1fce2e75 2021-08-14 op return;
431 1fce2e75 2021-08-14 op
432 98d3e6c1 2024-02-18 op if (!ev_timer_pending(autosavetimer)) {
433 1fce2e75 2021-08-14 op tv.tv_sec = autosave;
434 1fce2e75 2021-08-14 op tv.tv_usec = 0;
435 1fce2e75 2021-08-14 op
436 98d3e6c1 2024-02-18 op autosavetimer = ev_timer(&tv, autosave_timer, NULL);
437 64f4f8e2 2022-04-24 op }
438 64f4f8e2 2022-04-24 op }
439 64f4f8e2 2022-04-24 op
440 64f4f8e2 2022-04-24 op static inline int
441 64f4f8e2 2022-04-24 op parse_khost_line(char *line, char *tmp[3])
442 64f4f8e2 2022-04-24 op {
443 64f4f8e2 2022-04-24 op char **ap;
444 64f4f8e2 2022-04-24 op
445 64f4f8e2 2022-04-24 op for (ap = tmp; ap < &tmp[3] &&
446 64f4f8e2 2022-04-24 op (*ap = strsep(&line, " \t\n")) != NULL;) {
447 64f4f8e2 2022-04-24 op if (**ap != '\0')
448 64f4f8e2 2022-04-24 op ap++;
449 64f4f8e2 2022-04-24 op }
450 64f4f8e2 2022-04-24 op
451 64f4f8e2 2022-04-24 op return ap == &tmp[3] && *line == '\0';
452 64f4f8e2 2022-04-24 op }
453 64f4f8e2 2022-04-24 op
454 64f4f8e2 2022-04-24 op static void
455 64f4f8e2 2022-04-24 op load_certs(struct ohash *certs)
456 64f4f8e2 2022-04-24 op {
457 64f4f8e2 2022-04-24 op char *tmp[3], *line = NULL;
458 64f4f8e2 2022-04-24 op const char *errstr;
459 64f4f8e2 2022-04-24 op size_t lineno = 0, linesize = 0;
460 64f4f8e2 2022-04-24 op ssize_t linelen;
461 64f4f8e2 2022-04-24 op FILE *f;
462 64f4f8e2 2022-04-24 op struct tofu_entry *e;
463 64f4f8e2 2022-04-24 op
464 64f4f8e2 2022-04-24 op if ((f = fopen(known_hosts_file, "r")) == NULL)
465 64f4f8e2 2022-04-24 op return;
466 64f4f8e2 2022-04-24 op
467 64f4f8e2 2022-04-24 op if ((e = calloc(1, sizeof(*e))) == NULL) {
468 64f4f8e2 2022-04-24 op fclose(f);
469 64f4f8e2 2022-04-24 op return;
470 1fce2e75 2021-08-14 op }
471 64f4f8e2 2022-04-24 op
472 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, f)) != -1) {
473 64f4f8e2 2022-04-24 op lineno++;
474 64f4f8e2 2022-04-24 op
475 64f4f8e2 2022-04-24 op if (parse_khost_line(line, tmp)) {
476 64f4f8e2 2022-04-24 op strlcpy(e->domain, tmp[0], sizeof(e->domain));
477 64f4f8e2 2022-04-24 op strlcpy(e->hash, tmp[1], sizeof(e->hash));
478 64f4f8e2 2022-04-24 op
479 64f4f8e2 2022-04-24 op e->verified = strtonum(tmp[2], 0, 1, &errstr);
480 64f4f8e2 2022-04-24 op if (errstr != NULL)
481 64f4f8e2 2022-04-24 op errx(1, "%s:%zu verification for %s is %s: %s",
482 64f4f8e2 2022-04-24 op known_hosts_file, lineno,
483 64f4f8e2 2022-04-24 op e->domain, errstr, tmp[2]);
484 64f4f8e2 2022-04-24 op
485 64f4f8e2 2022-04-24 op tofu_add(certs, e);
486 64f4f8e2 2022-04-24 op } else
487 64f4f8e2 2022-04-24 op warnx("%s:%zu invalid entry",
488 64f4f8e2 2022-04-24 op known_hosts_file, lineno);
489 64f4f8e2 2022-04-24 op }
490 64f4f8e2 2022-04-24 op
491 64f4f8e2 2022-04-24 op free(line);
492 64f4f8e2 2022-04-24 op fclose(f);
493 64f4f8e2 2022-04-24 op return;
494 1fce2e75 2021-08-14 op }
495 64f4f8e2 2022-04-24 op
496 64f4f8e2 2022-04-24 op
497 64f4f8e2 2022-04-24 op static void
498 64f4f8e2 2022-04-24 op load_hist(void)
499 64f4f8e2 2022-04-24 op {
500 64f4f8e2 2022-04-24 op FILE *hist;
501 64f4f8e2 2022-04-24 op size_t linesize = 0;
502 64f4f8e2 2022-04-24 op ssize_t linelen;
503 64f4f8e2 2022-04-24 op char *nl, *spc, *line = NULL;
504 64f4f8e2 2022-04-24 op const char *errstr;
505 64f4f8e2 2022-04-24 op struct histitem hi;
506 64f4f8e2 2022-04-24 op
507 64f4f8e2 2022-04-24 op if ((hist = fopen(history_file, "r")) == NULL)
508 64f4f8e2 2022-04-24 op return;
509 64f4f8e2 2022-04-24 op
510 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, hist)) != -1) {
511 64f4f8e2 2022-04-24 op if ((nl = strchr(line, '\n')) != NULL)
512 64f4f8e2 2022-04-24 op *nl = '\0';
513 64f4f8e2 2022-04-24 op if ((spc = strchr(line, ' ')) == NULL)
514 64f4f8e2 2022-04-24 op continue;
515 64f4f8e2 2022-04-24 op *spc = '\0';
516 64f4f8e2 2022-04-24 op spc++;
517 64f4f8e2 2022-04-24 op
518 64f4f8e2 2022-04-24 op memset(&hi, 0, sizeof(hi));
519 64f4f8e2 2022-04-24 op hi.ts = strtonum(line, INT64_MIN, INT64_MAX, &errstr);
520 64f4f8e2 2022-04-24 op if (errstr != NULL)
521 64f4f8e2 2022-04-24 op continue;
522 64f4f8e2 2022-04-24 op if (strlcpy(hi.uri, spc, sizeof(hi.uri)) >= sizeof(hi.uri))
523 64f4f8e2 2022-04-24 op continue;
524 64f4f8e2 2022-04-24 op
525 64f4f8e2 2022-04-24 op history_push(&hi);
526 64f4f8e2 2022-04-24 op }
527 64f4f8e2 2022-04-24 op
528 64f4f8e2 2022-04-24 op fclose(hist);
529 64f4f8e2 2022-04-24 op free(line);
530 64f4f8e2 2022-04-24 op
531 64f4f8e2 2022-04-24 op history_sort();
532 64f4f8e2 2022-04-24 op }
533 64f4f8e2 2022-04-24 op
534 64f4f8e2 2022-04-24 op /*
535 64f4f8e2 2022-04-24 op * Check if the last time telescope crashed. The check is done by
536 64f4f8e2 2022-04-24 op * looking at `crashed_file': if it exists then last time we crashed.
537 64f4f8e2 2022-04-24 op * Then, while here, touch the file too, it's removed during the
538 64f4f8e2 2022-04-24 op * teardown.
539 64f4f8e2 2022-04-24 op */
540 64f4f8e2 2022-04-24 op static int
541 64f4f8e2 2022-04-24 op last_time_crashed(void)
542 64f4f8e2 2022-04-24 op {
543 64f4f8e2 2022-04-24 op int fd, crashed = 1;
544 64f4f8e2 2022-04-24 op
545 64f4f8e2 2022-04-24 op if (safe_mode)
546 64f4f8e2 2022-04-24 op return 0;
547 64f4f8e2 2022-04-24 op
548 64f4f8e2 2022-04-24 op if (unlink(crashed_file) == -1 && errno == ENOENT)
549 64f4f8e2 2022-04-24 op crashed = 0;
550 64f4f8e2 2022-04-24 op
551 64f4f8e2 2022-04-24 op if ((fd = open(crashed_file, O_CREAT|O_WRONLY, 0600)) == -1)
552 64f4f8e2 2022-04-24 op return crashed;
553 64f4f8e2 2022-04-24 op close(fd);
554 64f4f8e2 2022-04-24 op
555 64f4f8e2 2022-04-24 op return crashed;
556 64f4f8e2 2022-04-24 op }
557 64f4f8e2 2022-04-24 op
558 64f4f8e2 2022-04-24 op /*
559 64f4f8e2 2022-04-24 op * Parse and restore a tab from the session file. The format is:
560 64f4f8e2 2022-04-24 op *
561 64f4f8e2 2022-04-24 op * URL [flags,...] [title]\n
562 64f4f8e2 2022-04-24 op */
563 64f4f8e2 2022-04-24 op static inline struct tab *
564 64f4f8e2 2022-04-24 op parse_tab_line(char *line, struct tab **ct)
565 64f4f8e2 2022-04-24 op {
566 64f4f8e2 2022-04-24 op struct tab *tab;
567 64f4f8e2 2022-04-24 op char *s, *t, *ap;
568 64f4f8e2 2022-04-24 op const char *uri, *title = "";
569 64f4f8e2 2022-04-24 op int current = 0, killed = 0;
570 64f4f8e2 2022-04-24 op size_t tline = 0, cline = 0;
571 64f4f8e2 2022-04-24 op
572 64f4f8e2 2022-04-24 op uri = line;
573 64f4f8e2 2022-04-24 op if ((s = strchr(line, ' ')) == NULL)
574 64f4f8e2 2022-04-24 op return NULL;
575 64f4f8e2 2022-04-24 op *s++ = '\0';
576 64f4f8e2 2022-04-24 op
577 64f4f8e2 2022-04-24 op if ((t = strchr(s, ' ')) != NULL) {
578 64f4f8e2 2022-04-24 op *t++ = '\0';
579 64f4f8e2 2022-04-24 op title = t;
580 64f4f8e2 2022-04-24 op }
581 64f4f8e2 2022-04-24 op
582 64f4f8e2 2022-04-24 op while ((ap = strsep(&s, ",")) != NULL) {
583 64f4f8e2 2022-04-24 op if (!strcmp(ap, "current"))
584 64f4f8e2 2022-04-24 op current = 1;
585 64f4f8e2 2022-04-24 op else if (!strcmp(ap, "killed"))
586 64f4f8e2 2022-04-24 op killed = 1;
587 64f4f8e2 2022-04-24 op else if (!strncmp(ap, "top=", 4))
588 64f4f8e2 2022-04-24 op tline = strtonum(ap+4, 0, UINT32_MAX, NULL);
589 64f4f8e2 2022-04-24 op else if (!strncmp(ap, "cur=", 4))
590 64f4f8e2 2022-04-24 op cline = strtonum(ap + 4, 0, UINT32_MAX, NULL);
591 64f4f8e2 2022-04-24 op }
592 64f4f8e2 2022-04-24 op
593 64f4f8e2 2022-04-24 op if (tline > cline) {
594 64f4f8e2 2022-04-24 op tline = 0;
595 64f4f8e2 2022-04-24 op cline = 0;
596 64f4f8e2 2022-04-24 op }
597 64f4f8e2 2022-04-24 op
598 64f4f8e2 2022-04-24 op if ((tab = new_tab(uri, NULL, NULL)) == NULL)
599 64f4f8e2 2022-04-24 op err(1, "new_tab");
600 65c49665 2024-01-23 op hist_set_offs(tab->hist, tline, cline);
601 64f4f8e2 2022-04-24 op strlcpy(tab->buffer.page.title, title, sizeof(tab->buffer.page.title));
602 64f4f8e2 2022-04-24 op
603 64f4f8e2 2022-04-24 op if (current)
604 64f4f8e2 2022-04-24 op *ct = tab;
605 64f4f8e2 2022-04-24 op else if (killed)
606 64f4f8e2 2022-04-24 op kill_tab(tab, 1);
607 64f4f8e2 2022-04-24 op
608 64f4f8e2 2022-04-24 op return tab;
609 64f4f8e2 2022-04-24 op }
610 64f4f8e2 2022-04-24 op
611 64f4f8e2 2022-04-24 op static void
612 64f4f8e2 2022-04-24 op load_tabs(void)
613 64f4f8e2 2022-04-24 op {
614 64f4f8e2 2022-04-24 op struct tab *tab = NULL, *ct = NULL;
615 64f4f8e2 2022-04-24 op FILE *session;
616 64f4f8e2 2022-04-24 op size_t lineno = 0, linesize = 0;
617 64f4f8e2 2022-04-24 op ssize_t linelen;
618 64f4f8e2 2022-04-24 op char *uri, *line = NULL;
619 64f4f8e2 2022-04-24 op
620 64f4f8e2 2022-04-24 op if ((session = fopen(session_file, "r")) == NULL) {
621 64f4f8e2 2022-04-24 op new_tab("about:new", NULL, NULL);
622 64f4f8e2 2022-04-24 op new_tab("about:help", NULL, NULL);
623 64f4f8e2 2022-04-24 op return;
624 64f4f8e2 2022-04-24 op }
625 64f4f8e2 2022-04-24 op
626 64f4f8e2 2022-04-24 op while ((linelen = getline(&line, &linesize, session)) != -1) {
627 64f4f8e2 2022-04-24 op lineno++;
628 64f4f8e2 2022-04-24 op
629 64f4f8e2 2022-04-24 op if (linelen > 0 && line[linelen-1] == '\n')
630 64f4f8e2 2022-04-24 op line[linelen-1] = '\0';
631 64f4f8e2 2022-04-24 op
632 64f4f8e2 2022-04-24 op if (*line == '<' || *line == '>') {
633 64f4f8e2 2022-04-24 op uri = line + 1;
634 64f4f8e2 2022-04-24 op if (*uri != ' ' || tab == NULL) {
635 64f4f8e2 2022-04-24 op fprintf(stderr, "%s:%zu invalid line\n",
636 64f4f8e2 2022-04-24 op session_file, lineno);
637 64f4f8e2 2022-04-24 op continue;
638 64f4f8e2 2022-04-24 op }
639 64f4f8e2 2022-04-24 op uri++;
640 64f4f8e2 2022-04-24 op
641 64f4f8e2 2022-04-24 op if (*line == '>') /* future hist */
642 65c49665 2024-01-23 op hist_append(tab->hist, uri);
643 64f4f8e2 2022-04-24 op else
644 65c49665 2024-01-23 op hist_prepend(tab->hist, uri);
645 64f4f8e2 2022-04-24 op } else
646 64f4f8e2 2022-04-24 op tab = parse_tab_line(line, &ct);
647 64f4f8e2 2022-04-24 op }
648 64f4f8e2 2022-04-24 op
649 64f4f8e2 2022-04-24 op fclose(session);
650 64f4f8e2 2022-04-24 op free(line);
651 64f4f8e2 2022-04-24 op
652 64f4f8e2 2022-04-24 op if (ct == NULL || TAILQ_EMPTY(&tabshead))
653 64f4f8e2 2022-04-24 op ct = new_tab("about:new", NULL, NULL);
654 64f4f8e2 2022-04-24 op
655 64f4f8e2 2022-04-24 op switch_to_tab(ct);
656 64f4f8e2 2022-04-24 op
657 64f4f8e2 2022-04-24 op if (last_time_crashed())
658 64f4f8e2 2022-04-24 op new_tab("about:crash", NULL, NULL);
659 64f4f8e2 2022-04-24 op }
660 64f4f8e2 2022-04-24 op
661 64f4f8e2 2022-04-24 op int
662 64f4f8e2 2022-04-24 op load_session(struct ohash *certs)
663 64f4f8e2 2022-04-24 op {
664 64f4f8e2 2022-04-24 op load_certs(certs);
665 64f4f8e2 2022-04-24 op load_hist();
666 64f4f8e2 2022-04-24 op load_tabs();
667 64f4f8e2 2022-04-24 op return 0;
668 64f4f8e2 2022-04-24 op }
669 64f4f8e2 2022-04-24 op
670 64f4f8e2 2022-04-24 op int
671 64f4f8e2 2022-04-24 op lock_session(void)
672 64f4f8e2 2022-04-24 op {
673 64f4f8e2 2022-04-24 op struct flock lock;
674 64f4f8e2 2022-04-24 op int fd;
675 64f4f8e2 2022-04-24 op
676 64f4f8e2 2022-04-24 op if ((fd = open(lockfile_path, O_WRONLY|O_CREAT, 0600)) == -1)
677 64f4f8e2 2022-04-24 op return -1;
678 64f4f8e2 2022-04-24 op
679 64f4f8e2 2022-04-24 op lock.l_start = 0;
680 64f4f8e2 2022-04-24 op lock.l_len = 0;
681 64f4f8e2 2022-04-24 op lock.l_type = F_WRLCK;
682 64f4f8e2 2022-04-24 op lock.l_whence = SEEK_SET;
683 64f4f8e2 2022-04-24 op
684 64f4f8e2 2022-04-24 op if (fcntl(fd, F_SETLK, &lock) == -1) {
685 64f4f8e2 2022-04-24 op close(fd);
686 64f4f8e2 2022-04-24 op return -1;
687 64f4f8e2 2022-04-24 op }
688 64f4f8e2 2022-04-24 op
689 64f4f8e2 2022-04-24 op return fd;
690 64f4f8e2 2022-04-24 op }