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