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/x-patch 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 1ac119fb 2024-01-23 op #include "utils.h"
29 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
30 1ac119fb 2024-01-23 op
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);
33 1ac119fb 2024-01-23 op
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,
38 c1d27b0e 2024-06-14 op };
39 1ac119fb 2024-01-23 op
40 1ac119fb 2024-01-23 op static int
41 c1d27b0e 2024-06-14 op tpatch_emit_line(struct buffer *b, const char *line, size_t linelen)
42 1ac119fb 2024-01-23 op {
43 1ac119fb 2024-01-23 op struct line *l;
44 1ac119fb 2024-01-23 op
45 3d89457c 2024-06-18 thomas.ad l = xcalloc(1, sizeof(*l));
46 1ac119fb 2024-01-23 op
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;
49 1ac119fb 2024-01-23 op else
50 1ac119fb 2024-01-23 op l->type = LINE_PATCH;
51 1ac119fb 2024-01-23 op
52 1ac119fb 2024-01-23 op if (linelen != 0) {
53 3d89457c 2024-06-18 thomas.ad l->line = xcalloc(1, linelen + 1);
54 1ac119fb 2024-01-23 op
55 1ac119fb 2024-01-23 op memcpy(l->line, line, linelen);
56 1ac119fb 2024-01-23 op
57 c1d27b0e 2024-06-14 op if (!(b->parser_flags & PARSER_IN_PATCH_HDR))
58 1ac119fb 2024-01-23 op switch (*l->line) {
59 1ac119fb 2024-01-23 op case '+':
60 1ac119fb 2024-01-23 op l->type = LINE_PATCH_ADD;
61 1ac119fb 2024-01-23 op break;
62 1ac119fb 2024-01-23 op case '-':
63 1ac119fb 2024-01-23 op l->type = LINE_PATCH_DEL;
64 1ac119fb 2024-01-23 op break;
65 1ac119fb 2024-01-23 op case '@':
66 1ac119fb 2024-01-23 op l->type = LINE_PATCH_HUNK_HDR;
67 1ac119fb 2024-01-23 op break;
68 1ac119fb 2024-01-23 op case ' ':
69 1ac119fb 2024-01-23 op /* context lines */
70 1ac119fb 2024-01-23 op break;
71 1ac119fb 2024-01-23 op default:
72 1ac119fb 2024-01-23 op /*
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.
76 1ac119fb 2024-01-23 op */
77 1ac119fb 2024-01-23 op l->type = LINE_PATCH_HDR;
78 c1d27b0e 2024-06-14 op b->parser_flags |= PARSER_IN_PATCH_HDR;
79 1ac119fb 2024-01-23 op break;
80 1ac119fb 2024-01-23 op }
81 1ac119fb 2024-01-23 op
82 1ac119fb 2024-01-23 op if (!strncmp(l->line, "+++", 3))
83 c1d27b0e 2024-06-14 op b->parser_flags &= ~PARSER_IN_PATCH_HDR;
84 1ac119fb 2024-01-23 op }
85 1ac119fb 2024-01-23 op
86 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&b->head, l, lines);
87 1ac119fb 2024-01-23 op
88 1ac119fb 2024-01-23 op return 1;
89 1ac119fb 2024-01-23 op }
90 1ac119fb 2024-01-23 op
91 1ac119fb 2024-01-23 op static int
92 c1d27b0e 2024-06-14 op tpatch_parse_line(struct buffer *b, const char *line, size_t linelen)
93 1ac119fb 2024-01-23 op {
94 c1d27b0e 2024-06-14 op return tpatch_emit_line(b, line, linelen);
95 1ac119fb 2024-01-23 op }