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