[BACK]Return to diff3.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/diff3.c, Revision 1.1

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