2 1ac119fb 2024-01-23 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 1ac119fb 2024-01-23 op * Permission to use, copy, modify, and distribute this software for any
5 1ac119fb 2024-01-23 op * purpose with or without fee is hereby granted, provided that the above
6 1ac119fb 2024-01-23 op * copyright notice and this permission notice appear in all copies.
8 1ac119fb 2024-01-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 1ac119fb 2024-01-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 1ac119fb 2024-01-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 1ac119fb 2024-01-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 1ac119fb 2024-01-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 1ac119fb 2024-01-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 1ac119fb 2024-01-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1ac119fb 2024-01-23 op #include "compat.h"
19 1ac119fb 2024-01-23 op #include <stdlib.h>
20 1ac119fb 2024-01-23 op #include <string.h>
22 1ac119fb 2024-01-23 op #include "hist.h"
23 1ac119fb 2024-01-23 op #include "parser.h"
24 1ac119fb 2024-01-23 op #include "telescope.h"
27 1ac119fb 2024-01-23 op parser_init(struct tab *tab, parserfn fn)
29 1ac119fb 2024-01-23 op erase_buffer(&tab->buffer);
30 1ac119fb 2024-01-23 op fn(&tab->buffer.page);
31 1ac119fb 2024-01-23 op tab->buffer.page.init = fn;
35 1ac119fb 2024-01-23 op parser_parse(struct tab *tab, const char *chunk, size_t len)
37 1ac119fb 2024-01-23 op return tab->buffer.page.parse(&tab->buffer.page, chunk, len);
41 1ac119fb 2024-01-23 op parser_parsef(struct tab *tab, const char *fmt, ...)
47 1ac119fb 2024-01-23 op va_start(ap, fmt);
48 1ac119fb 2024-01-23 op r = vasprintf(&s, fmt, ap);
54 1ac119fb 2024-01-23 op r = parser_parse(tab, s, strlen(s));
60 1ac119fb 2024-01-23 op parser_free(struct tab *tab)
63 1ac119fb 2024-01-23 op char *tilde, *slash;
65 1ac119fb 2024-01-23 op r = tab->buffer.page.free(&tab->buffer.page);
67 1ac119fb 2024-01-23 op if (*tab->buffer.page.title != '\0')
71 1ac119fb 2024-01-23 op * heuristic: see if there is a "tilde user" and use that as
72 1ac119fb 2024-01-23 op * page title, using the full domain name as fallback.
74 1ac119fb 2024-01-23 op if ((tilde = strstr(hist_cur(tab->hist), "/~")) != NULL) {
75 1ac119fb 2024-01-23 op strlcpy(tab->buffer.page.title, tilde+1,
76 1ac119fb 2024-01-23 op sizeof(tab->buffer.page.title));
78 1ac119fb 2024-01-23 op if ((slash = strchr(tab->buffer.page.title, '/')) != NULL)
79 1ac119fb 2024-01-23 op *slash = '\0';
81 1ac119fb 2024-01-23 op strlcpy(tab->buffer.page.title, tab->iri.iri_host,
82 1ac119fb 2024-01-23 op sizeof(tab->buffer.page.title));
88 1ac119fb 2024-01-23 op parser_serialize(struct tab *tab, FILE *fp)
90 1ac119fb 2024-01-23 op struct line *line;
91 1ac119fb 2024-01-23 op const char *text;
94 1ac119fb 2024-01-23 op if (tab->buffer.page.serialize != NULL)
95 1ac119fb 2024-01-23 op return tab->buffer.page.serialize(&tab->buffer.page, fp);
97 1ac119fb 2024-01-23 op /* a default implementation good enough for plain text */
98 1ac119fb 2024-01-23 op TAILQ_FOREACH(line, &tab->buffer.page.head, lines) {
99 1ac119fb 2024-01-23 op if ((text = line->line) == NULL)
102 1ac119fb 2024-01-23 op r = fprintf(fp, "%s\n", text);
111 1ac119fb 2024-01-23 op parser_append(struct parser *p, const char *buf, size_t len)
113 1ac119fb 2024-01-23 op size_t newlen;
116 1ac119fb 2024-01-23 op newlen = len + p->len;
117 1ac119fb 2024-01-23 op if ((t = calloc(1, newlen)) == NULL)
119 1ac119fb 2024-01-23 op memcpy(t, p->buf, p->len);
120 1ac119fb 2024-01-23 op memcpy(t + p->len, buf, len);
121 1ac119fb 2024-01-23 op free(p->buf);
123 1ac119fb 2024-01-23 op p->len = newlen;
128 1ac119fb 2024-01-23 op parser_set_buf(struct parser *p, const char *buf, size_t len)
132 1ac119fb 2024-01-23 op if (len == 0) {
134 1ac119fb 2024-01-23 op free(p->buf);
135 1ac119fb 2024-01-23 op p->buf = NULL;
140 1ac119fb 2024-01-23 op * p->buf and buf can (and probably almost always will)
144 1ac119fb 2024-01-23 op if ((tmp = calloc(1, len)) == NULL)
146 1ac119fb 2024-01-23 op memcpy(tmp, buf, len);
147 1ac119fb 2024-01-23 op free(p->buf);
148 1ac119fb 2024-01-23 op p->buf = tmp;
149 1ac119fb 2024-01-23 op p->len = len;
154 1ac119fb 2024-01-23 op parser_foreach_line(struct parser *p, const char *buf, size_t size,
155 1ac119fb 2024-01-23 op parsechunkfn fn)
158 1ac119fb 2024-01-23 op unsigned int ch;
159 1ac119fb 2024-01-23 op size_t i, l, len;
161 1ac119fb 2024-01-23 op if (!parser_append(p, buf, size))
164 1ac119fb 2024-01-23 op len = p->len;
166 1ac119fb 2024-01-23 op if (!(p->flags & PARSER_IN_BODY) && len < 3)
169 1ac119fb 2024-01-23 op if (!(p->flags & PARSER_IN_BODY)) {
170 1ac119fb 2024-01-23 op p->flags |= PARSER_IN_BODY;
173 1ac119fb 2024-01-23 op * drop the BOM: only UTF-8 is supported, and there
174 1ac119fb 2024-01-23 op * it's useless; some editors may still add one
177 1ac119fb 2024-01-23 op if (memmem(b, len, "\xEF\xBB\xBF", 3) == b) {
183 1ac119fb 2024-01-23 op /* drop every "funny" ASCII character */
184 1ac119fb 2024-01-23 op for (i = 0; i < len; ) {
186 1ac119fb 2024-01-23 op if ((ch >= ' ' || ch == '\n' || ch == '\t')
187 1ac119fb 2024-01-23 op && ch != 127) { /* del */
191 1ac119fb 2024-01-23 op memmove(&b[i], &b[i+1], len - i - 1);
195 1ac119fb 2024-01-23 op while (len > 0) {
196 1ac119fb 2024-01-23 op if ((e = memmem((char*)b, len, "\n", 1)) == NULL)
200 1ac119fb 2024-01-23 op if (!fn(p, b, l))
206 1ac119fb 2024-01-23 op if (len > 0) {
207 1ac119fb 2024-01-23 op /* skip \n */
213 1ac119fb 2024-01-23 op return parser_set_buf(p, b, len);