Blame


1 70d237df 2021-07-25 op /*
2 70d237df 2021-07-25 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 70d237df 2021-07-25 op *
4 70d237df 2021-07-25 op * Permission to use, copy, modify, and distribute this software for any
5 70d237df 2021-07-25 op * purpose with or without fee is hereby granted, provided that the above
6 70d237df 2021-07-25 op * copyright notice and this permission notice appear in all copies.
7 70d237df 2021-07-25 op *
8 70d237df 2021-07-25 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 70d237df 2021-07-25 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 70d237df 2021-07-25 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 70d237df 2021-07-25 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 70d237df 2021-07-25 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 70d237df 2021-07-25 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 70d237df 2021-07-25 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 70d237df 2021-07-25 op */
16 70d237df 2021-07-25 op
17 70d237df 2021-07-25 op /*
18 70d237df 2021-07-25 op * A streaming text/plain "parser."
19 70d237df 2021-07-25 op */
20 70d237df 2021-07-25 op
21 70d237df 2021-07-25 op #include <stdlib.h>
22 70d237df 2021-07-25 op #include <string.h>
23 70d237df 2021-07-25 op
24 70d237df 2021-07-25 op #include "telescope.h"
25 70d237df 2021-07-25 op #include "parser.h"
26 70d237df 2021-07-25 op
27 70d237df 2021-07-25 op static int textplain_parse(struct parser*, const char*, size_t);
28 70d237df 2021-07-25 op static int textplain_foreach_line(struct parser*, const char*, size_t);
29 70d237df 2021-07-25 op static int textplain_free(struct parser*);
30 70d237df 2021-07-25 op
31 70d237df 2021-07-25 op static inline int
32 d646a8f7 2021-07-25 op emit_line(struct parser *p, const char *line, size_t len)
33 70d237df 2021-07-25 op {
34 70d237df 2021-07-25 op struct line *l;
35 70d237df 2021-07-25 op
36 70d237df 2021-07-25 op if ((l = calloc(1, sizeof(*l))) == NULL)
37 70d237df 2021-07-25 op return 0;
38 70d237df 2021-07-25 op
39 04439a78 2021-07-25 op l->type = LINE_TEXT;
40 70d237df 2021-07-25 op
41 70d237df 2021-07-25 op if (len != 0) {
42 70d237df 2021-07-25 op if ((l->line = calloc(1, len+1)) == NULL) {
43 70d237df 2021-07-25 op free(l);
44 70d237df 2021-07-25 op return 0;
45 70d237df 2021-07-25 op }
46 70d237df 2021-07-25 op
47 70d237df 2021-07-25 op memcpy(l->line, line, len);
48 70d237df 2021-07-25 op }
49 70d237df 2021-07-25 op
50 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
51 70d237df 2021-07-25 op
52 70d237df 2021-07-25 op return 1;
53 70d237df 2021-07-25 op }
54 70d237df 2021-07-25 op
55 70d237df 2021-07-25 op void
56 70d237df 2021-07-25 op textplain_initparser(struct parser *p)
57 70d237df 2021-07-25 op {
58 70d237df 2021-07-25 op memset(p, 0, sizeof(*p));
59 70d237df 2021-07-25 op
60 70d237df 2021-07-25 op p->name = "text/plain";
61 70d237df 2021-07-25 op p->parse = &textplain_parse;
62 70d237df 2021-07-25 op p->free = &textplain_free;
63 78894e73 2021-08-12 op
64 78894e73 2021-08-12 op TAILQ_INIT(&p->head);
65 70d237df 2021-07-25 op }
66 70d237df 2021-07-25 op
67 70d237df 2021-07-25 op static int
68 70d237df 2021-07-25 op textplain_parse(struct parser *p, const char *buf, size_t size)
69 70d237df 2021-07-25 op {
70 70d237df 2021-07-25 op return parser_foreach_line(p, buf, size, textplain_foreach_line);
71 70d237df 2021-07-25 op }
72 70d237df 2021-07-25 op
73 70d237df 2021-07-25 op static int
74 70d237df 2021-07-25 op textplain_foreach_line(struct parser *p, const char *line, size_t linelen)
75 70d237df 2021-07-25 op {
76 d646a8f7 2021-07-25 op return emit_line(p, line, linelen);
77 70d237df 2021-07-25 op }
78 70d237df 2021-07-25 op
79 70d237df 2021-07-25 op static int
80 70d237df 2021-07-25 op textplain_free(struct parser *p)
81 70d237df 2021-07-25 op {
82 70d237df 2021-07-25 op if (p->len != 0)
83 d646a8f7 2021-07-25 op return emit_line(p, p->buf, p->len);
84 70d237df 2021-07-25 op return 1;
85 70d237df 2021-07-25 op }