xref: /freebsd/usr.bin/patch/pch.c (revision 25696725b65b651814799574fdfadb1b1a5e0b02)
12dd076b8SGabor Kovesdan /*-
22dd076b8SGabor Kovesdan  * Copyright 1986, Larry Wall
32dd076b8SGabor Kovesdan  *
42dd076b8SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
52dd076b8SGabor Kovesdan  * modification, are permitted provided that the following condition is met:
62dd076b8SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright notice,
72dd076b8SGabor Kovesdan  * this condition and the following disclaimer.
82dd076b8SGabor Kovesdan  *
92dd076b8SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
102dd076b8SGabor Kovesdan  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
112dd076b8SGabor Kovesdan  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
122dd076b8SGabor Kovesdan  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
132dd076b8SGabor Kovesdan  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
142dd076b8SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
152dd076b8SGabor Kovesdan  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
162dd076b8SGabor Kovesdan  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
172dd076b8SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
182dd076b8SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
192dd076b8SGabor Kovesdan  * SUCH DAMAGE.
202dd076b8SGabor Kovesdan  *
212dd076b8SGabor Kovesdan  * patch - a program to apply diffs to original files
222dd076b8SGabor Kovesdan  *
232dd076b8SGabor Kovesdan  * -C option added in 1998, original code by Marc Espie, based on FreeBSD
242dd076b8SGabor Kovesdan  * behaviour
252dd076b8SGabor Kovesdan  *
262b468ebaSPedro F. Giffuni  * $OpenBSD: pch.c,v 1.43 2014/11/18 17:03:35 tobias Exp $
272dd076b8SGabor Kovesdan  */
282dd076b8SGabor Kovesdan 
292dd076b8SGabor Kovesdan #include <sys/types.h>
302dd076b8SGabor Kovesdan #include <sys/stat.h>
312dd076b8SGabor Kovesdan 
322dd076b8SGabor Kovesdan #include <ctype.h>
332dd076b8SGabor Kovesdan #include <libgen.h>
342dd076b8SGabor Kovesdan #include <limits.h>
35df6e4074SPedro F. Giffuni #include <stdint.h>
362dd076b8SGabor Kovesdan #include <stdio.h>
372dd076b8SGabor Kovesdan #include <stdlib.h>
382dd076b8SGabor Kovesdan #include <string.h>
392dd076b8SGabor Kovesdan #include <unistd.h>
402dd076b8SGabor Kovesdan 
412dd076b8SGabor Kovesdan #include "common.h"
422dd076b8SGabor Kovesdan #include "util.h"
432dd076b8SGabor Kovesdan #include "pch.h"
442dd076b8SGabor Kovesdan #include "pathnames.h"
452dd076b8SGabor Kovesdan 
462dd076b8SGabor Kovesdan /* Patch (diff listing) abstract type. */
472dd076b8SGabor Kovesdan 
48e91a64deSPedro F. Giffuni static off_t	p_filesize;	/* size of the patch file */
492dd076b8SGabor Kovesdan static LINENUM	p_first;	/* 1st line number */
502dd076b8SGabor Kovesdan static LINENUM	p_newfirst;	/* 1st line number of replacement */
512dd076b8SGabor Kovesdan static LINENUM	p_ptrn_lines;	/* # lines in pattern */
522dd076b8SGabor Kovesdan static LINENUM	p_repl_lines;	/* # lines in replacement text */
532dd076b8SGabor Kovesdan static LINENUM	p_end = -1;	/* last line in hunk */
542dd076b8SGabor Kovesdan static LINENUM	p_max;		/* max allowed value of p_end */
552dd076b8SGabor Kovesdan static LINENUM	p_context = 3;	/* # of context lines */
562dd076b8SGabor Kovesdan static LINENUM	p_input_line = 0;	/* current line # from patch file */
572dd076b8SGabor Kovesdan static char	**p_line = NULL;/* the text of the hunk */
58851a9da3SDag-Erling Smørgrav static size_t	*p_len = NULL;	/* length of each line */
592dd076b8SGabor Kovesdan static char	*p_char = NULL;	/* +, -, and ! */
602dd076b8SGabor Kovesdan static int	hunkmax = INITHUNKMAX;	/* size of above arrays to begin with */
612dd076b8SGabor Kovesdan static int	p_indent;	/* indent to patch */
62e91a64deSPedro F. Giffuni static off_t	p_base;		/* where to intuit this time */
632dd076b8SGabor Kovesdan static LINENUM	p_bline;	/* line # of p_base */
64e91a64deSPedro F. Giffuni static off_t	p_start;	/* where intuit found a patch */
652dd076b8SGabor Kovesdan static LINENUM	p_sline;	/* and the line number for it */
662dd076b8SGabor Kovesdan static LINENUM	p_hunk_beg;	/* line number of current hunk */
672dd076b8SGabor Kovesdan static LINENUM	p_efake = -1;	/* end of faked up lines--don't free */
682dd076b8SGabor Kovesdan static LINENUM	p_bfake = -1;	/* beg of faked up lines */
692dd076b8SGabor Kovesdan static FILE	*pfp = NULL;	/* patch file pointer */
702dd076b8SGabor Kovesdan static char	*bestguess = NULL;	/* guess at correct filename */
712dd076b8SGabor Kovesdan 
7250dacbf6SKyle Evans char		*source_file;
7350dacbf6SKyle Evans 
742dd076b8SGabor Kovesdan static void	grow_hunkmax(void);
752dd076b8SGabor Kovesdan static int	intuit_diff_type(void);
76e91a64deSPedro F. Giffuni static void	next_intuit_at(off_t, LINENUM);
77e91a64deSPedro F. Giffuni static void	skip_to(off_t, LINENUM);
782dd076b8SGabor Kovesdan static size_t	pgets(bool _do_indent);
792dd076b8SGabor Kovesdan static char	*best_name(const struct file_name *, bool);
802dd076b8SGabor Kovesdan static char	*posix_name(const struct file_name *, bool);
812dd076b8SGabor Kovesdan static size_t	num_components(const char *);
82d3fc0cb8SPedro F. Giffuni static LINENUM	strtolinenum(char *, char **);
832dd076b8SGabor Kovesdan 
842dd076b8SGabor Kovesdan /*
852dd076b8SGabor Kovesdan  * Prepare to look for the next patch in the patch file.
862dd076b8SGabor Kovesdan  */
872dd076b8SGabor Kovesdan void
882dd076b8SGabor Kovesdan re_patch(void)
892dd076b8SGabor Kovesdan {
902dd076b8SGabor Kovesdan 	p_first = 0;
912dd076b8SGabor Kovesdan 	p_newfirst = 0;
922dd076b8SGabor Kovesdan 	p_ptrn_lines = 0;
932dd076b8SGabor Kovesdan 	p_repl_lines = 0;
942dd076b8SGabor Kovesdan 	p_end = (LINENUM) - 1;
952dd076b8SGabor Kovesdan 	p_max = 0;
962dd076b8SGabor Kovesdan 	p_indent = 0;
972dd076b8SGabor Kovesdan }
982dd076b8SGabor Kovesdan 
992dd076b8SGabor Kovesdan /*
1002dd076b8SGabor Kovesdan  * Open the patch file at the beginning of time.
1012dd076b8SGabor Kovesdan  */
1022dd076b8SGabor Kovesdan void
1032dd076b8SGabor Kovesdan open_patch_file(const char *filename)
1042dd076b8SGabor Kovesdan {
1052dd076b8SGabor Kovesdan 	struct stat filestat;
1060571fd57SDavid E. O'Brien 	int nr, nw;
1072dd076b8SGabor Kovesdan 
1082dd076b8SGabor Kovesdan 	if (filename == NULL || *filename == '\0' || strEQ(filename, "-")) {
1092dd076b8SGabor Kovesdan 		pfp = fopen(TMPPATNAME, "w");
1102dd076b8SGabor Kovesdan 		if (pfp == NULL)
1112dd076b8SGabor Kovesdan 			pfatal("can't create %s", TMPPATNAME);
1120571fd57SDavid E. O'Brien 		while ((nr = fread(buf, 1, buf_size, stdin)) > 0) {
1130571fd57SDavid E. O'Brien 			nw = fwrite(buf, 1, nr, pfp);
1140571fd57SDavid E. O'Brien 			if (nr != nw)
1150571fd57SDavid E. O'Brien 				pfatal("write error to %s", TMPPATNAME);
1160571fd57SDavid E. O'Brien 		}
1172dd076b8SGabor Kovesdan 		if (ferror(pfp) || fclose(pfp))
1182dd076b8SGabor Kovesdan 			pfatal("can't write %s", TMPPATNAME);
1192dd076b8SGabor Kovesdan 		filename = TMPPATNAME;
1202dd076b8SGabor Kovesdan 	}
1212dd076b8SGabor Kovesdan 	pfp = fopen(filename, "r");
1222dd076b8SGabor Kovesdan 	if (pfp == NULL)
1232dd076b8SGabor Kovesdan 		pfatal("patch file %s not found", filename);
124e91a64deSPedro F. Giffuni 	if (fstat(fileno(pfp), &filestat))
125e91a64deSPedro F. Giffuni 		pfatal("can't stat %s", filename);
1262dd076b8SGabor Kovesdan 	p_filesize = filestat.st_size;
127e91a64deSPedro F. Giffuni 	next_intuit_at(0, 1L);	/* start at the beginning */
1282dd076b8SGabor Kovesdan 	set_hunkmax();
1292dd076b8SGabor Kovesdan }
1302dd076b8SGabor Kovesdan 
1312dd076b8SGabor Kovesdan /*
1322dd076b8SGabor Kovesdan  * Make sure our dynamically realloced tables are malloced to begin with.
1332dd076b8SGabor Kovesdan  */
1342dd076b8SGabor Kovesdan void
1352dd076b8SGabor Kovesdan set_hunkmax(void)
1362dd076b8SGabor Kovesdan {
1372dd076b8SGabor Kovesdan 	if (p_line == NULL)
138c44f94d0SPedro F. Giffuni 		p_line = malloc(hunkmax * sizeof(char *));
1392dd076b8SGabor Kovesdan 	if (p_len == NULL)
140851a9da3SDag-Erling Smørgrav 		p_len = malloc(hunkmax * sizeof(size_t));
1412dd076b8SGabor Kovesdan 	if (p_char == NULL)
142c44f94d0SPedro F. Giffuni 		p_char = malloc(hunkmax * sizeof(char));
1432dd076b8SGabor Kovesdan }
1442dd076b8SGabor Kovesdan 
1452dd076b8SGabor Kovesdan /*
1462dd076b8SGabor Kovesdan  * Enlarge the arrays containing the current hunk of patch.
1472dd076b8SGabor Kovesdan  */
1482dd076b8SGabor Kovesdan static void
1492dd076b8SGabor Kovesdan grow_hunkmax(void)
1502dd076b8SGabor Kovesdan {
151c44f94d0SPedro F. Giffuni 	int new_hunkmax = hunkmax * 2;
1522dd076b8SGabor Kovesdan 
1532dd076b8SGabor Kovesdan 	if (p_line == NULL || p_len == NULL || p_char == NULL)
1542dd076b8SGabor Kovesdan 		fatal("Internal memory allocation error\n");
1552dd076b8SGabor Kovesdan 
156c44f94d0SPedro F. Giffuni 	p_line = reallocf(p_line, new_hunkmax * sizeof(char *));
157851a9da3SDag-Erling Smørgrav 	p_len = reallocf(p_len, new_hunkmax * sizeof(size_t));
158c44f94d0SPedro F. Giffuni 	p_char = reallocf(p_char, new_hunkmax * sizeof(char));
1592dd076b8SGabor Kovesdan 
1602dd076b8SGabor Kovesdan 	if (p_line != NULL && p_len != NULL && p_char != NULL) {
1612dd076b8SGabor Kovesdan 		hunkmax = new_hunkmax;
1622dd076b8SGabor Kovesdan 		return;
1632dd076b8SGabor Kovesdan 	}
1642dd076b8SGabor Kovesdan 
1652dd076b8SGabor Kovesdan 	if (!using_plan_a)
1662dd076b8SGabor Kovesdan 		fatal("out of memory\n");
1672dd076b8SGabor Kovesdan 	out_of_mem = true;	/* whatever is null will be allocated again */
1682dd076b8SGabor Kovesdan 				/* from within plan_a(), of all places */
1692dd076b8SGabor Kovesdan }
1702dd076b8SGabor Kovesdan 
1712dd076b8SGabor Kovesdan /* True if the remainder of the patch file contains a diff of some sort. */
1722dd076b8SGabor Kovesdan 
1732dd076b8SGabor Kovesdan bool
1742dd076b8SGabor Kovesdan there_is_another_patch(void)
1752dd076b8SGabor Kovesdan {
1762dd076b8SGabor Kovesdan 	bool exists = false;
1772dd076b8SGabor Kovesdan 
178e91a64deSPedro F. Giffuni 	if (p_base != 0 && p_base >= p_filesize) {
1792dd076b8SGabor Kovesdan 		if (verbose)
1802dd076b8SGabor Kovesdan 			say("done\n");
1812dd076b8SGabor Kovesdan 		return false;
1822dd076b8SGabor Kovesdan 	}
183ef30b5a8SKyle Evans 	if (p_filesize == 0)
184ef30b5a8SKyle Evans 		return false;
185ef30b5a8SKyle Evans 	nonempty_patchf_seen = true;
1862dd076b8SGabor Kovesdan 	if (verbose)
1872dd076b8SGabor Kovesdan 		say("Hmm...");
1882dd076b8SGabor Kovesdan 	diff_type = intuit_diff_type();
1892dd076b8SGabor Kovesdan 	if (!diff_type) {
190e91a64deSPedro F. Giffuni 		if (p_base != 0) {
1912dd076b8SGabor Kovesdan 			if (verbose)
1922dd076b8SGabor Kovesdan 				say("  Ignoring the trailing garbage.\ndone\n");
1932dd076b8SGabor Kovesdan 		} else
1942dd076b8SGabor Kovesdan 			say("  I can't seem to find a patch in there anywhere.\n");
1952dd076b8SGabor Kovesdan 		return false;
1962dd076b8SGabor Kovesdan 	}
1972dd076b8SGabor Kovesdan 	if (verbose)
1982dd076b8SGabor Kovesdan 		say("  %sooks like %s to me...\n",
199e91a64deSPedro F. Giffuni 		    (p_base == 0 ? "L" : "The next patch l"),
2002dd076b8SGabor Kovesdan 		    diff_type == UNI_DIFF ? "a unified diff" :
2012dd076b8SGabor Kovesdan 		    diff_type == CONTEXT_DIFF ? "a context diff" :
2022dd076b8SGabor Kovesdan 		diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
2032dd076b8SGabor Kovesdan 		    diff_type == NORMAL_DIFF ? "a normal diff" :
2042dd076b8SGabor Kovesdan 		    "an ed script");
2052dd076b8SGabor Kovesdan 	if (p_indent && verbose)
2062dd076b8SGabor Kovesdan 		say("(Patch is indented %d space%s.)\n", p_indent,
2072dd076b8SGabor Kovesdan 		    p_indent == 1 ? "" : "s");
2082dd076b8SGabor Kovesdan 	skip_to(p_start, p_sline);
2092dd076b8SGabor Kovesdan 	while (filearg[0] == NULL) {
2102dd076b8SGabor Kovesdan 		if (force || batch) {
2112dd076b8SGabor Kovesdan 			say("No file to patch.  Skipping...\n");
212547e0acbSPedro F. Giffuni 			filearg[0] = xstrdup(bestguess);
2132dd076b8SGabor Kovesdan 			skip_rest_of_patch = true;
2142dd076b8SGabor Kovesdan 			return true;
2152dd076b8SGabor Kovesdan 		}
2162dd076b8SGabor Kovesdan 		ask("File to patch: ");
2172dd076b8SGabor Kovesdan 		if (*buf != '\n') {
2182dd076b8SGabor Kovesdan 			free(bestguess);
219547e0acbSPedro F. Giffuni 			bestguess = xstrdup(buf);
2202dd076b8SGabor Kovesdan 			filearg[0] = fetchname(buf, &exists, 0);
2212dd076b8SGabor Kovesdan 		}
22250dacbf6SKyle Evans 		/*
22350dacbf6SKyle Evans 		 * fetchname can now return buf = NULL, exists = true, to
22450dacbf6SKyle Evans 		 * indicate to the caller that /dev/null was specified.  Retain
22550dacbf6SKyle Evans 		 * previous behavior for now until this can be better evaluted.
22650dacbf6SKyle Evans 		 */
22750dacbf6SKyle Evans 		if (filearg[0] == NULL || !exists) {
22812300d3aSPedro F. Giffuni 			int def_skip = *bestguess == '\0';
22912300d3aSPedro F. Giffuni 			ask("No file found--skip this patch? [%c] ",
23012300d3aSPedro F. Giffuni 			    def_skip  ? 'y' : 'n');
23112300d3aSPedro F. Giffuni 			if (*buf == 'n' || (!def_skip && *buf != 'y'))
2322dd076b8SGabor Kovesdan 				continue;
2332dd076b8SGabor Kovesdan 			if (verbose)
2342dd076b8SGabor Kovesdan 				say("Skipping patch...\n");
2352dd076b8SGabor Kovesdan 			free(filearg[0]);
2362dd076b8SGabor Kovesdan 			filearg[0] = fetchname(bestguess, &exists, 0);
2372dd076b8SGabor Kovesdan 			skip_rest_of_patch = true;
2382dd076b8SGabor Kovesdan 			return true;
2392dd076b8SGabor Kovesdan 		}
2402dd076b8SGabor Kovesdan 	}
2412dd076b8SGabor Kovesdan 	return true;
2422dd076b8SGabor Kovesdan }
2432dd076b8SGabor Kovesdan 
2442dd076b8SGabor Kovesdan static void
2452dd076b8SGabor Kovesdan p4_fetchname(struct file_name *name, char *str)
2462dd076b8SGabor Kovesdan {
2472dd076b8SGabor Kovesdan 	char *t, *h;
2482dd076b8SGabor Kovesdan 
2492dd076b8SGabor Kovesdan 	/* Skip leading whitespace. */
2502dd076b8SGabor Kovesdan 	while (isspace((unsigned char)*str))
2512dd076b8SGabor Kovesdan 		str++;
2522dd076b8SGabor Kovesdan 
2532dd076b8SGabor Kovesdan 	/* Remove the file revision number. */
2542dd076b8SGabor Kovesdan 	for (t = str, h = NULL; *t != '\0' && !isspace((unsigned char)*t); t++)
2552dd076b8SGabor Kovesdan 		if (*t == '#')
2562dd076b8SGabor Kovesdan 			h = t;
2572dd076b8SGabor Kovesdan 	if (h != NULL)
2582dd076b8SGabor Kovesdan 		*h = '\0';
2592dd076b8SGabor Kovesdan 
2602dd076b8SGabor Kovesdan 	name->path = fetchname(str, &name->exists, strippath);
2612dd076b8SGabor Kovesdan }
2622dd076b8SGabor Kovesdan 
2632dd076b8SGabor Kovesdan /* Determine what kind of diff is in the remaining part of the patch file. */
2642dd076b8SGabor Kovesdan 
2652dd076b8SGabor Kovesdan static int
2662dd076b8SGabor Kovesdan intuit_diff_type(void)
2672dd076b8SGabor Kovesdan {
268e91a64deSPedro F. Giffuni 	off_t	this_line = 0, previous_line;
269e91a64deSPedro F. Giffuni 	off_t	first_command_line = -1;
2702dd076b8SGabor Kovesdan 	LINENUM	fcl_line = -1;
2712dd076b8SGabor Kovesdan 	bool	last_line_was_command = false, this_is_a_command = false;
2722dd076b8SGabor Kovesdan 	bool	stars_last_line = false, stars_this_line = false;
2732dd076b8SGabor Kovesdan 	char	*s, *t;
2742dd076b8SGabor Kovesdan 	int	indent, retval;
2752dd076b8SGabor Kovesdan 	struct file_name names[MAX_FILE];
27685823a60SPedro F. Giffuni 	int	piece_of_git = 0;
2772dd076b8SGabor Kovesdan 
2782dd076b8SGabor Kovesdan 	memset(names, 0, sizeof(names));
2792dd076b8SGabor Kovesdan 	ok_to_create_file = false;
280e91a64deSPedro F. Giffuni 	fseeko(pfp, p_base, SEEK_SET);
2812dd076b8SGabor Kovesdan 	p_input_line = p_bline - 1;
2822dd076b8SGabor Kovesdan 	for (;;) {
2832dd076b8SGabor Kovesdan 		previous_line = this_line;
2842dd076b8SGabor Kovesdan 		last_line_was_command = this_is_a_command;
2852dd076b8SGabor Kovesdan 		stars_last_line = stars_this_line;
286e91a64deSPedro F. Giffuni 		this_line = ftello(pfp);
2872dd076b8SGabor Kovesdan 		indent = 0;
2882dd076b8SGabor Kovesdan 		p_input_line++;
2892dd076b8SGabor Kovesdan 		if (pgets(false) == 0) {
290e91a64deSPedro F. Giffuni 			if (first_command_line >= 0) {
2912dd076b8SGabor Kovesdan 				/* nothing but deletes!? */
2922dd076b8SGabor Kovesdan 				p_start = first_command_line;
2932dd076b8SGabor Kovesdan 				p_sline = fcl_line;
2942dd076b8SGabor Kovesdan 				retval = ED_DIFF;
2952dd076b8SGabor Kovesdan 				goto scan_exit;
2962dd076b8SGabor Kovesdan 			} else {
2972dd076b8SGabor Kovesdan 				p_start = this_line;
2982dd076b8SGabor Kovesdan 				p_sline = p_input_line;
2992dd076b8SGabor Kovesdan 				retval = 0;
3002dd076b8SGabor Kovesdan 				goto scan_exit;
3012dd076b8SGabor Kovesdan 			}
3022dd076b8SGabor Kovesdan 		}
3032dd076b8SGabor Kovesdan 		for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
3042dd076b8SGabor Kovesdan 			if (*s == '\t')
3052dd076b8SGabor Kovesdan 				indent += 8 - (indent % 8);
3062dd076b8SGabor Kovesdan 			else
3072dd076b8SGabor Kovesdan 				indent++;
3082dd076b8SGabor Kovesdan 		}
3092dd076b8SGabor Kovesdan 		for (t = s; isdigit((unsigned char)*t) || *t == ','; t++)
3102dd076b8SGabor Kovesdan 			;
3112dd076b8SGabor Kovesdan 		this_is_a_command = (isdigit((unsigned char)*s) &&
3122dd076b8SGabor Kovesdan 		    (*t == 'd' || *t == 'c' || *t == 'a'));
313e91a64deSPedro F. Giffuni 		if (first_command_line < 0 && this_is_a_command) {
3142dd076b8SGabor Kovesdan 			first_command_line = this_line;
3152dd076b8SGabor Kovesdan 			fcl_line = p_input_line;
3162dd076b8SGabor Kovesdan 			p_indent = indent;	/* assume this for now */
3172dd076b8SGabor Kovesdan 		}
3182dd076b8SGabor Kovesdan 		if (!stars_last_line && strnEQ(s, "*** ", 4))
3192dd076b8SGabor Kovesdan 			names[OLD_FILE].path = fetchname(s + 4,
3202dd076b8SGabor Kovesdan 			    &names[OLD_FILE].exists, strippath);
32185823a60SPedro F. Giffuni 		else if (strnEQ(s, "--- ", 4)) {
32285823a60SPedro F. Giffuni 			size_t off = 4;
32376df519fSPedro F. Giffuni 			if (piece_of_git && strippath == 957 &&
32476df519fSPedro F. Giffuni 			    strnEQ(s, "--- a/", 6))
32585823a60SPedro F. Giffuni 				off = 6;
32685823a60SPedro F. Giffuni 			names[NEW_FILE].path = fetchname(s + off,
3272dd076b8SGabor Kovesdan 			    &names[NEW_FILE].exists, strippath);
32885823a60SPedro F. Giffuni 		} else if (strnEQ(s, "+++ ", 4)) {
3292dd076b8SGabor Kovesdan 			/* pretend it is the old name */
33085823a60SPedro F. Giffuni 			size_t off = 4;
33176df519fSPedro F. Giffuni 			if (piece_of_git && strippath == 957 &&
33276df519fSPedro F. Giffuni 			    strnEQ(s, "+++ b/", 6))
33385823a60SPedro F. Giffuni 				off = 6;
33485823a60SPedro F. Giffuni 			names[OLD_FILE].path = fetchname(s + off,
3352dd076b8SGabor Kovesdan 			    &names[OLD_FILE].exists, strippath);
33685823a60SPedro F. Giffuni 		} else if (strnEQ(s, "Index:", 6))
3372dd076b8SGabor Kovesdan 			names[INDEX_FILE].path = fetchname(s + 6,
3382dd076b8SGabor Kovesdan 			    &names[INDEX_FILE].exists, strippath);
3392dd076b8SGabor Kovesdan 		else if (strnEQ(s, "Prereq:", 7)) {
3402dd076b8SGabor Kovesdan 			for (t = s + 7; isspace((unsigned char)*t); t++)
3412dd076b8SGabor Kovesdan 				;
342547e0acbSPedro F. Giffuni 			revision = xstrdup(t);
3432b468ebaSPedro F. Giffuni 			for (t = revision;
3442b468ebaSPedro F. Giffuni 			     *t && !isspace((unsigned char)*t); t++)
3452dd076b8SGabor Kovesdan 				;
3462dd076b8SGabor Kovesdan 			*t = '\0';
3472dd076b8SGabor Kovesdan 			if (*revision == '\0') {
3482dd076b8SGabor Kovesdan 				free(revision);
3492dd076b8SGabor Kovesdan 				revision = NULL;
3502dd076b8SGabor Kovesdan 			}
35185823a60SPedro F. Giffuni 		} else if (strnEQ(s, "diff --git a/", 13)) {
35285823a60SPedro F. Giffuni 			/* Git-style diffs. */
35385823a60SPedro F. Giffuni 			piece_of_git = 1;
3542dd076b8SGabor Kovesdan 		} else if (strnEQ(s, "==== ", 5)) {
3552dd076b8SGabor Kovesdan 			/* Perforce-style diffs. */
3562dd076b8SGabor Kovesdan 			if ((t = strstr(s + 5, " - ")) != NULL)
3572dd076b8SGabor Kovesdan 				p4_fetchname(&names[NEW_FILE], t + 3);
3582dd076b8SGabor Kovesdan 			p4_fetchname(&names[OLD_FILE], s + 5);
3592dd076b8SGabor Kovesdan 		}
3602dd076b8SGabor Kovesdan 		if ((!diff_type || diff_type == ED_DIFF) &&
361e91a64deSPedro F. Giffuni 		    first_command_line >= 0 &&
3622dd076b8SGabor Kovesdan 		    strEQ(s, ".\n")) {
3632dd076b8SGabor Kovesdan 			p_indent = indent;
3642dd076b8SGabor Kovesdan 			p_start = first_command_line;
3652dd076b8SGabor Kovesdan 			p_sline = fcl_line;
3662dd076b8SGabor Kovesdan 			retval = ED_DIFF;
3672dd076b8SGabor Kovesdan 			goto scan_exit;
3682dd076b8SGabor Kovesdan 		}
3692dd076b8SGabor Kovesdan 		if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) {
3702dd076b8SGabor Kovesdan 			if (strnEQ(s + 4, "0,0", 3))
3712dd076b8SGabor Kovesdan 				ok_to_create_file = true;
3722dd076b8SGabor Kovesdan 			p_indent = indent;
3732dd076b8SGabor Kovesdan 			p_start = this_line;
3742dd076b8SGabor Kovesdan 			p_sline = p_input_line;
3752dd076b8SGabor Kovesdan 			retval = UNI_DIFF;
3762dd076b8SGabor Kovesdan 			goto scan_exit;
3772dd076b8SGabor Kovesdan 		}
3782dd076b8SGabor Kovesdan 		stars_this_line = strnEQ(s, "********", 8);
3792dd076b8SGabor Kovesdan 		if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
3802dd076b8SGabor Kovesdan 		    strnEQ(s, "*** ", 4)) {
381d3fc0cb8SPedro F. Giffuni 			if (strtolinenum(s + 4, &s) == 0)
3822dd076b8SGabor Kovesdan 				ok_to_create_file = true;
3832dd076b8SGabor Kovesdan 			/*
3842dd076b8SGabor Kovesdan 			 * If this is a new context diff the character just
385e91a64deSPedro F. Giffuni 			 * at the end of the line is a '*'.
3862dd076b8SGabor Kovesdan 			 */
387e91a64deSPedro F. Giffuni 			while (*s && *s != '\n')
3882dd076b8SGabor Kovesdan 				s++;
3892dd076b8SGabor Kovesdan 			p_indent = indent;
3902dd076b8SGabor Kovesdan 			p_start = previous_line;
3912dd076b8SGabor Kovesdan 			p_sline = p_input_line - 1;
3922dd076b8SGabor Kovesdan 			retval = (*(s - 1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
3932dd076b8SGabor Kovesdan 			goto scan_exit;
3942dd076b8SGabor Kovesdan 		}
3952dd076b8SGabor Kovesdan 		if ((!diff_type || diff_type == NORMAL_DIFF) &&
3962dd076b8SGabor Kovesdan 		    last_line_was_command &&
3972dd076b8SGabor Kovesdan 		    (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2))) {
3982dd076b8SGabor Kovesdan 			p_start = previous_line;
3992dd076b8SGabor Kovesdan 			p_sline = p_input_line - 1;
4002dd076b8SGabor Kovesdan 			p_indent = indent;
4012dd076b8SGabor Kovesdan 			retval = NORMAL_DIFF;
4022dd076b8SGabor Kovesdan 			goto scan_exit;
4032dd076b8SGabor Kovesdan 		}
4042dd076b8SGabor Kovesdan 	}
4052dd076b8SGabor Kovesdan scan_exit:
4062dd076b8SGabor Kovesdan 	if (retval == UNI_DIFF) {
4072dd076b8SGabor Kovesdan 		/* unswap old and new */
4082dd076b8SGabor Kovesdan 		struct file_name tmp = names[OLD_FILE];
4092dd076b8SGabor Kovesdan 		names[OLD_FILE] = names[NEW_FILE];
4102dd076b8SGabor Kovesdan 		names[NEW_FILE] = tmp;
4112dd076b8SGabor Kovesdan 	}
41250dacbf6SKyle Evans 
41350dacbf6SKyle Evans 	/* Invalidated */
41450dacbf6SKyle Evans 	free(source_file);
41550dacbf6SKyle Evans 	source_file = NULL;
41650dacbf6SKyle Evans 
41750dacbf6SKyle Evans 	if (retval != 0) {
41850dacbf6SKyle Evans 		/*
41950dacbf6SKyle Evans 		 * If we've successfully determined a diff type, stored in
42050dacbf6SKyle Evans 		 * retval, path == NULL means _PATH_DEVNULL if exists is set.
42150dacbf6SKyle Evans 		 * Explicitly specify it here to make it easier to detect later
42250dacbf6SKyle Evans 		 * on that we're actually creating a file and not that we've
42350dacbf6SKyle Evans 		 * just goofed something up.
42450dacbf6SKyle Evans 		 */
42550dacbf6SKyle Evans 		if (names[OLD_FILE].path != NULL)
42650dacbf6SKyle Evans 			source_file = xstrdup(names[OLD_FILE].path);
42750dacbf6SKyle Evans 		else if (names[OLD_FILE].exists)
42850dacbf6SKyle Evans 			source_file = xstrdup(_PATH_DEVNULL);
42950dacbf6SKyle Evans 	}
4302dd076b8SGabor Kovesdan 	if (filearg[0] == NULL) {
4312dd076b8SGabor Kovesdan 		if (posix)
4322dd076b8SGabor Kovesdan 			filearg[0] = posix_name(names, ok_to_create_file);
4332dd076b8SGabor Kovesdan 		else {
4342dd076b8SGabor Kovesdan 			/* Ignore the Index: name for context diffs, like GNU */
4352dd076b8SGabor Kovesdan 			if (names[OLD_FILE].path != NULL ||
4362dd076b8SGabor Kovesdan 			    names[NEW_FILE].path != NULL) {
4372dd076b8SGabor Kovesdan 				free(names[INDEX_FILE].path);
4382dd076b8SGabor Kovesdan 				names[INDEX_FILE].path = NULL;
4392dd076b8SGabor Kovesdan 			}
4402dd076b8SGabor Kovesdan 			filearg[0] = best_name(names, ok_to_create_file);
4412dd076b8SGabor Kovesdan 		}
4422dd076b8SGabor Kovesdan 	}
4432dd076b8SGabor Kovesdan 
4442dd076b8SGabor Kovesdan 	free(bestguess);
4452dd076b8SGabor Kovesdan 	bestguess = NULL;
4462dd076b8SGabor Kovesdan 	if (filearg[0] != NULL)
447547e0acbSPedro F. Giffuni 		bestguess = xstrdup(filearg[0]);
4482dd076b8SGabor Kovesdan 	else if (!ok_to_create_file) {
4492dd076b8SGabor Kovesdan 		/*
4502dd076b8SGabor Kovesdan 		 * We don't want to create a new file but we need a
4512dd076b8SGabor Kovesdan 		 * filename to set bestguess.  Avoid setting filearg[0]
4522dd076b8SGabor Kovesdan 		 * so the file is not created automatically.
4532dd076b8SGabor Kovesdan 		 */
4542dd076b8SGabor Kovesdan 		if (posix)
4552dd076b8SGabor Kovesdan 			bestguess = posix_name(names, true);
4562dd076b8SGabor Kovesdan 		else
4572dd076b8SGabor Kovesdan 			bestguess = best_name(names, true);
4582dd076b8SGabor Kovesdan 	}
4592dd076b8SGabor Kovesdan 	free(names[OLD_FILE].path);
4602dd076b8SGabor Kovesdan 	free(names[NEW_FILE].path);
4612dd076b8SGabor Kovesdan 	free(names[INDEX_FILE].path);
4622dd076b8SGabor Kovesdan 	return retval;
4632dd076b8SGabor Kovesdan }
4642dd076b8SGabor Kovesdan 
4652dd076b8SGabor Kovesdan /*
4662dd076b8SGabor Kovesdan  * Remember where this patch ends so we know where to start up again.
4672dd076b8SGabor Kovesdan  */
4682dd076b8SGabor Kovesdan static void
469e91a64deSPedro F. Giffuni next_intuit_at(off_t file_pos, LINENUM file_line)
4702dd076b8SGabor Kovesdan {
4712dd076b8SGabor Kovesdan 	p_base = file_pos;
4722dd076b8SGabor Kovesdan 	p_bline = file_line;
4732dd076b8SGabor Kovesdan }
4742dd076b8SGabor Kovesdan 
4752dd076b8SGabor Kovesdan /*
476e91a64deSPedro F. Giffuni  * Basically a verbose fseeko() to the actual diff listing.
4772dd076b8SGabor Kovesdan  */
4782dd076b8SGabor Kovesdan static void
479e91a64deSPedro F. Giffuni skip_to(off_t file_pos, LINENUM file_line)
4802dd076b8SGabor Kovesdan {
4812dd076b8SGabor Kovesdan 	size_t	len;
4822dd076b8SGabor Kovesdan 
4832dd076b8SGabor Kovesdan 	if (p_base > file_pos)
484e91a64deSPedro F. Giffuni 		fatal("Internal error: seek %lld>%lld\n",
485e91a64deSPedro F. Giffuni 		   (long long)p_base, (long long)file_pos);
4862dd076b8SGabor Kovesdan 	if (verbose && p_base < file_pos) {
487e91a64deSPedro F. Giffuni 		fseeko(pfp, p_base, SEEK_SET);
4882dd076b8SGabor Kovesdan 		say("The text leading up to this was:\n--------------------------\n");
489e91a64deSPedro F. Giffuni 		while (ftello(pfp) < file_pos) {
4902dd076b8SGabor Kovesdan 			len = pgets(false);
4912dd076b8SGabor Kovesdan 			if (len == 0)
4922dd076b8SGabor Kovesdan 				fatal("Unexpected end of file\n");
4932dd076b8SGabor Kovesdan 			say("|%s", buf);
4942dd076b8SGabor Kovesdan 		}
4952dd076b8SGabor Kovesdan 		say("--------------------------\n");
4962dd076b8SGabor Kovesdan 	} else
497e91a64deSPedro F. Giffuni 		fseeko(pfp, file_pos, SEEK_SET);
4982dd076b8SGabor Kovesdan 	p_input_line = file_line - 1;
4992dd076b8SGabor Kovesdan }
5002dd076b8SGabor Kovesdan 
5012dd076b8SGabor Kovesdan /* Make this a function for better debugging.  */
5022dd076b8SGabor Kovesdan static void
5032dd076b8SGabor Kovesdan malformed(void)
5042dd076b8SGabor Kovesdan {
5052dd076b8SGabor Kovesdan 	fatal("malformed patch at line %ld: %s", p_input_line, buf);
5062dd076b8SGabor Kovesdan 	/* about as informative as "Syntax error" in C */
5072dd076b8SGabor Kovesdan }
5082dd076b8SGabor Kovesdan 
5092dd076b8SGabor Kovesdan /*
5102dd076b8SGabor Kovesdan  * True if the line has been discarded (i.e. it is a line saying
5112dd076b8SGabor Kovesdan  *  "\ No newline at end of file".)
5122dd076b8SGabor Kovesdan  */
5132dd076b8SGabor Kovesdan static bool
5142dd076b8SGabor Kovesdan remove_special_line(void)
5152dd076b8SGabor Kovesdan {
5162dd076b8SGabor Kovesdan 	int	c;
5172dd076b8SGabor Kovesdan 
5182dd076b8SGabor Kovesdan 	c = fgetc(pfp);
5192dd076b8SGabor Kovesdan 	if (c == '\\') {
5202dd076b8SGabor Kovesdan 		do {
5212dd076b8SGabor Kovesdan 			c = fgetc(pfp);
5222dd076b8SGabor Kovesdan 		} while (c != EOF && c != '\n');
5232dd076b8SGabor Kovesdan 
5242dd076b8SGabor Kovesdan 		return true;
5252dd076b8SGabor Kovesdan 	}
5262dd076b8SGabor Kovesdan 	if (c != EOF)
527e91a64deSPedro F. Giffuni 		fseeko(pfp, -1, SEEK_CUR);
5282dd076b8SGabor Kovesdan 
5292dd076b8SGabor Kovesdan 	return false;
5302dd076b8SGabor Kovesdan }
5312dd076b8SGabor Kovesdan 
5322dd076b8SGabor Kovesdan /*
5332dd076b8SGabor Kovesdan  * True if there is more of the current diff listing to process.
5342dd076b8SGabor Kovesdan  */
5352dd076b8SGabor Kovesdan bool
5362dd076b8SGabor Kovesdan another_hunk(void)
5372dd076b8SGabor Kovesdan {
538e91a64deSPedro F. Giffuni 	off_t	line_beginning;			/* file pos of the current line */
5392dd076b8SGabor Kovesdan 	LINENUM	repl_beginning;			/* index of --- line */
5402dd076b8SGabor Kovesdan 	LINENUM	fillcnt;			/* #lines of missing ptrn or repl */
5412dd076b8SGabor Kovesdan 	LINENUM	fillsrc;			/* index of first line to copy */
5422dd076b8SGabor Kovesdan 	LINENUM	filldst;			/* index of first missing line */
543463a577bSEitan Adler 	bool	ptrn_spaces_eaten;		/* ptrn was slightly malformed */
5442dd076b8SGabor Kovesdan 	bool	repl_could_be_missing;		/* no + or ! lines in this hunk */
5452dd076b8SGabor Kovesdan 	bool	repl_missing;			/* we are now backtracking */
546e91a64deSPedro F. Giffuni 	off_t	repl_backtrack_position;	/* file pos of first repl line */
5472dd076b8SGabor Kovesdan 	LINENUM	repl_patch_line;		/* input line number for same */
5482dd076b8SGabor Kovesdan 	LINENUM	ptrn_copiable;			/* # of copiable lines in ptrn */
5492dd076b8SGabor Kovesdan 	char	*s;
5502dd076b8SGabor Kovesdan 	size_t	len;
5512dd076b8SGabor Kovesdan 	int	context = 0;
5522dd076b8SGabor Kovesdan 
5532dd076b8SGabor Kovesdan 	while (p_end >= 0) {
5542dd076b8SGabor Kovesdan 		if (p_end == p_efake)
5552dd076b8SGabor Kovesdan 			p_end = p_bfake;	/* don't free twice */
5562dd076b8SGabor Kovesdan 		else
5572dd076b8SGabor Kovesdan 			free(p_line[p_end]);
5582dd076b8SGabor Kovesdan 		p_end--;
5592dd076b8SGabor Kovesdan 	}
5602dd076b8SGabor Kovesdan 	p_efake = -1;
5612dd076b8SGabor Kovesdan 
5622dd076b8SGabor Kovesdan 	p_max = hunkmax;	/* gets reduced when --- found */
5632dd076b8SGabor Kovesdan 	if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
564e91a64deSPedro F. Giffuni 		line_beginning = ftello(pfp);
5652dd076b8SGabor Kovesdan 		repl_beginning = 0;
5662dd076b8SGabor Kovesdan 		fillcnt = 0;
5672dd076b8SGabor Kovesdan 		fillsrc = 0;
5682dd076b8SGabor Kovesdan 		filldst = 0;
5692dd076b8SGabor Kovesdan 		ptrn_spaces_eaten = false;
5702dd076b8SGabor Kovesdan 		repl_could_be_missing = true;
5712dd076b8SGabor Kovesdan 		repl_missing = false;
5722dd076b8SGabor Kovesdan 		repl_backtrack_position = 0;
5732dd076b8SGabor Kovesdan 		repl_patch_line = 0;
5742dd076b8SGabor Kovesdan 		ptrn_copiable = 0;
5752dd076b8SGabor Kovesdan 
5762dd076b8SGabor Kovesdan 		len = pgets(true);
5772dd076b8SGabor Kovesdan 		p_input_line++;
5782dd076b8SGabor Kovesdan 		if (len == 0 || strnNE(buf, "********", 8)) {
5792dd076b8SGabor Kovesdan 			next_intuit_at(line_beginning, p_input_line);
5802dd076b8SGabor Kovesdan 			return false;
5812dd076b8SGabor Kovesdan 		}
5822dd076b8SGabor Kovesdan 		p_context = 100;
5832dd076b8SGabor Kovesdan 		p_hunk_beg = p_input_line + 1;
5842dd076b8SGabor Kovesdan 		while (p_end < p_max) {
585e91a64deSPedro F. Giffuni 			line_beginning = ftello(pfp);
5862dd076b8SGabor Kovesdan 			len = pgets(true);
5872dd076b8SGabor Kovesdan 			p_input_line++;
5882dd076b8SGabor Kovesdan 			if (len == 0) {
5892dd076b8SGabor Kovesdan 				if (repl_beginning && repl_could_be_missing) {
5902dd076b8SGabor Kovesdan 					repl_missing = true;
5912dd076b8SGabor Kovesdan 					goto hunk_done;
5922dd076b8SGabor Kovesdan 				}
5932dd076b8SGabor Kovesdan 				fatal("unexpected end of file in patch\n");
5942dd076b8SGabor Kovesdan 			}
5952dd076b8SGabor Kovesdan 			p_end++;
5962dd076b8SGabor Kovesdan 			if (p_end >= hunkmax)
5972dd076b8SGabor Kovesdan 				fatal("Internal error: hunk larger than hunk "
5982dd076b8SGabor Kovesdan 				    "buffer size");
5992dd076b8SGabor Kovesdan 			p_char[p_end] = *buf;
6002dd076b8SGabor Kovesdan 			p_line[p_end] = NULL;
6012dd076b8SGabor Kovesdan 			switch (*buf) {
6022dd076b8SGabor Kovesdan 			case '*':
6032dd076b8SGabor Kovesdan 				if (strnEQ(buf, "********", 8)) {
6042dd076b8SGabor Kovesdan 					if (repl_beginning && repl_could_be_missing) {
6052dd076b8SGabor Kovesdan 						repl_missing = true;
6062dd076b8SGabor Kovesdan 						goto hunk_done;
6072dd076b8SGabor Kovesdan 					} else
6082dd076b8SGabor Kovesdan 						fatal("unexpected end of hunk "
6092dd076b8SGabor Kovesdan 						    "at line %ld\n",
6102dd076b8SGabor Kovesdan 						    p_input_line);
6112dd076b8SGabor Kovesdan 				}
6122dd076b8SGabor Kovesdan 				if (p_end != 0) {
6132dd076b8SGabor Kovesdan 					if (repl_beginning && repl_could_be_missing) {
6142dd076b8SGabor Kovesdan 						repl_missing = true;
6152dd076b8SGabor Kovesdan 						goto hunk_done;
6162dd076b8SGabor Kovesdan 					}
6172dd076b8SGabor Kovesdan 					fatal("unexpected *** at line %ld: %s",
6182dd076b8SGabor Kovesdan 					    p_input_line, buf);
6192dd076b8SGabor Kovesdan 				}
6202dd076b8SGabor Kovesdan 				context = 0;
6212dd076b8SGabor Kovesdan 				p_line[p_end] = savestr(buf);
6222dd076b8SGabor Kovesdan 				if (out_of_mem) {
6232dd076b8SGabor Kovesdan 					p_end--;
6242dd076b8SGabor Kovesdan 					return false;
6252dd076b8SGabor Kovesdan 				}
6262b468ebaSPedro F. Giffuni 				for (s = buf;
6272b468ebaSPedro F. Giffuni 				     *s && !isdigit((unsigned char)*s); s++)
6282dd076b8SGabor Kovesdan 					;
6292dd076b8SGabor Kovesdan 				if (!*s)
6302dd076b8SGabor Kovesdan 					malformed();
6312dd076b8SGabor Kovesdan 				if (strnEQ(s, "0,0", 3))
6322dd076b8SGabor Kovesdan 					memmove(s, s + 2, strlen(s + 2) + 1);
633d3fc0cb8SPedro F. Giffuni 				p_first = strtolinenum(s, &s);
6342dd076b8SGabor Kovesdan 				if (*s == ',') {
6352b468ebaSPedro F. Giffuni 					for (;
6362b468ebaSPedro F. Giffuni 					     *s && !isdigit((unsigned char)*s); s++)
6372dd076b8SGabor Kovesdan 						;
6382dd076b8SGabor Kovesdan 					if (!*s)
6392dd076b8SGabor Kovesdan 						malformed();
640d3fc0cb8SPedro F. Giffuni 					p_ptrn_lines = strtolinenum(s, &s) - p_first + 1;
641d3fc0cb8SPedro F. Giffuni 					if (p_ptrn_lines < 0)
642d3fc0cb8SPedro F. Giffuni 						malformed();
6432dd076b8SGabor Kovesdan 				} else if (p_first)
6442dd076b8SGabor Kovesdan 					p_ptrn_lines = 1;
6452dd076b8SGabor Kovesdan 				else {
6462dd076b8SGabor Kovesdan 					p_ptrn_lines = 0;
6472dd076b8SGabor Kovesdan 					p_first = 1;
6482dd076b8SGabor Kovesdan 				}
649d3fc0cb8SPedro F. Giffuni 				if (p_first >= LINENUM_MAX - p_ptrn_lines ||
650d3fc0cb8SPedro F. Giffuni 				    p_ptrn_lines >= LINENUM_MAX - 6)
651d3fc0cb8SPedro F. Giffuni 					malformed();
6522dd076b8SGabor Kovesdan 
6532dd076b8SGabor Kovesdan 				/* we need this much at least */
6542dd076b8SGabor Kovesdan 				p_max = p_ptrn_lines + 6;
6552dd076b8SGabor Kovesdan 				while (p_max >= hunkmax)
6562dd076b8SGabor Kovesdan 					grow_hunkmax();
6572dd076b8SGabor Kovesdan 				p_max = hunkmax;
6582dd076b8SGabor Kovesdan 				break;
6592dd076b8SGabor Kovesdan 			case '-':
6602dd076b8SGabor Kovesdan 				if (buf[1] == '-') {
6612dd076b8SGabor Kovesdan 					if (repl_beginning ||
6622dd076b8SGabor Kovesdan 					    (p_end != p_ptrn_lines + 1 +
6632dd076b8SGabor Kovesdan 					    (p_char[p_end - 1] == '\n'))) {
6642dd076b8SGabor Kovesdan 						if (p_end == 1) {
6652dd076b8SGabor Kovesdan 							/*
6662dd076b8SGabor Kovesdan 							 * `old' lines were omitted;
6672dd076b8SGabor Kovesdan 							 * set up to fill them in
6682dd076b8SGabor Kovesdan 							 * from 'new' context lines.
6692dd076b8SGabor Kovesdan 							 */
6702dd076b8SGabor Kovesdan 							p_end = p_ptrn_lines + 1;
6712dd076b8SGabor Kovesdan 							fillsrc = p_end + 1;
6722dd076b8SGabor Kovesdan 							filldst = 1;
6732dd076b8SGabor Kovesdan 							fillcnt = p_ptrn_lines;
6742dd076b8SGabor Kovesdan 						} else {
6752dd076b8SGabor Kovesdan 							if (repl_beginning) {
6762dd076b8SGabor Kovesdan 								if (repl_could_be_missing) {
6772dd076b8SGabor Kovesdan 									repl_missing = true;
6782dd076b8SGabor Kovesdan 									goto hunk_done;
6792dd076b8SGabor Kovesdan 								}
6802dd076b8SGabor Kovesdan 								fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n",
6812dd076b8SGabor Kovesdan 								    p_input_line, p_hunk_beg + repl_beginning);
6822dd076b8SGabor Kovesdan 							} else {
6832dd076b8SGabor Kovesdan 								fatal("%s \"---\" at line %ld--check line numbers at line %ld\n",
6842dd076b8SGabor Kovesdan 								    (p_end <= p_ptrn_lines
6852dd076b8SGabor Kovesdan 								    ? "Premature"
6862dd076b8SGabor Kovesdan 								    : "Overdue"),
6872dd076b8SGabor Kovesdan 								    p_input_line, p_hunk_beg);
6882dd076b8SGabor Kovesdan 							}
6892dd076b8SGabor Kovesdan 						}
6902dd076b8SGabor Kovesdan 					}
6912dd076b8SGabor Kovesdan 					repl_beginning = p_end;
692e91a64deSPedro F. Giffuni 					repl_backtrack_position = ftello(pfp);
6932dd076b8SGabor Kovesdan 					repl_patch_line = p_input_line;
6942dd076b8SGabor Kovesdan 					p_line[p_end] = savestr(buf);
6952dd076b8SGabor Kovesdan 					if (out_of_mem) {
6962dd076b8SGabor Kovesdan 						p_end--;
6972dd076b8SGabor Kovesdan 						return false;
6982dd076b8SGabor Kovesdan 					}
6992dd076b8SGabor Kovesdan 					p_char[p_end] = '=';
7002dd076b8SGabor Kovesdan 					for (s = buf; *s && !isdigit((unsigned char)*s); s++)
7012dd076b8SGabor Kovesdan 						;
7022dd076b8SGabor Kovesdan 					if (!*s)
7032dd076b8SGabor Kovesdan 						malformed();
704d3fc0cb8SPedro F. Giffuni 					p_newfirst = strtolinenum(s, &s);
7052dd076b8SGabor Kovesdan 					if (*s == ',') {
7062dd076b8SGabor Kovesdan 						for (; *s && !isdigit((unsigned char)*s); s++)
7072dd076b8SGabor Kovesdan 							;
7082dd076b8SGabor Kovesdan 						if (!*s)
7092dd076b8SGabor Kovesdan 							malformed();
710d3fc0cb8SPedro F. Giffuni 						p_repl_lines = strtolinenum(s, &s) -
7112dd076b8SGabor Kovesdan 						    p_newfirst + 1;
712d3fc0cb8SPedro F. Giffuni 						if (p_repl_lines < 0)
713d3fc0cb8SPedro F. Giffuni 							malformed();
7142dd076b8SGabor Kovesdan 					} else if (p_newfirst)
7152dd076b8SGabor Kovesdan 						p_repl_lines = 1;
7162dd076b8SGabor Kovesdan 					else {
7172dd076b8SGabor Kovesdan 						p_repl_lines = 0;
7182dd076b8SGabor Kovesdan 						p_newfirst = 1;
7192dd076b8SGabor Kovesdan 					}
720d3fc0cb8SPedro F. Giffuni 					if (p_newfirst >= LINENUM_MAX - p_repl_lines ||
721d3fc0cb8SPedro F. Giffuni 					    p_repl_lines >= LINENUM_MAX - p_end)
722d3fc0cb8SPedro F. Giffuni 						malformed();
7232dd076b8SGabor Kovesdan 					p_max = p_repl_lines + p_end;
7242dd076b8SGabor Kovesdan 					if (p_max > MAXHUNKSIZE)
7252dd076b8SGabor Kovesdan 						fatal("hunk too large (%ld lines) at line %ld: %s",
7262dd076b8SGabor Kovesdan 						    p_max, p_input_line, buf);
7272dd076b8SGabor Kovesdan 					while (p_max >= hunkmax)
7282dd076b8SGabor Kovesdan 						grow_hunkmax();
7292dd076b8SGabor Kovesdan 					if (p_repl_lines != ptrn_copiable &&
7302dd076b8SGabor Kovesdan 					    (p_context != 0 || p_repl_lines != 1))
7312dd076b8SGabor Kovesdan 						repl_could_be_missing = false;
7322dd076b8SGabor Kovesdan 					break;
7332dd076b8SGabor Kovesdan 				}
7342dd076b8SGabor Kovesdan 				goto change_line;
7352dd076b8SGabor Kovesdan 			case '+':
7362dd076b8SGabor Kovesdan 			case '!':
7372dd076b8SGabor Kovesdan 				repl_could_be_missing = false;
7382dd076b8SGabor Kovesdan 		change_line:
7392dd076b8SGabor Kovesdan 				if (buf[1] == '\n' && canonicalize)
7402dd076b8SGabor Kovesdan 					strlcpy(buf + 1, " \n", buf_size - 1);
7412b468ebaSPedro F. Giffuni 				if (!isspace((unsigned char)buf[1]) &&
7422b468ebaSPedro F. Giffuni 				    buf[1] != '>' && buf[1] != '<' &&
7432dd076b8SGabor Kovesdan 				    repl_beginning && repl_could_be_missing) {
7442dd076b8SGabor Kovesdan 					repl_missing = true;
7452dd076b8SGabor Kovesdan 					goto hunk_done;
7462dd076b8SGabor Kovesdan 				}
7472dd076b8SGabor Kovesdan 				if (context >= 0) {
7482dd076b8SGabor Kovesdan 					if (context < p_context)
7492dd076b8SGabor Kovesdan 						p_context = context;
7502dd076b8SGabor Kovesdan 					context = -1000;
7512dd076b8SGabor Kovesdan 				}
7522dd076b8SGabor Kovesdan 				p_line[p_end] = savestr(buf + 2);
7532dd076b8SGabor Kovesdan 				if (out_of_mem) {
7542dd076b8SGabor Kovesdan 					p_end--;
7552dd076b8SGabor Kovesdan 					return false;
7562dd076b8SGabor Kovesdan 				}
7572dd076b8SGabor Kovesdan 				if (p_end == p_ptrn_lines) {
7582dd076b8SGabor Kovesdan 					if (remove_special_line()) {
7592dd076b8SGabor Kovesdan 						int	l;
7602dd076b8SGabor Kovesdan 
7612dd076b8SGabor Kovesdan 						l = strlen(p_line[p_end]) - 1;
7622dd076b8SGabor Kovesdan 						(p_line[p_end])[l] = 0;
7632dd076b8SGabor Kovesdan 					}
7642dd076b8SGabor Kovesdan 				}
7652dd076b8SGabor Kovesdan 				break;
7662dd076b8SGabor Kovesdan 			case '\t':
7672dd076b8SGabor Kovesdan 			case '\n':	/* assume the 2 spaces got eaten */
7682dd076b8SGabor Kovesdan 				if (repl_beginning && repl_could_be_missing &&
7692dd076b8SGabor Kovesdan 				    (!ptrn_spaces_eaten ||
7702dd076b8SGabor Kovesdan 				    diff_type == NEW_CONTEXT_DIFF)) {
7712dd076b8SGabor Kovesdan 					repl_missing = true;
7722dd076b8SGabor Kovesdan 					goto hunk_done;
7732dd076b8SGabor Kovesdan 				}
7742dd076b8SGabor Kovesdan 				p_line[p_end] = savestr(buf);
7752dd076b8SGabor Kovesdan 				if (out_of_mem) {
7762dd076b8SGabor Kovesdan 					p_end--;
7772dd076b8SGabor Kovesdan 					return false;
7782dd076b8SGabor Kovesdan 				}
7792dd076b8SGabor Kovesdan 				if (p_end != p_ptrn_lines + 1) {
7802dd076b8SGabor Kovesdan 					ptrn_spaces_eaten |= (repl_beginning != 0);
7812dd076b8SGabor Kovesdan 					context++;
7822dd076b8SGabor Kovesdan 					if (!repl_beginning)
7832dd076b8SGabor Kovesdan 						ptrn_copiable++;
7842dd076b8SGabor Kovesdan 					p_char[p_end] = ' ';
7852dd076b8SGabor Kovesdan 				}
7862dd076b8SGabor Kovesdan 				break;
7872dd076b8SGabor Kovesdan 			case ' ':
7882dd076b8SGabor Kovesdan 				if (!isspace((unsigned char)buf[1]) &&
7892dd076b8SGabor Kovesdan 				    repl_beginning && repl_could_be_missing) {
7902dd076b8SGabor Kovesdan 					repl_missing = true;
7912dd076b8SGabor Kovesdan 					goto hunk_done;
7922dd076b8SGabor Kovesdan 				}
7932dd076b8SGabor Kovesdan 				context++;
7942dd076b8SGabor Kovesdan 				if (!repl_beginning)
7952dd076b8SGabor Kovesdan 					ptrn_copiable++;
7962dd076b8SGabor Kovesdan 				p_line[p_end] = savestr(buf + 2);
7972dd076b8SGabor Kovesdan 				if (out_of_mem) {
7982dd076b8SGabor Kovesdan 					p_end--;
7992dd076b8SGabor Kovesdan 					return false;
8002dd076b8SGabor Kovesdan 				}
8012dd076b8SGabor Kovesdan 				break;
8022dd076b8SGabor Kovesdan 			default:
8032dd076b8SGabor Kovesdan 				if (repl_beginning && repl_could_be_missing) {
8042dd076b8SGabor Kovesdan 					repl_missing = true;
8052dd076b8SGabor Kovesdan 					goto hunk_done;
8062dd076b8SGabor Kovesdan 				}
8072dd076b8SGabor Kovesdan 				malformed();
8082dd076b8SGabor Kovesdan 			}
8092dd076b8SGabor Kovesdan 			/* set up p_len for strncmp() so we don't have to */
8102dd076b8SGabor Kovesdan 			/* assume null termination */
8112dd076b8SGabor Kovesdan 			if (p_line[p_end])
8122dd076b8SGabor Kovesdan 				p_len[p_end] = strlen(p_line[p_end]);
8132dd076b8SGabor Kovesdan 			else
8142dd076b8SGabor Kovesdan 				p_len[p_end] = 0;
8152dd076b8SGabor Kovesdan 		}
8162dd076b8SGabor Kovesdan 
8172dd076b8SGabor Kovesdan hunk_done:
8182dd076b8SGabor Kovesdan 		if (p_end >= 0 && !repl_beginning)
8192dd076b8SGabor Kovesdan 			fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
8202dd076b8SGabor Kovesdan 
8212dd076b8SGabor Kovesdan 		if (repl_missing) {
8222dd076b8SGabor Kovesdan 
8232dd076b8SGabor Kovesdan 			/* reset state back to just after --- */
8242dd076b8SGabor Kovesdan 			p_input_line = repl_patch_line;
8252dd076b8SGabor Kovesdan 			for (p_end--; p_end > repl_beginning; p_end--)
8262dd076b8SGabor Kovesdan 				free(p_line[p_end]);
827e91a64deSPedro F. Giffuni 			fseeko(pfp, repl_backtrack_position, SEEK_SET);
8282dd076b8SGabor Kovesdan 
8292dd076b8SGabor Kovesdan 			/* redundant 'new' context lines were omitted - set */
8302dd076b8SGabor Kovesdan 			/* up to fill them in from the old file context */
8312dd076b8SGabor Kovesdan 			if (!p_context && p_repl_lines == 1) {
8322dd076b8SGabor Kovesdan 				p_repl_lines = 0;
8332dd076b8SGabor Kovesdan 				p_max--;
8342dd076b8SGabor Kovesdan 			}
8352dd076b8SGabor Kovesdan 			fillsrc = 1;
8362dd076b8SGabor Kovesdan 			filldst = repl_beginning + 1;
8372dd076b8SGabor Kovesdan 			fillcnt = p_repl_lines;
8382dd076b8SGabor Kovesdan 			p_end = p_max;
8392dd076b8SGabor Kovesdan 		} else if (!p_context && fillcnt == 1) {
8402dd076b8SGabor Kovesdan 			/* the first hunk was a null hunk with no context */
8412dd076b8SGabor Kovesdan 			/* and we were expecting one line -- fix it up. */
8422dd076b8SGabor Kovesdan 			while (filldst < p_end) {
8432dd076b8SGabor Kovesdan 				p_line[filldst] = p_line[filldst + 1];
8442dd076b8SGabor Kovesdan 				p_char[filldst] = p_char[filldst + 1];
8452dd076b8SGabor Kovesdan 				p_len[filldst] = p_len[filldst + 1];
8462dd076b8SGabor Kovesdan 				filldst++;
8472dd076b8SGabor Kovesdan 			}
8482dd076b8SGabor Kovesdan #if 0
8492dd076b8SGabor Kovesdan 			repl_beginning--;	/* this doesn't need to be fixed */
8502dd076b8SGabor Kovesdan #endif
8512dd076b8SGabor Kovesdan 			p_end--;
8522dd076b8SGabor Kovesdan 			p_first++;	/* do append rather than insert */
8532dd076b8SGabor Kovesdan 			fillcnt = 0;
8542dd076b8SGabor Kovesdan 			p_ptrn_lines = 0;
8552dd076b8SGabor Kovesdan 		}
8562dd076b8SGabor Kovesdan 		if (diff_type == CONTEXT_DIFF &&
8572dd076b8SGabor Kovesdan 		    (fillcnt || (p_first > 1 && ptrn_copiable > 2 * p_context))) {
8582dd076b8SGabor Kovesdan 			if (verbose)
8592dd076b8SGabor Kovesdan 				say("%s\n%s\n%s\n",
8602dd076b8SGabor Kovesdan 				    "(Fascinating--this is really a new-style context diff but without",
8612dd076b8SGabor Kovesdan 				    "the telltale extra asterisks on the *** line that usually indicate",
8622dd076b8SGabor Kovesdan 				    "the new style...)");
8632dd076b8SGabor Kovesdan 			diff_type = NEW_CONTEXT_DIFF;
8642dd076b8SGabor Kovesdan 		}
8652dd076b8SGabor Kovesdan 		/* if there were omitted context lines, fill them in now */
8662dd076b8SGabor Kovesdan 		if (fillcnt) {
8672dd076b8SGabor Kovesdan 			p_bfake = filldst;	/* remember where not to free() */
8682dd076b8SGabor Kovesdan 			p_efake = filldst + fillcnt - 1;
8692dd076b8SGabor Kovesdan 			while (fillcnt-- > 0) {
8702dd076b8SGabor Kovesdan 				while (fillsrc <= p_end && p_char[fillsrc] != ' ')
8712dd076b8SGabor Kovesdan 					fillsrc++;
8722dd076b8SGabor Kovesdan 				if (fillsrc > p_end)
8732dd076b8SGabor Kovesdan 					fatal("replacement text or line numbers mangled in hunk at line %ld\n",
8742dd076b8SGabor Kovesdan 					    p_hunk_beg);
8752dd076b8SGabor Kovesdan 				p_line[filldst] = p_line[fillsrc];
8762dd076b8SGabor Kovesdan 				p_char[filldst] = p_char[fillsrc];
8772dd076b8SGabor Kovesdan 				p_len[filldst] = p_len[fillsrc];
8782dd076b8SGabor Kovesdan 				fillsrc++;
8792dd076b8SGabor Kovesdan 				filldst++;
8802dd076b8SGabor Kovesdan 			}
8812dd076b8SGabor Kovesdan 			while (fillsrc <= p_end && fillsrc != repl_beginning &&
8822dd076b8SGabor Kovesdan 			    p_char[fillsrc] != ' ')
8832dd076b8SGabor Kovesdan 				fillsrc++;
8842dd076b8SGabor Kovesdan #ifdef DEBUGGING
8852dd076b8SGabor Kovesdan 			if (debug & 64)
8862dd076b8SGabor Kovesdan 				printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
8872dd076b8SGabor Kovesdan 				fillsrc, filldst, repl_beginning, p_end + 1);
8882dd076b8SGabor Kovesdan #endif
8892dd076b8SGabor Kovesdan 			if (fillsrc != p_end + 1 && fillsrc != repl_beginning)
8902dd076b8SGabor Kovesdan 				malformed();
8912dd076b8SGabor Kovesdan 			if (filldst != p_end + 1 && filldst != repl_beginning)
8922dd076b8SGabor Kovesdan 				malformed();
8932dd076b8SGabor Kovesdan 		}
8942dd076b8SGabor Kovesdan 		if (p_line[p_end] != NULL) {
8952dd076b8SGabor Kovesdan 			if (remove_special_line()) {
8962dd076b8SGabor Kovesdan 				p_len[p_end] -= 1;
8972dd076b8SGabor Kovesdan 				(p_line[p_end])[p_len[p_end]] = 0;
8982dd076b8SGabor Kovesdan 			}
8992dd076b8SGabor Kovesdan 		}
9002dd076b8SGabor Kovesdan 	} else if (diff_type == UNI_DIFF) {
9012dd076b8SGabor Kovesdan 		LINENUM	fillold;	/* index of old lines */
9022dd076b8SGabor Kovesdan 		LINENUM	fillnew;	/* index of new lines */
9032dd076b8SGabor Kovesdan 		char	ch;
9042dd076b8SGabor Kovesdan 
905e91a64deSPedro F. Giffuni 		line_beginning = ftello(pfp); /* file pos of the current line */
9062dd076b8SGabor Kovesdan 		len = pgets(true);
9072dd076b8SGabor Kovesdan 		p_input_line++;
9082dd076b8SGabor Kovesdan 		if (len == 0 || strnNE(buf, "@@ -", 4)) {
9092dd076b8SGabor Kovesdan 			next_intuit_at(line_beginning, p_input_line);
9102dd076b8SGabor Kovesdan 			return false;
9112dd076b8SGabor Kovesdan 		}
9122dd076b8SGabor Kovesdan 		s = buf + 4;
9132dd076b8SGabor Kovesdan 		if (!*s)
9142dd076b8SGabor Kovesdan 			malformed();
915d3fc0cb8SPedro F. Giffuni 		p_first = strtolinenum(s, &s);
9162dd076b8SGabor Kovesdan 		if (*s == ',') {
917d3fc0cb8SPedro F. Giffuni 			p_ptrn_lines = strtolinenum(s + 1, &s);
9182dd076b8SGabor Kovesdan 		} else
9192dd076b8SGabor Kovesdan 			p_ptrn_lines = 1;
9202dd076b8SGabor Kovesdan 		if (*s == ' ')
9212dd076b8SGabor Kovesdan 			s++;
9222dd076b8SGabor Kovesdan 		if (*s != '+' || !*++s)
9232dd076b8SGabor Kovesdan 			malformed();
924d3fc0cb8SPedro F. Giffuni 		p_newfirst = strtolinenum(s, &s);
9252dd076b8SGabor Kovesdan 		if (*s == ',') {
926d3fc0cb8SPedro F. Giffuni 			p_repl_lines = strtolinenum(s + 1, &s);
9272dd076b8SGabor Kovesdan 		} else
9282dd076b8SGabor Kovesdan 			p_repl_lines = 1;
9292dd076b8SGabor Kovesdan 		if (*s == ' ')
9302dd076b8SGabor Kovesdan 			s++;
9312dd076b8SGabor Kovesdan 		if (*s != '@')
9322dd076b8SGabor Kovesdan 			malformed();
933d3fc0cb8SPedro F. Giffuni 		if (p_first >= LINENUM_MAX - p_ptrn_lines ||
934d3fc0cb8SPedro F. Giffuni 		    p_newfirst > LINENUM_MAX - p_repl_lines ||
935d3fc0cb8SPedro F. Giffuni 		    p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1)
936d3fc0cb8SPedro F. Giffuni 			malformed();
9372dd076b8SGabor Kovesdan 		if (!p_ptrn_lines)
9382dd076b8SGabor Kovesdan 			p_first++;	/* do append rather than insert */
9392dd076b8SGabor Kovesdan 		p_max = p_ptrn_lines + p_repl_lines + 1;
9402dd076b8SGabor Kovesdan 		while (p_max >= hunkmax)
9412dd076b8SGabor Kovesdan 			grow_hunkmax();
9422dd076b8SGabor Kovesdan 		fillold = 1;
9432dd076b8SGabor Kovesdan 		fillnew = fillold + p_ptrn_lines;
9442dd076b8SGabor Kovesdan 		p_end = fillnew + p_repl_lines;
9452dd076b8SGabor Kovesdan 		snprintf(buf, buf_size, "*** %ld,%ld ****\n", p_first,
9462dd076b8SGabor Kovesdan 		    p_first + p_ptrn_lines - 1);
9472dd076b8SGabor Kovesdan 		p_line[0] = savestr(buf);
9482dd076b8SGabor Kovesdan 		if (out_of_mem) {
9492dd076b8SGabor Kovesdan 			p_end = -1;
9502dd076b8SGabor Kovesdan 			return false;
9512dd076b8SGabor Kovesdan 		}
9522dd076b8SGabor Kovesdan 		p_char[0] = '*';
9532dd076b8SGabor Kovesdan 		snprintf(buf, buf_size, "--- %ld,%ld ----\n", p_newfirst,
9542dd076b8SGabor Kovesdan 		    p_newfirst + p_repl_lines - 1);
9552dd076b8SGabor Kovesdan 		p_line[fillnew] = savestr(buf);
9562dd076b8SGabor Kovesdan 		if (out_of_mem) {
9572dd076b8SGabor Kovesdan 			p_end = 0;
9582dd076b8SGabor Kovesdan 			return false;
9592dd076b8SGabor Kovesdan 		}
9602dd076b8SGabor Kovesdan 		p_char[fillnew++] = '=';
9612dd076b8SGabor Kovesdan 		p_context = 100;
9622dd076b8SGabor Kovesdan 		context = 0;
9632dd076b8SGabor Kovesdan 		p_hunk_beg = p_input_line + 1;
9642dd076b8SGabor Kovesdan 		while (fillold <= p_ptrn_lines || fillnew <= p_end) {
965e91a64deSPedro F. Giffuni 			line_beginning = ftello(pfp);
9662dd076b8SGabor Kovesdan 			len = pgets(true);
9672dd076b8SGabor Kovesdan 			p_input_line++;
9682dd076b8SGabor Kovesdan 			if (len == 0) {
9692dd076b8SGabor Kovesdan 				if (p_max - fillnew < 3) {
9702dd076b8SGabor Kovesdan 					/* assume blank lines got chopped */
9712dd076b8SGabor Kovesdan 					strlcpy(buf, " \n", buf_size);
9722dd076b8SGabor Kovesdan 				} else {
9732dd076b8SGabor Kovesdan 					fatal("unexpected end of file in patch\n");
9742dd076b8SGabor Kovesdan 				}
9752dd076b8SGabor Kovesdan 			}
9762dd076b8SGabor Kovesdan 			if (*buf == '\t' || *buf == '\n') {
9772dd076b8SGabor Kovesdan 				ch = ' ';	/* assume the space got eaten */
9782dd076b8SGabor Kovesdan 				s = savestr(buf);
9792dd076b8SGabor Kovesdan 			} else {
9802dd076b8SGabor Kovesdan 				ch = *buf;
9812dd076b8SGabor Kovesdan 				s = savestr(buf + 1);
9822dd076b8SGabor Kovesdan 			}
9832dd076b8SGabor Kovesdan 			if (out_of_mem) {
9842dd076b8SGabor Kovesdan 				while (--fillnew > p_ptrn_lines)
9852dd076b8SGabor Kovesdan 					free(p_line[fillnew]);
9862dd076b8SGabor Kovesdan 				p_end = fillold - 1;
9872dd076b8SGabor Kovesdan 				return false;
9882dd076b8SGabor Kovesdan 			}
9892dd076b8SGabor Kovesdan 			switch (ch) {
9902dd076b8SGabor Kovesdan 			case '-':
9912dd076b8SGabor Kovesdan 				if (fillold > p_ptrn_lines) {
9922dd076b8SGabor Kovesdan 					free(s);
9932dd076b8SGabor Kovesdan 					p_end = fillnew - 1;
9942dd076b8SGabor Kovesdan 					malformed();
9952dd076b8SGabor Kovesdan 				}
9962dd076b8SGabor Kovesdan 				p_char[fillold] = ch;
9972dd076b8SGabor Kovesdan 				p_line[fillold] = s;
9982dd076b8SGabor Kovesdan 				p_len[fillold++] = strlen(s);
9992dd076b8SGabor Kovesdan 				if (fillold > p_ptrn_lines) {
10002dd076b8SGabor Kovesdan 					if (remove_special_line()) {
10012dd076b8SGabor Kovesdan 						p_len[fillold - 1] -= 1;
10022dd076b8SGabor Kovesdan 						s[p_len[fillold - 1]] = 0;
10032dd076b8SGabor Kovesdan 					}
10042dd076b8SGabor Kovesdan 				}
10052dd076b8SGabor Kovesdan 				break;
10062dd076b8SGabor Kovesdan 			case '=':
10072dd076b8SGabor Kovesdan 				ch = ' ';
10082dd076b8SGabor Kovesdan 				/* FALL THROUGH */
10092dd076b8SGabor Kovesdan 			case ' ':
10102dd076b8SGabor Kovesdan 				if (fillold > p_ptrn_lines) {
10112dd076b8SGabor Kovesdan 					free(s);
10122dd076b8SGabor Kovesdan 					while (--fillnew > p_ptrn_lines)
10132dd076b8SGabor Kovesdan 						free(p_line[fillnew]);
10142dd076b8SGabor Kovesdan 					p_end = fillold - 1;
10152dd076b8SGabor Kovesdan 					malformed();
10162dd076b8SGabor Kovesdan 				}
10172dd076b8SGabor Kovesdan 				context++;
10182dd076b8SGabor Kovesdan 				p_char[fillold] = ch;
10192dd076b8SGabor Kovesdan 				p_line[fillold] = s;
10202dd076b8SGabor Kovesdan 				p_len[fillold++] = strlen(s);
10212dd076b8SGabor Kovesdan 				s = savestr(s);
10222dd076b8SGabor Kovesdan 				if (out_of_mem) {
10232dd076b8SGabor Kovesdan 					while (--fillnew > p_ptrn_lines)
10242dd076b8SGabor Kovesdan 						free(p_line[fillnew]);
10252dd076b8SGabor Kovesdan 					p_end = fillold - 1;
10262dd076b8SGabor Kovesdan 					return false;
10272dd076b8SGabor Kovesdan 				}
10282dd076b8SGabor Kovesdan 				if (fillold > p_ptrn_lines) {
10292dd076b8SGabor Kovesdan 					if (remove_special_line()) {
10302dd076b8SGabor Kovesdan 						p_len[fillold - 1] -= 1;
10312dd076b8SGabor Kovesdan 						s[p_len[fillold - 1]] = 0;
10322dd076b8SGabor Kovesdan 					}
10332dd076b8SGabor Kovesdan 				}
10342dd076b8SGabor Kovesdan 				/* FALL THROUGH */
10352dd076b8SGabor Kovesdan 			case '+':
10362dd076b8SGabor Kovesdan 				if (fillnew > p_end) {
10372dd076b8SGabor Kovesdan 					free(s);
10382dd076b8SGabor Kovesdan 					while (--fillnew > p_ptrn_lines)
10392dd076b8SGabor Kovesdan 						free(p_line[fillnew]);
10402dd076b8SGabor Kovesdan 					p_end = fillold - 1;
10412dd076b8SGabor Kovesdan 					malformed();
10422dd076b8SGabor Kovesdan 				}
10432dd076b8SGabor Kovesdan 				p_char[fillnew] = ch;
10442dd076b8SGabor Kovesdan 				p_line[fillnew] = s;
10452dd076b8SGabor Kovesdan 				p_len[fillnew++] = strlen(s);
10462dd076b8SGabor Kovesdan 				if (fillold > p_ptrn_lines) {
10472dd076b8SGabor Kovesdan 					if (remove_special_line()) {
10482dd076b8SGabor Kovesdan 						p_len[fillnew - 1] -= 1;
10492dd076b8SGabor Kovesdan 						s[p_len[fillnew - 1]] = 0;
10502dd076b8SGabor Kovesdan 					}
10512dd076b8SGabor Kovesdan 				}
10522dd076b8SGabor Kovesdan 				break;
10532dd076b8SGabor Kovesdan 			default:
10542dd076b8SGabor Kovesdan 				p_end = fillnew;
10552dd076b8SGabor Kovesdan 				malformed();
10562dd076b8SGabor Kovesdan 			}
10572dd076b8SGabor Kovesdan 			if (ch != ' ' && context > 0) {
10582dd076b8SGabor Kovesdan 				if (context < p_context)
10592dd076b8SGabor Kovesdan 					p_context = context;
10602dd076b8SGabor Kovesdan 				context = -1000;
10612dd076b8SGabor Kovesdan 			}
10622dd076b8SGabor Kovesdan 		}		/* while */
10632dd076b8SGabor Kovesdan 	} else {		/* normal diff--fake it up */
10642dd076b8SGabor Kovesdan 		char	hunk_type;
10652dd076b8SGabor Kovesdan 		int	i;
10662dd076b8SGabor Kovesdan 		LINENUM	min, max;
10672dd076b8SGabor Kovesdan 
1068e91a64deSPedro F. Giffuni 		line_beginning = ftello(pfp);
10692dd076b8SGabor Kovesdan 		p_context = 0;
10702dd076b8SGabor Kovesdan 		len = pgets(true);
10712dd076b8SGabor Kovesdan 		p_input_line++;
10722dd076b8SGabor Kovesdan 		if (len == 0 || !isdigit((unsigned char)*buf)) {
10732dd076b8SGabor Kovesdan 			next_intuit_at(line_beginning, p_input_line);
10742dd076b8SGabor Kovesdan 			return false;
10752dd076b8SGabor Kovesdan 		}
1076d3fc0cb8SPedro F. Giffuni 		p_first = strtolinenum(buf, &s);
10772dd076b8SGabor Kovesdan 		if (*s == ',') {
1078d3fc0cb8SPedro F. Giffuni 			p_ptrn_lines = strtolinenum(s + 1, &s) - p_first + 1;
1079d3fc0cb8SPedro F. Giffuni 			if (p_ptrn_lines < 0)
1080d3fc0cb8SPedro F. Giffuni 				malformed();
10812dd076b8SGabor Kovesdan 		} else
10822dd076b8SGabor Kovesdan 			p_ptrn_lines = (*s != 'a');
10832dd076b8SGabor Kovesdan 		hunk_type = *s;
10842dd076b8SGabor Kovesdan 		if (hunk_type == 'a')
10852dd076b8SGabor Kovesdan 			p_first++;	/* do append rather than insert */
1086d3fc0cb8SPedro F. Giffuni 		min = strtolinenum(s + 1, &s);
10872dd076b8SGabor Kovesdan 		if (*s == ',')
1088d3fc0cb8SPedro F. Giffuni 			max = strtolinenum(s + 1, &s);
10892dd076b8SGabor Kovesdan 		else
10902dd076b8SGabor Kovesdan 			max = min;
1091d3fc0cb8SPedro F. Giffuni 		if (min < 0 || min > max || max - min == LINENUM_MAX)
1092d3fc0cb8SPedro F. Giffuni 			malformed();
10932dd076b8SGabor Kovesdan 		if (hunk_type == 'd')
10942dd076b8SGabor Kovesdan 			min++;
1095d3fc0cb8SPedro F. Giffuni 		p_newfirst = min;
1096d3fc0cb8SPedro F. Giffuni 		p_repl_lines = max - min + 1;
1097d3fc0cb8SPedro F. Giffuni 		if (p_newfirst > LINENUM_MAX - p_repl_lines ||
1098d3fc0cb8SPedro F. Giffuni 		    p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1)
1099d3fc0cb8SPedro F. Giffuni 			malformed();
1100d3fc0cb8SPedro F. Giffuni 		p_end = p_ptrn_lines + p_repl_lines + 1;
11012dd076b8SGabor Kovesdan 		if (p_end > MAXHUNKSIZE)
11022dd076b8SGabor Kovesdan 			fatal("hunk too large (%ld lines) at line %ld: %s",
11032dd076b8SGabor Kovesdan 			    p_end, p_input_line, buf);
11042dd076b8SGabor Kovesdan 		while (p_end >= hunkmax)
11052dd076b8SGabor Kovesdan 			grow_hunkmax();
11062dd076b8SGabor Kovesdan 		snprintf(buf, buf_size, "*** %ld,%ld\n", p_first,
11072dd076b8SGabor Kovesdan 		    p_first + p_ptrn_lines - 1);
11082dd076b8SGabor Kovesdan 		p_line[0] = savestr(buf);
11092dd076b8SGabor Kovesdan 		if (out_of_mem) {
11102dd076b8SGabor Kovesdan 			p_end = -1;
11112dd076b8SGabor Kovesdan 			return false;
11122dd076b8SGabor Kovesdan 		}
11132dd076b8SGabor Kovesdan 		p_char[0] = '*';
11142dd076b8SGabor Kovesdan 		for (i = 1; i <= p_ptrn_lines; i++) {
11152dd076b8SGabor Kovesdan 			len = pgets(true);
11162dd076b8SGabor Kovesdan 			p_input_line++;
11172dd076b8SGabor Kovesdan 			if (len == 0)
11182dd076b8SGabor Kovesdan 				fatal("unexpected end of file in patch at line %ld\n",
11192dd076b8SGabor Kovesdan 				    p_input_line);
11202dd076b8SGabor Kovesdan 			if (*buf != '<')
11212dd076b8SGabor Kovesdan 				fatal("< expected at line %ld of patch\n",
11222dd076b8SGabor Kovesdan 				    p_input_line);
11232dd076b8SGabor Kovesdan 			p_line[i] = savestr(buf + 2);
11242dd076b8SGabor Kovesdan 			if (out_of_mem) {
11252dd076b8SGabor Kovesdan 				p_end = i - 1;
11262dd076b8SGabor Kovesdan 				return false;
11272dd076b8SGabor Kovesdan 			}
11282dd076b8SGabor Kovesdan 			p_len[i] = strlen(p_line[i]);
11292dd076b8SGabor Kovesdan 			p_char[i] = '-';
11302dd076b8SGabor Kovesdan 		}
11312dd076b8SGabor Kovesdan 
11322dd076b8SGabor Kovesdan 		if (remove_special_line()) {
11332dd076b8SGabor Kovesdan 			p_len[i - 1] -= 1;
11342dd076b8SGabor Kovesdan 			(p_line[i - 1])[p_len[i - 1]] = 0;
11352dd076b8SGabor Kovesdan 		}
11362dd076b8SGabor Kovesdan 		if (hunk_type == 'c') {
11372dd076b8SGabor Kovesdan 			len = pgets(true);
11382dd076b8SGabor Kovesdan 			p_input_line++;
11392dd076b8SGabor Kovesdan 			if (len == 0)
11402dd076b8SGabor Kovesdan 				fatal("unexpected end of file in patch at line %ld\n",
11412dd076b8SGabor Kovesdan 				    p_input_line);
11422dd076b8SGabor Kovesdan 			if (*buf != '-')
11432dd076b8SGabor Kovesdan 				fatal("--- expected at line %ld of patch\n",
11442dd076b8SGabor Kovesdan 				    p_input_line);
11452dd076b8SGabor Kovesdan 		}
11462dd076b8SGabor Kovesdan 		snprintf(buf, buf_size, "--- %ld,%ld\n", min, max);
11472dd076b8SGabor Kovesdan 		p_line[i] = savestr(buf);
11482dd076b8SGabor Kovesdan 		if (out_of_mem) {
11492dd076b8SGabor Kovesdan 			p_end = i - 1;
11502dd076b8SGabor Kovesdan 			return false;
11512dd076b8SGabor Kovesdan 		}
11522dd076b8SGabor Kovesdan 		p_char[i] = '=';
11532dd076b8SGabor Kovesdan 		for (i++; i <= p_end; i++) {
11542dd076b8SGabor Kovesdan 			len = pgets(true);
11552dd076b8SGabor Kovesdan 			p_input_line++;
11562dd076b8SGabor Kovesdan 			if (len == 0)
11572dd076b8SGabor Kovesdan 				fatal("unexpected end of file in patch at line %ld\n",
11582dd076b8SGabor Kovesdan 				    p_input_line);
11592dd076b8SGabor Kovesdan 			if (*buf != '>')
11602dd076b8SGabor Kovesdan 				fatal("> expected at line %ld of patch\n",
11612dd076b8SGabor Kovesdan 				    p_input_line);
11624835edfaSKyle Evans 			/* Don't overrun if we don't have enough line */
11634835edfaSKyle Evans 			if (len > 2)
11642dd076b8SGabor Kovesdan 				p_line[i] = savestr(buf + 2);
11654835edfaSKyle Evans 			else
11664835edfaSKyle Evans 				p_line[i] = savestr("");
11674835edfaSKyle Evans 
11682dd076b8SGabor Kovesdan 			if (out_of_mem) {
11692dd076b8SGabor Kovesdan 				p_end = i - 1;
11702dd076b8SGabor Kovesdan 				return false;
11712dd076b8SGabor Kovesdan 			}
11722dd076b8SGabor Kovesdan 			p_len[i] = strlen(p_line[i]);
11732dd076b8SGabor Kovesdan 			p_char[i] = '+';
11742dd076b8SGabor Kovesdan 		}
11752dd076b8SGabor Kovesdan 
11762dd076b8SGabor Kovesdan 		if (remove_special_line()) {
11772dd076b8SGabor Kovesdan 			p_len[i - 1] -= 1;
11782dd076b8SGabor Kovesdan 			(p_line[i - 1])[p_len[i - 1]] = 0;
11792dd076b8SGabor Kovesdan 		}
11802dd076b8SGabor Kovesdan 	}
11812dd076b8SGabor Kovesdan 	if (reverse)		/* backwards patch? */
11822dd076b8SGabor Kovesdan 		if (!pch_swap())
11832dd076b8SGabor Kovesdan 			say("Not enough memory to swap next hunk!\n");
11842dd076b8SGabor Kovesdan #ifdef DEBUGGING
11852dd076b8SGabor Kovesdan 	if (debug & 2) {
11862c4eed47SPedro F. Giffuni 		LINENUM	i;
11872dd076b8SGabor Kovesdan 		char	special;
11882dd076b8SGabor Kovesdan 
11892dd076b8SGabor Kovesdan 		for (i = 0; i <= p_end; i++) {
11902dd076b8SGabor Kovesdan 			if (i == p_ptrn_lines)
11912dd076b8SGabor Kovesdan 				special = '^';
11922dd076b8SGabor Kovesdan 			else
11932dd076b8SGabor Kovesdan 				special = ' ';
11942c4eed47SPedro F. Giffuni 			fprintf(stderr, "%3ld %c %c %s", i, p_char[i],
11952dd076b8SGabor Kovesdan 			    special, p_line[i]);
11962dd076b8SGabor Kovesdan 			fflush(stderr);
11972dd076b8SGabor Kovesdan 		}
11982dd076b8SGabor Kovesdan 	}
11992dd076b8SGabor Kovesdan #endif
12002dd076b8SGabor Kovesdan 	if (p_end + 1 < hunkmax)/* paranoia reigns supreme... */
12012dd076b8SGabor Kovesdan 		p_char[p_end + 1] = '^';	/* add a stopper for apply_hunk */
12022dd076b8SGabor Kovesdan 	return true;
12032dd076b8SGabor Kovesdan }
12042dd076b8SGabor Kovesdan 
12052dd076b8SGabor Kovesdan /*
12062dd076b8SGabor Kovesdan  * Input a line from the patch file.
12072dd076b8SGabor Kovesdan  * Worry about indentation if do_indent is true.
12082dd076b8SGabor Kovesdan  * The line is read directly into the buf global variable which
12092dd076b8SGabor Kovesdan  * is resized if necessary in order to hold the complete line.
12102dd076b8SGabor Kovesdan  * Returns the number of characters read including the terminating
12112dd076b8SGabor Kovesdan  * '\n', if any.
12122dd076b8SGabor Kovesdan  */
12132dd076b8SGabor Kovesdan size_t
12142dd076b8SGabor Kovesdan pgets(bool do_indent)
12152dd076b8SGabor Kovesdan {
1216*25696725SMartin Tournoij 	char *line = NULL;
1217*25696725SMartin Tournoij 	ssize_t len = 0;
1218*25696725SMartin Tournoij 	size_t buflen = 0;
12192dd076b8SGabor Kovesdan 	int indent = 0, skipped = 0;
12202dd076b8SGabor Kovesdan 
1221*25696725SMartin Tournoij 	if ((len = getline(&line, &buflen, pfp)) >= 0) {
1222*25696725SMartin Tournoij 		char *linep = line;
1223*25696725SMartin Tournoij 		if ((size_t)(len + 1) > buf_size) {
1224*25696725SMartin Tournoij 			while ((size_t)(len + 1) > buf_size)
12252dd076b8SGabor Kovesdan 				buf_size *= 2;
12262dd076b8SGabor Kovesdan 			free(buf);
12272dd076b8SGabor Kovesdan 			buf = malloc(buf_size);
12282dd076b8SGabor Kovesdan 			if (buf == NULL)
12292dd076b8SGabor Kovesdan 				fatal("out of memory\n");
12302dd076b8SGabor Kovesdan 		}
12312dd076b8SGabor Kovesdan 		if (do_indent == 1 && p_indent) {
12322dd076b8SGabor Kovesdan 			for (;
12332dd076b8SGabor Kovesdan 			    indent < p_indent && (*line == ' ' || *line == '\t' || *line == 'X');
12342dd076b8SGabor Kovesdan 			    line++, skipped++) {
12352dd076b8SGabor Kovesdan 				if (*line == '\t')
12362dd076b8SGabor Kovesdan 					indent += 8 - (indent %7);
12372dd076b8SGabor Kovesdan 				else
12382dd076b8SGabor Kovesdan 					indent++;
12392dd076b8SGabor Kovesdan 			}
12402dd076b8SGabor Kovesdan 		}
12416d85e39bSDavid E. O'Brien 		memcpy(buf, line, len - skipped);
12422dd076b8SGabor Kovesdan 		buf[len - skipped] = '\0';
1243*25696725SMartin Tournoij 		line = linep;
12442dd076b8SGabor Kovesdan 	}
1245*25696725SMartin Tournoij 	free(line);
1246*25696725SMartin Tournoij 	return (len > 0) ? len : 0;
12472dd076b8SGabor Kovesdan }
12482dd076b8SGabor Kovesdan 
12492dd076b8SGabor Kovesdan 
12502dd076b8SGabor Kovesdan /*
12512dd076b8SGabor Kovesdan  * Reverse the old and new portions of the current hunk.
12522dd076b8SGabor Kovesdan  */
12532dd076b8SGabor Kovesdan bool
12542dd076b8SGabor Kovesdan pch_swap(void)
12552dd076b8SGabor Kovesdan {
12562dd076b8SGabor Kovesdan 	char	**tp_line;	/* the text of the hunk */
1257851a9da3SDag-Erling Smørgrav 	size_t	*tp_len;	/* length of each line */
12582dd076b8SGabor Kovesdan 	char	*tp_char;	/* +, -, and ! */
12592dd076b8SGabor Kovesdan 	LINENUM	i;
12602dd076b8SGabor Kovesdan 	LINENUM	n;
12612dd076b8SGabor Kovesdan 	bool	blankline = false;
12622dd076b8SGabor Kovesdan 	char	*s;
12632dd076b8SGabor Kovesdan 
12642dd076b8SGabor Kovesdan 	i = p_first;
12652dd076b8SGabor Kovesdan 	p_first = p_newfirst;
12662dd076b8SGabor Kovesdan 	p_newfirst = i;
12672dd076b8SGabor Kovesdan 
12682dd076b8SGabor Kovesdan 	/* make a scratch copy */
12692dd076b8SGabor Kovesdan 
12702dd076b8SGabor Kovesdan 	tp_line = p_line;
12712dd076b8SGabor Kovesdan 	tp_len = p_len;
12722dd076b8SGabor Kovesdan 	tp_char = p_char;
12732dd076b8SGabor Kovesdan 	p_line = NULL;	/* force set_hunkmax to allocate again */
12742dd076b8SGabor Kovesdan 	p_len = NULL;
12752dd076b8SGabor Kovesdan 	p_char = NULL;
12762dd076b8SGabor Kovesdan 	set_hunkmax();
12772dd076b8SGabor Kovesdan 	if (p_line == NULL || p_len == NULL || p_char == NULL) {
12782dd076b8SGabor Kovesdan 
12792dd076b8SGabor Kovesdan 		free(p_line);
12802dd076b8SGabor Kovesdan 		p_line = tp_line;
12812dd076b8SGabor Kovesdan 		free(p_len);
12822dd076b8SGabor Kovesdan 		p_len = tp_len;
12832dd076b8SGabor Kovesdan 		free(p_char);
12842dd076b8SGabor Kovesdan 		p_char = tp_char;
12852dd076b8SGabor Kovesdan 		return false;	/* not enough memory to swap hunk! */
12862dd076b8SGabor Kovesdan 	}
12872dd076b8SGabor Kovesdan 	/* now turn the new into the old */
12882dd076b8SGabor Kovesdan 
12892dd076b8SGabor Kovesdan 	i = p_ptrn_lines + 1;
12902dd076b8SGabor Kovesdan 	if (tp_char[i] == '\n') {	/* account for possible blank line */
12912dd076b8SGabor Kovesdan 		blankline = true;
12922dd076b8SGabor Kovesdan 		i++;
12932dd076b8SGabor Kovesdan 	}
12942dd076b8SGabor Kovesdan 	if (p_efake >= 0) {	/* fix non-freeable ptr range */
12952dd076b8SGabor Kovesdan 		if (p_efake <= i)
12962dd076b8SGabor Kovesdan 			n = p_end - i + 1;
12972dd076b8SGabor Kovesdan 		else
12982dd076b8SGabor Kovesdan 			n = -i;
12992dd076b8SGabor Kovesdan 		p_efake += n;
13002dd076b8SGabor Kovesdan 		p_bfake += n;
13012dd076b8SGabor Kovesdan 	}
13022dd076b8SGabor Kovesdan 	for (n = 0; i <= p_end; i++, n++) {
13032dd076b8SGabor Kovesdan 		p_line[n] = tp_line[i];
13042dd076b8SGabor Kovesdan 		p_char[n] = tp_char[i];
13052dd076b8SGabor Kovesdan 		if (p_char[n] == '+')
13062dd076b8SGabor Kovesdan 			p_char[n] = '-';
13072dd076b8SGabor Kovesdan 		p_len[n] = tp_len[i];
13082dd076b8SGabor Kovesdan 	}
13092dd076b8SGabor Kovesdan 	if (blankline) {
13102dd076b8SGabor Kovesdan 		i = p_ptrn_lines + 1;
13112dd076b8SGabor Kovesdan 		p_line[n] = tp_line[i];
13122dd076b8SGabor Kovesdan 		p_char[n] = tp_char[i];
13132dd076b8SGabor Kovesdan 		p_len[n] = tp_len[i];
13142dd076b8SGabor Kovesdan 		n++;
13152dd076b8SGabor Kovesdan 	}
13162dd076b8SGabor Kovesdan 	if (p_char[0] != '=')
13172dd076b8SGabor Kovesdan 		fatal("Malformed patch at line %ld: expected '=' found '%c'\n",
13182dd076b8SGabor Kovesdan 		    p_input_line, p_char[0]);
13192dd076b8SGabor Kovesdan 	p_char[0] = '*';
13202dd076b8SGabor Kovesdan 	for (s = p_line[0]; *s; s++)
13212dd076b8SGabor Kovesdan 		if (*s == '-')
13222dd076b8SGabor Kovesdan 			*s = '*';
13232dd076b8SGabor Kovesdan 
13242dd076b8SGabor Kovesdan 	/* now turn the old into the new */
13252dd076b8SGabor Kovesdan 
13262dd076b8SGabor Kovesdan 	if (p_char[0] != '*')
13272dd076b8SGabor Kovesdan 		fatal("Malformed patch at line %ld: expected '*' found '%c'\n",
13282dd076b8SGabor Kovesdan 		    p_input_line, p_char[0]);
13292dd076b8SGabor Kovesdan 	tp_char[0] = '=';
13302dd076b8SGabor Kovesdan 	for (s = tp_line[0]; *s; s++)
13312dd076b8SGabor Kovesdan 		if (*s == '*')
13322dd076b8SGabor Kovesdan 			*s = '-';
13332dd076b8SGabor Kovesdan 	for (i = 0; n <= p_end; i++, n++) {
13342dd076b8SGabor Kovesdan 		p_line[n] = tp_line[i];
13352dd076b8SGabor Kovesdan 		p_char[n] = tp_char[i];
13362dd076b8SGabor Kovesdan 		if (p_char[n] == '-')
13372dd076b8SGabor Kovesdan 			p_char[n] = '+';
13382dd076b8SGabor Kovesdan 		p_len[n] = tp_len[i];
13392dd076b8SGabor Kovesdan 	}
13402dd076b8SGabor Kovesdan 
13412dd076b8SGabor Kovesdan 	if (i != p_ptrn_lines + 1)
13422dd076b8SGabor Kovesdan 		fatal("Malformed patch at line %ld: expected %ld lines, "
13432dd076b8SGabor Kovesdan 		    "got %ld\n",
13442dd076b8SGabor Kovesdan 		    p_input_line, p_ptrn_lines + 1, i);
13452dd076b8SGabor Kovesdan 
13462dd076b8SGabor Kovesdan 	i = p_ptrn_lines;
13472dd076b8SGabor Kovesdan 	p_ptrn_lines = p_repl_lines;
13482dd076b8SGabor Kovesdan 	p_repl_lines = i;
13492dd076b8SGabor Kovesdan 
13502dd076b8SGabor Kovesdan 	free(tp_line);
13512dd076b8SGabor Kovesdan 	free(tp_len);
13522dd076b8SGabor Kovesdan 	free(tp_char);
13532dd076b8SGabor Kovesdan 
13542dd076b8SGabor Kovesdan 	return true;
13552dd076b8SGabor Kovesdan }
13562dd076b8SGabor Kovesdan 
13572dd076b8SGabor Kovesdan /*
13582dd076b8SGabor Kovesdan  * Return the specified line position in the old file of the old context.
13592dd076b8SGabor Kovesdan  */
13602dd076b8SGabor Kovesdan LINENUM
13612dd076b8SGabor Kovesdan pch_first(void)
13622dd076b8SGabor Kovesdan {
13632dd076b8SGabor Kovesdan 	return p_first;
13642dd076b8SGabor Kovesdan }
13652dd076b8SGabor Kovesdan 
13662dd076b8SGabor Kovesdan /*
13672dd076b8SGabor Kovesdan  * Return the number of lines of old context.
13682dd076b8SGabor Kovesdan  */
13692dd076b8SGabor Kovesdan LINENUM
13702dd076b8SGabor Kovesdan pch_ptrn_lines(void)
13712dd076b8SGabor Kovesdan {
13722dd076b8SGabor Kovesdan 	return p_ptrn_lines;
13732dd076b8SGabor Kovesdan }
13742dd076b8SGabor Kovesdan 
13752dd076b8SGabor Kovesdan /*
13762dd076b8SGabor Kovesdan  * Return the probable line position in the new file of the first line.
13772dd076b8SGabor Kovesdan  */
13782dd076b8SGabor Kovesdan LINENUM
13792dd076b8SGabor Kovesdan pch_newfirst(void)
13802dd076b8SGabor Kovesdan {
13812dd076b8SGabor Kovesdan 	return p_newfirst;
13822dd076b8SGabor Kovesdan }
13832dd076b8SGabor Kovesdan 
13842dd076b8SGabor Kovesdan /*
13852dd076b8SGabor Kovesdan  * Return the number of lines in the replacement text including context.
13862dd076b8SGabor Kovesdan  */
13872dd076b8SGabor Kovesdan LINENUM
13882dd076b8SGabor Kovesdan pch_repl_lines(void)
13892dd076b8SGabor Kovesdan {
13902dd076b8SGabor Kovesdan 	return p_repl_lines;
13912dd076b8SGabor Kovesdan }
13922dd076b8SGabor Kovesdan 
13932dd076b8SGabor Kovesdan /*
13942dd076b8SGabor Kovesdan  * Return the number of lines in the whole hunk.
13952dd076b8SGabor Kovesdan  */
13962dd076b8SGabor Kovesdan LINENUM
13972dd076b8SGabor Kovesdan pch_end(void)
13982dd076b8SGabor Kovesdan {
13992dd076b8SGabor Kovesdan 	return p_end;
14002dd076b8SGabor Kovesdan }
14012dd076b8SGabor Kovesdan 
14022dd076b8SGabor Kovesdan /*
14032dd076b8SGabor Kovesdan  * Return the number of context lines before the first changed line.
14042dd076b8SGabor Kovesdan  */
14052dd076b8SGabor Kovesdan LINENUM
14062dd076b8SGabor Kovesdan pch_context(void)
14072dd076b8SGabor Kovesdan {
14082dd076b8SGabor Kovesdan 	return p_context;
14092dd076b8SGabor Kovesdan }
14102dd076b8SGabor Kovesdan 
14112dd076b8SGabor Kovesdan /*
14122dd076b8SGabor Kovesdan  * Return the length of a particular patch line.
14132dd076b8SGabor Kovesdan  */
1414851a9da3SDag-Erling Smørgrav size_t
14152dd076b8SGabor Kovesdan pch_line_len(LINENUM line)
14162dd076b8SGabor Kovesdan {
14172dd076b8SGabor Kovesdan 	return p_len[line];
14182dd076b8SGabor Kovesdan }
14192dd076b8SGabor Kovesdan 
14202dd076b8SGabor Kovesdan /*
14212dd076b8SGabor Kovesdan  * Return the control character (+, -, *, !, etc) for a patch line.
14222dd076b8SGabor Kovesdan  */
14232dd076b8SGabor Kovesdan char
14242dd076b8SGabor Kovesdan pch_char(LINENUM line)
14252dd076b8SGabor Kovesdan {
14262dd076b8SGabor Kovesdan 	return p_char[line];
14272dd076b8SGabor Kovesdan }
14282dd076b8SGabor Kovesdan 
14292dd076b8SGabor Kovesdan /*
14302dd076b8SGabor Kovesdan  * Return a pointer to a particular patch line.
14312dd076b8SGabor Kovesdan  */
14322dd076b8SGabor Kovesdan char *
14332dd076b8SGabor Kovesdan pfetch(LINENUM line)
14342dd076b8SGabor Kovesdan {
14352dd076b8SGabor Kovesdan 	return p_line[line];
14362dd076b8SGabor Kovesdan }
14372dd076b8SGabor Kovesdan 
14382dd076b8SGabor Kovesdan /*
14392dd076b8SGabor Kovesdan  * Return where in the patch file this hunk began, for error messages.
14402dd076b8SGabor Kovesdan  */
14412dd076b8SGabor Kovesdan LINENUM
14422dd076b8SGabor Kovesdan pch_hunk_beg(void)
14432dd076b8SGabor Kovesdan {
14442dd076b8SGabor Kovesdan 	return p_hunk_beg;
14452dd076b8SGabor Kovesdan }
14462dd076b8SGabor Kovesdan 
14472dd076b8SGabor Kovesdan /*
14482dd076b8SGabor Kovesdan  * Apply an ed script by feeding ed itself.
14492dd076b8SGabor Kovesdan  */
14502dd076b8SGabor Kovesdan void
14512dd076b8SGabor Kovesdan do_ed_script(void)
14522dd076b8SGabor Kovesdan {
14532dd076b8SGabor Kovesdan 	char	*t;
1454e91a64deSPedro F. Giffuni 	off_t	beginning_of_this_line;
14552dd076b8SGabor Kovesdan 	FILE	*pipefp = NULL;
14563fd78bfaSXin LI 	int	continuation;
14572dd076b8SGabor Kovesdan 
14582dd076b8SGabor Kovesdan 	if (!skip_rest_of_patch) {
14592dd076b8SGabor Kovesdan 		if (copy_file(filearg[0], TMPOUTNAME) < 0) {
14602dd076b8SGabor Kovesdan 			unlink(TMPOUTNAME);
14612dd076b8SGabor Kovesdan 			fatal("can't create temp file %s", TMPOUTNAME);
14622dd076b8SGabor Kovesdan 		}
14633fd78bfaSXin LI 		snprintf(buf, buf_size, "%s%s%s", _PATH_RED,
14642dd076b8SGabor Kovesdan 		    verbose ? " " : " -s ", TMPOUTNAME);
14652dd076b8SGabor Kovesdan 		pipefp = popen(buf, "w");
14662dd076b8SGabor Kovesdan 	}
14672dd076b8SGabor Kovesdan 	for (;;) {
1468e91a64deSPedro F. Giffuni 		beginning_of_this_line = ftello(pfp);
14692dd076b8SGabor Kovesdan 		if (pgets(true) == 0) {
14702dd076b8SGabor Kovesdan 			next_intuit_at(beginning_of_this_line, p_input_line);
14712dd076b8SGabor Kovesdan 			break;
14722dd076b8SGabor Kovesdan 		}
14732dd076b8SGabor Kovesdan 		p_input_line++;
14742dd076b8SGabor Kovesdan 		for (t = buf; isdigit((unsigned char)*t) || *t == ','; t++)
14752dd076b8SGabor Kovesdan 			;
14762dd076b8SGabor Kovesdan 		/* POSIX defines allowed commands as {a,c,d,i,s} */
14772b468ebaSPedro F. Giffuni 		if (isdigit((unsigned char)*buf) &&
14782b468ebaSPedro F. Giffuni 		    (*t == 'a' || *t == 'c' || *t == 'd' || *t == 'i' || *t == 's')) {
14792dd076b8SGabor Kovesdan 			if (pipefp != NULL)
14802dd076b8SGabor Kovesdan 				fputs(buf, pipefp);
14813fd78bfaSXin LI 			if (*t == 's') {
14823fd78bfaSXin LI 				for (;;) {
14833fd78bfaSXin LI 					continuation = 0;
14843fd78bfaSXin LI 					t = strchr(buf, '\0') - 1;
14853fd78bfaSXin LI 					while (--t >= buf && *t == '\\')
14863fd78bfaSXin LI 						continuation = !continuation;
14873fd78bfaSXin LI 					if (!continuation ||
14883fd78bfaSXin LI 					    pgets(true) == 0)
14893fd78bfaSXin LI 						break;
14903fd78bfaSXin LI 					if (pipefp != NULL)
14913fd78bfaSXin LI 						fputs(buf, pipefp);
14923fd78bfaSXin LI 				}
14933fd78bfaSXin LI 			} else if (*t != 'd') {
14942dd076b8SGabor Kovesdan 				while (pgets(true)) {
14952dd076b8SGabor Kovesdan 					p_input_line++;
14962dd076b8SGabor Kovesdan 					if (pipefp != NULL)
14972dd076b8SGabor Kovesdan 						fputs(buf, pipefp);
14982dd076b8SGabor Kovesdan 					if (strEQ(buf, ".\n"))
14992dd076b8SGabor Kovesdan 						break;
15002dd076b8SGabor Kovesdan 				}
15012dd076b8SGabor Kovesdan 			}
15022dd076b8SGabor Kovesdan 		} else {
15032dd076b8SGabor Kovesdan 			next_intuit_at(beginning_of_this_line, p_input_line);
15042dd076b8SGabor Kovesdan 			break;
15052dd076b8SGabor Kovesdan 		}
15062dd076b8SGabor Kovesdan 	}
15072dd076b8SGabor Kovesdan 	if (pipefp == NULL)
15082dd076b8SGabor Kovesdan 		return;
15092dd076b8SGabor Kovesdan 	fprintf(pipefp, "w\n");
15102dd076b8SGabor Kovesdan 	fprintf(pipefp, "q\n");
15112dd076b8SGabor Kovesdan 	fflush(pipefp);
15122dd076b8SGabor Kovesdan 	pclose(pipefp);
15132dd076b8SGabor Kovesdan 	ignore_signals();
15142dd076b8SGabor Kovesdan 	if (!check_only) {
15152dd076b8SGabor Kovesdan 		if (move_file(TMPOUTNAME, outname) < 0) {
15162dd076b8SGabor Kovesdan 			toutkeep = true;
15172dd076b8SGabor Kovesdan 			chmod(TMPOUTNAME, filemode);
15182dd076b8SGabor Kovesdan 		} else
15192dd076b8SGabor Kovesdan 			chmod(outname, filemode);
15202dd076b8SGabor Kovesdan 	}
15212dd076b8SGabor Kovesdan 	set_signals(1);
15222dd076b8SGabor Kovesdan }
15232dd076b8SGabor Kovesdan 
15242dd076b8SGabor Kovesdan /*
15252dd076b8SGabor Kovesdan  * Choose the name of the file to be patched based on POSIX rules.
15262dd076b8SGabor Kovesdan  * NOTE: the POSIX rules are amazingly stupid and we only follow them
15272dd076b8SGabor Kovesdan  *       if the user specified --posix or set POSIXLY_CORRECT.
15282dd076b8SGabor Kovesdan  */
15292dd076b8SGabor Kovesdan static char *
15302dd076b8SGabor Kovesdan posix_name(const struct file_name *names, bool assume_exists)
15312dd076b8SGabor Kovesdan {
15322dd076b8SGabor Kovesdan 	char *path = NULL;
15332dd076b8SGabor Kovesdan 	int i;
15342dd076b8SGabor Kovesdan 
15352dd076b8SGabor Kovesdan 	/*
15362dd076b8SGabor Kovesdan 	 * POSIX states that the filename will be chosen from one
15372dd076b8SGabor Kovesdan 	 * of the old, new and index names (in that order) if
15382dd076b8SGabor Kovesdan 	 * the file exists relative to CWD after -p stripping.
15392dd076b8SGabor Kovesdan 	 */
15402dd076b8SGabor Kovesdan 	for (i = 0; i < MAX_FILE; i++) {
15412dd076b8SGabor Kovesdan 		if (names[i].path != NULL && names[i].exists) {
15422dd076b8SGabor Kovesdan 			path = names[i].path;
15432dd076b8SGabor Kovesdan 			break;
15442dd076b8SGabor Kovesdan 		}
15452dd076b8SGabor Kovesdan 	}
15462dd076b8SGabor Kovesdan 	if (path == NULL && !assume_exists) {
15472dd076b8SGabor Kovesdan 		/*
1548e678759cSXin LI 		 * No files found, check to see if the diff could be
1549e678759cSXin LI 		 * creating a new file.
15502dd076b8SGabor Kovesdan 		 */
15512dd076b8SGabor Kovesdan 		if (path == NULL && ok_to_create_file &&
15522dd076b8SGabor Kovesdan 		    names[NEW_FILE].path != NULL)
15532dd076b8SGabor Kovesdan 			path = names[NEW_FILE].path;
15542dd076b8SGabor Kovesdan 	}
15552dd076b8SGabor Kovesdan 
1556547e0acbSPedro F. Giffuni 	return path ? xstrdup(path) : NULL;
15572dd076b8SGabor Kovesdan }
15582dd076b8SGabor Kovesdan 
15592dd076b8SGabor Kovesdan static char *
1560e678759cSXin LI compare_names(const struct file_name *names, bool assume_exists)
15612dd076b8SGabor Kovesdan {
15622dd076b8SGabor Kovesdan 	size_t min_components, min_baselen, min_len, tmp;
15632dd076b8SGabor Kovesdan 	char *best = NULL;
156479d8aaa9SStefan Eßer 	char *path;
15652dd076b8SGabor Kovesdan 	int i;
15662dd076b8SGabor Kovesdan 
15672dd076b8SGabor Kovesdan 	/*
15682dd076b8SGabor Kovesdan 	 * The "best" name is the one with the fewest number of path
15692dd076b8SGabor Kovesdan 	 * components, the shortest basename length, and the shortest
15702dd076b8SGabor Kovesdan 	 * overall length (in that order).  We only use the Index: file
15712dd076b8SGabor Kovesdan 	 * if neither of the old or new files could be intuited from
15722dd076b8SGabor Kovesdan 	 * the diff header.
15732dd076b8SGabor Kovesdan 	 */
15742dd076b8SGabor Kovesdan 	min_components = min_baselen = min_len = SIZE_MAX;
15752dd076b8SGabor Kovesdan 	for (i = INDEX_FILE; i >= OLD_FILE; i--) {
157679d8aaa9SStefan Eßer 		path = names[i].path;
1577e678759cSXin LI 		if (path == NULL || (!names[i].exists && !assume_exists))
15782dd076b8SGabor Kovesdan 			continue;
157979d8aaa9SStefan Eßer 		if ((tmp = num_components(path)) > min_components)
15802dd076b8SGabor Kovesdan 			continue;
15816b239879SStefan Eßer 		if (tmp < min_components) {
15822dd076b8SGabor Kovesdan 			min_components = tmp;
158379d8aaa9SStefan Eßer 			best = path;
15846b239879SStefan Eßer 		}
158579d8aaa9SStefan Eßer 		if ((tmp = strlen(basename(path))) > min_baselen)
15862dd076b8SGabor Kovesdan 			continue;
15876b239879SStefan Eßer 		if (tmp < min_baselen) {
15882dd076b8SGabor Kovesdan 			min_baselen = tmp;
158979d8aaa9SStefan Eßer 			best = path;
15906b239879SStefan Eßer 		}
159179d8aaa9SStefan Eßer 		if ((tmp = strlen(path)) > min_len)
15922dd076b8SGabor Kovesdan 			continue;
15932dd076b8SGabor Kovesdan 		min_len = tmp;
159479d8aaa9SStefan Eßer 		best = path;
15952dd076b8SGabor Kovesdan 	}
159679d8aaa9SStefan Eßer 	return best;
159779d8aaa9SStefan Eßer }
159879d8aaa9SStefan Eßer 
15992dd076b8SGabor Kovesdan /*
160079d8aaa9SStefan Eßer  * Choose the name of the file to be patched based the "best" one
160179d8aaa9SStefan Eßer  * available.
16022dd076b8SGabor Kovesdan  */
160379d8aaa9SStefan Eßer static char *
160479d8aaa9SStefan Eßer best_name(const struct file_name *names, bool assume_exists)
160579d8aaa9SStefan Eßer {
160679d8aaa9SStefan Eßer 	char *best;
160779d8aaa9SStefan Eßer 
1608e678759cSXin LI 	best = compare_names(names, assume_exists);
1609e678759cSXin LI 
1610e678759cSXin LI 	/* No match?  Check to see if the diff could be creating a new file. */
1611e678759cSXin LI 	if (best == NULL && ok_to_create_file)
16122dd076b8SGabor Kovesdan 		best = names[NEW_FILE].path;
16132dd076b8SGabor Kovesdan 
1614547e0acbSPedro F. Giffuni 	return best ? xstrdup(best) : NULL;
16152dd076b8SGabor Kovesdan }
16162dd076b8SGabor Kovesdan 
16172dd076b8SGabor Kovesdan static size_t
16182dd076b8SGabor Kovesdan num_components(const char *path)
16192dd076b8SGabor Kovesdan {
16202dd076b8SGabor Kovesdan 	size_t n;
16212dd076b8SGabor Kovesdan 	const char *cp;
16222dd076b8SGabor Kovesdan 
16239610cbc0SPedro F. Giffuni 	for (n = 0, cp = path; (cp = strchr(cp, '/')) != NULL; n++) {
16249610cbc0SPedro F. Giffuni 		cp++;
16252dd076b8SGabor Kovesdan 		while (*cp == '/')
16262dd076b8SGabor Kovesdan 			cp++;		/* skip consecutive slashes */
16272dd076b8SGabor Kovesdan 	}
16282dd076b8SGabor Kovesdan 	return n;
16292dd076b8SGabor Kovesdan }
1630d3fc0cb8SPedro F. Giffuni 
1631d3fc0cb8SPedro F. Giffuni /*
1632d3fc0cb8SPedro F. Giffuni  * Convert number at NPTR into LINENUM and save address of first
1633d3fc0cb8SPedro F. Giffuni  * character that is not a digit in ENDPTR.  If conversion is not
1634d3fc0cb8SPedro F. Giffuni  * possible, call fatal.
1635d3fc0cb8SPedro F. Giffuni  */
1636d3fc0cb8SPedro F. Giffuni static LINENUM
1637d3fc0cb8SPedro F. Giffuni strtolinenum(char *nptr, char **endptr)
1638d3fc0cb8SPedro F. Giffuni {
1639d3fc0cb8SPedro F. Giffuni 	LINENUM rv;
1640d3fc0cb8SPedro F. Giffuni 	char c;
1641d3fc0cb8SPedro F. Giffuni 	char *p;
1642d3fc0cb8SPedro F. Giffuni 	const char *errstr;
1643d3fc0cb8SPedro F. Giffuni 
1644d3fc0cb8SPedro F. Giffuni 	for (p = nptr; isdigit((unsigned char)*p); p++)
1645d3fc0cb8SPedro F. Giffuni 		;
1646d3fc0cb8SPedro F. Giffuni 
1647d3fc0cb8SPedro F. Giffuni 	if (p == nptr)
1648d3fc0cb8SPedro F. Giffuni 		malformed();
1649d3fc0cb8SPedro F. Giffuni 
1650d3fc0cb8SPedro F. Giffuni 	c = *p;
1651d3fc0cb8SPedro F. Giffuni 	*p = '\0';
1652d3fc0cb8SPedro F. Giffuni 
1653d3fc0cb8SPedro F. Giffuni 	rv = strtonum(nptr, 0, LINENUM_MAX, &errstr);
1654d3fc0cb8SPedro F. Giffuni 	if (errstr != NULL)
1655d3fc0cb8SPedro F. Giffuni 		fatal("invalid line number at line %ld: `%s' is %s\n",
1656d3fc0cb8SPedro F. Giffuni 		    p_input_line, nptr, errstr);
1657d3fc0cb8SPedro F. Giffuni 
1658d3fc0cb8SPedro F. Giffuni 	*p = c;
1659d3fc0cb8SPedro F. Giffuni 	*endptr = p;
1660d3fc0cb8SPedro F. Giffuni 
1661d3fc0cb8SPedro F. Giffuni 	return rv;
1662d3fc0cb8SPedro F. Giffuni }
1663