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