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 <stdlib.h>
22 27bf601b 2021-03-12 op #include <string.h>
23 27bf601b 2021-03-12 op
24 63f1dea5 2021-07-12 op #include "telescope.h"
25 63f1dea5 2021-07-12 op #include "parser.h"
26 63f1dea5 2021-07-12 op
27 27bf601b 2021-03-12 op static int textplain_parse(struct parser*, const char*, size_t);
28 a5845bb5 2021-03-20 op static int textplain_foreach_line(struct parser*, const char*, size_t);
29 27bf601b 2021-03-12 op static int textplain_free(struct parser*);
30 27bf601b 2021-03-12 op
31 27bf601b 2021-03-12 op static inline int
32 27bf601b 2021-03-12 op emit_line(struct parser *p, enum line_type type, const char *line, size_t len)
33 27bf601b 2021-03-12 op {
34 27bf601b 2021-03-12 op struct line *l;
35 27bf601b 2021-03-12 op
36 27bf601b 2021-03-12 op if ((l = calloc(1, sizeof(*l))) == NULL)
37 27bf601b 2021-03-12 op return 0;
38 27bf601b 2021-03-12 op
39 27bf601b 2021-03-12 op l->type = type;
40 27bf601b 2021-03-12 op
41 27bf601b 2021-03-12 op if (len != 0) {
42 27bf601b 2021-03-12 op if ((l->line = calloc(1, len+1)) == NULL) {
43 27bf601b 2021-03-12 op free(l);
44 27bf601b 2021-03-12 op return 0;
45 27bf601b 2021-03-12 op }
46 27bf601b 2021-03-12 op
47 27bf601b 2021-03-12 op memcpy(l->line, line, len);
48 27bf601b 2021-03-12 op }
49 27bf601b 2021-03-12 op
50 27bf601b 2021-03-12 op if (TAILQ_EMPTY(&p->head))
51 27bf601b 2021-03-12 op TAILQ_INSERT_HEAD(&p->head, l, lines);
52 27bf601b 2021-03-12 op else
53 27bf601b 2021-03-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
54 27bf601b 2021-03-12 op
55 27bf601b 2021-03-12 op return 1;
56 27bf601b 2021-03-12 op }
57 27bf601b 2021-03-12 op
58 27bf601b 2021-03-12 op void
59 27bf601b 2021-03-12 op textplain_initparser(struct parser *p)
60 27bf601b 2021-03-12 op {
61 27bf601b 2021-03-12 op memset(p, 0, sizeof(*p));
62 27bf601b 2021-03-12 op
63 fc43eadd 2021-03-12 op p->name = "text/plain";
64 27bf601b 2021-03-12 op p->parse = &textplain_parse;
65 27bf601b 2021-03-12 op p->free = &textplain_free;
66 27bf601b 2021-03-12 op }
67 27bf601b 2021-03-12 op
68 27bf601b 2021-03-12 op static int
69 27bf601b 2021-03-12 op textplain_parse(struct parser *p, const char *buf, size_t size)
70 27bf601b 2021-03-12 op {
71 a5845bb5 2021-03-20 op return parser_foreach_line(p, buf, size, textplain_foreach_line);
72 a5845bb5 2021-03-20 op }
73 27bf601b 2021-03-12 op
74 a5845bb5 2021-03-20 op static int
75 a5845bb5 2021-03-20 op textplain_foreach_line(struct parser *p, const char *line, size_t linelen)
76 a5845bb5 2021-03-20 op {
77 a5845bb5 2021-03-20 op return emit_line(p, LINE_PRE_CONTENT, line, linelen);
78 27bf601b 2021-03-12 op }
79 27bf601b 2021-03-12 op
80 27bf601b 2021-03-12 op static int
81 27bf601b 2021-03-12 op textplain_free(struct parser *p)
82 27bf601b 2021-03-12 op {
83 a97be00d 2021-03-16 op if (p->len != 0)
84 a97be00d 2021-03-16 op return emit_line(p, LINE_PRE_CONTENT, p->buf, p->len);
85 a97be00d 2021-03-16 op return 1;
86 27bf601b 2021-03-12 op }