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