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"
27 const char *gemtext_prefixes[] = {
28 [LINE_TEXT] = "",
29 [LINE_TITLE_1] = "# ",
30 [LINE_TITLE_2] = "## ",
31 [LINE_TITLE_3] = "### ",
32 [LINE_ITEM] = "* ",
33 [LINE_QUOTE] = "> ",
34 [LINE_PRE_START] = "``` ",
35 [LINE_PRE_CONTENT] = "",
36 [LINE_PRE_END] = "```",
37 };
39 struct mcache {
40 struct ohash h;
41 size_t tot;
42 } mcache;
44 struct mcache_entry {
45 int trust;
46 struct evbuffer *evb;
47 char url[];
48 };
50 static void *hash_alloc(size_t, void *);
51 static void *hash_calloc(size_t, size_t, void *);
52 static void hash_free(void *, void *);
54 static void *
55 hash_alloc(size_t len, void *d)
56 {
57 if ((d = malloc(len)) == NULL)
58 abort();
59 return d;
60 }
62 static void *
63 hash_calloc(size_t nmemb, size_t size, void *d)
64 {
65 if ((d = calloc(nmemb, size)) == NULL)
66 abort();
67 return d;
68 }
70 static void
71 hash_free(void *ptr, void *d)
72 {
73 free(ptr);
74 }
76 void
77 mcache_init(void)
78 {
79 struct ohash_info info = {
80 .key_offset = offsetof(struct mcache_entry, url),
81 .calloc = hash_calloc,
82 .free = hash_free,
83 .alloc = hash_alloc,
84 };
86 ohash_init(&mcache.h, 5, &info);
87 }
89 int
90 mcache_tab(struct tab *tab)
91 {
92 struct mcache_entry *e;
93 struct line *line;
94 unsigned int slot;
95 size_t l, len;
96 const char *url;
98 url = tab->hist_cur->h;
99 l = strlen(url);
100 len = sizeof(*e) + l + 1;
102 if ((e = calloc(1, len)) == NULL)
103 return -1;
104 e->trust = tab->trust;
105 memcpy(e->url, url, l);
107 if ((e->evb = evbuffer_new()) == NULL)
108 goto err;
110 TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
111 const char *text, *alt;
112 int r;
114 if ((text = line->line) == NULL)
115 text = "";
117 if ((alt = line->alt) == NULL)
118 alt = "";
120 switch (line->type) {
121 case LINE_TEXT:
122 case LINE_TITLE_1:
123 case LINE_TITLE_2:
124 case LINE_TITLE_3:
125 case LINE_ITEM:
126 case LINE_QUOTE:
127 case LINE_PRE_START:
128 case LINE_PRE_CONTENT:
129 case LINE_PRE_END:
130 r = evbuffer_add_printf(e->evb, "%s%s\n",
131 gemtext_prefixes[line->type], text);
132 break;
134 case LINE_LINK:
135 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
136 alt, text);
137 break;
139 case LINE_PATCH:
140 case LINE_PATCH_HDR:
141 case LINE_PATCH_HUNK_HDR:
142 case LINE_PATCH_ADD:
143 case LINE_PATCH_DEL:
144 /* TODO */
145 r = -1;
146 break;
148 case LINE_COMPL:
149 case LINE_COMPL_CURRENT:
150 case LINE_HELP:
151 case LINE_DOWNLOAD:
152 case LINE_DOWNLOAD_DONE:
153 case LINE_DOWNLOAD_INFO:
154 case LINE_FRINGE:
155 /* not reached */
156 abort();
159 if (r == -1)
160 goto err;
163 slot = ohash_qlookup(&mcache.h, url);
164 ohash_insert(&mcache.h, slot, e);
165 return 0;
167 err:
168 if (e->evb != NULL)
169 evbuffer_free(e->evb);
170 free(e);
171 return -1;
174 int
175 mcache_lookup(const char *url, struct tab *tab)
177 struct mcache_entry *e;
178 unsigned int slot;
180 slot = ohash_qlookup(&mcache.h, url);
181 if ((e = ohash_find(&mcache.h, slot)) == NULL)
182 return 0;
184 parser_init(tab, gemtext_initparser);
185 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
186 goto err;
187 if (!parser_free(tab))
188 goto err;
190 tab->trust = e->trust;
191 return 1;
193 err:
194 parser_free(tab);
195 erase_buffer(&tab->buffer);
196 return 0;