aboutsummaryrefslogtreecommitdiffstats
path: root/libpatch_parse_ed_patch.c
blob: f3dec51b021f4fcaf0238be7e747abeaeda2ee58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* See LICENSE file for copyright and license details. */
#include "common.h"


static size_t
hunk_head(const char *text, size_t len, size_t *first, size_t *last)
{
	size_t off = 1, r;
	off += r = libpatch_get_zu__(&text[off], len - off, first);
	if (!r)
		return 0;
	if (off < len && text[off] == ',') {
		off += 1;
		off += r = libpatch_get_zu__(&text[off], len - off, last);
		if (!r)
			return 0;
	} else {
		*last = *first;
	}
	return off;
}


int
libpatch_parse_ed_patch(const char *text, size_t textlen, size_t *textend,
                        struct libpatch_patch **patch, size_t *patchlen)
{
	const char *patchtext = text;
	size_t len = 0, size = 0, off, line, last, count;
	int in_hunk = 0, can_continue = 0, has_patch = 0;
	size_t *hunks = NULL, *new;
	size_t nhunks = 0, hunks_size = 0;
	struct libpatch_patch *patchcopy = NULL;
	size_t patchcopysize = 0;

	*patch = NULL;
	*patchlen = 0;

	while ((len = libpatch_next_line__(&text, &textlen, len))) {
		if (can_continue) {
			can_continue = 0;
			if (len == 1 && text[0] == 'a') {
				in_hunk = 1;
			} else if (len == 1 && text[0] == 'i') {
				goto garbage; /* TODO */
			} else if (len >= 5 && !strncmp(text, "s/", 2) && text[len - 1] == '/') {
				if (*patchlen < 2 || !IS_LINE((*patch)[*patchlen - 2].type))
					goto garbage;
				/* TODO add support for more weird alternatives */
				if (!(len == 5 && !strncmp(text, "s/.//", len)) &&
				    !(len == 6 && !strncmp(text, "s/^.//", len)) &&
				    !(len == 6 && !strncmp(text, "s/\\.//", len)) &&
				    !(len == 7 && !strncmp(text, "s/^\\.//", len)))
					goto garbage;
				(*patch)[*patchlen - 2].text_offset += 1;
				(*patch)[*patchlen - 2].text_length -= 1;
				APPEND_STRING(len, LIBPATCH_PATCH_SYNTACTICAL);
			} else {
				goto else_if;
			}
			APPEND_STRING(len, LIBPATCH_PATCH_SYNTACTICAL);

		} else else_if: if (in_hunk && len == 1 && text[0] == '.') {
			APPEND_STRING(len, LIBPATCH_PATCH_SYNTACTICAL);
			in_hunk = 0;
			can_continue = 1;

		} else if (in_hunk) {
			APPEND_STRING(0, LIBPATCH_PATCH_FILE2_ONLY);
			(*patch)[*patchlen - 1].file1_lineno = line;

		} else if (len >= 5 && !strncmp(text, "diff ", 5)) {
			if (has_patch && textend)
				break;
			if (libpatch_reverse_hunks__(*patch, *patchlen, hunks, nhunks, &patchcopy, &patchcopysize))
				goto fail;
			nhunks = 0;
			APPEND_STRING(5, LIBPATCH_PATCH_DIFF_LINE);

		} else if (len >= 2 && isdigit(text[0])) {
			off = hunk_head(text, len, &line, &last);
			if (len - off != 1)
				goto garbage;
			if (!line && last)
				goto garbage;
			else if (!line)
				count = 0, line = 1;
			else if (last < line)
				goto garbage;
			else
				count = last - line + 1;

			if (text[off] == 'd') {
				APPEND_NO_TEXT(LIBPATCH_PATCH_HUNK_WITH_DELETION);
				(*patch)[*patchlen - 1].first_line = line;
				(*patch)[*patchlen - 1].line_count = count;
			} else if (text[off] == 'a') {
				APPEND_NO_TEXT(LIBPATCH_PATCH_HUNK);
				in_hunk = 1;
			} else if (text[off] == 'c') {
				APPEND_NO_TEXT(LIBPATCH_PATCH_HUNK_WITH_DELETION);
				(*patch)[*patchlen - 1].first_line = line;
				(*patch)[*patchlen - 1].line_count = count;
				in_hunk = 1;
			} else if (text[off] == 'i') {
				goto garbage; /* TODO */
			} else {
				goto garbage;
			}

			if (nhunks == hunks_size) {
				if (hunks_size > SIZE_MAX / sizeof(*hunks) - 16)
					goto enomem;
				new = realloc(hunks, (hunks_size += 16) * sizeof(*hunks));
				if (!new)
					goto fail;
				hunks = new;
			}
			hunks[nhunks++] = *patchlen - 1;
			has_patch = 1;

		} else {
		garbage:
			APPEND_STRING(0, LIBPATCH_PATCH_GARBAGE);
			if (len == textlen)
				(*patch)[*patchlen - 1].lf_terminated = 0;
		}
	}

	if (libpatch_reverse_hunks__(*patch, *patchlen, hunks, nhunks, &patchcopy, &patchcopysize))
		goto fail;

	if (textend)
		*textend = (size_t)(text - patchtext);

	free(patchcopy);
	free(hunks);
	return 0;

enomem:
	errno = ENOMEM;
fail:
	free(patchcopy);
	free(hunks);
	free(*patch);
	*patch = NULL;
	*patchlen = 0;
	return -1;
}