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