xref: /src/usr.bin/diff3/diff3.c (revision 33424fd50ee0333bc6f70b5b14a270b85e5b5de1)
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 
321 	posix_spawn_file_actions_addclose(&fa, fd[0]);
322 	posix_spawn_file_actions_adddup2(&fa, fd[1], STDOUT_FILENO);
323 	posix_spawn_file_actions_addclose(&fa, fd[1]);
324 
325 	error = posix_spawn(&pid, diffargv[0], &fa, &sa, diffargv, environ);
326 	if (error != 0)
327 		errc(2, error, "could not spawn diff");
328 
329 	posix_spawn_file_actions_destroy(&fa);
330 	posix_spawnattr_destroy(&sa);
331 	close(fd[1]);
332 	return (pd);
333 }
334 
335 static char *
getchange(FILE * b)336 getchange(FILE *b)
337 {
338 	char *line;
339 
340 	while ((line = get_line(b, NULL)) != NULL) {
341 		if (isdigit((unsigned char)line[0]))
342 			return (line);
343 	}
344 	return (NULL);
345 }
346 
347 
348 static char *
get_line(FILE * b,size_t * n)349 get_line(FILE *b, size_t *n)
350 {
351 	ssize_t len;
352 	static char *buf = NULL;
353 	static size_t bufsize = 0;
354 
355 	if ((len = getline(&buf, &bufsize, b)) < 0)
356 		return (NULL);
357 
358 	if (strip_cr && len >= 2 && strcmp("\r\n", &(buf[len - 2])) == 0) {
359 		buf[len - 2] = '\n';
360 		buf[len - 1] = '\0';
361 		len--;
362 	}
363 
364 	if (n != NULL)
365 		*n = len;
366 
367 	return (buf);
368 }
369 
370 static void
merge(int m1,int m2)371 merge(int m1, int m2)
372 {
373 	struct diff *d1, *d2, *d3;
374 	int j, t1, t2;
375 	int f1f3delta;
376 	bool dup = false;
377 
378 	d1 = d13;
379 	d2 = d23;
380 	j = 0;
381 	f1f3delta = 0;
382 
383 	for (;;) {
384 		t1 = (d1 < d13 + m1);
385 		t2 = (d2 < d23 + m2);
386 		if (!t1 && !t2)
387 			break;
388 
389 		/* first file is different from the others */
390 		if (!t2 || (t1 && d1->new.to < d2->new.from)) {
391 			/* stuff peculiar to 1st file */
392 			if (eflag == EFLAG_NONE) {
393 				separate("1");
394 				change(1, &d1->old, false);
395 				keep(2, &d1->new);
396 				change(3, &d1->new, false);
397 			} else if (mflag) {
398 				j++;
399 				de[j].type = DIFF_TYPE1;
400 				de[j].old = d1->old;
401 				de[j].new = d1->new;
402 				overlap[j] = 0;
403 			} else if (eflag == EFLAG_OVERLAP) {
404 				j = edit(d2, dup, j, DIFF_TYPE1);
405 			}
406 			f1f3delta += (d1->old.to - d1->old.from) -
407 			    (d1->new.to - d1->new.from);
408 			d1++;
409 			continue;
410 		}
411 		/* second file is different from others */
412 		if (!t1 || (t2 && d2->new.to < d1->new.from)) {
413 			if (eflag == EFLAG_NONE) {
414 				separate("2");
415 				keep(1, &d2->new);
416 				change(3, &d2->new, false);
417 				change(2, &d2->old, false);
418 			} else if (Aflag || mflag) {
419 				if (eflag == EFLAG_UNMERGED) {
420 					j = edit(d2, dup, j, DIFF_TYPE2);
421 					de_delta[j] = f1f3delta;
422 				}
423 			}
424 			d2++;
425 			continue;
426 		}
427 		/*
428 		 * Merge overlapping changes in first file
429 		 * this happens after extension (see below).
430 		 */
431 		if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
432 			d1[1].old.from = d1->old.from;
433 			d1[1].new.from = d1->new.from;
434 			d1++;
435 			continue;
436 		}
437 
438 		/* merge overlapping changes in second */
439 		if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
440 			d2[1].old.from = d2->old.from;
441 			d2[1].new.from = d2->new.from;
442 			d2++;
443 			continue;
444 		}
445 		/* stuff peculiar to third file or different in all */
446 		if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
447 			dup = duplicate(&d1->old, &d2->old);
448 			/*
449 			 * dup = 0 means all files differ
450 			 * dup = 1 means files 1 and 2 identical
451 			 */
452 			if (eflag == EFLAG_NONE) {
453 				separate(dup ? "3" : "");
454 				change(1, &d1->old, dup);
455 				change(2, &d2->old, false);
456 				d3 = d1->old.to > d1->old.from ? d1 : d2;
457 				change(3, &d3->new, false);
458 			} else if (mflag) {
459 				j++;
460 				de[j].type = DIFF_TYPE3;
461 				de[j].old = d1->old;
462 				de[j].new = d1->new;
463 				overlap[j] = !dup;
464 				if (!dup)
465 					overlapcnt++;
466 			} else {
467 				j = edit(d1, dup, j, DIFF_TYPE3);
468 			}
469 			dup = false;
470 			f1f3delta += (d1->old.to - d1->old.from) -
471 			    (d1->new.to - d1->new.from);
472 			d1++;
473 			d2++;
474 			continue;
475 		}
476 		/*
477 		 * Overlapping changes from file 1 and 2; extend changes
478 		 * appropriately to make them coincide.
479 		 */
480 		if (d1->new.from < d2->new.from) {
481 			d2->old.from -= d2->new.from - d1->new.from;
482 			d2->new.from = d1->new.from;
483 		} else if (d2->new.from < d1->new.from) {
484 			d1->old.from -= d1->new.from - d2->new.from;
485 			d1->new.from = d2->new.from;
486 		}
487 		if (d1->new.to > d2->new.to) {
488 			d2->old.to += d1->new.to - d2->new.to;
489 			d2->new.to = d1->new.to;
490 		} else if (d2->new.to > d1->new.to) {
491 			d1->old.to += d2->new.to - d1->new.to;
492 			d1->new.to = d2->new.to;
493 		}
494 	}
495 
496 	if (mflag)
497 		mergescript(j, f1f3delta);
498 	else if (Aflag)
499 		Ascript(j);
500 	else if (eflag)
501 		edscript(j);
502 }
503 
504 static void
separate(const char * s)505 separate(const char *s)
506 {
507 	printf("====%s\n", s);
508 }
509 
510 /*
511  * The range of lines rold.from thru rold.to in file i is to be changed.
512  * It is to be printed only if it does not duplicate something to be
513  * printed later.
514  */
515 static void
change(int i,struct range * rold,bool dup)516 change(int i, struct range *rold, bool dup)
517 {
518 
519 	printf("%d:", i);
520 	last[i] = rold->to;
521 	prange(rold, false);
522 	if (dup)
523 		return;
524 	i--;
525 	skip(i, rold->from, NULL);
526 	skip(i, rold->to, "  ");
527 }
528 
529 /*
530  * Print the range of line numbers, rold.from thru rold.to, as n1,n2 or
531  * n1.
532  */
533 static void
prange(struct range * rold,bool delete)534 prange(struct range *rold, bool delete)
535 {
536 
537 	if (rold->to <= rold->from)
538 		printf("%da\n", rold->from - 1);
539 	else {
540 		printf("%d", rold->from);
541 		if (rold->to > rold->from + 1)
542 			printf(",%d", rold->to - 1);
543 		if (delete)
544 			printf("d\n");
545 		else
546 			printf("c\n");
547 	}
548 }
549 
550 /*
551  * No difference was reported by diff between file 1 (or 2) and file 3,
552  * and an artificial dummy difference (trange) must be ginned up to
553  * correspond to the change reported in the other file.
554  */
555 static void
keep(int i,struct range * rnew)556 keep(int i, struct range *rnew)
557 {
558 	int delta;
559 	struct range trange;
560 
561 	delta = last[3] - last[i];
562 	trange.from = rnew->from - delta;
563 	trange.to = rnew->to - delta;
564 	change(i, &trange, true);
565 }
566 
567 /*
568  * skip to just before line number from in file "i".  If "pr" is non-NULL,
569  * print all skipped stuff with string pr as a prefix.
570  */
571 static int
skip(int i,int from,const char * pr)572 skip(int i, int from, const char *pr)
573 {
574 	size_t j, n;
575 	char *line;
576 
577 	for (n = 0; cline[i] < from - 1; n += j) {
578 		if ((line = get_line(fp[i], &j)) == NULL)
579 			errx(2, "logic error");
580 		if (pr != NULL)
581 			printf("%s%s", Tflag == 1 ? "\t" : pr, line);
582 		cline[i]++;
583 	}
584 	return ((int) n);
585 }
586 
587 /*
588  * Return 1 or 0 according as the old range (in file 1) contains exactly
589  * the same data as the new range (in file 2).
590  */
591 static bool
duplicate(struct range * r1,struct range * r2)592 duplicate(struct range *r1, struct range *r2)
593 {
594 	int c, d;
595 	int nchar;
596 	int nline;
597 
598 	if (r1->to-r1->from != r2->to-r2->from)
599 		return (0);
600 	skip(0, r1->from, NULL);
601 	skip(1, r2->from, NULL);
602 	nchar = 0;
603 	for (nline = 0; nline < r1->to - r1->from; nline++) {
604 		do {
605 			c = getc(fp[0]);
606 			d = getc(fp[1]);
607 			if (c == -1 && d == -1)
608 				break;
609 			if (c == -1 || d == -1)
610 				errx(2, "logic error");
611 			nchar++;
612 			if (c != d) {
613 				repos(nchar);
614 				return (0);
615 			}
616 		} while (c != '\n');
617 	}
618 	repos(nchar);
619 	return (1);
620 }
621 
622 static void
repos(int nchar)623 repos(int nchar)
624 {
625 	int i;
626 
627 	for (i = 0; i < 2; i++)
628 		(void)fseek(fp[i], (long)-nchar, SEEK_CUR);
629 }
630 
631 /*
632  * collect an editing script for later regurgitation
633  */
634 static int
edit(struct diff * diff,bool dup,int j,enum difftype difftype)635 edit(struct diff *diff, bool dup, int j, enum difftype difftype)
636 {
637 	if (!(eflag == EFLAG_UNMERGED ||
638 		(!dup && eflag == EFLAG_OVERLAP ) ||
639 		(dup && eflag == EFLAG_NOOVERLAP))) {
640 		return (j);
641 	}
642 	j++;
643 	overlap[j] = !dup;
644 	if (!dup)
645 		overlapcnt++;
646 
647 	de[j].type = difftype;
648 	de[j].old.from = diff->old.from;
649 	de[j].old.to = diff->old.to;
650 	de[j].new.from = diff->new.from;
651 	de[j].new.to = diff->new.to;
652 	return (j);
653 }
654 
655 static void
printrange(FILE * p,struct range * r)656 printrange(FILE *p, struct range *r)
657 {
658 	char *line = NULL;
659 	size_t len = 0;
660 	int i = 1;
661 
662 	/* We haven't been asked to print anything */
663 	if (r->from == r->to)
664 		return;
665 
666 	if (r->from > r->to)
667 		errx(2, "invalid print range");
668 
669 	/*
670 	 * XXX-THJ: We read through all of the file for each range printed.
671 	 * This duplicates work and will probably impact performance on large
672 	 * files with lots of ranges.
673 	 */
674 	fseek(p, 0L, SEEK_SET);
675 	while (getline(&line, &len, p) > 0) {
676 		if (i >= r->from)
677 			printf("%s", line);
678 		if (++i > r->to - 1)
679 			break;
680 	}
681 	free(line);
682 }
683 
684 /* regurgitate */
685 static void
edscript(int n)686 edscript(int n)
687 {
688 	bool delete;
689 	struct range *new, *old;
690 
691 	for (; n > 0; n--) {
692 		new = &de[n].new;
693 		old = &de[n].old;
694 
695 		delete = (new->from == new->to);
696 		if (de[n].type == DIFF_TYPE1) {
697 			if (delete)
698 				printf("%dd\n", new->from - 1);
699 			else if (old->from == new->from && old->to == new->to) {
700 				printf("%dc\n", old->from);
701 				printrange(fp[2], old);
702 				printf(".\n");
703 			}
704 			continue;
705 		} else {
706 			if (!oflag || !overlap[n]) {
707 				prange(old, delete);
708 			} else {
709 				printf("%da\n", old->to - 1);
710 				printf("%s\n", divider);
711 			}
712 			printrange(fp[2], new);
713 			if (!oflag || !overlap[n]) {
714 				if (!delete)
715 					printf(".\n");
716 			} else {
717 				printf("%s %s\n.\n", newmark, f3mark);
718 				printf("%da\n%s %s\n.\n", old->from - 1,
719 					oldmark, f1mark);
720 			}
721 		}
722 	}
723 	if (iflag)
724 		printf("w\nq\n");
725 
726 	exit(oflag ? overlapcnt > 0 : 0);
727 }
728 
729 /*
730  * Output an edit script to turn mine into yours, when there is a conflict
731  * between the 3 files bracket the changes. Regurgitate the diffs in reverse
732  * order to allow the ed script to track down where the lines are as changes
733  * are made.
734  */
735 static void
Ascript(int n)736 Ascript(int n)
737 {
738 	int startmark;
739 	bool deletenew;
740 	bool deleteold;
741 
742 	struct range *new, *old;
743 
744 	for (; n > 0; n--) {
745 		new = &de[n].new;
746 		old = &de[n].old;
747 		deletenew = (new->from == new->to);
748 		deleteold = (old->from == old->to);
749 
750 		if (de[n].type == DIFF_TYPE2) {
751 			if (!oflag || !overlap[n]) {
752 				prange(old, deletenew);
753 				printrange(fp[2], new);
754 			} else {
755 				startmark = new->to - 1 + de_delta[n];
756 
757 				printf("%da\n", startmark);
758 				printf("%s %s\n", newmark, f3mark);
759 
760 				printf(".\n");
761 
762 				printf("%da\n", startmark -
763 					(new->to - new->from));
764 				printf("%s %s\n", oldmark, f2mark);
765 				if (!deleteold)
766 					printrange(fp[1], old);
767 				printf("%s\n.\n", divider);
768 			}
769 
770 		} else if (de[n].type == DIFF_TYPE3) {
771 			startmark = old->to - 1;
772 
773 			if (!oflag || !overlap[n]) {
774 				prange(old, deletenew);
775 				printrange(fp[2], new);
776 			} else {
777 				printf("%da\n", startmark);
778 				printf("%s %s\n", orgmark, f2mark);
779 
780 				if (deleteold) {
781 					struct range r;
782 					r.from = old->from-1;
783 					r.to = new->to;
784 					printrange(fp[1], &r);
785 				} else
786 					printrange(fp[1], old);
787 
788 				printf("%s\n", divider);
789 				printrange(fp[2], new);
790 			}
791 
792 			if (!oflag || !overlap[n]) {
793 				if (!deletenew)
794 					printf(".\n");
795 			} else {
796 				printf("%s %s\n.\n", newmark, f3mark);
797 
798 				/*
799 				 * Go to the start of the conflict in original
800 				 * file and append lines
801 				 */
802 				printf("%da\n%s %s\n.\n",
803 					startmark - (old->to - old->from),
804 					oldmark, f1mark);
805 			}
806 		}
807 	}
808 	if (iflag)
809 		printf("w\nq\n");
810 
811 	exit(overlapcnt > 0);
812 }
813 
814 /*
815  * Output the merged file directly (don't generate an ed script). When
816  * regurgitating diffs we need to walk forward through the file and print any
817  * inbetween lines.
818  */
819 static void
mergescript(int i,int f1f3delta)820 mergescript(int i, int f1f3delta)
821 {
822 	struct range r, *new, *old;
823 	int n;
824 
825 	r.from = 1;
826 	r.to = 1;
827 
828 	for (n = 1; n <= i; n++) {
829 		new = &de[n].new;
830 		old = &de[n].old;
831 
832 		/*
833 		 * Print any lines leading up to here. If we are merging don't
834 		 * print deleted ranges.
835 		 */
836 		if (de[n].type == DIFF_TYPE1)
837 			r.to = old->to;
838 		else if (de[n].type == DIFF_TYPE2)
839 			r.to = new->from + de_delta[n];
840 		else
841 			r.to = old->from;
842 
843 		printrange(fp[0], &r);
844 		switch (de[n].type) {
845 		case DIFF_TYPE1:
846 			/* Content included in "between" printing from fp[0] */
847 			break;
848 		case DIFF_TYPE2:
849 			printf("%s %s\n", oldmark, f2mark);
850 			printrange(fp[1], old);
851 			printf("%s\n", divider);
852 			printrange(fp[2], new);
853 			printf("%s %s\n", newmark, f3mark);
854 			break;
855 		case DIFF_TYPE3:
856 			if (!oflag || !overlap[n]) {
857 				printrange(fp[2], new);
858 			} else {
859 
860 				printf("%s %s\n", oldmark, f1mark);
861 				printrange(fp[0], old);
862 
863 				if (eflag != EFLAG_OVERLAP) {
864 					printf("%s %s\n", orgmark, f2mark);
865 					if (old->from == old->to) {
866 						struct range or;
867 						or.from = old->from - 1;
868 						or.to = new->to;
869 						printrange(fp[1], &or);
870 					} else {
871 						printrange(fp[1], old);
872 					}
873 				}
874 
875 				printf("%s\n", divider);
876 
877 				printrange(fp[2], new);
878 				printf("%s %s\n", newmark, f3mark);
879 			}
880 			break;
881 		default:
882 			__assert_unreachable();
883 		}
884 
885 		if (de[n].type == DIFF_TYPE2)
886 			r.from = new->to + de_delta[n];
887 		else
888 			r.from = old->to;
889 	}
890 
891 	/*
892 	 * Print from the final range to the end of 'myfile'. Any deletions or
893 	 * additions to this file should have been handled by now.
894 	 */
895 	r.from -= f1f3delta;
896 	r.to = INT_MAX;
897 	printrange(fp[2], &r);
898 	exit(overlapcnt > 0);
899 }
900 
901 static void
increase(void)902 increase(void)
903 {
904 	struct diff *p;
905 	char *q;
906 	int *s;
907 	size_t newsz, incr;
908 
909 	/* are the memset(3) calls needed? */
910 	newsz = szchanges == 0 ? 64 : 2 * szchanges;
911 	incr = newsz - szchanges;
912 
913 	p = reallocarray(d13, newsz, sizeof(*p));
914 	if (p == NULL)
915 		err(2, NULL);
916 	memset(p + szchanges, 0, incr * sizeof(*p));
917 	d13 = p;
918 	p = reallocarray(d23, newsz, sizeof(*p));
919 	if (p == NULL)
920 		err(2, NULL);
921 	memset(p + szchanges, 0, incr * sizeof(*p));
922 	d23 = p;
923 	p = reallocarray(de, newsz, sizeof(*p));
924 	if (p == NULL)
925 		err(2, NULL);
926 	memset(p + szchanges, 0, incr * sizeof(*p));
927 	de = p;
928 	q = reallocarray(overlap, newsz, 1);
929 	if (q == NULL)
930 		err(2, NULL);
931 	memset(q + szchanges, 0, incr * 1);
932 	overlap = q;
933 	s = reallocarray(de_delta, newsz, sizeof(*s));
934 	if (s == NULL)
935 		err(2, NULL);
936 	memset(s + szchanges, 0, incr * sizeof(*s));
937 	de_delta = s;
938 	szchanges = newsz;
939 }
940 
941 static void
wait_and_check(int pd)942 wait_and_check(int pd)
943 {
944 	int status;
945 
946 	while (pdwait(pd, &status, WEXITED, NULL, NULL) == -1) {
947 		if (errno != EINTR)
948 			err(2, "pdwait");
949 	}
950 	close(pd);
951 
952 	if (WIFEXITED(status) && WEXITSTATUS(status) >= 2)
953 		errx(2, "diff exited abnormally");
954 	if (WIFSIGNALED(status))
955 		errx(2, "diff killed by signal %d", WTERMSIG(status));
956 }
957 
958 int
main(int argc,char ** argv)959 main(int argc, char **argv)
960 {
961 	int ch, nblabels, m, n;
962 	char *labels[] = { NULL, NULL, NULL };
963 	const char *diffprog = DIFF_PATH;
964 	char *file1, *file2, *file3;
965 	char *diffargv[7];
966 	int diffargc = 0;
967 	int fd13[2], fd23[2];
968 	int pd13, pd23;
969 	cap_rights_t rights_ro;
970 
971 	nblabels = 0;
972 	eflag = EFLAG_NONE;
973 	oflag = 0;
974 	diffargv[diffargc++] = __DECONST(char *, diffprog);
975 	while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
976 		switch (ch) {
977 		case '3':
978 			eflag = EFLAG_NOOVERLAP;
979 			break;
980 		case 'a':
981 			diffargv[diffargc++] = __DECONST(char *, "-a");
982 			break;
983 		case 'A':
984 			Aflag = 1;
985 			break;
986 		case 'e':
987 			eflag = EFLAG_UNMERGED;
988 			break;
989 		case 'E':
990 			eflag = EFLAG_OVERLAP;
991 			oflag = 1;
992 			break;
993 		case 'i':
994 			iflag = 1;
995 			break;
996 		case 'L':
997 			oflag = 1;
998 			if (nblabels >= 3)
999 				errx(2, "too many file label options");
1000 			labels[nblabels++] = optarg;
1001 			break;
1002 		case 'm':
1003 			Aflag = 1;
1004 			oflag = 1;
1005 			mflag = 1;
1006 			break;
1007 		case 'T':
1008 			Tflag = 1;
1009 			break;
1010 		case 'x':
1011 			eflag = EFLAG_OVERLAP;
1012 			break;
1013 		case 'X':
1014 			oflag = 1;
1015 			eflag = EFLAG_OVERLAP;
1016 			break;
1017 		case DIFFPROG_OPT:
1018 			diffargv[0] = optarg;
1019 			break;
1020 		case STRIPCR_OPT:
1021 			strip_cr = 1;
1022 			diffargv[diffargc++] = __DECONST(char *, "--strip-trailing-cr");
1023 			break;
1024 		case HELP_OPT:
1025 			usage();
1026 			exit(0);
1027 		case VERSION_OPT:
1028 			printf("%s\n", diff3_version);
1029 			exit(0);
1030 		}
1031 	}
1032 	argc -= optind;
1033 	argv += optind;
1034 
1035 	if (Aflag) {
1036 		if (eflag == EFLAG_NONE)
1037 			eflag = EFLAG_UNMERGED;
1038 		oflag = 1;
1039 	}
1040 
1041 	if (argc != 3) {
1042 		usage();
1043 		exit(2);
1044 	}
1045 
1046 	if (caph_limit_stdio() == -1)
1047 		err(2, "unable to limit stdio");
1048 
1049 	cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
1050 
1051 	/* TODO stdio */
1052 	file1 = argv[0];
1053 	file2 = argv[1];
1054 	file3 = argv[2];
1055 
1056 	if (oflag) {
1057 		asprintf(&f1mark, "%s",
1058 		    labels[0] != NULL ? labels[0] : file1);
1059 		if (f1mark == NULL)
1060 			err(2, "asprintf");
1061 		asprintf(&f2mark, "%s",
1062 		    labels[1] != NULL ? labels[1] : file2);
1063 		if (f2mark == NULL)
1064 			err(2, "asprintf");
1065 		asprintf(&f3mark, "%s",
1066 		    labels[2] != NULL ? labels[2] : file3);
1067 		if (f3mark == NULL)
1068 			err(2, "asprintf");
1069 	}
1070 	fp[0] = fopen(file1, "r");
1071 	if (fp[0] == NULL)
1072 		err(2, "Can't open %s", file1);
1073 	if (caph_rights_limit(fileno(fp[0]), &rights_ro) < 0)
1074 		err(2, "unable to limit rights on: %s", file1);
1075 
1076 	fp[1] = fopen(file2, "r");
1077 	if (fp[1] == NULL)
1078 		err(2, "Can't open %s", file2);
1079 	if (caph_rights_limit(fileno(fp[1]), &rights_ro) < 0)
1080 		err(2, "unable to limit rights on: %s", file2);
1081 
1082 	fp[2] = fopen(file3, "r");
1083 	if (fp[2] == NULL)
1084 		err(2, "Can't open %s", file3);
1085 	if (caph_rights_limit(fileno(fp[2]), &rights_ro) < 0)
1086 		err(2, "unable to limit rights on: %s", file3);
1087 
1088 	if (pipe(fd13))
1089 		err(2, "pipe");
1090 	if (pipe(fd23))
1091 		err(2, "pipe");
1092 
1093 
1094 	diffargv[diffargc] = file1;
1095 	diffargv[diffargc + 1] = file3;
1096 	diffargv[diffargc + 2] = NULL;
1097 	pd13 = diffexec(diffargv, fd13);
1098 
1099 	diffargv[diffargc] = file2;
1100 	pd23 = diffexec(diffargv, fd23);
1101 
1102 	caph_cache_catpages();
1103 	if (caph_enter() < 0)
1104 		err(2, "unable to enter capability mode");
1105 
1106 	/* parse diffs */
1107 	increase();
1108 	m = readin(fd13[0], &d13);
1109 	n = readin(fd23[0], &d23);
1110 
1111 	wait_and_check(pd13);
1112 	wait_and_check(pd23);
1113 
1114 	merge(m, n);
1115 
1116 	exit(0);
1117 }
1118