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->parse = &textplain_parse;
62 p->free = &textplain_free;
64 emit_line(p, LINE_PRE_START, NULL, 0);
65 }
67 static inline int
68 append(struct parser *p, const char *buf, size_t len)
69 {
70 size_t newlen;
71 char *t;
73 newlen = len + p->len;
74 if ((t = calloc(1, newlen)) == NULL)
75 return 0;
76 memcpy(t, p->buf, p->len);
77 memcpy(t + p->len, buf, len);
78 free(p->buf);
79 p->buf = t;
80 p->len = newlen;
81 return 1;
82 }
84 static inline int
85 set_buf(struct parser *p, const char *buf, size_t len)
86 {
87 free(p->buf);
88 p->buf = NULL;
90 if (len == 0) {
91 p->len = 0;
92 return 1;
93 }
95 if ((p->buf = calloc(1, len)) == NULL)
96 return 0;
97 memcpy(p->buf, buf, len);
98 p->len = len;
99 return 1;
102 static int
103 textplain_parse(struct parser *p, const char *buf, size_t size)
105 const char *b, *e;
106 size_t len, l;
107 int r;
109 if (p->len == 0) {
110 b = buf;
111 len = size;
112 } else {
113 if (!append(p, buf, size))
114 return 0;
115 b = p->buf;
116 len = p->len;
119 while (len > 0) {
120 if ((e = telescope_strnchr((char*)b, '\n', len)) == NULL)
121 break;
122 l = e - b;
124 r = emit_line(p, LINE_PRE_CONTENT, b, l);
125 if (!r)
126 return 0;
128 len -= l;
129 b += l;
131 if (len > 0) {
132 /* skip \n */
133 len--;
134 b++;
138 return set_buf(p, b, len);
141 static int
142 textplain_free(struct parser *p)
144 /* flush the buffer */
145 if (p->len != 0) {
146 if (!emit_line(p, LINE_PRE_CONTENT, p->buf, p->len))
147 return 0;
150 return emit_line(p, LINE_PRE_END, NULL, 0);