Blob


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