[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.27

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