xref: /dflybsd-src/libexec/diff3prog/diff3prog.c (revision c9733229451fac5faa53b1a016b01866eae75a1c)
1*c9733229SMatthew Dillon /*	$OpenBSD: diff3prog.c,v 1.21 2021/04/13 14:20:23 stsp Exp $	*/
2*c9733229SMatthew Dillon 
3*c9733229SMatthew Dillon /*
4*c9733229SMatthew Dillon  * Copyright (C) Caldera International Inc.  2001-2002.
5*c9733229SMatthew Dillon  * All rights reserved.
6*c9733229SMatthew Dillon  *
7*c9733229SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8*c9733229SMatthew Dillon  * modification, are permitted provided that the following conditions
9*c9733229SMatthew Dillon  * are met:
10*c9733229SMatthew Dillon  * 1. Redistributions of source code and documentation must retain the above
11*c9733229SMatthew Dillon  *    copyright notice, this list of conditions and the following disclaimer.
12*c9733229SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
13*c9733229SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
14*c9733229SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
15*c9733229SMatthew Dillon  * 3. All advertising materials mentioning features or use of this software
16*c9733229SMatthew Dillon  *    must display the following acknowledgement:
17*c9733229SMatthew Dillon  *	This product includes software developed or owned by Caldera
18*c9733229SMatthew Dillon  *	International, Inc.
19*c9733229SMatthew Dillon  * 4. Neither the name of Caldera International, Inc. nor the names of other
20*c9733229SMatthew Dillon  *    contributors may be used to endorse or promote products derived from
21*c9733229SMatthew Dillon  *    this software without specific prior written permission.
22*c9733229SMatthew Dillon  *
23*c9733229SMatthew Dillon  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24*c9733229SMatthew Dillon  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25*c9733229SMatthew Dillon  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26*c9733229SMatthew Dillon  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27*c9733229SMatthew Dillon  * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28*c9733229SMatthew Dillon  * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29*c9733229SMatthew Dillon  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30*c9733229SMatthew Dillon  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31*c9733229SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32*c9733229SMatthew Dillon  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33*c9733229SMatthew Dillon  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34*c9733229SMatthew Dillon  * POSSIBILITY OF SUCH DAMAGE.
35*c9733229SMatthew Dillon  */
36*c9733229SMatthew Dillon /*-
37*c9733229SMatthew Dillon  * Copyright (c) 1991, 1993
38*c9733229SMatthew Dillon  *	The Regents of the University of California.  All rights reserved.
39*c9733229SMatthew Dillon  *
40*c9733229SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
41*c9733229SMatthew Dillon  * modification, are permitted provided that the following conditions
42*c9733229SMatthew Dillon  * are met:
43*c9733229SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
44*c9733229SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
45*c9733229SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
46*c9733229SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
47*c9733229SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
48*c9733229SMatthew Dillon  * 3. Neither the name of the University nor the names of its contributors
49*c9733229SMatthew Dillon  *    may be used to endorse or promote products derived from this software
50*c9733229SMatthew Dillon  *    without specific prior written permission.
51*c9733229SMatthew Dillon  *
52*c9733229SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53*c9733229SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54*c9733229SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55*c9733229SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56*c9733229SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57*c9733229SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58*c9733229SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59*c9733229SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60*c9733229SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61*c9733229SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62*c9733229SMatthew Dillon  * SUCH DAMAGE.
63*c9733229SMatthew Dillon  *
64*c9733229SMatthew Dillon  *	@(#)diff3.c	8.1 (Berkeley) 6/6/93
65*c9733229SMatthew Dillon  */
66*c9733229SMatthew Dillon 
67*c9733229SMatthew Dillon #include <ctype.h>
68*c9733229SMatthew Dillon #include <err.h>
69*c9733229SMatthew Dillon #include <stdio.h>
70*c9733229SMatthew Dillon #include <stdlib.h>
71*c9733229SMatthew Dillon #include <string.h>
72*c9733229SMatthew Dillon #include <unistd.h>
73*c9733229SMatthew Dillon 
74*c9733229SMatthew Dillon /* diff3 - 3-way differential file comparison */
75*c9733229SMatthew Dillon 
76*c9733229SMatthew Dillon /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
77*c9733229SMatthew Dillon  *
78*c9733229SMatthew Dillon  * d13 = diff report on f1 vs f3
79*c9733229SMatthew Dillon  * d23 = diff report on f2 vs f3
80*c9733229SMatthew Dillon  * f1, f2, f3 the 3 files
81*c9733229SMatthew Dillon  * if changes in f1 overlap with changes in f3, m1 and m3 are used
82*c9733229SMatthew Dillon  * to mark the overlaps; otherwise, the file names f1 and f3 are used
83*c9733229SMatthew Dillon  * (only for options E and X).
84*c9733229SMatthew Dillon  */
85*c9733229SMatthew Dillon 
86*c9733229SMatthew Dillon /*
87*c9733229SMatthew Dillon  * "from" is first in range of changed lines; "to" is last+1
88*c9733229SMatthew Dillon  * from=to=line after point of insertion for added lines.
89*c9733229SMatthew Dillon  */
90*c9733229SMatthew Dillon struct  range {
91*c9733229SMatthew Dillon 	int from;
92*c9733229SMatthew Dillon 	int to;
93*c9733229SMatthew Dillon };
94*c9733229SMatthew Dillon struct diff {
95*c9733229SMatthew Dillon 	struct range old;
96*c9733229SMatthew Dillon 	struct range new;
97*c9733229SMatthew Dillon };
98*c9733229SMatthew Dillon 
99*c9733229SMatthew Dillon size_t szchanges;
100*c9733229SMatthew Dillon 
101*c9733229SMatthew Dillon struct diff *d13;
102*c9733229SMatthew Dillon struct diff *d23;
103*c9733229SMatthew Dillon /*
104*c9733229SMatthew Dillon  * "de" is used to gather editing scripts.  These are later spewed out in
105*c9733229SMatthew Dillon  * reverse order.  Its first element must be all zero, the "new" component
106*c9733229SMatthew Dillon  * of "de" contains line positions or byte positions depending on when you
107*c9733229SMatthew Dillon  * look (!?).  Array overlap indicates which sections in "de" correspond to
108*c9733229SMatthew Dillon  * lines that are different in all three files.
109*c9733229SMatthew Dillon  */
110*c9733229SMatthew Dillon struct diff *de;
111*c9733229SMatthew Dillon char *overlap;
112*c9733229SMatthew Dillon int  overlapcnt;
113*c9733229SMatthew Dillon FILE *fp[3];
114*c9733229SMatthew Dillon int cline[3];		/* # of the last-read line in each file (0-2) */
115*c9733229SMatthew Dillon /*
116*c9733229SMatthew Dillon  * the latest known correspondence between line numbers of the 3 files
117*c9733229SMatthew Dillon  * is stored in last[1-3];
118*c9733229SMatthew Dillon  */
119*c9733229SMatthew Dillon int last[4];
120*c9733229SMatthew Dillon int eflag;
121*c9733229SMatthew Dillon int oflag;		/* indicates whether to mark overlaps (-E or -X)*/
122*c9733229SMatthew Dillon int debug  = 0;
123*c9733229SMatthew Dillon char f1mark[40], f3mark[40];	/* markers for -E and -X */
124*c9733229SMatthew Dillon 
125*c9733229SMatthew Dillon int duplicate(struct range *, struct range *);
126*c9733229SMatthew Dillon int edit(struct diff *, int, int);
127*c9733229SMatthew Dillon char *getchange(FILE *);
128*c9733229SMatthew Dillon char *get_line(FILE *, size_t *);
129*c9733229SMatthew Dillon int number(char **);
130*c9733229SMatthew Dillon int readin(char *, struct diff **);
131*c9733229SMatthew Dillon int skip(int, int, const char *);
132*c9733229SMatthew Dillon void change(int, struct range *, int);
133*c9733229SMatthew Dillon void keep(int, struct range *);
134*c9733229SMatthew Dillon void merge(int, int);
135*c9733229SMatthew Dillon void prange(struct range *);
136*c9733229SMatthew Dillon void repos(int);
137*c9733229SMatthew Dillon void separate(const char *);
138*c9733229SMatthew Dillon void edscript(int);
139*c9733229SMatthew Dillon void trouble(void);
140*c9733229SMatthew Dillon void increase(void);
141*c9733229SMatthew Dillon void usage(void);
142*c9733229SMatthew Dillon 
143*c9733229SMatthew Dillon int
main(int argc,char ** argv)144*c9733229SMatthew Dillon main(int argc, char **argv)
145*c9733229SMatthew Dillon {
146*c9733229SMatthew Dillon 	int ch, i, m, n;
147*c9733229SMatthew Dillon 
148*c9733229SMatthew Dillon 	eflag = 0;
149*c9733229SMatthew Dillon 	oflag = 0;
150*c9733229SMatthew Dillon 	while ((ch = getopt(argc, argv, "EeXx3")) != -1) {
151*c9733229SMatthew Dillon 		switch (ch) {
152*c9733229SMatthew Dillon 		case 'E':
153*c9733229SMatthew Dillon 			eflag = 3;
154*c9733229SMatthew Dillon 			oflag = 1;
155*c9733229SMatthew Dillon 			break;
156*c9733229SMatthew Dillon 		case 'e':
157*c9733229SMatthew Dillon 			eflag = 3;
158*c9733229SMatthew Dillon 			break;
159*c9733229SMatthew Dillon 		case 'X':
160*c9733229SMatthew Dillon 			oflag = eflag = 1;
161*c9733229SMatthew Dillon 			break;
162*c9733229SMatthew Dillon 		case 'x':
163*c9733229SMatthew Dillon 			eflag = 1;
164*c9733229SMatthew Dillon 			break;
165*c9733229SMatthew Dillon 		case '3':
166*c9733229SMatthew Dillon 			eflag = 2;
167*c9733229SMatthew Dillon 			break;
168*c9733229SMatthew Dillon 		}
169*c9733229SMatthew Dillon 	}
170*c9733229SMatthew Dillon 	argc -= optind;
171*c9733229SMatthew Dillon 	argv += optind;
172*c9733229SMatthew Dillon 	/* XXX - argc usage seems wrong here */
173*c9733229SMatthew Dillon 	if (argc < 5)
174*c9733229SMatthew Dillon 		usage();
175*c9733229SMatthew Dillon 
176*c9733229SMatthew Dillon 	if (oflag) {
177*c9733229SMatthew Dillon 		(void)snprintf(f1mark, sizeof(f1mark), "<<<<<<< %s",
178*c9733229SMatthew Dillon 		    argc >= 6 ? argv[5] : argv[2]);
179*c9733229SMatthew Dillon 		(void)snprintf(f3mark, sizeof(f3mark), ">>>>>>> %s",
180*c9733229SMatthew Dillon 		    argc >= 7 ? argv[6] : argv[4]);
181*c9733229SMatthew Dillon 	}
182*c9733229SMatthew Dillon 
183*c9733229SMatthew Dillon 	increase();
184*c9733229SMatthew Dillon 	m = readin(argv[0], &d13);
185*c9733229SMatthew Dillon 	n = readin(argv[1], &d23);
186*c9733229SMatthew Dillon 	for (i = 0; i <= 2; i++) {
187*c9733229SMatthew Dillon 		if ((fp[i] = fopen(argv[i + 2], "r")) == NULL)
188*c9733229SMatthew Dillon 			err(EXIT_FAILURE, "can't open %s", argv[i + 2]);
189*c9733229SMatthew Dillon 	}
190*c9733229SMatthew Dillon 	merge(m, n);
191*c9733229SMatthew Dillon 	exit(EXIT_SUCCESS);
192*c9733229SMatthew Dillon }
193*c9733229SMatthew Dillon 
194*c9733229SMatthew Dillon /*
195*c9733229SMatthew Dillon  * Pick up the line numbers of all changes from one change file.
196*c9733229SMatthew Dillon  * (This puts the numbers in a vector, which is not strictly necessary,
197*c9733229SMatthew Dillon  * since the vector is processed in one sequential pass.
198*c9733229SMatthew Dillon  * The vector could be optimized out of existence)
199*c9733229SMatthew Dillon  */
200*c9733229SMatthew Dillon int
readin(char * name,struct diff ** dd)201*c9733229SMatthew Dillon readin(char *name, struct diff **dd)
202*c9733229SMatthew Dillon {
203*c9733229SMatthew Dillon 	int a, b, c, d;
204*c9733229SMatthew Dillon     size_t i;
205*c9733229SMatthew Dillon 	char kind, *p;
206*c9733229SMatthew Dillon 
207*c9733229SMatthew Dillon 	fp[0] = fopen(name, "r");
208*c9733229SMatthew Dillon 	if (fp[0] == NULL)
209*c9733229SMatthew Dillon 		err(EXIT_FAILURE, "can't open %s", name);
210*c9733229SMatthew Dillon 	for (i=0; (p = getchange(fp[0])); i++) {
211*c9733229SMatthew Dillon 		if (i >= szchanges - 1)
212*c9733229SMatthew Dillon 			increase();
213*c9733229SMatthew Dillon 		a = b = number(&p);
214*c9733229SMatthew Dillon 		if (*p == ',') {
215*c9733229SMatthew Dillon 			p++;
216*c9733229SMatthew Dillon 			b = number(&p);
217*c9733229SMatthew Dillon 		}
218*c9733229SMatthew Dillon 		kind = *p++;
219*c9733229SMatthew Dillon 		c = d = number(&p);
220*c9733229SMatthew Dillon 		if (*p==',') {
221*c9733229SMatthew Dillon 			p++;
222*c9733229SMatthew Dillon 			d = number(&p);
223*c9733229SMatthew Dillon 		}
224*c9733229SMatthew Dillon 		if (kind == 'a')
225*c9733229SMatthew Dillon 			a++;
226*c9733229SMatthew Dillon 		if (kind == 'd')
227*c9733229SMatthew Dillon 			c++;
228*c9733229SMatthew Dillon 		b++;
229*c9733229SMatthew Dillon 		d++;
230*c9733229SMatthew Dillon 		(*dd)[i].old.from = a;
231*c9733229SMatthew Dillon 		(*dd)[i].old.to = b;
232*c9733229SMatthew Dillon 		(*dd)[i].new.from = c;
233*c9733229SMatthew Dillon 		(*dd)[i].new.to = d;
234*c9733229SMatthew Dillon 	}
235*c9733229SMatthew Dillon 	if (i) {
236*c9733229SMatthew Dillon 		(*dd)[i].old.from = (*dd)[i-1].old.to;
237*c9733229SMatthew Dillon 		(*dd)[i].new.from = (*dd)[i-1].new.to;
238*c9733229SMatthew Dillon 	}
239*c9733229SMatthew Dillon 	(void)fclose(fp[0]);
240*c9733229SMatthew Dillon 	return (i);
241*c9733229SMatthew Dillon }
242*c9733229SMatthew Dillon 
243*c9733229SMatthew Dillon int
number(char ** lc)244*c9733229SMatthew Dillon number(char **lc)
245*c9733229SMatthew Dillon {
246*c9733229SMatthew Dillon 	int nn;
247*c9733229SMatthew Dillon 	nn = 0;
248*c9733229SMatthew Dillon 	while (isdigit((unsigned char)(**lc)))
249*c9733229SMatthew Dillon 		nn = nn*10 + *(*lc)++ - '0';
250*c9733229SMatthew Dillon 	return (nn);
251*c9733229SMatthew Dillon }
252*c9733229SMatthew Dillon 
253*c9733229SMatthew Dillon char *
getchange(FILE * b)254*c9733229SMatthew Dillon getchange(FILE *b)
255*c9733229SMatthew Dillon {
256*c9733229SMatthew Dillon 	char *line;
257*c9733229SMatthew Dillon 
258*c9733229SMatthew Dillon 	while ((line = get_line(b, NULL))) {
259*c9733229SMatthew Dillon 		if (isdigit((unsigned char)line[0]))
260*c9733229SMatthew Dillon 			return (line);
261*c9733229SMatthew Dillon 	}
262*c9733229SMatthew Dillon 	return (NULL);
263*c9733229SMatthew Dillon }
264*c9733229SMatthew Dillon 
265*c9733229SMatthew Dillon char *
get_line(FILE * b,size_t * n)266*c9733229SMatthew Dillon get_line(FILE *b, size_t *n)
267*c9733229SMatthew Dillon {
268*c9733229SMatthew Dillon 	char *cp;
269*c9733229SMatthew Dillon 	size_t len;
270*c9733229SMatthew Dillon 	static char *buf;
271*c9733229SMatthew Dillon 	static size_t bufsize;
272*c9733229SMatthew Dillon 
273*c9733229SMatthew Dillon 	if ((cp = fgetln(b, &len)) == NULL)
274*c9733229SMatthew Dillon 		return (NULL);
275*c9733229SMatthew Dillon 
276*c9733229SMatthew Dillon 	if (cp[len - 1] != '\n')
277*c9733229SMatthew Dillon 		len++;
278*c9733229SMatthew Dillon 	if (len + 1 > bufsize) {
279*c9733229SMatthew Dillon 		do {
280*c9733229SMatthew Dillon 			bufsize += 1024;
281*c9733229SMatthew Dillon 		} while (len + 1 > bufsize);
282*c9733229SMatthew Dillon 		if ((buf = realloc(buf, bufsize)) == NULL)
283*c9733229SMatthew Dillon 			err(EXIT_FAILURE, NULL);
284*c9733229SMatthew Dillon 	}
285*c9733229SMatthew Dillon 	memcpy(buf, cp, len - 1);
286*c9733229SMatthew Dillon 	buf[len - 1] = '\n';
287*c9733229SMatthew Dillon 	buf[len] = '\0';
288*c9733229SMatthew Dillon 	if (n != NULL)
289*c9733229SMatthew Dillon 		*n = len;
290*c9733229SMatthew Dillon 	return (buf);
291*c9733229SMatthew Dillon }
292*c9733229SMatthew Dillon 
293*c9733229SMatthew Dillon void
merge(int m1,int m2)294*c9733229SMatthew Dillon merge(int m1, int m2)
295*c9733229SMatthew Dillon {
296*c9733229SMatthew Dillon 	struct diff *d1, *d2, *d3;
297*c9733229SMatthew Dillon 	int dup, j, t1, t2;
298*c9733229SMatthew Dillon 
299*c9733229SMatthew Dillon 	d1 = d13;
300*c9733229SMatthew Dillon 	d2 = d23;
301*c9733229SMatthew Dillon 	j = 0;
302*c9733229SMatthew Dillon 	for (;;) {
303*c9733229SMatthew Dillon 		t1 = (d1 < d13 + m1);
304*c9733229SMatthew Dillon 		t2 = (d2 < d23 + m2);
305*c9733229SMatthew Dillon 		if (!t1 && !t2)
306*c9733229SMatthew Dillon 			break;
307*c9733229SMatthew Dillon 
308*c9733229SMatthew Dillon 		if (debug) {
309*c9733229SMatthew Dillon 			printf("%d,%d=%d,%d %d,%d=%d,%d\n",
310*c9733229SMatthew Dillon 			d1->old.from,d1->old.to,
311*c9733229SMatthew Dillon 			d1->new.from,d1->new.to,
312*c9733229SMatthew Dillon 			d2->old.from,d2->old.to,
313*c9733229SMatthew Dillon 			d2->new.from,d2->new.to);
314*c9733229SMatthew Dillon 		}
315*c9733229SMatthew Dillon 		/* first file is different from others */
316*c9733229SMatthew Dillon 		if (!t2 || (t1 && d1->new.to < d2->new.from)) {
317*c9733229SMatthew Dillon 			/* stuff peculiar to 1st file */
318*c9733229SMatthew Dillon 			if (eflag==0) {
319*c9733229SMatthew Dillon 				separate("1");
320*c9733229SMatthew Dillon 				change(1, &d1->old, 0);
321*c9733229SMatthew Dillon 				keep(2, &d1->new);
322*c9733229SMatthew Dillon 				change(3, &d1->new, 0);
323*c9733229SMatthew Dillon 			}
324*c9733229SMatthew Dillon 			d1++;
325*c9733229SMatthew Dillon 			continue;
326*c9733229SMatthew Dillon 		}
327*c9733229SMatthew Dillon 		/* second file is different from others */
328*c9733229SMatthew Dillon 		if (!t1 || (t2 && d2->new.to < d1->new.from)) {
329*c9733229SMatthew Dillon 			if (eflag==0) {
330*c9733229SMatthew Dillon 				separate("2");
331*c9733229SMatthew Dillon 				keep(1, &d2->new);
332*c9733229SMatthew Dillon 				change(2, &d2->old, 0);
333*c9733229SMatthew Dillon 				change(3, &d2->new, 0);
334*c9733229SMatthew Dillon 			}
335*c9733229SMatthew Dillon 			d2++;
336*c9733229SMatthew Dillon 			continue;
337*c9733229SMatthew Dillon 		}
338*c9733229SMatthew Dillon 		/*
339*c9733229SMatthew Dillon 		 * Merge overlapping changes in first file
340*c9733229SMatthew Dillon 		 * this happens after extension (see below).
341*c9733229SMatthew Dillon 		 */
342*c9733229SMatthew Dillon 		if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
343*c9733229SMatthew Dillon 			d1[1].old.from = d1->old.from;
344*c9733229SMatthew Dillon 			d1[1].new.from = d1->new.from;
345*c9733229SMatthew Dillon 			d1++;
346*c9733229SMatthew Dillon 			continue;
347*c9733229SMatthew Dillon 		}
348*c9733229SMatthew Dillon 
349*c9733229SMatthew Dillon 		/* merge overlapping changes in second */
350*c9733229SMatthew Dillon 		if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
351*c9733229SMatthew Dillon 			d2[1].old.from = d2->old.from;
352*c9733229SMatthew Dillon 			d2[1].new.from = d2->new.from;
353*c9733229SMatthew Dillon 			d2++;
354*c9733229SMatthew Dillon 			continue;
355*c9733229SMatthew Dillon 		}
356*c9733229SMatthew Dillon 		/* stuff peculiar to third file or different in all */
357*c9733229SMatthew Dillon 		if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
358*c9733229SMatthew Dillon 			dup = duplicate(&d1->old,&d2->old);
359*c9733229SMatthew Dillon 			/*
360*c9733229SMatthew Dillon 			 * dup = 0 means all files differ
361*c9733229SMatthew Dillon 			 * dup = 1 means files 1 and 2 identical
362*c9733229SMatthew Dillon 			 */
363*c9733229SMatthew Dillon 			if (eflag==0) {
364*c9733229SMatthew Dillon 				separate(dup ? "3" : "");
365*c9733229SMatthew Dillon 				change(1, &d1->old, dup);
366*c9733229SMatthew Dillon 				change(2, &d2->old, 0);
367*c9733229SMatthew Dillon 				d3 = d1->old.to > d1->old.from ? d1 : d2;
368*c9733229SMatthew Dillon 				change(3, &d3->new, 0);
369*c9733229SMatthew Dillon 			} else
370*c9733229SMatthew Dillon 				j = edit(d1, dup, j);
371*c9733229SMatthew Dillon 			d1++;
372*c9733229SMatthew Dillon 			d2++;
373*c9733229SMatthew Dillon 			continue;
374*c9733229SMatthew Dillon 		}
375*c9733229SMatthew Dillon 		/*
376*c9733229SMatthew Dillon 		 * Overlapping changes from file 1 and 2; extend changes
377*c9733229SMatthew Dillon 		 * appropriately to make them coincide.
378*c9733229SMatthew Dillon 		 */
379*c9733229SMatthew Dillon 		if (d1->new.from < d2->new.from) {
380*c9733229SMatthew Dillon 			d2->old.from -= d2->new.from-d1->new.from;
381*c9733229SMatthew Dillon 			d2->new.from = d1->new.from;
382*c9733229SMatthew Dillon 		} else if (d2->new.from < d1->new.from) {
383*c9733229SMatthew Dillon 			d1->old.from -= d1->new.from-d2->new.from;
384*c9733229SMatthew Dillon 			d1->new.from = d2->new.from;
385*c9733229SMatthew Dillon 		}
386*c9733229SMatthew Dillon 		if (d1->new.to > d2->new.to) {
387*c9733229SMatthew Dillon 			d2->old.to += d1->new.to - d2->new.to;
388*c9733229SMatthew Dillon 			d2->new.to = d1->new.to;
389*c9733229SMatthew Dillon 		} else if (d2->new.to > d1->new.to) {
390*c9733229SMatthew Dillon 			d1->old.to += d2->new.to - d1->new.to;
391*c9733229SMatthew Dillon 			d1->new.to = d2->new.to;
392*c9733229SMatthew Dillon 		}
393*c9733229SMatthew Dillon 	}
394*c9733229SMatthew Dillon 	if (eflag)
395*c9733229SMatthew Dillon 		edscript(j);
396*c9733229SMatthew Dillon }
397*c9733229SMatthew Dillon 
398*c9733229SMatthew Dillon void
separate(const char * s)399*c9733229SMatthew Dillon separate(const char *s)
400*c9733229SMatthew Dillon {
401*c9733229SMatthew Dillon 	printf("====%s\n", s);
402*c9733229SMatthew Dillon }
403*c9733229SMatthew Dillon 
404*c9733229SMatthew Dillon /*
405*c9733229SMatthew Dillon  * The range of lines rold.from thru rold.to in file i is to be changed.
406*c9733229SMatthew Dillon  * It is to be printed only if it does not duplicate something to be
407*c9733229SMatthew Dillon  * printed later.
408*c9733229SMatthew Dillon  */
409*c9733229SMatthew Dillon void
change(int i,struct range * rold,int dup)410*c9733229SMatthew Dillon change(int i, struct range *rold, int dup)
411*c9733229SMatthew Dillon {
412*c9733229SMatthew Dillon 	printf("%d:", i);
413*c9733229SMatthew Dillon 	last[i] = rold->to;
414*c9733229SMatthew Dillon 	prange(rold);
415*c9733229SMatthew Dillon 	if (dup || debug)
416*c9733229SMatthew Dillon 		return;
417*c9733229SMatthew Dillon 	i--;
418*c9733229SMatthew Dillon 	(void)skip(i, rold->from, NULL);
419*c9733229SMatthew Dillon 	(void)skip(i, rold->to, "  ");
420*c9733229SMatthew Dillon }
421*c9733229SMatthew Dillon 
422*c9733229SMatthew Dillon /*
423*c9733229SMatthew Dillon  * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
424*c9733229SMatthew Dillon  */
425*c9733229SMatthew Dillon void
prange(struct range * rold)426*c9733229SMatthew Dillon prange(struct range *rold)
427*c9733229SMatthew Dillon {
428*c9733229SMatthew Dillon 	if (rold->to <= rold->from)
429*c9733229SMatthew Dillon 		printf("%da\n", rold->from - 1);
430*c9733229SMatthew Dillon 	else {
431*c9733229SMatthew Dillon 		printf("%d", rold->from);
432*c9733229SMatthew Dillon 		if (rold->to > rold->from+1)
433*c9733229SMatthew Dillon 			printf(",%d", rold->to - 1);
434*c9733229SMatthew Dillon 		printf("c\n");
435*c9733229SMatthew Dillon 	}
436*c9733229SMatthew Dillon }
437*c9733229SMatthew Dillon 
438*c9733229SMatthew Dillon /*
439*c9733229SMatthew Dillon  * No difference was reported by diff between file 1 (or 2) and file 3,
440*c9733229SMatthew Dillon  * and an artificial dummy difference (trange) must be ginned up to
441*c9733229SMatthew Dillon  * correspond to the change reported in the other file.
442*c9733229SMatthew Dillon  */
443*c9733229SMatthew Dillon void
keep(int i,struct range * rnew)444*c9733229SMatthew Dillon keep(int i, struct range *rnew)
445*c9733229SMatthew Dillon {
446*c9733229SMatthew Dillon 	int delta;
447*c9733229SMatthew Dillon 	struct range trange;
448*c9733229SMatthew Dillon 
449*c9733229SMatthew Dillon 	delta = last[3] - last[i];
450*c9733229SMatthew Dillon 	trange.from = rnew->from - delta;
451*c9733229SMatthew Dillon 	trange.to = rnew->to - delta;
452*c9733229SMatthew Dillon 	change(i, &trange, 1);
453*c9733229SMatthew Dillon }
454*c9733229SMatthew Dillon 
455*c9733229SMatthew Dillon /*
456*c9733229SMatthew Dillon  * skip to just before line number from in file "i".  If "pr" is non-NULL,
457*c9733229SMatthew Dillon  * print all skipped stuff with string pr as a prefix.
458*c9733229SMatthew Dillon  */
459*c9733229SMatthew Dillon int
skip(int i,int from,const char * pr)460*c9733229SMatthew Dillon skip(int i, int from, const char *pr)
461*c9733229SMatthew Dillon {
462*c9733229SMatthew Dillon 	size_t j, n;
463*c9733229SMatthew Dillon 	char *line;
464*c9733229SMatthew Dillon 
465*c9733229SMatthew Dillon 	for (n = 0; cline[i] < from - 1; n += j) {
466*c9733229SMatthew Dillon 		if ((line = get_line(fp[i], &j)) == NULL)
467*c9733229SMatthew Dillon 			trouble();
468*c9733229SMatthew Dillon 		if (pr != NULL)
469*c9733229SMatthew Dillon 			printf("%s%s", pr, line);
470*c9733229SMatthew Dillon 		cline[i]++;
471*c9733229SMatthew Dillon 	}
472*c9733229SMatthew Dillon 	return ((int) n);
473*c9733229SMatthew Dillon }
474*c9733229SMatthew Dillon 
475*c9733229SMatthew Dillon /*
476*c9733229SMatthew Dillon  * Return 1 or 0 according as the old range (in file 1) contains exactly
477*c9733229SMatthew Dillon  * the same data as the new range (in file 2).
478*c9733229SMatthew Dillon  */
479*c9733229SMatthew Dillon int
duplicate(struct range * r1,struct range * r2)480*c9733229SMatthew Dillon duplicate(struct range *r1, struct range *r2)
481*c9733229SMatthew Dillon {
482*c9733229SMatthew Dillon 	int c,d;
483*c9733229SMatthew Dillon 	int nchar;
484*c9733229SMatthew Dillon 	int nline;
485*c9733229SMatthew Dillon 
486*c9733229SMatthew Dillon 	if (r1->to-r1->from != r2->to-r2->from)
487*c9733229SMatthew Dillon 		return (0);
488*c9733229SMatthew Dillon 	(void)skip(0, r1->from, NULL);
489*c9733229SMatthew Dillon 	(void)skip(1, r2->from, NULL);
490*c9733229SMatthew Dillon 	nchar = 0;
491*c9733229SMatthew Dillon 	for (nline=0; nline < r1->to - r1->from; nline++) {
492*c9733229SMatthew Dillon 		do {
493*c9733229SMatthew Dillon 			c = getc(fp[0]);
494*c9733229SMatthew Dillon 			d = getc(fp[1]);
495*c9733229SMatthew Dillon 			if (c == -1 && d == -1)
496*c9733229SMatthew Dillon 				break;
497*c9733229SMatthew Dillon 			if (c == -1 || d== -1)
498*c9733229SMatthew Dillon 				trouble();
499*c9733229SMatthew Dillon 			nchar++;
500*c9733229SMatthew Dillon 			if (c != d) {
501*c9733229SMatthew Dillon 				repos(nchar);
502*c9733229SMatthew Dillon 				return (0);
503*c9733229SMatthew Dillon 			}
504*c9733229SMatthew Dillon 		} while (c != '\n');
505*c9733229SMatthew Dillon 	}
506*c9733229SMatthew Dillon 	repos(nchar);
507*c9733229SMatthew Dillon 	return (1);
508*c9733229SMatthew Dillon }
509*c9733229SMatthew Dillon 
510*c9733229SMatthew Dillon void
repos(int nchar)511*c9733229SMatthew Dillon repos(int nchar)
512*c9733229SMatthew Dillon {
513*c9733229SMatthew Dillon 	int i;
514*c9733229SMatthew Dillon 
515*c9733229SMatthew Dillon 	for (i = 0; i < 2; i++)
516*c9733229SMatthew Dillon 		(void)fseek(fp[i], (long)-nchar, SEEK_CUR);
517*c9733229SMatthew Dillon }
518*c9733229SMatthew Dillon 
519*c9733229SMatthew Dillon void
trouble(void)520*c9733229SMatthew Dillon trouble(void)
521*c9733229SMatthew Dillon {
522*c9733229SMatthew Dillon 	errx(EXIT_FAILURE, "logic error");
523*c9733229SMatthew Dillon }
524*c9733229SMatthew Dillon 
525*c9733229SMatthew Dillon /*
526*c9733229SMatthew Dillon  * collect an editing script for later regurgitation
527*c9733229SMatthew Dillon  */
528*c9733229SMatthew Dillon int
edit(struct diff * diff,int dup,int j)529*c9733229SMatthew Dillon edit(struct diff *diff, int dup, int j)
530*c9733229SMatthew Dillon {
531*c9733229SMatthew Dillon 	if (((dup + 1) & eflag) == 0)
532*c9733229SMatthew Dillon 		return (j);
533*c9733229SMatthew Dillon 	j++;
534*c9733229SMatthew Dillon 	overlap[j] = !dup;
535*c9733229SMatthew Dillon 	if (!dup)
536*c9733229SMatthew Dillon 		overlapcnt++;
537*c9733229SMatthew Dillon 	de[j].old.from = diff->old.from;
538*c9733229SMatthew Dillon 	de[j].old.to = diff->old.to;
539*c9733229SMatthew Dillon 	de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
540*c9733229SMatthew Dillon 	de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
541*c9733229SMatthew Dillon 	return (j);
542*c9733229SMatthew Dillon }
543*c9733229SMatthew Dillon 
544*c9733229SMatthew Dillon /* regurgitate */
545*c9733229SMatthew Dillon void
edscript(int n)546*c9733229SMatthew Dillon edscript(int n)
547*c9733229SMatthew Dillon {
548*c9733229SMatthew Dillon 	int k;
549*c9733229SMatthew Dillon     size_t j;
550*c9733229SMatthew Dillon 	char block[BUFSIZ];
551*c9733229SMatthew Dillon 
552*c9733229SMatthew Dillon 	for (; n > 0; n--) {
553*c9733229SMatthew Dillon 		if (!oflag || !overlap[n])
554*c9733229SMatthew Dillon 			prange(&de[n].old);
555*c9733229SMatthew Dillon 		else
556*c9733229SMatthew Dillon 			printf("%da\n=======\n", de[n].old.to -1);
557*c9733229SMatthew Dillon 		(void)fseek(fp[2], (long)de[n].new.from, SEEK_SET);
558*c9733229SMatthew Dillon 		for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
559*c9733229SMatthew Dillon 			size_t r;
560*c9733229SMatthew Dillon 			j = k > BUFSIZ ? BUFSIZ : k;
561*c9733229SMatthew Dillon 			r = fread(block, 1, j, fp[2]);
562*c9733229SMatthew Dillon 			if (r == 0) {
563*c9733229SMatthew Dillon 				if (feof(fp[2]))
564*c9733229SMatthew Dillon 					break;
565*c9733229SMatthew Dillon 				trouble();
566*c9733229SMatthew Dillon 			}
567*c9733229SMatthew Dillon 			if (r != j)
568*c9733229SMatthew Dillon 				j = r;
569*c9733229SMatthew Dillon 			(void)fwrite(block, 1, j, stdout);
570*c9733229SMatthew Dillon 		}
571*c9733229SMatthew Dillon 		if (!oflag || !overlap[n])
572*c9733229SMatthew Dillon 			printf(".\n");
573*c9733229SMatthew Dillon 		else {
574*c9733229SMatthew Dillon 			printf("%s\n.\n", f3mark);
575*c9733229SMatthew Dillon 			printf("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
576*c9733229SMatthew Dillon 		}
577*c9733229SMatthew Dillon 	}
578*c9733229SMatthew Dillon 	exit(overlapcnt);
579*c9733229SMatthew Dillon }
580*c9733229SMatthew Dillon 
581*c9733229SMatthew Dillon void
increase(void)582*c9733229SMatthew Dillon increase(void)
583*c9733229SMatthew Dillon {
584*c9733229SMatthew Dillon 	struct diff *p;
585*c9733229SMatthew Dillon 	char *q;
586*c9733229SMatthew Dillon 	size_t newsz, incr;
587*c9733229SMatthew Dillon 
588*c9733229SMatthew Dillon 	/* are the memset(3) calls needed? */
589*c9733229SMatthew Dillon 	newsz = szchanges == 0 ? 64 : 2 * szchanges;
590*c9733229SMatthew Dillon 	incr = newsz - szchanges;
591*c9733229SMatthew Dillon 
592*c9733229SMatthew Dillon 	p = reallocarray(d13, newsz, sizeof(struct diff));
593*c9733229SMatthew Dillon 	if (p == NULL)
594*c9733229SMatthew Dillon 		err(1, NULL);
595*c9733229SMatthew Dillon 	memset(p + szchanges, 0, incr * sizeof(struct diff));
596*c9733229SMatthew Dillon 	d13 = p;
597*c9733229SMatthew Dillon 	p = reallocarray(d23, newsz, sizeof(struct diff));
598*c9733229SMatthew Dillon 	if (p == NULL)
599*c9733229SMatthew Dillon 		err(1, NULL);
600*c9733229SMatthew Dillon 	memset(p + szchanges, 0, incr * sizeof(struct diff));
601*c9733229SMatthew Dillon 	d23 = p;
602*c9733229SMatthew Dillon 	p = reallocarray(de, newsz, sizeof(struct diff));
603*c9733229SMatthew Dillon 	if (p == NULL)
604*c9733229SMatthew Dillon 		err(1, NULL);
605*c9733229SMatthew Dillon 	memset(p + szchanges, 0, incr * sizeof(struct diff));
606*c9733229SMatthew Dillon 	de = p;
607*c9733229SMatthew Dillon 	q = reallocarray(overlap, newsz, sizeof(char));
608*c9733229SMatthew Dillon 	if (q == NULL)
609*c9733229SMatthew Dillon 		err(1, NULL);
610*c9733229SMatthew Dillon 	memset(q + szchanges, 0, incr * sizeof(char));
611*c9733229SMatthew Dillon 	overlap = q;
612*c9733229SMatthew Dillon 	szchanges = newsz;
613*c9733229SMatthew Dillon }
614*c9733229SMatthew Dillon 
615*c9733229SMatthew Dillon 
616*c9733229SMatthew Dillon void
usage(void)617*c9733229SMatthew Dillon usage(void)
618*c9733229SMatthew Dillon {
619*c9733229SMatthew Dillon 	//extern char *__progname;
620*c9733229SMatthew Dillon 
621*c9733229SMatthew Dillon 	fprintf(stderr, "usage: %s [-exEX3] /tmp/d3a.?????????? "
622*c9733229SMatthew Dillon 	    "/tmp/d3b.?????????? file1 file2 file3\n", "diff3");
623*c9733229SMatthew Dillon 	exit(EXIT_FAILURE);
624*c9733229SMatthew Dillon }
625