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 c1d27b0e 2024-06-14 op #include "telescope.h"
28 1ac119fb 2024-01-23 op #include "utils.h"
29 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
31 c1d27b0e 2024-06-14 op static int tpatch_emit_line(struct buffer *, const char *, size_t);
32 c1d27b0e 2024-06-14 op static int tpatch_parse_line(struct buffer *, const char *, size_t);
34 fd1c80ce 2024-06-14 op const struct parser textpatch_parser = {
35 c1d27b0e 2024-06-14 op .name = "text/x-patch",
36 c1d27b0e 2024-06-14 op .parseline = &tpatch_parse_line,
37 c1d27b0e 2024-06-14 op .initflags = PARSER_IN_PATCH_HDR,
41 c1d27b0e 2024-06-14 op tpatch_emit_line(struct buffer *b, const char *line, size_t linelen)
43 1ac119fb 2024-01-23 op struct line *l;
45 3d89457c 2024-06-18 thomas.ad l = xcalloc(1, sizeof(*l));
47 c1d27b0e 2024-06-14 op if (b->parser_flags & PARSER_IN_PATCH_HDR)
48 1ac119fb 2024-01-23 op l->type = LINE_PATCH_HDR;
50 1ac119fb 2024-01-23 op l->type = LINE_PATCH;
52 1ac119fb 2024-01-23 op if (linelen != 0) {
53 3d89457c 2024-06-18 thomas.ad l->line = xcalloc(1, linelen + 1);
55 1ac119fb 2024-01-23 op memcpy(l->line, line, linelen);
57 c1d27b0e 2024-06-14 op if (!(b->parser_flags & PARSER_IN_PATCH_HDR))
58 1ac119fb 2024-01-23 op switch (*l->line) {
60 1ac119fb 2024-01-23 op l->type = LINE_PATCH_ADD;
63 1ac119fb 2024-01-23 op l->type = LINE_PATCH_DEL;
66 1ac119fb 2024-01-23 op l->type = LINE_PATCH_HUNK_HDR;
69 1ac119fb 2024-01-23 op /* context lines */
73 1ac119fb 2024-01-23 op * A single patch file can have more
74 1ac119fb 2024-01-23 op * than one "header" if touches more
75 1ac119fb 2024-01-23 op * than one file.
77 1ac119fb 2024-01-23 op l->type = LINE_PATCH_HDR;
78 c1d27b0e 2024-06-14 op b->parser_flags |= PARSER_IN_PATCH_HDR;
82 1ac119fb 2024-01-23 op if (!strncmp(l->line, "+++", 3))
83 c1d27b0e 2024-06-14 op b->parser_flags &= ~PARSER_IN_PATCH_HDR;
86 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&b->head, l, lines);
92 c1d27b0e 2024-06-14 op tpatch_parse_line(struct buffer *b, const char *line, size_t linelen)
94 c1d27b0e 2024-06-14 op return tpatch_emit_line(b, line, linelen);