Annotation of src/usr.bin/cvs/diff3.c, Revision 1.30
1.30 ! niallo 1: /* $OpenBSD: diff3.c,v 1.29 2006/11/10 08:32:37 xsa Exp $ */
1.1 joris 2:
3: /*
4: * Copyright (C) Caldera International Inc. 2001-2002.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code and documentation must retain the above
11: * copyright notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed or owned by Caldera
18: * International, Inc.
19: * 4. Neither the name of Caldera International, Inc. nor the names of other
20: * contributors may be used to endorse or promote products derived from
21: * this software without specific prior written permission.
22: *
23: * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24: * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27: * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28: * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33: * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34: * POSSIBILITY OF SUCH DAMAGE.
35: */
36: /*-
37: * Copyright (c) 1991, 1993
38: * The Regents of the University of California. All rights reserved.
39: *
40: * Redistribution and use in source and binary forms, with or without
41: * modification, are permitted provided that the following conditions
42: * are met:
43: * 1. Redistributions of source code must retain the above copyright
44: * notice, this list of conditions and the following disclaimer.
45: * 2. Redistributions in binary form must reproduce the above copyright
46: * notice, this list of conditions and the following disclaimer in the
47: * documentation and/or other materials provided with the distribution.
48: * 3. Neither the name of the University nor the names of its contributors
49: * may be used to endorse or promote products derived from this software
50: * without specific prior written permission.
51: *
52: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62: * SUCH DAMAGE.
63: *
64: * @(#)diff3.c 8.1 (Berkeley) 6/6/93
65: */
66:
67: #ifndef lint
68: static const char copyright[] =
69: "@(#) Copyright (c) 1991, 1993\n\
70: The Regents of the University of California. All rights reserved.\n";
71: #endif /* not lint */
72:
73: #ifndef lint
1.11 reyk 74: static const char rcsid[] =
1.30 ! niallo 75: "$OpenBSD: diff3.c,v 1.29 2006/11/10 08:32:37 xsa Exp $";
1.1 joris 76: #endif /* not lint */
77:
1.12 xsa 78: #include "includes.h"
1.1 joris 79:
80: #include "cvs.h"
81: #include "log.h"
82: #include "diff.h"
83:
84: /* diff3 - 3-way differential file comparison */
85:
86: /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
87: *
88: * d13 = diff report on f1 vs f3
89: * d23 = diff report on f2 vs f3
90: * f1, f2, f3 the 3 files
91: * if changes in f1 overlap with changes in f3, m1 and m3 are used
92: * to mark the overlaps; otherwise, the file names f1 and f3 are used
93: * (only for options E and X).
94: */
95:
96: /*
97: * "from" is first in range of changed lines; "to" is last+1
98: * from=to=line after point of insertion for added lines.
99: */
100: struct range {
101: int from;
102: int to;
103: };
104:
105: struct diff {
106: struct range old;
107: struct range new;
108: };
109:
110: static size_t szchanges;
111:
112: static struct diff *d13;
113: static struct diff *d23;
114:
115: /*
116: * "de" is used to gather editing scripts. These are later spewed out in
117: * reverse order. Its first element must be all zero, the "new" component
118: * of "de" contains line positions or byte positions depending on when you
119: * look (!?). Array overlap indicates which sections in "de" correspond to
120: * lines that are different in all three files.
121: */
122: static struct diff *de;
123: static char *overlap;
1.2 joris 124: static int overlapcnt = 0;
1.1 joris 125: static FILE *fp[3];
126: static int cline[3]; /* # of the last-read line in each file (0-2) */
127:
128: /*
129: * the latest known correspondence between line numbers of the 3 files
130: * is stored in last[1-3];
131: */
132: static int last[4];
133: static int eflag;
134: static int oflag; /* indicates whether to mark overlaps (-E or -X)*/
135: static int debug = 0;
136: static char f1mark[40], f3mark[40]; /* markers for -E and -X */
137:
138: static int duplicate(struct range *, struct range *);
139: static int edit(struct diff *, int, int);
140: static char *getchange(FILE *);
141: static char *getline(FILE *, size_t *);
142: static int number(char **);
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.18 xsa 176: if (verbose == 1)
177: cvs_printf("Retrieving revision %s\n", r1);
1.1 joris 178: if ((b2 = rcs_getrev(rf, rev1)) == NULL)
179: goto out;
180:
1.18 xsa 181: if (verbose == 1)
182: cvs_printf("Retrieving revision %s\n", r2);
1.1 joris 183: if ((b3 = rcs_getrev(rf, rev2)) == NULL)
184: goto out;
185:
1.10 joris 186: d1 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT);
187: d2 = cvs_buf_alloc((size_t)128, BUF_AUTOEXT);
188: diffb = cvs_buf_alloc((size_t)128, BUF_AUTOEXT);
1.1 joris 189:
1.28 xsa 190: (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", cvs_tmpdir);
191: (void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", cvs_tmpdir);
192: (void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", cvs_tmpdir);
193:
1.26 ray 194: cvs_buf_write_stmp(b1, path1, NULL);
195: cvs_buf_write_stmp(b2, path2, NULL);
196: cvs_buf_write_stmp(b3, path3, NULL);
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:
1.28 xsa 204: (void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", cvs_tmpdir);
1.26 ray 205: cvs_buf_write_stmp(d1, dp13, NULL);
1.1 joris 206:
207: cvs_buf_free(d1);
208: d1 = NULL;
209:
1.28 xsa 210: (void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", cvs_tmpdir);
1.26 ray 211: cvs_buf_write_stmp(d2, dp23, NULL);
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:
1.30 ! niallo 234: plen = cvs_buf_len(diffb);
1.1 joris 235: patch = cvs_buf_release(diffb);
1.30 ! niallo 236: dlen = cvs_buf_len(b1);
1.1 joris 237: data = cvs_buf_release(b1);
238: diffb = b1 = NULL;
239:
1.30 ! niallo 240: if ((diffb = cvs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
1.1 joris 241: goto out;
1.7 joris 242:
1.23 deraadt 243: if (verbose == 1 && diff3_conflicts != 0) {
1.24 joris 244: cvs_log(LP_ERR, "%d conflict%s found during merge, "
1.16 xsa 245: "please correct.", diff3_conflicts,
1.11 reyk 246: (diff3_conflicts > 1) ? "s" : "");
1.7 joris 247: }
1.1 joris 248:
1.8 joris 249: xfree(data);
250: xfree(patch);
1.1 joris 251:
252: out:
253: if (b1 != NULL)
254: cvs_buf_free(b1);
255: if (b2 != NULL)
256: cvs_buf_free(b2);
257: if (b3 != NULL)
258: cvs_buf_free(b3);
259: if (d1 != NULL)
260: cvs_buf_free(d1);
261: if (d2 != NULL)
262: cvs_buf_free(d2);
263:
264: (void)unlink(path1);
265: (void)unlink(path2);
266: (void)unlink(path3);
267: (void)unlink(dp13);
268: (void)unlink(dp23);
1.28 xsa 269:
270: if (path1 != NULL)
271: xfree(path1);
272: if (path2 != NULL)
273: xfree(path2);
274: if (path3 != NULL)
275: xfree(path3);
276: if (dp13 != NULL)
277: xfree(dp13);
278: if (dp23 != NULL)
279: xfree(dp23);
1.1 joris 280:
281: return (diffb);
282: }
283:
284: static int
285: diff3_internal(int argc, char **argv, const char *fmark, const char *rmark)
286: {
1.20 ray 287: size_t m, n;
288: int i;
1.1 joris 289:
290: /* XXX */
291: eflag = 3;
292: oflag = 1;
293:
294: if (argc < 5)
295: return (-1);
296:
1.29 xsa 297: i = snprintf(f1mark, sizeof(f1mark), "<<<<<<< %s", fmark);
298: if (i < 0 ||i >= (int)sizeof(f1mark))
299: fatal("diff3_internal: truncation");
300:
301: i = snprintf(f3mark, sizeof(f3mark), ">>>>>>> %s", rmark);
302: if (i < 0 ||i >= (int)sizeof(f3mark))
303: fatal("diff3_internal: truncation");
1.1 joris 304:
305: increase();
306: m = readin(argv[0], &d13);
307: n = readin(argv[1], &d23);
308:
309: for (i = 0; i <= 2; i++) {
310: if ((fp[i] = fopen(argv[i + 2], "r")) == NULL) {
1.24 joris 311: cvs_log(LP_ERR, "%s", argv[i + 2]);
1.1 joris 312: return (-1);
313: }
314: }
315:
316: return (merge(m, n));
317: }
318:
319: int
320: ed_patch_lines(struct cvs_lines *dlines, struct cvs_lines *plines)
321: {
322: char op, *ep;
1.29 xsa 323: struct cvs_line *sort, *lp, *dlp, *ndlp, *insert_after;
1.19 xsa 324: int start, end, i, lineno;
1.30 ! niallo 325: u_char tmp;
1.1 joris 326:
327: dlp = TAILQ_FIRST(&(dlines->l_lines));
328: lp = TAILQ_FIRST(&(plines->l_lines));
329:
1.19 xsa 330: end = 0;
1.1 joris 331: for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
332: lp = TAILQ_NEXT(lp, l_list)) {
1.30 ! niallo 333: /* Skip blank lines */
! 334: if (lp->l_len < 2)
! 335: continue;
! 336: /* NUL-terminate line buffer for strtol() safety. */
! 337: tmp = lp->l_line[lp->l_len - 1];
! 338: lp->l_line[lp->l_len - 1] = '\0';
! 339: /* len - 1 is NUL terminator so we use len - 2 for 'op' */
1.1 joris 340: op = lp->l_line[strlen(lp->l_line) - 1];
341: start = (int)strtol(lp->l_line, &ep, 10);
1.30 ! niallo 342: /* Restore the last byte of the buffer */
! 343: lp->l_line[lp->l_len - 1] = tmp;
1.1 joris 344: if (op == 'a') {
1.23 deraadt 345: if (start > dlines->l_nblines ||
346: start < 0 || *ep != 'a')
1.15 niallo 347: fatal("ed_patch_lines");
1.1 joris 348: } else if (op == 'c') {
1.23 deraadt 349: if (start > dlines->l_nblines ||
350: start < 0 || (*ep != ',' && *ep != 'c'))
1.15 niallo 351: fatal("ed_patch_lines");
1.1 joris 352:
1.2 joris 353: if (*ep == ',') {
354: ep++;
355: end = (int)strtol(ep, &ep, 10);
1.23 deraadt 356: if (end < 0 || *ep != 'c')
1.15 niallo 357: fatal("ed_patch_lines");
1.2 joris 358: } else {
359: end = start;
360: }
1.1 joris 361: }
362:
1.2 joris 363:
1.1 joris 364: for (;;) {
365: if (dlp == NULL)
366: break;
367: if (dlp->l_lineno == start)
368: break;
369: if (dlp->l_lineno > start) {
370: dlp = TAILQ_PREV(dlp, cvs_tqh, l_list);
371: } else if (dlp->l_lineno < start) {
372: ndlp = TAILQ_NEXT(dlp, l_list);
373: if (ndlp->l_lineno > start)
374: break;
375: dlp = ndlp;
376: }
377: }
378:
379: if (dlp == NULL)
1.15 niallo 380: fatal("ed_patch_lines");
1.1 joris 381:
1.2 joris 382:
1.1 joris 383: if (op == 'c') {
1.29 xsa 384: insert_after = TAILQ_PREV(dlp, cvs_tqh, l_list);
1.2 joris 385: for (i = 0; i <= (end - start); i++) {
1.1 joris 386: ndlp = TAILQ_NEXT(dlp, l_list);
387: TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
388: dlp = ndlp;
389: }
1.29 xsa 390: dlp = insert_after;
1.1 joris 391: }
392:
393: if (op == 'a' || op == 'c') {
394: for (;;) {
395: ndlp = lp;
396: lp = TAILQ_NEXT(lp, l_list);
397: if (lp == NULL)
1.15 niallo 398: fatal("ed_patch_lines");
1.1 joris 399:
1.30 ! niallo 400: if (!memcmp(lp->l_line, ".", 1))
1.1 joris 401: break;
402:
403: TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
404: TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
405: lp, l_list);
406: dlp = lp;
407:
408: lp->l_lineno = start;
409: lp = ndlp;
410: }
411: }
412:
413: /*
414: * always resort lines as the markers might be put at the
415: * same line as we first started editing.
416: */
417: lineno = 0;
418: TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
419: sort->l_lineno = lineno++;
420: dlines->l_nblines = lineno - 1;
421: }
422:
423: return (0);
424: }
425:
426: /*
427: * Pick up the line numbers of all changes from one change file.
428: * (This puts the numbers in a vector, which is not strictly necessary,
429: * since the vector is processed in one sequential pass.
430: * The vector could be optimized out of existence)
431: */
1.20 ray 432: static size_t
1.1 joris 433: readin(char *name, struct diff **dd)
434: {
435: int a, b, c, d;
436: char kind, *p;
437: size_t i;
438:
439: fp[0] = fopen(name, "r");
440: for (i = 0; (p = getchange(fp[0])); i++) {
441: if (i >= szchanges - 1)
442: increase();
443: a = b = number(&p);
444: if (*p == ',') {
445: p++;
446: b = number(&p);
447: }
448: kind = *p++;
449: c = d = number(&p);
450: if (*p==',') {
451: p++;
452: d = number(&p);
453: }
454: if (kind == 'a')
455: a++;
456: if (kind == 'd')
457: c++;
458: b++;
459: d++;
460: (*dd)[i].old.from = a;
461: (*dd)[i].old.to = b;
462: (*dd)[i].new.from = c;
463: (*dd)[i].new.to = d;
464: }
465:
1.14 xsa 466: if (i) {
467: (*dd)[i].old.from = (*dd)[i-1].old.to;
468: (*dd)[i].new.from = (*dd)[i-1].new.to;
469: }
1.1 joris 470: (void)fclose(fp[0]);
471:
472: return (i);
473: }
474:
475: static int
476: number(char **lc)
477: {
478: int nn;
479:
480: nn = 0;
481: while (isdigit((unsigned char)(**lc)))
482: nn = nn*10 + *(*lc)++ - '0';
483:
484: return (nn);
485: }
486:
487: static char *
488: getchange(FILE *b)
489: {
490: char *line;
491:
492: while ((line = getline(b, NULL))) {
493: if (isdigit((unsigned char)line[0]))
494: return (line);
495: }
496:
497: return (NULL);
498: }
499:
500: static char *
501: getline(FILE *b, size_t *n)
502: {
503: char *cp;
504: size_t len;
505: static char *buf;
506: static size_t bufsize;
507:
508: if ((cp = fgetln(b, &len)) == NULL)
509: return (NULL);
510:
511: if (cp[len - 1] != '\n')
512: len++;
513: if (len + 1 > bufsize) {
1.22 ray 514: char *newbuf;
1.1 joris 515: do {
516: bufsize += 1024;
517: } while (len + 1 > bufsize);
1.22 ray 518: newbuf = xrealloc(buf, 1, bufsize);
519: buf = newbuf;
1.1 joris 520: }
521: memcpy(buf, cp, len - 1);
522: buf[len - 1] = '\n';
523: buf[len] = '\0';
524: if (n != NULL)
525: *n = len;
526:
527: return (buf);
528: }
529:
530: static int
1.20 ray 531: merge(size_t m1, size_t m2)
1.1 joris 532: {
533: struct diff *d1, *d2, *d3;
534: int dpl, j, t1, t2;
535:
536: d1 = d13;
537: d2 = d23;
538: j = 0;
539: while ((t1 = d1 < d13 + m1) | (t2 = d2 < d23 + m2)) {
540: if (debug) {
541: printf("%d,%d=%d,%d %d,%d=%d,%d\n",
1.20 ray 542: d1->old.from, d1->old.to,
543: d1->new.from, d1->new.to,
544: d2->old.from, d2->old.to,
545: d2->new.from, d2->new.to);
1.1 joris 546: }
547:
548: /* first file is different from others */
549: if (!t2 || (t1 && d1->new.to < d2->new.from)) {
550: /* stuff peculiar to 1st file */
551: if (eflag==0) {
552: separate("1");
553: change(1, &d1->old, 0);
554: keep(2, &d1->new);
555: change(3, &d1->new, 0);
556: }
557: d1++;
558: continue;
559: }
560:
561: /* second file is different from others */
562: if (!t1 || (t2 && d2->new.to < d1->new.from)) {
563: if (eflag==0) {
564: separate("2");
565: keep(1, &d2->new);
566: change(2, &d2->old, 0);
567: change(3, &d2->new, 0);
568: }
569: d2++;
570: continue;
571: }
572:
573: /*
574: * Merge overlapping changes in first file
575: * this happens after extension (see below).
576: */
577: if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
578: d1[1].old.from = d1->old.from;
579: d1[1].new.from = d1->new.from;
580: d1++;
581: continue;
582: }
583:
584: /* merge overlapping changes in second */
585: if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
586: d2[1].old.from = d2->old.from;
587: d2[1].new.from = d2->new.from;
588: d2++;
589: continue;
590: }
591: /* stuff peculiar to third file or different in all */
592: if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
593: dpl = duplicate(&d1->old,&d2->old);
594: if (dpl == -1)
595: return (-1);
596:
597: /*
598: * dpl = 0 means all files differ
599: * dpl = 1 means files 1 and 2 identical
600: */
601: if (eflag==0) {
602: separate(dpl ? "3" : "");
603: change(1, &d1->old, dpl);
604: change(2, &d2->old, 0);
605: d3 = d1->old.to > d1->old.from ? d1 : d2;
606: change(3, &d3->new, 0);
607: } else
608: j = edit(d1, dpl, j);
609: d1++;
610: d2++;
611: continue;
612: }
613:
614: /*
615: * Overlapping changes from file 1 and 2; extend changes
616: * appropriately to make them coincide.
617: */
618: if (d1->new.from < d2->new.from) {
619: d2->old.from -= d2->new.from-d1->new.from;
620: d2->new.from = d1->new.from;
621: } else if (d2->new.from < d1->new.from) {
622: d1->old.from -= d1->new.from-d2->new.from;
623: d1->new.from = d2->new.from;
624: }
625: if (d1->new.to > d2->new.to) {
626: d2->old.to += d1->new.to - d2->new.to;
627: d2->new.to = d1->new.to;
628: } else if (d2->new.to > d1->new.to) {
629: d1->old.to += d2->new.to - d1->new.to;
630: d1->new.to = d2->new.to;
631: }
632: }
633:
634: return (edscript(j));
635: }
636:
637: static void
638: separate(const char *s)
639: {
640: diff_output("====%s\n", s);
641: }
642:
643: /*
644: * The range of lines rold.from thru rold.to in file i is to be changed.
645: * It is to be printed only if it does not duplicate something to be
646: * printed later.
647: */
648: static void
649: change(int i, struct range *rold, int fdup)
650: {
651: diff_output("%d:", i);
652: last[i] = rold->to;
653: prange(rold);
654: if (fdup || debug)
655: return;
656: i--;
657: (void)skip(i, rold->from, NULL);
658: (void)skip(i, rold->to, " ");
659: }
660:
661: /*
662: * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
663: */
664: static void
665: prange(struct range *rold)
666: {
667: if (rold->to <= rold->from)
668: diff_output("%da\n", rold->from - 1);
669: else {
670: diff_output("%d", rold->from);
671: if (rold->to > rold->from+1)
672: diff_output(",%d", rold->to - 1);
673: diff_output("c\n");
674: }
675: }
676:
677: /*
678: * No difference was reported by diff between file 1 (or 2) and file 3,
679: * and an artificial dummy difference (trange) must be ginned up to
680: * correspond to the change reported in the other file.
681: */
682: static void
683: keep(int i, struct range *rnew)
684: {
685: int delta;
686: struct range trange;
687:
688: delta = last[3] - last[i];
689: trange.from = rnew->from - delta;
690: trange.to = rnew->to - delta;
691: change(i, &trange, 1);
692: }
693:
694: /*
695: * skip to just before line number from in file "i". If "pr" is non-NULL,
696: * print all skipped stuff with string pr as a prefix.
697: */
698: static int
699: skip(int i, int from, char *pr)
700: {
701: size_t j, n;
702: char *line;
703:
704: for (n = 0; cline[i] < from - 1; n += j) {
705: if ((line = getline(fp[i], &j)) == NULL)
706: return (-1);
707: if (pr != NULL)
708: diff_output("%s%s", pr, line);
709: cline[i]++;
710: }
711: return ((int) n);
712: }
713:
714: /*
715: * Return 1 or 0 according as the old range (in file 1) contains exactly
716: * the same data as the new range (in file 2).
717: */
718: static int
719: duplicate(struct range *r1, struct range *r2)
720: {
721: int c,d;
722: int nchar;
723: int nline;
724:
725: if (r1->to-r1->from != r2->to-r2->from)
726: return (0);
727: (void)skip(0, r1->from, NULL);
728: (void)skip(1, r2->from, NULL);
729: nchar = 0;
730: for (nline=0; nline < r1->to - r1->from; nline++) {
731: do {
732: c = getc(fp[0]);
733: d = getc(fp[1]);
734: if (c == -1 || d== -1)
735: return (-1);
736: nchar++;
737: if (c != d) {
738: repos(nchar);
739: return (0);
740: }
741: } while (c != '\n');
742: }
743: repos(nchar);
744: return (1);
745: }
746:
747: static void
748: repos(int nchar)
749: {
750: int i;
751:
752: for (i = 0; i < 2; i++)
753: (void)fseek(fp[i], (long)-nchar, 1);
754: }
755:
756: /*
757: * collect an editing script for later regurgitation
758: */
759: static int
760: edit(struct diff *diff, int fdup, int j)
761: {
762: if (((fdup + 1) & eflag) == 0)
763: return (j);
764: j++;
765: overlap[j] = !fdup;
766: if (!fdup)
767: overlapcnt++;
768: de[j].old.from = diff->old.from;
769: de[j].old.to = diff->old.to;
770: de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
771: de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
772: return (j);
773: }
774:
775: /* regurgitate */
776: static int
777: edscript(int n)
778: {
779: int j, k;
1.2 joris 780: char block[BUFSIZ+1];
1.1 joris 781:
782: for (n = n; n > 0; n--) {
783: if (!oflag || !overlap[n])
784: prange(&de[n].old);
785: else
786: diff_output("%da\n=======\n", de[n].old.to -1);
787: (void)fseek(fp[2], (long)de[n].new.from, 0);
788: for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
789: j = k > BUFSIZ ? BUFSIZ : k;
1.11 reyk 790: if (fread(block, (size_t)1, (size_t)j,
791: fp[2]) != (size_t)j)
1.1 joris 792: return (-1);
793: block[j] = '\0';
794: diff_output("%s", block);
795: }
1.2 joris 796:
1.1 joris 797: if (!oflag || !overlap[n])
798: diff_output(".\n");
799: else {
800: diff_output("%s\n.\n", f3mark);
801: diff_output("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
802: }
803: }
804:
805: return (overlapcnt);
806: }
807:
808: static void
809: increase(void)
810: {
811: struct diff *p;
812: char *q;
813: size_t newsz, incr;
814:
815: /* are the memset(3) calls needed? */
816: newsz = szchanges == 0 ? 64 : 2 * szchanges;
817: incr = newsz - szchanges;
818:
1.22 ray 819: p = xrealloc(d13, newsz, sizeof(*d13));
820: memset(p + szchanges, 0, incr * sizeof(*d13));
1.1 joris 821: d13 = p;
1.22 ray 822: p = xrealloc(d23, newsz, sizeof(*d23));
823: memset(p + szchanges, 0, incr * sizeof(*d23));
1.1 joris 824: d23 = p;
1.22 ray 825: p = xrealloc(de, newsz, sizeof(*de));
826: memset(p + szchanges, 0, incr * sizeof(*de));
1.1 joris 827: de = p;
1.22 ray 828: q = xrealloc(overlap, newsz, sizeof(*overlap));
829: memset(q + szchanges, 0, incr * sizeof(*overlap));
1.1 joris 830: overlap = q;
831: szchanges = newsz;
832: }