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

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