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