-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse.c
331 lines (306 loc) · 6.7 KB
/
parse.c
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* wiggle - apply rejected patches
*
* Copyright (C) 2003-2013 Neil Brown <[email protected]>
* Copyright (C) 2014-2020 Neil Brown <[email protected]>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.
*
* Author: Neil Brown
* Email: <[email protected]>
*/
/*
* Parse a patch file to find the names of the different
* files to patch and record which parts of the patch
* file applies to which target file.
*/
#include "wiggle.h"
#include <unistd.h>
#include <fcntl.h>
/* determine how much we need to stripe of the front of
* paths to find them from current directory. This is
* used to guess correct '-p' value.
*/
static int get_strip(char *file)
{
int fd;
int strip = 0;
while (file && *file) {
fd = open(file, O_RDONLY);
if (fd >= 0) {
close(fd);
return strip;
}
strip++;
file = strchr(file, '/');
if (file)
while (*file == '/')
file++;
}
return -1;
}
int wiggle_set_prefix(struct plist *pl, int n, int strip)
{
int i;
for (i = 0; i < 4 && i < n && strip < 0; i++)
strip = get_strip(pl[i].file);
if (strip < 0) {
fprintf(stderr, "%s: Cannot find files to patch: please specify --strip\n",
wiggle_Cmd);
return 0;
}
for (i = 0; i < n; i++) {
char *p = pl[i].file;
int j;
for (j = 0; j < strip; j++) {
if (p)
p = strchr(p, '/');
while (p && *p == '/')
p++;
}
if (p == NULL) {
fprintf(stderr, "%s: cannot strip %d segments from %s\n",
wiggle_Cmd, strip, pl[i].file);
return 0;
}
memmove(pl[i].file, p, strlen(p)+1);
}
return 1;
}
static int pl_cmp(const void *av, const void *bv)
{
const struct plist *a = av;
const struct plist *b = bv;
return strcmp(a->file, b->file);
}
static int common_depth(char *a, char *b)
{
/* find number of path segments that these two have
* in common
*/
int depth = 0;
while (1) {
char *c;
int al, bl;
c = strchr(a, '/');
if (c)
al = c-a;
else
al = strlen(a);
c = strchr(b, '/');
if (c)
bl = c-b;
else
bl = strlen(b);
if (al == 0 || al != bl || strncmp(a, b, al) != 0)
return depth;
a += al;
while (*a == '/')
a++;
b += bl;
while (*b == '/')
b++;
depth++;
}
}
static struct plist *patch_add_file(struct plist *pl, int *np, char *file,
unsigned int start, unsigned int end)
{
/* size of pl is 0, 16, n^2 */
int n = *np;
int asize;
while (*file == '/')
/* leading '/' are bad... */
memmove(file, file+1, strlen(file));
if (n == 0)
asize = 0;
else if (n <= 16)
asize = 16;
else if ((n&(n-1)) == 0)
asize = n;
else
asize = n+1; /* not accurate, but not too large */
if (asize <= n) {
/* need to extend array */
struct plist *npl;
if (asize < 16)
asize = 16;
else
asize += asize;
npl = realloc(pl, asize * sizeof(struct plist));
if (!npl) {
fprintf(stderr, "realloc failed - skipping %s\n", file);
return pl;
}
pl = npl;
}
memset(&pl[n], 0, sizeof(pl[n]));
pl[n].file = file;
pl[n].start = start;
pl[n].end = end;
pl[n].last = pl[n].next = pl[n].prev = pl[n].parent = -1;
pl[n].conflicts = 100;
pl[n].open = 1;
*np = n+1;
return pl;
}
static struct plist *add_dir(struct plist *pl, int *np, char *file, char *curr)
{
/* any parent of file that is not a parent of curr
* needs to be added to pl
*/
int d = common_depth(file, curr);
char *buf = curr;
while (d) {
char *c = strchr(file, '/');
int l;
if (c)
l = c-file;
else
l = strlen(file);
file += l;
curr += l;
while (*file == '/')
file++;
while (*curr == '/')
curr++;
d--;
}
while (*file) {
if (curr > buf && curr[-1] != '/')
*curr++ = '/';
while (*file && *file != '/')
*curr++ = *file++;
while (*file == '/')
file++;
*curr = '\0';
if (*file)
pl = patch_add_file(pl, np, strdup(buf),
0, 0);
}
return pl;
}
struct plist *wiggle_sort_patches(struct plist *pl, int *np)
{
/* sort the patches, add directory names, and re-sort */
char curr[1024];
char *prev;
int parents[100];
int prevnode[100];
int i, n;
qsort(pl, *np, sizeof(struct plist), pl_cmp);
curr[0] = 0;
n = *np;
for (i = 0; i < n; i++)
pl = add_dir(pl, np, pl[i].file, curr);
qsort(pl, *np, sizeof(struct plist), pl_cmp);
/* array is now stable, so set up parent pointers */
n = *np;
curr[0] = 0;
prevnode[0] = -1;
prev = "";
for (i = 0; i < n; i++) {
int d = common_depth(prev, pl[i].file);
if (d == 0)
pl[i].parent = -1;
else {
pl[i].parent = parents[d-1];
pl[pl[i].parent].last = i;
}
pl[i].prev = prevnode[d];
if (pl[i].prev > -1)
pl[pl[i].prev].next = i;
prev = pl[i].file;
parents[d] = i;
prevnode[d] = i;
prevnode[d+1] = -1;
}
return pl;
}
struct plist *wiggle_parse_patch(FILE *f, FILE *of, int *np)
{
/* read a multi-file patch from 'f' and record relevant
* details in a plist.
* if 'of' >= 0, fd might not be seekable so we write
* to 'of' and use lseek on 'of' to determine position
*/
struct plist *plist = NULL;
*np = 0;
while (!feof(f)) {
/* first, find the start of a patch: "\n+++ "
* grab the file name and scan to the end of a line
*/
char *target = "\n+++ ";
char *target2 = "\n--- ";
char *pos = target;
int c = EOF;
char name[1024];
unsigned start, end;
while (*pos && (c = fgetc(f)) != EOF) {
if (of)
fputc(c, of);
if (c == *pos)
pos++;
else
pos = target;
}
if (c == EOF)
break;
assert(c == ' ');
/* now read a file name */
pos = name;
while ((c = fgetc(f)) != EOF
&& c != '\t' && c != '\n' && c != ' ' &&
pos - name < 1023) {
*pos++ = c;
if (of)
fputc(c, of);
}
*pos = 0;
if (c == EOF)
break;
if (of)
fputc(c, of);
while (c != '\n' && (c = fgetc(f)) != EOF)
if (of)
fputc(c, of);
start = ftell(of ? of : f);
if (c == EOF)
break;
/* now skip to end - "\n--- " */
pos = target2+1;
while (*pos && (c = fgetc(f)) != EOF) {
if (of)
fputc(c, of);
if (c == *pos)
pos++;
else
pos = target2;
}
end = ftell(of ? of : f);
if (pos > target2)
end -= (pos - target2) - 1;
plist = patch_add_file(plist, np,
strdup(name), start, end);
}
return plist;
}
void wiggle_plist_free(struct plist *pl, int num)
{
int i;
for (i = 0; i < num ; i++)
free(pl[i].file);
free(pl);
}