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 191b4ca2 2021-03-12 op int
24 191b4ca2 2021-03-12 op parser_append(struct parser *p, const char *buf, size_t len)
25 191b4ca2 2021-03-12 op {
26 191b4ca2 2021-03-12 op size_t newlen;
27 191b4ca2 2021-03-12 op char *t;
28 191b4ca2 2021-03-12 op
29 191b4ca2 2021-03-12 op newlen = len + p->len;
30 191b4ca2 2021-03-12 op if ((t = calloc(1, newlen)) == NULL)
31 191b4ca2 2021-03-12 op return 0;
32 191b4ca2 2021-03-12 op memcpy(t, p->buf, p->len);
33 191b4ca2 2021-03-12 op memcpy(t + p->len, buf, len);
34 191b4ca2 2021-03-12 op free(p->buf);
35 191b4ca2 2021-03-12 op p->buf = t;
36 191b4ca2 2021-03-12 op p->len = newlen;
37 191b4ca2 2021-03-12 op return 1;
38 191b4ca2 2021-03-12 op }
39 191b4ca2 2021-03-12 op
40 191b4ca2 2021-03-12 op int
41 191b4ca2 2021-03-12 op parser_set_buf(struct parser *p, const char *buf, size_t len)
42 191b4ca2 2021-03-12 op {
43 20107775 2021-03-19 op char *tmp;
44 191b4ca2 2021-03-12 op
45 191b4ca2 2021-03-12 op if (len == 0) {
46 191b4ca2 2021-03-12 op p->len = 0;
47 20107775 2021-03-19 op free(p->buf);
48 20107775 2021-03-19 op p->buf = NULL;
49 191b4ca2 2021-03-12 op return 1;
50 191b4ca2 2021-03-12 op }
51 191b4ca2 2021-03-12 op
52 be2ee49f 2021-05-14 op /*
53 be2ee49f 2021-05-14 op * p->buf and buf can (and probably almost always will)
54 be2ee49f 2021-05-14 op * overlap!
55 be2ee49f 2021-05-14 op */
56 20107775 2021-03-19 op
57 20107775 2021-03-19 op if ((tmp = calloc(1, len)) == NULL)
58 191b4ca2 2021-03-12 op return 0;
59 20107775 2021-03-19 op memcpy(tmp, buf, len);
60 20107775 2021-03-19 op free(p->buf);
61 20107775 2021-03-19 op p->buf = tmp;
62 191b4ca2 2021-03-12 op p->len = len;
63 191b4ca2 2021-03-12 op return 1;
64 191b4ca2 2021-03-12 op }
65 a5845bb5 2021-03-20 op
66 a5845bb5 2021-03-20 op int
67 a5845bb5 2021-03-20 op parser_foreach_line(struct parser *p, const char *buf, size_t size,
68 a5845bb5 2021-03-20 op parsechunkfn fn)
69 a5845bb5 2021-03-20 op {
70 31aa9f59 2021-03-20 op char *b, *e;
71 31aa9f59 2021-03-20 op unsigned int ch;
72 31aa9f59 2021-03-20 op size_t i, l, len;
73 a5845bb5 2021-03-20 op
74 31aa9f59 2021-03-20 op if (!parser_append(p, buf, size))
75 31aa9f59 2021-03-20 op return 0;
76 31aa9f59 2021-03-20 op b = p->buf;
77 31aa9f59 2021-03-20 op len = p->len;
78 31aa9f59 2021-03-20 op
79 27dce34f 2021-07-06 op if (!(p->flags & PARSER_IN_BODY) && len < 3)
80 c0b634dd 2021-07-06 op return 1;
81 c0b634dd 2021-07-06 op
82 27dce34f 2021-07-06 op if (!(p->flags & PARSER_IN_BODY)) {
83 27dce34f 2021-07-06 op p->flags |= PARSER_IN_BODY;
84 c0b634dd 2021-07-06 op
85 c0b634dd 2021-07-06 op /*
86 c0b634dd 2021-07-06 op * drop the BOM: only UTF-8 is supported, and there
87 c0b634dd 2021-07-06 op * it's useless; some editors may still add one
88 c0b634dd 2021-07-06 op * though.
89 c0b634dd 2021-07-06 op */
90 c0b634dd 2021-07-06 op if (memmem(b, len, "\xEF\xBB\xBF", 3) == b) {
91 c0b634dd 2021-07-06 op b += 3;
92 c0b634dd 2021-07-06 op len -= 3;
93 c0b634dd 2021-07-06 op }
94 c0b634dd 2021-07-06 op }
95 c0b634dd 2021-07-06 op
96 31aa9f59 2021-03-20 op /* drop every "funny" ASCII character */
97 31aa9f59 2021-03-20 op for (i = 0; i < len; ) {
98 31aa9f59 2021-03-20 op ch = b[i];
99 31aa9f59 2021-03-20 op if ((ch >= ' ' || ch == '\n' || ch == '\t')
100 31aa9f59 2021-03-20 op && ch != 127) { /* del */
101 31aa9f59 2021-03-20 op ++i;
102 31aa9f59 2021-03-20 op continue;
103 31aa9f59 2021-03-20 op }
104 31aa9f59 2021-03-20 op memmove(&b[i], &b[i+1], len - i);
105 31aa9f59 2021-03-20 op len--;
106 a5845bb5 2021-03-20 op }
107 a5845bb5 2021-03-20 op
108 a5845bb5 2021-03-20 op while (len > 0) {
109 a5845bb5 2021-03-20 op if ((e = memmem((char*)b, len, "\n", 1)) == NULL)
110 a5845bb5 2021-03-20 op break;
111 a5845bb5 2021-03-20 op l = e - b;
112 a5845bb5 2021-03-20 op
113 a5845bb5 2021-03-20 op if (!fn(p, b, l))
114 a5845bb5 2021-03-20 op return 0;
115 a5845bb5 2021-03-20 op
116 a5845bb5 2021-03-20 op len -= l;
117 a5845bb5 2021-03-20 op b += l;
118 a5845bb5 2021-03-20 op
119 a5845bb5 2021-03-20 op if (len > 0) {
120 a5845bb5 2021-03-20 op /* skip \n */
121 a5845bb5 2021-03-20 op len--;
122 a5845bb5 2021-03-20 op b++;
123 a5845bb5 2021-03-20 op }
124 a5845bb5 2021-03-20 op }
125 a5845bb5 2021-03-20 op
126 a5845bb5 2021-03-20 op return parser_set_buf(p, b, len);
127 a5845bb5 2021-03-20 op }