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