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 c1d27b0e 2024-06-14 op #include "telescope.h"
28 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
29 1ac119fb 2024-01-23 op
30 c1d27b0e 2024-06-14 op static int textplain_parse_line(struct buffer *, const char *, size_t);
31 1ac119fb 2024-01-23 op
32 fd1c80ce 2024-06-14 op const struct parser textplain_parser = {
33 c1d27b0e 2024-06-14 op .name = "text/plain",
34 c1d27b0e 2024-06-14 op .parseline = &textplain_parse_line,
35 c1d27b0e 2024-06-14 op };
36 c1d27b0e 2024-06-14 op
37 1ac119fb 2024-01-23 op static inline int
38 c1d27b0e 2024-06-14 op emit_line(struct buffer *b, const char *line, size_t len)
39 1ac119fb 2024-01-23 op {
40 1ac119fb 2024-01-23 op struct line *l;
41 1ac119fb 2024-01-23 op
42 3d89457c 2024-06-18 thomas.ad l = xcalloc(1, sizeof(*l));
43 1ac119fb 2024-01-23 op
44 1ac119fb 2024-01-23 op l->type = LINE_TEXT;
45 1ac119fb 2024-01-23 op
46 1ac119fb 2024-01-23 op if (len != 0) {
47 3d89457c 2024-06-18 thomas.ad l->line = xcalloc(1, len + 1);
48 1ac119fb 2024-01-23 op
49 1ac119fb 2024-01-23 op memcpy(l->line, line, len);
50 1ac119fb 2024-01-23 op }
51 1ac119fb 2024-01-23 op
52 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&b->head, l, lines);
53 1ac119fb 2024-01-23 op
54 1ac119fb 2024-01-23 op return 1;
55 1ac119fb 2024-01-23 op }
56 1ac119fb 2024-01-23 op
57 1ac119fb 2024-01-23 op static int
58 c1d27b0e 2024-06-14 op textplain_parse_line(struct buffer *b, const char *line, size_t linelen)
59 1ac119fb 2024-01-23 op {
60 c1d27b0e 2024-06-14 op return emit_line(b, line, linelen);
61 1ac119fb 2024-01-23 op }