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

Annotation of src/usr.bin/diff3/diff3prog.c, Revision 1.5

1.5     ! canacar     1: /*     $OpenBSD: diff3prog.c,v 1.4 2003/11/09 20:13:57 otto Exp $      */
1.1       millert     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.5     ! canacar    74: static const char rcsid[] = "$OpenBSD: diff3prog.c,v 1.4 2003/11/09 20:13:57 otto Exp $";
1.1       millert    75: #endif /* not lint */
                     76:
                     77: #include <stdio.h>
                     78: #include <stdlib.h>
                     79: #include <ctype.h>
                     80: #include <err.h>
                     81:
                     82: /* diff3 - 3-way differential file comparison */
                     83:
                     84: /* diff3 [-ex3EX] d13 d23 f1 f2 f3 [m1 m3]
                     85:  *
                     86:  * d13 = diff report on f1 vs f3
                     87:  * d23 = diff report on f2 vs f3
                     88:  * f1, f2, f3 the 3 files
                     89:  * if changes in f1 overlap with changes in f3, m1 and m3 are used
                     90:  * to mark the overlaps; otherwise, the file names f1 and f3 are used
                     91:  * (only for options E and X).
                     92:  */
                     93:
                     94: /*
                     95:  * "from" is first in range of changed lines; "to" is last+1
                     96:  * from=to=line after point of insertion for added lines.
                     97:  */
                     98: struct  range {
                     99:        int from;
                    100:        int to;
                    101: };
                    102: struct diff {
                    103:        struct range old;
                    104:        struct range new;
                    105: };
                    106:
                    107: #define NC 200
                    108: struct diff d13[NC];
                    109: struct diff d23[NC];
                    110: /*
                    111:  * "de" is used to gather editing scripts.  These are later spewed out in
                    112:  * reverse order.  Its first element must be all zero, the "new" component
                    113:  * of "de" contains line positions or byte positions depending on when you
                    114:  * look (!?).  Array overlap indicates which sections in "de" correspond to
                    115:  * lines that are different in all three files.
                    116:  */
                    117: struct diff de[NC];
                    118: char overlap[NC];
                    119: int  overlapcnt;
                    120: char line[256];
                    121: FILE *fp[3];
                    122: int cline[3];          /* # of the last-read line in each file (0-2) */
                    123: /*
                    124:  * the latest known correspondence between line numbers of the 3 files
                    125:  * is stored in last[1-3];
                    126:  */
                    127: int last[4];
                    128: int eflag;
1.2       deraadt   129: int oflag;             /* indicates whether to mark overlaps (-E or -X)*/
1.1       millert   130: int debug  = 0;
                    131: char f1mark[40], f3mark[40];   /* markers for -E and -X */
                    132:
                    133: int duplicate(struct range *, struct range *);
                    134: int edit(struct diff *, int, int);
                    135: int getchange(FILE *);
                    136: int getline(FILE *);
                    137: int number(char **);
                    138: int readin(char *, struct diff *);
                    139: int skip(int, int, char *);
                    140: void change(int, struct range *, int);
                    141: void keep(int, struct range *);
                    142: void merge(int, int);
                    143: void prange(struct range *);
                    144: void repos(int);
                    145: void separate(const char *);
                    146: __dead void edscript(int);
                    147: __dead void trouble(void);
                    148: __dead void usage(void);
                    149:
                    150: int
                    151: main(int argc, char **argv)
                    152: {
                    153:        int ch, i, m, n;
                    154:
1.2       deraadt   155:        eflag = 0;
1.1       millert   156:        oflag = 0;
                    157:        while ((ch = getopt(argc, argv, "EeXx3")) != -1) {
                    158:                switch (ch) {
1.2       deraadt   159:                case 'E':
                    160:                        eflag = 3;
                    161:                        oflag = 1;
                    162:                        break;
1.1       millert   163:                case 'e':
                    164:                        eflag = 3;
                    165:                        break;
1.2       deraadt   166:                case 'X':
                    167:                        oflag = eflag = 1;
                    168:                        break;
1.1       millert   169:                case 'x':
                    170:                        eflag = 1;
1.2       deraadt   171:                        break;
1.1       millert   172:                case '3':
                    173:                        eflag = 2;
                    174:                        break;
                    175:                }
                    176:        }
                    177:        argc -= optind;
                    178:        argv += optind;
                    179:        /* XXX - argc usage seems wrong here */
                    180:        if (argc < 5)
                    181:                usage();
                    182:
1.2       deraadt   183:        if (oflag) {
                    184:                (void)snprintf(f1mark, sizeof(f1mark), "<<<<<<< %s",
1.1       millert   185:                    argc >= 6 ? argv[5] : argv[2]);
1.2       deraadt   186:                (void)snprintf(f3mark, sizeof(f3mark), ">>>>>>> %s",
1.1       millert   187:                    argc >= 7 ? argv[6] : argv[4]);
1.2       deraadt   188:        }
1.1       millert   189:
                    190:        m = readin(argv[0], d13);
                    191:        n = readin(argv[1], d23);
                    192:        for (i = 0; i <= 2; i++) {
                    193:                if ((fp[i] = fopen(argv[i + 2], "r")) == NULL) {
                    194:                        printf("diff3: can't open %s\n", argv[i + 2]);
                    195:                        exit(EXIT_FAILURE);
                    196:                }
                    197:        }
                    198:        merge(m, n);
                    199:        exit(EXIT_SUCCESS);
                    200: }
                    201:
                    202: /*
1.4       otto      203:  * Pick up the line numbers of all changes from one change file.
1.1       millert   204:  * (This puts the numbers in a vector, which is not strictly necessary,
                    205:  * since the vector is processed in one sequential pass.
                    206:  * The vector could be optimized out of existence)
                    207:  */
                    208: int
                    209: readin(char *name, struct diff *dd)
                    210: {
                    211:        int i;
                    212:        int a,b,c,d;
                    213:        char kind;
                    214:        char *p;
                    215:        fp[0] = fopen(name, "r");
                    216:        for (i=0; getchange(fp[0]); i++) {
                    217:                if (i >= NC)
                    218:                        err(EXIT_FAILURE, "too many changes");
                    219:                p = line;
                    220:                a = b = number(&p);
                    221:                if (*p == ',') {
                    222:                        p++;
                    223:                        b = number(&p);
                    224:                }
                    225:                kind = *p++;
                    226:                c = d = number(&p);
                    227:                if (*p==',') {
                    228:                        p++;
                    229:                        d = number(&p);
                    230:                }
                    231:                if (kind == 'a')
                    232:                        a++;
                    233:                if (kind == 'd')
                    234:                        c++;
                    235:                b++;
                    236:                d++;
                    237:                dd[i].old.from = a;
                    238:                dd[i].old.to = b;
                    239:                dd[i].new.from = c;
                    240:                dd[i].new.to = d;
                    241:        }
                    242:        dd[i].old.from = dd[i-1].old.to;
                    243:        dd[i].new.from = dd[i-1].new.to;
                    244:        (void)fclose(fp[0]);
                    245:        return (i);
                    246: }
                    247:
                    248: int
                    249: number(char **lc)
                    250: {
                    251:        int nn;
                    252:        nn = 0;
                    253:        while (isdigit((unsigned char)(**lc)))
                    254:                nn = nn*10 + *(*lc)++ - '0';
                    255:        return (nn);
                    256: }
                    257:
                    258: int
                    259: getchange(FILE *b)
                    260: {
                    261:        while (getline(b)) {
                    262:                if (isdigit((unsigned char)line[0]))
                    263:                        return (1);
                    264:        }
                    265:        return (0);
                    266: }
                    267:
                    268: int
                    269: getline(FILE *b)
                    270: {
                    271:        int i, c;
                    272:
                    273:        for (i = 0; i < sizeof(line) - 1; i++) {
                    274:                c = getc(b);
                    275:                if (c == EOF)
                    276:                        break;
                    277:                line[i] = c;
                    278:                if (c == '\n') {
                    279:                        line[++i] = 0;
                    280:                        return (i);
                    281:                }
                    282:        }
                    283:        return (0);
                    284: }
                    285:
                    286: void
                    287: merge(int m1, int m2)
                    288: {
                    289:        struct diff *d1, *d2, *d3;
                    290:        int dup, j, t1, t2;
                    291:
                    292:        d1 = d13;
                    293:        d2 = d23;
                    294:        j = 0;
                    295:        while ((t1 = d1 < d13 + m1) | (t2 = d2 < d23 + m2)) {
                    296:                if (debug) {
                    297:                        printf("%d,%d=%d,%d %d,%d=%d,%d\n",
                    298:                        d1->old.from,d1->old.to,
                    299:                        d1->new.from,d1->new.to,
                    300:                        d2->old.from,d2->old.to,
                    301:                        d2->new.from,d2->new.to);
                    302:                }
                    303:                /* first file is different from others */
1.5     ! canacar   304:                if (!t2 || (t1 && d1->new.to < d2->new.from)) {
1.1       millert   305:                        /* stuff peculiar to 1st file */
                    306:                        if (eflag==0) {
                    307:                                separate("1");
                    308:                                change(1, &d1->old, 0);
                    309:                                keep(2, &d1->new);
                    310:                                change(3, &d1->new, 0);
                    311:                        }
                    312:                        d1++;
                    313:                        continue;
                    314:                }
                    315:                /* second file is different from others */
1.5     ! canacar   316:                if (!t1 || (t2 && d2->new.to < d1->new.from)) {
1.1       millert   317:                        if (eflag==0) {
                    318:                                separate("2");
                    319:                                keep(1, &d2->new);
                    320:                                change(2, &d2->old, 0);
                    321:                                change(3, &d2->new, 0);
                    322:                        }
                    323:                        d2++;
                    324:                        continue;
                    325:                }
                    326:                /*
                    327:                 * Merge overlapping changes in first file
                    328:                 * this happens after extension (see below).
                    329:                 */
                    330:                if (d1 + 1 < d13 + m1 && d1->new.to >= d1[1].new.from) {
                    331:                        d1[1].old.from = d1->old.from;
                    332:                        d1[1].new.from = d1->new.from;
                    333:                        d1++;
                    334:                        continue;
                    335:                }
                    336:
                    337:                /* merge overlapping changes in second */
                    338:                if (d2 + 1 < d23 + m2 && d2->new.to >= d2[1].new.from) {
                    339:                        d2[1].old.from = d2->old.from;
                    340:                        d2[1].new.from = d2->new.from;
                    341:                        d2++;
                    342:                        continue;
                    343:                }
                    344:                /* stuff peculiar to third file or different in all */
                    345:                if (d1->new.from == d2->new.from && d1->new.to == d2->new.to) {
                    346:                        dup = duplicate(&d1->old,&d2->old);
                    347:                        /*
                    348:                         * dup = 0 means all files differ
1.4       otto      349:                         * dup = 1 means files 1 and 2 identical
1.1       millert   350:                         */
                    351:                        if (eflag==0) {
                    352:                                separate(dup ? "3" : "");
                    353:                                change(1, &d1->old, dup);
                    354:                                change(2, &d2->old, 0);
                    355:                                d3 = d1->old.to > d1->old.from ? d1 : d2;
                    356:                                change(3, &d3->new, 0);
                    357:                        } else
                    358:                                j = edit(d1, dup, j);
                    359:                        d1++;
                    360:                        d2++;
                    361:                        continue;
                    362:                }
                    363:                /*
                    364:                 * Overlapping changes from file 1 and 2; extend changes
                    365:                 * appropriately to make them coincide.
                    366:                 */
1.2       deraadt   367:                if (d1->new.from < d2->new.from) {
1.1       millert   368:                        d2->old.from -= d2->new.from-d1->new.from;
                    369:                        d2->new.from = d1->new.from;
                    370:                } else if (d2->new.from < d1->new.from) {
                    371:                        d1->old.from -= d1->new.from-d2->new.from;
                    372:                        d1->new.from = d2->new.from;
                    373:                }
                    374:                if (d1->new.to > d2->new.to) {
                    375:                        d2->old.to += d1->new.to - d2->new.to;
                    376:                        d2->new.to = d1->new.to;
                    377:                } else if (d2->new.to > d1->new.to) {
                    378:                        d1->old.to += d2->new.to - d1->new.to;
                    379:                        d1->new.to = d2->new.to;
                    380:                }
                    381:        }
                    382:        if (eflag)
                    383:                edscript(j);
                    384: }
                    385:
                    386: void
                    387: separate(const char *s)
                    388: {
                    389:        printf("====%s\n", s);
                    390: }
                    391:
                    392: /*
                    393:  * The range of lines rold.from thru rold.to in file i is to be changed.
                    394:  * It is to be printed only if it does not duplicate something to be
                    395:  * printed later.
                    396:  */
                    397: void
                    398: change(int i, struct range *rold, int dup)
                    399: {
                    400:        printf("%d:", i);
                    401:        last[i] = rold->to;
                    402:        prange(rold);
                    403:        if (dup || debug)
                    404:                return;
                    405:        i--;
                    406:        (void)skip(i, rold->from, NULL);
                    407:        (void)skip(i, rold->to, "  ");
                    408: }
                    409:
                    410: /*
                    411:  * print the range of line numbers, rold.from thru rold.to, as n1,n2 or n1
                    412:  */
                    413: void
                    414: prange(struct range *rold)
                    415: {
                    416:        if (rold->to <= rold->from)
                    417:                printf("%da\n", rold->from - 1);
                    418:        else {
                    419:                printf("%d", rold->from);
                    420:                if (rold->to > rold->from+1)
                    421:                        printf(",%d", rold->to - 1);
                    422:                printf("c\n");
                    423:        }
                    424: }
                    425:
                    426: /*
                    427:  * No difference was reported by diff between file 1 (or 2) and file 3,
                    428:  * and an artificial dummy difference (trange) must be ginned up to
                    429:  * correspond to the change reported in the other file.
                    430:  */
                    431: void
                    432: keep(int i, struct range *rnew)
                    433: {
                    434:        int delta;
                    435:        struct range trange;
                    436:
                    437:        delta = last[3] - last[i];
                    438:        trange.from = rnew->from - delta;
                    439:        trange.to = rnew->to - delta;
                    440:        change(i, &trange, 1);
                    441: }
                    442:
                    443: /*
1.4       otto      444:  * skip to just before line number from in file "i".  If "pr" is non-NULL,
1.1       millert   445:  * print all skipped stuff with string pr as a prefix.
                    446:  */
                    447: int
                    448: skip(int i, int from, char *pr)
                    449: {
                    450:        int j, n;
                    451:
                    452:        for (n = 0; cline[i] < from - 1; n += j) {
1.3       avsm      453:                if ((j = getline(fp[i])) == 0)
1.1       millert   454:                        trouble();
                    455:                if (pr != NULL)
                    456:                        printf("%s%s", pr, line);
                    457:                cline[i]++;
                    458:        }
                    459:        return (n);
                    460: }
                    461:
                    462: /*
                    463:  * Return 1 or 0 according as the old range (in file 1) contains exactly
                    464:  * the same data as the new range (in file 2).
                    465:  */
                    466: int
                    467: duplicate(struct range *r1, struct range *r2)
                    468: {
                    469:        int c,d;
                    470:        int nchar;
                    471:        int nline;
                    472:
                    473:        if (r1->to-r1->from != r2->to-r2->from)
                    474:                return (0);
                    475:        (void)skip(0, r1->from, NULL);
                    476:        (void)skip(1, r2->from, NULL);
                    477:        nchar = 0;
                    478:        for (nline=0; nline < r1->to - r1->from; nline++) {
                    479:                do {
                    480:                        c = getc(fp[0]);
                    481:                        d = getc(fp[1]);
                    482:                        if (c == -1 || d== -1)
                    483:                                trouble();
                    484:                        nchar++;
                    485:                        if (c != d) {
                    486:                                repos(nchar);
                    487:                                return (0);
                    488:                        }
                    489:                } while (c != '\n');
                    490:        }
                    491:        repos(nchar);
                    492:        return (1);
                    493: }
                    494:
                    495: void
                    496: repos(int nchar)
                    497: {
                    498:        int i;
                    499:
1.2       deraadt   500:        for (i = 0; i < 2; i++)
1.1       millert   501:                (void)fseek(fp[i], (long)-nchar, 1);
                    502: }
                    503:
                    504: __dead void
                    505: trouble(void)
                    506: {
                    507:        errx(EXIT_FAILURE, "logic error");
                    508: }
                    509:
                    510: /*
                    511:  * collect an editing script for later regurgitation
                    512:  */
                    513: int
                    514: edit(struct diff *diff, int dup, int j)
                    515: {
                    516:        if (((dup + 1) & eflag) == 0)
                    517:                return (j);
                    518:        j++;
1.2       deraadt   519:        overlap[j] = !dup;
                    520:        if (!dup)
1.1       millert   521:                overlapcnt++;
                    522:        de[j].old.from = diff->old.from;
                    523:        de[j].old.to = diff->old.to;
                    524:        de[j].new.from = de[j-1].new.to + skip(2, diff->new.from, NULL);
                    525:        de[j].new.to = de[j].new.from + skip(2, diff->new.to, NULL);
                    526:        return (j);
                    527: }
                    528:
                    529: /* regurgitate */
                    530: __dead void
                    531: edscript(int n)
                    532: {
                    533:        int j,k;
                    534:        char block[BUFSIZ];
                    535:
                    536:        for (n = n; n > 0; n--) {
1.2       deraadt   537:                if (!oflag || !overlap[n])
                    538:                        prange(&de[n].old);
                    539:                else
                    540:                        printf("%da\n=======\n", de[n].old.to -1);
1.1       millert   541:                (void)fseek(fp[2], (long)de[n].new.from, 0);
                    542:                for (k = de[n].new.to-de[n].new.from; k > 0; k-= j) {
                    543:                        j = k > BUFSIZ ? BUFSIZ : k;
                    544:                        if (fread(block, 1, j, fp[2]) != j)
                    545:                                trouble();
                    546:                        (void)fwrite(block, 1, j, stdout);
                    547:                }
1.2       deraadt   548:                if (!oflag || !overlap[n])
                    549:                        printf(".\n");
                    550:                else {
                    551:                        printf("%s\n.\n", f3mark);
                    552:                        printf("%da\n%s\n.\n", de[n].old.from - 1, f1mark);
                    553:                }
1.1       millert   554:        }
1.2       deraadt   555:        exit(overlapcnt);
1.1       millert   556: }
                    557:
                    558: __dead void
                    559: usage(void)
                    560: {
                    561:        extern char *__progname;
                    562:
                    563:        fprintf(stderr, "usage: %s [-exEX3] /tmp/d3a.?????????? "
                    564:            "/tmp/d3b.?????????? file1 file2 file3\n", __progname);
                    565:        exit(EXIT_FAILURE);
                    566: }