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 55aa433f 2022-04-24 op parser_parsef(struct tab *tab, const char *fmt, ...)
46 55aa433f 2022-04-24 op va_start(ap, fmt);
47 55aa433f 2022-04-24 op r = vasprintf(&s, fmt, ap);
53 55aa433f 2022-04-24 op r = parser_parse(tab, s, strlen(s));
59 75a8a1ec 2022-02-08 op parser_free(struct tab *tab)
62 75a8a1ec 2022-02-08 op char *tilde, *slash;
64 75a8a1ec 2022-02-08 op r = tab->buffer.page.free(&tab->buffer.page);
66 75a8a1ec 2022-02-08 op if (*tab->buffer.page.title != '\0')
70 75a8a1ec 2022-02-08 op * heuristic: see if there is a "tilde user" and use that as
71 75a8a1ec 2022-02-08 op * page title, using the full domain name as fallback.
73 75a8a1ec 2022-02-08 op if ((tilde = strstr(tab->hist_cur->h, "/~")) != NULL) {
74 75a8a1ec 2022-02-08 op strlcpy(tab->buffer.page.title, tilde+1,
75 75a8a1ec 2022-02-08 op sizeof(tab->buffer.page.title));
77 75a8a1ec 2022-02-08 op if ((slash = strchr(tab->buffer.page.title, '/')) != NULL)
78 75a8a1ec 2022-02-08 op *slash = '\0';
80 75a8a1ec 2022-02-08 op strlcpy(tab->buffer.page.title, tab->uri.host,
81 75a8a1ec 2022-02-08 op sizeof(tab->buffer.page.title));
87 0110411e 2022-04-13 op parser_serialize(struct tab *tab, FILE *fp)
89 75a8a1ec 2022-02-08 op struct line *line;
90 75a8a1ec 2022-02-08 op const char *text;
93 75a8a1ec 2022-02-08 op if (tab->buffer.page.serialize != NULL)
94 0110411e 2022-04-13 op return tab->buffer.page.serialize(&tab->buffer.page, fp);
96 75a8a1ec 2022-02-08 op /* a default implementation good enough for plain text */
97 75a8a1ec 2022-02-08 op TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
98 75a8a1ec 2022-02-08 op if ((text = line->line) == NULL)
101 0110411e 2022-04-13 op r = fprintf(fp, "%s\n", text);
110 75a8a1ec 2022-02-08 op parser_append(struct parser *p, const char *buf, size_t len)
112 75a8a1ec 2022-02-08 op size_t newlen;
115 75a8a1ec 2022-02-08 op newlen = len + p->len;
116 75a8a1ec 2022-02-08 op if ((t = calloc(1, newlen)) == NULL)
118 75a8a1ec 2022-02-08 op memcpy(t, p->buf, p->len);
119 75a8a1ec 2022-02-08 op memcpy(t + p->len, buf, len);
120 75a8a1ec 2022-02-08 op free(p->buf);
122 75a8a1ec 2022-02-08 op p->len = newlen;
127 75a8a1ec 2022-02-08 op parser_set_buf(struct parser *p, const char *buf, size_t len)
131 75a8a1ec 2022-02-08 op if (len == 0) {
133 75a8a1ec 2022-02-08 op free(p->buf);
134 75a8a1ec 2022-02-08 op p->buf = NULL;
139 75a8a1ec 2022-02-08 op * p->buf and buf can (and probably almost always will)
143 75a8a1ec 2022-02-08 op if ((tmp = calloc(1, len)) == NULL)
145 75a8a1ec 2022-02-08 op memcpy(tmp, buf, len);
146 75a8a1ec 2022-02-08 op free(p->buf);
147 75a8a1ec 2022-02-08 op p->buf = tmp;
148 75a8a1ec 2022-02-08 op p->len = len;
153 75a8a1ec 2022-02-08 op parser_foreach_line(struct parser *p, const char *buf, size_t size,
154 75a8a1ec 2022-02-08 op parsechunkfn fn)
157 75a8a1ec 2022-02-08 op unsigned int ch;
158 75a8a1ec 2022-02-08 op size_t i, l, len;
160 75a8a1ec 2022-02-08 op if (!parser_append(p, buf, size))
163 75a8a1ec 2022-02-08 op len = p->len;
165 75a8a1ec 2022-02-08 op if (!(p->flags & PARSER_IN_BODY) && len < 3)
168 75a8a1ec 2022-02-08 op if (!(p->flags & PARSER_IN_BODY)) {
169 75a8a1ec 2022-02-08 op p->flags |= PARSER_IN_BODY;
172 75a8a1ec 2022-02-08 op * drop the BOM: only UTF-8 is supported, and there
173 75a8a1ec 2022-02-08 op * it's useless; some editors may still add one
176 75a8a1ec 2022-02-08 op if (memmem(b, len, "\xEF\xBB\xBF", 3) == b) {
182 75a8a1ec 2022-02-08 op /* drop every "funny" ASCII character */
183 75a8a1ec 2022-02-08 op for (i = 0; i < len; ) {
185 75a8a1ec 2022-02-08 op if ((ch >= ' ' || ch == '\n' || ch == '\t')
186 75a8a1ec 2022-02-08 op && ch != 127) { /* del */
190 75a8a1ec 2022-02-08 op memmove(&b[i], &b[i+1], len - i - 1);
194 75a8a1ec 2022-02-08 op while (len > 0) {
195 75a8a1ec 2022-02-08 op if ((e = memmem((char*)b, len, "\n", 1)) == NULL)
199 75a8a1ec 2022-02-08 op if (!fn(p, b, l))
205 75a8a1ec 2022-02-08 op if (len > 0) {
206 75a8a1ec 2022-02-08 op /* skip \n */
212 75a8a1ec 2022-02-08 op return parser_set_buf(p, b, len);