Annotation of src/usr.bin/cvs/diff3.c, Revision 1.60
1.60 ! millert 1: /* $OpenBSD: diff3.c,v 1.59 2015/11/05 09:48:21 nicm Exp $ */
1.1 joris 2:
3: /*
4: * Copyright (C) Caldera International Inc. 2001-2002.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code and documentation must retain the above
11: * copyright notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed or owned by Caldera
18: * International, Inc.
19: * 4. Neither the name of Caldera International, Inc. nor the names of other
20: * contributors may be used to endorse or promote products derived from
21: * this software without specific prior written permission.
22: *
23: * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24: * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27: * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28: * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33: * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34: * POSSIBILITY OF SUCH DAMAGE.
35: */
36: /*-
37: * Copyright (c) 1991, 1993
38: * The Regents of the University of California. All rights reserved.
39: *
40: * Redistribution and use in source and binary forms, with or without
41: * modification, are permitted provided that the following conditions
42: * are met:
43: * 1. Redistributions of source code must retain the above copyright
44: * notice, this list of conditions and the following disclaimer.
45: * 2. Redistributions in binary form must reproduce the above copyright
46: * notice, this list of conditions and the following disclaimer in the
47: * documentation and/or other materials provided with the distribution.
48: * 3. Neither the name of the University nor the names of its contributors
49: * may be used to endorse or promote products derived from this software
50: * without specific prior written permission.
51: *
52: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62: * SUCH DAMAGE.
63: *
64: * @(#)diff3.c 8.1 (Berkeley) 6/6/93
65: */
66:
1.35 otto 67: #include <ctype.h>
68: #include <errno.h>
69: #include <fcntl.h>
1.38 tobias 70: #include <stdio.h>
1.35 otto 71: #include <stdlib.h>
72: #include <string.h>
73: #include <unistd.h>
1.1 joris 74:
1.39 tobias 75: #include "atomicio.h"
1.1 joris 76: #include "cvs.h"
77: #include "diff.h"
78:
79: /* diff3 - 3-way differential file comparison */
80:
81: /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
82: *
83: * d13 = diff report on f1 vs f3
84: * d23 = diff report on f2 vs f3
85: * f1, f2, f3 the 3 files
86: * if changes in f1 overlap with changes in f3, m1 and m3 are used
87: * to mark the overlaps; otherwise, the file names f1 and f3 are used
88: * (only for options E and X).
89: */
90:
91: /*
92: * "from" is first in range of changed lines; "to" is last+1
93: * from=to=line after point of insertion for added lines.
94: */
95: struct range {
96: int from;
97: int to;
98: };
99:
100: struct diff {
101: struct range old;
102: struct range new;
103: };
104:
105: static size_t szchanges;
106:
1.53 ray 107: static struct diff *d13;
108: static struct diff *d23;
1.1 joris 109:
110: /*
111: * "de" is used to gather editing scripts. These are later spewed out in
112: * reverse order. Its first element must be all zero, the "new" component
113: * of "de" contains line positions or byte positions depending on when you
114: * look (!?). Array overlap indicates which sections in "de" correspond to
115: * lines that are different in all three files.
116: */
1.53 ray 117: static struct diff *de;
118: static char *overlap;
1.2 joris 119: static int overlapcnt = 0;
1.1 joris 120: static FILE *fp[3];
121: static int cline[3]; /* # of the last-read line in each file (0-2) */
122:
123: /*
124: * the latest known correspondence between line numbers of the 3 files
125: * is stored in last[1-3];
126: */
127: static int last[4];
128: static int eflag;
129: static int oflag; /* indicates whether to mark overlaps (-E or -X)*/
130: static int debug = 0;
1.57 deraadt 131: static char f1mark[PATH_MAX], f3mark[PATH_MAX]; /* markers for -E and -X */
1.1 joris 132:
133: static int duplicate(struct range *, struct range *);
134: static int edit(struct diff *, int, int);
135: static char *getchange(FILE *);
1.55 fgsch 136: static char *get_line(FILE *, size_t *);
1.1 joris 137: static int number(char **);
1.42 joris 138: static size_t readin(int, struct diff **);
1.1 joris 139: static int skip(int, int, char *);
140: static int edscript(int);
1.20 ray 141: static int merge(size_t, size_t);
1.1 joris 142: static void change(int, struct range *, int);
143: static void keep(int, struct range *);
144: static void prange(struct range *);
145: static void repos(int);
146: static void separate(const char *);
147: static void increase(void);
148: static int diff3_internal(int, char **, const char *, const char *);
149:
1.5 xsa 150: int diff3_conflicts = 0;
1.47 joris 151: RCSNUM *d3rev1 = NULL;
152: RCSNUM *d3rev2 = NULL;
1.4 joris 153:
1.42 joris 154: static int fds[5];
155:
1.33 joris 156: void
157: cvs_merge_file(struct cvs_file *cf, int verbose)
1.1 joris 158: {
1.42 joris 159: int i, argc;
1.1 joris 160: char *data, *patch;
1.37 xsa 161: char *argv[5], r1[CVS_REV_BUFSZ], r2[CVS_REV_BUFSZ];
1.28 xsa 162: char *dp13, *dp23, *path1, *path2, *path3;
1.54 nicm 163: BUF *b1, *d1, *d2, *diffb;
1.30 niallo 164: size_t dlen, plen;
1.53 ray 165: struct rcs_line *lp;
166: struct rcs_lines *dlines, *plines;
1.1 joris 167:
1.41 joris 168: overlapcnt = 0;
1.54 nicm 169: b1 = d1 = d2 = diffb = NULL;
1.47 joris 170:
171: rcsnum_tostr(d3rev1, r1, sizeof(r1));
172: rcsnum_tostr(d3rev2, r2, sizeof(r2));
1.1 joris 173:
1.53 ray 174: b1 = buf_load_fd(cf->fd);
175: d1 = buf_alloc(128);
176: d2 = buf_alloc(128);
177: diffb = buf_alloc(128);
1.1 joris 178:
1.28 xsa 179: (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
180: (void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
181: (void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", cvs_tmpdir);
182:
1.53 ray 183: fds[2] = buf_write_stmp(b1, path1, NULL);
1.32 niallo 184: if (verbose == 1)
185: cvs_printf("Retrieving revision %s\n", r1);
1.47 joris 186: fds[3] = rcs_rev_write_stmp(cf->file_rcs, d3rev1, path2, 0);
1.32 niallo 187: if (verbose == 1)
188: cvs_printf("Retrieving revision %s\n", r2);
1.47 joris 189: fds[4] = rcs_rev_write_stmp(cf->file_rcs, d3rev2, path3, 0);
1.1 joris 190:
1.50 ray 191: diffreg(path1, path3, fds[2], fds[4], d1, D_FORCEASCII);
192: diffreg(path2, path3, fds[3], fds[4], d2, D_FORCEASCII);
1.1 joris 193:
1.28 xsa 194: (void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", cvs_tmpdir);
1.53 ray 195: fds[0] = buf_write_stmp(d1, dp13, NULL);
196: buf_free(d1);
1.1 joris 197:
1.28 xsa 198: (void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", cvs_tmpdir);
1.53 ray 199: fds[1] = buf_write_stmp(d2, dp23, NULL);
200: buf_free(d2);
1.1 joris 201:
202: argc = 0;
203: diffbuf = diffb;
204: argv[argc++] = dp13;
205: argv[argc++] = dp23;
206: argv[argc++] = path1;
207: argv[argc++] = path2;
208: argv[argc++] = path3;
1.7 joris 209:
1.46 joris 210: if (lseek(fds[2], 0, SEEK_SET) < 0)
1.42 joris 211: fatal("cvs_merge_file: lseek fds[2]: %s", strerror(errno));
1.46 joris 212: if (lseek(fds[3], 0, SEEK_SET) < 0)
1.42 joris 213: fatal("cvs_merge_file: lseek fds[3]: %s", strerror(errno));
1.46 joris 214: if (lseek(fds[4], 0, SEEK_SET) < 0)
1.42 joris 215: fatal("cvs_merge_file: lseek fds[4]: %s", strerror(errno));
216:
1.33 joris 217: diff3_conflicts = diff3_internal(argc, argv, cf->file_path, r2);
218: if (diff3_conflicts < 0)
219: fatal("cvs_merge_file: merging failed for an unknown reason");
1.1 joris 220:
1.53 ray 221: plen = buf_len(diffb);
222: patch = buf_release(diffb);
223: dlen = buf_len(b1);
224: data = buf_release(b1);
1.1 joris 225:
1.47 joris 226: if (verbose == 1)
227: cvs_printf("Merging differences between %s and %s into `%s'\n",
228: r1, r2, cf->file_path);
1.33 joris 229:
230: dlines = cvs_splitlines(data, dlen);
231: plines = cvs_splitlines(patch, plen);
1.47 joris 232:
1.33 joris 233: ed_patch_lines(dlines, plines);
234: cvs_freelines(plines);
1.7 joris 235:
1.23 deraadt 236: if (verbose == 1 && diff3_conflicts != 0) {
1.24 joris 237: cvs_log(LP_ERR, "%d conflict%s found during merge, "
1.16 xsa 238: "please correct.", diff3_conflicts,
1.11 reyk 239: (diff3_conflicts > 1) ? "s" : "");
1.7 joris 240: }
1.1 joris 241:
1.42 joris 242: (void)close(cf->fd);
1.45 joris 243: cf->fd = open(cf->file_path, O_CREAT | O_RDWR | O_TRUNC, 0644);
1.42 joris 244: if (cf->fd == -1) {
245: fatal("cvs_merge_file: failed to reopen fd for writing: %s",
246: strerror(errno));
247: }
248:
1.33 joris 249: TAILQ_FOREACH(lp, &(dlines->l_lines), l_list) {
250: if (lp->l_line == NULL)
251: continue;
252:
1.39 tobias 253: if (atomicio(vwrite, cf->fd, lp->l_line, lp->l_len) !=
254: lp->l_len)
1.33 joris 255: fatal("cvs_merge_file: %s", strerror(errno));
256: }
257:
258: cvs_freelines(dlines);
259:
1.59 nicm 260: free(data);
261: free(patch);
1.1 joris 262:
1.42 joris 263: for (i = 0; i < 3; i++)
264: fclose(fp[i]);
265:
1.52 ray 266: worklist_run(&temp_files, worklist_unlink);
1.28 xsa 267:
1.59 nicm 268: free(path1);
269: free(path2);
270: free(path3);
271: free(dp13);
272: free(dp23);
1.1 joris 273: }
274:
275: static int
276: diff3_internal(int argc, char **argv, const char *fmark, const char *rmark)
277: {
1.20 ray 278: size_t m, n;
279: int i;
1.1 joris 280:
281: eflag = 3;
282: oflag = 1;
283:
284: if (argc < 5)
285: return (-1);
286:
1.34 xsa 287: (void)xsnprintf(f1mark, sizeof(f1mark), "<<<<<<< %s", fmark);
288: (void)xsnprintf(f3mark, sizeof(f3mark), ">>>>>>> %s", rmark);
1.1 joris 289:
1.47 joris 290: szchanges = 0;
291: memset(last, 0, sizeof(last));
292: memset(cline, 0, sizeof(cline));
1.59 nicm 293: free(d13);
294: free(d23);
295: free(overlap);
296: free(de);
1.47 joris 297:
1.60 ! millert 298: de = d13 = d23 = NULL;
! 299: overlap = NULL;
1.47 joris 300:
1.1 joris 301: increase();
1.42 joris 302:
303: /* fds[0] and fds[1] are closed in readin() */
304: m = readin(fds[0], &d13);
305: n = readin(fds[1], &d23);
1.1 joris 306:
307: for (i = 0; i <= 2; i++) {
1.42 joris 308: if ((fp[i] = fdopen(fds[i + 2], "r")) == NULL) {
1.24 joris 309: cvs_log(LP_ERR, "%s", argv[i + 2]);
1.1 joris 310: return (-1);
311: }
312: }
313:
314: return (merge(m, n));
315: }
316:
317: int
1.53 ray 318: ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
1.1 joris 319: {
1.48 joris 320: char op, *ep;
1.53 ray 321: struct rcs_line *sort, *lp, *dlp, *ndlp, *insert_after;
1.19 xsa 322: int start, end, i, lineno;
1.30 niallo 323: u_char tmp;
1.1 joris 324:
325: dlp = TAILQ_FIRST(&(dlines->l_lines));
326: lp = TAILQ_FIRST(&(plines->l_lines));
327:
1.19 xsa 328: end = 0;
1.1 joris 329: for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
330: lp = TAILQ_NEXT(lp, l_list)) {
1.30 niallo 331: /* Skip blank lines */
332: if (lp->l_len < 2)
333: continue;
1.47 joris 334:
1.30 niallo 335: /* NUL-terminate line buffer for strtol() safety. */
336: tmp = lp->l_line[lp->l_len - 1];
337: lp->l_line[lp->l_len - 1] = '\0';
1.47 joris 338:
1.1 joris 339: op = lp->l_line[strlen(lp->l_line) - 1];
340: start = (int)strtol(lp->l_line, &ep, 10);
1.47 joris 341:
1.30 niallo 342: /* Restore the last byte of the buffer */
343: lp->l_line[lp->l_len - 1] = tmp;
1.47 joris 344:
1.1 joris 345: if (op == 'a') {
1.23 deraadt 346: if (start > dlines->l_nblines ||
347: start < 0 || *ep != 'a')
1.47 joris 348: fatal("ed_patch_lines %d", start);
1.1 joris 349: } else if (op == 'c') {
1.23 deraadt 350: if (start > dlines->l_nblines ||
351: start < 0 || (*ep != ',' && *ep != 'c'))
1.15 niallo 352: fatal("ed_patch_lines");
1.1 joris 353:
1.2 joris 354: if (*ep == ',') {
355: ep++;
356: end = (int)strtol(ep, &ep, 10);
1.23 deraadt 357: if (end < 0 || *ep != 'c')
1.15 niallo 358: fatal("ed_patch_lines");
1.2 joris 359: } else {
360: end = start;
361: }
1.47 joris 362: } else {
363: fatal("invalid op %c found while merging", op);
1.1 joris 364: }
365:
1.2 joris 366:
1.1 joris 367: for (;;) {
368: if (dlp == NULL)
369: break;
370: if (dlp->l_lineno == start)
371: break;
372: if (dlp->l_lineno > start) {
1.52 ray 373: dlp = TAILQ_PREV(dlp, tqh, l_list);
1.1 joris 374: } else if (dlp->l_lineno < start) {
375: ndlp = TAILQ_NEXT(dlp, l_list);
376: if (ndlp->l_lineno > start)
377: break;
378: dlp = ndlp;
379: }
380: }
381:
382: if (dlp == NULL)
1.15 niallo 383: fatal("ed_patch_lines");
1.1 joris 384:
1.2 joris 385:
1.1 joris 386: if (op == 'c') {
1.52 ray 387: insert_after = TAILQ_PREV(dlp, tqh, l_list);
1.2 joris 388: for (i = 0; i <= (end - start); i++) {
1.1 joris 389: ndlp = TAILQ_NEXT(dlp, l_list);
390: TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
391: dlp = ndlp;
392: }
1.29 xsa 393: dlp = insert_after;
1.1 joris 394: }
395:
396: if (op == 'a' || op == 'c') {
397: for (;;) {
398: ndlp = lp;
399: lp = TAILQ_NEXT(lp, l_list);
400: if (lp == NULL)
1.15 niallo 401: fatal("ed_patch_lines");
1.1 joris 402:
1.47 joris 403: if (lp->l_len == 2 &&
404: lp->l_line[0] == '.' &&
405: lp->l_line[1] == '\n')
1.1 joris 406: break;
407:
408: TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
409: TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
410: lp, l_list);
411: dlp = lp;
412:
413: lp->l_lineno = start;
414: lp = ndlp;
415: }
416: }
417:
418: /*
419: * always resort lines as the markers might be put at the
420: * same line as we first started editing.
421: */
422: lineno = 0;
423: TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
424: sort->l_lineno = lineno++;
425: dlines->l_nblines = lineno - 1;
426: }
427:
428: return (0);
429: }
430:
431: /*
432: * Pick up the line numbers of all changes from one change file.
433: * (This puts the numbers in a vector, which is not strictly necessary,
434: * since the vector is processed in one sequential pass.
435: * The vector could be optimized out of existence)
436: */
1.20 ray 437: static size_t
1.42 joris 438: readin(int fd, struct diff **dd)
1.1 joris 439: {
440: int a, b, c, d;
441: char kind, *p;
442: size_t i;
443:
1.42 joris 444: fp[0] = fdopen(fd, "r");
445: if (fp[0] == NULL)
446: fatal("readin: fdopen: %s", strerror(errno));
447:
1.1 joris 448: for (i = 0; (p = getchange(fp[0])); i++) {
449: if (i >= szchanges - 1)
450: increase();
451: a = b = number(&p);
452: if (*p == ',') {
453: p++;
454: b = number(&p);
455: }
456: kind = *p++;
457: c = d = number(&p);
458: if (*p==',') {
459: p++;
460: d = number(&p);
461: }
462: if (kind == 'a')
463: a++;
464: if (kind == 'd')
465: c++;
466: b++;
467: d++;
468: (*dd)[i].old.from = a;
469: (*dd)[i].old.to = b;
470: (*dd)[i].new.from = c;
471: (*dd)[i].new.to = d;
472: }
473:
1.14 xsa 474: if (i) {
475: (*dd)[i].old.from = (*dd)[i-1].old.to;
476: (*dd)[i].new.from = (*dd)[i-1].new.to;
477: }
1.42 joris 478:
1.1 joris 479: (void)fclose(fp[0]);
480:
481: return (i);
482: }
483:
484: static int
485: number(char **lc)
486: {
487: int nn;
488:
489: nn = 0;
490: while (isdigit((unsigned char)(**lc)))
491: nn = nn*10 + *(*lc)++ - '0';
492:
493: return (nn);
494: }
495:
496: static char *
497: getchange(FILE *b)
498: {
499: char *line;
500:
1.55 fgsch 501: while ((line = get_line(b, NULL))) {
1.1 joris 502: if (isdigit((unsigned char)line[0]))
503: return (line);
504: }
505:
506: return (NULL);
507: }
508:
509: static char *
1.55 fgsch 510: get_line(FILE *b, size_t *n)
1.1 joris 511: {
512: char *cp;
513: size_t len;
514: static char *buf;
515: static size_t bufsize;
516:
517: if ((cp = fgetln(b, &len)) == NULL)
518: return (NULL);
519:
520: if (cp[len - 1] != '\n')
521: len++;
522: if (len + 1 > bufsize) {
523: do {
524: bufsize += 1024;
525: } while (len + 1 > bufsize);
1.56 deraadt 526: buf = xreallocarray(buf, 1, bufsize);
1.1 joris 527: }
528: memcpy(buf, cp, len - 1);
529: buf[len - 1] = '\n';
530: buf[len] = '\0';
531: if (n != NULL)
532: *n = len;
533:
534: return (buf);
535: }
536:
537: static int
1.20 ray 538: merge(size_t m1, size_t m2)
1.1 joris 539: {
540: struct diff *d1, *d2, *d3;
541: int dpl, j, t1, t2;
542:
543: d1 = d13;
544: d2 = d23;
545: j = 0;
1.58 jsg 546: while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
1.1 joris 547: if (debug) {
548: printf("%d,%d=%d,%d %d,%d=%d,%d\n",
1.20 ray 549: d1->old.from, d1->old.to,
550: d1->new.from, d1->new.to,
551: d2->old.from, d2->old.to,
552: d2->new.from, d2->new.to);
1.1 joris 553: }
554:
555: /* first file is different from others */
556: if (!t2 || (t1 && d1->new.to < d2->new.from)) {
557: /* stuff peculiar to 1st file */
558: if (eflag==0) {
559: separate("1");
560: change(1, &d1->old, 0);
561: keep(2, &d1->new);
562: change(3, &d1->new, 0);
563: }
564: d1++;
565: continue;
566: }
567:
568: /* second file is different from others */
569: if (!t1 || (t2 && d2->new.to < d1->new.from)) {
570: if (eflag==0) {
571: separate("2");
572: keep(1, &d2->new);
573: change(2, &d2->old, 0);
574: change(3, &d2->new, 0);
575: }
576: d2++;
577: continue;
578: }
579:
580: /*
581: * Merge overlapping changes in first file
582: * this happens after extension (see below).
583: */
584: if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
585: d1[1].old.from = d1->old.from;
586: d1[1].new.from = d1->new.from;
587: d1++;
588: continue;
589: }
590:
591: /* merge overlapping changes in second */
592: if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
593: d2[1].old.from = d2->old.from;
594: d2[1].new.from = d2->new.from;
595: d2++;
596: continue;
597: }
598: /* stuff peculiar to third file or different in all */
599: if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
600: dpl = duplicate(&d1->old,&d2->old);
601: if (dpl == -1)
602: return (-1);
603:
604: /*
605: * dpl = 0 means all files differ
606: * dpl = 1 means files 1 and 2 identical
607: */
608: if (eflag==0) {
609: separate(dpl ? "3" : "");
610: change(1, &d1->old, dpl);
611: change(2, &d2->old, 0);
612: d3 = d1->old.to > d1->old.from ? d1 : d2;
613: change(3, &d3->new, 0);
614: } else
615: j = edit(d1, dpl, j);
616: d1++;
617: d2++;
618: continue;
619: }
620:
621: /*
622: * Overlapping changes from file 1 and 2; extend changes
623: * appropriately to make them coincide.
624: */
625: if (d1->new.from < d2->new.from) {
626: d2->old.from -= d2->new.from-d1->new.from;
627: d2->new.from = d1->new.from;
628: } else if (d2->new.from < d1->new.from) {
629: d1->old.from -= d1->new.from-d2->new.from;
630: d1->new.from = d2->new.from;
631: }
632: if (d1->new.to > d2->new.to) {
633: d2->old.to += d1->new.to - d2->new.to;
634: d2->new.to = d1->new.to;
635: } else if (d2->new.to > d1->new.to) {
636: d1->old.to += d2->new.to - d1->new.to;
637: d1->new.to = d2->new.to;
638: }
639: }
640:
641: return (edscript(j));
642: }
643:
644: static void
645: separate(const char *s)
646: {
647: diff_output("====%s\n", s);
648: }
649:
650: /*
651: * The range of lines rold.from thru rold.to in file i is to be changed.
652: * It is to be printed only if it does not duplicate something to be
653: * printed later.
654: */
655: static void
656: change(int i, struct range *rold, int fdup)
657: {
658: diff_output("%d:", i);
659: last[i] = rold->to;
660: prange(rold);
661: if (fdup || debug)
662: return;
663: i--;
664: (void)skip(i, rold->from, NULL);
665: (void)skip(i, rold->to, " ");
666: }
667:
668: /*
669: * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
670: */
671: static void
672: prange(struct range *rold)
673: {
674: if (rold->to <= rold->from)
675: diff_output("%da\n", rold->from - 1);
676: else {
677: diff_output("%d", rold->from);
678: if (rold->to > rold->from+1)
679: diff_output(",%d", rold->to - 1);
680: diff_output("c\n");
681: }
682: }
683:
684: /*
685: * No difference was reported by diff between file 1 (or 2) and file 3,
686: * and an artificial dummy difference (trange) must be ginned up to
687: * correspond to the change reported in the other file.
688: */
689: static void
690: keep(int i, struct range *rnew)
691: {
692: int delta;
693: struct range trange;
694:
695: delta = last[3] - last[i];
696: trange.from = rnew->from - delta;
697: trange.to = rnew->to - delta;
698: change(i, &trange, 1);
699: }
700:
701: /*
702: * skip to just before line number from in file "i". If "pr" is non-NULL,
703: * print all skipped stuff with string pr as a prefix.
704: */
705: static int
706: skip(int i, int from, char *pr)
707: {
708: size_t j, n;
709: char *line;
710:
711: for (n = 0; cline[i] < from - 1; n += j) {
1.55 fgsch 712: if ((line = get_line(fp[i], &j)) == NULL)
1.1 joris 713: return (-1);
714: if (pr != NULL)
715: diff_output("%s%s", pr, line);
716: cline[i]++;
717: }
718: return ((int) n);
719: }
720:
721: /*
722: * Return 1 or 0 according as the old range (in file 1) contains exactly
723: * the same data as the new range (in file 2).
724: */
725: static int
726: duplicate(struct range *r1, struct range *r2)
727: {
728: int c,d;
729: int nchar;
730: int nline;
731:
732: if (r1->to-r1->from != r2->to-r2->from)
733: return (0);
734: (void)skip(0, r1->from, NULL);
735: (void)skip(1, r2->from, NULL);
736: nchar = 0;
737: for (nline=0; nline < r1->to - r1->from; nline++) {
738: do {
739: c = getc(fp[0]);
740: d = getc(fp[1]);
741: if (c == -1 || d== -1)
742: return (-1);
743: nchar++;
744: if (c != d) {
745: repos(nchar);
746: return (0);
747: }
748: } while (c != '\n');
749: }
750: repos(nchar);
751: return (1);
752: }
753:
754: static void
755: repos(int nchar)
756: {
757: int i;
758:
759: for (i = 0; i < 2; i++)
1.38 tobias 760: (void)fseek(fp[i], (long)-nchar, SEEK_CUR);
1.1 joris 761: }
762:
763: /*
764: * collect an editing script for later regurgitation
765: */
766: static int
767: edit(struct diff *diff, int fdup, int j)
768: {
769: if (((fdup + 1) & eflag) == 0)
770: return (j);
771: j++;
772: overlap[j] = !fdup;
773: if (!fdup)
774: overlapcnt++;
775: de[j].old.from = diff->old.from;
776: de[j].old.to = diff->old.to;
777: de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
778: de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
779: return (j);
780: }
781:
782: /* regurgitate */
783: static int
784: edscript(int n)
785: {
786: int j, k;
1.2 joris 787: char block[BUFSIZ+1];
1.1 joris 788:
789: for (n = n; n > 0; n--) {
790: if (!oflag || !overlap[n])
791: prange(&de[n].old);
792: else
793: diff_output("%da\n=======\n", de[n].old.to -1);
1.38 tobias 794: (void)fseek(fp[2], (long)de[n].new.from, SEEK_SET);
1.1 joris 795: for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
796: j = k > BUFSIZ ? BUFSIZ : k;
1.60 ! millert 797: if (fread(block, 1, j, fp[2]) != (size_t)j)
1.1 joris 798: return (-1);
799: block[j] = '\0';
800: diff_output("%s", block);
801: }
1.2 joris 802:
1.1 joris 803: if (!oflag || !overlap[n])
804: diff_output(".\n");
805: else {
806: diff_output("%s\n.\n", f3mark);
807: diff_output("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
808: }
809: }
810:
811: return (overlapcnt);
812: }
813:
814: static void
815: increase(void)
816: {
817: size_t newsz, incr;
818:
819: /* are the memset(3) calls needed? */
820: newsz = szchanges == 0 ? 64 : 2 * szchanges;
821: incr = newsz - szchanges;
822:
1.56 deraadt 823: d13 = xreallocarray(d13, newsz, sizeof(*d13));
1.36 ray 824: memset(d13 + szchanges, 0, incr * sizeof(*d13));
1.56 deraadt 825: d23 = xreallocarray(d23, newsz, sizeof(*d23));
1.36 ray 826: memset(d23 + szchanges, 0, incr * sizeof(*d23));
1.56 deraadt 827: de = xreallocarray(de, newsz, sizeof(*de));
1.36 ray 828: memset(de + szchanges, 0, incr * sizeof(*de));
1.56 deraadt 829: overlap = xreallocarray(overlap, newsz, sizeof(*overlap));
1.36 ray 830: memset(overlap + szchanges, 0, incr * sizeof(*overlap));
1.1 joris 831: szchanges = newsz;
832: }