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