xref: /illumos-gate/usr/src/cmd/diff3/diff3prog.c (revision 6a634c9d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5b369f4b8Sjohnlev  * Common Development and Distribution License (the "License").
6b369f4b8Sjohnlev  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23*23a1cceaSRoger A. Faulkner  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
26*23a1cceaSRoger A. Faulkner /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27*23a1cceaSRoger A. Faulkner /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <limits.h>
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*
387c478bd9Sstevel@tonic-gate  * diff3 - 3-way differential file comparison
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * d13 = diff report on f1 vs f3
457c478bd9Sstevel@tonic-gate  * d23 = diff report on f2 vs f3
467c478bd9Sstevel@tonic-gate  * f1, f2, f3 the 3 files
477c478bd9Sstevel@tonic-gate  * if changes in f1 overlap with changes in f3, m1 and m3 are used
487c478bd9Sstevel@tonic-gate  * to mark the overlaps; otherwise, the file names f1 and f3 are used
497c478bd9Sstevel@tonic-gate  * (only for options E and X).
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate struct  range {int from, to; };
537c478bd9Sstevel@tonic-gate 	/*
547c478bd9Sstevel@tonic-gate 	 * from is first in range of changed lines
557c478bd9Sstevel@tonic-gate 	 * to is last+1
567c478bd9Sstevel@tonic-gate 	 * from = to = line after point of insertion
577c478bd9Sstevel@tonic-gate 	 * for added lines
587c478bd9Sstevel@tonic-gate 	 */
597c478bd9Sstevel@tonic-gate struct diff {struct range old, new; };
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	NC 4096
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * de is used to gather editing scripts,
647c478bd9Sstevel@tonic-gate  * that are later spewed out in reverse order.
657c478bd9Sstevel@tonic-gate  * its first element must be all zero
667c478bd9Sstevel@tonic-gate  * the "new" component of de contains line positions
677c478bd9Sstevel@tonic-gate  * or byte positions depending on when you look(!?)
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate static struct diff d13[NC];
707c478bd9Sstevel@tonic-gate static struct diff d23[NC];
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * array overlap indicates which sections in de correspond to
747c478bd9Sstevel@tonic-gate  * lines that are different in all three files.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static struct diff de[NC];
787c478bd9Sstevel@tonic-gate static char overlap[NC];
797c478bd9Sstevel@tonic-gate static int  overlapcnt = 0;
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static char line[LINE_MAX+1];
827c478bd9Sstevel@tonic-gate static FILE *fp[3];
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  *	the number of the last-read line in each file
857c478bd9Sstevel@tonic-gate  *	is kept in cline[0-2]
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate static int cline[3];
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  *	the latest known correspondence between line
907c478bd9Sstevel@tonic-gate  *	numbers of the 3 files is stored in last[1-3]
917c478bd9Sstevel@tonic-gate  */
927c478bd9Sstevel@tonic-gate static int last[4];
937c478bd9Sstevel@tonic-gate static int eflag;
947c478bd9Sstevel@tonic-gate static int oflag;	/* indicates whether to mark overlaps (-E or -X) */
957c478bd9Sstevel@tonic-gate static int debug  = 0;
967c478bd9Sstevel@tonic-gate /* markers for -E and -X: */
977c478bd9Sstevel@tonic-gate static char f1mark[8+MAXPATHLEN], f3mark[8+MAXPATHLEN];
987c478bd9Sstevel@tonic-gate 		/* Need space for "<<<<<<< " or ">>>>>>> " plus filename   */
997c478bd9Sstevel@tonic-gate static int save_err;	/* saves errno */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static int readin(char *name, struct diff *dd);
1027c478bd9Sstevel@tonic-gate static int number(char **lc);
1037c478bd9Sstevel@tonic-gate static int digit(int c);
1047c478bd9Sstevel@tonic-gate static int getchange(FILE *b);
105*23a1cceaSRoger A. Faulkner static int getaline(FILE *b);
1067c478bd9Sstevel@tonic-gate static void merge(int m1, int m2);
1077c478bd9Sstevel@tonic-gate static void separate(char *s);
1087c478bd9Sstevel@tonic-gate static void change(int i, struct range *rold, int dup);
1097c478bd9Sstevel@tonic-gate static void prange(struct range *rold);
1107c478bd9Sstevel@tonic-gate static void keep(int i, struct range *rnew);
1117c478bd9Sstevel@tonic-gate static int skip(int i, int from, char *pr);
1127c478bd9Sstevel@tonic-gate static int duplicate(struct range *r1, struct range *r2);
1137c478bd9Sstevel@tonic-gate static void repos(int nchar);
1147c478bd9Sstevel@tonic-gate static void trouble();
1157c478bd9Sstevel@tonic-gate static int edit(struct diff *diff, int dup, int j);
1167c478bd9Sstevel@tonic-gate static void edscript(int n);
117b369f4b8Sjohnlev static void usage();
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1207c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	int i, m, n;
1237c478bd9Sstevel@tonic-gate 	eflag  = 0;
1247c478bd9Sstevel@tonic-gate 	oflag  = 0;
125b369f4b8Sjohnlev 	if ((argc > 1) && (*argv[1] == '-')) {
1267c478bd9Sstevel@tonic-gate 		switch (argv[1][1]) {
127b369f4b8Sjohnlev 		case 'e':
1287c478bd9Sstevel@tonic-gate 			eflag = 3;
1297c478bd9Sstevel@tonic-gate 			break;
1307c478bd9Sstevel@tonic-gate 		case '3':
1317c478bd9Sstevel@tonic-gate 			eflag = 2;
1327c478bd9Sstevel@tonic-gate 			break;
1337c478bd9Sstevel@tonic-gate 		case 'x':
1347c478bd9Sstevel@tonic-gate 			eflag = 1;
1357c478bd9Sstevel@tonic-gate 			break;
1367c478bd9Sstevel@tonic-gate 		case 'E':
1377c478bd9Sstevel@tonic-gate 			eflag = 3;
1387c478bd9Sstevel@tonic-gate 			oflag = 1;
1397c478bd9Sstevel@tonic-gate 			break;
1407c478bd9Sstevel@tonic-gate 		case 'X':
1417c478bd9Sstevel@tonic-gate 			oflag = eflag = 1;
1427c478bd9Sstevel@tonic-gate 			break;
143b369f4b8Sjohnlev 		default:
144b369f4b8Sjohnlev 			usage();
145b369f4b8Sjohnlev 			break;
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 		argv++;
1487c478bd9Sstevel@tonic-gate 		argc--;
1497c478bd9Sstevel@tonic-gate 	}
150b369f4b8Sjohnlev 	if (argc < 6)
151b369f4b8Sjohnlev 		usage();
1527c478bd9Sstevel@tonic-gate 	if (oflag) {
1537c478bd9Sstevel@tonic-gate 		(void) snprintf(f1mark, sizeof (f1mark), "<<<<<<< %s",
154*23a1cceaSRoger A. Faulkner 		    argc >= 7 ? argv[6] : argv[3]);
1557c478bd9Sstevel@tonic-gate 		(void) snprintf(f3mark, sizeof (f3mark), ">>>>>>> %s",
156*23a1cceaSRoger A. Faulkner 		    argc >= 8 ? argv[7] : argv[5]);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	m = readin(argv[1], d13);
1607c478bd9Sstevel@tonic-gate 	n = readin(argv[2], d23);
1617c478bd9Sstevel@tonic-gate 	for (i = 0; i <= 2; i++)
1627c478bd9Sstevel@tonic-gate 		if ((fp[i] = fopen(argv[i+3], "r")) == NULL) {
1637c478bd9Sstevel@tonic-gate 			save_err = errno;
1647c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "diff3: can't open %s: ",
165*23a1cceaSRoger A. Faulkner 			    argv[i+3]);
1667c478bd9Sstevel@tonic-gate 			errno = save_err;
1677c478bd9Sstevel@tonic-gate 			perror("");
1687c478bd9Sstevel@tonic-gate 			exit(1);
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 	merge(m, n);
1717c478bd9Sstevel@tonic-gate 	return (0);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * pick up the line numbers of all changes from
1767c478bd9Sstevel@tonic-gate  * one change file
1777c478bd9Sstevel@tonic-gate  * (this puts the numbers in a vector, which is not
1787c478bd9Sstevel@tonic-gate  * strictly necessary, since the vector is processed
1797c478bd9Sstevel@tonic-gate  * in one sequential pass. The vector could be optimized
1807c478bd9Sstevel@tonic-gate  * out of existence)
1817c478bd9Sstevel@tonic-gate  */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate static int
readin(char * name,struct diff * dd)1847c478bd9Sstevel@tonic-gate readin(char *name, struct diff *dd)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	int i;
1877c478bd9Sstevel@tonic-gate 	int a, b, c, d;
1887c478bd9Sstevel@tonic-gate 	char kind;
1897c478bd9Sstevel@tonic-gate 	char *p;
1907c478bd9Sstevel@tonic-gate 	if ((fp[0] = fopen(name, "r")) == NULL) {
1917c478bd9Sstevel@tonic-gate 		save_err = errno;
1927c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "diff3: can't open %s: ", name);
1937c478bd9Sstevel@tonic-gate 		errno = save_err;
1947c478bd9Sstevel@tonic-gate 		perror("");
1957c478bd9Sstevel@tonic-gate 		exit(1);
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 	for (i = 0; getchange(fp[0]); i++) {
1987c478bd9Sstevel@tonic-gate 		if (i >= NC) {
1997c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "diff3: too many changes\n");
2007c478bd9Sstevel@tonic-gate 			exit(0);
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 		p = line;
2037c478bd9Sstevel@tonic-gate 		a = b = number(&p);
2047c478bd9Sstevel@tonic-gate 		if (*p == ',') {
2057c478bd9Sstevel@tonic-gate 			p++;
2067c478bd9Sstevel@tonic-gate 			b = number(&p);
2077c478bd9Sstevel@tonic-gate 		}
2087c478bd9Sstevel@tonic-gate 		kind = *p++;
2097c478bd9Sstevel@tonic-gate 		c = d = number(&p);
2107c478bd9Sstevel@tonic-gate 		if (*p == ',') {
2117c478bd9Sstevel@tonic-gate 			p++;
2127c478bd9Sstevel@tonic-gate 			d = number(&p);
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 		if (kind == 'a')
2157c478bd9Sstevel@tonic-gate 			a++;
2167c478bd9Sstevel@tonic-gate 		if (kind == 'd')
2177c478bd9Sstevel@tonic-gate 			c++;
2187c478bd9Sstevel@tonic-gate 		b++;
2197c478bd9Sstevel@tonic-gate 		d++;
2207c478bd9Sstevel@tonic-gate 		dd[i].old.from = a;
2217c478bd9Sstevel@tonic-gate 		dd[i].old.to = b;
2227c478bd9Sstevel@tonic-gate 		dd[i].new.from = c;
2237c478bd9Sstevel@tonic-gate 		dd[i].new.to = d;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 	dd[i].old.from = dd[i-1].old.to;
2267c478bd9Sstevel@tonic-gate 	dd[i].new.from = dd[i-1].new.to;
2277c478bd9Sstevel@tonic-gate 	(void) fclose(fp[0]);
2287c478bd9Sstevel@tonic-gate 	return (i);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate static int
number(char ** lc)2327c478bd9Sstevel@tonic-gate number(char **lc)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	int nn;
2357c478bd9Sstevel@tonic-gate 	nn = 0;
2367c478bd9Sstevel@tonic-gate 	while (digit(**lc))
2377c478bd9Sstevel@tonic-gate 		nn = nn*10 + *(*lc)++ - '0';
2387c478bd9Sstevel@tonic-gate 	return (nn);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate static int
digit(int c)2427c478bd9Sstevel@tonic-gate digit(int c)
2437c478bd9Sstevel@tonic-gate {
2447c478bd9Sstevel@tonic-gate 	return (c >= '0' && c <= '9');
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate static int
getchange(FILE * b)2487c478bd9Sstevel@tonic-gate getchange(FILE *b)
2497c478bd9Sstevel@tonic-gate {
250*23a1cceaSRoger A. Faulkner 	while (getaline(b))
2517c478bd9Sstevel@tonic-gate 		if (digit(line[0]))
2527c478bd9Sstevel@tonic-gate 			return (1);
2537c478bd9Sstevel@tonic-gate 	return (0);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate static int
getaline(FILE * b)257*23a1cceaSRoger A. Faulkner getaline(FILE *b)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	int i, c;
2607c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (line)-1; i++) {
2617c478bd9Sstevel@tonic-gate 		c = getc(b);
2627c478bd9Sstevel@tonic-gate 		if (c == EOF) {
2637c478bd9Sstevel@tonic-gate 			line[i] = 0;
2647c478bd9Sstevel@tonic-gate 			return (i);
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 		line[i] = c;
2677c478bd9Sstevel@tonic-gate 		if (c == '\n') {
2687c478bd9Sstevel@tonic-gate 			line[++i] = 0;
2697c478bd9Sstevel@tonic-gate 			return (i);
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 	return (0);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate static void
merge(int m1,int m2)2767c478bd9Sstevel@tonic-gate merge(int m1, int m2)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	struct diff *d1, *d2, *d3;
2797c478bd9Sstevel@tonic-gate 	int dup;
2807c478bd9Sstevel@tonic-gate 	int j;
2817c478bd9Sstevel@tonic-gate 	int t1, t2;
2827c478bd9Sstevel@tonic-gate 	d1 = d13;
2837c478bd9Sstevel@tonic-gate 	d2 = d23;
2847c478bd9Sstevel@tonic-gate 	j = 0;
2857c478bd9Sstevel@tonic-gate 	for (; (t1 = d1 < d13+m1) | (t2 = d2 < d23+m2); ) {
2867c478bd9Sstevel@tonic-gate 		if (debug) {
2877c478bd9Sstevel@tonic-gate 			(void) printf("%d,%d=%d,%d %d,%d=%d,%d\n",
288*23a1cceaSRoger A. Faulkner 			    d1->old.from, d1->old.to,
289*23a1cceaSRoger A. Faulkner 			    d1->new.from, d1->new.to,
290*23a1cceaSRoger A. Faulkner 			    d2->old.from, d2->old.to,
291*23a1cceaSRoger A. Faulkner 			    d2->new.from, d2->new.to);
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		/* first file is different from others */
2957c478bd9Sstevel@tonic-gate 		if (!t2 || t1 && d1->new.to < d2->new.from) {
2967c478bd9Sstevel@tonic-gate 			/* stuff peculiar to 1st file */
2977c478bd9Sstevel@tonic-gate 			if (eflag == 0) {
2987c478bd9Sstevel@tonic-gate 				separate("1");
2997c478bd9Sstevel@tonic-gate 				change(1, &d1->old, 0);
3007c478bd9Sstevel@tonic-gate 				keep(2, &d1->new);
3017c478bd9Sstevel@tonic-gate 				change(3, &d1->new, 0);
3027c478bd9Sstevel@tonic-gate 			}
3037c478bd9Sstevel@tonic-gate 			d1++;
3047c478bd9Sstevel@tonic-gate 			continue;
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		/* second file is different from others */
3087c478bd9Sstevel@tonic-gate 		if (!t1 || t2 && d2->new.to < d1->new.from) {
3097c478bd9Sstevel@tonic-gate 			if (eflag == 0) {
3107c478bd9Sstevel@tonic-gate 				separate("2");
3117c478bd9Sstevel@tonic-gate 				keep(1, &d2->new);
3127c478bd9Sstevel@tonic-gate 				change(2, &d2->old, 0);
3137c478bd9Sstevel@tonic-gate 				change(3, &d2->new, 0);
3147c478bd9Sstevel@tonic-gate 			}
3157c478bd9Sstevel@tonic-gate 			d2++;
3167c478bd9Sstevel@tonic-gate 			continue;
3177c478bd9Sstevel@tonic-gate 		}
3187c478bd9Sstevel@tonic-gate 		/*
3197c478bd9Sstevel@tonic-gate 		 * merge overlapping changes in first file
3207c478bd9Sstevel@tonic-gate 		 * this happens after extension see below
3217c478bd9Sstevel@tonic-gate 		 */
3227c478bd9Sstevel@tonic-gate 		if (d1+1 < d13+m1 && d1->new.to >= d1[1].new.from) {
3237c478bd9Sstevel@tonic-gate 			d1[1].old.from = d1->old.from;
3247c478bd9Sstevel@tonic-gate 			d1[1].new.from = d1->new.from;
3257c478bd9Sstevel@tonic-gate 			d1++;
3267c478bd9Sstevel@tonic-gate 			continue;
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		/* merge overlapping changes in second */
3307c478bd9Sstevel@tonic-gate 		if (d2+1 < d23+m2 && d2->new.to >= d2[1].new.from) {
3317c478bd9Sstevel@tonic-gate 			d2[1].old.from = d2->old.from;
3327c478bd9Sstevel@tonic-gate 			d2[1].new.from = d2->new.from;
3337c478bd9Sstevel@tonic-gate 			d2++;
3347c478bd9Sstevel@tonic-gate 			continue;
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		/* stuff peculiar to third file or different in all */
3387c478bd9Sstevel@tonic-gate 		if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
3397c478bd9Sstevel@tonic-gate 			dup = duplicate(&d1->old, &d2->old);
3407c478bd9Sstevel@tonic-gate 			/*
3417c478bd9Sstevel@tonic-gate 			 * dup = 0 means all files differ
3427c478bd9Sstevel@tonic-gate 			 * dup = 1 meands files 1&2 identical
3437c478bd9Sstevel@tonic-gate 			 */
3447c478bd9Sstevel@tonic-gate 			if (eflag == 0) {
3457c478bd9Sstevel@tonic-gate 				separate(dup?"3":"");
3467c478bd9Sstevel@tonic-gate 				change(1, &d1->old, dup);
3477c478bd9Sstevel@tonic-gate 				change(2, &d2->old, 0);
3487c478bd9Sstevel@tonic-gate 				d3 = d1->old.to > d1->old.from ? d1 : d2;
3497c478bd9Sstevel@tonic-gate 				change(3, &d3->new, 0);
3507c478bd9Sstevel@tonic-gate 			} else
3517c478bd9Sstevel@tonic-gate 				j = edit(d1, dup, j);
3527c478bd9Sstevel@tonic-gate 			d1++;
3537c478bd9Sstevel@tonic-gate 			d2++;
3547c478bd9Sstevel@tonic-gate 			continue;
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 		/*
3577c478bd9Sstevel@tonic-gate 		 * overlapping changes from file1 & 2
3587c478bd9Sstevel@tonic-gate 		 * extend changes appropriately to
3597c478bd9Sstevel@tonic-gate 		 * make them coincide
3607c478bd9Sstevel@tonic-gate 		 */
3617c478bd9Sstevel@tonic-gate 		if (d1->new.from < d2->new.from) {
3627c478bd9Sstevel@tonic-gate 			d2->old.from -= d2->new.from-d1->new.from;
3637c478bd9Sstevel@tonic-gate 			d2->new.from = d1->new.from;
3647c478bd9Sstevel@tonic-gate 		} else if (d2->new.from < d1->new.from) {
3657c478bd9Sstevel@tonic-gate 			d1->old.from -= d1->new.from-d2->new.from;
3667c478bd9Sstevel@tonic-gate 			d1->new.from = d2->new.from;
3677c478bd9Sstevel@tonic-gate 		}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 		if (d1->new.to > d2->new.to) {
3707c478bd9Sstevel@tonic-gate 			d2->old.to += d1->new.to - d2->new.to;
3717c478bd9Sstevel@tonic-gate 			d2->new.to = d1->new.to;
3727c478bd9Sstevel@tonic-gate 		} else if (d2->new.to > d1->new.to) {
3737c478bd9Sstevel@tonic-gate 			d1->old.to += d2->new.to - d1->new.to;
3747c478bd9Sstevel@tonic-gate 			d1->new.to = d2->new.to;
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 	if (eflag) {
3787c478bd9Sstevel@tonic-gate 		edscript(j);
3797c478bd9Sstevel@tonic-gate 		if (j)
3807c478bd9Sstevel@tonic-gate 			(void) printf("w\nq\n");
3817c478bd9Sstevel@tonic-gate 	}
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate static void
separate(char * s)3857c478bd9Sstevel@tonic-gate separate(char *s)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate 	(void) printf("====%s\n", s);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate /*
3917c478bd9Sstevel@tonic-gate  * the range of ines rold.from thru rold.to in file i
3927c478bd9Sstevel@tonic-gate  * is to be changed. it is to be printed only if
3937c478bd9Sstevel@tonic-gate  * it does not duplicate something to be printed later
3947c478bd9Sstevel@tonic-gate  */
3957c478bd9Sstevel@tonic-gate static void
change(int i,struct range * rold,int dup)3967c478bd9Sstevel@tonic-gate change(int i, struct range *rold, int dup)
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate 	(void) printf("%d:", i);
3997c478bd9Sstevel@tonic-gate 	last[i] = rold->to;
4007c478bd9Sstevel@tonic-gate 	prange(rold);
4017c478bd9Sstevel@tonic-gate 	if (dup)
4027c478bd9Sstevel@tonic-gate 		return;
4037c478bd9Sstevel@tonic-gate 	if (debug)
4047c478bd9Sstevel@tonic-gate 		return;
4057c478bd9Sstevel@tonic-gate 	i--;
4067c478bd9Sstevel@tonic-gate 	(void) skip(i, rold->from, (char *)0);
4077c478bd9Sstevel@tonic-gate 	(void) skip(i, rold->to, "  ");
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /*
4117c478bd9Sstevel@tonic-gate  * print the range of line numbers, rold.from  thru rold.to
4127c478bd9Sstevel@tonic-gate  * as n1, n2 or n1
4137c478bd9Sstevel@tonic-gate  */
4147c478bd9Sstevel@tonic-gate static void
prange(struct range * rold)4157c478bd9Sstevel@tonic-gate prange(struct range *rold)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 	if (rold->to <= rold->from)
4187c478bd9Sstevel@tonic-gate 		(void) printf("%da\n", rold->from-1);
4197c478bd9Sstevel@tonic-gate 	else {
4207c478bd9Sstevel@tonic-gate 		(void) printf("%d", rold->from);
4217c478bd9Sstevel@tonic-gate 		if (rold->to > rold->from+1)
4227c478bd9Sstevel@tonic-gate 			(void) printf(",%d", rold->to-1);
4237c478bd9Sstevel@tonic-gate 		(void) printf("c\n");
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate /*
4287c478bd9Sstevel@tonic-gate  * no difference was reported by diff between file 1(or 2)
4297c478bd9Sstevel@tonic-gate  * and file 3, and an artificial dummy difference (trange)
4307c478bd9Sstevel@tonic-gate  * must be ginned up to correspond to the change reported
4317c478bd9Sstevel@tonic-gate  * in the other file
4327c478bd9Sstevel@tonic-gate  */
4337c478bd9Sstevel@tonic-gate static void
keep(int i,struct range * rnew)4347c478bd9Sstevel@tonic-gate keep(int i, struct range *rnew)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	int delta;
4377c478bd9Sstevel@tonic-gate 	struct range trange;
4387c478bd9Sstevel@tonic-gate 	delta = last[3] - last[i];
4397c478bd9Sstevel@tonic-gate 	trange.from = rnew->from - delta;
4407c478bd9Sstevel@tonic-gate 	trange.to = rnew->to - delta;
4417c478bd9Sstevel@tonic-gate 	change(i, &trange, 1);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate /*
4457c478bd9Sstevel@tonic-gate  * skip to just befor line number from in file i
4467c478bd9Sstevel@tonic-gate  * if "pr" is nonzero, print all skipped stuff
4477c478bd9Sstevel@tonic-gate  * with string pr as a prefix
4487c478bd9Sstevel@tonic-gate  */
4497c478bd9Sstevel@tonic-gate static int
skip(int i,int from,char * pr)4507c478bd9Sstevel@tonic-gate skip(int i, int from, char *pr)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	int j, n;
4537c478bd9Sstevel@tonic-gate 	for (n = 0; cline[i] < from-1; n += j) {
454*23a1cceaSRoger A. Faulkner 		if ((j = getaline(fp[i])) == 0)
4557c478bd9Sstevel@tonic-gate 			trouble();
4567c478bd9Sstevel@tonic-gate 		if (pr)
4577c478bd9Sstevel@tonic-gate 			(void) printf("%s%s", pr, line);
4587c478bd9Sstevel@tonic-gate 		cline[i]++;
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	return (n);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate  * return 1 or 0 according as the old range
4657c478bd9Sstevel@tonic-gate  * (in file 1) contains exactly the same data
4667c478bd9Sstevel@tonic-gate  * as the new range (in file 2)
4677c478bd9Sstevel@tonic-gate  */
4687c478bd9Sstevel@tonic-gate static int
duplicate(struct range * r1,struct range * r2)4697c478bd9Sstevel@tonic-gate duplicate(struct range *r1, struct range *r2)
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate 	int c, d;
4727c478bd9Sstevel@tonic-gate 	int nchar;
4737c478bd9Sstevel@tonic-gate 	int nline;
4747c478bd9Sstevel@tonic-gate 	if (r1->to-r1->from != r2->to-r2->from)
4757c478bd9Sstevel@tonic-gate 		return (0);
4767c478bd9Sstevel@tonic-gate 	(void) skip(0, r1->from, (char *)0);
4777c478bd9Sstevel@tonic-gate 	(void) skip(1, r2->from, (char *)0);
4787c478bd9Sstevel@tonic-gate 	nchar = 0;
4797c478bd9Sstevel@tonic-gate 	for (nline = 0; nline < r1->to-r1->from; nline++) {
4807c478bd9Sstevel@tonic-gate 		do {
4817c478bd9Sstevel@tonic-gate 			c = getc(fp[0]);
4827c478bd9Sstevel@tonic-gate 			d = getc(fp[1]);
4837c478bd9Sstevel@tonic-gate 			if (c == -1 || d == -1)
4847c478bd9Sstevel@tonic-gate 				trouble();
4857c478bd9Sstevel@tonic-gate 			nchar++;
4867c478bd9Sstevel@tonic-gate 			if (c != d) {
4877c478bd9Sstevel@tonic-gate 				repos(nchar);
4887c478bd9Sstevel@tonic-gate 				return (0);
4897c478bd9Sstevel@tonic-gate 			}
4907c478bd9Sstevel@tonic-gate 		} while (c != '\n');
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 	repos(nchar);
4937c478bd9Sstevel@tonic-gate 	return (1);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate static void
repos(int nchar)4977c478bd9Sstevel@tonic-gate repos(int nchar)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	int i;
5007c478bd9Sstevel@tonic-gate 	for (i = 0; i < 2; i++)
5017c478bd9Sstevel@tonic-gate 		(void) fseek(fp[i], (long)-nchar, 1);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate static void
trouble()5057c478bd9Sstevel@tonic-gate trouble()
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "diff3: logic error\n");
5087c478bd9Sstevel@tonic-gate 	abort();
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate /*
5127c478bd9Sstevel@tonic-gate  * collect an editing script for later regurgitation
5137c478bd9Sstevel@tonic-gate  */
5147c478bd9Sstevel@tonic-gate static int
edit(struct diff * diff,int dup,int j)5157c478bd9Sstevel@tonic-gate edit(struct diff *diff, int dup, int j)
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	if (((dup+1)&eflag) == 0)
5187c478bd9Sstevel@tonic-gate 		return (j);
5197c478bd9Sstevel@tonic-gate 	j++;
5207c478bd9Sstevel@tonic-gate 	overlap[j] = !dup;
5217c478bd9Sstevel@tonic-gate 	if (!dup) overlapcnt++;
5227c478bd9Sstevel@tonic-gate 	de[j].old.from = diff->old.from;
5237c478bd9Sstevel@tonic-gate 	de[j].old.to = diff->old.to;
5247c478bd9Sstevel@tonic-gate 	de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, (char *)0);
5257c478bd9Sstevel@tonic-gate 	de[j].new.to = de[j].new.from + skip(2, diff->new.to, (char *)0);
5267c478bd9Sstevel@tonic-gate 	return (j);
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate /*		regurgitate */
5307c478bd9Sstevel@tonic-gate static void
edscript(int n)5317c478bd9Sstevel@tonic-gate edscript(int n)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	int j, k;
5347c478bd9Sstevel@tonic-gate 	char	 block[BUFSIZ];
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	for (n = n; n > 0; n--) {
5377c478bd9Sstevel@tonic-gate 		if (!oflag || !overlap[n])
5387c478bd9Sstevel@tonic-gate 			prange(&de[n].old);
5397c478bd9Sstevel@tonic-gate 		else
5407c478bd9Sstevel@tonic-gate 			(void) printf("%da\n=======\n", de[n].old.to -1);
5417c478bd9Sstevel@tonic-gate 		(void) fseek(fp[2], (long)de[n].new.from, 0);
5427c478bd9Sstevel@tonic-gate 		for (k = de[n].new.to-de[n].new.from; k > 0; k -= j) {
5437c478bd9Sstevel@tonic-gate 			j = k > BUFSIZ?BUFSIZ:k;
5447c478bd9Sstevel@tonic-gate 			if (fread(block, 1, j, fp[2]) != j)
5457c478bd9Sstevel@tonic-gate 				trouble();
5467c478bd9Sstevel@tonic-gate 			(void) fwrite(block, 1, j, stdout);
5477c478bd9Sstevel@tonic-gate 		}
5487c478bd9Sstevel@tonic-gate 		if (!oflag || !overlap[n])
5497c478bd9Sstevel@tonic-gate 			(void) printf(".\n");
5507c478bd9Sstevel@tonic-gate 		else {
5517c478bd9Sstevel@tonic-gate 			(void) printf("%s\n.\n", f3mark);
5527c478bd9Sstevel@tonic-gate 			(void) printf("%da\n%s\n.\n", de[n].old.from-1, f1mark);
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 	}
5557c478bd9Sstevel@tonic-gate }
556b369f4b8Sjohnlev 
557b369f4b8Sjohnlev static void
usage()558b369f4b8Sjohnlev usage()
559b369f4b8Sjohnlev {
560b369f4b8Sjohnlev 	(void) fprintf(stderr,
561b369f4b8Sjohnlev 	    "\tusage: diff3prog [-ex3EX] d13 d23 f1 f2 f3 [m1 m2]\n");
562b369f4b8Sjohnlev 	exit(1);
563b369f4b8Sjohnlev }
564