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"
26 const char *gemtext_prefixes[] = {
27 [LINE_TEXT] = "",
28 [LINE_TITLE_1] = "# ",
29 [LINE_TITLE_2] = "## ",
30 [LINE_TITLE_3] = "### ",
31 [LINE_ITEM] = "* ",
32 [LINE_QUOTE] = "> ",
33 [LINE_PRE_START] = "``` ",
34 [LINE_PRE_CONTENT] = "",
35 [LINE_PRE_END] = "```",
36 };
38 struct mcache {
39 struct ohash h;
40 size_t tot;
41 } mcache;
43 struct mcache_entry {
44 int trust;
45 struct evbuffer *evb;
46 char url[];
47 };
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 *);
53 static void *
54 hash_alloc(size_t len, void *d)
55 {
56 if ((d = malloc(len)) == NULL)
57 abort();
58 return d;
59 }
61 static void *
62 hash_calloc(size_t nmemb, size_t size, void *d)
63 {
64 if ((d = calloc(nmemb, size)) == NULL)
65 abort();
66 return d;
67 }
69 static void
70 hash_free(void *ptr, void *d)
71 {
72 free(ptr);
73 }
75 void
76 mcache_init(void)
77 {
78 struct ohash_info info = {
79 .key_offset = offsetof(struct mcache_entry, url),
80 .calloc = hash_calloc,
81 .free = hash_free,
82 .alloc = hash_alloc,
83 };
85 ohash_init(&mcache.h, 5, &info);
86 }
88 int
89 mcache_buffer(const char *url, struct buffer *buf, int trust)
90 {
91 struct mcache_entry *e;
92 struct line *line;
93 unsigned int slot;
94 size_t l, len;
96 l = strlen(url);
97 len = sizeof(*e) + l + 1;
99 if ((e = calloc(1, len)) == NULL)
100 return -1;
101 e->trust = trust;
102 memcpy(e->url, url, l);
104 if ((e->evb = evbuffer_new()) == NULL)
105 goto err;
107 TAILQ_FOREACH(line, &buf->page.head, lines) {
108 const char *text, *alt;
109 int r;
111 if ((text = line->line) == NULL)
112 text = "";
114 if ((alt = line->alt) == NULL)
115 alt = "";
117 switch (line->type) {
118 case LINE_TEXT:
119 case LINE_TITLE_1:
120 case LINE_TITLE_2:
121 case LINE_TITLE_3:
122 case LINE_ITEM:
123 case LINE_QUOTE:
124 case LINE_PRE_START:
125 case LINE_PRE_CONTENT:
126 case LINE_PRE_END:
127 r = evbuffer_add_printf(e->evb, "%s%s\n",
128 gemtext_prefixes[line->type], text);
129 break;
131 case LINE_LINK:
132 r = evbuffer_add_printf(e->evb, "=> %s %s\n",
133 alt, text);
134 break;
136 case LINE_PATCH:
137 case LINE_PATCH_HDR:
138 case LINE_PATCH_HUNK_HDR:
139 case LINE_PATCH_ADD:
140 case LINE_PATCH_DEL:
141 /* TODO */
142 r = -1;
143 break;
145 case LINE_COMPL:
146 case LINE_COMPL_CURRENT:
147 case LINE_HELP:
148 case LINE_DOWNLOAD:
149 case LINE_DOWNLOAD_DONE:
150 case LINE_DOWNLOAD_INFO:
151 case LINE_FRINGE:
152 /* not reached */
153 abort();
156 if (r == -1)
157 goto err;
160 slot = ohash_qlookup(&mcache.h, url);
161 ohash_insert(&mcache.h, slot, e);
162 return 0;
164 err:
165 if (e->evb != NULL)
166 evbuffer_free(e->evb);
167 free(e);
168 return -1;
171 int
172 mcache_lookup(const char *url, struct evbuffer **ret, int *trust)
174 struct mcache_entry *e;
175 unsigned int slot;
177 slot = ohash_qlookup(&mcache.h, url);
178 if ((e = ohash_find(&mcache.h, slot)) == NULL)
179 return 0;
181 *ret = e->evb;
182 *trust = e->trust;
183 return 1;