Annotation of src/usr.bin/patch/pch.c, Revision 1.19
1.19 ! deraadt 1: /* $OpenBSD: pch.c,v 1.18 2003/07/21 14:27:35 deraadt Exp $ */
1.2 niklas 2:
1.1 deraadt 3: #ifndef lint
1.19 ! deraadt 4: static char rcsid[] = "$OpenBSD: pch.c,v 1.18 2003/07/21 14:27:35 deraadt Exp $";
1.1 deraadt 5: #endif /* not lint */
6:
7: #include "EXTERN.h"
8: #include "common.h"
9: #include "util.h"
10: #include "INTERN.h"
11: #include "pch.h"
12:
1.17 deraadt 13: extern bool check_only;
1.1 deraadt 14: /* Patch (diff listing) abstract type. */
15:
1.17 deraadt 16: static long p_filesize; /* size of the patch file */
17: static LINENUM p_first; /* 1st line number */
18: static LINENUM p_newfirst; /* 1st line number of replacement */
19: static LINENUM p_ptrn_lines; /* # lines in pattern */
20: static LINENUM p_repl_lines; /* # lines in replacement text */
21: static LINENUM p_end = -1; /* last line in hunk */
22: static LINENUM p_max; /* max allowed value of p_end */
23: static LINENUM p_context = 3; /* # of context lines */
24: static LINENUM p_input_line = 0; /* current line # from patch file */
25: static char **p_line = Null(char **); /* the text of the hunk */
26: static short *p_len = Null(short *); /* length of each line */
27: static char *p_char = Nullch;/* +, -, and ! */
28: static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */
29: static int p_indent; /* indent to patch */
30: static LINENUM p_base; /* where to intuit this time */
31: static LINENUM p_bline; /* line # of p_base */
32: static LINENUM p_start; /* where intuit found a patch */
33: static LINENUM p_sline; /* and the line number for it */
34: static LINENUM p_hunk_beg; /* line number of current hunk */
35: static LINENUM p_efake = -1; /* end of faked up lines--don't free */
36: static LINENUM p_bfake = -1; /* beg of faked up lines */
1.1 deraadt 37:
1.17 deraadt 38: /*
39: * Prepare to look for the next patch in the patch file.
40: */
1.1 deraadt 41: void
1.17 deraadt 42: re_patch(void)
1.1 deraadt 43: {
1.17 deraadt 44: p_first = Nulline;
45: p_newfirst = Nulline;
46: p_ptrn_lines = Nulline;
47: p_repl_lines = Nulline;
48: p_end = (LINENUM) - 1;
49: p_max = Nulline;
50: p_indent = 0;
1.1 deraadt 51: }
52:
1.17 deraadt 53: /*
54: * Open the patch file at the beginning of time.
55: */
1.1 deraadt 56: void
1.17 deraadt 57: open_patch_file(char *filename)
1.1 deraadt 58: {
1.17 deraadt 59: if (filename == Nullch || !*filename || strEQ(filename, "-")) {
60: pfp = fopen(TMPPATNAME, "w");
61: if (pfp == Nullfp)
62: pfatal("can't create %s", TMPPATNAME);
63: while (fgets(buf, sizeof buf, stdin) != Nullch)
64: fputs(buf, pfp);
65: fclose(pfp);
66: filename = TMPPATNAME;
67: }
68: pfp = fopen(filename, "r");
1.6 deraadt 69: if (pfp == Nullfp)
1.17 deraadt 70: pfatal("patch file %s not found", filename);
71: fstat(fileno(pfp), &filestat);
72: p_filesize = filestat.st_size;
73: next_intuit_at(0L, 1L); /* start at the beginning */
74: set_hunkmax();
1.1 deraadt 75: }
76:
1.17 deraadt 77: /*
78: * Make sure our dynamically realloced tables are malloced to begin with.
79: */
1.1 deraadt 80: void
1.17 deraadt 81: set_hunkmax(void)
1.1 deraadt 82: {
83: #ifndef lint
1.17 deraadt 84: if (p_line == Null(char **))
1.19 ! deraadt 85: p_line = (char **) malloc((size_t) hunkmax * sizeof(char *));
1.17 deraadt 86: if (p_len == Null(short *))
1.19 ! deraadt 87: p_len = (short *) malloc((size_t) hunkmax * sizeof(short));
1.1 deraadt 88: #endif
1.17 deraadt 89: if (p_char == Nullch)
1.19 ! deraadt 90: p_char = (char *) malloc((size_t) hunkmax * sizeof(char));
1.1 deraadt 91: }
92:
1.17 deraadt 93: /*
94: * Enlarge the arrays containing the current hunk of patch.
95: */
1.1 deraadt 96: void
1.17 deraadt 97: grow_hunkmax(void)
1.1 deraadt 98: {
1.17 deraadt 99: hunkmax *= 2;
100:
101: /*
102: * Note that on most systems, only the p_line array ever gets fresh memory
103: * since p_len can move into p_line's old space, and p_char can move into
104: * p_len's old space. Not on PDP-11's however. But it doesn't matter.
105: */
106: assert(p_line != Null(char **) &&p_len != Null(short *) &&p_char != Nullch);
1.1 deraadt 107: #ifndef lint
1.19 ! deraadt 108: p_line = (char **) realloc((char *) p_line, hunkmax * sizeof(char *));
! 109: p_len = (short *) realloc((char *) p_len, hunkmax * sizeof(short));
! 110: p_char = (char *) realloc((char *) p_char, hunkmax * sizeof(char));
1.1 deraadt 111: #endif
1.17 deraadt 112: if (p_line != Null(char **) &&p_len != Null(short *) &&p_char != Nullch)
113: return;
114: if (!using_plan_a)
115: fatal("out of memory\n");
116: out_of_mem = TRUE; /* whatever is null will be allocated again */
117: /* from within plan_a(), of all places */
1.1 deraadt 118: }
119:
120: /* True if the remainder of the patch file contains a diff of some sort. */
121:
122: bool
1.17 deraadt 123: there_is_another_patch(void)
1.1 deraadt 124: {
1.17 deraadt 125: if (p_base != 0L && p_base >= p_filesize) {
126: if (verbose)
127: say("done\n");
128: return FALSE;
129: }
1.1 deraadt 130: if (verbose)
1.17 deraadt 131: say("Hmm...");
132: diff_type = intuit_diff_type();
133: if (!diff_type) {
134: if (p_base != 0L) {
135: if (verbose)
136: say(" Ignoring the trailing garbage.\ndone\n");
137: } else
138: say(" I can't seem to find a patch in there anywhere.\n");
139: return FALSE;
1.1 deraadt 140: }
1.17 deraadt 141: if (verbose)
142: say(" %sooks like %s to me...\n",
143: (p_base == 0L ? "L" : "The next patch l"),
144: diff_type == UNI_DIFF ? "a unified diff" :
145: diff_type == CONTEXT_DIFF ? "a context diff" :
146: diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
147: diff_type == NORMAL_DIFF ? "a normal diff" :
148: "an ed script");
149: if (p_indent && verbose)
150: say("(Patch is indented %d space%s.)\n", p_indent,
151: p_indent == 1 ? "" : "s");
152: skip_to(p_start, p_sline);
153: while (filearg[0] == Nullch) {
154: if (force || batch) {
155: say("No file to patch. Skipping...\n");
156: filearg[0] = savestr(bestguess);
157: skip_rest_of_patch = TRUE;
158: return TRUE;
159: }
160: ask("File to patch: ");
161: if (*buf != '\n') {
162: if (bestguess)
163: free(bestguess);
164: bestguess = savestr(buf);
165: filearg[0] = fetchname(buf, 0, FALSE);
166: }
167: if (filearg[0] == Nullch) {
168: ask("No file found--skip this patch? [n] ");
169: if (*buf != 'y')
170: continue;
171: if (verbose)
172: say("Skipping patch...\n");
173: filearg[0] = fetchname(bestguess, 0, TRUE);
174: skip_rest_of_patch = TRUE;
175: return TRUE;
176: }
1.1 deraadt 177: }
1.17 deraadt 178: return TRUE;
1.1 deraadt 179: }
180:
181: /* Determine what kind of diff is in the remaining part of the patch file. */
182:
183: int
1.17 deraadt 184: intuit_diff_type(void)
1.1 deraadt 185: {
1.17 deraadt 186: long this_line = 0, previous_line;
187: long first_command_line = -1, fcl_line;
188: bool last_line_was_command = FALSE, this_is_a_command = FALSE;
189: bool stars_last_line = FALSE, stars_this_line = FALSE;
190: char *s, *t;
191: char *indtmp = Nullch;
192: char *oldtmp = Nullch;
193: char *newtmp = Nullch;
194: char *indname = Nullch;
195: char *oldname = Nullch;
196: char *newname = Nullch;
197: int indent, retval;
198: bool no_filearg = (filearg[0] == Nullch);
199:
200: ok_to_create_file = FALSE;
201: fseek(pfp, p_base, 0);
202: p_input_line = p_bline - 1;
203: for (;;) {
204: previous_line = this_line;
205: last_line_was_command = this_is_a_command;
206: stars_last_line = stars_this_line;
207: this_line = ftell(pfp);
208: indent = 0;
209: p_input_line++;
210: if (fgets(buf, sizeof buf, pfp) == Nullch) {
211: if (first_command_line >= 0L) {
212: /* nothing but deletes!? */
213: p_start = first_command_line;
214: p_sline = fcl_line;
215: retval = ED_DIFF;
216: goto scan_exit;
217: } else {
218: p_start = this_line;
219: p_sline = p_input_line;
220: retval = 0;
221: goto scan_exit;
222: }
223: }
224: for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
225: if (*s == '\t')
226: indent += 8 - (indent % 8);
227: else
228: indent++;
229: }
230: for (t = s; isdigit(*t) || *t == ','; t++)
231: ;
232: this_is_a_command = (isdigit(*s) &&
233: (*t == 'd' || *t == 'c' || *t == 'a'));
234: if (first_command_line < 0L && this_is_a_command) {
235: first_command_line = this_line;
236: fcl_line = p_input_line;
237: p_indent = indent; /* assume this for now */
238: }
239: if (!stars_last_line && strnEQ(s, "*** ", 4))
240: oldtmp = savestr(s + 4);
241: else if (strnEQ(s, "--- ", 4))
242: newtmp = savestr(s + 4);
243: else if (strnEQ(s, "+++ ", 4))
244: oldtmp = savestr(s + 4); /* pretend it is the old
245: * name */
246: else if (strnEQ(s, "Index:", 6))
247: indtmp = savestr(s + 6);
248: else if (strnEQ(s, "Prereq:", 7)) {
1.18 deraadt 249: for (t = s + 7; isspace(*t); t++)
250: ;
1.17 deraadt 251: revision = savestr(t);
1.18 deraadt 252: for (t = revision; *t && !isspace(*t); t++)
253: ;
1.17 deraadt 254: *t = '\0';
255: if (!*revision) {
256: free(revision);
257: revision = Nullch;
258: }
259: }
260: if ((!diff_type || diff_type == ED_DIFF) &&
261: first_command_line >= 0L &&
262: strEQ(s, ".\n")) {
263: p_indent = indent;
264: p_start = first_command_line;
265: p_sline = fcl_line;
266: retval = ED_DIFF;
267: goto scan_exit;
268: }
269: if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) {
270: if (!atol(s + 3))
271: ok_to_create_file = TRUE;
272: p_indent = indent;
273: p_start = this_line;
274: p_sline = p_input_line;
275: retval = UNI_DIFF;
276: goto scan_exit;
277: }
278: stars_this_line = strnEQ(s, "********", 8);
279: if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
280: strnEQ(s, "*** ", 4)) {
281: if (!atol(s + 4))
282: ok_to_create_file = TRUE;
283: /*
284: * if this is a new context diff the character just
285: * before
286: */
287: /* the newline is a '*'. */
288: while (*s != '\n')
289: s++;
290: p_indent = indent;
291: p_start = previous_line;
292: p_sline = p_input_line - 1;
293: retval = (*(s - 1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
294: goto scan_exit;
295: }
296: if ((!diff_type || diff_type == NORMAL_DIFF) &&
297: last_line_was_command &&
298: (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2))) {
299: p_start = previous_line;
300: p_sline = p_input_line - 1;
301: p_indent = indent;
302: retval = NORMAL_DIFF;
303: goto scan_exit;
304: }
305: }
306: scan_exit:
307: if (no_filearg) {
308: if (indtmp != Nullch)
309: indname = fetchname(indtmp, strippath, ok_to_create_file);
310: if (oldtmp != Nullch)
311: oldname = fetchname(oldtmp, strippath, ok_to_create_file);
312: if (newtmp != Nullch)
313: newname = fetchname(newtmp, strippath, ok_to_create_file);
314: if (indname)
315: filearg[0] = savestr(indname);
316: else if (oldname && newname) {
317: if (strlen(oldname) < strlen(newname))
318: filearg[0] = savestr(oldname);
319: else
320: filearg[0] = savestr(newname);
321: } else if (oldname)
322: filearg[0] = savestr(oldname);
323: else if (newname)
324: filearg[0] = savestr(newname);
325: }
326: if (bestguess) {
327: free(bestguess);
328: bestguess = Nullch;
329: }
330: if (filearg[0] != Nullch)
331: bestguess = savestr(filearg[0]);
332: else if (indtmp != Nullch)
333: bestguess = fetchname(indtmp, strippath, TRUE);
334: else {
335: if (oldtmp != Nullch)
336: oldname = fetchname(oldtmp, strippath, TRUE);
337: if (newtmp != Nullch)
338: newname = fetchname(newtmp, strippath, TRUE);
339: if (oldname && newname) {
340: if (strlen(oldname) < strlen(newname))
341: bestguess = savestr(oldname);
342: else
343: bestguess = savestr(newname);
344: } else if (oldname)
345: bestguess = savestr(oldname);
346: else if (newname)
347: bestguess = savestr(newname);
348: }
1.1 deraadt 349: if (indtmp != Nullch)
1.17 deraadt 350: free(indtmp);
1.1 deraadt 351: if (oldtmp != Nullch)
1.17 deraadt 352: free(oldtmp);
1.1 deraadt 353: if (newtmp != Nullch)
1.17 deraadt 354: free(newtmp);
355: if (indname != Nullch)
356: free(indname);
357: if (oldname != Nullch)
358: free(oldname);
359: if (newname != Nullch)
360: free(newname);
361: return retval;
1.1 deraadt 362: }
363:
1.17 deraadt 364: /*
365: * Remember where this patch ends so we know where to start up again.
366: */
1.1 deraadt 367: void
1.17 deraadt 368: next_intuit_at(long file_pos, long file_line)
1.1 deraadt 369: {
1.17 deraadt 370: p_base = file_pos;
371: p_bline = file_line;
1.1 deraadt 372: }
373:
1.17 deraadt 374: /*
375: * Basically a verbose fseek() to the actual diff listing.
376: */
1.1 deraadt 377: void
1.17 deraadt 378: skip_to(long file_pos, long file_line)
1.1 deraadt 379: {
1.17 deraadt 380: char *ret;
1.1 deraadt 381:
1.17 deraadt 382: assert(p_base <= file_pos);
383: if (verbose && p_base < file_pos) {
384: fseek(pfp, p_base, 0);
385: say("The text leading up to this was:\n--------------------------\n");
386: while (ftell(pfp) < file_pos) {
387: ret = fgets(buf, sizeof buf, pfp);
388: assert(ret != Nullch);
389: say("|%s", buf);
390: }
391: say("--------------------------\n");
392: } else
393: fseek(pfp, file_pos, 0);
394: p_input_line = file_line - 1;
1.1 deraadt 395: }
396:
397: /* Make this a function for better debugging. */
398: static void
1.16 deraadt 399: malformed(void)
1.1 deraadt 400: {
1.17 deraadt 401: fatal("malformed patch at line %ld: %s", p_input_line, buf);
402: /* about as informative as "Syntax error" in C */
1.1 deraadt 403: }
404:
1.14 otto 405: /*
406: * True if the line has been discarded (i.e. it is a line saying
407: * "\ No newline at end of file".)
408: */
409: static bool
410: remove_special_line(void)
411: {
1.17 deraadt 412: int c;
1.14 otto 413:
414: c = fgetc(pfp);
415: if (c == '\\') {
416: do {
417: c = fgetc(pfp);
418: } while (c != EOF && c != '\n');
419:
420: return TRUE;
421: }
422: if (c != EOF)
423: fseek(pfp, -1L, SEEK_CUR);
424:
425: return FALSE;
426: }
427:
1.17 deraadt 428: /*
429: * True if there is more of the current diff listing to process.
430: */
1.1 deraadt 431: bool
1.17 deraadt 432: another_hunk(void)
1.1 deraadt 433: {
1.17 deraadt 434: long line_beginning; /* file pos of the current line */
435: LINENUM repl_beginning; /* index of --- line */
436: LINENUM fillcnt; /* #lines of missing ptrn or repl */
437: LINENUM fillsrc; /* index of first line to copy */
438: LINENUM filldst; /* index of first missing line */
439: bool ptrn_spaces_eaten; /* ptrn was slightly misformed */
440: bool repl_could_be_missing; /* no + or ! lines in this hunk */
441: bool repl_missing; /* we are now backtracking */
442: long repl_backtrack_position; /* file pos of first repl line */
443: LINENUM repl_patch_line; /* input line number for same */
444: LINENUM ptrn_copiable; /* # of copiable lines in ptrn */
445: char *s, *ret;
446: int context = 0;
447:
448: while (p_end >= 0) {
449: if (p_end == p_efake)
450: p_end = p_bfake; /* don't free twice */
451: else
452: free(p_line[p_end]);
453: p_end--;
454: }
455: assert(p_end == -1);
456: p_efake = -1;
457:
458: p_max = hunkmax; /* gets reduced when --- found */
459: if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
460: line_beginning = ftell(pfp);
461: repl_beginning = 0;
462: fillcnt = 0;
463: ptrn_spaces_eaten = FALSE;
464: repl_could_be_missing = TRUE;
465: repl_missing = FALSE;
466: repl_backtrack_position = 0;
467: ptrn_copiable = 0;
468:
469: ret = pgets(buf, sizeof buf, pfp);
470: p_input_line++;
471: if (ret == Nullch || strnNE(buf, "********", 8)) {
472: next_intuit_at(line_beginning, p_input_line);
473: return FALSE;
474: }
475: p_context = 100;
476: p_hunk_beg = p_input_line + 1;
477: while (p_end < p_max) {
478: line_beginning = ftell(pfp);
479: ret = pgets(buf, sizeof buf, pfp);
480: p_input_line++;
481: if (ret == Nullch) {
482: if (p_max - p_end < 4) {
483: /* assume blank lines got chopped */
484: strlcpy(buf, " \n", sizeof buf);
485: } else {
486: if (repl_beginning && repl_could_be_missing) {
487: repl_missing = TRUE;
488: goto hunk_done;
489: }
490: fatal("unexpected end of file in patch\n");
491: }
492: }
493: p_end++;
494: assert(p_end < hunkmax);
495: p_char[p_end] = *buf;
496: p_line[p_end] = Nullch;
497: switch (*buf) {
498: case '*':
499: if (strnEQ(buf, "********", 8)) {
500: if (repl_beginning && repl_could_be_missing) {
501: repl_missing = TRUE;
502: goto hunk_done;
503: } else
504: fatal("unexpected end of hunk "
505: "at line %ld\n",
506: p_input_line);
507: }
508: if (p_end != 0) {
509: if (repl_beginning && repl_could_be_missing) {
510: repl_missing = TRUE;
511: goto hunk_done;
512: }
513: fatal("unexpected *** at line %ld: %s",
514: p_input_line, buf);
515: }
516: context = 0;
517: p_line[p_end] = savestr(buf);
518: if (out_of_mem) {
519: p_end--;
520: return FALSE;
521: }
522: for (s = buf; *s && !isdigit(*s); s++)
523: ;
524: if (!*s)
525: malformed();
526: if (strnEQ(s, "0,0", 3))
527: memmove(s, s + 2, strlen(s + 2) + 1);
528: p_first = (LINENUM) atol(s);
529: while (isdigit(*s))
530: s++;
531: if (*s == ',') {
1.18 deraadt 532: for (; *s && !isdigit(*s); s++)
533: ;
1.17 deraadt 534: if (!*s)
535: malformed();
536: p_ptrn_lines = ((LINENUM) atol(s)) - p_first + 1;
537: } else if (p_first)
538: p_ptrn_lines = 1;
539: else {
540: p_ptrn_lines = 0;
541: p_first = 1;
542: }
543: p_max = p_ptrn_lines + 6; /* we need this much at
544: * least */
545: while (p_max >= hunkmax)
546: grow_hunkmax();
547: p_max = hunkmax;
548: break;
549: case '-':
550: if (buf[1] == '-') {
551: if (repl_beginning ||
552: (p_end != p_ptrn_lines + 1 +
553: (p_char[p_end - 1] == '\n'))) {
554: if (p_end == 1) {
555: /*
556: * `old' lines were omitted;
557: * set up to fill them in
558: * from 'new' context lines.
559: */
560: p_end = p_ptrn_lines + 1;
561: fillsrc = p_end + 1;
562: filldst = 1;
563: fillcnt = p_ptrn_lines;
564: } else {
565: if (repl_beginning) {
566: if (repl_could_be_missing) {
567: repl_missing = TRUE;
568: goto hunk_done;
569: }
570: fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n",
571: p_input_line, p_hunk_beg + repl_beginning);
572: } else {
573: fatal("%s \"---\" at line %ld--check line numbers at line %ld\n",
574: (p_end <= p_ptrn_lines
575: ? "Premature"
576: : "Overdue"),
577: p_input_line, p_hunk_beg);
578: }
579: }
580: }
581: repl_beginning = p_end;
582: repl_backtrack_position = ftell(pfp);
583: repl_patch_line = p_input_line;
584: p_line[p_end] = savestr(buf);
585: if (out_of_mem) {
586: p_end--;
587: return FALSE;
588: }
589: p_char[p_end] = '=';
590: for (s = buf; *s && !isdigit(*s); s++)
591: ;
592: if (!*s)
593: malformed();
594: p_newfirst = (LINENUM) atol(s);
595: while (isdigit(*s))
596: s++;
597: if (*s == ',') {
598: for (; *s && !isdigit(*s); s++)
599: ;
600: if (!*s)
601: malformed();
602: p_repl_lines = ((LINENUM) atol(s)) -
603: p_newfirst + 1;
604: } else if (p_newfirst)
605: p_repl_lines = 1;
606: else {
607: p_repl_lines = 0;
608: p_newfirst = 1;
609: }
610: p_max = p_repl_lines + p_end;
611: if (p_max > MAXHUNKSIZE)
612: fatal("hunk too large (%ld lines) at line %ld: %s",
613: p_max, p_input_line, buf);
614: while (p_max >= hunkmax)
615: grow_hunkmax();
616: if (p_repl_lines != ptrn_copiable &&
617: (p_context != 0 || p_repl_lines != 1))
618: repl_could_be_missing = FALSE;
619: break;
620: }
621: goto change_line;
622: case '+':
623: case '!':
624: repl_could_be_missing = FALSE;
625: change_line:
626: if (buf[1] == '\n' && canonicalize)
627: strlcpy(buf + 1, " \n", sizeof buf - 1);
628: if (!isspace(buf[1]) && buf[1] != '>' &&
629: buf[1] != '<' &&
630: repl_beginning && repl_could_be_missing) {
631: repl_missing = TRUE;
632: goto hunk_done;
633: }
634: if (context >= 0) {
635: if (context < p_context)
636: p_context = context;
637: context = -1000;
638: }
639: p_line[p_end] = savestr(buf + 2);
640: if (out_of_mem) {
641: p_end--;
642: return FALSE;
643: }
644: if (p_end == p_ptrn_lines) {
645: if (remove_special_line()) {
646: int len;
647:
648: len = strlen(p_line[p_end]) - 1;
649: (p_line[p_end])[len] = 0;
650: }
651: }
652: break;
653: case '\t':
654: case '\n': /* assume the 2 spaces got eaten */
655: if (repl_beginning && repl_could_be_missing &&
656: (!ptrn_spaces_eaten ||
657: diff_type == NEW_CONTEXT_DIFF)) {
658: repl_missing = TRUE;
659: goto hunk_done;
660: }
661: p_line[p_end] = savestr(buf);
662: if (out_of_mem) {
663: p_end--;
664: return FALSE;
665: }
666: if (p_end != p_ptrn_lines + 1) {
667: ptrn_spaces_eaten |= (repl_beginning != 0);
668: context++;
669: if (!repl_beginning)
670: ptrn_copiable++;
671: p_char[p_end] = ' ';
672: }
673: break;
674: case ' ':
675: if (!isspace(buf[1]) &&
676: repl_beginning && repl_could_be_missing) {
677: repl_missing = TRUE;
678: goto hunk_done;
679: }
680: context++;
681: if (!repl_beginning)
682: ptrn_copiable++;
683: p_line[p_end] = savestr(buf + 2);
684: if (out_of_mem) {
685: p_end--;
686: return FALSE;
687: }
688: break;
689: default:
690: if (repl_beginning && repl_could_be_missing) {
691: repl_missing = TRUE;
692: goto hunk_done;
693: }
694: malformed();
695: }
696: /* set up p_len for strncmp() so we don't have to */
697: /* assume null termination */
698: if (p_line[p_end])
699: p_len[p_end] = strlen(p_line[p_end]);
700: else
701: p_len[p_end] = 0;
702: }
703:
704: hunk_done:
705: if (p_end >= 0 && !repl_beginning)
706: fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
707:
708: if (repl_missing) {
709:
710: /* reset state back to just after --- */
711: p_input_line = repl_patch_line;
712: for (p_end--; p_end > repl_beginning; p_end--)
713: free(p_line[p_end]);
714: fseek(pfp, repl_backtrack_position, 0);
715:
716: /* redundant 'new' context lines were omitted - set */
717: /* up to fill them in from the old file context */
718: if (!p_context && p_repl_lines == 1) {
719: p_repl_lines = 0;
720: p_max--;
721: }
722: fillsrc = 1;
723: filldst = repl_beginning + 1;
724: fillcnt = p_repl_lines;
725: p_end = p_max;
726: } else if (!p_context && fillcnt == 1) {
727: /* the first hunk was a null hunk with no context */
728: /* and we were expecting one line -- fix it up. */
729: while (filldst < p_end) {
730: p_line[filldst] = p_line[filldst + 1];
731: p_char[filldst] = p_char[filldst + 1];
732: p_len[filldst] = p_len[filldst + 1];
733: filldst++;
734: }
735: #if 0
736: repl_beginning--; /* this doesn't need to be
737: * fixed */
738: #endif
739: p_end--;
740: p_first++; /* do append rather than insert */
741: fillcnt = 0;
742: p_ptrn_lines = 0;
743: }
744: if (diff_type == CONTEXT_DIFF &&
745: (fillcnt || (p_first > 1 && ptrn_copiable > 2 * p_context))) {
746: if (verbose)
747: say("%s\n%s\n%s\n",
748: "(Fascinating--this is really a new-style context diff but without",
749: "the telltale extra asterisks on the *** line that usually indicate",
750: "the new style...)");
751: diff_type = NEW_CONTEXT_DIFF;
752: }
753: /* if there were omitted context lines, fill them in now */
754: if (fillcnt) {
755: p_bfake = filldst; /* remember where not to
756: * free() */
757: p_efake = filldst + fillcnt - 1;
758: while (fillcnt-- > 0) {
759: while (fillsrc <= p_end && p_char[fillsrc] != ' ')
760: fillsrc++;
761: if (fillsrc > p_end)
762: fatal("replacement text or line numbers mangled in hunk at line %ld\n",
763: p_hunk_beg);
764: p_line[filldst] = p_line[fillsrc];
765: p_char[filldst] = p_char[fillsrc];
766: p_len[filldst] = p_len[fillsrc];
767: fillsrc++;
768: filldst++;
769: }
770: while (fillsrc <= p_end && fillsrc != repl_beginning &&
771: p_char[fillsrc] != ' ')
772: fillsrc++;
773: #ifdef DEBUGGING
774: if (debug & 64)
775: printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
776: fillsrc, filldst, repl_beginning, p_end + 1);
1.1 deraadt 777: #endif
1.17 deraadt 778: assert(fillsrc == p_end + 1 || fillsrc == repl_beginning);
779: assert(filldst == p_end + 1 || filldst == repl_beginning);
1.1 deraadt 780: }
1.17 deraadt 781: if (p_line[p_end] != NULL) {
782: if (remove_special_line()) {
783: p_len[p_end] -= 1;
784: (p_line[p_end])[p_len[p_end]] = 0;
785: }
786: }
787: } else if (diff_type == UNI_DIFF) {
788: long line_beginning = ftell(pfp); /* file pos of the current line */
789: LINENUM fillsrc; /* index of old lines */
790: LINENUM filldst; /* index of new lines */
791: char ch;
792:
793: ret = pgets(buf, sizeof buf, pfp);
794: p_input_line++;
795: if (ret == Nullch || strnNE(buf, "@@ -", 4)) {
796: next_intuit_at(line_beginning, p_input_line);
797: return FALSE;
1.1 deraadt 798: }
1.17 deraadt 799: s = buf + 4;
1.1 deraadt 800: if (!*s)
1.17 deraadt 801: malformed();
1.1 deraadt 802: p_first = (LINENUM) atol(s);
1.17 deraadt 803: while (isdigit(*s))
804: s++;
805: if (*s == ',') {
806: p_ptrn_lines = (LINENUM) atol(++s);
807: while (isdigit(*s))
808: s++;
809: } else
810: p_ptrn_lines = 1;
811: if (*s == ' ')
812: s++;
813: if (*s != '+' || !*++s)
814: malformed();
815: p_newfirst = (LINENUM) atol(s);
816: while (isdigit(*s))
817: s++;
1.1 deraadt 818: if (*s == ',') {
1.17 deraadt 819: p_repl_lines = (LINENUM) atol(++s);
820: while (isdigit(*s))
821: s++;
822: } else
823: p_repl_lines = 1;
824: if (*s == ' ')
825: s++;
826: if (*s != '@')
827: malformed();
828: if (!p_ptrn_lines)
829: p_first++; /* do append rather than insert */
830: p_max = p_ptrn_lines + p_repl_lines + 1;
831: while (p_max >= hunkmax)
832: grow_hunkmax();
833: fillsrc = 1;
834: filldst = fillsrc + p_ptrn_lines;
835: p_end = filldst + p_repl_lines;
836: snprintf(buf, sizeof buf, "*** %ld,%ld ****\n", p_first,
837: p_first + p_ptrn_lines - 1);
838: p_line[0] = savestr(buf);
839: if (out_of_mem) {
840: p_end = -1;
841: return FALSE;
842: }
843: p_char[0] = '*';
844: snprintf(buf, sizeof buf, "--- %ld,%ld ----\n", p_newfirst,
845: p_newfirst + p_repl_lines - 1);
846: p_line[filldst] = savestr(buf);
847: if (out_of_mem) {
848: p_end = 0;
849: return FALSE;
1.1 deraadt 850: }
1.17 deraadt 851: p_char[filldst++] = '=';
852: p_context = 100;
853: context = 0;
854: p_hunk_beg = p_input_line + 1;
855: while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
856: line_beginning = ftell(pfp);
857: ret = pgets(buf, sizeof buf, pfp);
858: p_input_line++;
859: if (ret == Nullch) {
860: if (p_max - filldst < 3) {
861: /* assume blank lines got chopped */
862: strlcpy(buf, " \n", sizeof buf);
863: } else {
864: fatal("unexpected end of file in patch\n");
865: }
866: }
867: if (*buf == '\t' || *buf == '\n') {
868: ch = ' '; /* assume the space got eaten */
869: s = savestr(buf);
870: } else {
871: ch = *buf;
872: s = savestr(buf + 1);
873: }
874: if (out_of_mem) {
875: while (--filldst > p_ptrn_lines)
876: free(p_line[filldst]);
877: p_end = fillsrc - 1;
878: return FALSE;
879: }
880: switch (ch) {
881: case '-':
882: if (fillsrc > p_ptrn_lines) {
883: free(s);
884: p_end = filldst - 1;
885: malformed();
886: }
887: p_char[fillsrc] = ch;
888: p_line[fillsrc] = s;
889: p_len[fillsrc++] = strlen(s);
890: if (fillsrc > p_ptrn_lines) {
891: if (remove_special_line()) {
892: p_len[fillsrc - 1] -= 1;
893: s[p_len[fillsrc - 1]] = 0;
894: }
895: }
896: break;
897: case '=':
898: ch = ' ';
899: /* FALL THROUGH */
900: case ' ':
901: if (fillsrc > p_ptrn_lines) {
902: free(s);
903: while (--filldst > p_ptrn_lines)
904: free(p_line[filldst]);
905: p_end = fillsrc - 1;
906: malformed();
907: }
908: context++;
909: p_char[fillsrc] = ch;
910: p_line[fillsrc] = s;
911: p_len[fillsrc++] = strlen(s);
912: s = savestr(s);
913: if (out_of_mem) {
914: while (--filldst > p_ptrn_lines)
915: free(p_line[filldst]);
916: p_end = fillsrc - 1;
917: return FALSE;
918: }
919: /* FALL THROUGH */
920: case '+':
921: if (filldst > p_end) {
922: free(s);
923: while (--filldst > p_ptrn_lines)
924: free(p_line[filldst]);
925: p_end = fillsrc - 1;
926: malformed();
927: }
928: p_char[filldst] = ch;
929: p_line[filldst] = s;
930: p_len[filldst++] = strlen(s);
931: if (fillsrc > p_ptrn_lines) {
932: if (remove_special_line()) {
933: p_len[filldst - 1] -= 1;
934: s[p_len[filldst - 1]] = 0;
935: }
936: }
937: break;
938: default:
939: p_end = filldst;
940: malformed();
1.1 deraadt 941: }
1.17 deraadt 942: if (ch != ' ' && context > 0) {
943: if (context < p_context)
944: p_context = context;
945: context = -1000;
1.1 deraadt 946: }
1.17 deraadt 947: } /* while */
948: } else { /* normal diff--fake it up */
949: char hunk_type;
950: int i;
951: LINENUM min, max;
952: long line_beginning = ftell(pfp);
953:
954: p_context = 0;
955: ret = pgets(buf, sizeof buf, pfp);
956: p_input_line++;
957: if (ret == Nullch || !isdigit(*buf)) {
958: next_intuit_at(line_beginning, p_input_line);
1.1 deraadt 959: return FALSE;
1.17 deraadt 960: }
961: p_first = (LINENUM) atol(buf);
1.18 deraadt 962: for (s = buf; isdigit(*s); s++)
963: ;
1.17 deraadt 964: if (*s == ',') {
965: p_ptrn_lines = (LINENUM) atol(++s) - p_first + 1;
966: while (isdigit(*s))
967: s++;
968: } else
969: p_ptrn_lines = (*s != 'a');
970: hunk_type = *s;
971: if (hunk_type == 'a')
972: p_first++; /* do append rather than insert */
973: min = (LINENUM) atol(++s);
1.18 deraadt 974: for (; isdigit(*s); s++)
975: ;
1.17 deraadt 976: if (*s == ',')
977: max = (LINENUM) atol(++s);
978: else
979: max = min;
980: if (hunk_type == 'd')
981: min++;
982: p_end = p_ptrn_lines + 1 + max - min + 1;
983: if (p_end > MAXHUNKSIZE)
1.16 deraadt 984: fatal("hunk too large (%ld lines) at line %ld: %s",
1.17 deraadt 985: p_end, p_input_line, buf);
986: while (p_end >= hunkmax)
1.1 deraadt 987: grow_hunkmax();
1.17 deraadt 988: p_newfirst = min;
989: p_repl_lines = max - min + 1;
990: snprintf(buf, sizeof buf, "*** %ld,%ld\n", p_first,
991: p_first + p_ptrn_lines - 1);
992: p_line[0] = savestr(buf);
1.1 deraadt 993: if (out_of_mem) {
1.17 deraadt 994: p_end = -1;
995: return FALSE;
1.1 deraadt 996: }
1.17 deraadt 997: p_char[0] = '*';
998: for (i = 1; i <= p_ptrn_lines; i++) {
999: ret = pgets(buf, sizeof buf, pfp);
1000: p_input_line++;
1001: if (ret == Nullch)
1002: fatal("unexpected end of file in patch at line %ld\n",
1003: p_input_line);
1004: if (*buf != '<')
1005: fatal("< expected at line %ld of patch\n",
1006: p_input_line);
1007: p_line[i] = savestr(buf + 2);
1008: if (out_of_mem) {
1009: p_end = i - 1;
1010: return FALSE;
1.14 otto 1011: }
1.17 deraadt 1012: p_len[i] = strlen(p_line[i]);
1013: p_char[i] = '-';
1.14 otto 1014: }
1.17 deraadt 1015:
1016: if (remove_special_line()) {
1017: p_len[i - 1] -= 1;
1018: (p_line[i - 1])[p_len[i - 1]] = 0;
1.1 deraadt 1019: }
1.17 deraadt 1020: if (hunk_type == 'c') {
1021: ret = pgets(buf, sizeof buf, pfp);
1022: p_input_line++;
1023: if (ret == Nullch)
1024: fatal("unexpected end of file in patch at line %ld\n",
1025: p_input_line);
1026: if (*buf != '-')
1027: fatal("--- expected at line %ld of patch\n",
1028: p_input_line);
1.1 deraadt 1029: }
1.17 deraadt 1030: snprintf(buf, sizeof(buf), "--- %ld,%ld\n", min, max);
1031: p_line[i] = savestr(buf);
1.1 deraadt 1032: if (out_of_mem) {
1.17 deraadt 1033: p_end = i - 1;
1034: return FALSE;
1.1 deraadt 1035: }
1.17 deraadt 1036: p_char[i] = '=';
1037: for (i++; i <= p_end; i++) {
1038: ret = pgets(buf, sizeof buf, pfp);
1039: p_input_line++;
1040: if (ret == Nullch)
1041: fatal("unexpected end of file in patch at line %ld\n",
1042: p_input_line);
1043: if (*buf != '>')
1044: fatal("> expected at line %ld of patch\n",
1045: p_input_line);
1046: p_line[i] = savestr(buf + 2);
1047: if (out_of_mem) {
1048: p_end = i - 1;
1049: return FALSE;
1.14 otto 1050: }
1.17 deraadt 1051: p_len[i] = strlen(p_line[i]);
1052: p_char[i] = '+';
1.14 otto 1053: }
1.17 deraadt 1054:
1055: if (remove_special_line()) {
1056: p_len[i - 1] -= 1;
1057: (p_line[i - 1])[p_len[i - 1]] = 0;
1.14 otto 1058: }
1.1 deraadt 1059: }
1.17 deraadt 1060: if (reverse) /* backwards patch? */
1061: if (!pch_swap())
1062: say("Not enough memory to swap next hunk!\n");
1.1 deraadt 1063: #ifdef DEBUGGING
1.17 deraadt 1064: if (debug & 2) {
1065: int i;
1066: char special;
1067:
1068: for (i = 0; i <= p_end; i++) {
1069: if (i == p_ptrn_lines)
1070: special = '^';
1071: else
1072: special = ' ';
1073: fprintf(stderr, "%3d %c %c %s", i, p_char[i],
1074: special, p_line[i]);
1075: fflush(stderr);
1076: }
1.1 deraadt 1077: }
1078: #endif
1.17 deraadt 1079: if (p_end + 1 < hunkmax)/* paranoia reigns supreme... */
1080: p_char[p_end + 1] = '^'; /* add a stopper for apply_hunk */
1081: return TRUE;
1.1 deraadt 1082: }
1083:
1.17 deraadt 1084: /*
1085: * Input a line from the patch file, worrying about indentation.
1086: */
1087: char *
1088: pgets(char *bf, int sz, FILE *fp)
1089: {
1090: char *s, *ret = fgets(bf, sz, fp);
1091: int indent = 0;
1.1 deraadt 1092:
1.17 deraadt 1093: if (p_indent && ret != Nullch) {
1094: for (s = buf;
1095: indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X');
1096: s++) {
1097: if (*s == '\t')
1098: indent += 8 - (indent % 7);
1099: else
1100: indent++;
1101: }
1102: if (buf != s && strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
1103: fatal("buffer too small in pgets()\n");
1104: }
1105: return ret;
1.1 deraadt 1106: }
1107:
1.17 deraadt 1108: /*
1109: * Reverse the old and new portions of the current hunk.
1110: */
1.1 deraadt 1111: bool
1.17 deraadt 1112: pch_swap(void)
1.1 deraadt 1113: {
1.17 deraadt 1114: char **tp_line; /* the text of the hunk */
1115: short *tp_len; /* length of each line */
1116: char *tp_char; /* +, -, and ! */
1117: LINENUM i;
1118: LINENUM n;
1119: bool blankline = FALSE;
1120: char *s;
1121:
1122: i = p_first;
1123: p_first = p_newfirst;
1124: p_newfirst = i;
1125:
1126: /* make a scratch copy */
1127:
1128: tp_line = p_line;
1129: tp_len = p_len;
1130: tp_char = p_char;
1131: p_line = Null(char **); /* force set_hunkmax to allocate again */
1132: p_len = Null(short *);
1133: p_char = Nullch;
1134: set_hunkmax();
1135: if (p_line == Null(char **) ||p_len == Null(short *) ||p_char == Nullch) {
1.1 deraadt 1136: #ifndef lint
1.17 deraadt 1137: if (p_line == Null(char **))
1138: free((char *) p_line);
1139: p_line = tp_line;
1140: if (p_len == Null(short *))
1141: free((char *) p_len);
1142: p_len = tp_len;
1.1 deraadt 1143: #endif
1.17 deraadt 1144: if (p_char == Nullch)
1145: free((char *) p_char);
1146: p_char = tp_char;
1147: return FALSE; /* not enough memory to swap hunk! */
1148: }
1149: /* now turn the new into the old */
1150:
1.1 deraadt 1151: i = p_ptrn_lines + 1;
1.17 deraadt 1152: if (tp_char[i] == '\n') { /* account for possible blank line */
1153: blankline = TRUE;
1154: i++;
1155: }
1156: if (p_efake >= 0) { /* fix non-freeable ptr range */
1157: if (p_efake <= i)
1158: n = p_end - i + 1;
1159: else
1160: n = -i;
1161: p_efake += n;
1162: p_bfake += n;
1163: }
1164: for (n = 0; i <= p_end; i++, n++) {
1165: p_line[n] = tp_line[i];
1166: p_char[n] = tp_char[i];
1167: if (p_char[n] == '+')
1168: p_char[n] = '-';
1169: p_len[n] = tp_len[i];
1170: }
1171: if (blankline) {
1172: i = p_ptrn_lines + 1;
1173: p_line[n] = tp_line[i];
1174: p_char[n] = tp_char[i];
1175: p_len[n] = tp_len[i];
1176: n++;
1177: }
1178: assert(p_char[0] == '=');
1179: p_char[0] = '*';
1180: for (s = p_line[0]; *s; s++)
1181: if (*s == '-')
1182: *s = '*';
1183:
1184: /* now turn the old into the new */
1185:
1186: assert(tp_char[0] == '*');
1187: tp_char[0] = '=';
1188: for (s = tp_line[0]; *s; s++)
1189: if (*s == '*')
1190: *s = '-';
1191: for (i = 0; n <= p_end; i++, n++) {
1192: p_line[n] = tp_line[i];
1193: p_char[n] = tp_char[i];
1194: if (p_char[n] == '-')
1195: p_char[n] = '+';
1196: p_len[n] = tp_len[i];
1197: }
1198: assert(i == p_ptrn_lines + 1);
1199: i = p_ptrn_lines;
1200: p_ptrn_lines = p_repl_lines;
1201: p_repl_lines = i;
1.1 deraadt 1202: #ifndef lint
1.17 deraadt 1203: if (tp_line == Null(char **))
1204: free((char *) tp_line);
1205: if (tp_len == Null(short *))
1206: free((char *) tp_len);
1.1 deraadt 1207: #endif
1.17 deraadt 1208: if (tp_char == Nullch)
1209: free((char *) tp_char);
1210: return TRUE;
1.1 deraadt 1211: }
1212:
1.17 deraadt 1213: /*
1214: * Return the specified line position in the old file of the old context.
1215: */
1.1 deraadt 1216: LINENUM
1.17 deraadt 1217: pch_first(void)
1.1 deraadt 1218: {
1.17 deraadt 1219: return p_first;
1.1 deraadt 1220: }
1221:
1.17 deraadt 1222: /*
1223: * Return the number of lines of old context.
1224: */
1.1 deraadt 1225: LINENUM
1.17 deraadt 1226: pch_ptrn_lines(void)
1.1 deraadt 1227: {
1.17 deraadt 1228: return p_ptrn_lines;
1.1 deraadt 1229: }
1230:
1.17 deraadt 1231: /*
1232: * Return the probable line position in the new file of the first line.
1233: */
1.1 deraadt 1234: LINENUM
1.17 deraadt 1235: pch_newfirst(void)
1.1 deraadt 1236: {
1.17 deraadt 1237: return p_newfirst;
1.1 deraadt 1238: }
1239:
1.17 deraadt 1240: /*
1241: * Return the number of lines in the replacement text including context.
1242: */
1.1 deraadt 1243: LINENUM
1.17 deraadt 1244: pch_repl_lines(void)
1.1 deraadt 1245: {
1.17 deraadt 1246: return p_repl_lines;
1.1 deraadt 1247: }
1248:
1.17 deraadt 1249: /*
1250: * Return the number of lines in the whole hunk.
1251: */
1.1 deraadt 1252: LINENUM
1.17 deraadt 1253: pch_end(void)
1.1 deraadt 1254: {
1.17 deraadt 1255: return p_end;
1.1 deraadt 1256: }
1257:
1.17 deraadt 1258: /*
1259: * Return the number of context lines before the first changed line.
1260: */
1.1 deraadt 1261: LINENUM
1.17 deraadt 1262: pch_context(void)
1.1 deraadt 1263: {
1.17 deraadt 1264: return p_context;
1.1 deraadt 1265: }
1266:
1.17 deraadt 1267: /*
1268: * Return the length of a particular patch line.
1269: */
1.1 deraadt 1270: short
1.17 deraadt 1271: pch_line_len(LINENUM line)
1.1 deraadt 1272: {
1.17 deraadt 1273: return p_len[line];
1.1 deraadt 1274: }
1275:
1.17 deraadt 1276: /*
1277: * Return the control character (+, -, *, !, etc) for a patch line.
1278: */
1.1 deraadt 1279: char
1.17 deraadt 1280: pch_char(LINENUM line)
1.1 deraadt 1281: {
1.17 deraadt 1282: return p_char[line];
1.1 deraadt 1283: }
1284:
1.17 deraadt 1285: /*
1286: * Return a pointer to a particular patch line.
1287: */
1.1 deraadt 1288: char *
1.17 deraadt 1289: pfetch(LINENUM line)
1.1 deraadt 1290: {
1.17 deraadt 1291: return p_line[line];
1.1 deraadt 1292: }
1293:
1.17 deraadt 1294: /*
1295: * Return where in the patch file this hunk began, for error messages.
1296: */
1.1 deraadt 1297: LINENUM
1.17 deraadt 1298: pch_hunk_beg(void)
1.1 deraadt 1299: {
1.17 deraadt 1300: return p_hunk_beg;
1.1 deraadt 1301: }
1302:
1.17 deraadt 1303: /*
1304: * Apply an ed script by feeding ed itself.
1305: */
1.1 deraadt 1306: void
1.17 deraadt 1307: do_ed_script(void)
1.1 deraadt 1308: {
1.17 deraadt 1309: char *t;
1310: long beginning_of_this_line;
1311: bool this_line_is_command = FALSE;
1312: FILE *pipefp;
1313:
1314: if (!skip_rest_of_patch) {
1315: unlink(TMPOUTNAME);
1316: copy_file(filearg[0], TMPOUTNAME);
1317: if (verbose)
1318: snprintf(buf, sizeof buf, "/bin/ed %s", TMPOUTNAME);
1319: else
1320: snprintf(buf, sizeof buf, "/bin/ed - %s", TMPOUTNAME);
1321: pipefp = popen(buf, "w");
1322: }
1323: for (;;) {
1324: beginning_of_this_line = ftell(pfp);
1325: if (pgets(buf, sizeof buf, pfp) == Nullch) {
1326: next_intuit_at(beginning_of_this_line, p_input_line);
1327: break;
1328: }
1329: p_input_line++;
1.18 deraadt 1330: for (t = buf; isdigit(*t) || *t == ','; t++)
1331: ;
1.17 deraadt 1332: this_line_is_command = (isdigit(*buf) &&
1333: (*t == 'd' || *t == 'c' || *t == 'a'));
1334: if (this_line_is_command) {
1335: if (!skip_rest_of_patch)
1336: fputs(buf, pipefp);
1337: if (*t != 'd') {
1338: while (pgets(buf, sizeof buf, pfp) != Nullch) {
1339: p_input_line++;
1340: if (!skip_rest_of_patch)
1341: fputs(buf, pipefp);
1342: if (strEQ(buf, ".\n"))
1343: break;
1344: }
1345: }
1346: } else {
1347: next_intuit_at(beginning_of_this_line, p_input_line);
1.1 deraadt 1348: break;
1349: }
1350: }
1.17 deraadt 1351: if (skip_rest_of_patch)
1352: return;
1353: fprintf(pipefp, "w\n");
1354: fprintf(pipefp, "q\n");
1355: fflush(pipefp);
1356: pclose(pipefp);
1357: ignore_signals();
1358: if (!check_only) {
1359: if (move_file(TMPOUTNAME, outname) < 0) {
1360: toutkeep = TRUE;
1361: chmod(TMPOUTNAME, filemode);
1362: } else
1363: chmod(outname, filemode);
1.1 deraadt 1364: }
1.17 deraadt 1365: set_signals(1);
1.1 deraadt 1366: }