Annotation of src/usr.bin/patch/pch.c, Revision 1.15
1.15 ! otto 1: /* $OpenBSD: pch.c,v 1.14 2003/07/16 18:09:20 otto Exp $ */
1.2 niklas 2:
1.1 deraadt 3: #ifndef lint
1.15 ! otto 4: static char rcsid[] = "$OpenBSD: pch.c,v 1.14 2003/07/16 18:09:20 otto 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:
1.14 otto 407: /*
408: * True if the line has been discarded (i.e. it is a line saying
409: * "\ No newline at end of file".)
410: */
411: static bool
412: remove_special_line(void)
413: {
414: int c;
415:
416: c = fgetc(pfp);
417: if (c == '\\') {
418: do {
419: c = fgetc(pfp);
420: } while (c != EOF && c != '\n');
421:
422: return TRUE;
423: }
424:
425: if (c != EOF)
426: fseek(pfp, -1L, SEEK_CUR);
427:
428: return FALSE;
429: }
430:
1.1 deraadt 431: /* True if there is more of the current diff listing to process. */
432:
433: bool
434: another_hunk()
435: {
436: Reg1 char *s;
437: Reg8 char *ret;
438: Reg2 int context = 0;
439:
440: while (p_end >= 0) {
441: if (p_end == p_efake)
442: p_end = p_bfake; /* don't free twice */
443: else
444: free(p_line[p_end]);
445: p_end--;
446: }
447: assert(p_end == -1);
448: p_efake = -1;
449:
450: p_max = hunkmax; /* gets reduced when --- found */
451: if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
452: long line_beginning = ftell(pfp);
453: /* file pos of the current line */
454: LINENUM repl_beginning = 0; /* index of --- line */
455: Reg4 LINENUM fillcnt = 0; /* #lines of missing ptrn or repl */
456: Reg5 LINENUM fillsrc; /* index of first line to copy */
457: Reg6 LINENUM filldst; /* index of first missing line */
458: bool ptrn_spaces_eaten = FALSE; /* ptrn was slightly misformed */
459: Reg9 bool repl_could_be_missing = TRUE;
460: /* no + or ! lines in this hunk */
461: bool repl_missing = FALSE; /* we are now backtracking */
462: long repl_backtrack_position = 0;
463: /* file pos of first repl line */
464: LINENUM repl_patch_line; /* input line number for same */
465: Reg7 LINENUM ptrn_copiable = 0;
466: /* # of copiable lines in ptrn */
467:
468: ret = pgets(buf, sizeof buf, pfp);
469: p_input_line++;
470: if (ret == Nullch || strnNE(buf, "********", 8)) {
471: next_intuit_at(line_beginning,p_input_line);
472: return FALSE;
473: }
474: p_context = 100;
475: p_hunk_beg = p_input_line + 1;
476: while (p_end < p_max) {
477: line_beginning = ftell(pfp);
478: ret = pgets(buf, sizeof buf, pfp);
479: p_input_line++;
480: if (ret == Nullch) {
1.12 deraadt 481: if (p_max - p_end < 4) {
482: /* assume blank lines got chopped */
483: strlcpy(buf, " \n", sizeof buf);
484: } else {
1.1 deraadt 485: if (repl_beginning && repl_could_be_missing) {
486: repl_missing = TRUE;
487: goto hunk_done;
488: }
489: fatal1("unexpected end of file in patch\n");
490: }
491: }
492: p_end++;
493: assert(p_end < hunkmax);
494: p_char[p_end] = *buf;
495: #ifdef zilog
496: p_line[(short)p_end] = Nullch;
497: #else
498: p_line[p_end] = Nullch;
499: #endif
500: switch (*buf) {
501: case '*':
502: if (strnEQ(buf, "********", 8)) {
503: if (repl_beginning && repl_could_be_missing) {
504: repl_missing = TRUE;
505: goto hunk_done;
506: }
507: else
508: fatal2("unexpected end of hunk at line %ld\n",
509: p_input_line);
510: }
511: if (p_end != 0) {
512: if (repl_beginning && repl_could_be_missing) {
513: repl_missing = TRUE;
514: goto hunk_done;
515: }
516: fatal3("unexpected *** at line %ld: %s", p_input_line, buf);
517: }
518: context = 0;
519: p_line[p_end] = savestr(buf);
520: if (out_of_mem) {
521: p_end--;
522: return FALSE;
523: }
524: for (s=buf; *s && !isdigit(*s); s++) ;
525: if (!*s)
526: malformed ();
527: if (strnEQ(s,"0,0",3))
1.13 deraadt 528: memmove(s, s+2, strlen(s+2)+1);
1.1 deraadt 529: p_first = (LINENUM) atol(s);
530: while (isdigit(*s)) s++;
531: if (*s == ',') {
532: for (; *s && !isdigit(*s); s++) ;
533: if (!*s)
534: malformed ();
535: p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
536: }
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 least */
544: while (p_max >= hunkmax)
545: grow_hunkmax();
546: p_max = hunkmax;
547: break;
548: case '-':
549: if (buf[1] == '-') {
550: if (repl_beginning ||
551: (p_end != p_ptrn_lines + 1 + (p_char[p_end-1] == '\n')))
552: {
553: if (p_end == 1) {
554: /* `old' lines were omitted - set up to fill */
555: /* them in from 'new' context lines. */
556: p_end = p_ptrn_lines + 1;
557: fillsrc = p_end + 1;
558: filldst = 1;
559: fillcnt = p_ptrn_lines;
560: }
561: else {
562: if (repl_beginning) {
563: if (repl_could_be_missing){
564: repl_missing = TRUE;
565: goto hunk_done;
566: }
567: fatal3(
568: "duplicate \"---\" at line %ld--check line numbers at line %ld\n",
569: p_input_line, p_hunk_beg + repl_beginning);
570: }
571: else {
572: fatal4(
573: "%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: if (!*s)
592: malformed ();
593: p_newfirst = (LINENUM) atol(s);
594: while (isdigit(*s)) s++;
595: if (*s == ',') {
596: for (; *s && !isdigit(*s); s++) ;
597: if (!*s)
598: malformed ();
599: p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
600: }
601: else if (p_newfirst)
602: p_repl_lines = 1;
603: else {
604: p_repl_lines = 0;
605: p_newfirst = 1;
606: }
607: p_max = p_repl_lines + p_end;
608: if (p_max > MAXHUNKSIZE)
609: fatal4("hunk too large (%ld lines) at line %ld: %s",
610: p_max, p_input_line, buf);
611: while (p_max >= hunkmax)
612: grow_hunkmax();
613: if (p_repl_lines != ptrn_copiable
614: && (p_context != 0 || p_repl_lines != 1))
615: repl_could_be_missing = FALSE;
616: break;
617: }
618: goto change_line;
619: case '+': case '!':
620: repl_could_be_missing = FALSE;
621: change_line:
622: if (buf[1] == '\n' && canonicalize)
1.12 deraadt 623: strlcpy(buf+1," \n", sizeof buf -1);
1.1 deraadt 624: if (!isspace(buf[1]) && buf[1] != '>' && buf[1] != '<' &&
625: repl_beginning && repl_could_be_missing) {
626: repl_missing = TRUE;
627: goto hunk_done;
628: }
629: if (context >= 0) {
630: if (context < p_context)
631: p_context = context;
632: context = -1000;
633: }
634: p_line[p_end] = savestr(buf+2);
635: if (out_of_mem) {
636: p_end--;
637: return FALSE;
638: }
1.14 otto 639: if (p_end == p_ptrn_lines) {
640: if (remove_special_line()) {
641: int len;
642:
643: len = strlen(p_line[p_end]) - 1;
644: (p_line[p_end])[len] = 0;
645: }
646: }
1.1 deraadt 647: break;
648: case '\t': case '\n': /* assume the 2 spaces got eaten */
649: if (repl_beginning && repl_could_be_missing &&
650: (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
651: repl_missing = TRUE;
652: goto hunk_done;
653: }
654: p_line[p_end] = savestr(buf);
655: if (out_of_mem) {
656: p_end--;
657: return FALSE;
658: }
659: if (p_end != p_ptrn_lines + 1) {
660: ptrn_spaces_eaten |= (repl_beginning != 0);
661: context++;
662: if (!repl_beginning)
663: ptrn_copiable++;
664: p_char[p_end] = ' ';
665: }
666: break;
667: case ' ':
668: if (!isspace(buf[1]) &&
669: repl_beginning && repl_could_be_missing) {
670: repl_missing = TRUE;
671: goto hunk_done;
672: }
673: context++;
674: if (!repl_beginning)
675: ptrn_copiable++;
676: p_line[p_end] = savestr(buf+2);
677: if (out_of_mem) {
678: p_end--;
679: return FALSE;
680: }
681: break;
682: default:
683: if (repl_beginning && repl_could_be_missing) {
684: repl_missing = TRUE;
685: goto hunk_done;
686: }
687: malformed ();
688: }
689: /* set up p_len for strncmp() so we don't have to */
690: /* assume null termination */
691: if (p_line[p_end])
692: p_len[p_end] = strlen(p_line[p_end]);
693: else
694: p_len[p_end] = 0;
695: }
696:
697: hunk_done:
698: if (p_end >=0 && !repl_beginning)
699: fatal2("no --- found in patch at line %ld\n", pch_hunk_beg());
700:
701: if (repl_missing) {
702:
703: /* reset state back to just after --- */
704: p_input_line = repl_patch_line;
705: for (p_end--; p_end > repl_beginning; p_end--)
706: free(p_line[p_end]);
707: Fseek(pfp, repl_backtrack_position, 0);
708:
709: /* redundant 'new' context lines were omitted - set */
710: /* up to fill them in from the old file context */
711: if (!p_context && p_repl_lines == 1) {
712: p_repl_lines = 0;
713: p_max--;
714: }
715: fillsrc = 1;
716: filldst = repl_beginning+1;
717: fillcnt = p_repl_lines;
718: p_end = p_max;
719: }
720: else if (!p_context && fillcnt == 1) {
721: /* the first hunk was a null hunk with no context */
722: /* and we were expecting one line -- fix it up. */
723: while (filldst < p_end) {
724: p_line[filldst] = p_line[filldst+1];
725: p_char[filldst] = p_char[filldst+1];
726: p_len[filldst] = p_len[filldst+1];
727: filldst++;
728: }
729: #if 0
730: repl_beginning--; /* this doesn't need to be fixed */
731: #endif
732: p_end--;
733: p_first++; /* do append rather than insert */
734: fillcnt = 0;
735: p_ptrn_lines = 0;
736: }
737:
738: if (diff_type == CONTEXT_DIFF &&
739: (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
740: if (verbose)
741: say4("%s\n%s\n%s\n",
742: "(Fascinating--this is really a new-style context diff but without",
743: "the telltale extra asterisks on the *** line that usually indicate",
744: "the new style...)");
745: diff_type = NEW_CONTEXT_DIFF;
746: }
747:
748: /* if there were omitted context lines, fill them in now */
749: if (fillcnt) {
750: p_bfake = filldst; /* remember where not to free() */
751: p_efake = filldst + fillcnt - 1;
752: while (fillcnt-- > 0) {
753: while (fillsrc <= p_end && p_char[fillsrc] != ' ')
754: fillsrc++;
755: if (fillsrc > p_end)
756: fatal2("replacement text or line numbers mangled in hunk at line %ld\n",
757: p_hunk_beg);
758: p_line[filldst] = p_line[fillsrc];
759: p_char[filldst] = p_char[fillsrc];
760: p_len[filldst] = p_len[fillsrc];
761: fillsrc++; filldst++;
762: }
763: while (fillsrc <= p_end && fillsrc != repl_beginning &&
764: p_char[fillsrc] != ' ')
765: fillsrc++;
766: #ifdef DEBUGGING
767: if (debug & 64)
768: printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
769: fillsrc,filldst,repl_beginning,p_end+1);
770: #endif
771: assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
772: assert(filldst==p_end+1 || filldst==repl_beginning);
773: }
1.14 otto 774: if (p_line[p_end] != NULL) {
775: if (remove_special_line()) {
776: p_len[p_end] -= 1;
777: (p_line[p_end])[p_len[p_end]] = 0;
778: }
779: }
1.1 deraadt 780: }
781: else if (diff_type == UNI_DIFF) {
782: long line_beginning = ftell(pfp);
783: /* file pos of the current line */
784: Reg4 LINENUM fillsrc; /* index of old lines */
785: Reg5 LINENUM filldst; /* index of new lines */
786: char ch;
787:
788: ret = pgets(buf, sizeof buf, pfp);
789: p_input_line++;
790: if (ret == Nullch || strnNE(buf, "@@ -", 4)) {
791: next_intuit_at(line_beginning,p_input_line);
792: return FALSE;
793: }
794: s = buf+4;
795: if (!*s)
796: malformed ();
797: p_first = (LINENUM) atol(s);
798: while (isdigit(*s)) s++;
799: if (*s == ',') {
800: p_ptrn_lines = (LINENUM) atol(++s);
801: while (isdigit(*s)) s++;
802: } else
803: p_ptrn_lines = 1;
804: if (*s == ' ') s++;
805: if (*s != '+' || !*++s)
806: malformed ();
807: p_newfirst = (LINENUM) atol(s);
808: while (isdigit(*s)) s++;
809: if (*s == ',') {
810: p_repl_lines = (LINENUM) atol(++s);
811: while (isdigit(*s)) s++;
812: } else
813: p_repl_lines = 1;
814: if (*s == ' ') s++;
815: if (*s != '@')
816: malformed ();
817: if (!p_ptrn_lines)
818: p_first++; /* do append rather than insert */
819: p_max = p_ptrn_lines + p_repl_lines + 1;
820: while (p_max >= hunkmax)
821: grow_hunkmax();
822: fillsrc = 1;
823: filldst = fillsrc + p_ptrn_lines;
824: p_end = filldst + p_repl_lines;
1.7 millert 825: Snprintf(buf, sizeof buf, "*** %ld,%ld ****\n", p_first,
826: p_first + p_ptrn_lines - 1);
1.1 deraadt 827: p_line[0] = savestr(buf);
828: if (out_of_mem) {
829: p_end = -1;
830: return FALSE;
831: }
832: p_char[0] = '*';
1.7 millert 833: Snprintf(buf, sizeof buf, "--- %ld,%ld ----\n", p_newfirst,
834: p_newfirst + p_repl_lines - 1);
1.1 deraadt 835: p_line[filldst] = savestr(buf);
836: if (out_of_mem) {
837: p_end = 0;
838: return FALSE;
839: }
840: p_char[filldst++] = '=';
841: p_context = 100;
842: context = 0;
843: p_hunk_beg = p_input_line + 1;
844: while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
845: line_beginning = ftell(pfp);
846: ret = pgets(buf, sizeof buf, pfp);
847: p_input_line++;
848: if (ret == Nullch) {
1.12 deraadt 849: if (p_max - filldst < 3) {
850: /* assume blank lines got chopped */
851: strlcpy(buf, " \n", sizeof buf);
852: } else {
1.1 deraadt 853: fatal1("unexpected end of file in patch\n");
854: }
855: }
856: if (*buf == '\t' || *buf == '\n') {
857: ch = ' '; /* assume the space got eaten */
858: s = savestr(buf);
859: }
860: else {
861: ch = *buf;
862: s = savestr(buf+1);
863: }
864: if (out_of_mem) {
865: while (--filldst > p_ptrn_lines)
866: free(p_line[filldst]);
867: p_end = fillsrc-1;
868: return FALSE;
869: }
870: switch (ch) {
871: case '-':
872: if (fillsrc > p_ptrn_lines) {
873: free(s);
874: p_end = filldst-1;
875: malformed ();
876: }
877: p_char[fillsrc] = ch;
878: p_line[fillsrc] = s;
879: p_len[fillsrc++] = strlen(s);
1.14 otto 880: if (fillsrc > p_ptrn_lines) {
881: if (remove_special_line()) {
882: p_len[fillsrc - 1] -= 1;
883: s[p_len[fillsrc - 1]] = 0;
884: }
885: }
1.1 deraadt 886: break;
887: case '=':
888: ch = ' ';
889: /* FALL THROUGH */
890: case ' ':
891: if (fillsrc > p_ptrn_lines) {
892: free(s);
893: while (--filldst > p_ptrn_lines)
894: free(p_line[filldst]);
895: p_end = fillsrc-1;
896: malformed ();
897: }
898: context++;
899: p_char[fillsrc] = ch;
900: p_line[fillsrc] = s;
901: p_len[fillsrc++] = strlen(s);
902: s = savestr(s);
903: if (out_of_mem) {
904: while (--filldst > p_ptrn_lines)
905: free(p_line[filldst]);
906: p_end = fillsrc-1;
907: return FALSE;
908: }
909: /* FALL THROUGH */
910: case '+':
911: if (filldst > p_end) {
912: free(s);
913: while (--filldst > p_ptrn_lines)
914: free(p_line[filldst]);
915: p_end = fillsrc-1;
916: malformed ();
917: }
918: p_char[filldst] = ch;
919: p_line[filldst] = s;
920: p_len[filldst++] = strlen(s);
1.14 otto 921: if (fillsrc > p_ptrn_lines) {
922: if (remove_special_line()) {
1.15 ! otto 923: p_len[filldst - 1] -= 1;
! 924: s[p_len[filldst - 1]] = 0;
1.14 otto 925: }
926: }
1.1 deraadt 927: break;
928: default:
929: p_end = filldst;
930: malformed ();
931: }
932: if (ch != ' ' && context > 0) {
933: if (context < p_context)
934: p_context = context;
935: context = -1000;
936: }
937: }/* while */
938: }
939: else { /* normal diff--fake it up */
940: char hunk_type;
941: Reg3 int i;
942: LINENUM min, max;
943: long line_beginning = ftell(pfp);
944:
945: p_context = 0;
946: ret = pgets(buf, sizeof buf, pfp);
947: p_input_line++;
948: if (ret == Nullch || !isdigit(*buf)) {
949: next_intuit_at(line_beginning,p_input_line);
950: return FALSE;
951: }
952: p_first = (LINENUM)atol(buf);
953: for (s=buf; isdigit(*s); s++) ;
954: if (*s == ',') {
955: p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
956: while (isdigit(*s)) s++;
957: }
958: else
959: p_ptrn_lines = (*s != 'a');
960: hunk_type = *s;
961: if (hunk_type == 'a')
962: p_first++; /* do append rather than insert */
963: min = (LINENUM)atol(++s);
964: for (; isdigit(*s); s++) ;
965: if (*s == ',')
966: max = (LINENUM)atol(++s);
967: else
968: max = min;
969: if (hunk_type == 'd')
970: min++;
971: p_end = p_ptrn_lines + 1 + max - min + 1;
972: if (p_end > MAXHUNKSIZE)
973: fatal4("hunk too large (%ld lines) at line %ld: %s",
974: p_end, p_input_line, buf);
975: while (p_end >= hunkmax)
976: grow_hunkmax();
977: p_newfirst = min;
978: p_repl_lines = max - min + 1;
1.7 millert 979: Snprintf(buf, sizeof buf, "*** %ld,%ld\n", p_first,
980: p_first + p_ptrn_lines - 1);
1.1 deraadt 981: p_line[0] = savestr(buf);
982: if (out_of_mem) {
983: p_end = -1;
984: return FALSE;
985: }
986: p_char[0] = '*';
987: for (i=1; i<=p_ptrn_lines; i++) {
988: ret = pgets(buf, sizeof buf, pfp);
989: p_input_line++;
990: if (ret == Nullch)
991: fatal2("unexpected end of file in patch at line %ld\n",
992: p_input_line);
993: if (*buf != '<')
994: fatal2("< expected at line %ld of patch\n", p_input_line);
995: p_line[i] = savestr(buf+2);
996: if (out_of_mem) {
997: p_end = i-1;
998: return FALSE;
999: }
1000: p_len[i] = strlen(p_line[i]);
1001: p_char[i] = '-';
1002: }
1.14 otto 1003:
1004: if (remove_special_line()) {
1005: p_len[i - 1] -= 1;
1006: (p_line[i - 1])[p_len[i - 1]] = 0;
1007: }
1008:
1.1 deraadt 1009: if (hunk_type == 'c') {
1010: ret = pgets(buf, sizeof buf, pfp);
1011: p_input_line++;
1012: if (ret == Nullch)
1013: fatal2("unexpected end of file in patch at line %ld\n",
1014: p_input_line);
1015: if (*buf != '-')
1016: fatal2("--- expected at line %ld of patch\n", p_input_line);
1017: }
1.7 millert 1018: Snprintf(buf, sizeof(buf), "--- %ld,%ld\n", min, max);
1.1 deraadt 1019: p_line[i] = savestr(buf);
1020: if (out_of_mem) {
1021: p_end = i-1;
1022: return FALSE;
1023: }
1024: p_char[i] = '=';
1025: for (i++; i<=p_end; i++) {
1026: ret = pgets(buf, sizeof buf, pfp);
1027: p_input_line++;
1028: if (ret == Nullch)
1029: fatal2("unexpected end of file in patch at line %ld\n",
1030: p_input_line);
1031: if (*buf != '>')
1032: fatal2("> expected at line %ld of patch\n", p_input_line);
1033: p_line[i] = savestr(buf+2);
1034: if (out_of_mem) {
1035: p_end = i-1;
1036: return FALSE;
1037: }
1038: p_len[i] = strlen(p_line[i]);
1039: p_char[i] = '+';
1.14 otto 1040: }
1041:
1042: if (remove_special_line()) {
1043: p_len[i - 1] -= 1;
1044: (p_line[i - 1])[p_len[i - 1]] = 0;
1.1 deraadt 1045: }
1046: }
1047: if (reverse) /* backwards patch? */
1048: if (!pch_swap())
1049: say1("Not enough memory to swap next hunk!\n");
1050: #ifdef DEBUGGING
1051: if (debug & 2) {
1052: int i;
1053: char special;
1054:
1055: for (i=0; i <= p_end; i++) {
1056: if (i == p_ptrn_lines)
1057: special = '^';
1058: else
1059: special = ' ';
1060: fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, p_line[i]);
1061: Fflush(stderr);
1062: }
1063: }
1064: #endif
1065: if (p_end+1 < hunkmax) /* paranoia reigns supreme... */
1066: p_char[p_end+1] = '^'; /* add a stopper for apply_hunk */
1067: return TRUE;
1068: }
1069:
1070: /* Input a line from the patch file, worrying about indentation. */
1071:
1072: char *
1073: pgets(bf,sz,fp)
1074: char *bf;
1075: int sz;
1076: FILE *fp;
1077: {
1078: char *ret = fgets(bf, sz, fp);
1079: Reg1 char *s;
1080: Reg2 int indent = 0;
1081:
1082: if (p_indent && ret != Nullch) {
1083: for (s=buf;
1084: indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X'); s++) {
1085: if (*s == '\t')
1086: indent += 8 - (indent % 7);
1087: else
1088: indent++;
1089: }
1.10 provos 1090: if (buf != s && strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
1091: fatal1("buffer too small in pgets()\n");
1.1 deraadt 1092: }
1093: return ret;
1094: }
1095:
1096: /* Reverse the old and new portions of the current hunk. */
1097:
1098: bool
1099: pch_swap()
1100: {
1101: char **tp_line; /* the text of the hunk */
1102: short *tp_len; /* length of each line */
1103: char *tp_char; /* +, -, and ! */
1104: Reg1 LINENUM i;
1105: Reg2 LINENUM n;
1106: bool blankline = FALSE;
1107: Reg3 char *s;
1108:
1109: i = p_first;
1110: p_first = p_newfirst;
1111: p_newfirst = i;
1112:
1113: /* make a scratch copy */
1114:
1115: tp_line = p_line;
1116: tp_len = p_len;
1117: tp_char = p_char;
1118: p_line = Null(char**); /* force set_hunkmax to allocate again */
1119: p_len = Null(short*);
1120: p_char = Nullch;
1121: set_hunkmax();
1122: if (p_line == Null(char**) || p_len == Null(short*) || p_char == Nullch) {
1123: #ifndef lint
1124: if (p_line == Null(char**))
1125: free((char*)p_line);
1126: p_line = tp_line;
1127: if (p_len == Null(short*))
1128: free((char*)p_len);
1129: p_len = tp_len;
1130: #endif
1131: if (p_char == Nullch)
1132: free((char*)p_char);
1133: p_char = tp_char;
1134: return FALSE; /* not enough memory to swap hunk! */
1135: }
1136:
1137: /* now turn the new into the old */
1138:
1139: i = p_ptrn_lines + 1;
1140: if (tp_char[i] == '\n') { /* account for possible blank line */
1141: blankline = TRUE;
1142: i++;
1143: }
1144: if (p_efake >= 0) { /* fix non-freeable ptr range */
1145: if (p_efake <= i)
1146: n = p_end - i + 1;
1147: else
1148: n = -i;
1149: p_efake += n;
1150: p_bfake += n;
1151: }
1152: for (n=0; i <= p_end; i++,n++) {
1153: p_line[n] = tp_line[i];
1154: p_char[n] = tp_char[i];
1155: if (p_char[n] == '+')
1156: p_char[n] = '-';
1157: p_len[n] = tp_len[i];
1158: }
1159: if (blankline) {
1160: i = p_ptrn_lines + 1;
1161: p_line[n] = tp_line[i];
1162: p_char[n] = tp_char[i];
1163: p_len[n] = tp_len[i];
1164: n++;
1165: }
1166: assert(p_char[0] == '=');
1167: p_char[0] = '*';
1168: for (s=p_line[0]; *s; s++)
1169: if (*s == '-')
1170: *s = '*';
1171:
1172: /* now turn the old into the new */
1173:
1174: assert(tp_char[0] == '*');
1175: tp_char[0] = '=';
1176: for (s=tp_line[0]; *s; s++)
1177: if (*s == '*')
1178: *s = '-';
1179: for (i=0; n <= p_end; i++,n++) {
1180: p_line[n] = tp_line[i];
1181: p_char[n] = tp_char[i];
1182: if (p_char[n] == '-')
1183: p_char[n] = '+';
1184: p_len[n] = tp_len[i];
1185: }
1186: assert(i == p_ptrn_lines + 1);
1187: i = p_ptrn_lines;
1188: p_ptrn_lines = p_repl_lines;
1189: p_repl_lines = i;
1190: #ifndef lint
1191: if (tp_line == Null(char**))
1192: free((char*)tp_line);
1193: if (tp_len == Null(short*))
1194: free((char*)tp_len);
1195: #endif
1196: if (tp_char == Nullch)
1197: free((char*)tp_char);
1198: return TRUE;
1199: }
1200:
1201: /* Return the specified line position in the old file of the old context. */
1202:
1203: LINENUM
1204: pch_first()
1205: {
1206: return p_first;
1207: }
1208:
1209: /* Return the number of lines of old context. */
1210:
1211: LINENUM
1212: pch_ptrn_lines()
1213: {
1214: return p_ptrn_lines;
1215: }
1216:
1217: /* Return the probable line position in the new file of the first line. */
1218:
1219: LINENUM
1220: pch_newfirst()
1221: {
1222: return p_newfirst;
1223: }
1224:
1225: /* Return the number of lines in the replacement text including context. */
1226:
1227: LINENUM
1228: pch_repl_lines()
1229: {
1230: return p_repl_lines;
1231: }
1232:
1233: /* Return the number of lines in the whole hunk. */
1234:
1235: LINENUM
1236: pch_end()
1237: {
1238: return p_end;
1239: }
1240:
1241: /* Return the number of context lines before the first changed line. */
1242:
1243: LINENUM
1244: pch_context()
1245: {
1246: return p_context;
1247: }
1248:
1249: /* Return the length of a particular patch line. */
1250:
1251: short
1252: pch_line_len(line)
1253: LINENUM line;
1254: {
1255: return p_len[line];
1256: }
1257:
1258: /* Return the control character (+, -, *, !, etc) for a patch line. */
1259:
1260: char
1261: pch_char(line)
1262: LINENUM line;
1263: {
1264: return p_char[line];
1265: }
1266:
1267: /* Return a pointer to a particular patch line. */
1268:
1269: char *
1270: pfetch(line)
1271: LINENUM line;
1272: {
1273: return p_line[line];
1274: }
1275:
1276: /* Return where in the patch file this hunk began, for error messages. */
1277:
1278: LINENUM
1279: pch_hunk_beg()
1280: {
1281: return p_hunk_beg;
1282: }
1283:
1284: /* Apply an ed script by feeding ed itself. */
1285:
1286: void
1287: do_ed_script()
1288: {
1289: Reg1 char *t;
1290: Reg2 long beginning_of_this_line;
1291: Reg3 bool this_line_is_command = FALSE;
1292: Reg4 FILE *pipefp;
1293:
1294: if (!skip_rest_of_patch) {
1295: Unlink(TMPOUTNAME);
1296: copy_file(filearg[0], TMPOUTNAME);
1297: if (verbose)
1.7 millert 1298: Snprintf(buf, sizeof buf, "/bin/ed %s", TMPOUTNAME);
1.1 deraadt 1299: else
1.7 millert 1300: Snprintf(buf, sizeof buf, "/bin/ed - %s", TMPOUTNAME);
1.1 deraadt 1301: pipefp = popen(buf, "w");
1302: }
1303: for (;;) {
1304: beginning_of_this_line = ftell(pfp);
1305: if (pgets(buf, sizeof buf, pfp) == Nullch) {
1306: next_intuit_at(beginning_of_this_line,p_input_line);
1307: break;
1308: }
1309: p_input_line++;
1310: for (t=buf; isdigit(*t) || *t == ','; t++) ;
1311: this_line_is_command = (isdigit(*buf) &&
1312: (*t == 'd' || *t == 'c' || *t == 'a') );
1313: if (this_line_is_command) {
1314: if (!skip_rest_of_patch)
1315: fputs(buf, pipefp);
1316: if (*t != 'd') {
1317: while (pgets(buf, sizeof buf, pfp) != Nullch) {
1318: p_input_line++;
1319: if (!skip_rest_of_patch)
1320: fputs(buf, pipefp);
1321: if (strEQ(buf, ".\n"))
1322: break;
1323: }
1324: }
1325: }
1326: else {
1327: next_intuit_at(beginning_of_this_line,p_input_line);
1328: break;
1329: }
1330: }
1331: if (skip_rest_of_patch)
1332: return;
1333: fprintf(pipefp, "w\n");
1334: fprintf(pipefp, "q\n");
1335: Fflush(pipefp);
1336: Pclose(pipefp);
1337: ignore_signals();
1.9 espie 1338: if (!check_only) {
1339: if (move_file(TMPOUTNAME, outname) < 0) {
1340: toutkeep = TRUE;
1341: chmod(TMPOUTNAME, filemode);
1342: }
1343: else
1344: chmod(outname, filemode);
1.1 deraadt 1345: }
1346: set_signals(1);
1347: }