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