Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "compat.h"
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <string.h>
23 #include "telescope.h"
24 #include "mcache.h"
25 #include "parser.h"
26 #include "utils.h"
28 const char *gemtext_prefixes[] = {
29 [LINE_TEXT] = "",
30 [LINE_TITLE_1] = "# ",
31 [LINE_TITLE_2] = "## ",
32 [LINE_TITLE_3] = "### ",
33 [LINE_ITEM] = "* ",
34 [LINE_QUOTE] = "> ",
35 [LINE_PRE_START] = "``` ",
36 [LINE_PRE_CONTENT] = "",
37 [LINE_PRE_END] = "```",
38 };
40 struct mcache {
41 struct ohash h;
42 size_t npages;
43 size_t tot;
44 } mcache;
46 struct mcache_entry {
47 const char *parser_name;
48 int trust;
49 struct evbuffer *evb;
50 char url[];
51 };
53 static void
54 mcache_free_entry(const char *url)
55 {
56 struct mcache_entry *e;
57 unsigned int slot;
59 slot = ohash_qlookup(&mcache.h, url);
60 if ((e = ohash_remove(&mcache.h, slot)) == NULL)
61 return;
63 mcache.npages--;
64 mcache.tot -= EVBUFFER_LENGTH(e->evb);
66 evbuffer_free(e->evb);
67 free(e);
68 }
70 void
71 mcache_init(void)
72 {
73 struct ohash_info info = {
74 .key_offset = offsetof(struct mcache_entry, url),
75 .calloc = hash_calloc,
76 .free = hash_free,
77 .alloc = hash_alloc,
78 };
80 ohash_init(&mcache.h, 5, &info);
81 }
83 int
84 mcache_tab(struct tab *tab)
85 {
86 struct mcache_entry *e;
87 struct line *line;
88 unsigned int slot;
89 size_t l, len;
90 const char *url;
92 url = tab->hist_cur->h;
93 l = strlen(url);
94 len = sizeof(*e) + l + 1;
96 if ((e = calloc(1, len)) == NULL)
97 return -1;
98 e->parser_name = tab->buffer.page.name;
99 e->trust = tab->trust;
100 memcpy(e->url, url, l);
102 if ((e->evb = evbuffer_new()) == NULL)
103 goto err;
105 TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
106 const char *text, *alt;
107 int r;
109 if ((text = line->line) == NULL)
110 text = "";
112 if ((alt = line->alt) == NULL)
113 alt = "";
115 switch (line->type) {
116 case LINE_TEXT:
117 case LINE_TITLE_1:
118 case LINE_TITLE_2:
119 case LINE_TITLE_3:
120 case LINE_ITEM:
121 case LINE_QUOTE:
122 case LINE_PRE_START:
123 case LINE_PRE_CONTENT:
124 case LINE_PRE_END:
125 r = evbuffer_add_printf(e->evb, "%s%s\n",
126 gemtext_prefixes[line->type], text);
127 break;
129 case LINE_LINK:
130 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
131 alt, text);
132 break;
134 case LINE_PATCH:
135 case LINE_PATCH_HDR:
136 case LINE_PATCH_HUNK_HDR:
137 case LINE_PATCH_ADD:
138 case LINE_PATCH_DEL:
139 /* TODO */
140 r = -1;
141 break;
143 case LINE_COMPL:
144 case LINE_COMPL_CURRENT:
145 case LINE_HELP:
146 case LINE_DOWNLOAD:
147 case LINE_DOWNLOAD_DONE:
148 case LINE_DOWNLOAD_INFO:
149 case LINE_FRINGE:
150 /* not reached */
151 abort();
154 if (r == -1)
155 goto err;
158 /* free any previously cached copies of this page */
159 mcache_free_entry(url);
161 slot = ohash_qlookup(&mcache.h, url);
162 ohash_insert(&mcache.h, slot, e);
164 mcache.npages++;
165 mcache.tot += EVBUFFER_LENGTH(e->evb);
167 return 0;
169 err:
170 if (e->evb != NULL)
171 evbuffer_free(e->evb);
172 free(e);
173 return -1;
176 int
177 mcache_lookup(const char *url, struct tab *tab)
179 struct mcache_entry *e;
180 unsigned int slot;
182 slot = ohash_qlookup(&mcache.h, url);
183 if ((e = ohash_find(&mcache.h, slot)) == NULL)
184 return 0;
186 parser_init(tab, gemtext_initparser);
187 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
188 goto err;
189 if (!parser_free(tab))
190 goto err;
192 tab->buffer.page.name = e->parser_name;
193 tab->trust = e->trust;
194 return 1;
196 err:
197 parser_free(tab);
198 erase_buffer(&tab->buffer);
199 return 0;
202 void
203 mcache_info(size_t *npages, size_t *tot)
205 *npages = mcache.npages;
206 *tot = mcache.tot;