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