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

Annotation of src/usr.bin/rcs/diff3.c, Revision 1.14

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