xref: /minix3/usr.bin/patch/patch.c (revision 757e83288ac54fa2d663d17e4a302156fdb3650a)
1*757e8328SLionel Sambuc /*
2*757e8328SLionel Sambuc  * $OpenBSD: patch.c,v 1.45 2007/04/18 21:52:24 sobrado Exp $
3*757e8328SLionel Sambuc  * $DragonFly: src/usr.bin/patch/patch.c,v 1.10 2008/08/10 23:39:56 joerg Exp $
4*757e8328SLionel Sambuc  * $NetBSD: patch.c,v 1.29 2011/09/06 18:25:14 joerg Exp $
5*757e8328SLionel Sambuc  */
6*757e8328SLionel Sambuc 
7*757e8328SLionel Sambuc /*
8*757e8328SLionel Sambuc  * patch - a program to apply diffs to original files
9*757e8328SLionel Sambuc  *
10*757e8328SLionel Sambuc  * Copyright 1986, Larry Wall
11*757e8328SLionel Sambuc  *
12*757e8328SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
13*757e8328SLionel Sambuc  * modification, are permitted provided that the following condition is met:
14*757e8328SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright notice,
15*757e8328SLionel Sambuc  * this condition and the following disclaimer.
16*757e8328SLionel Sambuc  *
17*757e8328SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
18*757e8328SLionel Sambuc  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19*757e8328SLionel Sambuc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20*757e8328SLionel Sambuc  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21*757e8328SLionel Sambuc  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*757e8328SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23*757e8328SLionel Sambuc  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24*757e8328SLionel Sambuc  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*757e8328SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*757e8328SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*757e8328SLionel Sambuc  * SUCH DAMAGE.
28*757e8328SLionel Sambuc  *
29*757e8328SLionel Sambuc  * -C option added in 1998, original code by Marc Espie, based on FreeBSD
30*757e8328SLionel Sambuc  * behaviour
31*757e8328SLionel Sambuc  */
32*757e8328SLionel Sambuc 
33*757e8328SLionel Sambuc #include <sys/cdefs.h>
34*757e8328SLionel Sambuc __RCSID("$NetBSD: patch.c,v 1.29 2011/09/06 18:25:14 joerg Exp $");
35*757e8328SLionel Sambuc 
36*757e8328SLionel Sambuc #include <sys/types.h>
37*757e8328SLionel Sambuc #include <sys/stat.h>
38*757e8328SLionel Sambuc 
39*757e8328SLionel Sambuc #include <ctype.h>
40*757e8328SLionel Sambuc #include <getopt.h>
41*757e8328SLionel Sambuc #include <limits.h>
42*757e8328SLionel Sambuc #include <stdio.h>
43*757e8328SLionel Sambuc #include <string.h>
44*757e8328SLionel Sambuc #include <stdlib.h>
45*757e8328SLionel Sambuc #include <unistd.h>
46*757e8328SLionel Sambuc 
47*757e8328SLionel Sambuc #include "common.h"
48*757e8328SLionel Sambuc #include "util.h"
49*757e8328SLionel Sambuc #include "pch.h"
50*757e8328SLionel Sambuc #include "inp.h"
51*757e8328SLionel Sambuc #include "backupfile.h"
52*757e8328SLionel Sambuc #include "pathnames.h"
53*757e8328SLionel Sambuc 
54*757e8328SLionel Sambuc mode_t		filemode = 0644;
55*757e8328SLionel Sambuc 
56*757e8328SLionel Sambuc char		buf[MAXLINELEN];	/* general purpose buffer */
57*757e8328SLionel Sambuc size_t		buf_len = sizeof(buf);
58*757e8328SLionel Sambuc 
59*757e8328SLionel Sambuc bool		using_plan_a = true;	/* try to keep everything in memory */
60*757e8328SLionel Sambuc bool		out_of_mem = false;	/* ran out of memory in plan a */
61*757e8328SLionel Sambuc 
62*757e8328SLionel Sambuc #define MAXFILEC 2
63*757e8328SLionel Sambuc 
64*757e8328SLionel Sambuc char		*filearg[MAXFILEC];
65*757e8328SLionel Sambuc bool		ok_to_create_file = false;
66*757e8328SLionel Sambuc char		*outname = NULL;
67*757e8328SLionel Sambuc char		*origprae = NULL;
68*757e8328SLionel Sambuc char		*TMPOUTNAME;
69*757e8328SLionel Sambuc char		*TMPINNAME;
70*757e8328SLionel Sambuc char		*TMPREJNAME;
71*757e8328SLionel Sambuc char		*TMPPATNAME;
72*757e8328SLionel Sambuc bool		toutkeep = false;
73*757e8328SLionel Sambuc bool		trejkeep = false;
74*757e8328SLionel Sambuc bool		warn_on_invalid_line;
75*757e8328SLionel Sambuc bool		last_line_missing_eol;
76*757e8328SLionel Sambuc 
77*757e8328SLionel Sambuc #ifdef DEBUGGING
78*757e8328SLionel Sambuc int		debug = 0;
79*757e8328SLionel Sambuc #endif
80*757e8328SLionel Sambuc 
81*757e8328SLionel Sambuc bool		force = false;
82*757e8328SLionel Sambuc bool		batch = false;
83*757e8328SLionel Sambuc bool		verbose = true;
84*757e8328SLionel Sambuc bool		reverse = false;
85*757e8328SLionel Sambuc bool		noreverse = false;
86*757e8328SLionel Sambuc bool		skip_rest_of_patch = false;
87*757e8328SLionel Sambuc int		strippath = 957;
88*757e8328SLionel Sambuc bool		canonicalize = false;
89*757e8328SLionel Sambuc bool		check_only = false;
90*757e8328SLionel Sambuc int		diff_type = 0;
91*757e8328SLionel Sambuc char		*revision = NULL;	/* prerequisite revision, if any */
92*757e8328SLionel Sambuc LINENUM		input_lines = 0;	/* how long is input file in lines */
93*757e8328SLionel Sambuc int		posix = 0;		/* strict POSIX mode? */
94*757e8328SLionel Sambuc 
95*757e8328SLionel Sambuc static void	reinitialize_almost_everything(void);
96*757e8328SLionel Sambuc static void	get_some_switches(void);
97*757e8328SLionel Sambuc static LINENUM	locate_hunk(LINENUM);
98*757e8328SLionel Sambuc static void	abort_context_hunk(void);
99*757e8328SLionel Sambuc static void	rej_line(int, LINENUM);
100*757e8328SLionel Sambuc static void	abort_hunk(void);
101*757e8328SLionel Sambuc static void	apply_hunk(LINENUM);
102*757e8328SLionel Sambuc static void	init_output(const char *);
103*757e8328SLionel Sambuc static void	init_reject(const char *);
104*757e8328SLionel Sambuc static void	copy_till(LINENUM, bool);
105*757e8328SLionel Sambuc static bool	spew_output(void);
106*757e8328SLionel Sambuc static void	dump_line(LINENUM, bool);
107*757e8328SLionel Sambuc static bool	patch_match(LINENUM, LINENUM, LINENUM);
108*757e8328SLionel Sambuc static bool	similar(const char *, const char *, int);
109*757e8328SLionel Sambuc __dead static void	usage(void);
110*757e8328SLionel Sambuc 
111*757e8328SLionel Sambuc /* true if -E was specified on command line.  */
112*757e8328SLionel Sambuc static bool	remove_empty_files = false;
113*757e8328SLionel Sambuc 
114*757e8328SLionel Sambuc /* true if -R was specified on command line.  */
115*757e8328SLionel Sambuc static bool	reverse_flag_specified = false;
116*757e8328SLionel Sambuc 
117*757e8328SLionel Sambuc /* buffer holding the name of the rejected patch file. */
118*757e8328SLionel Sambuc static char	rejname[NAME_MAX + 1];
119*757e8328SLionel Sambuc 
120*757e8328SLionel Sambuc /* buffer for stderr */
121*757e8328SLionel Sambuc static char	serrbuf[BUFSIZ];
122*757e8328SLionel Sambuc 
123*757e8328SLionel Sambuc /* how many input lines have been irretractibly output */
124*757e8328SLionel Sambuc static LINENUM	last_frozen_line = 0;
125*757e8328SLionel Sambuc 
126*757e8328SLionel Sambuc static int	Argc;		/* guess */
127*757e8328SLionel Sambuc static char	**Argv;
128*757e8328SLionel Sambuc static int	Argc_last;	/* for restarting plan_b */
129*757e8328SLionel Sambuc static char	**Argv_last;
130*757e8328SLionel Sambuc 
131*757e8328SLionel Sambuc static FILE	*ofp = NULL;	/* output file pointer */
132*757e8328SLionel Sambuc static FILE	*rejfp = NULL;	/* reject file pointer */
133*757e8328SLionel Sambuc 
134*757e8328SLionel Sambuc static int	filec = 0;	/* how many file arguments? */
135*757e8328SLionel Sambuc static LINENUM	last_offset = 0;
136*757e8328SLionel Sambuc static LINENUM	maxfuzz = 2;
137*757e8328SLionel Sambuc 
138*757e8328SLionel Sambuc /* patch using ifdef, ifndef, etc. */
139*757e8328SLionel Sambuc static bool		do_defines = false;
140*757e8328SLionel Sambuc /* #ifdef xyzzy */
141*757e8328SLionel Sambuc static char		if_defined[128];
142*757e8328SLionel Sambuc /* #ifndef xyzzy */
143*757e8328SLionel Sambuc static char		not_defined[128];
144*757e8328SLionel Sambuc /* #else */
145*757e8328SLionel Sambuc static const char	else_defined[] = "#else\n";
146*757e8328SLionel Sambuc /* #endif xyzzy */
147*757e8328SLionel Sambuc static char		end_defined[128];
148*757e8328SLionel Sambuc 
149*757e8328SLionel Sambuc 
150*757e8328SLionel Sambuc /* Apply a set of diffs as appropriate. */
151*757e8328SLionel Sambuc 
152*757e8328SLionel Sambuc int
main(int argc,char * argv[])153*757e8328SLionel Sambuc main(int argc, char *argv[])
154*757e8328SLionel Sambuc {
155*757e8328SLionel Sambuc 	int	error = 0, hunk, failed, i, fd;
156*757e8328SLionel Sambuc 	LINENUM	where = 0, newwhere, fuzz, mymaxfuzz;
157*757e8328SLionel Sambuc 	const	char *tmpdir;
158*757e8328SLionel Sambuc 	char	*v;
159*757e8328SLionel Sambuc 
160*757e8328SLionel Sambuc 	setbuf(stderr, serrbuf);
161*757e8328SLionel Sambuc 	for (i = 0; i < MAXFILEC; i++)
162*757e8328SLionel Sambuc 		filearg[i] = NULL;
163*757e8328SLionel Sambuc 
164*757e8328SLionel Sambuc 	/* Cons up the names of the temporary files.  */
165*757e8328SLionel Sambuc 	if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0')
166*757e8328SLionel Sambuc 		tmpdir = _PATH_TMP;
167*757e8328SLionel Sambuc 	for (i = strlen(tmpdir) - 1; i > 0 && tmpdir[i] == '/'; i--)
168*757e8328SLionel Sambuc 		;
169*757e8328SLionel Sambuc 	i++;
170*757e8328SLionel Sambuc 	if (asprintf(&TMPOUTNAME, "%.*s/patchoXXXXXXXXXX", i, tmpdir) == -1)
171*757e8328SLionel Sambuc 		fatal("cannot allocate memory");
172*757e8328SLionel Sambuc 	if ((fd = mkstemp(TMPOUTNAME)) < 0)
173*757e8328SLionel Sambuc 		pfatal("can't create %s", TMPOUTNAME);
174*757e8328SLionel Sambuc 	close(fd);
175*757e8328SLionel Sambuc 
176*757e8328SLionel Sambuc 	if (asprintf(&TMPINNAME, "%.*s/patchiXXXXXXXXXX", i, tmpdir) == -1)
177*757e8328SLionel Sambuc 		fatal("cannot allocate memory");
178*757e8328SLionel Sambuc 	if ((fd = mkstemp(TMPINNAME)) < 0)
179*757e8328SLionel Sambuc 		pfatal("can't create %s", TMPINNAME);
180*757e8328SLionel Sambuc 	close(fd);
181*757e8328SLionel Sambuc 
182*757e8328SLionel Sambuc 	if (asprintf(&TMPREJNAME, "%.*s/patchrXXXXXXXXXX", i, tmpdir) == -1)
183*757e8328SLionel Sambuc 		fatal("cannot allocate memory");
184*757e8328SLionel Sambuc 	if ((fd = mkstemp(TMPREJNAME)) < 0)
185*757e8328SLionel Sambuc 		pfatal("can't create %s", TMPREJNAME);
186*757e8328SLionel Sambuc 	close(fd);
187*757e8328SLionel Sambuc 
188*757e8328SLionel Sambuc 	if (asprintf(&TMPPATNAME, "%.*s/patchpXXXXXXXXXX", i, tmpdir) == -1)
189*757e8328SLionel Sambuc 		fatal("cannot allocate memory");
190*757e8328SLionel Sambuc 	if ((fd = mkstemp(TMPPATNAME)) < 0)
191*757e8328SLionel Sambuc 		pfatal("can't create %s", TMPPATNAME);
192*757e8328SLionel Sambuc 	close(fd);
193*757e8328SLionel Sambuc 
194*757e8328SLionel Sambuc 	v = getenv("SIMPLE_BACKUP_SUFFIX");
195*757e8328SLionel Sambuc 	if (v)
196*757e8328SLionel Sambuc 		simple_backup_suffix = v;
197*757e8328SLionel Sambuc 	else
198*757e8328SLionel Sambuc 		simple_backup_suffix = ORIGEXT;
199*757e8328SLionel Sambuc 
200*757e8328SLionel Sambuc 	/* parse switches */
201*757e8328SLionel Sambuc 	Argc = argc;
202*757e8328SLionel Sambuc 	Argv = argv;
203*757e8328SLionel Sambuc 	get_some_switches();
204*757e8328SLionel Sambuc 
205*757e8328SLionel Sambuc 	if (backup_type == none) {
206*757e8328SLionel Sambuc 		if ((v = getenv("PATCH_VERSION_CONTROL")) == NULL)
207*757e8328SLionel Sambuc 			v = getenv("VERSION_CONTROL");
208*757e8328SLionel Sambuc 		if (v != NULL || !posix)
209*757e8328SLionel Sambuc 			backup_type = get_version(v);	/* OK to pass NULL. */
210*757e8328SLionel Sambuc 	}
211*757e8328SLionel Sambuc 
212*757e8328SLionel Sambuc 	/* make sure we clean up /tmp in case of disaster */
213*757e8328SLionel Sambuc 	set_signals(0);
214*757e8328SLionel Sambuc 
215*757e8328SLionel Sambuc 	for (open_patch_file(filearg[1]); there_is_another_patch();
216*757e8328SLionel Sambuc 	    reinitialize_almost_everything()) {
217*757e8328SLionel Sambuc 		/* for each patch in patch file */
218*757e8328SLionel Sambuc 
219*757e8328SLionel Sambuc 		warn_on_invalid_line = true;
220*757e8328SLionel Sambuc 
221*757e8328SLionel Sambuc 		if (outname == NULL)
222*757e8328SLionel Sambuc 			outname = savestr(filearg[0]);
223*757e8328SLionel Sambuc 
224*757e8328SLionel Sambuc 		/* for ed script just up and do it and exit */
225*757e8328SLionel Sambuc 		if (diff_type == ED_DIFF) {
226*757e8328SLionel Sambuc 			do_ed_script();
227*757e8328SLionel Sambuc 			continue;
228*757e8328SLionel Sambuc 		}
229*757e8328SLionel Sambuc 		/* initialize the patched file */
230*757e8328SLionel Sambuc 		if (!skip_rest_of_patch)
231*757e8328SLionel Sambuc 			init_output(TMPOUTNAME);
232*757e8328SLionel Sambuc 
233*757e8328SLionel Sambuc 		/* initialize reject file */
234*757e8328SLionel Sambuc 		init_reject(TMPREJNAME);
235*757e8328SLionel Sambuc 
236*757e8328SLionel Sambuc 		/* find out where all the lines are */
237*757e8328SLionel Sambuc 		if (!skip_rest_of_patch)
238*757e8328SLionel Sambuc 			scan_input(filearg[0]);
239*757e8328SLionel Sambuc 
240*757e8328SLionel Sambuc 		/* from here on, open no standard i/o files, because malloc */
241*757e8328SLionel Sambuc 		/* might misfire and we can't catch it easily */
242*757e8328SLionel Sambuc 
243*757e8328SLionel Sambuc 		/* apply each hunk of patch */
244*757e8328SLionel Sambuc 		hunk = 0;
245*757e8328SLionel Sambuc 		failed = 0;
246*757e8328SLionel Sambuc 		out_of_mem = false;
247*757e8328SLionel Sambuc 		while (another_hunk()) {
248*757e8328SLionel Sambuc 			hunk++;
249*757e8328SLionel Sambuc 			fuzz = 0;
250*757e8328SLionel Sambuc 			mymaxfuzz = pch_context();
251*757e8328SLionel Sambuc 			if (maxfuzz < mymaxfuzz)
252*757e8328SLionel Sambuc 				mymaxfuzz = maxfuzz;
253*757e8328SLionel Sambuc 			if (!skip_rest_of_patch) {
254*757e8328SLionel Sambuc 				do {
255*757e8328SLionel Sambuc 					where = locate_hunk(fuzz);
256*757e8328SLionel Sambuc 					if (hunk == 1 && where == 0 && !force) {
257*757e8328SLionel Sambuc 						/* dwim for reversed patch? */
258*757e8328SLionel Sambuc 						if (!pch_swap()) {
259*757e8328SLionel Sambuc 							if (fuzz == 0)
260*757e8328SLionel Sambuc 								say("Not enough memory to try swapped hunk!  Assuming unswapped.\n");
261*757e8328SLionel Sambuc 							continue;
262*757e8328SLionel Sambuc 						}
263*757e8328SLionel Sambuc 						reverse = !reverse;
264*757e8328SLionel Sambuc 						/* try again */
265*757e8328SLionel Sambuc 						where = locate_hunk(fuzz);
266*757e8328SLionel Sambuc 						if (where == 0) {
267*757e8328SLionel Sambuc 							/* didn't find it swapped */
268*757e8328SLionel Sambuc 							if (!pch_swap())
269*757e8328SLionel Sambuc 								/* put it back to normal */
270*757e8328SLionel Sambuc 								fatal("lost hunk on alloc error!\n");
271*757e8328SLionel Sambuc 							reverse = !reverse;
272*757e8328SLionel Sambuc 						} else if (noreverse) {
273*757e8328SLionel Sambuc 							if (!pch_swap())
274*757e8328SLionel Sambuc 								/* put it back to normal */
275*757e8328SLionel Sambuc 								fatal("lost hunk on alloc error!\n");
276*757e8328SLionel Sambuc 							reverse = !reverse;
277*757e8328SLionel Sambuc 							say("Ignoring previously applied (or reversed) patch.\n");
278*757e8328SLionel Sambuc 							skip_rest_of_patch = true;
279*757e8328SLionel Sambuc 						} else if (batch) {
280*757e8328SLionel Sambuc 							if (verbose)
281*757e8328SLionel Sambuc 								say("%seversed (or previously applied) patch detected!  %s -R.",
282*757e8328SLionel Sambuc 								    reverse ? "R" : "Unr",
283*757e8328SLionel Sambuc 								    reverse ? "Assuming" : "Ignoring");
284*757e8328SLionel Sambuc 						} else {
285*757e8328SLionel Sambuc 							ask("%seversed (or previously applied) patch detected!  %s -R? [y] ",
286*757e8328SLionel Sambuc 							    reverse ? "R" : "Unr",
287*757e8328SLionel Sambuc 							    reverse ? "Assume" : "Ignore");
288*757e8328SLionel Sambuc 							if (*buf == 'n') {
289*757e8328SLionel Sambuc 								ask("Apply anyway? [n] ");
290*757e8328SLionel Sambuc 								if (*buf != 'y')
291*757e8328SLionel Sambuc 									skip_rest_of_patch = true;
292*757e8328SLionel Sambuc 								where = 0;
293*757e8328SLionel Sambuc 								reverse = !reverse;
294*757e8328SLionel Sambuc 								if (!pch_swap())
295*757e8328SLionel Sambuc 									/* put it back to normal */
296*757e8328SLionel Sambuc 									fatal("lost hunk on alloc error!\n");
297*757e8328SLionel Sambuc 							}
298*757e8328SLionel Sambuc 						}
299*757e8328SLionel Sambuc 					}
300*757e8328SLionel Sambuc 				} while (!skip_rest_of_patch && where == 0 &&
301*757e8328SLionel Sambuc 				    ++fuzz <= mymaxfuzz);
302*757e8328SLionel Sambuc 
303*757e8328SLionel Sambuc 				if (skip_rest_of_patch) {	/* just got decided */
304*757e8328SLionel Sambuc 					if (ferror(ofp) || fclose(ofp)) {
305*757e8328SLionel Sambuc 						say("Error writing %s\n",
306*757e8328SLionel Sambuc 						    TMPOUTNAME);
307*757e8328SLionel Sambuc 						error = 1;
308*757e8328SLionel Sambuc 					}
309*757e8328SLionel Sambuc 					ofp = NULL;
310*757e8328SLionel Sambuc 				}
311*757e8328SLionel Sambuc 			}
312*757e8328SLionel Sambuc 			newwhere = pch_newfirst() + last_offset;
313*757e8328SLionel Sambuc 			if (skip_rest_of_patch) {
314*757e8328SLionel Sambuc 				abort_hunk();
315*757e8328SLionel Sambuc 				failed++;
316*757e8328SLionel Sambuc 				if (verbose)
317*757e8328SLionel Sambuc 					say("Hunk #%d ignored at %ld.\n",
318*757e8328SLionel Sambuc 					    hunk, newwhere);
319*757e8328SLionel Sambuc 			} else if (where == 0) {
320*757e8328SLionel Sambuc 				abort_hunk();
321*757e8328SLionel Sambuc 				failed++;
322*757e8328SLionel Sambuc 				if (verbose)
323*757e8328SLionel Sambuc 					say("Hunk #%d failed at %ld.\n",
324*757e8328SLionel Sambuc 					    hunk, newwhere);
325*757e8328SLionel Sambuc 			} else {
326*757e8328SLionel Sambuc 				apply_hunk(where);
327*757e8328SLionel Sambuc 				if (verbose) {
328*757e8328SLionel Sambuc 					say("Hunk #%d succeeded at %ld",
329*757e8328SLionel Sambuc 					    hunk, newwhere);
330*757e8328SLionel Sambuc 					if (fuzz != 0)
331*757e8328SLionel Sambuc 						say(" with fuzz %ld", fuzz);
332*757e8328SLionel Sambuc 					if (last_offset)
333*757e8328SLionel Sambuc 						say(" (offset %ld line%s)",
334*757e8328SLionel Sambuc 						    last_offset,
335*757e8328SLionel Sambuc 						    last_offset == 1L ? "" : "s");
336*757e8328SLionel Sambuc 					say(".\n");
337*757e8328SLionel Sambuc 				}
338*757e8328SLionel Sambuc 			}
339*757e8328SLionel Sambuc 		}
340*757e8328SLionel Sambuc 
341*757e8328SLionel Sambuc 		if (out_of_mem && using_plan_a) {
342*757e8328SLionel Sambuc 			Argc = Argc_last;
343*757e8328SLionel Sambuc 			Argv = Argv_last;
344*757e8328SLionel Sambuc 			say("\n\nRan out of memory using Plan A--trying again...\n\n");
345*757e8328SLionel Sambuc 			if (ofp)
346*757e8328SLionel Sambuc 				fclose(ofp);
347*757e8328SLionel Sambuc 			ofp = NULL;
348*757e8328SLionel Sambuc 			if (rejfp)
349*757e8328SLionel Sambuc 				fclose(rejfp);
350*757e8328SLionel Sambuc 			rejfp = NULL;
351*757e8328SLionel Sambuc 			continue;
352*757e8328SLionel Sambuc 		}
353*757e8328SLionel Sambuc 		if (hunk == 0)
354*757e8328SLionel Sambuc 			fatal("Internal error: hunk should not be 0\n");
355*757e8328SLionel Sambuc 
356*757e8328SLionel Sambuc 		/* finish spewing out the new file */
357*757e8328SLionel Sambuc 		if (!skip_rest_of_patch && !spew_output()) {
358*757e8328SLionel Sambuc 			say("Can't write %s\n", TMPOUTNAME);
359*757e8328SLionel Sambuc 			error = 1;
360*757e8328SLionel Sambuc 		}
361*757e8328SLionel Sambuc 
362*757e8328SLionel Sambuc 		/* and put the output where desired */
363*757e8328SLionel Sambuc 		ignore_signals();
364*757e8328SLionel Sambuc 		if (!skip_rest_of_patch) {
365*757e8328SLionel Sambuc 			struct stat	statbuf;
366*757e8328SLionel Sambuc 			char	*realout = outname;
367*757e8328SLionel Sambuc 
368*757e8328SLionel Sambuc 			if (!check_only) {
369*757e8328SLionel Sambuc 				if (move_file(TMPOUTNAME, outname) < 0) {
370*757e8328SLionel Sambuc 					toutkeep = true;
371*757e8328SLionel Sambuc 					realout = TMPOUTNAME;
372*757e8328SLionel Sambuc 					chmod(TMPOUTNAME, filemode);
373*757e8328SLionel Sambuc 				} else
374*757e8328SLionel Sambuc 					chmod(outname, filemode);
375*757e8328SLionel Sambuc 
376*757e8328SLionel Sambuc 				if (remove_empty_files &&
377*757e8328SLionel Sambuc 				    stat(realout, &statbuf) == 0 &&
378*757e8328SLionel Sambuc 				    statbuf.st_size == 0) {
379*757e8328SLionel Sambuc 					if (verbose)
380*757e8328SLionel Sambuc 						say("Removing %s (empty after patching).\n",
381*757e8328SLionel Sambuc 						    realout);
382*757e8328SLionel Sambuc 					unlink(realout);
383*757e8328SLionel Sambuc 				}
384*757e8328SLionel Sambuc 			}
385*757e8328SLionel Sambuc 		}
386*757e8328SLionel Sambuc 		if (ferror(rejfp) || fclose(rejfp)) {
387*757e8328SLionel Sambuc 			say("Error writing %s\n", rejname);
388*757e8328SLionel Sambuc 			error = 1;
389*757e8328SLionel Sambuc 		}
390*757e8328SLionel Sambuc 		rejfp = NULL;
391*757e8328SLionel Sambuc 		if (failed) {
392*757e8328SLionel Sambuc 			error = 1;
393*757e8328SLionel Sambuc 			if (*rejname == '\0') {
394*757e8328SLionel Sambuc 				if (strlcpy(rejname, outname,
395*757e8328SLionel Sambuc 				    sizeof(rejname)) >= sizeof(rejname))
396*757e8328SLionel Sambuc 					fatal("filename %s is too long\n", outname);
397*757e8328SLionel Sambuc 				if (strlcat(rejname, REJEXT,
398*757e8328SLionel Sambuc 				    sizeof(rejname)) >= sizeof(rejname))
399*757e8328SLionel Sambuc 					fatal("filename %s is too long\n", outname);
400*757e8328SLionel Sambuc 			}
401*757e8328SLionel Sambuc 			if (skip_rest_of_patch) {
402*757e8328SLionel Sambuc 				say("%d out of %d hunks ignored--saving rejects to %s\n",
403*757e8328SLionel Sambuc 				    failed, hunk, rejname);
404*757e8328SLionel Sambuc 			} else {
405*757e8328SLionel Sambuc 				say("%d out of %d hunks failed--saving rejects to %s\n",
406*757e8328SLionel Sambuc 				    failed, hunk, rejname);
407*757e8328SLionel Sambuc 			}
408*757e8328SLionel Sambuc 			if (!check_only && move_file(TMPREJNAME, rejname) < 0)
409*757e8328SLionel Sambuc 				trejkeep = true;
410*757e8328SLionel Sambuc 		}
411*757e8328SLionel Sambuc 		set_signals(1);
412*757e8328SLionel Sambuc 	}
413*757e8328SLionel Sambuc 	my_exit(error);
414*757e8328SLionel Sambuc 	/* NOTREACHED */
415*757e8328SLionel Sambuc }
416*757e8328SLionel Sambuc 
417*757e8328SLionel Sambuc /* Prepare to find the next patch to do in the patch file. */
418*757e8328SLionel Sambuc 
419*757e8328SLionel Sambuc static void
reinitialize_almost_everything(void)420*757e8328SLionel Sambuc reinitialize_almost_everything(void)
421*757e8328SLionel Sambuc {
422*757e8328SLionel Sambuc 	re_patch();
423*757e8328SLionel Sambuc 	re_input();
424*757e8328SLionel Sambuc 
425*757e8328SLionel Sambuc 	input_lines = 0;
426*757e8328SLionel Sambuc 	last_frozen_line = 0;
427*757e8328SLionel Sambuc 
428*757e8328SLionel Sambuc 	filec = 0;
429*757e8328SLionel Sambuc 	if (!out_of_mem) {
430*757e8328SLionel Sambuc 		free(filearg[0]);
431*757e8328SLionel Sambuc 		filearg[0] = NULL;
432*757e8328SLionel Sambuc 	}
433*757e8328SLionel Sambuc 
434*757e8328SLionel Sambuc 	free(outname);
435*757e8328SLionel Sambuc 	outname = NULL;
436*757e8328SLionel Sambuc 
437*757e8328SLionel Sambuc 	last_offset = 0;
438*757e8328SLionel Sambuc 	diff_type = 0;
439*757e8328SLionel Sambuc 
440*757e8328SLionel Sambuc 	free(revision);
441*757e8328SLionel Sambuc 	revision = NULL;
442*757e8328SLionel Sambuc 
443*757e8328SLionel Sambuc 	reverse = reverse_flag_specified;
444*757e8328SLionel Sambuc 	skip_rest_of_patch = false;
445*757e8328SLionel Sambuc 
446*757e8328SLionel Sambuc 	get_some_switches();
447*757e8328SLionel Sambuc }
448*757e8328SLionel Sambuc 
449*757e8328SLionel Sambuc /* Process switches and filenames. */
450*757e8328SLionel Sambuc 
451*757e8328SLionel Sambuc static void
get_some_switches(void)452*757e8328SLionel Sambuc get_some_switches(void)
453*757e8328SLionel Sambuc {
454*757e8328SLionel Sambuc 	const char *options = "b::B:cCd:D:eEfF:i:lnNo:p:r:RstuvV:x:z:";
455*757e8328SLionel Sambuc 	static struct option longopts[] = {
456*757e8328SLionel Sambuc 		{"backup",		no_argument,		0,	'b'},
457*757e8328SLionel Sambuc 		{"batch",		no_argument,		0,	't'},
458*757e8328SLionel Sambuc 		{"check",		no_argument,		0,	'C'},
459*757e8328SLionel Sambuc 		{"context",		no_argument,		0,	'c'},
460*757e8328SLionel Sambuc 		{"debug",		required_argument,	0,	'x'},
461*757e8328SLionel Sambuc 		{"directory",		required_argument,	0,	'd'},
462*757e8328SLionel Sambuc 		{"ed",			no_argument,		0,	'e'},
463*757e8328SLionel Sambuc 		{"force",		no_argument,		0,	'f'},
464*757e8328SLionel Sambuc 		{"forward",		no_argument,		0,	'N'},
465*757e8328SLionel Sambuc 		{"fuzz",		required_argument,	0,	'F'},
466*757e8328SLionel Sambuc 		{"ifdef",		required_argument,	0,	'D'},
467*757e8328SLionel Sambuc 		{"input",		required_argument,	0,	'i'},
468*757e8328SLionel Sambuc 		{"ignore-whitespace",	no_argument,		0,	'l'},
469*757e8328SLionel Sambuc 		{"normal",		no_argument,		0,	'n'},
470*757e8328SLionel Sambuc 		{"output",		required_argument,	0,	'o'},
471*757e8328SLionel Sambuc 		{"prefix",		required_argument,	0,	'B'},
472*757e8328SLionel Sambuc 		{"quiet",		no_argument,		0,	's'},
473*757e8328SLionel Sambuc 		{"reject-file",		required_argument,	0,	'r'},
474*757e8328SLionel Sambuc 		{"remove-empty-files",	no_argument,		0,	'E'},
475*757e8328SLionel Sambuc 		{"reverse",		no_argument,		0,	'R'},
476*757e8328SLionel Sambuc 		{"silent",		no_argument,		0,	's'},
477*757e8328SLionel Sambuc 		{"strip",		required_argument,	0,	'p'},
478*757e8328SLionel Sambuc 		{"suffix",		required_argument,	0,	'z'},
479*757e8328SLionel Sambuc 		{"unified",		no_argument,		0,	'u'},
480*757e8328SLionel Sambuc 		{"version",		no_argument,		0,	'v'},
481*757e8328SLionel Sambuc 		{"version-control",	required_argument,	0,	'V'},
482*757e8328SLionel Sambuc 		{"posix",		no_argument,		&posix,	1},
483*757e8328SLionel Sambuc 		{NULL,			0,			0,	0}
484*757e8328SLionel Sambuc 	};
485*757e8328SLionel Sambuc 	int ch;
486*757e8328SLionel Sambuc 
487*757e8328SLionel Sambuc 	rejname[0] = '\0';
488*757e8328SLionel Sambuc 	Argc_last = Argc;
489*757e8328SLionel Sambuc 	Argv_last = Argv;
490*757e8328SLionel Sambuc 	if (!Argc)
491*757e8328SLionel Sambuc 		return;
492*757e8328SLionel Sambuc 	optreset = optind = 1;
493*757e8328SLionel Sambuc 	while ((ch = getopt_long(Argc, Argv, options, longopts, NULL)) != -1) {
494*757e8328SLionel Sambuc 		switch (ch) {
495*757e8328SLionel Sambuc 		case 'b':
496*757e8328SLionel Sambuc 			if (backup_type == none)
497*757e8328SLionel Sambuc 				backup_type = numbered_existing;
498*757e8328SLionel Sambuc 			if (optarg == NULL)
499*757e8328SLionel Sambuc 				break;
500*757e8328SLionel Sambuc 			if (verbose)
501*757e8328SLionel Sambuc 				say("Warning, the ``-b suffix'' option has been"
502*757e8328SLionel Sambuc 				    " obsoleted by the -z option.\n");
503*757e8328SLionel Sambuc 			/* FALLTHROUGH */
504*757e8328SLionel Sambuc 		case 'z':
505*757e8328SLionel Sambuc 			/* must directly follow 'b' case for backwards compat */
506*757e8328SLionel Sambuc 			simple_backup_suffix = savestr(optarg);
507*757e8328SLionel Sambuc 			break;
508*757e8328SLionel Sambuc 		case 'B':
509*757e8328SLionel Sambuc 			origprae = savestr(optarg);
510*757e8328SLionel Sambuc 			break;
511*757e8328SLionel Sambuc 		case 'c':
512*757e8328SLionel Sambuc 			diff_type = CONTEXT_DIFF;
513*757e8328SLionel Sambuc 			break;
514*757e8328SLionel Sambuc 		case 'C':
515*757e8328SLionel Sambuc 			check_only = true;
516*757e8328SLionel Sambuc 			break;
517*757e8328SLionel Sambuc 		case 'd':
518*757e8328SLionel Sambuc 			if (chdir(optarg) < 0)
519*757e8328SLionel Sambuc 				pfatal("can't cd to %s", optarg);
520*757e8328SLionel Sambuc 			break;
521*757e8328SLionel Sambuc 		case 'D':
522*757e8328SLionel Sambuc 			do_defines = true;
523*757e8328SLionel Sambuc 			if (!isalpha((unsigned char)*optarg) && *optarg != '_')
524*757e8328SLionel Sambuc 				fatal("argument to -D is not an identifier\n");
525*757e8328SLionel Sambuc 			snprintf(if_defined, sizeof if_defined,
526*757e8328SLionel Sambuc 			    "#ifdef %s\n", optarg);
527*757e8328SLionel Sambuc 			snprintf(not_defined, sizeof not_defined,
528*757e8328SLionel Sambuc 			    "#ifndef %s\n", optarg);
529*757e8328SLionel Sambuc 			snprintf(end_defined, sizeof end_defined,
530*757e8328SLionel Sambuc 			    "#endif /* %s */\n", optarg);
531*757e8328SLionel Sambuc 			break;
532*757e8328SLionel Sambuc 		case 'e':
533*757e8328SLionel Sambuc 			diff_type = ED_DIFF;
534*757e8328SLionel Sambuc 			break;
535*757e8328SLionel Sambuc 		case 'E':
536*757e8328SLionel Sambuc 			remove_empty_files = true;
537*757e8328SLionel Sambuc 			break;
538*757e8328SLionel Sambuc 		case 'f':
539*757e8328SLionel Sambuc 			force = true;
540*757e8328SLionel Sambuc 			break;
541*757e8328SLionel Sambuc 		case 'F':
542*757e8328SLionel Sambuc 			maxfuzz = atoi(optarg);
543*757e8328SLionel Sambuc 			break;
544*757e8328SLionel Sambuc 		case 'i':
545*757e8328SLionel Sambuc 			if (++filec == MAXFILEC)
546*757e8328SLionel Sambuc 				fatal("too many file arguments\n");
547*757e8328SLionel Sambuc 			filearg[filec] = savestr(optarg);
548*757e8328SLionel Sambuc 			break;
549*757e8328SLionel Sambuc 		case 'l':
550*757e8328SLionel Sambuc 			canonicalize = true;
551*757e8328SLionel Sambuc 			break;
552*757e8328SLionel Sambuc 		case 'n':
553*757e8328SLionel Sambuc 			diff_type = NORMAL_DIFF;
554*757e8328SLionel Sambuc 			break;
555*757e8328SLionel Sambuc 		case 'N':
556*757e8328SLionel Sambuc 			noreverse = true;
557*757e8328SLionel Sambuc 			break;
558*757e8328SLionel Sambuc 		case 'o':
559*757e8328SLionel Sambuc 			outname = savestr(optarg);
560*757e8328SLionel Sambuc 			break;
561*757e8328SLionel Sambuc 		case 'p':
562*757e8328SLionel Sambuc 			strippath = atoi(optarg);
563*757e8328SLionel Sambuc 			break;
564*757e8328SLionel Sambuc 		case 'r':
565*757e8328SLionel Sambuc 			if (strlcpy(rejname, optarg,
566*757e8328SLionel Sambuc 			    sizeof(rejname)) >= sizeof(rejname))
567*757e8328SLionel Sambuc 				fatal("argument for -r is too long\n");
568*757e8328SLionel Sambuc 			break;
569*757e8328SLionel Sambuc 		case 'R':
570*757e8328SLionel Sambuc 			reverse = true;
571*757e8328SLionel Sambuc 			reverse_flag_specified = true;
572*757e8328SLionel Sambuc 			break;
573*757e8328SLionel Sambuc 		case 's':
574*757e8328SLionel Sambuc 			verbose = false;
575*757e8328SLionel Sambuc 			break;
576*757e8328SLionel Sambuc 		case 't':
577*757e8328SLionel Sambuc 			batch = true;
578*757e8328SLionel Sambuc 			break;
579*757e8328SLionel Sambuc 		case 'u':
580*757e8328SLionel Sambuc 			diff_type = UNI_DIFF;
581*757e8328SLionel Sambuc 			break;
582*757e8328SLionel Sambuc 		case 'v':
583*757e8328SLionel Sambuc 			version();
584*757e8328SLionel Sambuc 			break;
585*757e8328SLionel Sambuc 		case 'V':
586*757e8328SLionel Sambuc 			backup_type = get_version(optarg);
587*757e8328SLionel Sambuc 			break;
588*757e8328SLionel Sambuc #ifdef DEBUGGING
589*757e8328SLionel Sambuc 		case 'x':
590*757e8328SLionel Sambuc 			debug = atoi(optarg);
591*757e8328SLionel Sambuc 			break;
592*757e8328SLionel Sambuc #endif
593*757e8328SLionel Sambuc 		default:
594*757e8328SLionel Sambuc 			if (ch != '\0')
595*757e8328SLionel Sambuc 				usage();
596*757e8328SLionel Sambuc 			break;
597*757e8328SLionel Sambuc 		}
598*757e8328SLionel Sambuc 	}
599*757e8328SLionel Sambuc 	Argc -= optind;
600*757e8328SLionel Sambuc 	Argv += optind;
601*757e8328SLionel Sambuc 
602*757e8328SLionel Sambuc 	if (Argc > 0) {
603*757e8328SLionel Sambuc 		filearg[0] = savestr(*Argv++);
604*757e8328SLionel Sambuc 		Argc--;
605*757e8328SLionel Sambuc 		while (Argc > 0) {
606*757e8328SLionel Sambuc 			if (++filec == MAXFILEC)
607*757e8328SLionel Sambuc 				fatal("too many file arguments\n");
608*757e8328SLionel Sambuc 			filearg[filec] = savestr(*Argv++);
609*757e8328SLionel Sambuc 			Argc--;
610*757e8328SLionel Sambuc 		}
611*757e8328SLionel Sambuc 	}
612*757e8328SLionel Sambuc 
613*757e8328SLionel Sambuc 	if (getenv("POSIXLY_CORRECT") != NULL)
614*757e8328SLionel Sambuc 		posix = 1;
615*757e8328SLionel Sambuc }
616*757e8328SLionel Sambuc 
617*757e8328SLionel Sambuc static void
usage(void)618*757e8328SLionel Sambuc usage(void)
619*757e8328SLionel Sambuc {
620*757e8328SLionel Sambuc 	fprintf(stderr,
621*757e8328SLionel Sambuc "usage: patch [-bCcEeflNnRstuv] [-B backup-prefix] [-D symbol] [-d directory]\n"
622*757e8328SLionel Sambuc "             [-F max-fuzz] [-i patchfile] [-o out-file] [-p strip-count]\n"
623*757e8328SLionel Sambuc "             [-r rej-name] [-V t | nil | never] [-x number] [-z backup-ext]\n"
624*757e8328SLionel Sambuc "             [--posix] [origfile [patchfile]]\n"
625*757e8328SLionel Sambuc "       patch <patchfile\n");
626*757e8328SLionel Sambuc 	my_exit(EXIT_FAILURE);
627*757e8328SLionel Sambuc }
628*757e8328SLionel Sambuc 
629*757e8328SLionel Sambuc /*
630*757e8328SLionel Sambuc  * Attempt to find the right place to apply this hunk of patch.
631*757e8328SLionel Sambuc  */
632*757e8328SLionel Sambuc static LINENUM
locate_hunk(LINENUM fuzz)633*757e8328SLionel Sambuc locate_hunk(LINENUM fuzz)
634*757e8328SLionel Sambuc {
635*757e8328SLionel Sambuc 	LINENUM	first_guess = pch_first() + last_offset;
636*757e8328SLionel Sambuc 	LINENUM	offset;
637*757e8328SLionel Sambuc 	LINENUM	pat_lines = pch_ptrn_lines();
638*757e8328SLionel Sambuc 	LINENUM	max_pos_offset = input_lines - first_guess - pat_lines + 1;
639*757e8328SLionel Sambuc 	LINENUM	max_neg_offset = first_guess - last_frozen_line - 1 + pch_context();
640*757e8328SLionel Sambuc 
641*757e8328SLionel Sambuc 	if (pat_lines == 0) {		/* null range matches always */
642*757e8328SLionel Sambuc 		if (verbose && fuzz == 0 && (diff_type == CONTEXT_DIFF
643*757e8328SLionel Sambuc 		    || diff_type == NEW_CONTEXT_DIFF
644*757e8328SLionel Sambuc 		    || diff_type == UNI_DIFF)) {
645*757e8328SLionel Sambuc 			say("Empty context always matches.\n");
646*757e8328SLionel Sambuc 		}
647*757e8328SLionel Sambuc 		return (first_guess);
648*757e8328SLionel Sambuc 	}
649*757e8328SLionel Sambuc 	if (max_neg_offset >= first_guess)	/* do not try lines < 0 */
650*757e8328SLionel Sambuc 		max_neg_offset = first_guess - 1;
651*757e8328SLionel Sambuc 	if (first_guess <= input_lines && patch_match(first_guess, 0, fuzz))
652*757e8328SLionel Sambuc 		return first_guess;
653*757e8328SLionel Sambuc 	for (offset = 1; ; offset++) {
654*757e8328SLionel Sambuc 		bool	check_after = (offset <= max_pos_offset);
655*757e8328SLionel Sambuc 		bool	check_before = (offset <= max_neg_offset);
656*757e8328SLionel Sambuc 
657*757e8328SLionel Sambuc 		if (check_after && patch_match(first_guess, offset, fuzz)) {
658*757e8328SLionel Sambuc #ifdef DEBUGGING
659*757e8328SLionel Sambuc 			if (debug & 1)
660*757e8328SLionel Sambuc 				say("Offset changing from %ld to %ld\n",
661*757e8328SLionel Sambuc 				    last_offset, offset);
662*757e8328SLionel Sambuc #endif
663*757e8328SLionel Sambuc 			last_offset = offset;
664*757e8328SLionel Sambuc 			return first_guess + offset;
665*757e8328SLionel Sambuc 		} else if (check_before && patch_match(first_guess, -offset, fuzz)) {
666*757e8328SLionel Sambuc #ifdef DEBUGGING
667*757e8328SLionel Sambuc 			if (debug & 1)
668*757e8328SLionel Sambuc 				say("Offset changing from %ld to %ld\n",
669*757e8328SLionel Sambuc 				    last_offset, -offset);
670*757e8328SLionel Sambuc #endif
671*757e8328SLionel Sambuc 			last_offset = -offset;
672*757e8328SLionel Sambuc 			return first_guess - offset;
673*757e8328SLionel Sambuc 		} else if (!check_before && !check_after)
674*757e8328SLionel Sambuc 			return 0;
675*757e8328SLionel Sambuc 	}
676*757e8328SLionel Sambuc }
677*757e8328SLionel Sambuc 
678*757e8328SLionel Sambuc /* We did not find the pattern, dump out the hunk so they can handle it. */
679*757e8328SLionel Sambuc 
680*757e8328SLionel Sambuc static void
abort_context_hunk(void)681*757e8328SLionel Sambuc abort_context_hunk(void)
682*757e8328SLionel Sambuc {
683*757e8328SLionel Sambuc 	LINENUM	i;
684*757e8328SLionel Sambuc 	const LINENUM	pat_end = pch_end();
685*757e8328SLionel Sambuc 	/*
686*757e8328SLionel Sambuc 	 * add in last_offset to guess the same as the previous successful
687*757e8328SLionel Sambuc 	 * hunk
688*757e8328SLionel Sambuc 	 */
689*757e8328SLionel Sambuc 	const LINENUM	oldfirst = pch_first() + last_offset;
690*757e8328SLionel Sambuc 	const LINENUM	newfirst = pch_newfirst() + last_offset;
691*757e8328SLionel Sambuc 	const LINENUM	oldlast = oldfirst + pch_ptrn_lines() - 1;
692*757e8328SLionel Sambuc 	const LINENUM	newlast = newfirst + pch_repl_lines() - 1;
693*757e8328SLionel Sambuc 	const char	*stars = (diff_type >= NEW_CONTEXT_DIFF ? " ****" : "");
694*757e8328SLionel Sambuc 	const char	*minuses = (diff_type >= NEW_CONTEXT_DIFF ? " ----" : " -----");
695*757e8328SLionel Sambuc 
696*757e8328SLionel Sambuc 	fprintf(rejfp, "***************\n");
697*757e8328SLionel Sambuc 	for (i = 0; i <= pat_end; i++) {
698*757e8328SLionel Sambuc 		switch (pch_char(i)) {
699*757e8328SLionel Sambuc 		case '*':
700*757e8328SLionel Sambuc 			if (oldlast < oldfirst)
701*757e8328SLionel Sambuc 				fprintf(rejfp, "*** 0%s\n", stars);
702*757e8328SLionel Sambuc 			else if (oldlast == oldfirst)
703*757e8328SLionel Sambuc 				fprintf(rejfp, "*** %ld%s\n", oldfirst, stars);
704*757e8328SLionel Sambuc 			else
705*757e8328SLionel Sambuc 				fprintf(rejfp, "*** %ld,%ld%s\n", oldfirst,
706*757e8328SLionel Sambuc 				    oldlast, stars);
707*757e8328SLionel Sambuc 			break;
708*757e8328SLionel Sambuc 		case '=':
709*757e8328SLionel Sambuc 			if (newlast < newfirst)
710*757e8328SLionel Sambuc 				fprintf(rejfp, "--- 0%s\n", minuses);
711*757e8328SLionel Sambuc 			else if (newlast == newfirst)
712*757e8328SLionel Sambuc 				fprintf(rejfp, "--- %ld%s\n", newfirst, minuses);
713*757e8328SLionel Sambuc 			else
714*757e8328SLionel Sambuc 				fprintf(rejfp, "--- %ld,%ld%s\n", newfirst,
715*757e8328SLionel Sambuc 				    newlast, minuses);
716*757e8328SLionel Sambuc 			break;
717*757e8328SLionel Sambuc 		case '\n':
718*757e8328SLionel Sambuc 			fprintf(rejfp, "%s", pfetch(i));
719*757e8328SLionel Sambuc 			break;
720*757e8328SLionel Sambuc 		case ' ':
721*757e8328SLionel Sambuc 		case '-':
722*757e8328SLionel Sambuc 		case '+':
723*757e8328SLionel Sambuc 		case '!':
724*757e8328SLionel Sambuc 			fprintf(rejfp, "%c %s", pch_char(i), pfetch(i));
725*757e8328SLionel Sambuc 			break;
726*757e8328SLionel Sambuc 		default:
727*757e8328SLionel Sambuc 			fatal("fatal internal error in abort_context_hunk\n");
728*757e8328SLionel Sambuc 		}
729*757e8328SLionel Sambuc 	}
730*757e8328SLionel Sambuc }
731*757e8328SLionel Sambuc 
732*757e8328SLionel Sambuc static void
rej_line(int ch,LINENUM i)733*757e8328SLionel Sambuc rej_line(int ch, LINENUM i)
734*757e8328SLionel Sambuc {
735*757e8328SLionel Sambuc 	size_t len;
736*757e8328SLionel Sambuc 	const char *line = pfetch(i);
737*757e8328SLionel Sambuc 
738*757e8328SLionel Sambuc 	len = strlen(line);
739*757e8328SLionel Sambuc 
740*757e8328SLionel Sambuc 	fprintf(rejfp, "%c%s", ch, line);
741*757e8328SLionel Sambuc 	if (len == 0 || line[len-1] != '\n')
742*757e8328SLionel Sambuc 		fprintf(rejfp, "\n\\ No newline at end of file\n");
743*757e8328SLionel Sambuc }
744*757e8328SLionel Sambuc 
745*757e8328SLionel Sambuc static void
abort_hunk(void)746*757e8328SLionel Sambuc abort_hunk(void)
747*757e8328SLionel Sambuc {
748*757e8328SLionel Sambuc 	LINENUM		i, j, split;
749*757e8328SLionel Sambuc 	int		ch1, ch2;
750*757e8328SLionel Sambuc 	const LINENUM	pat_end = pch_end();
751*757e8328SLionel Sambuc 	const LINENUM	oldfirst = pch_first() + last_offset;
752*757e8328SLionel Sambuc 	const LINENUM	newfirst = pch_newfirst() + last_offset;
753*757e8328SLionel Sambuc 
754*757e8328SLionel Sambuc 	if (diff_type != UNI_DIFF) {
755*757e8328SLionel Sambuc 		abort_context_hunk();
756*757e8328SLionel Sambuc 		return;
757*757e8328SLionel Sambuc 	}
758*757e8328SLionel Sambuc 	split = -1;
759*757e8328SLionel Sambuc 	for (i = 0; i <= pat_end; i++) {
760*757e8328SLionel Sambuc 		if (pch_char(i) == '=') {
761*757e8328SLionel Sambuc 			split = i;
762*757e8328SLionel Sambuc 			break;
763*757e8328SLionel Sambuc 		}
764*757e8328SLionel Sambuc 	}
765*757e8328SLionel Sambuc 	if (split == -1) {
766*757e8328SLionel Sambuc 		fprintf(rejfp, "malformed hunk: no split found\n");
767*757e8328SLionel Sambuc 		return;
768*757e8328SLionel Sambuc 	}
769*757e8328SLionel Sambuc 	i = 0;
770*757e8328SLionel Sambuc 	j = split + 1;
771*757e8328SLionel Sambuc 	fprintf(rejfp, "@@ -%ld,%ld +%ld,%ld @@\n",
772*757e8328SLionel Sambuc 	    pch_ptrn_lines() ? oldfirst : 0,
773*757e8328SLionel Sambuc 	    pch_ptrn_lines(), newfirst, pch_repl_lines());
774*757e8328SLionel Sambuc 	while (i < split || j <= pat_end) {
775*757e8328SLionel Sambuc 		ch1 = i < split ? pch_char(i) : -1;
776*757e8328SLionel Sambuc 		ch2 = j <= pat_end ? pch_char(j) : -1;
777*757e8328SLionel Sambuc 		if (ch1 == '-') {
778*757e8328SLionel Sambuc 			rej_line('-', i);
779*757e8328SLionel Sambuc 			i++;
780*757e8328SLionel Sambuc 		} else if (ch1 == ' ' && ch2 == ' ') {
781*757e8328SLionel Sambuc 			rej_line(' ', i);
782*757e8328SLionel Sambuc 			i++;
783*757e8328SLionel Sambuc 			j++;
784*757e8328SLionel Sambuc 		} else if (ch1 == '!' && ch2 == '!') {
785*757e8328SLionel Sambuc 			while (i < split && ch1 == '!') {
786*757e8328SLionel Sambuc 				rej_line('-', i);
787*757e8328SLionel Sambuc 				i++;
788*757e8328SLionel Sambuc 				ch1 = i < split ? pch_char(i) : -1;
789*757e8328SLionel Sambuc 			}
790*757e8328SLionel Sambuc 			while (j <= pat_end && ch2 == '!') {
791*757e8328SLionel Sambuc 				rej_line('+', j);
792*757e8328SLionel Sambuc 				j++;
793*757e8328SLionel Sambuc 				ch2 = j <= pat_end ? pch_char(j) : -1;
794*757e8328SLionel Sambuc 			}
795*757e8328SLionel Sambuc 		} else if (ch1 == '*') {
796*757e8328SLionel Sambuc 			i++;
797*757e8328SLionel Sambuc 		} else if (ch2 == '+' || ch2 == ' ') {
798*757e8328SLionel Sambuc 			rej_line(ch2, j);
799*757e8328SLionel Sambuc 			j++;
800*757e8328SLionel Sambuc 		} else {
801*757e8328SLionel Sambuc 			fprintf(rejfp, "internal error on (%ld %ld %ld)\n",
802*757e8328SLionel Sambuc 			    i, split, j);
803*757e8328SLionel Sambuc 			rej_line(ch1, i);
804*757e8328SLionel Sambuc 			rej_line(ch2, j);
805*757e8328SLionel Sambuc 			return;
806*757e8328SLionel Sambuc 		}
807*757e8328SLionel Sambuc 	}
808*757e8328SLionel Sambuc }
809*757e8328SLionel Sambuc 
810*757e8328SLionel Sambuc /* We found where to apply it (we hope), so do it. */
811*757e8328SLionel Sambuc 
812*757e8328SLionel Sambuc static void
apply_hunk(LINENUM where)813*757e8328SLionel Sambuc apply_hunk(LINENUM where)
814*757e8328SLionel Sambuc {
815*757e8328SLionel Sambuc 	LINENUM		old = 1;
816*757e8328SLionel Sambuc 	const LINENUM	lastline = pch_ptrn_lines();
817*757e8328SLionel Sambuc 	LINENUM		new = lastline + 1;
818*757e8328SLionel Sambuc #define OUTSIDE 0
819*757e8328SLionel Sambuc #define IN_IFNDEF 1
820*757e8328SLionel Sambuc #define IN_IFDEF 2
821*757e8328SLionel Sambuc #define IN_ELSE 3
822*757e8328SLionel Sambuc 	int		def_state = OUTSIDE;
823*757e8328SLionel Sambuc 	const LINENUM	pat_end = pch_end();
824*757e8328SLionel Sambuc 
825*757e8328SLionel Sambuc 	where--;
826*757e8328SLionel Sambuc 	while (pch_char(new) == '=' || pch_char(new) == '\n')
827*757e8328SLionel Sambuc 		new++;
828*757e8328SLionel Sambuc 
829*757e8328SLionel Sambuc 	while (old <= lastline) {
830*757e8328SLionel Sambuc 		if (pch_char(old) == '-') {
831*757e8328SLionel Sambuc 			copy_till(where + old - 1, false);
832*757e8328SLionel Sambuc 			if (do_defines) {
833*757e8328SLionel Sambuc 				if (def_state == OUTSIDE) {
834*757e8328SLionel Sambuc 					fputs(not_defined, ofp);
835*757e8328SLionel Sambuc 					def_state = IN_IFNDEF;
836*757e8328SLionel Sambuc 				} else if (def_state == IN_IFDEF) {
837*757e8328SLionel Sambuc 					fputs(else_defined, ofp);
838*757e8328SLionel Sambuc 					def_state = IN_ELSE;
839*757e8328SLionel Sambuc 				}
840*757e8328SLionel Sambuc 				fputs(pfetch(old), ofp);
841*757e8328SLionel Sambuc 			}
842*757e8328SLionel Sambuc 			last_frozen_line++;
843*757e8328SLionel Sambuc 			old++;
844*757e8328SLionel Sambuc 		} else if (new > pat_end) {
845*757e8328SLionel Sambuc 			break;
846*757e8328SLionel Sambuc 		} else if (pch_char(new) == '+') {
847*757e8328SLionel Sambuc 			copy_till(where + old - 1, false);
848*757e8328SLionel Sambuc 			if (do_defines) {
849*757e8328SLionel Sambuc 				if (def_state == IN_IFNDEF) {
850*757e8328SLionel Sambuc 					fputs(else_defined, ofp);
851*757e8328SLionel Sambuc 					def_state = IN_ELSE;
852*757e8328SLionel Sambuc 				} else if (def_state == OUTSIDE) {
853*757e8328SLionel Sambuc 					fputs(if_defined, ofp);
854*757e8328SLionel Sambuc 					def_state = IN_IFDEF;
855*757e8328SLionel Sambuc 				}
856*757e8328SLionel Sambuc 			}
857*757e8328SLionel Sambuc 			fputs(pfetch(new), ofp);
858*757e8328SLionel Sambuc 			new++;
859*757e8328SLionel Sambuc 		} else if (pch_char(new) != pch_char(old)) {
860*757e8328SLionel Sambuc 			say("Out-of-sync patch, lines %ld,%ld--mangled text or line numbers, maybe?\n",
861*757e8328SLionel Sambuc 			    pch_hunk_beg() + old,
862*757e8328SLionel Sambuc 			    pch_hunk_beg() + new);
863*757e8328SLionel Sambuc #ifdef DEBUGGING
864*757e8328SLionel Sambuc 			say("oldchar = '%c', newchar = '%c'\n",
865*757e8328SLionel Sambuc 			    pch_char(old), pch_char(new));
866*757e8328SLionel Sambuc #endif
867*757e8328SLionel Sambuc 			my_exit(2);
868*757e8328SLionel Sambuc 		} else if (pch_char(new) == '!') {
869*757e8328SLionel Sambuc 			copy_till(where + old - 1, false);
870*757e8328SLionel Sambuc 			if (do_defines) {
871*757e8328SLionel Sambuc 				fputs(not_defined, ofp);
872*757e8328SLionel Sambuc 				def_state = IN_IFNDEF;
873*757e8328SLionel Sambuc 			}
874*757e8328SLionel Sambuc 			while (pch_char(old) == '!') {
875*757e8328SLionel Sambuc 				if (do_defines) {
876*757e8328SLionel Sambuc 					fputs(pfetch(old), ofp);
877*757e8328SLionel Sambuc 				}
878*757e8328SLionel Sambuc 				last_frozen_line++;
879*757e8328SLionel Sambuc 				old++;
880*757e8328SLionel Sambuc 			}
881*757e8328SLionel Sambuc 			if (do_defines) {
882*757e8328SLionel Sambuc 				fputs(else_defined, ofp);
883*757e8328SLionel Sambuc 				def_state = IN_ELSE;
884*757e8328SLionel Sambuc 			}
885*757e8328SLionel Sambuc 			while (pch_char(new) == '!') {
886*757e8328SLionel Sambuc 				fputs(pfetch(new), ofp);
887*757e8328SLionel Sambuc 				new++;
888*757e8328SLionel Sambuc 			}
889*757e8328SLionel Sambuc 		} else {
890*757e8328SLionel Sambuc 			if (pch_char(new) != ' ')
891*757e8328SLionel Sambuc 				fatal("Internal error: expected ' '\n");
892*757e8328SLionel Sambuc 			old++;
893*757e8328SLionel Sambuc 			new++;
894*757e8328SLionel Sambuc 			if (do_defines && def_state != OUTSIDE) {
895*757e8328SLionel Sambuc 				fputs(end_defined, ofp);
896*757e8328SLionel Sambuc 				def_state = OUTSIDE;
897*757e8328SLionel Sambuc 			}
898*757e8328SLionel Sambuc 		}
899*757e8328SLionel Sambuc 	}
900*757e8328SLionel Sambuc 	if (new <= pat_end && pch_char(new) == '+') {
901*757e8328SLionel Sambuc 		copy_till(where + old - 1, false);
902*757e8328SLionel Sambuc 		if (do_defines) {
903*757e8328SLionel Sambuc 			if (def_state == OUTSIDE) {
904*757e8328SLionel Sambuc 				fputs(if_defined, ofp);
905*757e8328SLionel Sambuc 				def_state = IN_IFDEF;
906*757e8328SLionel Sambuc 			} else if (def_state == IN_IFNDEF) {
907*757e8328SLionel Sambuc 				fputs(else_defined, ofp);
908*757e8328SLionel Sambuc 				def_state = IN_ELSE;
909*757e8328SLionel Sambuc 			}
910*757e8328SLionel Sambuc 		}
911*757e8328SLionel Sambuc 		while (new <= pat_end && pch_char(new) == '+') {
912*757e8328SLionel Sambuc 			fputs(pfetch(new), ofp);
913*757e8328SLionel Sambuc 			new++;
914*757e8328SLionel Sambuc 		}
915*757e8328SLionel Sambuc 	}
916*757e8328SLionel Sambuc 	if (do_defines && def_state != OUTSIDE) {
917*757e8328SLionel Sambuc 		fputs(end_defined, ofp);
918*757e8328SLionel Sambuc 	}
919*757e8328SLionel Sambuc }
920*757e8328SLionel Sambuc 
921*757e8328SLionel Sambuc /*
922*757e8328SLionel Sambuc  * Open the new file.
923*757e8328SLionel Sambuc  */
924*757e8328SLionel Sambuc static void
init_output(const char * name)925*757e8328SLionel Sambuc init_output(const char *name)
926*757e8328SLionel Sambuc {
927*757e8328SLionel Sambuc 	ofp = fopen(name, "w");
928*757e8328SLionel Sambuc 	if (ofp == NULL)
929*757e8328SLionel Sambuc 		pfatal("can't create %s", name);
930*757e8328SLionel Sambuc }
931*757e8328SLionel Sambuc 
932*757e8328SLionel Sambuc /*
933*757e8328SLionel Sambuc  * Open a file to put hunks we can't locate.
934*757e8328SLionel Sambuc  */
935*757e8328SLionel Sambuc static void
init_reject(const char * name)936*757e8328SLionel Sambuc init_reject(const char *name)
937*757e8328SLionel Sambuc {
938*757e8328SLionel Sambuc 	rejfp = fopen(name, "w");
939*757e8328SLionel Sambuc 	if (rejfp == NULL)
940*757e8328SLionel Sambuc 		pfatal("can't create %s", name);
941*757e8328SLionel Sambuc }
942*757e8328SLionel Sambuc 
943*757e8328SLionel Sambuc /*
944*757e8328SLionel Sambuc  * Copy input file to output, up to wherever hunk is to be applied.
945*757e8328SLionel Sambuc  * If endoffile is true, treat the last line specially since it may
946*757e8328SLionel Sambuc  * lack a newline.
947*757e8328SLionel Sambuc  */
948*757e8328SLionel Sambuc static void
copy_till(LINENUM lastline,bool endoffile)949*757e8328SLionel Sambuc copy_till(LINENUM lastline, bool endoffile)
950*757e8328SLionel Sambuc {
951*757e8328SLionel Sambuc 	if (last_frozen_line > lastline)
952*757e8328SLionel Sambuc 		fatal("misordered hunks! output would be garbled\n");
953*757e8328SLionel Sambuc 	while (last_frozen_line < lastline) {
954*757e8328SLionel Sambuc 		if (++last_frozen_line == lastline && endoffile)
955*757e8328SLionel Sambuc 			dump_line(last_frozen_line, !last_line_missing_eol);
956*757e8328SLionel Sambuc 		else
957*757e8328SLionel Sambuc 			dump_line(last_frozen_line, true);
958*757e8328SLionel Sambuc 	}
959*757e8328SLionel Sambuc }
960*757e8328SLionel Sambuc 
961*757e8328SLionel Sambuc /*
962*757e8328SLionel Sambuc  * Finish copying the input file to the output file.
963*757e8328SLionel Sambuc  */
964*757e8328SLionel Sambuc static bool
spew_output(void)965*757e8328SLionel Sambuc spew_output(void)
966*757e8328SLionel Sambuc {
967*757e8328SLionel Sambuc 	int rv;
968*757e8328SLionel Sambuc 
969*757e8328SLionel Sambuc #ifdef DEBUGGING
970*757e8328SLionel Sambuc 	if (debug & 256)
971*757e8328SLionel Sambuc 		say("il=%ld lfl=%ld\n", input_lines, last_frozen_line);
972*757e8328SLionel Sambuc #endif
973*757e8328SLionel Sambuc 	if (input_lines)
974*757e8328SLionel Sambuc 		copy_till(input_lines, true);	/* dump remainder of file */
975*757e8328SLionel Sambuc 	rv = ferror(ofp) == 0 && fclose(ofp) == 0;
976*757e8328SLionel Sambuc 	ofp = NULL;
977*757e8328SLionel Sambuc 	return rv;
978*757e8328SLionel Sambuc }
979*757e8328SLionel Sambuc 
980*757e8328SLionel Sambuc /*
981*757e8328SLionel Sambuc  * Copy one line from input to output.
982*757e8328SLionel Sambuc  */
983*757e8328SLionel Sambuc static void
dump_line(LINENUM line,bool write_newline)984*757e8328SLionel Sambuc dump_line(LINENUM line, bool write_newline)
985*757e8328SLionel Sambuc {
986*757e8328SLionel Sambuc 	char	*s;
987*757e8328SLionel Sambuc 
988*757e8328SLionel Sambuc 	s = ifetch(line, 0);
989*757e8328SLionel Sambuc 	if (s == NULL)
990*757e8328SLionel Sambuc 		return;
991*757e8328SLionel Sambuc 	/* Note: string is not NUL terminated. */
992*757e8328SLionel Sambuc 	for (; *s != '\n'; s++)
993*757e8328SLionel Sambuc 		putc(*s, ofp);
994*757e8328SLionel Sambuc 	if (write_newline)
995*757e8328SLionel Sambuc 		putc('\n', ofp);
996*757e8328SLionel Sambuc }
997*757e8328SLionel Sambuc 
998*757e8328SLionel Sambuc /*
999*757e8328SLionel Sambuc  * Does the patch pattern match at line base+offset?
1000*757e8328SLionel Sambuc  */
1001*757e8328SLionel Sambuc static bool
patch_match(LINENUM base,LINENUM offset,LINENUM fuzz)1002*757e8328SLionel Sambuc patch_match(LINENUM base, LINENUM offset, LINENUM fuzz)
1003*757e8328SLionel Sambuc {
1004*757e8328SLionel Sambuc 	LINENUM		pline = 1 + fuzz;
1005*757e8328SLionel Sambuc 	LINENUM		iline;
1006*757e8328SLionel Sambuc 	LINENUM		pat_lines = pch_ptrn_lines() - fuzz;
1007*757e8328SLionel Sambuc 	const char	*ilineptr;
1008*757e8328SLionel Sambuc 	const char	*plineptr;
1009*757e8328SLionel Sambuc 	short		plinelen;
1010*757e8328SLionel Sambuc 
1011*757e8328SLionel Sambuc 	for (iline = base + offset + fuzz; pline <= pat_lines; pline++, iline++) {
1012*757e8328SLionel Sambuc 		ilineptr = ifetch(iline, offset >= 0);
1013*757e8328SLionel Sambuc 		if (ilineptr == NULL)
1014*757e8328SLionel Sambuc 			return false;
1015*757e8328SLionel Sambuc 		plineptr = pfetch(pline);
1016*757e8328SLionel Sambuc 		plinelen = pch_line_len(pline);
1017*757e8328SLionel Sambuc 		if (canonicalize) {
1018*757e8328SLionel Sambuc 			if (!similar(ilineptr, plineptr, plinelen))
1019*757e8328SLionel Sambuc 				return false;
1020*757e8328SLionel Sambuc 		} else if (strnNE(ilineptr, plineptr, plinelen))
1021*757e8328SLionel Sambuc 			return false;
1022*757e8328SLionel Sambuc 		if (iline == input_lines) {
1023*757e8328SLionel Sambuc 			/*
1024*757e8328SLionel Sambuc 			 * We are looking at the last line of the file.
1025*757e8328SLionel Sambuc 			 * If the file has no eol, the patch line should
1026*757e8328SLionel Sambuc 			 * not have one either and vice-versa. Note that
1027*757e8328SLionel Sambuc 			 * plinelen > 0.
1028*757e8328SLionel Sambuc 			 */
1029*757e8328SLionel Sambuc 			if (last_line_missing_eol) {
1030*757e8328SLionel Sambuc 				if (plineptr[plinelen - 1] == '\n')
1031*757e8328SLionel Sambuc 					return false;
1032*757e8328SLionel Sambuc 			} else {
1033*757e8328SLionel Sambuc 				if (plineptr[plinelen - 1] != '\n')
1034*757e8328SLionel Sambuc 					return false;
1035*757e8328SLionel Sambuc 			}
1036*757e8328SLionel Sambuc 		}
1037*757e8328SLionel Sambuc 	}
1038*757e8328SLionel Sambuc 	return true;
1039*757e8328SLionel Sambuc }
1040*757e8328SLionel Sambuc 
1041*757e8328SLionel Sambuc /*
1042*757e8328SLionel Sambuc  * Do two lines match with canonicalized white space?
1043*757e8328SLionel Sambuc  */
1044*757e8328SLionel Sambuc static bool
similar(const char * a,const char * b,int len)1045*757e8328SLionel Sambuc similar(const char *a, const char *b, int len)
1046*757e8328SLionel Sambuc {
1047*757e8328SLionel Sambuc 	while (len) {
1048*757e8328SLionel Sambuc 		if (isspace((unsigned char)*b)) {	/* whitespace (or \n) to match? */
1049*757e8328SLionel Sambuc 			if (!isspace((unsigned char)*a))	/* no corresponding whitespace? */
1050*757e8328SLionel Sambuc 				return false;
1051*757e8328SLionel Sambuc 			while (len && isspace((unsigned char)*b) && *b != '\n')
1052*757e8328SLionel Sambuc 				b++, len--;	/* skip pattern whitespace */
1053*757e8328SLionel Sambuc 			while (isspace((unsigned char)*a) && *a != '\n')
1054*757e8328SLionel Sambuc 				a++;	/* skip target whitespace */
1055*757e8328SLionel Sambuc 			if (*a == '\n' || *b == '\n')
1056*757e8328SLionel Sambuc 				return (*a == *b);	/* should end in sync */
1057*757e8328SLionel Sambuc 		} else if (*a++ != *b++)	/* match non-whitespace chars */
1058*757e8328SLionel Sambuc 			return false;
1059*757e8328SLionel Sambuc 		else
1060*757e8328SLionel Sambuc 			len--;	/* probably not necessary */
1061*757e8328SLionel Sambuc 	}
1062*757e8328SLionel Sambuc 	return true;		/* actually, this is not reached */
1063*757e8328SLionel Sambuc 	/* since there is always a \n */
1064*757e8328SLionel Sambuc }
1065