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 void
53 mcache_init(void)
54 {
55 struct ohash_info info = {
56 .key_offset = offsetof(struct mcache_entry, url),
57 .calloc = hash_calloc,
58 .free = hash_free,
59 .alloc = hash_alloc,
60 };
62 ohash_init(&mcache.h, 5, &info);
63 }
65 int
66 mcache_tab(struct tab *tab)
67 {
68 struct mcache_entry *e;
69 struct line *line;
70 unsigned int slot;
71 size_t l, len;
72 const char *url;
74 url = tab->hist_cur->h;
75 l = strlen(url);
76 len = sizeof(*e) + l + 1;
78 if ((e = calloc(1, len)) == NULL)
79 return -1;
80 e->parser_name = tab->buffer.page.name;
81 e->trust = tab->trust;
82 memcpy(e->url, url, l);
84 if ((e->evb = evbuffer_new()) == NULL)
85 goto err;
87 TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
88 const char *text, *alt;
89 int r;
91 if ((text = line->line) == NULL)
92 text = "";
94 if ((alt = line->alt) == NULL)
95 alt = "";
97 switch (line->type) {
98 case LINE_TEXT:
99 case LINE_TITLE_1:
100 case LINE_TITLE_2:
101 case LINE_TITLE_3:
102 case LINE_ITEM:
103 case LINE_QUOTE:
104 case LINE_PRE_START:
105 case LINE_PRE_CONTENT:
106 case LINE_PRE_END:
107 r = evbuffer_add_printf(e->evb, "%s%s\n",
108 gemtext_prefixes[line->type], text);
109 break;
111 case LINE_LINK:
112 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
113 alt, text);
114 break;
116 case LINE_PATCH:
117 case LINE_PATCH_HDR:
118 case LINE_PATCH_HUNK_HDR:
119 case LINE_PATCH_ADD:
120 case LINE_PATCH_DEL:
121 /* TODO */
122 r = -1;
123 break;
125 case LINE_COMPL:
126 case LINE_COMPL_CURRENT:
127 case LINE_HELP:
128 case LINE_DOWNLOAD:
129 case LINE_DOWNLOAD_DONE:
130 case LINE_DOWNLOAD_INFO:
131 case LINE_FRINGE:
132 /* not reached */
133 abort();
136 if (r == -1)
137 goto err;
140 slot = ohash_qlookup(&mcache.h, url);
141 ohash_insert(&mcache.h, slot, e);
142 return 0;
144 err:
145 if (e->evb != NULL)
146 evbuffer_free(e->evb);
147 free(e);
148 return -1;
151 int
152 mcache_lookup(const char *url, struct tab *tab)
154 struct mcache_entry *e;
155 unsigned int slot;
157 slot = ohash_qlookup(&mcache.h, url);
158 if ((e = ohash_find(&mcache.h, slot)) == NULL)
159 return 0;
161 parser_init(tab, gemtext_initparser);
162 if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
163 goto err;
164 if (!parser_free(tab))
165 goto err;
167 tab->buffer.page.name = e->parser_name;
168 tab->trust = e->trust;
169 return 1;
171 err:
172 parser_free(tab);
173 erase_buffer(&tab->buffer);
174 return 0;