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