1 /* $OpenBSD: diff3prog.c,v 1.11 2009/10/27 23:59:37 deraadt Exp $ */
4 * Copyright (C) Caldera International Inc. 2001-2002.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
37 * Copyright (c) 1991, 1993
38 * The Regents of the University of California. All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
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.
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
64 * @(#)diff3.c 8.1 (Berkeley) 6/6/93
69 static char sccsid[] = "@(#)diff3.c 8.1 (Berkeley) 6/6/93";
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>
79 #include <capsicum_helpers.h>
92 * "from" is first in range of changed lines; "to" is last+1
93 * from=to=line after point of insertion for added lines.
108 /* Ranges as lines */
114 #define EFLAG_OVERLAP 1
115 #define EFLAG_NOOVERLAP 2
116 #define EFLAG_UNMERGED 3
118 static size_t szchanges;
120 static struct diff *d13;
121 static struct diff *d23;
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.
128 static struct diff *de;
129 static char *overlap;
130 static int overlapcnt;
132 static int cline[3]; /* # of the last-read line in each file (0-2) */
134 * The latest known correspondence between line numbers of the 3 files
135 * is stored in last[1-3];
138 static int Aflag, eflag, iflag, mflag, Tflag;
139 static int oflag; /* indicates whether to mark overlaps (-E or -X) */
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 = "=======";
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 *);
165 static const char diff3_version[] = "FreeBSD diff3 20220517";
174 #define DIFF_PATH "/usr/bin/diff"
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}
196 fprintf(stderr, "usage: diff3 [-3aAeEimTxX] [-L label1] [-L label2] "
197 "[-L label3] file1 file2 file3\n");
201 readin(int fd, struct diff **dd)
211 for (i = 0; (p = getchange(f)); i++) {
213 (*dd)[i].line = strdup(p);
216 if (i >= szchanges - 1)
218 a = b = (int)strtoimax(p, &p, 10);
221 b = (int)strtoimax(p, &p, 10);
224 c = d = (int)strtoimax(p, &p, 10);
227 d = (int)strtoimax(p, &p, 10);
235 (*dd)[i].old.from = a;
237 (*dd)[i].new.from = c;
241 (*dd)[i].old.from = (*dd)[i - 1].old.to;
242 (*dd)[i].new.from = (*dd)[i - 1].new.to;
249 diffexec(const char *diffprog, char **diffargv, int fd[])
253 switch (pdfork(&pd, PD_CLOEXEC)) {
256 if (dup2(fd[1], STDOUT_FILENO) == -1)
257 err(2, "child could not duplicate descriptor");
259 execvp(diffprog, diffargv);
260 err(2, "could not execute diff: %s", diffprog);
263 err(2, "could not fork");
275 while ((line = get_line(b, NULL))) {
276 if (isdigit((unsigned char)line[0]))
284 get_line(FILE *b, size_t *n)
287 static char *buf = NULL;
288 static size_t bufsize = 0;
290 if ((len = getline(&buf, &bufsize, b)) < 0)
293 if (strip_cr && len >= 2 && strcmp("\r\n", &(buf[len - 2])) == 0) {
306 merge(int m1, int m2)
308 struct diff *d1, *d2, *d3;
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) {
322 change(1, &d1->old, false);
324 change(3, &d1->new, false);
329 /* second file is different from others */
330 if (!t1 || (t2 && d2->new.to < d1->new.from)) {
331 if (eflag == EFLAG_NONE) {
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);
345 * Merge overlapping changes in first file
346 * this happens after extension (see below).
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;
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;
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);
366 * dup = 0 means all files differ
367 * dup = 1 means files 1 and 2 identical
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);
376 j = edit(d1, dup, j, DIFF_TYPE3);
384 * Overlapping changes from file 1 and 2; extend changes
385 * appropriately to make them coincide.
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;
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;
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
417 change(int i, struct range *rold, bool dup)
426 skip(i, rold->from, NULL);
427 skip(i, rold->to, " ");
431 * Print the range of line numbers, rold.from thru rold.to, as n1,n2 or
435 prange(struct range *rold, bool delete)
438 if (rold->to <= rold->from)
439 printf("%da\n", rold->from - 1);
441 printf("%d", rold->from);
442 if (rold->to > rold->from + 1)
443 printf(",%d", rold->to - 1);
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.
457 keep(int i, struct range *rnew)
462 delta = last[3] - last[i];
463 trange.from = rnew->from - delta;
464 trange.to = rnew->to - delta;
465 change(i, &trange, true);
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.
473 skip(int i, int from, const char *pr)
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");
482 printf("%s%s", Tflag == 1 ? "\t" : pr, line);
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).
493 duplicate(struct range *r1, struct range *r2)
499 if (r1->to-r1->from != r2->to-r2->from)
501 skip(0, r1->from, NULL);
502 skip(1, r2->from, NULL);
504 for (nline = 0; nline < r1->to - r1->from; nline++) {
508 if (c == -1 && d == -1)
510 if (c == -1 || d == -1)
511 errx(EXIT_FAILURE, "logic error");
528 for (i = 0; i < 2; i++)
529 (void)fseek(fp[i], (long)-nchar, SEEK_CUR);
533 * collect an editing script for later regurgitation
536 edit(struct diff *diff, bool dup, int j, int difftype)
538 if (!(eflag == EFLAG_UNMERGED ||
539 (!dup && eflag == EFLAG_OVERLAP ) ||
540 (dup && eflag == EFLAG_NOOVERLAP))) {
548 de[j].type = difftype;
550 de[j].line = strdup(diff->line);
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;
561 printrange(FILE *p, struct range *r)
568 /* We haven't been asked to print anything */
569 if (r->from == r->to)
573 errx(EXIT_FAILURE, "invalid print range");
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.
580 fseek(p, 0L, SEEK_SET);
581 while ((rlen = getline(&line, &len, p)) > 0) {
595 struct range *new, *old;
601 delete = (new->from == new->to);
602 if (!oflag || !overlap[n]) {
605 printf("%da\n", old->to - 1);
606 printf("%s\n", divider);
608 printrange(fp[2], new);
609 if (!oflag || !overlap[n]) {
613 printf("%s %s\n.\n", newmark, f3mark);
614 printf("%da\n%s %s\n.\n", old->from - 1,
621 exit(eflag == EFLAG_NONE ? overlapcnt : 0);
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
637 struct range *new, *old;
642 deletenew = (new->from == new->to);
643 deleteold = (old->from == old->to);
645 if (de[n].type == DIFF_TYPE2) {
646 if (!oflag || !overlap[n]) {
647 prange(old, deletenew);
648 printrange(fp[2], new);
655 printf("%da\n", startmark);
656 printf("%s %s\n", newmark, f3mark);
660 printf("%da\n", startmark -
661 (new->to - new->from));
662 printf("%s %s\n", oldmark, f2mark);
664 printrange(fp[1], old);
665 printf("%s\n.\n", divider);
668 } else if (de[n].type == DIFF_TYPE3) {
669 startmark = old->to - 1;
671 if (!oflag || !overlap[n]) {
672 prange(old, deletenew);
673 printrange(fp[2], new);
675 printf("%da\n", startmark);
676 printf("%s %s\n", orgmark, f2mark);
680 r.from = old->from-1;
682 printrange(fp[1], &r);
684 printrange(fp[1], old);
686 printf("%s\n", divider);
687 printrange(fp[2], new);
690 if (!oflag || !overlap[n]) {
694 printf("%s %s\n.\n", newmark, f3mark);
697 * Go to the start of the conflict in original
698 * file and append lines
700 printf("%da\n%s %s\n.\n",
701 startmark - (old->to - old->from),
709 exit(overlapcnt > 0);
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
720 struct range r, *new, *old;
726 for (n = 1; n < i+1; n++) {
730 /* print any lines leading up to here */
732 printrange(fp[0], &r);
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);
745 printf("%s %s\n", oldmark, f1mark);
746 printrange(fp[0], old);
748 printf("%s %s\n", orgmark, f2mark);
749 if (old->from == old->to) {
751 or.from = old->from - 1;
753 printrange(fp[1], &or);
755 printrange(fp[1], old);
757 printf("%s\n", divider);
759 printrange(fp[2], new);
760 printf("%s %s\n", newmark, f3mark);
764 if (old->from == old->to)
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.
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
779 if ((old->from == new->from) &&
780 (old->to == new->to))
782 else if (new->from == new->to)
786 * If the range is a 3 way merge then we need to skip a line in the
789 if (de[n-1].type == DIFF_TYPE3)
793 printrange(fp[0], &r);
794 exit(overlapcnt > 0);
804 /* are the memset(3) calls needed? */
805 newsz = szchanges == 0 ? 64 : 2 * szchanges;
806 incr = newsz - szchanges;
808 p = reallocarray(d13, newsz, sizeof(struct diff));
811 memset(p + szchanges, 0, incr * sizeof(struct diff));
813 p = reallocarray(d23, newsz, sizeof(struct diff));
816 memset(p + szchanges, 0, incr * sizeof(struct diff));
818 p = reallocarray(de, newsz, sizeof(struct diff));
821 memset(p + szchanges, 0, incr * sizeof(struct diff));
823 q = reallocarray(overlap, newsz, sizeof(char));
826 memset(q + szchanges, 0, incr * sizeof(char));
833 main(int argc, char **argv)
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;
841 int fd13[2], fd23[2];
843 cap_rights_t rights_ro;
849 diffargv[diffargc++] = __DECONST(char *, diffprog);
850 while ((ch = getopt_long(argc, argv, OPTIONS, longopts, NULL)) != -1) {
853 eflag = EFLAG_NOOVERLAP;
856 diffargv[diffargc++] = __DECONST(char *, "-a");
862 eflag = EFLAG_UNMERGED;
865 eflag = EFLAG_OVERLAP;
874 errx(2, "too many file label options");
875 labels[nblabels++] = optarg;
886 eflag = EFLAG_OVERLAP;
890 eflag = EFLAG_OVERLAP;
897 diffargv[diffargc++] = __DECONST(char *, "--strip-trailing-cr");
903 printf("%s\n", diff3_version);
911 if (eflag == EFLAG_NONE)
912 eflag = EFLAG_UNMERGED;
921 if (caph_limit_stdio() == -1)
922 err(2, "unable to limit stdio");
924 cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
930 e = malloc(2 * sizeof(struct kevent));
940 asprintf(&f1mark, "%s",
941 labels[0] != NULL ? labels[0] : file1);
944 asprintf(&f2mark, "%s",
945 labels[1] != NULL ? labels[1] : file2);
948 asprintf(&f3mark, "%s",
949 labels[2] != NULL ? labels[2] : file3);
953 fp[0] = fopen(file1, "r");
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);
959 fp[1] = fopen(file2, "r");
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);
965 fp[2] = fopen(file3, "r");
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);
976 diffargv[diffargc] = file1;
977 diffargv[diffargc + 1] = file3;
978 diffargv[diffargc + 2] = NULL;
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)
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)
994 caph_cache_catpages();
995 if (caph_enter() < 0)
996 err(2, "unable to enter capability mode");
1000 m = readin(fd13[0], &d13);
1001 n = readin(fd23[0], &d23);
1003 /* waitpid cooked over pdforks */
1005 nke = kevent(kq, NULL, 0, e, nleft, NULL);
1008 for (i = 0; i < nke; i++) {
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",
1020 return (EXIT_SUCCESS);