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