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