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#define FOR_patch
48#include "toys.h"
49
50GLOBALS(
51 char *i, *d;
52 long p, g;
53
54 struct double_list *current_hunk;
55 long oldline, oldlen, newline, newlen;
56 long linenum;
57 int context, state, filein, fileout, filepatch, hunknum;
58 char *tempname;
59)
60
61
62
63
64
65
66
67
68static void do_line(void *data)
69{
70 struct double_list *dlist = (struct double_list *)data;
71
72 if (TT.state>1 && *dlist->data != TT.state) {
73 char *s = dlist->data+(TT.state>3);
74 int i = TT.state == 2 ? 2 : TT.fileout;
75
76 xwrite(i, s, strlen(s));
77 xwrite(i, "\n", 1);
78 }
79
80 if (FLAG(x)) fprintf(stderr, "DO %d: %s\n", TT.state, dlist->data);
81
82 free(dlist->data);
83 free(data);
84}
85
86static void finish_oldfile(void)
87{
88 if (TT.tempname) replace_tempfile(TT.filein, TT.fileout, &TT.tempname);
89 TT.fileout = TT.filein = -1;
90}
91
92static void fail_hunk(void)
93{
94 if (!TT.current_hunk) return;
95
96 fprintf(stderr, "Hunk %d FAILED %ld/%ld.\n",
97 TT.hunknum, TT.oldline, TT.newline);
98 toys.exitval = 1;
99
100
101
102
103 TT.state = 2;
104 llist_traverse(TT.current_hunk, do_line);
105 TT.current_hunk = NULL;
106 if (!FLAG(dry_run)) delete_tempfile(TT.filein, TT.fileout, &TT.tempname);
107 TT.state = 0;
108}
109
110
111static int loosecmp(char *aa, char *bb)
112{
113 int a = 0, b = 0;
114
115 for (;;) {
116 while (isspace(aa[a])) a++;
117 while (isspace(bb[b])) b++;
118 if (aa[a] != bb[b]) return 1;
119 if (!aa[a]) return 0;
120 a++, b++;
121 }
122}
123
124
125
126
127
128
129
130
131static int apply_one_hunk(void)
132{
133 struct double_list *plist, *buf = NULL, *check;
134 int matcheof, trailing = 0, reverse = FLAG(R), backwarn = 0;
135 int (*lcmp)(char *aa, char *bb);
136
137 lcmp = FLAG(l) ? (void *)loosecmp : (void *)strcmp;
138 dlist_terminate(TT.current_hunk);
139
140
141 for (plist = TT.current_hunk; plist; plist = plist->next) {
142 if (plist->data[0]==' ') trailing++;
143 else trailing = 0;
144 if (FLAG(x)) fprintf(stderr, "HUNK:%s\n", plist->data);
145 }
146 matcheof = !trailing || trailing < TT.context;
147
148 if (FLAG(x)) fprintf(stderr,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N');
149
150
151
152
153 plist = TT.current_hunk;
154 buf = NULL;
155
156 for (;;) {
157 char *data = get_line(TT.filein);
158
159 TT.linenum++;
160
161
162 while (plist && *plist->data == "+-"[reverse]) {
163 if (data && !lcmp(data, plist->data+1))
164 if (!backwarn) backwarn = TT.linenum;
165 plist = plist->next;
166 }
167
168
169 if (!data) {
170 if (FLAG(x)) fprintf(stderr, "INEOF\n");
171
172
173 if (!plist && matcheof) break;
174
175 if (backwarn && !FLAG(s))
176 fprintf(stderr, "Possibly reversed hunk %d at %ld\n",
177 TT.hunknum, TT.linenum);
178
179
180 fail_hunk();
181 goto done;
182 } else if (FLAG(x)) fprintf(stderr, "IN: %s\n", data);
183 check = dlist_add(&buf, data);
184
185
186
187
188
189
190
191
192
193
194 for (;;) {
195 if (!plist || lcmp(check->data, plist->data+1)) {
196
197
198
199 if (FLAG(x)) {
200 int bug = 0;
201
202 if (!plist) fprintf(stderr, "NULL plist\n");
203 else {
204 while (plist->data[bug] == check->data[bug]) bug++;
205 fprintf(stderr, "NOT(%d:%d!=%d): %s\n", bug, plist->data[bug],
206 check->data[bug], plist->data);
207 }
208 }
209
210
211 if (!TT.context || trailing>TT.context) {
212 fail_hunk();
213 goto done;
214 }
215
216 TT.state = 3;
217 do_line(check = dlist_pop(&buf));
218 plist = TT.current_hunk;
219
220
221
222 if (!buf) break;
223 check = buf;
224 } else {
225 if (FLAG(x)) fprintf(stderr, "MAYBE: %s\n", plist->data);
226
227 plist = plist->next;
228 if (!plist && !matcheof) goto out;
229 check = check->next;
230 if (check == buf) break;
231 }
232 }
233 }
234out:
235
236 TT.state = "-+"[reverse];
237 llist_traverse(TT.current_hunk, do_line);
238 TT.current_hunk = NULL;
239 TT.state = 1;
240done:
241 if (buf) {
242 dlist_terminate(buf);
243 llist_traverse(buf, do_line);
244 }
245
246 return TT.state;
247}
248
249
250char *unquote_file(char *filename) {
251 char *s = filename, *result, *t, *u;
252 int quote = 0, ch;
253
254
255 result = xmalloc(strlen(filename) + 1);
256 t = result;
257 if (*s == '"') {
258 s++;
259 quote = 1;
260 }
261 for (; *s && !(quote && *s == '"' && !s[1]); s++) {
262
263 if (quote && *s == '\\' && s[1]) {
264 if (s[1] >= '0' && s[1] < '8') {
265 *t++ = strtoul(s + 1, &u, 8);
266 s = u - 1;
267 } else {
268 ch = unescape(s[1]);
269 *t++ = ch ? ch : s[1];
270 s++;
271 }
272 } else *t++ = *s;
273 }
274 *t = 0;
275 return result;
276}
277
278
279
280
281
282
283
284
285
286void patch_main(void)
287{
288 int reverse = FLAG(R), state = 0, patchlinenum = 0, strip = 0;
289 char *oldname = NULL, *newname = NULL;
290
291 if (TT.i) TT.filepatch = xopenro(TT.i);
292 TT.filein = TT.fileout = -1;
293
294 if (TT.d) xchdir(TT.d);
295
296
297 for (;;) {
298 char *patchline;
299
300 patchline = get_line(TT.filepatch);
301 if (!patchline) break;
302
303
304 if (strip || !patchlinenum++) {
305 int len = strlen(patchline);
306 if (patchline[len-1] == '\r') {
307 if (!strip && !FLAG(s)) fprintf(stderr, "Removing DOS newlines\n");
308 strip = 1;
309 patchline[len-1]=0;
310 }
311 }
312 if (!*patchline) {
313 free(patchline);
314 patchline = xstrdup(" ");
315 }
316
317
318 if (state >= 2) {
319 if (*patchline==' ' || *patchline=='+' || *patchline=='-') {
320 dlist_add(&TT.current_hunk, patchline);
321
322 if (*patchline != '+') TT.oldlen--;
323 if (*patchline != '-') TT.newlen--;
324
325
326 if (*patchline==' ' && state==2) TT.context++;
327 else state=3;
328
329
330 if (!TT.oldlen && !TT.newlen) state = apply_one_hunk();
331 continue;
332 }
333 dlist_terminate(TT.current_hunk);
334 fail_hunk();
335 state = 0;
336 continue;
337 }
338
339
340 if (!strncmp("--- ", patchline, 4) || !strncmp("+++ ", patchline, 4)) {
341 char *s, **name = &oldname;
342 int i;
343
344 if (*patchline == '+') {
345 name = &newname;
346 state = 1;
347 }
348
349 free(*name);
350 finish_oldfile();
351
352
353 for (s = patchline+4; *s && *s!='\t'; s++);
354 i = atoi(s);
355 if (i>1900 && i<=1970) *name = xstrdup("/dev/null");
356 else {
357 *s = 0;
358 *name = unquote_file(patchline+4);
359 }
360
361
362
363
364
365
366
367
368 } else if (state == 1 && !strncmp("@@ -", patchline, 4)) {
369 int i;
370 char *s = patchline+4;
371
372
373
374 TT.oldlen = TT.newlen = 1;
375 TT.oldline = strtol(s, &s, 10);
376 if (*s == ',') TT.oldlen=strtol(s+1, &s, 10);
377 TT.newline = strtol(s+2, &s, 10);
378 if (*s == ',') TT.newlen = strtol(s+1, &s, 10);
379
380 TT.context = 0;
381 state = 2;
382
383
384 if (TT.filein == -1) {
385 int oldsum, newsum, del = 0;
386 char *name;
387
388 oldsum = TT.oldline + TT.oldlen;
389 newsum = TT.newline + TT.newlen;
390
391 name = reverse ? oldname : newname;
392
393
394
395 if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum))
396 {
397 name = reverse ? newname : oldname;
398 del++;
399 }
400
401
402 for (i = 0, s = name; *s;) {
403 if (FLAG(p) && TT.p == i) break;
404 if (*s++ != '/') continue;
405 while (*s == '/') s++;
406 name = s;
407 i++;
408 }
409
410 if (del) {
411 if (!FLAG(s)) printf("removing %s\n", name);
412 xunlink(name);
413 state = 0;
414
415 } else if (!FLAG(p) || i <= TT.p) {
416
417 if ((!strcmp(oldname, "/dev/null") || !oldsum) && access(name, F_OK))
418 {
419 if (!FLAG(s)) printf("creating %s\n", name);
420 if (mkpath(name)) perror_exit("mkpath %s", name);
421 TT.filein = xcreate(name, O_CREAT|O_EXCL|O_RDWR, 0666);
422 } else {
423 if (!FLAG(s)) printf("patching %s\n", name);
424 TT.filein = xopenro(name);
425 }
426 if (FLAG(dry_run)) TT.fileout = xopen("/dev/null", O_RDWR);
427 else TT.fileout = copy_tempfile(TT.filein, name, &TT.tempname);
428 TT.linenum = 0;
429 TT.hunknum = 0;
430 }
431 }
432
433 TT.hunknum++;
434
435 continue;
436 }
437
438
439 free(patchline);
440 }
441
442 finish_oldfile();
443
444 if (CFG_TOYBOX_FREE) {
445 close(TT.filepatch);
446 free(oldname);
447 free(newname);
448 }
449}
450