xref: /freebsd/usr.bin/diff3/diff3.c (revision 32cd3ee5901ea33d41ff550e5f40ce743c8d4165)
1 /*	$OpenBSD: diff3prog.c,v 1.11 2009/10/27 23:59:37 deraadt 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 
65 #include <sys/types.h>
66 #include <sys/capsicum.h>
67 #include <sys/procdesc.h>
68 #include <sys/wait.h>
69 
70 #include <assert.h>
71 #include <capsicum_helpers.h>
72 #include <ctype.h>
73 #include <err.h>
74 #include <getopt.h>
75 #include <inttypes.h>
76 #include <limits.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <unistd.h>
81 
82 /*
83  * "from" is first in range of changed lines; "to" is last+1
84  * from=to=line after point of insertion for added lines.
85  */
86 struct range {
87 	int from;
88 	int to;
89 };
90 
91 enum difftype {
92 	DIFF_NONE,
93 	DIFF_TYPE1,
94 	DIFF_TYPE2,
95 	DIFF_TYPE3,
96 };
97 
98 struct diff {
99 	enum difftype type;
100 
101 	/* Ranges as lines */
102 	struct range old;
103 	struct range new;
104 };
105 
106 #define EFLAG_NONE 	0
107 #define EFLAG_OVERLAP 	1
108 #define EFLAG_NOOVERLAP	2
109 #define EFLAG_UNMERGED	3
110 
111 static size_t szchanges;
112 
113 static struct diff *d13;
114 static struct diff *d23;
115 /*
116  * "de" is used to gather editing scripts.  These are later spewed out in
117  * reverse order.  Its first element must be all zero, the "old" and "new"
118  * components of "de" contain line positions. Array overlap indicates which
119  * sections in "de" correspond to lines that are different in all three files.
120  */
121 static struct diff *de;
122 static char *overlap;
123 static int  *de_delta;	/* file1-file3 line number delta per edit */
124 static int  overlapcnt;
125 static FILE *fp[3];
126 static int cline[3];		/* # of the last-read line in each file (0-2) */
127 /*
128  * The latest known correspondence between line numbers of the 3 files
129  * is stored in last[1-3];
130  */
131 static int last[4];
132 static int Aflag, eflag, iflag, mflag, Tflag;
133 static int oflag;		/* indicates whether to mark overlaps (-E or -X) */
134 static int strip_cr;
135 static char *f1mark, *f2mark, *f3mark;
136 static const char *oldmark = "<<<<<<<";
137 static const char *orgmark = "|||||||";
138 static const char *newmark = ">>>>>>>";
139 static const char *divider = "=======";
140 
141 static bool duplicate(struct range *, struct range *);
142 static int edit(struct diff *, bool, int, enum difftype);
143 static char *getchange(FILE *);
144 static char *get_line(FILE *, size_t *);
145 static int readin(int fd, struct diff **);
146 static int skip(int, int, const char *);
147 static void change(int, struct range *, bool);
148 static void keep(int, struct range *);
149 static void merge(int, int);
150 static void prange(struct range *, bool);
151 static void repos(int);
152 static void separate(const char *);
153 static void edscript(int) __dead2;
154 static void Ascript(int) __dead2;
155 static void mergescript(int, int) __dead2;
156 static void increase(void);
157 static void usage(void);
158 static void printrange(FILE *, struct range *);
159 
160 static const char diff3_version[] = "FreeBSD diff3 20260213";
161 
162 enum {
163 	DIFFPROG_OPT,
164 	STRIPCR_OPT,
165 	HELP_OPT,
166 	VERSION_OPT
167 };
168 
169 #define DIFF_PATH "/usr/bin/diff"
170 
171 #define OPTIONS "3aAeEiL:mTxX"
172 static struct option longopts[] = {
173 	{ "ed",			no_argument,		NULL,	'e' },
174 	{ "show-overlap",	no_argument,		NULL,	'E' },
175 	{ "overlap-only",	no_argument,		NULL,	'x' },
176 	{ "initial-tab",	no_argument,		NULL,	'T' },
177 	{ "text",		no_argument,		NULL,	'a' },
178 	{ "strip-trailing-cr",	no_argument,		NULL,	STRIPCR_OPT },
179 	{ "show-all",		no_argument,		NULL,	'A' },
180 	{ "easy-only",		no_argument,		NULL,	'3' },
181 	{ "merge",		no_argument,		NULL,	'm' },
182 	{ "label",		required_argument,	NULL,	'L' },
183 	{ "diff-program",	required_argument,	NULL,	DIFFPROG_OPT },
184 	{ "help",		no_argument,		NULL,	HELP_OPT},
185 	{ "version",		no_argument,		NULL,	VERSION_OPT}
186 };
187 
188 static void
189 usage(void)
190 {
191 	fprintf(stderr, "usage: diff3 [-3aAeEimTxX] [-L label1] [-L label2] "
192 	    "[-L label3] file1 file2 file3\n");
193 }
194 
195 static int
196 strtoi(char *str, char **end)
197 {
198 	intmax_t num;
199 
200 	errno = 0;
201 	num = strtoimax(str, end, 10);
202 	if ((end != NULL && *end == str) ||
203 	    num < 0 || num > INT_MAX ||
204 	    errno == EINVAL || errno == ERANGE)
205 		err(1, "error in diff output");
206 	return (int)num;
207 }
208 
209 /*
210  * Read diff hunks into the array pointed to by *dd.
211  *
212  * The output from `diff foo bar` consists of a series of hunks describing
213  * an addition (lines in bar not present in foo), change (lines in bar
214  * different from lines in foo), or deletion (lines in foo not present in
215  * bar).  Each record starts with a line of the form:
216  *
217  * a[,b]xc[,d]
218  *
219  * where a, b, c, and d are nonnegative integers (b and d are printed only
220  * if they differ from a and c, respectively), and x is either 'a' for an
221  * addition, 'c' for a change, or 'd' for a deletion.  This is then
222  * followed by a series of lines (which we ignore) giving the added,
223  * changed, or deleted text.
224  *
225  * For an addition, a == b is the last line in 'foo' before the addition,
226  * while c through d is the range of lines in 'bar' to be added to 'foo'.
227  *
228  * For a change, a through b is the range of lines in 'foo' to be replaced
229  * and c through d is the range of lines in 'bar' to replace them with.
230  *
231  * For a deletion, a through b is the range of lines in 'foo' to remove
232  * and c == d is the line in 'bar' which corresponds to the last line
233  * before the deletion.
234  *
235  * The observant reader will have noticed that x is not really needed and
236  * that we can fully describe any hunk using only a, b, c, and d:
237  *
238  * - an addition replaces a zero-length range in one file with a
239  *   non-zero-length range from the other
240  *
241  * - a change replaces a non-zero-length range in one file with a
242  *   non-zero-length range from the other
243  *
244  * - a deletion replaces a non-zero-length range in one file with a
245  *   zero-length range from the other
246  */
247 static int
248 readin(int fd, struct diff **dd)
249 {
250 	int a, b, c, d;
251 	int i;
252 	char kind, *p;
253 	FILE *f;
254 
255 	f = fdopen(fd, "r");
256 	if (f == NULL)
257 		err(2, "fdopen");
258 	for (i = 0; (p = getchange(f)) != NULL; i++) {
259 		if ((size_t)i >= szchanges - 1)
260 			increase();
261 
262 		a = b = strtoi(p, &p);
263 		if (*p == ',')
264 			b = strtoi(p + 1, &p);
265 		kind = *p++;
266 		c = d = strtoi(p, &p);
267 		if (*p == ',')
268 			d = strtoi(p + 1, &p);
269 		if (*p != '\n')
270 			errx(1, "error in diff output");
271 		if (kind == 'a')
272 			a++;
273 		else if (kind == 'c')
274 			/* nothing */ ;
275 		else if (kind == 'd')
276 			c++;
277 		else
278 			errx(1, "error in diff output");
279 		b++;
280 		d++;
281 		if (b < a || d < c)
282 			errx(1, "error in diff output");
283 		(*dd)[i].old.from = a;
284 		(*dd)[i].old.to = b;
285 		(*dd)[i].new.from = c;
286 		(*dd)[i].new.to = d;
287 		if (i > 0) {
288 			if ((*dd)[i].old.from < (*dd)[i - 1].old.to ||
289 			    (*dd)[i].new.from < (*dd)[i - 1].new.to)
290 				errx(1, "diff output out of order");
291 		}
292 	}
293 	if (i > 0) {
294 		(*dd)[i].old.from = (*dd)[i].old.to = (*dd)[i - 1].old.to;
295 		(*dd)[i].new.from = (*dd)[i].new.to = (*dd)[i - 1].new.to;
296 	}
297 	fclose(f);
298 	return (i);
299 }
300 
301 static int
302 diffexec(const char *diffprog, char **diffargv, int fd[])
303 {
304 	int pd;
305 
306 	switch (pdfork(&pd, PD_CLOEXEC)) {
307 	case 0:
308 		close(fd[0]);
309 		if (dup2(fd[1], STDOUT_FILENO) == -1)
310 			err(2, "child could not duplicate descriptor");
311 		close(fd[1]);
312 		execvp(diffprog, diffargv);
313 		err(2, "could not execute diff: %s", diffprog);
314 		break;
315 	case -1:
316 		err(2, "could not fork");
317 		break;
318 	}
319 	close(fd[1]);
320 	return (pd);
321 }
322 
323 static char *
324 getchange(FILE *b)
325 {
326 	char *line;
327 
328 	while ((line = get_line(b, NULL)) != NULL) {
329 		if (isdigit((unsigned char)line[0]))
330 			return (line);
331 	}
332 	return (NULL);
333 }
334 
335 
336 static char *
337 get_line(FILE *b, size_t *n)
338 {
339 	ssize_t len;
340 	static char *buf = NULL;
341 	static size_t bufsize = 0;
342 
343 	if ((len = getline(&buf, &bufsize, b)) < 0)
344 		return (NULL);
345 
346 	if (strip_cr && len >= 2 && strcmp("\r\n", &(buf[len - 2])) == 0) {
347 		buf[len - 2] = '\n';
348 		buf[len - 1] = '\0';
349 		len--;
350 	}
351 
352 	if (n != NULL)
353 		*n = len;
354 
355 	return (buf);
356 }
357 
358 static void
359 merge(int m1, int m2)
360 {
361 	struct diff *d1, *d2, *d3;
362 	int j, t1, t2;
363 	int f1f3delta;
364 	bool dup = false;
365 
366 	d1 = d13;
367 	d2 = d23;
368 	j = 0;
369 	f1f3delta = 0;
370 
371 	for (;;) {
372 		t1 = (d1 < d13 + m1);
373 		t2 = (d2 < d23 + m2);
374 		if (!t1 && !t2)
375 			break;
376 
377 		/* first file is different from the others */
378 		if (!t2 || (t1 && d1->new.to < d2->new.from)) {
379 			/* stuff peculiar to 1st file */
380 			if (eflag == EFLAG_NONE) {
381 				separate("1");
382 				change(1, &d1->old, false);
383 				keep(2, &d1->new);
384 				change(3, &d1->new, false);
385 			} else if (mflag) {
386 				j++;
387 				de[j].type = DIFF_TYPE1;
388 				de[j].old = d1->old;
389 				de[j].new = d1->new;
390 				overlap[j] = 0;
391 			} else if (eflag == EFLAG_OVERLAP) {
392 				j = edit(d2, dup, j, DIFF_TYPE1);
393 			}
394 			f1f3delta += (d1->old.to - d1->old.from) -
395 			    (d1->new.to - d1->new.from);
396 			d1++;
397 			continue;
398 		}
399 		/* second file is different from others */
400 		if (!t1 || (t2 && d2->new.to < d1->new.from)) {
401 			if (eflag == EFLAG_NONE) {
402 				separate("2");
403 				keep(1, &d2->new);
404 				change(3, &d2->new, false);
405 				change(2, &d2->old, false);
406 			} else if (Aflag || mflag) {
407 				if (eflag == EFLAG_UNMERGED) {
408 					j = edit(d2, dup, j, DIFF_TYPE2);
409 					de_delta[j] = f1f3delta;
410 				}
411 			}
412 			d2++;
413 			continue;
414 		}
415 		/*
416 		 * Merge overlapping changes in first file
417 		 * this happens after extension (see below).
418 		 */
419 		if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
420 			d1[1].old.from = d1->old.from;
421 			d1[1].new.from = d1->new.from;
422 			d1++;
423 			continue;
424 		}
425 
426 		/* merge overlapping changes in second */
427 		if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
428 			d2[1].old.from = d2->old.from;
429 			d2[1].new.from = d2->new.from;
430 			d2++;
431 			continue;
432 		}
433 		/* stuff peculiar to third file or different in all */
434 		if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
435 			dup = duplicate(&d1->old, &d2->old);
436 			/*
437 			 * dup = 0 means all files differ
438 			 * dup = 1 means files 1 and 2 identical
439 			 */
440 			if (eflag == EFLAG_NONE) {
441 				separate(dup ? "3" : "");
442 				change(1, &d1->old, dup);
443 				change(2, &d2->old, false);
444 				d3 = d1->old.to > d1->old.from ? d1 : d2;
445 				change(3, &d3->new, false);
446 			} else if (mflag) {
447 				j++;
448 				de[j].type = DIFF_TYPE3;
449 				de[j].old = d1->old;
450 				de[j].new = d1->new;
451 				overlap[j] = !dup;
452 				if (!dup)
453 					overlapcnt++;
454 			} else {
455 				j = edit(d1, dup, j, DIFF_TYPE3);
456 			}
457 			dup = false;
458 			f1f3delta += (d1->old.to - d1->old.from) -
459 			    (d1->new.to - d1->new.from);
460 			d1++;
461 			d2++;
462 			continue;
463 		}
464 		/*
465 		 * Overlapping changes from file 1 and 2; extend changes
466 		 * appropriately to make them coincide.
467 		 */
468 		if (d1->new.from < d2->new.from) {
469 			d2->old.from -= d2->new.from - d1->new.from;
470 			d2->new.from = d1->new.from;
471 		} else if (d2->new.from < d1->new.from) {
472 			d1->old.from -= d1->new.from - d2->new.from;
473 			d1->new.from = d2->new.from;
474 		}
475 		if (d1->new.to > d2->new.to) {
476 			d2->old.to += d1->new.to - d2->new.to;
477 			d2->new.to = d1->new.to;
478 		} else if (d2->new.to > d1->new.to) {
479 			d1->old.to += d2->new.to - d1->new.to;
480 			d1->new.to = d2->new.to;
481 		}
482 	}
483 
484 	if (mflag)
485 		mergescript(j, f1f3delta);
486 	else if (Aflag)
487 		Ascript(j);
488 	else if (eflag)
489 		edscript(j);
490 }
491 
492 static void
493 separate(const char *s)
494 {
495 	printf("====%s\n", s);
496 }
497 
498 /*
499  * The range of lines rold.from thru rold.to in file i is to be changed.
500  * It is to be printed only if it does not duplicate something to be
501  * printed later.
502  */
503 static void
504 change(int i, struct range *rold, bool dup)
505 {
506 
507 	printf("%d:", i);
508 	last[i] = rold->to;
509 	prange(rold, false);
510 	if (dup)
511 		return;
512 	i--;
513 	skip(i, rold->from, NULL);
514 	skip(i, rold->to, "  ");
515 }
516 
517 /*
518  * Print the range of line numbers, rold.from thru rold.to, as n1,n2 or
519  * n1.
520  */
521 static void
522 prange(struct range *rold, bool delete)
523 {
524 
525 	if (rold->to <= rold->from)
526 		printf("%da\n", rold->from - 1);
527 	else {
528 		printf("%d", rold->from);
529 		if (rold->to > rold->from + 1)
530 			printf(",%d", rold->to - 1);
531 		if (delete)
532 			printf("d\n");
533 		else
534 			printf("c\n");
535 	}
536 }
537 
538 /*
539  * No difference was reported by diff between file 1 (or 2) and file 3,
540  * and an artificial dummy difference (trange) must be ginned up to
541  * correspond to the change reported in the other file.
542  */
543 static void
544 keep(int i, struct range *rnew)
545 {
546 	int delta;
547 	struct range trange;
548 
549 	delta = last[3] - last[i];
550 	trange.from = rnew->from - delta;
551 	trange.to = rnew->to - delta;
552 	change(i, &trange, true);
553 }
554 
555 /*
556  * skip to just before line number from in file "i".  If "pr" is non-NULL,
557  * print all skipped stuff with string pr as a prefix.
558  */
559 static int
560 skip(int i, int from, const char *pr)
561 {
562 	size_t j, n;
563 	char *line;
564 
565 	for (n = 0; cline[i] < from - 1; n += j) {
566 		if ((line = get_line(fp[i], &j)) == NULL)
567 			errx(1, "logic error");
568 		if (pr != NULL)
569 			printf("%s%s", Tflag == 1 ? "\t" : pr, line);
570 		cline[i]++;
571 	}
572 	return ((int) n);
573 }
574 
575 /*
576  * Return 1 or 0 according as the old range (in file 1) contains exactly
577  * the same data as the new range (in file 2).
578  */
579 static bool
580 duplicate(struct range *r1, struct range *r2)
581 {
582 	int c, d;
583 	int nchar;
584 	int nline;
585 
586 	if (r1->to-r1->from != r2->to-r2->from)
587 		return (0);
588 	skip(0, r1->from, NULL);
589 	skip(1, r2->from, NULL);
590 	nchar = 0;
591 	for (nline = 0; nline < r1->to - r1->from; nline++) {
592 		do {
593 			c = getc(fp[0]);
594 			d = getc(fp[1]);
595 			if (c == -1 && d == -1)
596 				break;
597 			if (c == -1 || d == -1)
598 				errx(1, "logic error");
599 			nchar++;
600 			if (c != d) {
601 				repos(nchar);
602 				return (0);
603 			}
604 		} while (c != '\n');
605 	}
606 	repos(nchar);
607 	return (1);
608 }
609 
610 static void
611 repos(int nchar)
612 {
613 	int i;
614 
615 	for (i = 0; i < 2; i++)
616 		(void)fseek(fp[i], (long)-nchar, SEEK_CUR);
617 }
618 
619 /*
620  * collect an editing script for later regurgitation
621  */
622 static int
623 edit(struct diff *diff, bool dup, int j, enum difftype difftype)
624 {
625 	if (!(eflag == EFLAG_UNMERGED ||
626 		(!dup && eflag == EFLAG_OVERLAP ) ||
627 		(dup && eflag == EFLAG_NOOVERLAP))) {
628 		return (j);
629 	}
630 	j++;
631 	overlap[j] = !dup;
632 	if (!dup)
633 		overlapcnt++;
634 
635 	de[j].type = difftype;
636 	de[j].old.from = diff->old.from;
637 	de[j].old.to = diff->old.to;
638 	de[j].new.from = diff->new.from;
639 	de[j].new.to = diff->new.to;
640 	return (j);
641 }
642 
643 static void
644 printrange(FILE *p, struct range *r)
645 {
646 	char *line = NULL;
647 	size_t len = 0;
648 	int i = 1;
649 
650 	/* We haven't been asked to print anything */
651 	if (r->from == r->to)
652 		return;
653 
654 	if (r->from > r->to)
655 		errx(1, "invalid print range");
656 
657 	/*
658 	 * XXX-THJ: We read through all of the file for each range printed.
659 	 * This duplicates work and will probably impact performance on large
660 	 * files with lots of ranges.
661 	 */
662 	fseek(p, 0L, SEEK_SET);
663 	while (getline(&line, &len, p) > 0) {
664 		if (i >= r->from)
665 			printf("%s", line);
666 		if (++i > r->to - 1)
667 			break;
668 	}
669 	free(line);
670 }
671 
672 /* regurgitate */
673 static void
674 edscript(int n)
675 {
676 	bool delete;
677 	struct range *new, *old;
678 
679 	for (; n > 0; n--) {
680 		new = &de[n].new;
681 		old = &de[n].old;
682 
683 		delete = (new->from == new->to);
684 		if (de[n].type == DIFF_TYPE1) {
685 			if (delete)
686 				printf("%dd\n", new->from - 1);
687 			else if (old->from == new->from && old->to == new->to) {
688 				printf("%dc\n", old->from);
689 				printrange(fp[2], old);
690 				printf(".\n");
691 			}
692 			continue;
693 		} else {
694 			if (!oflag || !overlap[n]) {
695 				prange(old, delete);
696 			} else {
697 				printf("%da\n", old->to - 1);
698 				printf("%s\n", divider);
699 			}
700 			printrange(fp[2], new);
701 			if (!oflag || !overlap[n]) {
702 				if (!delete)
703 					printf(".\n");
704 			} else {
705 				printf("%s %s\n.\n", newmark, f3mark);
706 				printf("%da\n%s %s\n.\n", old->from - 1,
707 					oldmark, f1mark);
708 			}
709 		}
710 	}
711 	if (iflag)
712 		printf("w\nq\n");
713 
714 	exit(oflag ? overlapcnt > 0 : 0);
715 }
716 
717 /*
718  * Output an edit script to turn mine into yours, when there is a conflict
719  * between the 3 files bracket the changes. Regurgitate the diffs in reverse
720  * order to allow the ed script to track down where the lines are as changes
721  * are made.
722  */
723 static void
724 Ascript(int n)
725 {
726 	int startmark;
727 	bool deletenew;
728 	bool deleteold;
729 
730 	struct range *new, *old;
731 
732 	for (; n > 0; n--) {
733 		new = &de[n].new;
734 		old = &de[n].old;
735 		deletenew = (new->from == new->to);
736 		deleteold = (old->from == old->to);
737 
738 		if (de[n].type == DIFF_TYPE2) {
739 			if (!oflag || !overlap[n]) {
740 				prange(old, deletenew);
741 				printrange(fp[2], new);
742 			} else {
743 				startmark = new->to - 1 + de_delta[n];
744 
745 				printf("%da\n", startmark);
746 				printf("%s %s\n", newmark, f3mark);
747 
748 				printf(".\n");
749 
750 				printf("%da\n", startmark -
751 					(new->to - new->from));
752 				printf("%s %s\n", oldmark, f2mark);
753 				if (!deleteold)
754 					printrange(fp[1], old);
755 				printf("%s\n.\n", divider);
756 			}
757 
758 		} else if (de[n].type == DIFF_TYPE3) {
759 			startmark = old->to - 1;
760 
761 			if (!oflag || !overlap[n]) {
762 				prange(old, deletenew);
763 				printrange(fp[2], new);
764 			} else {
765 				printf("%da\n", startmark);
766 				printf("%s %s\n", orgmark, f2mark);
767 
768 				if (deleteold) {
769 					struct range r;
770 					r.from = old->from-1;
771 					r.to = new->to;
772 					printrange(fp[1], &r);
773 				} else
774 					printrange(fp[1], old);
775 
776 				printf("%s\n", divider);
777 				printrange(fp[2], new);
778 			}
779 
780 			if (!oflag || !overlap[n]) {
781 				if (!deletenew)
782 					printf(".\n");
783 			} else {
784 				printf("%s %s\n.\n", newmark, f3mark);
785 
786 				/*
787 				 * Go to the start of the conflict in original
788 				 * file and append lines
789 				 */
790 				printf("%da\n%s %s\n.\n",
791 					startmark - (old->to - old->from),
792 					oldmark, f1mark);
793 			}
794 		}
795 	}
796 	if (iflag)
797 		printf("w\nq\n");
798 
799 	exit(overlapcnt > 0);
800 }
801 
802 /*
803  * Output the merged file directly (don't generate an ed script). When
804  * regurgitating diffs we need to walk forward through the file and print any
805  * inbetween lines.
806  */
807 static void
808 mergescript(int i, int f1f3delta)
809 {
810 	struct range r, *new, *old;
811 	int n;
812 
813 	r.from = 1;
814 	r.to = 1;
815 
816 	for (n = 1; n <= i; n++) {
817 		new = &de[n].new;
818 		old = &de[n].old;
819 
820 		/*
821 		 * Print any lines leading up to here. If we are merging don't
822 		 * print deleted ranges.
823 		 */
824 		if (de[n].type == DIFF_TYPE1)
825 			r.to = old->to;
826 		else if (de[n].type == DIFF_TYPE2)
827 			r.to = new->from + de_delta[n];
828 		else
829 			r.to = old->from;
830 
831 		printrange(fp[0], &r);
832 		switch (de[n].type) {
833 		case DIFF_TYPE1:
834 			/* Content included in "between" printing from fp[0] */
835 			break;
836 		case DIFF_TYPE2:
837 			printf("%s %s\n", oldmark, f2mark);
838 			printrange(fp[1], old);
839 			printf("%s\n", divider);
840 			printrange(fp[2], new);
841 			printf("%s %s\n", newmark, f3mark);
842 			break;
843 		case DIFF_TYPE3:
844 			if (!oflag || !overlap[n]) {
845 				printrange(fp[2], new);
846 			} else {
847 
848 				printf("%s %s\n", oldmark, f1mark);
849 				printrange(fp[0], old);
850 
851 				if (eflag != EFLAG_OVERLAP) {
852 					printf("%s %s\n", orgmark, f2mark);
853 					if (old->from == old->to) {
854 						struct range or;
855 						or.from = old->from - 1;
856 						or.to = new->to;
857 						printrange(fp[1], &or);
858 					} else {
859 						printrange(fp[1], old);
860 					}
861 				}
862 
863 				printf("%s\n", divider);
864 
865 				printrange(fp[2], new);
866 				printf("%s %s\n", newmark, f3mark);
867 			}
868 			break;
869 		default:
870 			__assert_unreachable();
871 		}
872 
873 		if (de[n].type == DIFF_TYPE2)
874 			r.from = new->to + de_delta[n];
875 		else
876 			r.from = old->to;
877 	}
878 
879 	/*
880 	 * Print from the final range to the end of 'myfile'. Any deletions or
881 	 * additions to this file should have been handled by now.
882 	 */
883 	r.from -= f1f3delta;
884 	r.to = INT_MAX;
885 	printrange(fp[2], &r);
886 	exit(overlapcnt > 0);
887 }
888 
889 static void
890 increase(void)
891 {
892 	struct diff *p;
893 	char *q;
894 	int *s;
895 	size_t newsz, incr;
896 
897 	/* are the memset(3) calls needed? */
898 	newsz = szchanges == 0 ? 64 : 2 * szchanges;
899 	incr = newsz - szchanges;
900 
901 	p = reallocarray(d13, newsz, sizeof(*p));
902 	if (p == NULL)
903 		err(1, NULL);
904 	memset(p + szchanges, 0, incr * sizeof(*p));
905 	d13 = p;
906 	p = reallocarray(d23, newsz, sizeof(*p));
907 	if (p == NULL)
908 		err(1, NULL);
909 	memset(p + szchanges, 0, incr * sizeof(*p));
910 	d23 = p;
911 	p = reallocarray(de, newsz, sizeof(*p));
912 	if (p == NULL)
913 		err(1, NULL);
914 	memset(p + szchanges, 0, incr * sizeof(*p));
915 	de = p;
916 	q = reallocarray(overlap, newsz, 1);
917 	if (q == NULL)
918 		err(1, NULL);
919 	memset(q + szchanges, 0, incr * 1);
920 	overlap = q;
921 	s = reallocarray(de_delta, newsz, sizeof(*s));
922 	if (s == NULL)
923 		err(1, NULL);
924 	memset(s + szchanges, 0, incr * sizeof(*s));
925 	de_delta = s;
926 	szchanges = newsz;
927 }
928 
929 static void
930 wait_and_check(int pd)
931 {
932 	int status;
933 
934 	while (pdwait(pd, &status, WEXITED, NULL, NULL) == -1) {
935 		if (errno != EINTR)
936 			err(2, "pdwait");
937 	}
938 
939 	if (WIFEXITED(status) && WEXITSTATUS(status) >= 2)
940 		errx(2, "diff exited abnormally");
941 	if (WIFSIGNALED(status))
942 		errx(2, "diff killed by signal %d", WTERMSIG(status));
943 }
944 
945 int
946 main(int argc, char **argv)
947 {
948 	int ch, nblabels, m, n;
949 	char *labels[] = { NULL, NULL, NULL };
950 	const char *diffprog = DIFF_PATH;
951 	char *file1, *file2, *file3;
952 	char *diffargv[7];
953 	int diffargc = 0;
954 	int fd13[2], fd23[2];
955 	int pd13, pd23;
956 	cap_rights_t rights_ro;
957 
958 	nblabels = 0;
959 	eflag = EFLAG_NONE;
960 	oflag = 0;
961 	diffargv[diffargc++] = __DECONST(char *, diffprog);
962 	while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
963 		switch (ch) {
964 		case '3':
965 			eflag = EFLAG_NOOVERLAP;
966 			break;
967 		case 'a':
968 			diffargv[diffargc++] = __DECONST(char *, "-a");
969 			break;
970 		case 'A':
971 			Aflag = 1;
972 			break;
973 		case 'e':
974 			eflag = EFLAG_UNMERGED;
975 			break;
976 		case 'E':
977 			eflag = EFLAG_OVERLAP;
978 			oflag = 1;
979 			break;
980 		case 'i':
981 			iflag = 1;
982 			break;
983 		case 'L':
984 			oflag = 1;
985 			if (nblabels >= 3)
986 				errx(2, "too many file label options");
987 			labels[nblabels++] = optarg;
988 			break;
989 		case 'm':
990 			Aflag = 1;
991 			oflag = 1;
992 			mflag = 1;
993 			break;
994 		case 'T':
995 			Tflag = 1;
996 			break;
997 		case 'x':
998 			eflag = EFLAG_OVERLAP;
999 			break;
1000 		case 'X':
1001 			oflag = 1;
1002 			eflag = EFLAG_OVERLAP;
1003 			break;
1004 		case DIFFPROG_OPT:
1005 			diffprog = optarg;
1006 			break;
1007 		case STRIPCR_OPT:
1008 			strip_cr = 1;
1009 			diffargv[diffargc++] = __DECONST(char *, "--strip-trailing-cr");
1010 			break;
1011 		case HELP_OPT:
1012 			usage();
1013 			exit(0);
1014 		case VERSION_OPT:
1015 			printf("%s\n", diff3_version);
1016 			exit(0);
1017 		}
1018 	}
1019 	argc -= optind;
1020 	argv += optind;
1021 
1022 	if (Aflag) {
1023 		if (eflag == EFLAG_NONE)
1024 			eflag = EFLAG_UNMERGED;
1025 		oflag = 1;
1026 	}
1027 
1028 	if (argc != 3) {
1029 		usage();
1030 		exit(2);
1031 	}
1032 
1033 	if (caph_limit_stdio() == -1)
1034 		err(2, "unable to limit stdio");
1035 
1036 	cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
1037 
1038 	/* TODO stdio */
1039 	file1 = argv[0];
1040 	file2 = argv[1];
1041 	file3 = argv[2];
1042 
1043 	if (oflag) {
1044 		asprintf(&f1mark, "%s",
1045 		    labels[0] != NULL ? labels[0] : file1);
1046 		if (f1mark == NULL)
1047 			err(2, "asprintf");
1048 		asprintf(&f2mark, "%s",
1049 		    labels[1] != NULL ? labels[1] : file2);
1050 		if (f2mark == NULL)
1051 			err(2, "asprintf");
1052 		asprintf(&f3mark, "%s",
1053 		    labels[2] != NULL ? labels[2] : file3);
1054 		if (f3mark == NULL)
1055 			err(2, "asprintf");
1056 	}
1057 	fp[0] = fopen(file1, "r");
1058 	if (fp[0] == NULL)
1059 		err(2, "Can't open %s", file1);
1060 	if (caph_rights_limit(fileno(fp[0]), &rights_ro) < 0)
1061 		err(2, "unable to limit rights on: %s", file1);
1062 
1063 	fp[1] = fopen(file2, "r");
1064 	if (fp[1] == NULL)
1065 		err(2, "Can't open %s", file2);
1066 	if (caph_rights_limit(fileno(fp[1]), &rights_ro) < 0)
1067 		err(2, "unable to limit rights on: %s", file2);
1068 
1069 	fp[2] = fopen(file3, "r");
1070 	if (fp[2] == NULL)
1071 		err(2, "Can't open %s", file3);
1072 	if (caph_rights_limit(fileno(fp[2]), &rights_ro) < 0)
1073 		err(2, "unable to limit rights on: %s", file3);
1074 
1075 	if (pipe(fd13))
1076 		err(2, "pipe");
1077 	if (pipe(fd23))
1078 		err(2, "pipe");
1079 
1080 	diffargv[diffargc] = file1;
1081 	diffargv[diffargc + 1] = file3;
1082 	diffargv[diffargc + 2] = NULL;
1083 	pd13 = diffexec(diffprog, diffargv, fd13);
1084 
1085 	diffargv[diffargc] = file2;
1086 	pd23 = diffexec(diffprog, diffargv, fd23);
1087 
1088 	caph_cache_catpages();
1089 	if (caph_enter() < 0)
1090 		err(2, "unable to enter capability mode");
1091 
1092 	/* parse diffs */
1093 	increase();
1094 	m = readin(fd13[0], &d13);
1095 	n = readin(fd23[0], &d23);
1096 
1097 	wait_and_check(pd13);
1098 	wait_and_check(pd23);
1099 
1100 	merge(m, n);
1101 
1102 	exit(0);
1103 }
1104