2 191b4ca2 2021-03-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 a467be63 2021-08-14 op #include "compat.h"
19 191b4ca2 2021-03-12 op #include <stdlib.h>
20 191b4ca2 2021-03-12 op #include <string.h>
22 a467be63 2021-08-14 op #include "parser.h"
23 a467be63 2021-08-14 op #include "telescope.h"
24 a467be63 2021-08-14 op #include "ui.h"
27 1fce2e75 2021-08-14 op * Load a text/gemini page given the string page. Always returns 0.
30 1fce2e75 2021-08-14 op load_page_from_str(struct tab *tab, const char *page)
32 1fce2e75 2021-08-14 op parser_init(tab, gemtext_initparser);
33 1fce2e75 2021-08-14 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
35 1fce2e75 2021-08-14 op if (!tab->buffer.page.free(&tab->buffer.page))
37 1fce2e75 2021-08-14 op ui_on_tab_refresh(tab);
38 1fce2e75 2021-08-14 op ui_on_tab_loaded(tab);
43 8c1b0837 2021-07-25 op parser_init(struct tab *tab, parserfn fn)
45 9eca3d7c 2021-11-02 op erase_buffer(&tab->buffer);
46 8c1b0837 2021-07-25 op fn(&tab->buffer.page);
50 8c1b0837 2021-07-25 op parser_parse(struct tab *tab, const char *chunk, size_t len)
52 8c1b0837 2021-07-25 op return tab->buffer.page.parse(&tab->buffer.page, chunk, len);
56 8c1b0837 2021-07-25 op parser_free(struct tab *tab)
59 2fd6d754 2021-07-30 op char *tilde, *slash;
61 8c1b0837 2021-07-25 op r = tab->buffer.page.free(&tab->buffer.page);
63 2fd6d754 2021-07-30 op if (*tab->buffer.page.title != '\0')
67 dafcab64 2021-08-26 op * heuristic: see if there is a "tilde user" and use that as
68 dafcab64 2021-08-26 op * page title, using the full domain name as fallback.
70 2fd6d754 2021-07-30 op if ((tilde = strstr(tab->hist_cur->h, "/~")) != NULL) {
71 2fd6d754 2021-07-30 op strlcpy(tab->buffer.page.title, tilde+1,
72 2fd6d754 2021-07-30 op sizeof(tab->buffer.page.title));
74 2fd6d754 2021-07-30 op if ((slash = strchr(tab->buffer.page.title, '/')) != NULL)
75 2fd6d754 2021-07-30 op *slash = '\0';
77 8c1b0837 2021-07-25 op strlcpy(tab->buffer.page.title, tab->uri.host,
78 8c1b0837 2021-07-25 op sizeof(tab->buffer.page.title));
84 191b4ca2 2021-03-12 op parser_append(struct parser *p, const char *buf, size_t len)
86 191b4ca2 2021-03-12 op size_t newlen;
89 191b4ca2 2021-03-12 op newlen = len + p->len;
90 191b4ca2 2021-03-12 op if ((t = calloc(1, newlen)) == NULL)
92 191b4ca2 2021-03-12 op memcpy(t, p->buf, p->len);
93 191b4ca2 2021-03-12 op memcpy(t + p->len, buf, len);
96 191b4ca2 2021-03-12 op p->len = newlen;
101 191b4ca2 2021-03-12 op parser_set_buf(struct parser *p, const char *buf, size_t len)
105 191b4ca2 2021-03-12 op if (len == 0) {
107 20107775 2021-03-19 op free(p->buf);
108 20107775 2021-03-19 op p->buf = NULL;
113 be2ee49f 2021-05-14 op * p->buf and buf can (and probably almost always will)
117 20107775 2021-03-19 op if ((tmp = calloc(1, len)) == NULL)
119 20107775 2021-03-19 op memcpy(tmp, buf, len);
120 20107775 2021-03-19 op free(p->buf);
121 20107775 2021-03-19 op p->buf = tmp;
122 191b4ca2 2021-03-12 op p->len = len;
127 a5845bb5 2021-03-20 op parser_foreach_line(struct parser *p, const char *buf, size_t size,
128 a5845bb5 2021-03-20 op parsechunkfn fn)
131 31aa9f59 2021-03-20 op unsigned int ch;
132 31aa9f59 2021-03-20 op size_t i, l, len;
134 31aa9f59 2021-03-20 op if (!parser_append(p, buf, size))
137 31aa9f59 2021-03-20 op len = p->len;
139 27dce34f 2021-07-06 op if (!(p->flags & PARSER_IN_BODY) && len < 3)
142 27dce34f 2021-07-06 op if (!(p->flags & PARSER_IN_BODY)) {
143 95a8c791 2021-08-26 op p->flags |= PARSER_IN_BODY;
146 c0b634dd 2021-07-06 op * drop the BOM: only UTF-8 is supported, and there
147 c0b634dd 2021-07-06 op * it's useless; some editors may still add one
150 c0b634dd 2021-07-06 op if (memmem(b, len, "\xEF\xBB\xBF", 3) == b) {
156 31aa9f59 2021-03-20 op /* drop every "funny" ASCII character */
157 31aa9f59 2021-03-20 op for (i = 0; i < len; ) {
159 31aa9f59 2021-03-20 op if ((ch >= ' ' || ch == '\n' || ch == '\t')
160 31aa9f59 2021-03-20 op && ch != 127) { /* del */
164 08b9feb5 2021-07-26 op memmove(&b[i], &b[i+1], len - i - 1);
168 a5845bb5 2021-03-20 op while (len > 0) {
169 a5845bb5 2021-03-20 op if ((e = memmem((char*)b, len, "\n", 1)) == NULL)
173 a5845bb5 2021-03-20 op if (!fn(p, b, l))
179 a5845bb5 2021-03-20 op if (len > 0) {
180 a5845bb5 2021-03-20 op /* skip \n */
186 a5845bb5 2021-03-20 op return parser_set_buf(p, b, len);