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