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 tot;
43 } mcache;
45 struct mcache_entry {
46 const char *parser_name;
47 int trust;
48 struct evbuffer *evb;
49 char url[];
50 };
52 static void
53 mcache_free_entry(const char *url)
54 {
55 struct mcache_entry *e;
56 unsigned int slot;
58 slot = ohash_qlookup(&mcache.h, url);
59 if ((e = ohash_remove(&mcache.h, slot)) == NULL)
60 return;
62 evbuffer_free(e->evb);
63 free(e);
64 }
66 void
67 mcache_init(void)
68 {
69 struct ohash_info info = {
70 .key_offset = offsetof(struct mcache_entry, url),
71 .calloc = hash_calloc,
72 .free = hash_free,
73 .alloc = hash_alloc,
74 };
76 ohash_init(&mcache.h, 5, &info);
77 }
79 int
80 mcache_tab(struct tab *tab)
81 {
82 struct mcache_entry *e;
83 struct line *line;
84 unsigned int slot;
85 size_t l, len;
86 const char *url;
88 url = tab->hist_cur->h;
89 l = strlen(url);
90 len = sizeof(*e) + l + 1;
92 if ((e = calloc(1, len)) == NULL)
93 return -1;
94 e->parser_name = tab->buffer.page.name;
95 e->trust = tab->trust;
96 memcpy(e->url, url, l);
98 if ((e->evb = evbuffer_new()) == NULL)
99 goto err;
101 TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
102 const char *text, *alt;
103 int r;
105 if ((text = line->line) == NULL)
106 text = "";
108 if ((alt = line->alt) == NULL)
109 alt = "";
111 switch (line->type) {
112 case LINE_TEXT:
113 case LINE_TITLE_1:
114 case LINE_TITLE_2:
115 case LINE_TITLE_3:
116 case LINE_ITEM:
117 case LINE_QUOTE:
118 case LINE_PRE_START:
119 case LINE_PRE_CONTENT:
120 case LINE_PRE_END:
121 r = evbuffer_add_printf(e->evb, "%s%s\n",
122 gemtext_prefixes[line->type], text);
123 break;
125 case LINE_LINK:
126 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
127 alt, text);
128 break;
130 case LINE_PATCH:
131 case LINE_PATCH_HDR:
132 case LINE_PATCH_HUNK_HDR:
133 case LINE_PATCH_ADD:
134 case LINE_PATCH_DEL:
135 /* TODO */
136 r = -1;
137 break;
139 case LINE_COMPL:
140 case LINE_COMPL_CURRENT:
141 case LINE_HELP:
142 case LINE_DOWNLOAD:
143 case LINE_DOWNLOAD_DONE:
144 case LINE_DOWNLOAD_INFO:
145 case LINE_FRINGE:
146 /* not reached */
147 abort();
150 if (r == -1)
151 goto err;
154 /* free any previously cached copies of this page */
155 mcache_free_entry(url);
157 slot = ohash_qlookup(&mcache.h, url);
158 ohash_insert(&mcache.h, slot, e);
159 return 0;
161 err:
162 if (e->evb != NULL)
163 evbuffer_free(e->evb);
164 free(e);
165 return -1;
168 int
169 mcache_lookup(const char *url, struct tab *tab)
171 struct mcache_entry *e;
172 unsigned int slot;
174 slot = ohash_qlookup(&mcache.h, url);
175 if ((e = ohash_find(&mcache.h, slot)) == NULL)
176 return 0;
178 parser_init(tab, gemtext_initparser);
179 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
180 goto err;
181 if (!parser_free(tab))
182 goto err;
184 tab->buffer.page.name = e->parser_name;
185 tab->trust = e->trust;
186 return 1;
188 err:
189 parser_free(tab);
190 erase_buffer(&tab->buffer);
191 return 0;