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