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