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 "compat.h"
23 #include <stdlib.h>
24 #include <string.h>
26 #include "parser.h"
27 #include "telescope.h"
28 #include "xwrapper.h"
30 static int textplain_parse_line(struct buffer *, const char *, size_t);
32 const struct parser textplain_parser = {
33 .name = "text/plain",
34 .parseline = &textplain_parse_line,
35 };
37 static inline int
38 emit_line(struct buffer *b, const char *line, size_t len)
39 {
40 struct line *l;
42 l = xcalloc(1, sizeof(*l));
44 l->type = LINE_TEXT;
46 if (len != 0) {
47 l->line = xcalloc(1, len + 1);
49 memcpy(l->line, line, len);
50 }
52 TAILQ_INSERT_TAIL(&b->head, l, lines);
54 return 1;
55 }
57 static int
58 textplain_parse_line(struct buffer *b, const char *line, size_t linelen)
59 {
60 return emit_line(b, line, linelen);
61 }