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