Blame


1 191b4ca2 2021-03-12 op /*
2 191b4ca2 2021-03-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 191b4ca2 2021-03-12 op *
4 191b4ca2 2021-03-12 op * Permission to use, copy, modify, and distribute this software for any
5 191b4ca2 2021-03-12 op * purpose with or without fee is hereby granted, provided that the above
6 191b4ca2 2021-03-12 op * copyright notice and this permission notice appear in all copies.
7 191b4ca2 2021-03-12 op *
8 191b4ca2 2021-03-12 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 191b4ca2 2021-03-12 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 191b4ca2 2021-03-12 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 191b4ca2 2021-03-12 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 191b4ca2 2021-03-12 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 191b4ca2 2021-03-12 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 191b4ca2 2021-03-12 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 191b4ca2 2021-03-12 op */
16 191b4ca2 2021-03-12 op
17 a467be63 2021-08-14 op #include "compat.h"
18 191b4ca2 2021-03-12 op
19 191b4ca2 2021-03-12 op #include <stdlib.h>
20 191b4ca2 2021-03-12 op #include <string.h>
21 191b4ca2 2021-03-12 op
22 a467be63 2021-08-14 op #include "parser.h"
23 a467be63 2021-08-14 op #include "telescope.h"
24 a467be63 2021-08-14 op #include "ui.h"
25 a467be63 2021-08-14 op
26 1fce2e75 2021-08-14 op /*
27 1fce2e75 2021-08-14 op * Load a text/gemini page given the string page. Always returns 0.
28 1fce2e75 2021-08-14 op */
29 1fce2e75 2021-08-14 op int
30 1fce2e75 2021-08-14 op load_page_from_str(struct tab *tab, const char *page)
31 1fce2e75 2021-08-14 op {
32 1fce2e75 2021-08-14 op parser_init(tab, gemtext_initparser);
33 1fce2e75 2021-08-14 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
34 1fce2e75 2021-08-14 op abort();
35 1fce2e75 2021-08-14 op if (!tab->buffer.page.free(&tab->buffer.page))
36 1fce2e75 2021-08-14 op abort();
37 1fce2e75 2021-08-14 op ui_on_tab_refresh(tab);
38 1fce2e75 2021-08-14 op ui_on_tab_loaded(tab);
39 1fce2e75 2021-08-14 op return 0;
40 1fce2e75 2021-08-14 op }
41 1fce2e75 2021-08-14 op
42 8c1b0837 2021-07-25 op void
43 8c1b0837 2021-07-25 op parser_init(struct tab *tab, parserfn fn)
44 8c1b0837 2021-07-25 op {
45 9eca3d7c 2021-11-02 op erase_buffer(&tab->buffer);
46 8c1b0837 2021-07-25 op fn(&tab->buffer.page);
47 5a21e0eb 2022-01-19 op tab->buffer.page.init = fn;
48 8c1b0837 2021-07-25 op }
49 8c1b0837 2021-07-25 op
50 191b4ca2 2021-03-12 op int
51 8c1b0837 2021-07-25 op parser_parse(struct tab *tab, const char *chunk, size_t len)
52 8c1b0837 2021-07-25 op {
53 8c1b0837 2021-07-25 op return tab->buffer.page.parse(&tab->buffer.page, chunk, len);
54 8c1b0837 2021-07-25 op }
55 8c1b0837 2021-07-25 op
56 8c1b0837 2021-07-25 op int
57 8c1b0837 2021-07-25 op parser_free(struct tab *tab)
58 8c1b0837 2021-07-25 op {
59 2fd6d754 2021-07-30 op int r;
60 2fd6d754 2021-07-30 op char *tilde, *slash;
61 8c1b0837 2021-07-25 op
62 8c1b0837 2021-07-25 op r = tab->buffer.page.free(&tab->buffer.page);
63 8c1b0837 2021-07-25 op
64 2fd6d754 2021-07-30 op if (*tab->buffer.page.title != '\0')
65 2fd6d754 2021-07-30 op return r;
66 2fd6d754 2021-07-30 op
67 dafcab64 2021-08-26 op /*
68 dafcab64 2021-08-26 op * heuristic: see if there is a "tilde user" and use that as
69 dafcab64 2021-08-26 op * page title, using the full domain name as fallback.
70 dafcab64 2021-08-26 op */
71 2fd6d754 2021-07-30 op if ((tilde = strstr(tab->hist_cur->h, "/~")) != NULL) {
72 2fd6d754 2021-07-30 op strlcpy(tab->buffer.page.title, tilde+1,
73 2fd6d754 2021-07-30 op sizeof(tab->buffer.page.title));
74 2fd6d754 2021-07-30 op
75 2fd6d754 2021-07-30 op if ((slash = strchr(tab->buffer.page.title, '/')) != NULL)
76 2fd6d754 2021-07-30 op *slash = '\0';
77 2fd6d754 2021-07-30 op } else
78 8c1b0837 2021-07-25 op strlcpy(tab->buffer.page.title, tab->uri.host,
79 8c1b0837 2021-07-25 op sizeof(tab->buffer.page.title));
80 8c1b0837 2021-07-25 op
81 8c1b0837 2021-07-25 op return r;
82 8c1b0837 2021-07-25 op }
83 8c1b0837 2021-07-25 op
84 8c1b0837 2021-07-25 op int
85 069c9a92 2022-01-19 op parser_serialize(struct tab *tab, struct evbuffer *evb)
86 069c9a92 2022-01-19 op {
87 069c9a92 2022-01-19 op struct line *line;
88 069c9a92 2022-01-19 op const char *text;
89 069c9a92 2022-01-19 op int r;
90 069c9a92 2022-01-19 op
91 069c9a92 2022-01-19 op if (tab->buffer.page.serialize != NULL)
92 069c9a92 2022-01-19 op return tab->buffer.page.serialize(&tab->buffer.page, evb);
93 069c9a92 2022-01-19 op
94 069c9a92 2022-01-19 op /* a default implementation good enough for plain text */
95 069c9a92 2022-01-19 op TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
96 069c9a92 2022-01-19 op if ((text = line->line) == NULL)
97 069c9a92 2022-01-19 op text = "";
98 069c9a92 2022-01-19 op
99 069c9a92 2022-01-19 op r = evbuffer_add_printf(evb, "%s\n", text);
100 069c9a92 2022-01-19 op if (r == -1)
101 069c9a92 2022-01-19 op return 0;
102 069c9a92 2022-01-19 op }
103 069c9a92 2022-01-19 op
104 069c9a92 2022-01-19 op return 1;
105 069c9a92 2022-01-19 op }
106 069c9a92 2022-01-19 op
107 069c9a92 2022-01-19 op int
108 191b4ca2 2021-03-12 op parser_append(struct parser *p, const char *buf, size_t len)
109 191b4ca2 2021-03-12 op {
110 191b4ca2 2021-03-12 op size_t newlen;
111 191b4ca2 2021-03-12 op char *t;
112 191b4ca2 2021-03-12 op
113 191b4ca2 2021-03-12 op newlen = len + p->len;
114 191b4ca2 2021-03-12 op if ((t = calloc(1, newlen)) == NULL)
115 191b4ca2 2021-03-12 op return 0;
116 191b4ca2 2021-03-12 op memcpy(t, p->buf, p->len);
117 191b4ca2 2021-03-12 op memcpy(t + p->len, buf, len);
118 191b4ca2 2021-03-12 op free(p->buf);
119 191b4ca2 2021-03-12 op p->buf = t;
120 191b4ca2 2021-03-12 op p->len = newlen;
121 191b4ca2 2021-03-12 op return 1;
122 191b4ca2 2021-03-12 op }
123 191b4ca2 2021-03-12 op
124 191b4ca2 2021-03-12 op int
125 191b4ca2 2021-03-12 op parser_set_buf(struct parser *p, const char *buf, size_t len)
126 191b4ca2 2021-03-12 op {
127 20107775 2021-03-19 op char *tmp;
128 191b4ca2 2021-03-12 op
129 191b4ca2 2021-03-12 op if (len == 0) {
130 191b4ca2 2021-03-12 op p->len = 0;
131 20107775 2021-03-19 op free(p->buf);
132 20107775 2021-03-19 op p->buf = NULL;
133 191b4ca2 2021-03-12 op return 1;
134 191b4ca2 2021-03-12 op }
135 191b4ca2 2021-03-12 op
136 be2ee49f 2021-05-14 op /*
137 be2ee49f 2021-05-14 op * p->buf and buf can (and probably almost always will)
138 be2ee49f 2021-05-14 op * overlap!
139 be2ee49f 2021-05-14 op */
140 20107775 2021-03-19 op
141 20107775 2021-03-19 op if ((tmp = calloc(1, len)) == NULL)
142 191b4ca2 2021-03-12 op return 0;
143 20107775 2021-03-19 op memcpy(tmp, buf, len);
144 20107775 2021-03-19 op free(p->buf);
145 20107775 2021-03-19 op p->buf = tmp;
146 191b4ca2 2021-03-12 op p->len = len;
147 191b4ca2 2021-03-12 op return 1;
148 191b4ca2 2021-03-12 op }
149 a5845bb5 2021-03-20 op
150 a5845bb5 2021-03-20 op int
151 a5845bb5 2021-03-20 op parser_foreach_line(struct parser *p, const char *buf, size_t size,
152 a5845bb5 2021-03-20 op parsechunkfn fn)
153 a5845bb5 2021-03-20 op {
154 31aa9f59 2021-03-20 op char *b, *e;
155 31aa9f59 2021-03-20 op unsigned int ch;
156 31aa9f59 2021-03-20 op size_t i, l, len;
157 a5845bb5 2021-03-20 op
158 31aa9f59 2021-03-20 op if (!parser_append(p, buf, size))
159 31aa9f59 2021-03-20 op return 0;
160 31aa9f59 2021-03-20 op b = p->buf;
161 31aa9f59 2021-03-20 op len = p->len;
162 31aa9f59 2021-03-20 op
163 27dce34f 2021-07-06 op if (!(p->flags & PARSER_IN_BODY) && len < 3)
164 c0b634dd 2021-07-06 op return 1;
165 c0b634dd 2021-07-06 op
166 27dce34f 2021-07-06 op if (!(p->flags & PARSER_IN_BODY)) {
167 95a8c791 2021-08-26 op p->flags |= PARSER_IN_BODY;
168 c0b634dd 2021-07-06 op
169 c0b634dd 2021-07-06 op /*
170 c0b634dd 2021-07-06 op * drop the BOM: only UTF-8 is supported, and there
171 c0b634dd 2021-07-06 op * it's useless; some editors may still add one
172 c0b634dd 2021-07-06 op * though.
173 c0b634dd 2021-07-06 op */
174 c0b634dd 2021-07-06 op if (memmem(b, len, "\xEF\xBB\xBF", 3) == b) {
175 c0b634dd 2021-07-06 op b += 3;
176 c0b634dd 2021-07-06 op len -= 3;
177 c0b634dd 2021-07-06 op }
178 c0b634dd 2021-07-06 op }
179 c0b634dd 2021-07-06 op
180 31aa9f59 2021-03-20 op /* drop every "funny" ASCII character */
181 31aa9f59 2021-03-20 op for (i = 0; i < len; ) {
182 31aa9f59 2021-03-20 op ch = b[i];
183 31aa9f59 2021-03-20 op if ((ch >= ' ' || ch == '\n' || ch == '\t')
184 31aa9f59 2021-03-20 op && ch != 127) { /* del */
185 31aa9f59 2021-03-20 op ++i;
186 31aa9f59 2021-03-20 op continue;
187 31aa9f59 2021-03-20 op }
188 08b9feb5 2021-07-26 op memmove(&b[i], &b[i+1], len - i - 1);
189 31aa9f59 2021-03-20 op len--;
190 a5845bb5 2021-03-20 op }
191 a5845bb5 2021-03-20 op
192 a5845bb5 2021-03-20 op while (len > 0) {
193 a5845bb5 2021-03-20 op if ((e = memmem((char*)b, len, "\n", 1)) == NULL)
194 a5845bb5 2021-03-20 op break;
195 a5845bb5 2021-03-20 op l = e - b;
196 a5845bb5 2021-03-20 op
197 a5845bb5 2021-03-20 op if (!fn(p, b, l))
198 a5845bb5 2021-03-20 op return 0;
199 a5845bb5 2021-03-20 op
200 a5845bb5 2021-03-20 op len -= l;
201 a5845bb5 2021-03-20 op b += l;
202 a5845bb5 2021-03-20 op
203 a5845bb5 2021-03-20 op if (len > 0) {
204 a5845bb5 2021-03-20 op /* skip \n */
205 a5845bb5 2021-03-20 op len--;
206 a5845bb5 2021-03-20 op b++;
207 a5845bb5 2021-03-20 op }
208 a5845bb5 2021-03-20 op }
209 a5845bb5 2021-03-20 op
210 a5845bb5 2021-03-20 op return parser_set_buf(p, b, len);
211 a5845bb5 2021-03-20 op }