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