Annotation of src/usr.bin/cvs/diff3.c, Revision 1.59
1.59 ! nicm 1: /* $OpenBSD: diff3.c,v 1.58 2015/09/05 09:47:08 jsg 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.59 ! nicm 298: de = d13 = d23 = overlap = NULL;
1.47 joris 299:
1.1 joris 300: increase();
1.42 joris 301:
302: /* fds[0] and fds[1] are closed in readin() */
303: m = readin(fds[0], &d13);
304: n = readin(fds[1], &d23);
1.1 joris 305:
306: for (i = 0; i <= 2; i++) {
1.42 joris 307: if ((fp[i] = fdopen(fds[i + 2], "r")) == NULL) {
1.24 joris 308: cvs_log(LP_ERR, "%s", argv[i + 2]);
1.1 joris 309: return (-1);
310: }
311: }
312:
313: return (merge(m, n));
314: }
315:
316: int
1.53 ray 317: ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
1.1 joris 318: {
1.48 joris 319: char op, *ep;
1.53 ray 320: struct rcs_line *sort, *lp, *dlp, *ndlp, *insert_after;
1.19 xsa 321: int start, end, i, lineno;
1.30 niallo 322: u_char tmp;
1.1 joris 323:
324: dlp = TAILQ_FIRST(&(dlines->l_lines));
325: lp = TAILQ_FIRST(&(plines->l_lines));
326:
1.19 xsa 327: end = 0;
1.1 joris 328: for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
329: lp = TAILQ_NEXT(lp, l_list)) {
1.30 niallo 330: /* Skip blank lines */
331: if (lp->l_len < 2)
332: continue;
1.47 joris 333:
1.30 niallo 334: /* NUL-terminate line buffer for strtol() safety. */
335: tmp = lp->l_line[lp->l_len - 1];
336: lp->l_line[lp->l_len - 1] = '\0';
1.47 joris 337:
1.1 joris 338: op = lp->l_line[strlen(lp->l_line) - 1];
339: start = (int)strtol(lp->l_line, &ep, 10);
1.47 joris 340:
1.30 niallo 341: /* Restore the last byte of the buffer */
342: lp->l_line[lp->l_len - 1] = tmp;
1.47 joris 343:
1.1 joris 344: if (op == 'a') {
1.23 deraadt 345: if (start > dlines->l_nblines ||
346: start < 0 || *ep != 'a')
1.47 joris 347: fatal("ed_patch_lines %d", start);
1.1 joris 348: } else if (op == 'c') {
1.23 deraadt 349: if (start > dlines->l_nblines ||
350: start < 0 || (*ep != ',' && *ep != 'c'))
1.15 niallo 351: fatal("ed_patch_lines");
1.1 joris 352:
1.2 joris 353: if (*ep == ',') {
354: ep++;
355: end = (int)strtol(ep, &ep, 10);
1.23 deraadt 356: if (end < 0 || *ep != 'c')
1.15 niallo 357: fatal("ed_patch_lines");
1.2 joris 358: } else {
359: end = start;
360: }
1.47 joris 361: } else {
362: fatal("invalid op %c found while merging", op);
1.1 joris 363: }
364:
1.2 joris 365:
1.1 joris 366: for (;;) {
367: if (dlp == NULL)
368: break;
369: if (dlp->l_lineno == start)
370: break;
371: if (dlp->l_lineno > start) {
1.52 ray 372: dlp = TAILQ_PREV(dlp, tqh, l_list);
1.1 joris 373: } else if (dlp->l_lineno < start) {
374: ndlp = TAILQ_NEXT(dlp, l_list);
375: if (ndlp->l_lineno > start)
376: break;
377: dlp = ndlp;
378: }
379: }
380:
381: if (dlp == NULL)
1.15 niallo 382: fatal("ed_patch_lines");
1.1 joris 383:
1.2 joris 384:
1.1 joris 385: if (op == 'c') {
1.52 ray 386: insert_after = TAILQ_PREV(dlp, tqh, l_list);
1.2 joris 387: for (i = 0; i <= (end - start); i++) {
1.1 joris 388: ndlp = TAILQ_NEXT(dlp, l_list);
389: TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
390: dlp = ndlp;
391: }
1.29 xsa 392: dlp = insert_after;
1.1 joris 393: }
394:
395: if (op == 'a' || op == 'c') {
396: for (;;) {
397: ndlp = lp;
398: lp = TAILQ_NEXT(lp, l_list);
399: if (lp == NULL)
1.15 niallo 400: fatal("ed_patch_lines");
1.1 joris 401:
1.47 joris 402: if (lp->l_len == 2 &&
403: lp->l_line[0] == '.' &&
404: lp->l_line[1] == '\n')
1.1 joris 405: break;
406:
407: TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
408: TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
409: lp, l_list);
410: dlp = lp;
411:
412: lp->l_lineno = start;
413: lp = ndlp;
414: }
415: }
416:
417: /*
418: * always resort lines as the markers might be put at the
419: * same line as we first started editing.
420: */
421: lineno = 0;
422: TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
423: sort->l_lineno = lineno++;
424: dlines->l_nblines = lineno - 1;
425: }
426:
427: return (0);
428: }
429:
430: /*
431: * Pick up the line numbers of all changes from one change file.
432: * (This puts the numbers in a vector, which is not strictly necessary,
433: * since the vector is processed in one sequential pass.
434: * The vector could be optimized out of existence)
435: */
1.20 ray 436: static size_t
1.42 joris 437: readin(int fd, struct diff **dd)
1.1 joris 438: {
439: int a, b, c, d;
440: char kind, *p;
441: size_t i;
442:
1.42 joris 443: fp[0] = fdopen(fd, "r");
444: if (fp[0] == NULL)
445: fatal("readin: fdopen: %s", strerror(errno));
446:
1.1 joris 447: for (i = 0; (p = getchange(fp[0])); i++) {
448: if (i >= szchanges - 1)
449: increase();
450: a = b = number(&p);
451: if (*p == ',') {
452: p++;
453: b = number(&p);
454: }
455: kind = *p++;
456: c = d = number(&p);
457: if (*p==',') {
458: p++;
459: d = number(&p);
460: }
461: if (kind == 'a')
462: a++;
463: if (kind == 'd')
464: c++;
465: b++;
466: d++;
467: (*dd)[i].old.from = a;
468: (*dd)[i].old.to = b;
469: (*dd)[i].new.from = c;
470: (*dd)[i].new.to = d;
471: }
472:
1.14 xsa 473: if (i) {
474: (*dd)[i].old.from = (*dd)[i-1].old.to;
475: (*dd)[i].new.from = (*dd)[i-1].new.to;
476: }
1.42 joris 477:
1.1 joris 478: (void)fclose(fp[0]);
479:
480: return (i);
481: }
482:
483: static int
484: number(char **lc)
485: {
486: int nn;
487:
488: nn = 0;
489: while (isdigit((unsigned char)(**lc)))
490: nn = nn*10 + *(*lc)++ - '0';
491:
492: return (nn);
493: }
494:
495: static char *
496: getchange(FILE *b)
497: {
498: char *line;
499:
1.55 fgsch 500: while ((line = get_line(b, NULL))) {
1.1 joris 501: if (isdigit((unsigned char)line[0]))
502: return (line);
503: }
504:
505: return (NULL);
506: }
507:
508: static char *
1.55 fgsch 509: get_line(FILE *b, size_t *n)
1.1 joris 510: {
511: char *cp;
512: size_t len;
513: static char *buf;
514: static size_t bufsize;
515:
516: if ((cp = fgetln(b, &len)) == NULL)
517: return (NULL);
518:
519: if (cp[len - 1] != '\n')
520: len++;
521: if (len + 1 > bufsize) {
522: do {
523: bufsize += 1024;
524: } while (len + 1 > bufsize);
1.56 deraadt 525: buf = xreallocarray(buf, 1, bufsize);
1.1 joris 526: }
527: memcpy(buf, cp, len - 1);
528: buf[len - 1] = '\n';
529: buf[len] = '\0';
530: if (n != NULL)
531: *n = len;
532:
533: return (buf);
534: }
535:
536: static int
1.20 ray 537: merge(size_t m1, size_t m2)
1.1 joris 538: {
539: struct diff *d1, *d2, *d3;
540: int dpl, j, t1, t2;
541:
542: d1 = d13;
543: d2 = d23;
544: j = 0;
1.58 jsg 545: while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
1.1 joris 546: if (debug) {
547: printf("%d,%d=%d,%d %d,%d=%d,%d\n",
1.20 ray 548: d1->old.from, d1->old.to,
549: d1->new.from, d1->new.to,
550: d2->old.from, d2->old.to,
551: d2->new.from, d2->new.to);
1.1 joris 552: }
553:
554: /* first file is different from others */
555: if (!t2 || (t1 && d1->new.to < d2->new.from)) {
556: /* stuff peculiar to 1st file */
557: if (eflag==0) {
558: separate("1");
559: change(1, &d1->old, 0);
560: keep(2, &d1->new);
561: change(3, &d1->new, 0);
562: }
563: d1++;
564: continue;
565: }
566:
567: /* second file is different from others */
568: if (!t1 || (t2 && d2->new.to < d1->new.from)) {
569: if (eflag==0) {
570: separate("2");
571: keep(1, &d2->new);
572: change(2, &d2->old, 0);
573: change(3, &d2->new, 0);
574: }
575: d2++;
576: continue;
577: }
578:
579: /*
580: * Merge overlapping changes in first file
581: * this happens after extension (see below).
582: */
583: if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
584: d1[1].old.from = d1->old.from;
585: d1[1].new.from = d1->new.from;
586: d1++;
587: continue;
588: }
589:
590: /* merge overlapping changes in second */
591: if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
592: d2[1].old.from = d2->old.from;
593: d2[1].new.from = d2->new.from;
594: d2++;
595: continue;
596: }
597: /* stuff peculiar to third file or different in all */
598: if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
599: dpl = duplicate(&d1->old,&d2->old);
600: if (dpl == -1)
601: return (-1);
602:
603: /*
604: * dpl = 0 means all files differ
605: * dpl = 1 means files 1 and 2 identical
606: */
607: if (eflag==0) {
608: separate(dpl ? "3" : "");
609: change(1, &d1->old, dpl);
610: change(2, &d2->old, 0);
611: d3 = d1->old.to > d1->old.from ? d1 : d2;
612: change(3, &d3->new, 0);
613: } else
614: j = edit(d1, dpl, j);
615: d1++;
616: d2++;
617: continue;
618: }
619:
620: /*
621: * Overlapping changes from file 1 and 2; extend changes
622: * appropriately to make them coincide.
623: */
624: if (d1->new.from < d2->new.from) {
625: d2->old.from -= d2->new.from-d1->new.from;
626: d2->new.from = d1->new.from;
627: } else if (d2->new.from < d1->new.from) {
628: d1->old.from -= d1->new.from-d2->new.from;
629: d1->new.from = d2->new.from;
630: }
631: if (d1->new.to > d2->new.to) {
632: d2->old.to += d1->new.to - d2->new.to;
633: d2->new.to = d1->new.to;
634: } else if (d2->new.to > d1->new.to) {
635: d1->old.to += d2->new.to - d1->new.to;
636: d1->new.to = d2->new.to;
637: }
638: }
639:
640: return (edscript(j));
641: }
642:
643: static void
644: separate(const char *s)
645: {
646: diff_output("====%s\n", s);
647: }
648:
649: /*
650: * The range of lines rold.from thru rold.to in file i is to be changed.
651: * It is to be printed only if it does not duplicate something to be
652: * printed later.
653: */
654: static void
655: change(int i, struct range *rold, int fdup)
656: {
657: diff_output("%d:", i);
658: last[i] = rold->to;
659: prange(rold);
660: if (fdup || debug)
661: return;
662: i--;
663: (void)skip(i, rold->from, NULL);
664: (void)skip(i, rold->to, " ");
665: }
666:
667: /*
668: * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
669: */
670: static void
671: prange(struct range *rold)
672: {
673: if (rold->to <= rold->from)
674: diff_output("%da\n", rold->from - 1);
675: else {
676: diff_output("%d", rold->from);
677: if (rold->to > rold->from+1)
678: diff_output(",%d", rold->to - 1);
679: diff_output("c\n");
680: }
681: }
682:
683: /*
684: * No difference was reported by diff between file 1 (or 2) and file 3,
685: * and an artificial dummy difference (trange) must be ginned up to
686: * correspond to the change reported in the other file.
687: */
688: static void
689: keep(int i, struct range *rnew)
690: {
691: int delta;
692: struct range trange;
693:
694: delta = last[3] - last[i];
695: trange.from = rnew->from - delta;
696: trange.to = rnew->to - delta;
697: change(i, &trange, 1);
698: }
699:
700: /*
701: * skip to just before line number from in file "i". If "pr" is non-NULL,
702: * print all skipped stuff with string pr as a prefix.
703: */
704: static int
705: skip(int i, int from, char *pr)
706: {
707: size_t j, n;
708: char *line;
709:
710: for (n = 0; cline[i] < from - 1; n += j) {
1.55 fgsch 711: if ((line = get_line(fp[i], &j)) == NULL)
1.1 joris 712: return (-1);
713: if (pr != NULL)
714: diff_output("%s%s", pr, line);
715: cline[i]++;
716: }
717: return ((int) n);
718: }
719:
720: /*
721: * Return 1 or 0 according as the old range (in file 1) contains exactly
722: * the same data as the new range (in file 2).
723: */
724: static int
725: duplicate(struct range *r1, struct range *r2)
726: {
727: int c,d;
728: int nchar;
729: int nline;
730:
731: if (r1->to-r1->from != r2->to-r2->from)
732: return (0);
733: (void)skip(0, r1->from, NULL);
734: (void)skip(1, r2->from, NULL);
735: nchar = 0;
736: for (nline=0; nline < r1->to - r1->from; nline++) {
737: do {
738: c = getc(fp[0]);
739: d = getc(fp[1]);
740: if (c == -1 || d== -1)
741: return (-1);
742: nchar++;
743: if (c != d) {
744: repos(nchar);
745: return (0);
746: }
747: } while (c != '\n');
748: }
749: repos(nchar);
750: return (1);
751: }
752:
753: static void
754: repos(int nchar)
755: {
756: int i;
757:
758: for (i = 0; i < 2; i++)
1.38 tobias 759: (void)fseek(fp[i], (long)-nchar, SEEK_CUR);
1.1 joris 760: }
761:
762: /*
763: * collect an editing script for later regurgitation
764: */
765: static int
766: edit(struct diff *diff, int fdup, int j)
767: {
768: if (((fdup + 1) & eflag) == 0)
769: return (j);
770: j++;
771: overlap[j] = !fdup;
772: if (!fdup)
773: overlapcnt++;
774: de[j].old.from = diff->old.from;
775: de[j].old.to = diff->old.to;
776: de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
777: de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
778: return (j);
779: }
780:
781: /* regurgitate */
782: static int
783: edscript(int n)
784: {
785: int j, k;
1.2 joris 786: char block[BUFSIZ+1];
1.1 joris 787:
788: for (n = n; n > 0; n--) {
789: if (!oflag || !overlap[n])
790: prange(&de[n].old);
791: else
792: diff_output("%da\n=======\n", de[n].old.to -1);
1.38 tobias 793: (void)fseek(fp[2], (long)de[n].new.from, SEEK_SET);
1.1 joris 794: for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
795: j = k > BUFSIZ ? BUFSIZ : k;
1.53 ray 796: if (fread(block, 1, j, fp[2]) != j)
1.1 joris 797: return (-1);
798: block[j] = '\0';
799: diff_output("%s", block);
800: }
1.2 joris 801:
1.1 joris 802: if (!oflag || !overlap[n])
803: diff_output(".\n");
804: else {
805: diff_output("%s\n.\n", f3mark);
806: diff_output("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
807: }
808: }
809:
810: return (overlapcnt);
811: }
812:
813: static void
814: increase(void)
815: {
816: size_t newsz, incr;
817:
818: /* are the memset(3) calls needed? */
819: newsz = szchanges == 0 ? 64 : 2 * szchanges;
820: incr = newsz - szchanges;
821:
1.56 deraadt 822: d13 = xreallocarray(d13, newsz, sizeof(*d13));
1.36 ray 823: memset(d13 + szchanges, 0, incr * sizeof(*d13));
1.56 deraadt 824: d23 = xreallocarray(d23, newsz, sizeof(*d23));
1.36 ray 825: memset(d23 + szchanges, 0, incr * sizeof(*d23));
1.56 deraadt 826: de = xreallocarray(de, newsz, sizeof(*de));
1.36 ray 827: memset(de + szchanges, 0, incr * sizeof(*de));
1.56 deraadt 828: overlap = xreallocarray(overlap, newsz, sizeof(*overlap));
1.36 ray 829: memset(overlap + szchanges, 0, incr * sizeof(*overlap));
1.1 joris 830: szchanges = newsz;
831: }