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