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