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 int
69 textplain_parse(struct parser *p, const char *buf, size_t size)
70 {
71 const char *b, *e;
72 size_t len, l;
73 int r;
75 if (p->len == 0) {
76 b = buf;
77 len = size;
78 } else {
79 if (!parser_append(p, buf, size))
80 return 0;
81 b = p->buf;
82 len = p->len;
83 }
85 while (len > 0) {
86 if ((e = telescope_strnchr((char*)b, '\n', len)) == NULL)
87 break;
88 l = e - b;
90 r = emit_line(p, LINE_PRE_CONTENT, b, l);
91 if (!r)
92 return 0;
94 len -= l;
95 b += l;
97 if (len > 0) {
98 /* skip \n */
99 len--;
100 b++;
104 return parser_set_buf(p, b, len);
107 static int
108 textplain_free(struct parser *p)
110 /* flush the buffer */
111 if (p->len != 0) {
112 if (!emit_line(p, LINE_PRE_CONTENT, p->buf, p->len))
113 return 0;
116 return emit_line(p, LINE_PRE_END, NULL, 0);