2 1ac119fb 2024-01-23 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
18 1ac119fb 2024-01-23 op * A streaming text/x-patch parser
21 b66e7d4d 2024-02-06 op #include "compat.h"
23 1ac119fb 2024-01-23 op #include <stdlib.h>
24 1ac119fb 2024-01-23 op #include <string.h>
26 1ac119fb 2024-01-23 op #include "parser.h"
27 1ac119fb 2024-01-23 op #include "utils.h"
29 1ac119fb 2024-01-23 op static int tpatch_parse(struct parser *, const char *, size_t);
30 1ac119fb 2024-01-23 op static int tpatch_emit_line(struct parser *, const char *, size_t);
31 1ac119fb 2024-01-23 op static int tpatch_foreach_line(struct parser *, const char *, size_t);
32 1ac119fb 2024-01-23 op static int tpatch_free(struct parser *);
35 1ac119fb 2024-01-23 op textpatch_initparser(struct parser *p)
37 1ac119fb 2024-01-23 op memset(p, 0, sizeof(*p));
39 1ac119fb 2024-01-23 op p->name = "text/x-patch";
40 1ac119fb 2024-01-23 op p->parse = &tpatch_parse;
41 1ac119fb 2024-01-23 op p->free = &tpatch_free;
43 1ac119fb 2024-01-23 op p->flags = PARSER_IN_PATCH_HDR;
45 1ac119fb 2024-01-23 op TAILQ_INIT(&p->head);
49 1ac119fb 2024-01-23 op tpatch_parse(struct parser *p, const char *buf, size_t size)
51 1ac119fb 2024-01-23 op return parser_foreach_line(p, buf, size, tpatch_foreach_line);
55 1ac119fb 2024-01-23 op tpatch_emit_line(struct parser *p, const char *line, size_t linelen)
57 1ac119fb 2024-01-23 op struct line *l;
59 1ac119fb 2024-01-23 op if ((l = calloc(1, sizeof(*l))) == NULL)
62 1ac119fb 2024-01-23 op if (p->flags & PARSER_IN_PATCH_HDR)
63 1ac119fb 2024-01-23 op l->type = LINE_PATCH_HDR;
65 1ac119fb 2024-01-23 op l->type = LINE_PATCH;
67 1ac119fb 2024-01-23 op if (linelen != 0) {
68 1ac119fb 2024-01-23 op if ((l->line = calloc(1, linelen+1)) == NULL) {
73 1ac119fb 2024-01-23 op memcpy(l->line, line, linelen);
75 1ac119fb 2024-01-23 op if (!(p->flags & PARSER_IN_PATCH_HDR))
76 1ac119fb 2024-01-23 op switch (*l->line) {
78 1ac119fb 2024-01-23 op l->type = LINE_PATCH_ADD;
81 1ac119fb 2024-01-23 op l->type = LINE_PATCH_DEL;
84 1ac119fb 2024-01-23 op l->type = LINE_PATCH_HUNK_HDR;
87 1ac119fb 2024-01-23 op /* context lines */
91 1ac119fb 2024-01-23 op * A single patch file can have more
92 1ac119fb 2024-01-23 op * than one "header" if touches more
93 1ac119fb 2024-01-23 op * than one file.
95 1ac119fb 2024-01-23 op l->type = LINE_PATCH_HDR;
96 1ac119fb 2024-01-23 op p->flags |= PARSER_IN_PATCH_HDR;
100 1ac119fb 2024-01-23 op if (!strncmp(l->line, "+++", 3))
101 1ac119fb 2024-01-23 op p->flags &= ~PARSER_IN_PATCH_HDR;
104 1ac119fb 2024-01-23 op TAILQ_INSERT_TAIL(&p->head, l, lines);
110 1ac119fb 2024-01-23 op tpatch_foreach_line(struct parser *p, const char *line, size_t linelen)
112 1ac119fb 2024-01-23 op return tpatch_emit_line(p, line, linelen);
116 1ac119fb 2024-01-23 op tpatch_free(struct parser *p)
118 1ac119fb 2024-01-23 op if (p->len != 0)
119 1ac119fb 2024-01-23 op return tpatch_emit_line(p, p->buf, p->len);