2 75a8a1ec 2022-02-08 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 75a8a1ec 2022-02-08 op * Permission to use, copy, modify, and distribute this software for any
5 75a8a1ec 2022-02-08 op * purpose with or without fee is hereby granted, provided that the above
6 75a8a1ec 2022-02-08 op * copyright notice and this permission notice appear in all copies.
8 75a8a1ec 2022-02-08 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 75a8a1ec 2022-02-08 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 75a8a1ec 2022-02-08 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 75a8a1ec 2022-02-08 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 75a8a1ec 2022-02-08 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 75a8a1ec 2022-02-08 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 75a8a1ec 2022-02-08 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 75a8a1ec 2022-02-08 op #include "compat.h"
19 75a8a1ec 2022-02-08 op #include <stdlib.h>
20 75a8a1ec 2022-02-08 op #include <string.h>
22 75a8a1ec 2022-02-08 op #include "parser.h"
23 75a8a1ec 2022-02-08 op #include "telescope.h"
26 75a8a1ec 2022-02-08 op parser_init(struct tab *tab, parserfn fn)
28 75a8a1ec 2022-02-08 op erase_buffer(&tab->buffer);
29 75a8a1ec 2022-02-08 op fn(&tab->buffer.page);
30 75a8a1ec 2022-02-08 op tab->buffer.page.init = fn;
34 75a8a1ec 2022-02-08 op parser_parse(struct tab *tab, const char *chunk, size_t len)
36 75a8a1ec 2022-02-08 op return tab->buffer.page.parse(&tab->buffer.page, chunk, len);
40 75a8a1ec 2022-02-08 op parser_free(struct tab *tab)
43 75a8a1ec 2022-02-08 op char *tilde, *slash;
45 75a8a1ec 2022-02-08 op r = tab->buffer.page.free(&tab->buffer.page);
47 75a8a1ec 2022-02-08 op if (*tab->buffer.page.title != '\0')
51 75a8a1ec 2022-02-08 op * heuristic: see if there is a "tilde user" and use that as
52 75a8a1ec 2022-02-08 op * page title, using the full domain name as fallback.
54 75a8a1ec 2022-02-08 op if ((tilde = strstr(tab->hist_cur->h, "/~")) != NULL) {
55 75a8a1ec 2022-02-08 op strlcpy(tab->buffer.page.title, tilde+1,
56 75a8a1ec 2022-02-08 op sizeof(tab->buffer.page.title));
58 75a8a1ec 2022-02-08 op if ((slash = strchr(tab->buffer.page.title, '/')) != NULL)
59 75a8a1ec 2022-02-08 op *slash = '\0';
61 75a8a1ec 2022-02-08 op strlcpy(tab->buffer.page.title, tab->uri.host,
62 75a8a1ec 2022-02-08 op sizeof(tab->buffer.page.title));
68 75a8a1ec 2022-02-08 op parser_serialize(struct tab *tab, struct evbuffer *evb)
70 75a8a1ec 2022-02-08 op struct line *line;
71 75a8a1ec 2022-02-08 op const char *text;
74 75a8a1ec 2022-02-08 op if (tab->buffer.page.serialize != NULL)
75 75a8a1ec 2022-02-08 op return tab->buffer.page.serialize(&tab->buffer.page, evb);
77 75a8a1ec 2022-02-08 op /* a default implementation good enough for plain text */
78 75a8a1ec 2022-02-08 op TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
79 75a8a1ec 2022-02-08 op if ((text = line->line) == NULL)
82 75a8a1ec 2022-02-08 op r = evbuffer_add_printf(evb, "%s\n", text);
91 75a8a1ec 2022-02-08 op parser_append(struct parser *p, const char *buf, size_t len)
93 75a8a1ec 2022-02-08 op size_t newlen;
96 75a8a1ec 2022-02-08 op newlen = len + p->len;
97 75a8a1ec 2022-02-08 op if ((t = calloc(1, newlen)) == NULL)
99 75a8a1ec 2022-02-08 op memcpy(t, p->buf, p->len);
100 75a8a1ec 2022-02-08 op memcpy(t + p->len, buf, len);
101 75a8a1ec 2022-02-08 op free(p->buf);
103 75a8a1ec 2022-02-08 op p->len = newlen;
108 75a8a1ec 2022-02-08 op parser_set_buf(struct parser *p, const char *buf, size_t len)
112 75a8a1ec 2022-02-08 op if (len == 0) {
114 75a8a1ec 2022-02-08 op free(p->buf);
115 75a8a1ec 2022-02-08 op p->buf = NULL;
120 75a8a1ec 2022-02-08 op * p->buf and buf can (and probably almost always will)
124 75a8a1ec 2022-02-08 op if ((tmp = calloc(1, len)) == NULL)
126 75a8a1ec 2022-02-08 op memcpy(tmp, buf, len);
127 75a8a1ec 2022-02-08 op free(p->buf);
128 75a8a1ec 2022-02-08 op p->buf = tmp;
129 75a8a1ec 2022-02-08 op p->len = len;
134 75a8a1ec 2022-02-08 op parser_foreach_line(struct parser *p, const char *buf, size_t size,
135 75a8a1ec 2022-02-08 op parsechunkfn fn)
138 75a8a1ec 2022-02-08 op unsigned int ch;
139 75a8a1ec 2022-02-08 op size_t i, l, len;
141 75a8a1ec 2022-02-08 op if (!parser_append(p, buf, size))
144 75a8a1ec 2022-02-08 op len = p->len;
146 75a8a1ec 2022-02-08 op if (!(p->flags & PARSER_IN_BODY) && len < 3)
149 75a8a1ec 2022-02-08 op if (!(p->flags & PARSER_IN_BODY)) {
150 75a8a1ec 2022-02-08 op p->flags |= PARSER_IN_BODY;
153 75a8a1ec 2022-02-08 op * drop the BOM: only UTF-8 is supported, and there
154 75a8a1ec 2022-02-08 op * it's useless; some editors may still add one
157 75a8a1ec 2022-02-08 op if (memmem(b, len, "\xEF\xBB\xBF", 3) == b) {
163 75a8a1ec 2022-02-08 op /* drop every "funny" ASCII character */
164 75a8a1ec 2022-02-08 op for (i = 0; i < len; ) {
166 75a8a1ec 2022-02-08 op if ((ch >= ' ' || ch == '\n' || ch == '\t')
167 75a8a1ec 2022-02-08 op && ch != 127) { /* del */
171 75a8a1ec 2022-02-08 op memmove(&b[i], &b[i+1], len - i - 1);
175 75a8a1ec 2022-02-08 op while (len > 0) {
176 75a8a1ec 2022-02-08 op if ((e = memmem((char*)b, len, "\n", 1)) == NULL)
180 75a8a1ec 2022-02-08 op if (!fn(p, b, l))
186 75a8a1ec 2022-02-08 op if (len > 0) {
187 75a8a1ec 2022-02-08 op /* skip \n */
193 75a8a1ec 2022-02-08 op return parser_set_buf(p, b, len);