2 75a8a1ec 2022-02-08 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 75a8a1ec 2022-02-08 op * Permission to use, copy, modify, and distribute this software for any
5 75a8a1ec 2022-02-08 op * purpose with or without fee is hereby granted, provided that the above
6 75a8a1ec 2022-02-08 op * copyright notice and this permission notice appear in all copies.
8 75a8a1ec 2022-02-08 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 75a8a1ec 2022-02-08 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 75a8a1ec 2022-02-08 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 75a8a1ec 2022-02-08 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 75a8a1ec 2022-02-08 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 75a8a1ec 2022-02-08 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 75a8a1ec 2022-02-08 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 75a8a1ec 2022-02-08 op * A streaming text/x-patch parser
21 75a8a1ec 2022-02-08 op #include <stdlib.h>
22 75a8a1ec 2022-02-08 op #include <string.h>
24 75a8a1ec 2022-02-08 op #include "telescope.h"
25 75a8a1ec 2022-02-08 op #include "parser.h"
26 75a8a1ec 2022-02-08 op #include "utils.h"
28 75a8a1ec 2022-02-08 op static int tpatch_parse(struct parser *, const char *, size_t);
29 75a8a1ec 2022-02-08 op static int tpatch_emit_line(struct parser *, const char *, size_t);
30 75a8a1ec 2022-02-08 op static int tpatch_foreach_line(struct parser *, const char *, size_t);
31 75a8a1ec 2022-02-08 op static int tpatch_free(struct parser *);
34 75a8a1ec 2022-02-08 op textpatch_initparser(struct parser *p)
36 75a8a1ec 2022-02-08 op memset(p, 0, sizeof(*p));
38 75a8a1ec 2022-02-08 op p->name = "text/x-patch";
39 75a8a1ec 2022-02-08 op p->parse = &tpatch_parse;
40 75a8a1ec 2022-02-08 op p->free = &tpatch_free;
42 75a8a1ec 2022-02-08 op p->flags = PARSER_IN_PATCH_HDR;
44 75a8a1ec 2022-02-08 op TAILQ_INIT(&p->head);
48 75a8a1ec 2022-02-08 op tpatch_parse(struct parser *p, const char *buf, size_t size)
50 75a8a1ec 2022-02-08 op return parser_foreach_line(p, buf, size, tpatch_foreach_line);
54 75a8a1ec 2022-02-08 op tpatch_emit_line(struct parser *p, const char *line, size_t linelen)
56 75a8a1ec 2022-02-08 op struct line *l;
58 75a8a1ec 2022-02-08 op if ((l = calloc(1, sizeof(*l))) == NULL)
61 75a8a1ec 2022-02-08 op if (p->flags & PARSER_IN_PATCH_HDR)
62 75a8a1ec 2022-02-08 op l->type = LINE_PATCH_HDR;
64 75a8a1ec 2022-02-08 op l->type = LINE_PATCH;
66 75a8a1ec 2022-02-08 op if (linelen != 0) {
67 75a8a1ec 2022-02-08 op if ((l->line = calloc(1, linelen+1)) == NULL) {
72 75a8a1ec 2022-02-08 op memcpy(l->line, line, linelen);
74 75a8a1ec 2022-02-08 op if (!(p->flags & PARSER_IN_PATCH_HDR))
75 75a8a1ec 2022-02-08 op switch (*l->line) {
77 75a8a1ec 2022-02-08 op l->type = LINE_PATCH_ADD;
80 75a8a1ec 2022-02-08 op l->type = LINE_PATCH_DEL;
83 75a8a1ec 2022-02-08 op l->type = LINE_PATCH_HUNK_HDR;
86 75a8a1ec 2022-02-08 op /* context lines */
90 75a8a1ec 2022-02-08 op * A single patch file can have more
91 75a8a1ec 2022-02-08 op * than one "header" if touches more
92 75a8a1ec 2022-02-08 op * than one file.
94 75a8a1ec 2022-02-08 op l->type = LINE_PATCH_HDR;
95 75a8a1ec 2022-02-08 op p->flags |= PARSER_IN_PATCH_HDR;
99 d89eb764 2022-04-24 op if (!strncmp(l->line, "+++", 3))
100 75a8a1ec 2022-02-08 op p->flags &= ~PARSER_IN_PATCH_HDR;
103 75a8a1ec 2022-02-08 op TAILQ_INSERT_TAIL(&p->head, l, lines);
109 75a8a1ec 2022-02-08 op tpatch_foreach_line(struct parser *p, const char *line, size_t linelen)
111 75a8a1ec 2022-02-08 op return tpatch_emit_line(p, line, linelen);
115 75a8a1ec 2022-02-08 op tpatch_free(struct parser *p)
117 75a8a1ec 2022-02-08 op if (p->len != 0)
118 75a8a1ec 2022-02-08 op return tpatch_emit_line(p, p->buf, p->len);