2 d5af38cc 2022-01-10 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 d5af38cc 2022-01-10 op * Permission to use, copy, modify, and distribute this software for any
5 d5af38cc 2022-01-10 op * purpose with or without fee is hereby granted, provided that the above
6 d5af38cc 2022-01-10 op * copyright notice and this permission notice appear in all copies.
8 d5af38cc 2022-01-10 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d5af38cc 2022-01-10 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d5af38cc 2022-01-10 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d5af38cc 2022-01-10 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d5af38cc 2022-01-10 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d5af38cc 2022-01-10 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d5af38cc 2022-01-10 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 d5af38cc 2022-01-10 op #include "compat.h"
19 d5af38cc 2022-01-10 op #include <stdlib.h>
20 d5af38cc 2022-01-10 op #include <stdint.h>
21 d5af38cc 2022-01-10 op #include <string.h>
23 d5af38cc 2022-01-10 op #include "telescope.h"
24 d5af38cc 2022-01-10 op #include "mcache.h"
25 8f3c9af8 2022-01-11 op #include "parser.h"
26 9d65b1d9 2022-01-11 op #include "utils.h"
28 d5af38cc 2022-01-10 op const char *gemtext_prefixes[] = {
29 d5af38cc 2022-01-10 op [LINE_TEXT] = "",
30 d5af38cc 2022-01-10 op [LINE_TITLE_1] = "# ",
31 d5af38cc 2022-01-10 op [LINE_TITLE_2] = "## ",
32 d5af38cc 2022-01-10 op [LINE_TITLE_3] = "### ",
33 d5af38cc 2022-01-10 op [LINE_ITEM] = "* ",
34 d5af38cc 2022-01-10 op [LINE_QUOTE] = "> ",
35 d5af38cc 2022-01-10 op [LINE_PRE_START] = "``` ",
36 d5af38cc 2022-01-10 op [LINE_PRE_CONTENT] = "",
37 d5af38cc 2022-01-10 op [LINE_PRE_END] = "```",
40 d5af38cc 2022-01-10 op struct mcache {
41 d5af38cc 2022-01-10 op struct ohash h;
45 d5af38cc 2022-01-10 op struct mcache_entry {
46 4e3871f5 2022-01-11 op const char *parser_name;
48 d5af38cc 2022-01-10 op struct evbuffer *evb;
53 ffcd827c 2022-01-11 op mcache_free_entry(const char *url)
55 ffcd827c 2022-01-11 op struct mcache_entry *e;
56 ffcd827c 2022-01-11 op unsigned int slot;
58 ffcd827c 2022-01-11 op slot = ohash_qlookup(&mcache.h, url);
59 ffcd827c 2022-01-11 op if ((e = ohash_remove(&mcache.h, slot)) == NULL)
62 ffcd827c 2022-01-11 op evbuffer_free(e->evb);
67 d5af38cc 2022-01-10 op mcache_init(void)
69 d5af38cc 2022-01-10 op struct ohash_info info = {
70 d5af38cc 2022-01-10 op .key_offset = offsetof(struct mcache_entry, url),
71 d5af38cc 2022-01-10 op .calloc = hash_calloc,
72 d5af38cc 2022-01-10 op .free = hash_free,
73 d5af38cc 2022-01-10 op .alloc = hash_alloc,
76 d5af38cc 2022-01-10 op ohash_init(&mcache.h, 5, &info);
80 8f3c9af8 2022-01-11 op mcache_tab(struct tab *tab)
82 d5af38cc 2022-01-10 op struct mcache_entry *e;
83 d5af38cc 2022-01-10 op struct line *line;
84 d5af38cc 2022-01-10 op unsigned int slot;
85 d5af38cc 2022-01-10 op size_t l, len;
86 8f3c9af8 2022-01-11 op const char *url;
88 8f3c9af8 2022-01-11 op url = tab->hist_cur->h;
89 d5af38cc 2022-01-10 op l = strlen(url);
90 d5af38cc 2022-01-10 op len = sizeof(*e) + l + 1;
92 d5af38cc 2022-01-10 op if ((e = calloc(1, len)) == NULL)
94 4e3871f5 2022-01-11 op e->parser_name = tab->buffer.page.name;
95 8f3c9af8 2022-01-11 op e->trust = tab->trust;
96 d5af38cc 2022-01-10 op memcpy(e->url, url, l);
98 d5af38cc 2022-01-10 op if ((e->evb = evbuffer_new()) == NULL)
101 8f3c9af8 2022-01-11 op TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
102 d5af38cc 2022-01-10 op const char *text, *alt;
105 d5af38cc 2022-01-10 op if ((text = line->line) == NULL)
108 d5af38cc 2022-01-10 op if ((alt = line->alt) == NULL)
111 d5af38cc 2022-01-10 op switch (line->type) {
112 d5af38cc 2022-01-10 op case LINE_TEXT:
113 d5af38cc 2022-01-10 op case LINE_TITLE_1:
114 d5af38cc 2022-01-10 op case LINE_TITLE_2:
115 d5af38cc 2022-01-10 op case LINE_TITLE_3:
116 d5af38cc 2022-01-10 op case LINE_ITEM:
117 d5af38cc 2022-01-10 op case LINE_QUOTE:
118 d5af38cc 2022-01-10 op case LINE_PRE_START:
119 d5af38cc 2022-01-10 op case LINE_PRE_CONTENT:
120 d5af38cc 2022-01-10 op case LINE_PRE_END:
121 d5af38cc 2022-01-10 op r = evbuffer_add_printf(e->evb, "%s%s\n",
122 d5af38cc 2022-01-10 op gemtext_prefixes[line->type], text);
125 d5af38cc 2022-01-10 op case LINE_LINK:
126 d5af38cc 2022-01-10 op r = evbuffer_add_printf(e->evb, "=> %s %s\n",
130 d5af38cc 2022-01-10 op case LINE_PATCH:
131 d5af38cc 2022-01-10 op case LINE_PATCH_HDR:
132 d5af38cc 2022-01-10 op case LINE_PATCH_HUNK_HDR:
133 d5af38cc 2022-01-10 op case LINE_PATCH_ADD:
134 d5af38cc 2022-01-10 op case LINE_PATCH_DEL:
139 d5af38cc 2022-01-10 op case LINE_COMPL:
140 d5af38cc 2022-01-10 op case LINE_COMPL_CURRENT:
141 d5af38cc 2022-01-10 op case LINE_HELP:
142 d5af38cc 2022-01-10 op case LINE_DOWNLOAD:
143 d5af38cc 2022-01-10 op case LINE_DOWNLOAD_DONE:
144 d5af38cc 2022-01-10 op case LINE_DOWNLOAD_INFO:
145 d5af38cc 2022-01-10 op case LINE_FRINGE:
146 d5af38cc 2022-01-10 op /* not reached */
154 ffcd827c 2022-01-11 op /* free any previously cached copies of this page */
155 ffcd827c 2022-01-11 op mcache_free_entry(url);
157 d5af38cc 2022-01-10 op slot = ohash_qlookup(&mcache.h, url);
158 d5af38cc 2022-01-10 op ohash_insert(&mcache.h, slot, e);
162 d5af38cc 2022-01-10 op if (e->evb != NULL)
163 d5af38cc 2022-01-10 op evbuffer_free(e->evb);
169 8f3c9af8 2022-01-11 op mcache_lookup(const char *url, struct tab *tab)
171 d5af38cc 2022-01-10 op struct mcache_entry *e;
172 d5af38cc 2022-01-10 op unsigned int slot;
174 d5af38cc 2022-01-10 op slot = ohash_qlookup(&mcache.h, url);
175 d5af38cc 2022-01-10 op if ((e = ohash_find(&mcache.h, slot)) == NULL)
178 8f3c9af8 2022-01-11 op parser_init(tab, gemtext_initparser);
179 8f3c9af8 2022-01-11 op if (!parser_parse(tab, EVBUFFER_DATA(e->evb), EVBUFFER_LENGTH(e->evb)))
181 8f3c9af8 2022-01-11 op if (!parser_free(tab))
184 4e3871f5 2022-01-11 op tab->buffer.page.name = e->parser_name;
185 8f3c9af8 2022-01-11 op tab->trust = e->trust;
189 8f3c9af8 2022-01-11 op parser_free(tab);
190 8f3c9af8 2022-01-11 op erase_buffer(&tab->buffer);