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