Annotation of src/usr.bin/rcs/diff3.c, Revision 1.23
1.23 ! ray 1: /* $OpenBSD: diff3.c,v 1.22 2007/06/30 08:23:49 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.23 ! ray 75: "$OpenBSD: diff3.c,v 1.22 2007/06/30 08:23:49 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.23 ! ray 202: if ((diffreg(path1, path3, d1, 0) == D_ERROR) ||
! 203: (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;
1.22 xsa 285: char *argv[5], r1[RCS_REV_BUFSZ], r2[RCS_REV_BUFSZ];
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.23 ! ray 329: if ((diffreg(path1, path3, d1, 0) == D_ERROR) ||
! 330: (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: do {
636: bufsize += 1024;
637: } while (len + 1 > bufsize);
1.21 ray 638: buf = xrealloc(buf, 1, bufsize);
1.1 joris 639: }
640: memcpy(buf, cp, len - 1);
641: buf[len - 1] = '\n';
642: buf[len] = '\0';
643: if (n != NULL)
644: *n = len;
645:
646: return (buf);
647: }
648:
649: static int
650: merge(size_t m1, size_t m2)
651: {
652: struct diff *d1, *d2, *d3;
653: int dpl, j, t1, t2;
654:
655: d1 = d13;
656: d2 = d23;
657: j = 0;
658: while ((t1 = d1 < d13 + m1) | (t2 = d2 < d23 + m2)) {
659: if (debug) {
660: printf("%d,%d=%d,%d %d,%d=%d,%d\n",
661: d1->old.from, d1->old.to,
662: d1->new.from, d1->new.to,
663: d2->old.from, d2->old.to,
664: d2->new.from, d2->new.to);
665: }
666:
667: /* first file is different from others */
668: if (!t2 || (t1 && d1->new.to < d2->new.from)) {
669: /* stuff peculiar to 1st file */
670: if (eflag==0) {
671: separate("1");
672: change(1, &d1->old, 0);
673: keep(2, &d1->new);
674: change(3, &d1->new, 0);
675: }
676: d1++;
677: continue;
678: }
679:
680: /* second file is different from others */
681: if (!t1 || (t2 && d2->new.to < d1->new.from)) {
682: if (eflag==0) {
683: separate("2");
684: keep(1, &d2->new);
685: change(2, &d2->old, 0);
686: change(3, &d2->new, 0);
687: }
688: d2++;
689: continue;
690: }
691:
692: /*
693: * Merge overlapping changes in first file
694: * this happens after extension (see below).
695: */
696: if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
697: d1[1].old.from = d1->old.from;
698: d1[1].new.from = d1->new.from;
699: d1++;
700: continue;
701: }
702:
703: /* merge overlapping changes in second */
704: if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
705: d2[1].old.from = d2->old.from;
706: d2[1].new.from = d2->new.from;
707: d2++;
708: continue;
709: }
710: /* stuff peculiar to third file or different in all */
711: if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
712: dpl = duplicate(&d1->old,&d2->old);
713: if (dpl == -1)
714: return (-1);
715:
716: /*
717: * dpl = 0 means all files differ
718: * dpl = 1 means files 1 and 2 identical
719: */
720: if (eflag==0) {
721: separate(dpl ? "3" : "");
722: change(1, &d1->old, dpl);
723: change(2, &d2->old, 0);
724: d3 = d1->old.to > d1->old.from ? d1 : d2;
725: change(3, &d3->new, 0);
726: } else
727: j = edit(d1, dpl, j);
728: d1++;
729: d2++;
730: continue;
731: }
732:
733: /*
734: * Overlapping changes from file 1 and 2; extend changes
735: * appropriately to make them coincide.
736: */
737: if (d1->new.from < d2->new.from) {
738: d2->old.from -= d2->new.from-d1->new.from;
739: d2->new.from = d1->new.from;
740: } else if (d2->new.from < d1->new.from) {
741: d1->old.from -= d1->new.from-d2->new.from;
742: d1->new.from = d2->new.from;
743: }
744: if (d1->new.to > d2->new.to) {
745: d2->old.to += d1->new.to - d2->new.to;
746: d2->new.to = d1->new.to;
747: } else if (d2->new.to > d1->new.to) {
748: d1->old.to += d2->new.to - d1->new.to;
749: d1->new.to = d2->new.to;
750: }
751: }
752:
753: return (edscript(j));
754: }
755:
756: static void
757: separate(const char *s)
758: {
759: diff_output("====%s\n", s);
760: }
761:
762: /*
763: * The range of lines rold.from thru rold.to in file i is to be changed.
764: * It is to be printed only if it does not duplicate something to be
765: * printed later.
766: */
767: static void
768: change(int i, struct range *rold, int fdup)
769: {
770: diff_output("%d:", i);
771: last[i] = rold->to;
772: prange(rold);
773: if (fdup || debug)
774: return;
775: i--;
776: (void)skip(i, rold->from, NULL);
777: (void)skip(i, rold->to, " ");
778: }
779:
780: /*
781: * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
782: */
783: static void
784: prange(struct range *rold)
785: {
786: if (rold->to <= rold->from)
787: diff_output("%da\n", rold->from - 1);
788: else {
789: diff_output("%d", rold->from);
790: if (rold->to > rold->from+1)
791: diff_output(",%d", rold->to - 1);
792: diff_output("c\n");
793: }
794: }
795:
796: /*
797: * No difference was reported by diff between file 1 (or 2) and file 3,
798: * and an artificial dummy difference (trange) must be ginned up to
799: * correspond to the change reported in the other file.
800: */
801: static void
802: keep(int i, struct range *rnew)
803: {
804: int delta;
805: struct range trange;
806:
807: delta = last[3] - last[i];
808: trange.from = rnew->from - delta;
809: trange.to = rnew->to - delta;
810: change(i, &trange, 1);
811: }
812:
813: /*
814: * skip to just before line number from in file "i". If "pr" is non-NULL,
815: * print all skipped stuff with string pr as a prefix.
816: */
817: static int
818: skip(int i, int from, char *pr)
819: {
820: size_t j, n;
821: char *line;
822:
823: for (n = 0; cline[i] < from - 1; n += j) {
824: if ((line = getline(fp[i], &j)) == NULL)
825: return (-1);
826: if (pr != NULL)
827: diff_output("%s%s", pr, line);
828: cline[i]++;
829: }
830: return ((int) n);
831: }
832:
833: /*
834: * Return 1 or 0 according as the old range (in file 1) contains exactly
835: * the same data as the new range (in file 2).
836: */
837: static int
838: duplicate(struct range *r1, struct range *r2)
839: {
840: int c,d;
841: int nchar;
842: int nline;
843:
844: if (r1->to-r1->from != r2->to-r2->from)
845: return (0);
846: (void)skip(0, r1->from, NULL);
847: (void)skip(1, r2->from, NULL);
848: nchar = 0;
849: for (nline=0; nline < r1->to - r1->from; nline++) {
850: do {
851: c = getc(fp[0]);
852: d = getc(fp[1]);
853: if (c == -1 || d== -1)
854: return (-1);
855: nchar++;
856: if (c != d) {
857: repos(nchar);
858: return (0);
859: }
860: } while (c != '\n');
861: }
862: repos(nchar);
863: return (1);
864: }
865:
866: static void
867: repos(int nchar)
868: {
869: int i;
870:
871: for (i = 0; i < 2; i++)
872: (void)fseek(fp[i], (long)-nchar, 1);
873: }
874:
875: /*
876: * collect an editing script for later regurgitation
877: */
878: static int
879: edit(struct diff *diff, int fdup, int j)
880: {
881: if (((fdup + 1) & eflag) == 0)
882: return (j);
883: j++;
884: overlap[j] = !fdup;
885: if (!fdup)
886: overlapcnt++;
887: de[j].old.from = diff->old.from;
888: de[j].old.to = diff->old.to;
889: de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
890: de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
891: return (j);
892: }
893:
894: /* regurgitate */
895: static int
896: edscript(int n)
897: {
898: int j, k;
899: char block[BUFSIZ+1];
900:
901: for (n = n; n > 0; n--) {
902: if (!oflag || !overlap[n])
903: prange(&de[n].old);
904: else
905: diff_output("%da\n=======\n", de[n].old.to -1);
906: (void)fseek(fp[2], (long)de[n].new.from, 0);
907: for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
908: j = k > BUFSIZ ? BUFSIZ : k;
1.8 ray 909: if (fread(block, 1, (size_t)j,
1.1 joris 910: fp[2]) != (size_t)j)
911: return (-1);
912: block[j] = '\0';
913: diff_output("%s", block);
914: }
915:
916: if (!oflag || !overlap[n])
917: diff_output(".\n");
918: else {
919: diff_output("%s\n.\n", f3mark);
920: diff_output("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
921: }
922: }
923:
924: return (overlapcnt);
925: }
926:
927: static void
928: increase(void)
929: {
930: size_t newsz, incr;
931:
932: /* are the memset(3) calls needed? */
933: newsz = szchanges == 0 ? 64 : 2 * szchanges;
934: incr = newsz - szchanges;
935:
1.21 ray 936: d13 = xrealloc(d13, newsz, sizeof(*d13));
937: memset(d13 + szchanges, 0, incr * sizeof(*d13));
938: d23 = xrealloc(d23, newsz, sizeof(*d23));
939: memset(d23 + szchanges, 0, incr * sizeof(*d23));
940: de = xrealloc(de, newsz, sizeof(*de));
941: memset(de + szchanges, 0, incr * sizeof(*de));
942: overlap = xrealloc(overlap, newsz, sizeof(*overlap));
943: memset(overlap + szchanges, 0, incr * sizeof(*overlap));
1.1 joris 944: szchanges = newsz;
945: }