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