Blame


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