1 /* $OpenBSD: diff3.c,v 1.41 2016/10/18 21:06:52 millert 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
78 /* diff3 - 3-way differential file comparison */
80 /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
82 * d13 = diff report on f1 vs f3
83 * d23 = diff report on f2 vs f3
84 * f1, f2, f3 the 3 files
85 * if changes in f1 overlap with changes in f3, m1 and m3 are used
86 * to mark the overlaps; otherwise, the file names f1 and f3 are used
87 * (only for options E and X).
91 * "from" is first in range of changed lines; "to" is last+1
92 * from=to=line after point of insertion for added lines.
104 static size_t szchanges;
106 static struct diff *d13;
107 static struct diff *d23;
110 * "de" is used to gather editing scripts. These are later spewed out in
111 * reverse order. Its first element must be all zero, the "new" component
112 * of "de" contains line positions or byte positions depending on when you
113 * look (!?). Array overlap indicates which sections in "de" correspond to
114 * lines that are different in all three files.
116 static struct diff *de;
117 static char *overlap;
118 static int overlapcnt = 0;
120 static int cline[3]; /* # of the last-read line in each file (0-2) */
123 * the latest known correspondence between line numbers of the 3 files
124 * is stored in last[1-3];
127 static int eflag = 3; /* default -E for compatibility with former RCS */
128 static int oflag = 1; /* default -E for compatibility with former RCS */
129 static int debug = 0;
130 static char f1mark[PATH_MAX], f3mark[PATH_MAX]; /* markers for -E and -X */
132 static int duplicate(struct range *, struct range *);
133 static int edit(struct diff *, int, int);
134 static char *getchange(FILE *);
135 static char *get_line(FILE *, size_t *);
136 static int number(char **);
137 static ssize_t readin(char *, struct diff **);
138 static int skip(int, int, char *);
139 static int edscript(int);
140 static int merge(size_t, size_t);
141 static void change(int, struct range *, int);
142 static void keep(int, struct range *);
143 static void prange(struct range *);
144 static void repos(int);
145 static void separate(const char *);
146 static void increase(void);
147 static int diff3_internal(int, char **, const char *, const char *);
149 int diff3_conflicts = 0;
155 merge_diff3(char **av, int flags)
158 char *argv[5], *dp13, *dp23, *path1, *path2, *path3;
159 BUF *b1, *b2, *b3, *d1, *d2, *diffb;
160 u_char *data, *patch;
163 b1 = b2 = b3 = d1 = d2 = diffb = NULL;
164 dp13 = dp23 = path1 = path2 = path3 = NULL;
167 if ((flags & MERGE_EFLAG) && !(flags & MERGE_OFLAG))
170 if ((b1 = buf_load(av[0])) == NULL)
172 if ((b2 = buf_load(av[1])) == NULL)
174 if ((b3 = buf_load(av[2])) == NULL)
179 diffb = buf_alloc(128);
181 (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
182 (void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
183 (void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", rcs_tmpdir);
185 buf_write_stmp(b1, path1);
186 buf_write_stmp(b2, path2);
187 buf_write_stmp(b3, path3);
192 if ((diffreg(path1, path3, d1, D_FORCEASCII) == D_ERROR) ||
193 (diffreg(path2, path3, d2, D_FORCEASCII) == D_ERROR)) {
199 (void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
200 buf_write_stmp(d1, dp13);
205 (void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
206 buf_write_stmp(d2, dp23);
215 argv[argc++] = path1;
216 argv[argc++] = path2;
217 argv[argc++] = path3;
219 diff3_conflicts = diff3_internal(argc, argv, av[0], av[2]);
220 if (diff3_conflicts < 0) {
226 plen = buf_len(diffb);
227 patch = buf_release(diffb);
229 data = buf_release(b1);
231 if ((diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
234 if (!(flags & QUIET) && diff3_conflicts != 0)
235 warnx("warning: overlaps or other problems during merge");
261 rcs_diff3(RCSFILE *rf, char *workfile, RCSNUM *rev1, RCSNUM *rev2, int flags)
264 char *argv[5], r1[RCS_REV_BUFSZ], r2[RCS_REV_BUFSZ];
265 char *dp13, *dp23, *path1, *path2, *path3;
266 BUF *b1, *b2, *b3, *d1, *d2, *diffb;
268 u_char *data, *patch;
270 b1 = b2 = b3 = d1 = d2 = diffb = NULL;
271 dp13 = dp23 = path1 = path2 = path3 = NULL;
274 if ((flags & MERGE_EFLAG) && !(flags & MERGE_OFLAG))
277 rcsnum_tostr(rev1, r1, sizeof(r1));
278 rcsnum_tostr(rev2, r2, sizeof(r2));
280 if ((b1 = buf_load(workfile)) == NULL)
283 if (!(flags & QUIET))
284 (void)fprintf(stderr, "retrieving revision %s\n", r1);
285 if ((b2 = rcs_getrev(rf, rev1)) == NULL)
288 if (!(flags & QUIET))
289 (void)fprintf(stderr, "retrieving revision %s\n", r2);
290 if ((b3 = rcs_getrev(rf, rev2)) == NULL)
295 diffb = buf_alloc(128);
297 (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
298 (void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
299 (void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", rcs_tmpdir);
301 buf_write_stmp(b1, path1);
302 buf_write_stmp(b2, path2);
303 buf_write_stmp(b3, path3);
308 if ((diffreg(path1, path3, d1, D_FORCEASCII) == D_ERROR) ||
309 (diffreg(path2, path3, d2, D_FORCEASCII) == D_ERROR)) {
315 (void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
316 buf_write_stmp(d1, dp13);
321 (void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
322 buf_write_stmp(d2, dp23);
331 argv[argc++] = path1;
332 argv[argc++] = path2;
333 argv[argc++] = path3;
335 diff3_conflicts = diff3_internal(argc, argv, workfile, r2);
336 if (diff3_conflicts < 0) {
342 plen = buf_len(diffb);
343 patch = buf_release(diffb);
345 data = buf_release(b1);
347 if ((diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
350 if (!(flags & QUIET) && diff3_conflicts != 0)
351 warnx("warning: overlaps or other problems during merge");
377 diff3_internal(int argc, char **argv, const char *fmark, const char *rmark)
386 i = snprintf(f1mark, sizeof(f1mark), "<<<<<<< %s", fmark);
387 if (i < 0 || i >= (int)sizeof(f1mark))
388 errx(1, "diff3_internal: string truncated");
390 i = snprintf(f3mark, sizeof(f3mark), ">>>>>>> %s", rmark);
391 if (i < 0 || i >= (int)sizeof(f3mark))
392 errx(1, "diff3_internal: string truncated");
396 if ((m = readin(argv[0], &d13)) < 0) {
400 if ((n = readin(argv[1], &d23)) < 0) {
405 for (i = 0; i <= 2; i++)
406 if ((fp[i] = fopen(argv[i + 2], "r")) == NULL) {
407 warn("%s", argv[i + 2]);
411 return (merge(m, n));
415 ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
418 struct rcs_line *sort, *lp, *dlp, *ndlp, *insert_after;
419 int start, end, i, lineno;
422 dlp = TAILQ_FIRST(&(dlines->l_lines));
423 lp = TAILQ_FIRST(&(plines->l_lines));
426 for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
427 lp = TAILQ_NEXT(lp, l_list)) {
428 /* Skip blank lines */
432 /* NUL-terminate line buffer for strtol() safety. */
433 tmp = lp->l_line[lp->l_len - 1];
434 lp->l_line[lp->l_len - 1] = '\0';
436 /* len - 1 is NUL terminator so we use len - 2 for 'op' */
437 op = lp->l_line[lp->l_len - 2];
438 start = (int)strtol(lp->l_line, &ep, 10);
440 /* Restore the last byte of the buffer */
441 lp->l_line[lp->l_len - 1] = tmp;
444 if (start > dlines->l_nblines ||
445 start < 0 || *ep != 'a')
446 errx(1, "ed_patch_lines");
447 } else if (op == 'c') {
448 if (start > dlines->l_nblines ||
449 start < 0 || (*ep != ',' && *ep != 'c'))
450 errx(1, "ed_patch_lines");
454 end = (int)strtol(ep, &ep, 10);
455 if (end < 0 || *ep != 'c')
456 errx(1, "ed_patch_lines");
466 if (dlp->l_lineno == start)
468 if (dlp->l_lineno > start) {
469 dlp = TAILQ_PREV(dlp, tqh, l_list);
470 } else if (dlp->l_lineno < start) {
471 ndlp = TAILQ_NEXT(dlp, l_list);
472 if (ndlp->l_lineno > start)
479 errx(1, "ed_patch_lines");
483 insert_after = TAILQ_PREV(dlp, tqh, l_list);
484 for (i = 0; i <= (end - start); i++) {
485 ndlp = TAILQ_NEXT(dlp, l_list);
486 TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
492 if (op == 'a' || op == 'c') {
495 lp = TAILQ_NEXT(lp, l_list);
497 errx(1, "ed_patch_lines");
499 if (!memcmp(lp->l_line, ".", 1))
502 TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
503 TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
507 lp->l_lineno = start;
513 * always resort lines as the markers might be put at the
514 * same line as we first started editing.
517 TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
518 sort->l_lineno = lineno++;
519 dlines->l_nblines = lineno - 1;
526 * Pick up the line numbers of all changes from one change file.
527 * (This puts the numbers in a vector, which is not strictly necessary,
528 * since the vector is processed in one sequential pass.
529 * The vector could be optimized out of existence)
532 readin(char *name, struct diff **dd)
538 fp[0] = fopen(name, "r");
541 for (i = 0; (p = getchange(fp[0])); i++) {
542 if (i >= szchanges - 1)
561 (*dd)[i].old.from = a;
563 (*dd)[i].new.from = c;
568 (*dd)[i].old.from = (*dd)[i-1].old.to;
569 (*dd)[i].new.from = (*dd)[i-1].new.to;
583 while (isdigit((unsigned char)(**lc)))
584 nn = nn*10 + *(*lc)++ - '0';
594 while ((line = get_line(b, NULL))) {
595 if (isdigit((unsigned char)line[0]))
603 get_line(FILE *b, size_t *n)
608 static size_t bufsize;
610 if ((cp = fgetln(b, &len)) == NULL)
613 if (cp[len - 1] != '\n')
615 if (len + 1 > bufsize) {
618 } while (len + 1 > bufsize);
619 buf = xreallocarray(buf, 1, bufsize);
621 memcpy(buf, cp, len - 1);
631 merge(size_t m1, size_t m2)
633 struct diff *d1, *d2, *d3;
640 t1 = (d1 < d13 + m1);
641 t2 = (d2 < d23 + m2);
646 printf("%d,%d=%d,%d %d,%d=%d,%d\n",
647 d1->old.from, d1->old.to,
648 d1->new.from, d1->new.to,
649 d2->old.from, d2->old.to,
650 d2->new.from, d2->new.to);
653 /* first file is different from others */
654 if (!t2 || (t1 && d1->new.to < d2->new.from)) {
655 /* stuff peculiar to 1st file */
658 change(1, &d1->old, 0);
660 change(3, &d1->new, 0);
666 /* second file is different from others */
667 if (!t1 || (t2 && d2->new.to < d1->new.from)) {
671 change(2, &d2->old, 0);
672 change(3, &d2->new, 0);
679 * Merge overlapping changes in first file
680 * this happens after extension (see below).
682 if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
683 d1[1].old.from = d1->old.from;
684 d1[1].new.from = d1->new.from;
689 /* merge overlapping changes in second */
690 if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
691 d2[1].old.from = d2->old.from;
692 d2[1].new.from = d2->new.from;
696 /* stuff peculiar to third file or different in all */
697 if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
698 dpl = duplicate(&d1->old,&d2->old);
703 * dpl = 0 means all files differ
704 * dpl = 1 means files 1 and 2 identical
707 separate(dpl ? "3" : "");
708 change(1, &d1->old, dpl);
709 change(2, &d2->old, 0);
710 d3 = d1->old.to > d1->old.from ? d1 : d2;
711 change(3, &d3->new, 0);
713 j = edit(d1, dpl, j);
720 * Overlapping changes from file 1 and 2; extend changes
721 * appropriately to make them coincide.
723 if (d1->new.from < d2->new.from) {
724 d2->old.from -= d2->new.from-d1->new.from;
725 d2->new.from = d1->new.from;
726 } else if (d2->new.from < d1->new.from) {
727 d1->old.from -= d1->new.from-d2->new.from;
728 d1->new.from = d2->new.from;
730 if (d1->new.to > d2->new.to) {
731 d2->old.to += d1->new.to - d2->new.to;
732 d2->new.to = d1->new.to;
733 } else if (d2->new.to > d1->new.to) {
734 d1->old.to += d2->new.to - d1->new.to;
735 d1->new.to = d2->new.to;
739 return (edscript(j));
743 separate(const char *s)
745 diff_output("====%s\n", s);
749 * The range of lines rold.from thru rold.to in file i is to be changed.
750 * It is to be printed only if it does not duplicate something to be
754 change(int i, struct range *rold, int fdup)
756 diff_output("%d:", i);
762 (void)skip(i, rold->from, NULL);
763 (void)skip(i, rold->to, " ");
767 * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
770 prange(struct range *rold)
772 if (rold->to <= rold->from)
773 diff_output("%da\n", rold->from - 1);
775 diff_output("%d", rold->from);
776 if (rold->to > rold->from+1)
777 diff_output(",%d", rold->to - 1);
783 * No difference was reported by diff between file 1 (or 2) and file 3,
784 * and an artificial dummy difference (trange) must be ginned up to
785 * correspond to the change reported in the other file.
788 keep(int i, struct range *rnew)
793 delta = last[3] - last[i];
794 trange.from = rnew->from - delta;
795 trange.to = rnew->to - delta;
796 change(i, &trange, 1);
800 * skip to just before line number from in file "i". If "pr" is non-NULL,
801 * print all skipped stuff with string pr as a prefix.
804 skip(int i, int from, char *pr)
809 for (n = 0; cline[i] < from - 1; n += j) {
810 if ((line = get_line(fp[i], &j)) == NULL)
813 diff_output("%s%s", pr, line);
820 * Return 1 or 0 according as the old range (in file 1) contains exactly
821 * the same data as the new range (in file 2).
824 duplicate(struct range *r1, struct range *r2)
830 if (r1->to-r1->from != r2->to-r2->from)
832 (void)skip(0, r1->from, NULL);
833 (void)skip(1, r2->from, NULL);
835 for (nline=0; nline < r1->to - r1->from; nline++) {
839 if (c == -1 || d== -1)
857 for (i = 0; i < 2; i++)
858 (void)fseek(fp[i], (long)-nchar, SEEK_CUR);
862 * collect an editing script for later regurgitation
865 edit(struct diff *diff, int fdup, int j)
867 if (((fdup + 1) & eflag) == 0)
873 de[j].old.from = diff->old.from;
874 de[j].old.to = diff->old.to;
875 de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
876 de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
885 char block[BUFSIZ+1];
888 if (!oflag || !overlap[n])
891 diff_output("%da\n=======\n", de[n].old.to -1);
892 (void)fseek(fp[2], (long)de[n].new.from, SEEK_SET);
893 for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
894 j = k > BUFSIZ ? BUFSIZ : k;
895 if (fread(block, 1, j, fp[2]) != (size_t)j)
898 diff_output("%s", block);
901 if (!oflag || !overlap[n])
904 diff_output("%s\n.\n", f3mark);
905 diff_output("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
917 /* are the memset(3) calls needed? */
918 newsz = szchanges == 0 ? 64 : 2 * szchanges;
919 incr = newsz - szchanges;
921 d13 = xreallocarray(d13, newsz, sizeof(*d13));
922 memset(d13 + szchanges, 0, incr * sizeof(*d13));
923 d23 = xreallocarray(d23, newsz, sizeof(*d23));
924 memset(d23 + szchanges, 0, incr * sizeof(*d23));
925 de = xreallocarray(de, newsz, sizeof(*de));
926 memset(de + szchanges, 0, incr * sizeof(*de));
927 overlap = xreallocarray(overlap, newsz, sizeof(*overlap));
928 memset(overlap + szchanges, 0, incr * sizeof(*overlap));