Annotation of src/usr.bin/rcs/diff3.c, Revision 1.9
1.9 ! xsa 1: /* $OpenBSD: diff3.c,v 1.8 2006/05/10 01:10:23 ray 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:
67: #ifndef lint
68: static const char copyright[] =
69: "@(#) Copyright (c) 1991, 1993\n\
70: The Regents of the University of California. All rights reserved.\n";
71: #endif /* not lint */
72:
73: #ifndef lint
74: static const char rcsid[] =
1.9 ! xsa 75: "$OpenBSD: diff3.c,v 1.8 2006/05/10 01:10:23 ray Exp $";
1.1 joris 76: #endif /* not lint */
77:
78: #include "includes.h"
79:
80: #include "diff.h"
1.4 xsa 81: #include "rcsprog.h"
1.1 joris 82:
83: /* diff3 - 3-way differential file comparison */
84:
85: /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
86: *
87: * d13 = diff report on f1 vs f3
88: * d23 = diff report on f2 vs f3
89: * f1, f2, f3 the 3 files
90: * if changes in f1 overlap with changes in f3, m1 and m3 are used
91: * to mark the overlaps; otherwise, the file names f1 and f3 are used
92: * (only for options E and X).
93: */
94:
95: /*
96: * "from" is first in range of changed lines; "to" is last+1
97: * from=to=line after point of insertion for added lines.
98: */
99: struct range {
100: int from;
101: int to;
102: };
103:
104: struct diff {
105: struct range old;
106: struct range new;
107: };
108:
109: static size_t szchanges;
110:
111: static struct diff *d13;
112: static struct diff *d23;
113:
114: /*
115: * "de" is used to gather editing scripts. These are later spewed out in
116: * reverse order. Its first element must be all zero, the "new" component
117: * of "de" contains line positions or byte positions depending on when you
118: * look (!?). Array overlap indicates which sections in "de" correspond to
119: * lines that are different in all three files.
120: */
121: static struct diff *de;
122: static char *overlap;
123: static int overlapcnt = 0;
124: static FILE *fp[3];
125: static int cline[3]; /* # of the last-read line in each file (0-2) */
126:
127: /*
128: * the latest known correspondence between line numbers of the 3 files
129: * is stored in last[1-3];
130: */
131: static int last[4];
132: static int eflag;
133: static int oflag; /* indicates whether to mark overlaps (-E or -X)*/
134: static int debug = 0;
135: static char f1mark[40], f3mark[40]; /* markers for -E and -X */
136:
137: static int duplicate(struct range *, struct range *);
138: static int edit(struct diff *, int, int);
139: static char *getchange(FILE *);
140: static char *getline(FILE *, size_t *);
141: static int number(char **);
142: static size_t readin(char *, struct diff **);
143: static int skip(int, int, char *);
144: static int edscript(int);
145: static int merge(size_t, size_t);
146: static void change(int, struct range *, int);
147: static void keep(int, struct range *);
148: static void prange(struct range *);
149: static void repos(int);
150: static void separate(const char *);
151: static void increase(void);
152: static int diff3_internal(int, char **, const char *, const char *);
153:
154: int diff3_conflicts = 0;
1.9 ! xsa 155:
! 156: /*
! 157: * For merge(1).
! 158: */
! 159: BUF *
! 160: merge_diff3(char **av, int flags)
! 161: {
! 162: int argc;
! 163: char *data, *patch;
! 164: char *argv[5], *dp13, *dp23, *path1, *path2, *path3;
! 165: BUF *b1, *b2, *b3, *d1, *d2, *diffb;
! 166:
! 167: b1 = b2 = b3 = d1 = d2 = diffb = NULL;
! 168: dp13 = dp23 = path1 = path2 = path3 = NULL;
! 169:
! 170: if ((b1 = rcs_buf_load(av[0], BUF_AUTOEXT)) == NULL)
! 171: goto out;
! 172: if ((b2 = rcs_buf_load(av[1], BUF_AUTOEXT)) == NULL)
! 173: goto out;
! 174: if ((b3 = rcs_buf_load(av[2], BUF_AUTOEXT)) == NULL)
! 175: goto out;
! 176:
! 177: d1 = rcs_buf_alloc(128, BUF_AUTOEXT);
! 178: d2 = rcs_buf_alloc(128, BUF_AUTOEXT);
! 179: diffb = rcs_buf_alloc(128, BUF_AUTOEXT);
! 180:
! 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);
! 184:
! 185: rcs_buf_write_stmp(b1, path1, 0600);
! 186: rcs_buf_write_stmp(b2, path2, 0600);
! 187: rcs_buf_write_stmp(b3, path3, 0600);
! 188:
! 189: rcs_buf_free(b2);
! 190: b2 = NULL;
! 191:
! 192: if ((rcs_diffreg(path1, path3, d1) == D_ERROR) ||
! 193: (rcs_diffreg(path2, path3, d2) == D_ERROR)) {
! 194: rcs_buf_free(diffb);
! 195: diffb = NULL;
! 196: goto out;
! 197: }
! 198:
! 199: (void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
! 200: rcs_buf_write_stmp(d1, dp13, 0600);
! 201:
! 202: rcs_buf_free(d1);
! 203: d1 = NULL;
! 204:
! 205: (void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
! 206: rcs_buf_write_stmp(d2, dp23, 0600);
! 207:
! 208: rcs_buf_free(d2);
! 209: d2 = NULL;
! 210:
! 211: argc = 0;
! 212: diffbuf = diffb;
! 213: argv[argc++] = dp13;
! 214: argv[argc++] = dp23;
! 215: argv[argc++] = path1;
! 216: argv[argc++] = path2;
! 217: argv[argc++] = path3;
! 218:
! 219: diff3_conflicts = diff3_internal(argc, argv, av[0], av[2]);
! 220: if (diff3_conflicts < 0) {
! 221: rcs_buf_free(diffb);
! 222: diffb = NULL;
! 223: goto out;
! 224: }
! 225:
! 226: rcs_buf_putc(diffb, '\0');
! 227: rcs_buf_putc(b1, '\0');
! 228:
! 229: patch = rcs_buf_release(diffb);
! 230: data = rcs_buf_release(b1);
! 231: diffb = b1 = NULL;
! 232:
! 233: if ((diffb = rcs_patchfile(data, patch, ed_patch_lines)) == NULL)
! 234: goto out;
! 235:
! 236: if (!(flags & QUIET) && diff3_conflicts != 0)
! 237: warnx("warning: overlaps or other problems during merge");
! 238:
! 239: xfree(data);
! 240: xfree(patch);
! 241: out:
! 242: if (b1 != NULL)
! 243: rcs_buf_free(b1);
! 244: if (b2 != NULL)
! 245: rcs_buf_free(b2);
! 246: if (b3 != NULL)
! 247: rcs_buf_free(b3);
! 248: if (d1 != NULL)
! 249: rcs_buf_free(d1);
! 250: if (d2 != NULL)
! 251: rcs_buf_free(d2);
! 252:
! 253: (void)unlink(path1);
! 254: (void)unlink(path2);
! 255: (void)unlink(path3);
! 256: (void)unlink(dp13);
! 257: (void)unlink(dp23);
! 258:
! 259: if (path1 != NULL)
! 260: xfree(path1);
! 261: if (path2 != NULL)
! 262: xfree(path2);
! 263: if (path3 != NULL)
! 264: xfree(path3);
! 265: if (dp13 != NULL)
! 266: xfree(dp13);
! 267: if (dp23 != NULL)
! 268: xfree(dp23);
! 269:
! 270: return (diffb);
! 271: }
1.1 joris 272:
273: BUF *
274: rcs_diff3(RCSFILE *rf, char *workfile, RCSNUM *rev1, RCSNUM *rev2, int verbose)
275: {
276: int argc;
277: char *data, *patch;
278: char *argv[5], r1[16], r2[16];
1.5 xsa 279: char *dp13, *dp23, *path1, *path2, *path3;
1.1 joris 280: BUF *b1, *b2, *b3, *d1, *d2, *diffb;
281:
282: b1 = b2 = b3 = d1 = d2 = diffb = NULL;
1.7 xsa 283: dp13 = dp23 = path1 = path2 = path3 = NULL;
1.1 joris 284:
285: rcsnum_tostr(rev1, r1, sizeof(r1));
286: rcsnum_tostr(rev2, r2, sizeof(r2));
287:
288: if ((b1 = rcs_buf_load(workfile, BUF_AUTOEXT)) == NULL)
289: goto out;
290:
291: if (verbose == 1)
1.6 xsa 292: (void)fprintf(stderr, "retrieving revision %s\n", r1);
1.1 joris 293: if ((b2 = rcs_getrev(rf, rev1)) == NULL)
294: goto out;
295:
296: if (verbose == 1)
1.6 xsa 297: (void)fprintf(stderr, "retrieving revision %s\n", r2);
1.1 joris 298: if ((b3 = rcs_getrev(rf, rev2)) == NULL)
299: goto out;
300:
1.8 ray 301: d1 = rcs_buf_alloc(128, BUF_AUTOEXT);
302: d2 = rcs_buf_alloc(128, BUF_AUTOEXT);
303: diffb = rcs_buf_alloc(128, BUF_AUTOEXT);
1.1 joris 304:
1.5 xsa 305: (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
306: (void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
307: (void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", rcs_tmpdir);
1.3 ray 308:
1.1 joris 309: rcs_buf_write_stmp(b1, path1, 0600);
310: rcs_buf_write_stmp(b2, path2, 0600);
311: rcs_buf_write_stmp(b3, path3, 0600);
312:
313: rcs_buf_free(b2);
314: b2 = NULL;
315:
1.7 xsa 316: if ((rcs_diffreg(path1, path3, d1) == D_ERROR) ||
317: (rcs_diffreg(path2, path3, d2) == D_ERROR)) {
318: rcs_buf_free(diffb);
319: diffb = NULL;
320: goto out;
321: }
1.1 joris 322:
1.5 xsa 323: (void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
1.1 joris 324: rcs_buf_write_stmp(d1, dp13, 0600);
325:
326: rcs_buf_free(d1);
327: d1 = NULL;
328:
1.5 xsa 329: (void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
1.1 joris 330: rcs_buf_write_stmp(d2, dp23, 0600);
331:
332: rcs_buf_free(d2);
333: d2 = NULL;
334:
335: argc = 0;
336: diffbuf = diffb;
337: argv[argc++] = dp13;
338: argv[argc++] = dp23;
339: argv[argc++] = path1;
340: argv[argc++] = path2;
341: argv[argc++] = path3;
342:
343: diff3_conflicts = diff3_internal(argc, argv, workfile, r2);
344: if (diff3_conflicts < 0) {
345: rcs_buf_free(diffb);
346: diffb = NULL;
347: goto out;
348: }
349:
350: rcs_buf_putc(diffb, '\0');
351: rcs_buf_putc(b1, '\0');
352:
353: patch = rcs_buf_release(diffb);
354: data = rcs_buf_release(b1);
355: diffb = b1 = NULL;
356:
357: if ((diffb = rcs_patchfile(data, patch, ed_patch_lines)) == NULL)
358: goto out;
359:
1.6 xsa 360: if (verbose == 1 && diff3_conflicts != 0)
361: warnx("warning: overlaps or other problems during merge");
1.1 joris 362:
363: xfree(data);
364: xfree(patch);
365:
366: out:
367: if (b1 != NULL)
368: rcs_buf_free(b1);
369: if (b2 != NULL)
370: rcs_buf_free(b2);
371: if (b3 != NULL)
372: rcs_buf_free(b3);
373: if (d1 != NULL)
374: rcs_buf_free(d1);
375: if (d2 != NULL)
376: rcs_buf_free(d2);
377:
378: (void)unlink(path1);
379: (void)unlink(path2);
380: (void)unlink(path3);
381: (void)unlink(dp13);
382: (void)unlink(dp23);
1.5 xsa 383:
384: if (path1 != NULL)
385: xfree(path1);
386: if (path2 != NULL)
387: xfree(path2);
388: if (path3 != NULL)
389: xfree(path3);
390: if (dp13 != NULL)
391: xfree(dp13);
392: if (dp23 != NULL)
393: xfree(dp23);
1.1 joris 394:
395: return (diffb);
396: }
397:
398: static int
399: diff3_internal(int argc, char **argv, const char *fmark, const char *rmark)
400: {
401: size_t m, n;
402: int i;
403:
404: /* XXX */
405: eflag = 3;
406: oflag = 1;
407:
408: if (argc < 5)
409: return (-1);
410:
1.3 ray 411: if (strlcpy(f1mark, "<<<<<<< ", sizeof(f1mark)) >= sizeof(f1mark) ||
412: strlcat(f1mark, fmark, sizeof(f1mark)) >= sizeof(f1mark))
413: errx(1, "diff3_internal: string truncated");
414:
415: if (strlcpy(f3mark, ">>>>>>> ", sizeof(f3mark)) >= sizeof(f3mark) ||
416: strlcat(f3mark, rmark, sizeof(f3mark)) >= sizeof(f3mark))
417: errx(1, "diff3_internal: strlcat");
1.1 joris 418:
419: increase();
420: m = readin(argv[0], &d13);
421: n = readin(argv[1], &d23);
422:
1.3 ray 423: for (i = 0; i <= 2; i++)
1.1 joris 424: if ((fp[i] = fopen(argv[i + 2], "r")) == NULL) {
425: warn("%s", argv[i + 2]);
426: return (-1);
427: }
428:
429: return (merge(m, n));
430: }
431:
432: int
433: ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
434: {
435: char op, *ep;
436: struct rcs_line *sort, *lp, *dlp, *ndlp;
437: int start, end, i, lineno;
438:
439: dlp = TAILQ_FIRST(&(dlines->l_lines));
440: lp = TAILQ_FIRST(&(plines->l_lines));
441:
442: end = 0;
443: for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
444: lp = TAILQ_NEXT(lp, l_list)) {
445: op = lp->l_line[strlen(lp->l_line) - 1];
446: start = (int)strtol(lp->l_line, &ep, 10);
447: if (op == 'a') {
448: if (start > dlines->l_nblines ||
449: start < 0 || *ep != 'a')
450: errx(1, "ed_patch_lines");
451: } else if (op == 'c') {
452: if (start > dlines->l_nblines ||
453: start < 0 || (*ep != ',' && *ep != 'c'))
454: errx(1, "ed_patch_lines");
455:
456: if (*ep == ',') {
457: ep++;
458: end = (int)strtol(ep, &ep, 10);
459: if (end < 0 || *ep != 'c')
460: errx(1, "ed_patch_lines");
461: } else {
462: end = start;
463: }
464: }
465:
466:
467: for (;;) {
468: if (dlp == NULL)
469: break;
470: if (dlp->l_lineno == start)
471: break;
472: if (dlp->l_lineno > start) {
473: dlp = TAILQ_PREV(dlp, rcs_tqh, l_list);
474: } else if (dlp->l_lineno < start) {
475: ndlp = TAILQ_NEXT(dlp, l_list);
476: if (ndlp->l_lineno > start)
477: break;
478: dlp = ndlp;
479: }
480: }
481:
482: if (dlp == NULL)
483: errx(1, "ed_patch_lines");
484:
485:
486: if (op == 'c') {
487: for (i = 0; i <= (end - start); i++) {
488: ndlp = TAILQ_NEXT(dlp, l_list);
489: TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
490: dlp = ndlp;
491: }
492: dlp = TAILQ_PREV(dlp, rcs_tqh, l_list);
493: }
494:
495: if (op == 'a' || op == 'c') {
496: for (;;) {
497: ndlp = lp;
498: lp = TAILQ_NEXT(lp, l_list);
499: if (lp == NULL)
500: errx(1, "ed_patch_lines");
501:
502: if (!strcmp(lp->l_line, "."))
503: break;
504:
505: TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
506: TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
507: lp, l_list);
508: dlp = lp;
509:
510: lp->l_lineno = start;
511: lp = ndlp;
512: }
513: }
514:
515: /*
516: * always resort lines as the markers might be put at the
517: * same line as we first started editing.
518: */
519: lineno = 0;
520: TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
521: sort->l_lineno = lineno++;
522: dlines->l_nblines = lineno - 1;
523: }
524:
525: return (0);
526: }
527:
528: /*
529: * Pick up the line numbers of all changes from one change file.
530: * (This puts the numbers in a vector, which is not strictly necessary,
531: * since the vector is processed in one sequential pass.
532: * The vector could be optimized out of existence)
533: */
534: static size_t
535: readin(char *name, struct diff **dd)
536: {
537: int a, b, c, d;
538: char kind, *p;
539: size_t i;
540:
541: fp[0] = fopen(name, "r");
542: for (i = 0; (p = getchange(fp[0])); i++) {
543: if (i >= szchanges - 1)
544: increase();
545: a = b = number(&p);
546: if (*p == ',') {
547: p++;
548: b = number(&p);
549: }
550: kind = *p++;
551: c = d = number(&p);
552: if (*p==',') {
553: p++;
554: d = number(&p);
555: }
556: if (kind == 'a')
557: a++;
558: if (kind == 'd')
559: c++;
560: b++;
561: d++;
562: (*dd)[i].old.from = a;
563: (*dd)[i].old.to = b;
564: (*dd)[i].new.from = c;
565: (*dd)[i].new.to = d;
566: }
567:
568: if (i) {
569: (*dd)[i].old.from = (*dd)[i-1].old.to;
570: (*dd)[i].new.from = (*dd)[i-1].new.to;
571: }
572: (void)fclose(fp[0]);
573:
574: return (i);
575: }
576:
577: static int
578: number(char **lc)
579: {
580: int nn;
581:
582: nn = 0;
583: while (isdigit((unsigned char)(**lc)))
584: nn = nn*10 + *(*lc)++ - '0';
585:
586: return (nn);
587: }
588:
589: static char *
590: getchange(FILE *b)
591: {
592: char *line;
593:
594: while ((line = getline(b, NULL))) {
595: if (isdigit((unsigned char)line[0]))
596: return (line);
597: }
598:
599: return (NULL);
600: }
601:
602: static char *
603: getline(FILE *b, size_t *n)
604: {
605: char *cp;
606: size_t len;
607: static char *buf;
608: static size_t bufsize;
609:
610: if ((cp = fgetln(b, &len)) == NULL)
611: return (NULL);
612:
613: if (cp[len - 1] != '\n')
614: len++;
615: if (len + 1 > bufsize) {
616: char *newbuf;
617: do {
618: bufsize += 1024;
619: } while (len + 1 > bufsize);
620: newbuf = xrealloc(buf, 1, bufsize);
621: buf = newbuf;
622: }
623: memcpy(buf, cp, len - 1);
624: buf[len - 1] = '\n';
625: buf[len] = '\0';
626: if (n != NULL)
627: *n = len;
628:
629: return (buf);
630: }
631:
632: static int
633: merge(size_t m1, size_t m2)
634: {
635: struct diff *d1, *d2, *d3;
636: int dpl, j, t1, t2;
637:
638: d1 = d13;
639: d2 = d23;
640: j = 0;
641: while ((t1 = d1 < d13 + m1) | (t2 = d2 < d23 + m2)) {
642: if (debug) {
643: printf("%d,%d=%d,%d %d,%d=%d,%d\n",
644: d1->old.from, d1->old.to,
645: d1->new.from, d1->new.to,
646: d2->old.from, d2->old.to,
647: d2->new.from, d2->new.to);
648: }
649:
650: /* first file is different from others */
651: if (!t2 || (t1 && d1->new.to < d2->new.from)) {
652: /* stuff peculiar to 1st file */
653: if (eflag==0) {
654: separate("1");
655: change(1, &d1->old, 0);
656: keep(2, &d1->new);
657: change(3, &d1->new, 0);
658: }
659: d1++;
660: continue;
661: }
662:
663: /* second file is different from others */
664: if (!t1 || (t2 && d2->new.to < d1->new.from)) {
665: if (eflag==0) {
666: separate("2");
667: keep(1, &d2->new);
668: change(2, &d2->old, 0);
669: change(3, &d2->new, 0);
670: }
671: d2++;
672: continue;
673: }
674:
675: /*
676: * Merge overlapping changes in first file
677: * this happens after extension (see below).
678: */
679: if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
680: d1[1].old.from = d1->old.from;
681: d1[1].new.from = d1->new.from;
682: d1++;
683: continue;
684: }
685:
686: /* merge overlapping changes in second */
687: if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
688: d2[1].old.from = d2->old.from;
689: d2[1].new.from = d2->new.from;
690: d2++;
691: continue;
692: }
693: /* stuff peculiar to third file or different in all */
694: if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
695: dpl = duplicate(&d1->old,&d2->old);
696: if (dpl == -1)
697: return (-1);
698:
699: /*
700: * dpl = 0 means all files differ
701: * dpl = 1 means files 1 and 2 identical
702: */
703: if (eflag==0) {
704: separate(dpl ? "3" : "");
705: change(1, &d1->old, dpl);
706: change(2, &d2->old, 0);
707: d3 = d1->old.to > d1->old.from ? d1 : d2;
708: change(3, &d3->new, 0);
709: } else
710: j = edit(d1, dpl, j);
711: d1++;
712: d2++;
713: continue;
714: }
715:
716: /*
717: * Overlapping changes from file 1 and 2; extend changes
718: * appropriately to make them coincide.
719: */
720: if (d1->new.from < d2->new.from) {
721: d2->old.from -= d2->new.from-d1->new.from;
722: d2->new.from = d1->new.from;
723: } else if (d2->new.from < d1->new.from) {
724: d1->old.from -= d1->new.from-d2->new.from;
725: d1->new.from = d2->new.from;
726: }
727: if (d1->new.to > d2->new.to) {
728: d2->old.to += d1->new.to - d2->new.to;
729: d2->new.to = d1->new.to;
730: } else if (d2->new.to > d1->new.to) {
731: d1->old.to += d2->new.to - d1->new.to;
732: d1->new.to = d2->new.to;
733: }
734: }
735:
736: return (edscript(j));
737: }
738:
739: static void
740: separate(const char *s)
741: {
742: diff_output("====%s\n", s);
743: }
744:
745: /*
746: * The range of lines rold.from thru rold.to in file i is to be changed.
747: * It is to be printed only if it does not duplicate something to be
748: * printed later.
749: */
750: static void
751: change(int i, struct range *rold, int fdup)
752: {
753: diff_output("%d:", i);
754: last[i] = rold->to;
755: prange(rold);
756: if (fdup || debug)
757: return;
758: i--;
759: (void)skip(i, rold->from, NULL);
760: (void)skip(i, rold->to, " ");
761: }
762:
763: /*
764: * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
765: */
766: static void
767: prange(struct range *rold)
768: {
769: if (rold->to <= rold->from)
770: diff_output("%da\n", rold->from - 1);
771: else {
772: diff_output("%d", rold->from);
773: if (rold->to > rold->from+1)
774: diff_output(",%d", rold->to - 1);
775: diff_output("c\n");
776: }
777: }
778:
779: /*
780: * No difference was reported by diff between file 1 (or 2) and file 3,
781: * and an artificial dummy difference (trange) must be ginned up to
782: * correspond to the change reported in the other file.
783: */
784: static void
785: keep(int i, struct range *rnew)
786: {
787: int delta;
788: struct range trange;
789:
790: delta = last[3] - last[i];
791: trange.from = rnew->from - delta;
792: trange.to = rnew->to - delta;
793: change(i, &trange, 1);
794: }
795:
796: /*
797: * skip to just before line number from in file "i". If "pr" is non-NULL,
798: * print all skipped stuff with string pr as a prefix.
799: */
800: static int
801: skip(int i, int from, char *pr)
802: {
803: size_t j, n;
804: char *line;
805:
806: for (n = 0; cline[i] < from - 1; n += j) {
807: if ((line = getline(fp[i], &j)) == NULL)
808: return (-1);
809: if (pr != NULL)
810: diff_output("%s%s", pr, line);
811: cline[i]++;
812: }
813: return ((int) n);
814: }
815:
816: /*
817: * Return 1 or 0 according as the old range (in file 1) contains exactly
818: * the same data as the new range (in file 2).
819: */
820: static int
821: duplicate(struct range *r1, struct range *r2)
822: {
823: int c,d;
824: int nchar;
825: int nline;
826:
827: if (r1->to-r1->from != r2->to-r2->from)
828: return (0);
829: (void)skip(0, r1->from, NULL);
830: (void)skip(1, r2->from, NULL);
831: nchar = 0;
832: for (nline=0; nline < r1->to - r1->from; nline++) {
833: do {
834: c = getc(fp[0]);
835: d = getc(fp[1]);
836: if (c == -1 || d== -1)
837: return (-1);
838: nchar++;
839: if (c != d) {
840: repos(nchar);
841: return (0);
842: }
843: } while (c != '\n');
844: }
845: repos(nchar);
846: return (1);
847: }
848:
849: static void
850: repos(int nchar)
851: {
852: int i;
853:
854: for (i = 0; i < 2; i++)
855: (void)fseek(fp[i], (long)-nchar, 1);
856: }
857:
858: /*
859: * collect an editing script for later regurgitation
860: */
861: static int
862: edit(struct diff *diff, int fdup, int j)
863: {
864: if (((fdup + 1) & eflag) == 0)
865: return (j);
866: j++;
867: overlap[j] = !fdup;
868: if (!fdup)
869: overlapcnt++;
870: de[j].old.from = diff->old.from;
871: de[j].old.to = diff->old.to;
872: de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
873: de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
874: return (j);
875: }
876:
877: /* regurgitate */
878: static int
879: edscript(int n)
880: {
881: int j, k;
882: char block[BUFSIZ+1];
883:
884: for (n = n; n > 0; n--) {
885: if (!oflag || !overlap[n])
886: prange(&de[n].old);
887: else
888: diff_output("%da\n=======\n", de[n].old.to -1);
889: (void)fseek(fp[2], (long)de[n].new.from, 0);
890: for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
891: j = k > BUFSIZ ? BUFSIZ : k;
1.8 ray 892: if (fread(block, 1, (size_t)j,
1.1 joris 893: fp[2]) != (size_t)j)
894: return (-1);
895: block[j] = '\0';
896: diff_output("%s", block);
897: }
898:
899: if (!oflag || !overlap[n])
900: diff_output(".\n");
901: else {
902: diff_output("%s\n.\n", f3mark);
903: diff_output("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
904: }
905: }
906:
907: return (overlapcnt);
908: }
909:
910: static void
911: increase(void)
912: {
913: struct diff *p;
914: char *q;
915: size_t newsz, incr;
916:
917: /* are the memset(3) calls needed? */
918: newsz = szchanges == 0 ? 64 : 2 * szchanges;
919: incr = newsz - szchanges;
920:
921: p = xrealloc(d13, newsz, sizeof(*d13));
922: memset(p + szchanges, 0, incr * sizeof(*d13));
923: d13 = p;
924: p = xrealloc(d23, newsz, sizeof(*d23));
925: memset(p + szchanges, 0, incr * sizeof(*d23));
926: d23 = p;
927: p = xrealloc(de, newsz, sizeof(*de));
928: memset(p + szchanges, 0, incr * sizeof(*de));
929: de = p;
930: q = xrealloc(overlap, newsz, sizeof(*overlap));
931: memset(q + szchanges, 0, incr * sizeof(*overlap));
932: overlap = q;
933: szchanges = newsz;
934: }