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
25 8c1b0837 2021-07-25 op void
26 8c1b0837 2021-07-25 op parser_init(struct tab *tab, parserfn fn)
27 8c1b0837 2021-07-25 op {
28 9eca3d7c 2021-11-02 op erase_buffer(&tab->buffer);
29 8c1b0837 2021-07-25 op fn(&tab->buffer.page);
30 5a21e0eb 2022-01-19 op tab->buffer.page.init = fn;
31 8c1b0837 2021-07-25 op }
32 8c1b0837 2021-07-25 op
33 191b4ca2 2021-03-12 op int
34 8c1b0837 2021-07-25 op parser_parse(struct tab *tab, const char *chunk, size_t len)
35 8c1b0837 2021-07-25 op {
36 8c1b0837 2021-07-25 op return tab->buffer.page.parse(&tab->buffer.page, chunk, len);
37 8c1b0837 2021-07-25 op }
38 8c1b0837 2021-07-25 op
39 8c1b0837 2021-07-25 op int
40 8c1b0837 2021-07-25 op parser_free(struct tab *tab)
41 8c1b0837 2021-07-25 op {
42 2fd6d754 2021-07-30 op int r;
43 2fd6d754 2021-07-30 op char *tilde, *slash;
44 8c1b0837 2021-07-25 op
45 8c1b0837 2021-07-25 op r = tab->buffer.page.free(&tab->buffer.page);
46 8c1b0837 2021-07-25 op
47 2fd6d754 2021-07-30 op if (*tab->buffer.page.title != '\0')
48 2fd6d754 2021-07-30 op return r;
49 2fd6d754 2021-07-30 op
50 dafcab64 2021-08-26 op /*
51 dafcab64 2021-08-26 op * heuristic: see if there is a "tilde user" and use that as
52 dafcab64 2021-08-26 op * page title, using the full domain name as fallback.
53 dafcab64 2021-08-26 op */
54 2fd6d754 2021-07-30 op if ((tilde = strstr(tab->hist_cur->h, "/~")) != NULL) {
55 2fd6d754 2021-07-30 op strlcpy(tab->buffer.page.title, tilde+1,
56 2fd6d754 2021-07-30 op sizeof(tab->buffer.page.title));
57 2fd6d754 2021-07-30 op
58 2fd6d754 2021-07-30 op if ((slash = strchr(tab->buffer.page.title, '/')) != NULL)
59 2fd6d754 2021-07-30 op *slash = '\0';
60 2fd6d754 2021-07-30 op } else
61 8c1b0837 2021-07-25 op strlcpy(tab->buffer.page.title, tab->uri.host,
62 8c1b0837 2021-07-25 op sizeof(tab->buffer.page.title));
63 8c1b0837 2021-07-25 op
64 8c1b0837 2021-07-25 op return r;
65 8c1b0837 2021-07-25 op }
66 8c1b0837 2021-07-25 op
67 8c1b0837 2021-07-25 op int
68 069c9a92 2022-01-19 op parser_serialize(struct tab *tab, struct evbuffer *evb)
69 069c9a92 2022-01-19 op {
70 069c9a92 2022-01-19 op struct line *line;
71 069c9a92 2022-01-19 op const char *text;
72 069c9a92 2022-01-19 op int r;
73 069c9a92 2022-01-19 op
74 069c9a92 2022-01-19 op if (tab->buffer.page.serialize != NULL)
75 069c9a92 2022-01-19 op return tab->buffer.page.serialize(&tab->buffer.page, evb);
76 069c9a92 2022-01-19 op
77 069c9a92 2022-01-19 op /* a default implementation good enough for plain text */
78 069c9a92 2022-01-19 op TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
79 069c9a92 2022-01-19 op if ((text = line->line) == NULL)
80 069c9a92 2022-01-19 op text = "";
81 069c9a92 2022-01-19 op
82 069c9a92 2022-01-19 op r = evbuffer_add_printf(evb, "%s\n", text);
83 069c9a92 2022-01-19 op if (r == -1)
84 069c9a92 2022-01-19 op return 0;
85 069c9a92 2022-01-19 op }
86 069c9a92 2022-01-19 op
87 069c9a92 2022-01-19 op return 1;
88 069c9a92 2022-01-19 op }
89 069c9a92 2022-01-19 op
90 069c9a92 2022-01-19 op int
91 191b4ca2 2021-03-12 op parser_append(struct parser *p, const char *buf, size_t len)
92 191b4ca2 2021-03-12 op {
93 191b4ca2 2021-03-12 op size_t newlen;
94 191b4ca2 2021-03-12 op char *t;
95 191b4ca2 2021-03-12 op
96 191b4ca2 2021-03-12 op newlen = len + p->len;
97 191b4ca2 2021-03-12 op if ((t = calloc(1, newlen)) == NULL)
98 191b4ca2 2021-03-12 op return 0;
99 191b4ca2 2021-03-12 op memcpy(t, p->buf, p->len);
100 191b4ca2 2021-03-12 op memcpy(t + p->len, buf, len);
101 191b4ca2 2021-03-12 op free(p->buf);
102 191b4ca2 2021-03-12 op p->buf = t;
103 191b4ca2 2021-03-12 op p->len = newlen;
104 191b4ca2 2021-03-12 op return 1;
105 191b4ca2 2021-03-12 op }
106 191b4ca2 2021-03-12 op
107 191b4ca2 2021-03-12 op int
108 191b4ca2 2021-03-12 op parser_set_buf(struct parser *p, const char *buf, size_t len)
109 191b4ca2 2021-03-12 op {
110 20107775 2021-03-19 op char *tmp;
111 191b4ca2 2021-03-12 op
112 191b4ca2 2021-03-12 op if (len == 0) {
113 191b4ca2 2021-03-12 op p->len = 0;
114 20107775 2021-03-19 op free(p->buf);
115 20107775 2021-03-19 op p->buf = NULL;
116 191b4ca2 2021-03-12 op return 1;
117 191b4ca2 2021-03-12 op }
118 191b4ca2 2021-03-12 op
119 be2ee49f 2021-05-14 op /*
120 be2ee49f 2021-05-14 op * p->buf and buf can (and probably almost always will)
121 be2ee49f 2021-05-14 op * overlap!
122 be2ee49f 2021-05-14 op */
123 20107775 2021-03-19 op
124 20107775 2021-03-19 op if ((tmp = calloc(1, len)) == NULL)
125 191b4ca2 2021-03-12 op return 0;
126 20107775 2021-03-19 op memcpy(tmp, buf, len);
127 20107775 2021-03-19 op free(p->buf);
128 20107775 2021-03-19 op p->buf = tmp;
129 191b4ca2 2021-03-12 op p->len = len;
130 191b4ca2 2021-03-12 op return 1;
131 191b4ca2 2021-03-12 op }
132 a5845bb5 2021-03-20 op
133 a5845bb5 2021-03-20 op int
134 a5845bb5 2021-03-20 op parser_foreach_line(struct parser *p, const char *buf, size_t size,
135 a5845bb5 2021-03-20 op parsechunkfn fn)
136 a5845bb5 2021-03-20 op {
137 31aa9f59 2021-03-20 op char *b, *e;
138 31aa9f59 2021-03-20 op unsigned int ch;
139 31aa9f59 2021-03-20 op size_t i, l, len;
140 a5845bb5 2021-03-20 op
141 31aa9f59 2021-03-20 op if (!parser_append(p, buf, size))
142 31aa9f59 2021-03-20 op return 0;
143 31aa9f59 2021-03-20 op b = p->buf;
144 31aa9f59 2021-03-20 op len = p->len;
145 31aa9f59 2021-03-20 op
146 27dce34f 2021-07-06 op if (!(p->flags & PARSER_IN_BODY) && len < 3)
147 c0b634dd 2021-07-06 op return 1;
148 c0b634dd 2021-07-06 op
149 27dce34f 2021-07-06 op if (!(p->flags & PARSER_IN_BODY)) {
150 95a8c791 2021-08-26 op p->flags |= PARSER_IN_BODY;
151 c0b634dd 2021-07-06 op
152 c0b634dd 2021-07-06 op /*
153 c0b634dd 2021-07-06 op * drop the BOM: only UTF-8 is supported, and there
154 c0b634dd 2021-07-06 op * it's useless; some editors may still add one
155 c0b634dd 2021-07-06 op * though.
156 c0b634dd 2021-07-06 op */
157 c0b634dd 2021-07-06 op if (memmem(b, len, "\xEF\xBB\xBF", 3) == b) {
158 c0b634dd 2021-07-06 op b += 3;
159 c0b634dd 2021-07-06 op len -= 3;
160 c0b634dd 2021-07-06 op }
161 c0b634dd 2021-07-06 op }
162 c0b634dd 2021-07-06 op
163 31aa9f59 2021-03-20 op /* drop every "funny" ASCII character */
164 31aa9f59 2021-03-20 op for (i = 0; i < len; ) {
165 31aa9f59 2021-03-20 op ch = b[i];
166 31aa9f59 2021-03-20 op if ((ch >= ' ' || ch == '\n' || ch == '\t')
167 31aa9f59 2021-03-20 op && ch != 127) { /* del */
168 31aa9f59 2021-03-20 op ++i;
169 31aa9f59 2021-03-20 op continue;
170 31aa9f59 2021-03-20 op }
171 08b9feb5 2021-07-26 op memmove(&b[i], &b[i+1], len - i - 1);
172 31aa9f59 2021-03-20 op len--;
173 a5845bb5 2021-03-20 op }
174 a5845bb5 2021-03-20 op
175 a5845bb5 2021-03-20 op while (len > 0) {
176 a5845bb5 2021-03-20 op if ((e = memmem((char*)b, len, "\n", 1)) == NULL)
177 a5845bb5 2021-03-20 op break;
178 a5845bb5 2021-03-20 op l = e - b;
179 a5845bb5 2021-03-20 op
180 a5845bb5 2021-03-20 op if (!fn(p, b, l))
181 a5845bb5 2021-03-20 op return 0;
182 a5845bb5 2021-03-20 op
183 a5845bb5 2021-03-20 op len -= l;
184 a5845bb5 2021-03-20 op b += l;
185 a5845bb5 2021-03-20 op
186 a5845bb5 2021-03-20 op if (len > 0) {
187 a5845bb5 2021-03-20 op /* skip \n */
188 a5845bb5 2021-03-20 op len--;
189 a5845bb5 2021-03-20 op b++;
190 a5845bb5 2021-03-20 op }
191 a5845bb5 2021-03-20 op }
192 a5845bb5 2021-03-20 op
193 a5845bb5 2021-03-20 op return parser_set_buf(p, b, len);
194 a5845bb5 2021-03-20 op }