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