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