Annotation of src/usr.bin/rcs/diff3.c, Revision 1.40
1.40 ! okan 1: /* $OpenBSD: diff3.c,v 1.39 2016/08/26 09:02:54 guenther 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:
1.20 xsa 67: #include <ctype.h>
68: #include <err.h>
69: #include <stdio.h>
70: #include <stdlib.h>
71: #include <string.h>
1.39 guenther 72: #include <time.h>
1.20 xsa 73: #include <unistd.h>
1.1 joris 74:
75: #include "diff.h"
1.4 xsa 76: #include "rcsprog.h"
1.1 joris 77:
78: /* diff3 - 3-way differential file comparison */
79:
80: /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
81: *
82: * d13 = diff report on f1 vs f3
83: * d23 = diff report on f2 vs f3
84: * f1, f2, f3 the 3 files
85: * if changes in f1 overlap with changes in f3, m1 and m3 are used
86: * to mark the overlaps; otherwise, the file names f1 and f3 are used
87: * (only for options E and X).
88: */
89:
90: /*
91: * "from" is first in range of changed lines; "to" is last+1
92: * from=to=line after point of insertion for added lines.
93: */
94: struct range {
95: int from;
96: int to;
97: };
98:
99: struct diff {
100: struct range old;
101: struct range new;
102: };
103:
104: static size_t szchanges;
105:
106: static struct diff *d13;
107: static struct diff *d23;
108:
109: /*
110: * "de" is used to gather editing scripts. These are later spewed out in
111: * reverse order. Its first element must be all zero, the "new" component
112: * of "de" contains line positions or byte positions depending on when you
113: * look (!?). Array overlap indicates which sections in "de" correspond to
114: * lines that are different in all three files.
115: */
116: static struct diff *de;
117: static char *overlap;
118: static int overlapcnt = 0;
119: static FILE *fp[3];
120: static int cline[3]; /* # of the last-read line in each file (0-2) */
121:
122: /*
123: * the latest known correspondence between line numbers of the 3 files
124: * is stored in last[1-3];
125: */
126: static int last[4];
1.14 xsa 127: static int eflag = 3; /* default -E for compatibility with former RCS */
128: static int oflag = 1; /* default -E for compatibility with former RCS */
1.1 joris 129: static int debug = 0;
1.35 deraadt 130: static char f1mark[PATH_MAX], f3mark[PATH_MAX]; /* markers for -E and -X */
1.1 joris 131:
132: static int duplicate(struct range *, struct range *);
133: static int edit(struct diff *, int, int);
134: static char *getchange(FILE *);
1.33 fgsch 135: static char *get_line(FILE *, size_t *);
1.1 joris 136: static int number(char **);
1.26 tobias 137: static ssize_t readin(char *, struct diff **);
1.1 joris 138: static int skip(int, int, char *);
139: static int edscript(int);
140: static int merge(size_t, size_t);
141: static void change(int, struct range *, int);
142: static void keep(int, struct range *);
143: static void prange(struct range *);
144: static void repos(int);
145: static void separate(const char *);
146: static void increase(void);
147: static int diff3_internal(int, char **, const char *, const char *);
148:
149: int diff3_conflicts = 0;
1.9 xsa 150:
151: /*
152: * For merge(1).
153: */
154: BUF *
155: merge_diff3(char **av, int flags)
156: {
157: int argc;
158: char *argv[5], *dp13, *dp23, *path1, *path2, *path3;
159: BUF *b1, *b2, *b3, *d1, *d2, *diffb;
1.17 niallo 160: u_char *data, *patch;
161: size_t dlen, plen;
1.9 xsa 162:
163: b1 = b2 = b3 = d1 = d2 = diffb = NULL;
164: dp13 = dp23 = path1 = path2 = path3 = NULL;
1.17 niallo 165: data = patch = NULL;
1.9 xsa 166:
1.14 xsa 167: if ((flags & MERGE_EFLAG) && !(flags & MERGE_OFLAG))
168: oflag = 0;
169:
1.31 ray 170: if ((b1 = buf_load(av[0])) == NULL)
1.9 xsa 171: goto out;
1.31 ray 172: if ((b2 = buf_load(av[1])) == NULL)
1.9 xsa 173: goto out;
1.31 ray 174: if ((b3 = buf_load(av[2])) == NULL)
1.9 xsa 175: goto out;
176:
1.31 ray 177: d1 = buf_alloc(128);
178: d2 = buf_alloc(128);
179: diffb = buf_alloc(128);
1.9 xsa 180:
181: (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
182: (void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
183: (void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", rcs_tmpdir);
184:
1.30 ray 185: buf_write_stmp(b1, path1);
186: buf_write_stmp(b2, path2);
187: buf_write_stmp(b3, path3);
1.9 xsa 188:
1.30 ray 189: buf_free(b2);
1.9 xsa 190: b2 = NULL;
191:
1.27 ray 192: if ((diffreg(path1, path3, d1, D_FORCEASCII) == D_ERROR) ||
193: (diffreg(path2, path3, d2, D_FORCEASCII) == D_ERROR)) {
1.30 ray 194: buf_free(diffb);
1.9 xsa 195: diffb = NULL;
196: goto out;
197: }
198:
199: (void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
1.30 ray 200: buf_write_stmp(d1, dp13);
1.9 xsa 201:
1.30 ray 202: buf_free(d1);
1.9 xsa 203: d1 = NULL;
204:
205: (void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
1.30 ray 206: buf_write_stmp(d2, dp23);
1.9 xsa 207:
1.30 ray 208: buf_free(d2);
1.9 xsa 209: d2 = NULL;
210:
211: argc = 0;
212: diffbuf = diffb;
213: argv[argc++] = dp13;
214: argv[argc++] = dp23;
215: argv[argc++] = path1;
216: argv[argc++] = path2;
217: argv[argc++] = path3;
218:
219: diff3_conflicts = diff3_internal(argc, argv, av[0], av[2]);
220: if (diff3_conflicts < 0) {
1.30 ray 221: buf_free(diffb);
1.9 xsa 222: diffb = NULL;
223: goto out;
224: }
225:
1.30 ray 226: plen = buf_len(diffb);
227: patch = buf_release(diffb);
228: dlen = buf_len(b1);
229: data = buf_release(b1);
1.17 niallo 230:
231: if ((diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
1.9 xsa 232: goto out;
233:
234: if (!(flags & QUIET) && diff3_conflicts != 0)
235: warnx("warning: overlaps or other problems during merge");
236:
237: out:
1.38 nicm 238: buf_free(b2);
239: buf_free(b3);
240: buf_free(d1);
241: buf_free(d2);
1.9 xsa 242:
243: (void)unlink(path1);
244: (void)unlink(path2);
245: (void)unlink(path3);
246: (void)unlink(dp13);
247: (void)unlink(dp23);
248:
1.36 nicm 249: free(path1);
250: free(path2);
251: free(path3);
252: free(dp13);
253: free(dp23);
254: free(data);
255: free(patch);
1.9 xsa 256:
257: return (diffb);
258: }
1.1 joris 259:
260: BUF *
1.14 xsa 261: rcs_diff3(RCSFILE *rf, char *workfile, RCSNUM *rev1, RCSNUM *rev2, int flags)
1.1 joris 262: {
263: int argc;
1.22 xsa 264: char *argv[5], r1[RCS_REV_BUFSZ], r2[RCS_REV_BUFSZ];
1.5 xsa 265: char *dp13, *dp23, *path1, *path2, *path3;
1.1 joris 266: BUF *b1, *b2, *b3, *d1, *d2, *diffb;
1.17 niallo 267: size_t dlen, plen;
268: u_char *data, *patch;
1.1 joris 269:
270: b1 = b2 = b3 = d1 = d2 = diffb = NULL;
1.7 xsa 271: dp13 = dp23 = path1 = path2 = path3 = NULL;
1.17 niallo 272: data = patch = NULL;
1.1 joris 273:
1.14 xsa 274: if ((flags & MERGE_EFLAG) && !(flags & MERGE_OFLAG))
275: oflag = 0;
276:
1.1 joris 277: rcsnum_tostr(rev1, r1, sizeof(r1));
278: rcsnum_tostr(rev2, r2, sizeof(r2));
279:
1.31 ray 280: if ((b1 = buf_load(workfile)) == NULL)
1.1 joris 281: goto out;
282:
1.14 xsa 283: if (!(flags & QUIET))
1.6 xsa 284: (void)fprintf(stderr, "retrieving revision %s\n", r1);
1.1 joris 285: if ((b2 = rcs_getrev(rf, rev1)) == NULL)
286: goto out;
287:
1.14 xsa 288: if (!(flags & QUIET))
1.6 xsa 289: (void)fprintf(stderr, "retrieving revision %s\n", r2);
1.1 joris 290: if ((b3 = rcs_getrev(rf, rev2)) == NULL)
291: goto out;
292:
1.31 ray 293: d1 = buf_alloc(128);
294: d2 = buf_alloc(128);
295: diffb = buf_alloc(128);
1.1 joris 296:
1.5 xsa 297: (void)xasprintf(&path1, "%s/diff1.XXXXXXXXXX", rcs_tmpdir);
298: (void)xasprintf(&path2, "%s/diff2.XXXXXXXXXX", rcs_tmpdir);
299: (void)xasprintf(&path3, "%s/diff3.XXXXXXXXXX", rcs_tmpdir);
1.3 ray 300:
1.30 ray 301: buf_write_stmp(b1, path1);
302: buf_write_stmp(b2, path2);
303: buf_write_stmp(b3, path3);
1.1 joris 304:
1.30 ray 305: buf_free(b2);
1.1 joris 306: b2 = NULL;
307:
1.27 ray 308: if ((diffreg(path1, path3, d1, D_FORCEASCII) == D_ERROR) ||
309: (diffreg(path2, path3, d2, D_FORCEASCII) == D_ERROR)) {
1.30 ray 310: buf_free(diffb);
1.7 xsa 311: diffb = NULL;
312: goto out;
313: }
1.1 joris 314:
1.5 xsa 315: (void)xasprintf(&dp13, "%s/d13.XXXXXXXXXX", rcs_tmpdir);
1.30 ray 316: buf_write_stmp(d1, dp13);
1.1 joris 317:
1.30 ray 318: buf_free(d1);
1.1 joris 319: d1 = NULL;
320:
1.5 xsa 321: (void)xasprintf(&dp23, "%s/d23.XXXXXXXXXX", rcs_tmpdir);
1.30 ray 322: buf_write_stmp(d2, dp23);
1.1 joris 323:
1.30 ray 324: buf_free(d2);
1.1 joris 325: d2 = NULL;
326:
327: argc = 0;
328: diffbuf = diffb;
329: argv[argc++] = dp13;
330: argv[argc++] = dp23;
331: argv[argc++] = path1;
332: argv[argc++] = path2;
333: argv[argc++] = path3;
334:
335: diff3_conflicts = diff3_internal(argc, argv, workfile, r2);
336: if (diff3_conflicts < 0) {
1.30 ray 337: buf_free(diffb);
1.1 joris 338: diffb = NULL;
339: goto out;
340: }
341:
1.30 ray 342: plen = buf_len(diffb);
343: patch = buf_release(diffb);
344: dlen = buf_len(b1);
345: data = buf_release(b1);
1.17 niallo 346:
347: if ((diffb = rcs_patchfile(data, dlen, patch, plen, ed_patch_lines)) == NULL)
1.1 joris 348: goto out;
349:
1.14 xsa 350: if (!(flags & QUIET) && diff3_conflicts != 0)
1.6 xsa 351: warnx("warning: overlaps or other problems during merge");
1.1 joris 352:
353: out:
1.38 nicm 354: buf_free(b2);
355: buf_free(b3);
356: buf_free(d1);
357: buf_free(d2);
1.1 joris 358:
359: (void)unlink(path1);
360: (void)unlink(path2);
361: (void)unlink(path3);
362: (void)unlink(dp13);
363: (void)unlink(dp23);
1.5 xsa 364:
1.36 nicm 365: free(path1);
366: free(path2);
367: free(path3);
368: free(dp13);
369: free(dp23);
370: free(data);
371: free(patch);
1.1 joris 372:
373: return (diffb);
374: }
375:
376: static int
377: diff3_internal(int argc, char **argv, const char *fmark, const char *rmark)
378: {
1.26 tobias 379: ssize_t m, n;
1.1 joris 380: int i;
381:
382: if (argc < 5)
383: return (-1);
384:
1.14 xsa 385: if (oflag) {
386: i = snprintf(f1mark, sizeof(f1mark), "<<<<<<< %s", fmark);
1.19 xsa 387: if (i < 0 || i >= (int)sizeof(f1mark))
1.14 xsa 388: errx(1, "diff3_internal: string truncated");
389:
390: i = snprintf(f3mark, sizeof(f3mark), ">>>>>>> %s", rmark);
1.19 xsa 391: if (i < 0 || i >= (int)sizeof(f3mark))
1.14 xsa 392: errx(1, "diff3_internal: string truncated");
393: }
1.1 joris 394:
395: increase();
1.26 tobias 396: if ((m = readin(argv[0], &d13)) < 0) {
397: warn("%s", argv[0]);
398: return (-1);
399: }
400: if ((n = readin(argv[1], &d23)) < 0) {
401: warn("%s", argv[1]);
402: return (-1);
403: }
1.1 joris 404:
1.3 ray 405: for (i = 0; i <= 2; i++)
1.1 joris 406: if ((fp[i] = fopen(argv[i + 2], "r")) == NULL) {
407: warn("%s", argv[i + 2]);
408: return (-1);
409: }
410:
411: return (merge(m, n));
412: }
413:
414: int
415: ed_patch_lines(struct rcs_lines *dlines, struct rcs_lines *plines)
416: {
417: char op, *ep;
1.13 espie 418: struct rcs_line *sort, *lp, *dlp, *ndlp, *insert_after;
1.1 joris 419: int start, end, i, lineno;
1.17 niallo 420: u_char tmp;
1.1 joris 421:
422: dlp = TAILQ_FIRST(&(dlines->l_lines));
423: lp = TAILQ_FIRST(&(plines->l_lines));
424:
425: end = 0;
426: for (lp = TAILQ_NEXT(lp, l_list); lp != NULL;
427: lp = TAILQ_NEXT(lp, l_list)) {
1.17 niallo 428: /* Skip blank lines */
429: if (lp->l_len < 2)
430: continue;
1.30 ray 431:
1.17 niallo 432: /* NUL-terminate line buffer for strtol() safety. */
433: tmp = lp->l_line[lp->l_len - 1];
434: lp->l_line[lp->l_len - 1] = '\0';
1.30 ray 435:
1.17 niallo 436: /* len - 1 is NUL terminator so we use len - 2 for 'op' */
437: op = lp->l_line[lp->l_len - 2];
1.1 joris 438: start = (int)strtol(lp->l_line, &ep, 10);
1.30 ray 439:
1.17 niallo 440: /* Restore the last byte of the buffer */
441: lp->l_line[lp->l_len - 1] = tmp;
1.30 ray 442:
1.1 joris 443: if (op == 'a') {
444: if (start > dlines->l_nblines ||
445: start < 0 || *ep != 'a')
446: errx(1, "ed_patch_lines");
447: } else if (op == 'c') {
448: if (start > dlines->l_nblines ||
449: start < 0 || (*ep != ',' && *ep != 'c'))
450: errx(1, "ed_patch_lines");
451:
452: if (*ep == ',') {
453: ep++;
454: end = (int)strtol(ep, &ep, 10);
455: if (end < 0 || *ep != 'c')
456: errx(1, "ed_patch_lines");
457: } else {
458: end = start;
459: }
460: }
461:
462:
463: for (;;) {
464: if (dlp == NULL)
465: break;
466: if (dlp->l_lineno == start)
467: break;
468: if (dlp->l_lineno > start) {
1.29 ray 469: dlp = TAILQ_PREV(dlp, tqh, l_list);
1.1 joris 470: } else if (dlp->l_lineno < start) {
471: ndlp = TAILQ_NEXT(dlp, l_list);
472: if (ndlp->l_lineno > start)
473: break;
474: dlp = ndlp;
475: }
476: }
477:
478: if (dlp == NULL)
479: errx(1, "ed_patch_lines");
480:
481:
482: if (op == 'c') {
1.29 ray 483: insert_after = TAILQ_PREV(dlp, tqh, l_list);
1.1 joris 484: for (i = 0; i <= (end - start); i++) {
485: ndlp = TAILQ_NEXT(dlp, l_list);
486: TAILQ_REMOVE(&(dlines->l_lines), dlp, l_list);
487: dlp = ndlp;
488: }
1.13 espie 489: dlp = insert_after;
1.1 joris 490: }
491:
492: if (op == 'a' || op == 'c') {
493: for (;;) {
494: ndlp = lp;
495: lp = TAILQ_NEXT(lp, l_list);
496: if (lp == NULL)
497: errx(1, "ed_patch_lines");
498:
1.17 niallo 499: if (!memcmp(lp->l_line, ".", 1))
1.1 joris 500: break;
501:
502: TAILQ_REMOVE(&(plines->l_lines), lp, l_list);
503: TAILQ_INSERT_AFTER(&(dlines->l_lines), dlp,
504: lp, l_list);
505: dlp = lp;
506:
507: lp->l_lineno = start;
508: lp = ndlp;
509: }
510: }
511:
512: /*
513: * always resort lines as the markers might be put at the
514: * same line as we first started editing.
515: */
516: lineno = 0;
517: TAILQ_FOREACH(sort, &(dlines->l_lines), l_list)
518: sort->l_lineno = lineno++;
519: dlines->l_nblines = lineno - 1;
520: }
521:
522: return (0);
523: }
524:
525: /*
526: * Pick up the line numbers of all changes from one change file.
527: * (This puts the numbers in a vector, which is not strictly necessary,
528: * since the vector is processed in one sequential pass.
529: * The vector could be optimized out of existence)
530: */
1.26 tobias 531: static ssize_t
1.1 joris 532: readin(char *name, struct diff **dd)
533: {
534: int a, b, c, d;
535: char kind, *p;
536: size_t i;
537:
538: fp[0] = fopen(name, "r");
1.26 tobias 539: if (fp[0] == NULL)
540: return (-1);
1.1 joris 541: for (i = 0; (p = getchange(fp[0])); i++) {
542: if (i >= szchanges - 1)
543: increase();
544: a = b = number(&p);
545: if (*p == ',') {
546: p++;
547: b = number(&p);
548: }
549: kind = *p++;
550: c = d = number(&p);
551: if (*p==',') {
552: p++;
553: d = number(&p);
554: }
555: if (kind == 'a')
556: a++;
557: if (kind == 'd')
558: c++;
559: b++;
560: d++;
561: (*dd)[i].old.from = a;
562: (*dd)[i].old.to = b;
563: (*dd)[i].new.from = c;
564: (*dd)[i].new.to = d;
565: }
566:
567: if (i) {
568: (*dd)[i].old.from = (*dd)[i-1].old.to;
569: (*dd)[i].new.from = (*dd)[i-1].new.to;
570: }
1.30 ray 571:
1.1 joris 572: (void)fclose(fp[0]);
573:
574: return (i);
575: }
576:
577: static int
578: number(char **lc)
579: {
580: int nn;
581:
582: nn = 0;
583: while (isdigit((unsigned char)(**lc)))
584: nn = nn*10 + *(*lc)++ - '0';
585:
586: return (nn);
587: }
588:
589: static char *
590: getchange(FILE *b)
591: {
592: char *line;
593:
1.33 fgsch 594: while ((line = get_line(b, NULL))) {
1.1 joris 595: if (isdigit((unsigned char)line[0]))
596: return (line);
597: }
598:
599: return (NULL);
600: }
601:
602: static char *
1.33 fgsch 603: get_line(FILE *b, size_t *n)
1.1 joris 604: {
605: char *cp;
606: size_t len;
607: static char *buf;
608: static size_t bufsize;
609:
610: if ((cp = fgetln(b, &len)) == NULL)
611: return (NULL);
612:
613: if (cp[len - 1] != '\n')
614: len++;
615: if (len + 1 > bufsize) {
616: do {
617: bufsize += 1024;
618: } while (len + 1 > bufsize);
1.34 deraadt 619: buf = xreallocarray(buf, 1, bufsize);
1.1 joris 620: }
621: memcpy(buf, cp, len - 1);
622: buf[len - 1] = '\n';
623: buf[len] = '\0';
624: if (n != NULL)
625: *n = len;
626:
627: return (buf);
628: }
629:
630: static int
631: merge(size_t m1, size_t m2)
632: {
633: struct diff *d1, *d2, *d3;
634: int dpl, j, t1, t2;
635:
636: d1 = d13;
637: d2 = d23;
638: j = 0;
1.37 jsg 639: while ((t1 = (d1 < d13 + m1)) | (t2 = (d2 < d23 + m2))) {
1.1 joris 640: if (debug) {
641: printf("%d,%d=%d,%d %d,%d=%d,%d\n",
642: d1->old.from, d1->old.to,
643: d1->new.from, d1->new.to,
644: d2->old.from, d2->old.to,
645: d2->new.from, d2->new.to);
646: }
647:
648: /* first file is different from others */
649: if (!t2 || (t1 && d1->new.to < d2->new.from)) {
650: /* stuff peculiar to 1st file */
651: if (eflag==0) {
652: separate("1");
653: change(1, &d1->old, 0);
654: keep(2, &d1->new);
655: change(3, &d1->new, 0);
656: }
657: d1++;
658: continue;
659: }
660:
661: /* second file is different from others */
662: if (!t1 || (t2 && d2->new.to < d1->new.from)) {
663: if (eflag==0) {
664: separate("2");
665: keep(1, &d2->new);
666: change(2, &d2->old, 0);
667: change(3, &d2->new, 0);
668: }
669: d2++;
670: continue;
671: }
672:
673: /*
674: * Merge overlapping changes in first file
675: * this happens after extension (see below).
676: */
677: if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
678: d1[1].old.from = d1->old.from;
679: d1[1].new.from = d1->new.from;
680: d1++;
681: continue;
682: }
683:
684: /* merge overlapping changes in second */
685: if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
686: d2[1].old.from = d2->old.from;
687: d2[1].new.from = d2->new.from;
688: d2++;
689: continue;
690: }
691: /* stuff peculiar to third file or different in all */
692: if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
693: dpl = duplicate(&d1->old,&d2->old);
694: if (dpl == -1)
695: return (-1);
696:
697: /*
698: * dpl = 0 means all files differ
699: * dpl = 1 means files 1 and 2 identical
700: */
701: if (eflag==0) {
702: separate(dpl ? "3" : "");
703: change(1, &d1->old, dpl);
704: change(2, &d2->old, 0);
705: d3 = d1->old.to > d1->old.from ? d1 : d2;
706: change(3, &d3->new, 0);
707: } else
708: j = edit(d1, dpl, j);
709: d1++;
710: d2++;
711: continue;
712: }
713:
714: /*
715: * Overlapping changes from file 1 and 2; extend changes
716: * appropriately to make them coincide.
717: */
718: if (d1->new.from < d2->new.from) {
719: d2->old.from -= d2->new.from-d1->new.from;
720: d2->new.from = d1->new.from;
721: } else if (d2->new.from < d1->new.from) {
722: d1->old.from -= d1->new.from-d2->new.from;
723: d1->new.from = d2->new.from;
724: }
725: if (d1->new.to > d2->new.to) {
726: d2->old.to += d1->new.to - d2->new.to;
727: d2->new.to = d1->new.to;
728: } else if (d2->new.to > d1->new.to) {
729: d1->old.to += d2->new.to - d1->new.to;
730: d1->new.to = d2->new.to;
731: }
732: }
733:
734: return (edscript(j));
735: }
736:
737: static void
738: separate(const char *s)
739: {
740: diff_output("====%s\n", s);
741: }
742:
743: /*
744: * The range of lines rold.from thru rold.to in file i is to be changed.
745: * It is to be printed only if it does not duplicate something to be
746: * printed later.
747: */
748: static void
749: change(int i, struct range *rold, int fdup)
750: {
751: diff_output("%d:", i);
752: last[i] = rold->to;
753: prange(rold);
754: if (fdup || debug)
755: return;
756: i--;
757: (void)skip(i, rold->from, NULL);
758: (void)skip(i, rold->to, " ");
759: }
760:
761: /*
762: * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
763: */
764: static void
765: prange(struct range *rold)
766: {
767: if (rold->to <= rold->from)
768: diff_output("%da\n", rold->from - 1);
769: else {
770: diff_output("%d", rold->from);
771: if (rold->to > rold->from+1)
772: diff_output(",%d", rold->to - 1);
773: diff_output("c\n");
774: }
775: }
776:
777: /*
778: * No difference was reported by diff between file 1 (or 2) and file 3,
779: * and an artificial dummy difference (trange) must be ginned up to
780: * correspond to the change reported in the other file.
781: */
782: static void
783: keep(int i, struct range *rnew)
784: {
785: int delta;
786: struct range trange;
787:
788: delta = last[3] - last[i];
789: trange.from = rnew->from - delta;
790: trange.to = rnew->to - delta;
791: change(i, &trange, 1);
792: }
793:
794: /*
795: * skip to just before line number from in file "i". If "pr" is non-NULL,
796: * print all skipped stuff with string pr as a prefix.
797: */
798: static int
799: skip(int i, int from, char *pr)
800: {
801: size_t j, n;
802: char *line;
803:
804: for (n = 0; cline[i] < from - 1; n += j) {
1.33 fgsch 805: if ((line = get_line(fp[i], &j)) == NULL)
1.1 joris 806: return (-1);
807: if (pr != NULL)
808: diff_output("%s%s", pr, line);
809: cline[i]++;
810: }
811: return ((int) n);
812: }
813:
814: /*
815: * Return 1 or 0 according as the old range (in file 1) contains exactly
816: * the same data as the new range (in file 2).
817: */
818: static int
819: duplicate(struct range *r1, struct range *r2)
820: {
821: int c,d;
822: int nchar;
823: int nline;
824:
825: if (r1->to-r1->from != r2->to-r2->from)
826: return (0);
827: (void)skip(0, r1->from, NULL);
828: (void)skip(1, r2->from, NULL);
829: nchar = 0;
830: for (nline=0; nline < r1->to - r1->from; nline++) {
831: do {
832: c = getc(fp[0]);
833: d = getc(fp[1]);
834: if (c == -1 || d== -1)
835: return (-1);
836: nchar++;
837: if (c != d) {
838: repos(nchar);
839: return (0);
840: }
841: } while (c != '\n');
842: }
843: repos(nchar);
844: return (1);
845: }
846:
847: static void
848: repos(int nchar)
849: {
850: int i;
851:
852: for (i = 0; i < 2; i++)
1.24 tobias 853: (void)fseek(fp[i], (long)-nchar, SEEK_CUR);
1.1 joris 854: }
855:
856: /*
857: * collect an editing script for later regurgitation
858: */
859: static int
860: edit(struct diff *diff, int fdup, int j)
861: {
862: if (((fdup + 1) & eflag) == 0)
863: return (j);
864: j++;
865: overlap[j] = !fdup;
866: if (!fdup)
867: overlapcnt++;
868: de[j].old.from = diff->old.from;
869: de[j].old.to = diff->old.to;
870: de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
871: de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
872: return (j);
873: }
874:
875: /* regurgitate */
876: static int
877: edscript(int n)
878: {
879: int j, k;
880: char block[BUFSIZ+1];
881:
1.32 nicm 882: for (; n > 0; n--) {
1.1 joris 883: if (!oflag || !overlap[n])
884: prange(&de[n].old);
885: else
886: diff_output("%da\n=======\n", de[n].old.to -1);
1.24 tobias 887: (void)fseek(fp[2], (long)de[n].new.from, SEEK_SET);
1.1 joris 888: for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
889: j = k > BUFSIZ ? BUFSIZ : k;
1.40 ! okan 890: if (fread(block, 1, j, fp[2]) != (size_t)j)
1.1 joris 891: return (-1);
892: block[j] = '\0';
893: diff_output("%s", block);
894: }
895:
896: if (!oflag || !overlap[n])
897: diff_output(".\n");
898: else {
899: diff_output("%s\n.\n", f3mark);
900: diff_output("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
901: }
902: }
903:
904: return (overlapcnt);
905: }
906:
907: static void
908: increase(void)
909: {
910: size_t newsz, incr;
911:
912: /* are the memset(3) calls needed? */
913: newsz = szchanges == 0 ? 64 : 2 * szchanges;
914: incr = newsz - szchanges;
915:
1.34 deraadt 916: d13 = xreallocarray(d13, newsz, sizeof(*d13));
1.21 ray 917: memset(d13 + szchanges, 0, incr * sizeof(*d13));
1.34 deraadt 918: d23 = xreallocarray(d23, newsz, sizeof(*d23));
1.21 ray 919: memset(d23 + szchanges, 0, incr * sizeof(*d23));
1.34 deraadt 920: de = xreallocarray(de, newsz, sizeof(*de));
1.21 ray 921: memset(de + szchanges, 0, incr * sizeof(*de));
1.34 deraadt 922: overlap = xreallocarray(overlap, newsz, sizeof(*overlap));
1.21 ray 923: memset(overlap + szchanges, 0, incr * sizeof(*overlap));
1.1 joris 924: szchanges = newsz;
925: }