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