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