Blame


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