2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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.
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.
23 #include "telescope.h"
26 const char *gemtext_prefixes[] = {
28 [LINE_TITLE_1] = "# ",
29 [LINE_TITLE_2] = "## ",
30 [LINE_TITLE_3] = "### ",
33 [LINE_PRE_START] = "``` ",
34 [LINE_PRE_CONTENT] = "",
35 [LINE_PRE_END] = "```",
49 static void *hash_alloc(size_t, void *);
50 static void *hash_calloc(size_t, size_t, void *);
51 static void hash_free(void *, void *);
54 hash_alloc(size_t len, void *d)
56 if ((d = malloc(len)) == NULL)
62 hash_calloc(size_t nmemb, size_t size, void *d)
64 if ((d = calloc(nmemb, size)) == NULL)
70 hash_free(void *ptr, void *d)
78 struct ohash_info info = {
79 .key_offset = offsetof(struct mcache_entry, url),
80 .calloc = hash_calloc,
85 ohash_init(&mcache.h, 5, &info);
89 mcache_buffer(const char *url, struct buffer *buf, int trust)
91 struct mcache_entry *e;
97 len = sizeof(*e) + l + 1;
99 if ((e = calloc(1, len)) == NULL)
102 memcpy(e->url, url, l);
104 if ((e->evb = evbuffer_new()) == NULL)
107 TAILQ_FOREACH(line, &buf->page.head, lines) {
108 const char *text, *alt;
111 if ((text = line->line) == NULL)
114 if ((alt = line->alt) == NULL)
117 switch (line->type) {
125 case LINE_PRE_CONTENT:
127 r = evbuffer_add_printf(e->evb, "%s%s\n",
128 gemtext_prefixes[line->type], text);
132 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
138 case LINE_PATCH_HUNK_HDR:
146 case LINE_COMPL_CURRENT:
149 case LINE_DOWNLOAD_DONE:
150 case LINE_DOWNLOAD_INFO:
160 slot = ohash_qlookup(&mcache.h, url);
161 ohash_insert(&mcache.h, slot, e);
166 evbuffer_free(e->evb);
172 mcache_lookup(const char *url, struct evbuffer **ret, int *trust)
174 struct mcache_entry *e;
177 slot = ohash_qlookup(&mcache.h, url);
178 if ((e = ohash_find(&mcache.h, slot)) == NULL)