Blame


1 191b4ca2 2021-03-12 op /*
2 191b4ca2 2021-03-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 191b4ca2 2021-03-12 op *
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.
7 191b4ca2 2021-03-12 op *
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.
15 191b4ca2 2021-03-12 op */
16 191b4ca2 2021-03-12 op
17 395b9f4e 2021-07-12 op #include "parser.h"
18 191b4ca2 2021-03-12 op #include "telescope.h"
19 1fce2e75 2021-08-14 op #include "ui.h"
20 191b4ca2 2021-03-12 op
21 191b4ca2 2021-03-12 op #include <stdlib.h>
22 191b4ca2 2021-03-12 op #include <string.h>
23 191b4ca2 2021-03-12 op
24 1fce2e75 2021-08-14 op /*
25 1fce2e75 2021-08-14 op * Load a text/gemini page given the string page. Always returns 0.
26 1fce2e75 2021-08-14 op */
27 1fce2e75 2021-08-14 op int
28 1fce2e75 2021-08-14 op load_page_from_str(struct tab *tab, const char *page)
29 1fce2e75 2021-08-14 op {
30 1fce2e75 2021-08-14 op erase_buffer(&tab->buffer);
31 1fce2e75 2021-08-14 op parser_init(tab, gemtext_initparser);
32 1fce2e75 2021-08-14 op if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
33 1fce2e75 2021-08-14 op abort();
34 1fce2e75 2021-08-14 op if (!tab->buffer.page.free(&tab->buffer.page))
35 1fce2e75 2021-08-14 op abort();
36 1fce2e75 2021-08-14 op ui_on_tab_refresh(tab);
37 1fce2e75 2021-08-14 op ui_on_tab_loaded(tab);
38 1fce2e75 2021-08-14 op return 0;
39 1fce2e75 2021-08-14 op }
40 1fce2e75 2021-08-14 op
41 8c1b0837 2021-07-25 op void
42 8c1b0837 2021-07-25 op parser_init(struct tab *tab, parserfn fn)
43 8c1b0837 2021-07-25 op {
44 8c1b0837 2021-07-25 op fn(&tab->buffer.page);
45 8c1b0837 2021-07-25 op }
46 8c1b0837 2021-07-25 op
47 191b4ca2 2021-03-12 op int
48 8c1b0837 2021-07-25 op parser_parse(struct tab *tab, const char *chunk, size_t len)
49 8c1b0837 2021-07-25 op {
50 8c1b0837 2021-07-25 op return tab->buffer.page.parse(&tab->buffer.page, chunk, len);
51 8c1b0837 2021-07-25 op }
52 8c1b0837 2021-07-25 op
53 8c1b0837 2021-07-25 op int
54 8c1b0837 2021-07-25 op parser_free(struct tab *tab)
55 8c1b0837 2021-07-25 op {
56 2fd6d754 2021-07-30 op int r;
57 2fd6d754 2021-07-30 op char *tilde, *slash;
58 8c1b0837 2021-07-25 op
59 8c1b0837 2021-07-25 op r = tab->buffer.page.free(&tab->buffer.page);
60 8c1b0837 2021-07-25 op
61 8c1b0837 2021-07-25 op /* fallback to the host as title if nothing else */
62 2fd6d754 2021-07-30 op if (*tab->buffer.page.title != '\0')
63 2fd6d754 2021-07-30 op return r;
64 2fd6d754 2021-07-30 op
65 2fd6d754 2021-07-30 op if ((tilde = strstr(tab->hist_cur->h, "/~")) != NULL) {
66 2fd6d754 2021-07-30 op strlcpy(tab->buffer.page.title, tilde+1,
67 2fd6d754 2021-07-30 op sizeof(tab->buffer.page.title));
68 2fd6d754 2021-07-30 op
69 2fd6d754 2021-07-30 op if ((slash = strchr(tab->buffer.page.title, '/')) != NULL)
70 2fd6d754 2021-07-30 op *slash = '\0';
71 2fd6d754 2021-07-30 op } else
72 8c1b0837 2021-07-25 op strlcpy(tab->buffer.page.title, tab->uri.host,
73 8c1b0837 2021-07-25 op sizeof(tab->buffer.page.title));
74 8c1b0837 2021-07-25 op
75 8c1b0837 2021-07-25 op return r;
76 8c1b0837 2021-07-25 op }
77 8c1b0837 2021-07-25 op
78 8c1b0837 2021-07-25 op int
79 191b4ca2 2021-03-12 op parser_append(struct parser *p, const char *buf, size_t len)
80 191b4ca2 2021-03-12 op {
81 191b4ca2 2021-03-12 op size_t newlen;
82 191b4ca2 2021-03-12 op char *t;
83 191b4ca2 2021-03-12 op
84 191b4ca2 2021-03-12 op newlen = len + p->len;
85 191b4ca2 2021-03-12 op if ((t = calloc(1, newlen)) == NULL)
86 191b4ca2 2021-03-12 op return 0;
87 191b4ca2 2021-03-12 op memcpy(t, p->buf, p->len);
88 191b4ca2 2021-03-12 op memcpy(t + p->len, buf, len);
89 191b4ca2 2021-03-12 op free(p->buf);
90 191b4ca2 2021-03-12 op p->buf = t;
91 191b4ca2 2021-03-12 op p->len = newlen;
92 191b4ca2 2021-03-12 op return 1;
93 191b4ca2 2021-03-12 op }
94 191b4ca2 2021-03-12 op
95 191b4ca2 2021-03-12 op int
96 191b4ca2 2021-03-12 op parser_set_buf(struct parser *p, const char *buf, size_t len)
97 191b4ca2 2021-03-12 op {
98 20107775 2021-03-19 op char *tmp;
99 191b4ca2 2021-03-12 op
100 191b4ca2 2021-03-12 op if (len == 0) {
101 191b4ca2 2021-03-12 op p->len = 0;
102 20107775 2021-03-19 op free(p->buf);
103 20107775 2021-03-19 op p->buf = NULL;
104 191b4ca2 2021-03-12 op return 1;
105 191b4ca2 2021-03-12 op }
106 191b4ca2 2021-03-12 op
107 be2ee49f 2021-05-14 op /*
108 be2ee49f 2021-05-14 op * p->buf and buf can (and probably almost always will)
109 be2ee49f 2021-05-14 op * overlap!
110 be2ee49f 2021-05-14 op */
111 20107775 2021-03-19 op
112 20107775 2021-03-19 op if ((tmp = calloc(1, len)) == NULL)
113 191b4ca2 2021-03-12 op return 0;
114 20107775 2021-03-19 op memcpy(tmp, buf, len);
115 20107775 2021-03-19 op free(p->buf);
116 20107775 2021-03-19 op p->buf = tmp;
117 191b4ca2 2021-03-12 op p->len = len;
118 191b4ca2 2021-03-12 op return 1;
119 191b4ca2 2021-03-12 op }
120 a5845bb5 2021-03-20 op
121 a5845bb5 2021-03-20 op int
122 a5845bb5 2021-03-20 op parser_foreach_line(struct parser *p, const char *buf, size_t size,
123 a5845bb5 2021-03-20 op parsechunkfn fn)
124 a5845bb5 2021-03-20 op {
125 31aa9f59 2021-03-20 op char *b, *e;
126 31aa9f59 2021-03-20 op unsigned int ch;
127 31aa9f59 2021-03-20 op size_t i, l, len;
128 a5845bb5 2021-03-20 op
129 31aa9f59 2021-03-20 op if (!parser_append(p, buf, size))
130 31aa9f59 2021-03-20 op return 0;
131 31aa9f59 2021-03-20 op b = p->buf;
132 31aa9f59 2021-03-20 op len = p->len;
133 31aa9f59 2021-03-20 op
134 27dce34f 2021-07-06 op if (!(p->flags & PARSER_IN_BODY) && len < 3)
135 c0b634dd 2021-07-06 op return 1;
136 c0b634dd 2021-07-06 op
137 27dce34f 2021-07-06 op if (!(p->flags & PARSER_IN_BODY)) {
138 27dce34f 2021-07-06 op p->flags |= PARSER_IN_BODY;
139 c0b634dd 2021-07-06 op
140 c0b634dd 2021-07-06 op /*
141 c0b634dd 2021-07-06 op * drop the BOM: only UTF-8 is supported, and there
142 c0b634dd 2021-07-06 op * it's useless; some editors may still add one
143 c0b634dd 2021-07-06 op * though.
144 c0b634dd 2021-07-06 op */
145 c0b634dd 2021-07-06 op if (memmem(b, len, "\xEF\xBB\xBF", 3) == b) {
146 c0b634dd 2021-07-06 op b += 3;
147 c0b634dd 2021-07-06 op len -= 3;
148 c0b634dd 2021-07-06 op }
149 c0b634dd 2021-07-06 op }
150 c0b634dd 2021-07-06 op
151 31aa9f59 2021-03-20 op /* drop every "funny" ASCII character */
152 31aa9f59 2021-03-20 op for (i = 0; i < len; ) {
153 31aa9f59 2021-03-20 op ch = b[i];
154 31aa9f59 2021-03-20 op if ((ch >= ' ' || ch == '\n' || ch == '\t')
155 31aa9f59 2021-03-20 op && ch != 127) { /* del */
156 31aa9f59 2021-03-20 op ++i;
157 31aa9f59 2021-03-20 op continue;
158 31aa9f59 2021-03-20 op }
159 08b9feb5 2021-07-26 op memmove(&b[i], &b[i+1], len - i - 1);
160 31aa9f59 2021-03-20 op len--;
161 a5845bb5 2021-03-20 op }
162 a5845bb5 2021-03-20 op
163 a5845bb5 2021-03-20 op while (len > 0) {
164 a5845bb5 2021-03-20 op if ((e = memmem((char*)b, len, "\n", 1)) == NULL)
165 a5845bb5 2021-03-20 op break;
166 a5845bb5 2021-03-20 op l = e - b;
167 a5845bb5 2021-03-20 op
168 a5845bb5 2021-03-20 op if (!fn(p, b, l))
169 a5845bb5 2021-03-20 op return 0;
170 a5845bb5 2021-03-20 op
171 a5845bb5 2021-03-20 op len -= l;
172 a5845bb5 2021-03-20 op b += l;
173 a5845bb5 2021-03-20 op
174 a5845bb5 2021-03-20 op if (len > 0) {
175 a5845bb5 2021-03-20 op /* skip \n */
176 a5845bb5 2021-03-20 op len--;
177 a5845bb5 2021-03-20 op b++;
178 a5845bb5 2021-03-20 op }
179 a5845bb5 2021-03-20 op }
180 a5845bb5 2021-03-20 op
181 a5845bb5 2021-03-20 op return parser_set_buf(p, b, len);
182 a5845bb5 2021-03-20 op }