xref: /openbsd-src/usr.bin/patch/pch.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: pch.c,v 1.11 2000/12/14 00:02:19 beck Exp $	*/
2 
3 #ifndef lint
4 static char rcsid[] = "$OpenBSD: pch.c,v 1.11 2000/12/14 00:02:19 beck 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 extern bool check_only;
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, "-")) {
59 	pfp = fopen(TMPPATNAME, "w");
60 	if (pfp == Nullfp)
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);
153 	    skip_rest_of_patch = TRUE;
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);
311 	if (indname)
312 	    filearg[0] = savestr(indname);
313 	else if (oldname && newname) {
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) {
457 		if (p_max - p_end < 4)
458 		    strcpy(buf, "  \n");  /* assume blank lines got chopped */
459 		else {
460 		    if (repl_beginning && repl_could_be_missing) {
461 			repl_missing = TRUE;
462 			goto hunk_done;
463 		    }
464 		    fatal1("unexpected end of file in patch\n");
465 		}
466 	    }
467 	    p_end++;
468 	    assert(p_end < hunkmax);
469 	    p_char[p_end] = *buf;
470 #ifdef zilog
471 	    p_line[(short)p_end] = Nullch;
472 #else
473 	    p_line[p_end] = Nullch;
474 #endif
475 	    switch (*buf) {
476 	    case '*':
477 		if (strnEQ(buf, "********", 8)) {
478 		    if (repl_beginning && repl_could_be_missing) {
479 			repl_missing = TRUE;
480 			goto hunk_done;
481 		    }
482 		    else
483 			fatal2("unexpected end of hunk at line %ld\n",
484 			    p_input_line);
485 		}
486 		if (p_end != 0) {
487 		    if (repl_beginning && repl_could_be_missing) {
488 			repl_missing = TRUE;
489 			goto hunk_done;
490 		    }
491 		    fatal3("unexpected *** at line %ld: %s", p_input_line, buf);
492 		}
493 		context = 0;
494 		p_line[p_end] = savestr(buf);
495 		if (out_of_mem) {
496 		    p_end--;
497 		    return FALSE;
498 		}
499 		for (s=buf; *s && !isdigit(*s); s++) ;
500 		if (!*s)
501 		    malformed ();
502 		if (strnEQ(s,"0,0",3))
503 		    strcpy(s, s+2);
504 		p_first = (LINENUM) atol(s);
505 		while (isdigit(*s)) s++;
506 		if (*s == ',') {
507 		    for (; *s && !isdigit(*s); s++) ;
508 		    if (!*s)
509 			malformed ();
510 		    p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
511 		}
512 		else if (p_first)
513 		    p_ptrn_lines = 1;
514 		else {
515 		    p_ptrn_lines = 0;
516 		    p_first = 1;
517 		}
518 		p_max = p_ptrn_lines + 6;	/* we need this much at least */
519 		while (p_max >= hunkmax)
520 		    grow_hunkmax();
521 		p_max = hunkmax;
522 		break;
523 	    case '-':
524 		if (buf[1] == '-') {
525 		    if (repl_beginning ||
526 			(p_end != p_ptrn_lines + 1 + (p_char[p_end-1] == '\n')))
527 		    {
528 			if (p_end == 1) {
529 			    /* `old' lines were omitted - set up to fill */
530 			    /* them in from 'new' context lines. */
531 			    p_end = p_ptrn_lines + 1;
532 			    fillsrc = p_end + 1;
533 			    filldst = 1;
534 			    fillcnt = p_ptrn_lines;
535 			}
536 			else {
537 			    if (repl_beginning) {
538 				if (repl_could_be_missing){
539 				    repl_missing = TRUE;
540 				    goto hunk_done;
541 				}
542 				fatal3(
543 "duplicate \"---\" at line %ld--check line numbers at line %ld\n",
544 				    p_input_line, p_hunk_beg + repl_beginning);
545 			    }
546 			    else {
547 				fatal4(
548 "%s \"---\" at line %ld--check line numbers at line %ld\n",
549 				    (p_end <= p_ptrn_lines
550 					? "Premature"
551 					: "Overdue" ),
552 				    p_input_line, p_hunk_beg);
553 			    }
554 			}
555 		    }
556 		    repl_beginning = p_end;
557 		    repl_backtrack_position = ftell(pfp);
558 		    repl_patch_line = p_input_line;
559 		    p_line[p_end] = savestr(buf);
560 		    if (out_of_mem) {
561 			p_end--;
562 			return FALSE;
563 		    }
564 		    p_char[p_end] = '=';
565 		    for (s=buf; *s && !isdigit(*s); s++) ;
566 		    if (!*s)
567 			malformed ();
568 		    p_newfirst = (LINENUM) atol(s);
569 		    while (isdigit(*s)) s++;
570 		    if (*s == ',') {
571 			for (; *s && !isdigit(*s); s++) ;
572 			if (!*s)
573 			    malformed ();
574 			p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
575 		    }
576 		    else if (p_newfirst)
577 			p_repl_lines = 1;
578 		    else {
579 			p_repl_lines = 0;
580 			p_newfirst = 1;
581 		    }
582 		    p_max = p_repl_lines + p_end;
583 		    if (p_max > MAXHUNKSIZE)
584 			fatal4("hunk too large (%ld lines) at line %ld: %s",
585 			      p_max, p_input_line, buf);
586 		    while (p_max >= hunkmax)
587 			grow_hunkmax();
588 		    if (p_repl_lines != ptrn_copiable
589 		     && (p_context != 0 || p_repl_lines != 1))
590 			repl_could_be_missing = FALSE;
591 		    break;
592 		}
593 		goto change_line;
594 	    case '+':  case '!':
595 		repl_could_be_missing = FALSE;
596 	      change_line:
597 		if (buf[1] == '\n' && canonicalize)
598 		    strcpy(buf+1," \n");
599 		if (!isspace(buf[1]) && buf[1] != '>' && buf[1] != '<' &&
600 		  repl_beginning && repl_could_be_missing) {
601 		    repl_missing = TRUE;
602 		    goto hunk_done;
603 		}
604 		if (context >= 0) {
605 		    if (context < p_context)
606 			p_context = context;
607 		    context = -1000;
608 		}
609 		p_line[p_end] = savestr(buf+2);
610 		if (out_of_mem) {
611 		    p_end--;
612 		    return FALSE;
613 		}
614 		break;
615 	    case '\t': case '\n':	/* assume the 2 spaces got eaten */
616 		if (repl_beginning && repl_could_be_missing &&
617 		  (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
618 		    repl_missing = TRUE;
619 		    goto hunk_done;
620 		}
621 		p_line[p_end] = savestr(buf);
622 		if (out_of_mem) {
623 		    p_end--;
624 		    return FALSE;
625 		}
626 		if (p_end != p_ptrn_lines + 1) {
627 		    ptrn_spaces_eaten |= (repl_beginning != 0);
628 		    context++;
629 		    if (!repl_beginning)
630 			ptrn_copiable++;
631 		    p_char[p_end] = ' ';
632 		}
633 		break;
634 	    case ' ':
635 		if (!isspace(buf[1]) &&
636 		  repl_beginning && repl_could_be_missing) {
637 		    repl_missing = TRUE;
638 		    goto hunk_done;
639 		}
640 		context++;
641 		if (!repl_beginning)
642 		    ptrn_copiable++;
643 		p_line[p_end] = savestr(buf+2);
644 		if (out_of_mem) {
645 		    p_end--;
646 		    return FALSE;
647 		}
648 		break;
649 	    default:
650 		if (repl_beginning && repl_could_be_missing) {
651 		    repl_missing = TRUE;
652 		    goto hunk_done;
653 		}
654 		malformed ();
655 	    }
656 	    /* set up p_len for strncmp() so we don't have to */
657 	    /* assume null termination */
658 	    if (p_line[p_end])
659 		p_len[p_end] = strlen(p_line[p_end]);
660 	    else
661 		p_len[p_end] = 0;
662 	}
663 
664     hunk_done:
665 	if (p_end >=0 && !repl_beginning)
666 	    fatal2("no --- found in patch at line %ld\n", pch_hunk_beg());
667 
668 	if (repl_missing) {
669 
670 	    /* reset state back to just after --- */
671 	    p_input_line = repl_patch_line;
672 	    for (p_end--; p_end > repl_beginning; p_end--)
673 		free(p_line[p_end]);
674 	    Fseek(pfp, repl_backtrack_position, 0);
675 
676 	    /* redundant 'new' context lines were omitted - set */
677 	    /* up to fill them in from the old file context */
678 	    if (!p_context && p_repl_lines == 1) {
679 		p_repl_lines = 0;
680 		p_max--;
681 	    }
682 	    fillsrc = 1;
683 	    filldst = repl_beginning+1;
684 	    fillcnt = p_repl_lines;
685 	    p_end = p_max;
686 	}
687 	else if (!p_context && fillcnt == 1) {
688 	    /* the first hunk was a null hunk with no context */
689 	    /* and we were expecting one line -- fix it up. */
690 	    while (filldst < p_end) {
691 		p_line[filldst] = p_line[filldst+1];
692 		p_char[filldst] = p_char[filldst+1];
693 		p_len[filldst] = p_len[filldst+1];
694 		filldst++;
695 	    }
696 #if 0
697 	    repl_beginning--;		/* this doesn't need to be fixed */
698 #endif
699 	    p_end--;
700 	    p_first++;			/* do append rather than insert */
701 	    fillcnt = 0;
702 	    p_ptrn_lines = 0;
703 	}
704 
705 	if (diff_type == CONTEXT_DIFF &&
706 	  (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
707 	    if (verbose)
708 		say4("%s\n%s\n%s\n",
709 "(Fascinating--this is really a new-style context diff but without",
710 "the telltale extra asterisks on the *** line that usually indicate",
711 "the new style...)");
712 	    diff_type = NEW_CONTEXT_DIFF;
713 	}
714 
715 	/* if there were omitted context lines, fill them in now */
716 	if (fillcnt) {
717 	    p_bfake = filldst;		/* remember where not to free() */
718 	    p_efake = filldst + fillcnt - 1;
719 	    while (fillcnt-- > 0) {
720 		while (fillsrc <= p_end && p_char[fillsrc] != ' ')
721 		    fillsrc++;
722 		if (fillsrc > p_end)
723 		    fatal2("replacement text or line numbers mangled in hunk at line %ld\n",
724 			p_hunk_beg);
725 		p_line[filldst] = p_line[fillsrc];
726 		p_char[filldst] = p_char[fillsrc];
727 		p_len[filldst] = p_len[fillsrc];
728 		fillsrc++; filldst++;
729 	    }
730 	    while (fillsrc <= p_end && fillsrc != repl_beginning &&
731 	      p_char[fillsrc] != ' ')
732 		fillsrc++;
733 #ifdef DEBUGGING
734 	    if (debug & 64)
735 		printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
736 		    fillsrc,filldst,repl_beginning,p_end+1);
737 #endif
738 	    assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
739 	    assert(filldst==p_end+1 || filldst==repl_beginning);
740 	}
741     }
742     else if (diff_type == UNI_DIFF) {
743 	long line_beginning = ftell(pfp);
744 					/* file pos of the current line */
745 	Reg4 LINENUM fillsrc;		/* index of old lines */
746 	Reg5 LINENUM filldst;		/* index of new lines */
747 	char ch;
748 
749 	ret = pgets(buf, sizeof buf, pfp);
750 	p_input_line++;
751 	if (ret == Nullch || strnNE(buf, "@@ -", 4)) {
752 	    next_intuit_at(line_beginning,p_input_line);
753 	    return FALSE;
754 	}
755 	s = buf+4;
756 	if (!*s)
757 	    malformed ();
758 	p_first = (LINENUM) atol(s);
759 	while (isdigit(*s)) s++;
760 	if (*s == ',') {
761 	    p_ptrn_lines = (LINENUM) atol(++s);
762 	    while (isdigit(*s)) s++;
763 	} else
764 	    p_ptrn_lines = 1;
765 	if (*s == ' ') s++;
766 	if (*s != '+' || !*++s)
767 	    malformed ();
768 	p_newfirst = (LINENUM) atol(s);
769 	while (isdigit(*s)) s++;
770 	if (*s == ',') {
771 	    p_repl_lines = (LINENUM) atol(++s);
772 	    while (isdigit(*s)) s++;
773 	} else
774 	    p_repl_lines = 1;
775 	if (*s == ' ') s++;
776 	if (*s != '@')
777 	    malformed ();
778 	if (!p_ptrn_lines)
779 	    p_first++;			/* do append rather than insert */
780 	p_max = p_ptrn_lines + p_repl_lines + 1;
781 	while (p_max >= hunkmax)
782 	    grow_hunkmax();
783 	fillsrc = 1;
784 	filldst = fillsrc + p_ptrn_lines;
785 	p_end = filldst + p_repl_lines;
786 	Snprintf(buf, sizeof buf, "*** %ld,%ld ****\n", p_first,
787 	    p_first + p_ptrn_lines - 1);
788 	p_line[0] = savestr(buf);
789 	if (out_of_mem) {
790 	    p_end = -1;
791 	    return FALSE;
792 	}
793 	p_char[0] = '*';
794         Snprintf(buf, sizeof buf, "--- %ld,%ld ----\n", p_newfirst,
795 	    p_newfirst + p_repl_lines - 1);
796 	p_line[filldst] = savestr(buf);
797 	if (out_of_mem) {
798 	    p_end = 0;
799 	    return FALSE;
800 	}
801 	p_char[filldst++] = '=';
802 	p_context = 100;
803 	context = 0;
804 	p_hunk_beg = p_input_line + 1;
805 	while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
806 	    line_beginning = ftell(pfp);
807 	    ret = pgets(buf, sizeof buf, pfp);
808 	    p_input_line++;
809 	    if (ret == Nullch) {
810 		if (p_max - filldst < 3)
811 		    strcpy(buf, " \n");  /* assume blank lines got chopped */
812 		else {
813 		    fatal1("unexpected end of file in patch\n");
814 		}
815 	    }
816 	    if (*buf == '\t' || *buf == '\n') {
817 		ch = ' ';		/* assume the space got eaten */
818 		s = savestr(buf);
819 	    }
820 	    else {
821 		ch = *buf;
822 		s = savestr(buf+1);
823 	    }
824 	    if (out_of_mem) {
825 		while (--filldst > p_ptrn_lines)
826 		    free(p_line[filldst]);
827 		p_end = fillsrc-1;
828 		return FALSE;
829 	    }
830 	    switch (ch) {
831 	    case '-':
832 		if (fillsrc > p_ptrn_lines) {
833 		    free(s);
834 		    p_end = filldst-1;
835 		    malformed ();
836 		}
837 		p_char[fillsrc] = ch;
838 		p_line[fillsrc] = s;
839 		p_len[fillsrc++] = strlen(s);
840 		break;
841 	    case '=':
842 		ch = ' ';
843 		/* FALL THROUGH */
844 	    case ' ':
845 		if (fillsrc > p_ptrn_lines) {
846 		    free(s);
847 		    while (--filldst > p_ptrn_lines)
848 			free(p_line[filldst]);
849 		    p_end = fillsrc-1;
850 		    malformed ();
851 		}
852 		context++;
853 		p_char[fillsrc] = ch;
854 		p_line[fillsrc] = s;
855 		p_len[fillsrc++] = strlen(s);
856 		s = savestr(s);
857 		if (out_of_mem) {
858 		    while (--filldst > p_ptrn_lines)
859 			free(p_line[filldst]);
860 		    p_end = fillsrc-1;
861 		    return FALSE;
862 		}
863 		/* FALL THROUGH */
864 	    case '+':
865 		if (filldst > p_end) {
866 		    free(s);
867 		    while (--filldst > p_ptrn_lines)
868 			free(p_line[filldst]);
869 		    p_end = fillsrc-1;
870 		    malformed ();
871 		}
872 		p_char[filldst] = ch;
873 		p_line[filldst] = s;
874 		p_len[filldst++] = strlen(s);
875 		break;
876 	    default:
877 		p_end = filldst;
878 		malformed ();
879 	    }
880 	    if (ch != ' ' && context > 0) {
881 		if (context < p_context)
882 		    p_context = context;
883 		context = -1000;
884 	    }
885 	}/* while */
886     }
887     else {				/* normal diff--fake it up */
888 	char hunk_type;
889 	Reg3 int i;
890 	LINENUM min, max;
891 	long line_beginning = ftell(pfp);
892 
893 	p_context = 0;
894 	ret = pgets(buf, sizeof buf, pfp);
895 	p_input_line++;
896 	if (ret == Nullch || !isdigit(*buf)) {
897 	    next_intuit_at(line_beginning,p_input_line);
898 	    return FALSE;
899 	}
900 	p_first = (LINENUM)atol(buf);
901 	for (s=buf; isdigit(*s); s++) ;
902 	if (*s == ',') {
903 	    p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
904 	    while (isdigit(*s)) s++;
905 	}
906 	else
907 	    p_ptrn_lines = (*s != 'a');
908 	hunk_type = *s;
909 	if (hunk_type == 'a')
910 	    p_first++;			/* do append rather than insert */
911 	min = (LINENUM)atol(++s);
912 	for (; isdigit(*s); s++) ;
913 	if (*s == ',')
914 	    max = (LINENUM)atol(++s);
915 	else
916 	    max = min;
917 	if (hunk_type == 'd')
918 	    min++;
919 	p_end = p_ptrn_lines + 1 + max - min + 1;
920 	if (p_end > MAXHUNKSIZE)
921 	    fatal4("hunk too large (%ld lines) at line %ld: %s",
922 		  p_end, p_input_line, buf);
923 	while (p_end >= hunkmax)
924 	    grow_hunkmax();
925 	p_newfirst = min;
926 	p_repl_lines = max - min + 1;
927 	Snprintf(buf, sizeof buf, "*** %ld,%ld\n", p_first,
928 	    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 	Snprintf(buf, sizeof(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 && strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
1028 	    fatal1("buffer too small in pgets()\n");
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;
1230 
1231     if (!skip_rest_of_patch) {
1232 	Unlink(TMPOUTNAME);
1233 	copy_file(filearg[0], TMPOUTNAME);
1234 	if (verbose)
1235 	    Snprintf(buf, sizeof buf, "/bin/ed %s", TMPOUTNAME);
1236 	else
1237 	    Snprintf(buf, sizeof 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(*t) || *t == ','; t++) ;
1248 	this_line_is_command = (isdigit(*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 (!check_only) {
1276 	if (move_file(TMPOUTNAME, outname) < 0) {
1277 	    toutkeep = TRUE;
1278 	    chmod(TMPOUTNAME, filemode);
1279 	}
1280 	else
1281 	    chmod(outname, filemode);
1282     }
1283     set_signals(1);
1284 }
1285