Blame


1 27bf601b 2021-03-12 op /*
2 27bf601b 2021-03-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 27bf601b 2021-03-12 op *
4 27bf601b 2021-03-12 op * Permission to use, copy, modify, and distribute this software for any
5 27bf601b 2021-03-12 op * purpose with or without fee is hereby granted, provided that the above
6 27bf601b 2021-03-12 op * copyright notice and this permission notice appear in all copies.
7 27bf601b 2021-03-12 op *
8 27bf601b 2021-03-12 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 27bf601b 2021-03-12 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 27bf601b 2021-03-12 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 27bf601b 2021-03-12 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 27bf601b 2021-03-12 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 27bf601b 2021-03-12 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 27bf601b 2021-03-12 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 27bf601b 2021-03-12 op */
16 27bf601b 2021-03-12 op
17 27bf601b 2021-03-12 op /*
18 27bf601b 2021-03-12 op * A streaming text/plain "parser."
19 27bf601b 2021-03-12 op */
20 27bf601b 2021-03-12 op
21 27bf601b 2021-03-12 op #include "telescope.h"
22 27bf601b 2021-03-12 op
23 27bf601b 2021-03-12 op #include <stdlib.h>
24 27bf601b 2021-03-12 op #include <string.h>
25 27bf601b 2021-03-12 op
26 27bf601b 2021-03-12 op static int textplain_parse(struct parser*, const char*, size_t);
27 27bf601b 2021-03-12 op static int textplain_free(struct parser*);
28 27bf601b 2021-03-12 op
29 27bf601b 2021-03-12 op static inline int
30 27bf601b 2021-03-12 op emit_line(struct parser *p, enum line_type type, const char *line, size_t len)
31 27bf601b 2021-03-12 op {
32 27bf601b 2021-03-12 op struct line *l;
33 27bf601b 2021-03-12 op
34 27bf601b 2021-03-12 op if ((l = calloc(1, sizeof(*l))) == NULL)
35 27bf601b 2021-03-12 op return 0;
36 27bf601b 2021-03-12 op
37 27bf601b 2021-03-12 op l->type = type;
38 27bf601b 2021-03-12 op
39 27bf601b 2021-03-12 op if (len != 0) {
40 27bf601b 2021-03-12 op if ((l->line = calloc(1, len+1)) == NULL) {
41 27bf601b 2021-03-12 op free(l);
42 27bf601b 2021-03-12 op return 0;
43 27bf601b 2021-03-12 op }
44 27bf601b 2021-03-12 op
45 27bf601b 2021-03-12 op memcpy(l->line, line, len);
46 27bf601b 2021-03-12 op }
47 27bf601b 2021-03-12 op
48 27bf601b 2021-03-12 op if (TAILQ_EMPTY(&p->head))
49 27bf601b 2021-03-12 op TAILQ_INSERT_HEAD(&p->head, l, lines);
50 27bf601b 2021-03-12 op else
51 27bf601b 2021-03-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
52 27bf601b 2021-03-12 op
53 27bf601b 2021-03-12 op return 1;
54 27bf601b 2021-03-12 op }
55 27bf601b 2021-03-12 op
56 27bf601b 2021-03-12 op void
57 27bf601b 2021-03-12 op textplain_initparser(struct parser *p)
58 27bf601b 2021-03-12 op {
59 27bf601b 2021-03-12 op memset(p, 0, sizeof(*p));
60 27bf601b 2021-03-12 op
61 fc43eadd 2021-03-12 op p->name = "text/plain";
62 27bf601b 2021-03-12 op p->parse = &textplain_parse;
63 27bf601b 2021-03-12 op p->free = &textplain_free;
64 27bf601b 2021-03-12 op
65 27bf601b 2021-03-12 op emit_line(p, LINE_PRE_START, NULL, 0);
66 27bf601b 2021-03-12 op }
67 27bf601b 2021-03-12 op
68 27bf601b 2021-03-12 op static inline int
69 27bf601b 2021-03-12 op append(struct parser *p, const char *buf, size_t len)
70 27bf601b 2021-03-12 op {
71 27bf601b 2021-03-12 op size_t newlen;
72 27bf601b 2021-03-12 op char *t;
73 27bf601b 2021-03-12 op
74 27bf601b 2021-03-12 op newlen = len + p->len;
75 27bf601b 2021-03-12 op if ((t = calloc(1, newlen)) == NULL)
76 27bf601b 2021-03-12 op return 0;
77 27bf601b 2021-03-12 op memcpy(t, p->buf, p->len);
78 27bf601b 2021-03-12 op memcpy(t + p->len, buf, len);
79 27bf601b 2021-03-12 op free(p->buf);
80 27bf601b 2021-03-12 op p->buf = t;
81 27bf601b 2021-03-12 op p->len = newlen;
82 27bf601b 2021-03-12 op return 1;
83 27bf601b 2021-03-12 op }
84 27bf601b 2021-03-12 op
85 27bf601b 2021-03-12 op static inline int
86 27bf601b 2021-03-12 op set_buf(struct parser *p, const char *buf, size_t len)
87 27bf601b 2021-03-12 op {
88 27bf601b 2021-03-12 op free(p->buf);
89 27bf601b 2021-03-12 op p->buf = NULL;
90 27bf601b 2021-03-12 op
91 27bf601b 2021-03-12 op if (len == 0) {
92 27bf601b 2021-03-12 op p->len = 0;
93 27bf601b 2021-03-12 op return 1;
94 27bf601b 2021-03-12 op }
95 27bf601b 2021-03-12 op
96 27bf601b 2021-03-12 op if ((p->buf = calloc(1, len)) == NULL)
97 27bf601b 2021-03-12 op return 0;
98 27bf601b 2021-03-12 op memcpy(p->buf, buf, len);
99 27bf601b 2021-03-12 op p->len = len;
100 27bf601b 2021-03-12 op return 1;
101 27bf601b 2021-03-12 op }
102 27bf601b 2021-03-12 op
103 27bf601b 2021-03-12 op static int
104 27bf601b 2021-03-12 op textplain_parse(struct parser *p, const char *buf, size_t size)
105 27bf601b 2021-03-12 op {
106 27bf601b 2021-03-12 op const char *b, *e;
107 27bf601b 2021-03-12 op size_t len, l;
108 27bf601b 2021-03-12 op int r;
109 27bf601b 2021-03-12 op
110 27bf601b 2021-03-12 op if (p->len == 0) {
111 27bf601b 2021-03-12 op b = buf;
112 27bf601b 2021-03-12 op len = size;
113 27bf601b 2021-03-12 op } else {
114 27bf601b 2021-03-12 op if (!append(p, buf, size))
115 27bf601b 2021-03-12 op return 0;
116 27bf601b 2021-03-12 op b = p->buf;
117 27bf601b 2021-03-12 op len = p->len;
118 27bf601b 2021-03-12 op }
119 27bf601b 2021-03-12 op
120 27bf601b 2021-03-12 op while (len > 0) {
121 27bf601b 2021-03-12 op if ((e = telescope_strnchr((char*)b, '\n', len)) == NULL)
122 27bf601b 2021-03-12 op break;
123 27bf601b 2021-03-12 op l = e - b;
124 27bf601b 2021-03-12 op
125 27bf601b 2021-03-12 op r = emit_line(p, LINE_PRE_CONTENT, b, l);
126 27bf601b 2021-03-12 op if (!r)
127 27bf601b 2021-03-12 op return 0;
128 27bf601b 2021-03-12 op
129 27bf601b 2021-03-12 op len -= l;
130 27bf601b 2021-03-12 op b += l;
131 27bf601b 2021-03-12 op
132 27bf601b 2021-03-12 op if (len > 0) {
133 27bf601b 2021-03-12 op /* skip \n */
134 27bf601b 2021-03-12 op len--;
135 27bf601b 2021-03-12 op b++;
136 27bf601b 2021-03-12 op }
137 27bf601b 2021-03-12 op }
138 27bf601b 2021-03-12 op
139 27bf601b 2021-03-12 op return set_buf(p, b, len);
140 27bf601b 2021-03-12 op }
141 27bf601b 2021-03-12 op
142 27bf601b 2021-03-12 op static int
143 27bf601b 2021-03-12 op textplain_free(struct parser *p)
144 27bf601b 2021-03-12 op {
145 27bf601b 2021-03-12 op /* flush the buffer */
146 27bf601b 2021-03-12 op if (p->len != 0) {
147 27bf601b 2021-03-12 op if (!emit_line(p, LINE_PRE_CONTENT, p->buf, p->len))
148 27bf601b 2021-03-12 op return 0;
149 27bf601b 2021-03-12 op }
150 27bf601b 2021-03-12 op
151 27bf601b 2021-03-12 op return emit_line(p, LINE_PRE_END, NULL, 0);
152 27bf601b 2021-03-12 op }