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