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

Annotation of src/usr.bin/cvs/diff_internals.c, Revision 1.10

1.10    ! ray         1: /*     $OpenBSD: diff_internals.c,v 1.9 2007/05/29 00:19:10 ray Exp $  */
1.1       joris       2: /*
                      3:  * Copyright (C) Caldera International Inc.  2001-2002.
                      4:  * All rights reserved.
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code and documentation must retain the above
                     10:  *    copyright notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  * 3. All advertising materials mentioning features or use of this software
                     15:  *    must display the following acknowledgement:
                     16:  *     This product includes software developed or owned by Caldera
                     17:  *     International, Inc.
                     18:  * 4. Neither the name of Caldera International, Inc. nor the names of other
                     19:  *    contributors may be used to endorse or promote products derived from
                     20:  *    this software without specific prior written permission.
                     21:  *
                     22:  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
                     23:  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
                     24:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     25:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     26:  * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
                     27:  * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
                     28:  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
                     29:  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
                     31:  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
                     32:  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     33:  * POSSIBILITY OF SUCH DAMAGE.
                     34:  */
                     35: /*-
                     36:  * Copyright (c) 1991, 1993
                     37:  *     The Regents of the University of California.  All rights reserved.
                     38:  * Copyright (c) 2004 Jean-Francois Brousseau.  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:  *     @(#)diffreg.c   8.1 (Berkeley) 6/6/93
                     65:  */
                     66: /*
                     67:  *     Uses an algorithm due to Harold Stone, which finds
                     68:  *     a pair of longest identical subsequences in the two
                     69:  *     files.
                     70:  *
                     71:  *     The major goal is to generate the match vector J.
                     72:  *     J[i] is the index of the line in file1 corresponding
                     73:  *     to line i file0. J[i] = 0 if there is no
                     74:  *     such line in file1.
                     75:  *
                     76:  *     Lines are hashed so as to work in core. All potential
                     77:  *     matches are located by sorting the lines of each file
                     78:  *     on the hash (called ``value''). In particular, this
                     79:  *     collects the equivalence classes in file1 together.
                     80:  *     Subroutine equiv replaces the value of each line in
                     81:  *     file0 by the index of the first element of its
                     82:  *     matching equivalence in (the reordered) file1.
                     83:  *     To save space equiv squeezes file1 into a single
                     84:  *     array member in which the equivalence classes
                     85:  *     are simply concatenated, except that their first
                     86:  *     members are flagged by changing sign.
                     87:  *
                     88:  *     Next the indices that point into member are unsorted into
                     89:  *     array class according to the original order of file0.
                     90:  *
                     91:  *     The cleverness lies in routine stone. This marches
                     92:  *     through the lines of file0, developing a vector klist
                     93:  *     of "k-candidates". At step i a k-candidate is a matched
                     94:  *     pair of lines x,y (x in file0 y in file1) such that
                     95:  *     there is a common subsequence of length k
                     96:  *     between the first i lines of file0 and the first y
                     97:  *     lines of file1, but there is no such subsequence for
                     98:  *     any smaller y. x is the earliest possible mate to y
                     99:  *     that occurs in such a subsequence.
                    100:  *
                    101:  *     Whenever any of the members of the equivalence class of
                    102:  *     lines in file1 matable to a line in file0 has serial number
                    103:  *     less than the y of some k-candidate, that k-candidate
                    104:  *     with the smallest such y is replaced. The new
                    105:  *     k-candidate is chained (via pred) to the current
                    106:  *     k-1 candidate so that the actual subsequence can
                    107:  *     be recovered. When a member has serial number greater
                    108:  *     that the y of all k-candidates, the klist is extended.
                    109:  *     At the end, the longest subsequence is pulled out
                    110:  *     and placed in the array J by unravel
                    111:  *
                    112:  *     With J in hand, the matches there recorded are
                    113:  *     check'ed against reality to assure that no spurious
                    114:  *     matches have crept in due to hashing. If they have,
                    115:  *     they are broken, and "jackpot" is recorded--a harmless
                    116:  *     matter except that a true match for a spuriously
                    117:  *     mated line may now be unnecessarily reported as a change.
                    118:  *
                    119:  *     Much of the complexity of the program comes simply
                    120:  *     from trying to minimize core utilization and
                    121:  *     maximize the range of doable problems by dynamically
                    122:  *     allocating what is needed and reusing what is not.
                    123:  *     The core requirements for problems larger than somewhat
                    124:  *     are (in words) 2*length(file0) + length(file1) +
                    125:  *     3*(number of k-candidates installed),  typically about
                    126:  *     6n words for files of length n.
                    127:  */
                    128:
1.4       otto      129: #include <sys/stat.h>
                    130:
                    131: #include <ctype.h>
                    132: #include <errno.h>
                    133: #include <regex.h>
                    134: #include <stddef.h>
                    135: #include <string.h>
                    136: #include <unistd.h>
1.1       joris     137:
                    138: #include "cvs.h"
                    139: #include "diff.h"
                    140:
                    141: struct cand {
                    142:        int     x;
                    143:        int     y;
                    144:        int     pred;
                    145: } cand;
                    146:
                    147: struct line {
                    148:        int     serial;
                    149:        int     value;
                    150: } *file[2];
                    151:
                    152: /*
1.7       ray       153:  * The following struct is used to record change information when
1.1       joris     154:  * doing a "context" or "unified" diff.  (see routine "change" to
                    155:  * understand the highly mnemonic field names)
                    156:  */
                    157: struct context_vec {
1.6       ray       158:        int     a;              /* start line in old file */
                    159:        int     b;              /* end line in old file */
                    160:        int     c;              /* start line in new file */
                    161:        int     d;              /* end line in new file */
1.1       joris     162: };
                    163:
                    164: struct diff_arg {
                    165:        char    *rev1;
                    166:        char    *rev2;
                    167:        char    *date1;
                    168:        char    *date2;
                    169: };
                    170:
                    171: static void     output(FILE *, FILE *);
                    172: static void     check(FILE *, FILE *);
                    173: static void     range(int, int, char *);
                    174: static void     uni_range(int, int);
                    175: static void     dump_context_vec(FILE *, FILE *);
                    176: static void     dump_unified_vec(FILE *, FILE *);
                    177: static int      prepare(int, FILE *, off_t);
                    178: static void     prune(void);
                    179: static void     equiv(struct line *, int, struct line *, int, int *);
                    180: static void     unravel(int);
                    181: static void     unsort(struct line *, int, int *);
                    182: static void     change(FILE *, FILE *, int, int, int, int);
                    183: static void     sort(struct line *, int);
                    184: static int      ignoreline(char *);
                    185: static int      asciifile(FILE *);
                    186: static void     fetch(long *, int, int, FILE *, int, int);
                    187: static int      newcand(int, int, int);
                    188: static int      search(int *, int, int);
                    189: static int      skipline(FILE *);
                    190: static int      isqrt(int);
                    191: static int      stone(int *, int, int *, int *);
                    192: static int      readhash(FILE *);
                    193: static int      files_differ(FILE *, FILE *);
                    194: static char    *match_function(const long *, int, FILE *);
                    195: static char    *preadline(int, size_t, off_t);
                    196:
1.2       joris     197: static int aflag, bflag, dflag, iflag, tflag, Tflag, wflag;
1.1       joris     198: static int context = 3;
                    199: int diff_format = D_NORMAL;
1.2       joris     200: int diff_pflag = 0;
1.1       joris     201: char *diff_file = NULL;
                    202: RCSNUM *diff_rev1 = NULL;
                    203: RCSNUM *diff_rev2 = NULL;
                    204: char diffargs[128];
                    205: static struct stat stb1, stb2;
                    206: static char *ifdefname, *ignore_pats;
                    207: regex_t ignore_re;
                    208:
                    209: static int  *J;                        /* will be overlaid on class */
                    210: static int  *class;            /* will be overlaid on file[0] */
                    211: static int  *klist;            /* will be overlaid on file[0] after class */
                    212: static int  *member;           /* will be overlaid on file[1] */
                    213: static int   clen;
                    214: static int   inifdef;          /* whether or not we are in a #ifdef block */
                    215: static int   diff_len[2];
                    216: static int   pref, suff;       /* length of prefix and suffix */
                    217: static int   slen[2];
                    218: static int   anychange;
                    219: static long *ixnew;            /* will be overlaid on file[1] */
                    220: static long *ixold;            /* will be overlaid on klist */
                    221: static struct cand *clist;     /* merely a free storage pot for candidates */
                    222: static int   clistlen;         /* the length of clist */
                    223: static struct line *sfile[2];  /* shortened by pruning common prefix/suffix */
                    224: static u_char *chrtran;                /* translation table for case-folding */
                    225: static struct context_vec *context_vec_start;
                    226: static struct context_vec *context_vec_end;
                    227: static struct context_vec *context_vec_ptr;
                    228:
1.5       xsa       229: #define FUNCTION_CONTEXT_SIZE  55
1.1       joris     230: static char lastbuf[FUNCTION_CONTEXT_SIZE];
1.8       ray       231: static int lastline;
                    232: static int lastmatchline;
1.1       joris     233: BUF  *diffbuf = NULL;
                    234:
                    235: /*
                    236:  * chrtran points to one of 2 translation tables: cup2low if folding upper to
                    237:  * lower case clow2low if not folding case
                    238:  */
                    239: u_char clow2low[256] = {
                    240:        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
                    241:        0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                    242:        0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
                    243:        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
                    244:        0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
                    245:        0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
                    246:        0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
                    247:        0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
                    248:        0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62,
                    249:        0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
                    250:        0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
                    251:        0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
                    252:        0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
                    253:        0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
                    254:        0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
                    255:        0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
                    256:        0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
                    257:        0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
                    258:        0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
                    259:        0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
                    260:        0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
                    261:        0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
                    262:        0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
                    263:        0xfd, 0xfe, 0xff
                    264: };
                    265:
                    266: u_char cup2low[256] = {
                    267:        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
                    268:        0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                    269:        0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
                    270:        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
                    271:        0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
                    272:        0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x60, 0x61,
                    273:        0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
                    274:        0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
                    275:        0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x60, 0x61, 0x62,
                    276:        0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
                    277:        0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
                    278:        0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
                    279:        0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
                    280:        0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
                    281:        0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
                    282:        0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
                    283:        0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
                    284:        0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
                    285:        0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
                    286:        0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
                    287:        0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
                    288:        0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
                    289:        0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
                    290:        0xfd, 0xfe, 0xff
                    291: };
                    292:
                    293: int
                    294: cvs_diffreg(const char *file1, const char *file2, BUF *out)
                    295: {
                    296:        FILE *f1, *f2;
                    297:        int i, rval;
                    298:
                    299:        f1 = f2 = NULL;
                    300:        rval = D_SAME;
                    301:        anychange = 0;
                    302:        lastline = 0;
                    303:        lastmatchline = 0;
                    304:        context_vec_ptr = context_vec_start - 1;
                    305:        chrtran = (iflag ? cup2low : clow2low);
                    306:        if (out != NULL)
                    307:                diffbuf = out;
                    308:
                    309:        f1 = fopen(file1, "r");
                    310:        if (f1 == NULL) {
                    311:                cvs_log(LP_ERR, "%s", file1);
                    312:                goto closem;
                    313:        }
                    314:
                    315:        f2 = fopen(file2, "r");
                    316:        if (f2 == NULL) {
                    317:                cvs_log(LP_ERR, "%s", file2);
                    318:                goto closem;
                    319:        }
                    320:
                    321:        if (stat(file1, &stb1) < 0) {
                    322:                cvs_log(LP_ERR, "%s", file1);
                    323:                goto closem;
                    324:        }
                    325:        if (stat(file2, &stb2) < 0) {
                    326:                cvs_log(LP_ERR, "%s", file2);
                    327:                goto closem;
                    328:        }
                    329:        switch (files_differ(f1, f2)) {
                    330:        case 0:
                    331:                goto closem;
                    332:        case 1:
                    333:                break;
                    334:        default:
                    335:                /* error */
                    336:                goto closem;
                    337:        }
                    338:
                    339:        if (!asciifile(f1) || !asciifile(f2)) {
                    340:                rval = D_BINARY;
                    341:                goto closem;
                    342:        }
                    343:        if (prepare(0, f1, stb1.st_size) < 0 ||
                    344:            prepare(1, f2, stb2.st_size) < 0) {
                    345:                goto closem;
                    346:        }
                    347:        prune();
                    348:        sort(sfile[0], slen[0]);
                    349:        sort(sfile[1], slen[1]);
                    350:
                    351:        member = (int *)file[1];
                    352:        equiv(sfile[0], slen[0], sfile[1], slen[1], member);
1.9       ray       353:        member = xrealloc(member, slen[1] + 2, sizeof(*member));
1.1       joris     354:
                    355:        class = (int *)file[0];
                    356:        unsort(sfile[0], slen[0], class);
1.9       ray       357:        class = xrealloc(class, slen[0] + 2, sizeof(*class));
1.1       joris     358:
                    359:        klist = xcalloc(slen[0] + 2, sizeof(*klist));
                    360:        clen = 0;
                    361:        clistlen = 100;
                    362:        clist = xcalloc(clistlen, sizeof(*clist));
                    363:
                    364:        if ((i = stone(class, slen[0], member, klist)) < 0)
                    365:                goto closem;
                    366:
                    367:        xfree(member);
                    368:        xfree(class);
                    369:
1.9       ray       370:        J = xrealloc(J, diff_len[0] + 2, sizeof(*J));
1.1       joris     371:        unravel(klist[i]);
                    372:        xfree(clist);
                    373:        xfree(klist);
                    374:
1.9       ray       375:        ixold = xrealloc(ixold, diff_len[0] + 2, sizeof(*ixold));
1.1       joris     376:
1.9       ray       377:        ixnew = xrealloc(ixnew, diff_len[1] + 2, sizeof(*ixnew));
1.1       joris     378:        check(f1, f2);
                    379:        output(f1, f2);
                    380:
                    381: closem:
                    382:        if (anychange == 1) {
                    383:                if (rval == D_SAME)
                    384:                        rval = D_DIFFER;
                    385:        }
                    386:        if (f1 != NULL)
                    387:                fclose(f1);
                    388:        if (f2 != NULL)
                    389:                fclose(f2);
                    390:
                    391:        return (rval);
                    392: }
                    393:
                    394: /*
                    395:  * Check to see if the given files differ.
                    396:  * Returns 0 if they are the same, 1 if different, and -1 on error.
                    397:  * XXX - could use code from cmp(1) [faster]
                    398:  */
                    399: static int
                    400: files_differ(FILE *f1, FILE *f2)
                    401: {
                    402:        char buf1[BUFSIZ], buf2[BUFSIZ];
                    403:        size_t i, j;
                    404:
                    405:        if (stb1.st_size != stb2.st_size)
                    406:                return (1);
                    407:        for (;;) {
                    408:                i = fread(buf1, (size_t)1, sizeof(buf1), f1);
                    409:                j = fread(buf2, (size_t)1, sizeof(buf2), f2);
                    410:                if (i != j)
                    411:                        return (1);
                    412:                if (i == 0 && j == 0) {
                    413:                        if (ferror(f1) || ferror(f2))
                    414:                                return (1);
                    415:                        return (0);
                    416:                }
                    417:                if (memcmp(buf1, buf2, i) != 0)
                    418:                        return (1);
                    419:        }
                    420: }
                    421:
                    422: static int
                    423: prepare(int i, FILE *fd, off_t filesize)
                    424: {
                    425:        struct line *p;
                    426:        int j, h;
                    427:        size_t sz;
                    428:
                    429:        rewind(fd);
                    430:
                    431:        sz = ((size_t)filesize <= SIZE_MAX ? (size_t)filesize : SIZE_MAX) / 25;
                    432:        if (sz < 100)
                    433:                sz = 100;
                    434:
                    435:        p = xcalloc(sz + 3, sizeof(*p));
                    436:        for (j = 0; (h = readhash(fd));) {
                    437:                if (j == (int)sz) {
                    438:                        sz = sz * 3 / 2;
1.9       ray       439:                        p = xrealloc(p, sz + 3, sizeof(*p));
1.1       joris     440:                }
                    441:                p[++j].value = h;
                    442:        }
                    443:        diff_len[i] = j;
                    444:        file[i] = p;
                    445:
                    446:        return (0);
                    447: }
                    448:
                    449: static void
                    450: prune(void)
                    451: {
                    452:        int i, j;
                    453:
                    454:        for (pref = 0; pref < diff_len[0] && pref < diff_len[1] &&
                    455:            file[0][pref + 1].value == file[1][pref + 1].value;
                    456:            pref++)
                    457:                ;
                    458:        for (suff = 0;
                    459:            (suff < diff_len[0] - pref) && (suff < diff_len[1] - pref) &&
                    460:            (file[0][diff_len[0] - suff].value ==
                    461:            file[1][diff_len[1] - suff].value);
                    462:            suff++)
                    463:                ;
                    464:        for (j = 0; j < 2; j++) {
                    465:                sfile[j] = file[j] + pref;
                    466:                slen[j] = diff_len[j] - pref - suff;
                    467:                for (i = 0; i <= slen[j]; i++)
                    468:                        sfile[j][i].serial = i;
                    469:        }
                    470: }
                    471:
                    472: static void
                    473: equiv(struct line *a, int n, struct line *b, int m, int *c)
                    474: {
                    475:        int i, j;
                    476:
                    477:        i = j = 1;
                    478:        while (i <= n && j <= m) {
                    479:                if (a[i].value < b[j].value)
                    480:                        a[i++].value = 0;
                    481:                else if (a[i].value == b[j].value)
                    482:                        a[i++].value = j;
                    483:                else
                    484:                        j++;
                    485:        }
                    486:        while (i <= n)
                    487:                a[i++].value = 0;
                    488:        b[m + 1].value = 0;
                    489:        j = 0;
                    490:        while (++j <= m) {
                    491:                c[j] = -b[j].serial;
                    492:                while (b[j + 1].value == b[j].value) {
                    493:                        j++;
                    494:                        c[j] = b[j].serial;
                    495:                }
                    496:        }
                    497:        c[j] = -1;
                    498: }
                    499:
                    500: /* Code taken from ping.c */
                    501: static int
                    502: isqrt(int n)
                    503: {
                    504:        int y, x = 1;
                    505:
                    506:        if (n == 0)
                    507:                return (0);
                    508:
                    509:        do { /* newton was a stinker */
                    510:                y = x;
                    511:                x = n / x;
                    512:                x += y;
                    513:                x /= 2;
                    514:        } while (x - y > 1 || x - y < -1);
                    515:
                    516:        return (x);
                    517: }
                    518:
                    519: static int
                    520: stone(int *a, int n, int *b, int *c)
                    521: {
                    522:        int ret;
                    523:        int i, k, y, j, l;
                    524:        int oldc, tc, oldl;
                    525:        u_int numtries;
                    526:
                    527:        /* XXX move the isqrt() out of the macro to avoid multiple calls */
                    528:        const u_int bound = dflag ? UINT_MAX : MAX(256, (u_int)isqrt(n));
                    529:
                    530:        k = 0;
                    531:        if ((ret = newcand(0, 0, 0)) < 0)
                    532:                return (-1);
                    533:        c[0] = ret;
                    534:        for (i = 1; i <= n; i++) {
                    535:                j = a[i];
                    536:                if (j == 0)
                    537:                        continue;
                    538:                y = -b[j];
                    539:                oldl = 0;
                    540:                oldc = c[0];
                    541:                numtries = 0;
                    542:                do {
                    543:                        if (y <= clist[oldc].y)
                    544:                                continue;
                    545:                        l = search(c, k, y);
                    546:                        if (l != oldl + 1)
                    547:                                oldc = c[l - 1];
                    548:                        if (l <= k) {
                    549:                                if (clist[c[l]].y <= y)
                    550:                                        continue;
                    551:                                tc = c[l];
                    552:                                if ((ret = newcand(i, y, oldc)) < 0)
                    553:                                        return (-1);
                    554:                                c[l] = ret;
                    555:                                oldc = tc;
                    556:                                oldl = l;
                    557:                                numtries++;
                    558:                        } else {
                    559:                                if ((ret = newcand(i, y, oldc)) < 0)
                    560:                                        return (-1);
                    561:                                c[l] = ret;
                    562:                                k++;
                    563:                                break;
                    564:                        }
                    565:                } while ((y = b[++j]) > 0 && numtries < bound);
                    566:        }
                    567:        return (k);
                    568: }
                    569:
                    570: static int
                    571: newcand(int x, int y, int pred)
                    572: {
1.9       ray       573:        struct cand *q;
1.1       joris     574:
                    575:        if (clen == clistlen) {
1.10    ! ray       576:                clistlen = clistlen * 11 / 10;
        !           577:                clist = xrealloc(clist, clistlen, sizeof(*clist));
1.1       joris     578:        }
                    579:        q = clist + clen;
                    580:        q->x = x;
                    581:        q->y = y;
                    582:        q->pred = pred;
                    583:        return (clen++);
                    584: }
                    585:
                    586: static int
                    587: search(int *c, int k, int y)
                    588: {
                    589:        int i, j, l, t;
                    590:
                    591:        if (clist[c[k]].y < y)  /* quick look for typical case */
                    592:                return (k + 1);
                    593:        i = 0;
                    594:        j = k + 1;
                    595:        for (;;) {
                    596:                l = (i + j) / 2;
                    597:                if (l <= i)
                    598:                        break;
                    599:                t = clist[c[l]].y;
                    600:                if (t > y)
                    601:                        j = l;
                    602:                else if (t < y)
                    603:                        i = l;
                    604:                else
                    605:                        return (l);
                    606:        }
                    607:        return (l + 1);
                    608: }
                    609:
                    610: static void
                    611: unravel(int p)
                    612: {
                    613:        struct cand *q;
                    614:        int i;
                    615:
                    616:        for (i = 0; i <= diff_len[0]; i++)
                    617:                J[i] = i <= pref ? i :
                    618:                    i > diff_len[0] - suff ? i + diff_len[1] - diff_len[0] : 0;
                    619:        for (q = clist + p; q->y != 0; q = clist + q->pred)
                    620:                J[q->x + pref] = q->y + pref;
                    621: }
                    622:
                    623: /*
                    624:  * Check does double duty:
                    625:  *  1. ferret out any fortuitous correspondences due
                    626:  *     to confounding by hashing (which result in "jackpot")
                    627:  *  2.  collect random access indexes to the two files
                    628:  */
                    629: static void
                    630: check(FILE *f1, FILE *f2)
                    631: {
                    632:        int i, j, jackpot, c, d;
                    633:        long ctold, ctnew;
                    634:
                    635:        rewind(f1);
                    636:        rewind(f2);
                    637:        j = 1;
                    638:        ixold[0] = ixnew[0] = 0;
                    639:        jackpot = 0;
                    640:        ctold = ctnew = 0;
                    641:        for (i = 1; i <= diff_len[0]; i++) {
                    642:                if (J[i] == 0) {
                    643:                        ixold[i] = ctold += skipline(f1);
                    644:                        continue;
                    645:                }
                    646:                while (j < J[i]) {
                    647:                        ixnew[j] = ctnew += skipline(f2);
                    648:                        j++;
                    649:                }
                    650:                if (bflag == 1 || wflag == 1 || iflag == 1) {
                    651:                        for (;;) {
                    652:                                c = getc(f1);
                    653:                                d = getc(f2);
                    654:                                /*
                    655:                                 * GNU diff ignores a missing newline
                    656:                                 * in one file if bflag || wflag.
                    657:                                 */
                    658:                                if ((bflag == 1 || wflag == 1) &&
                    659:                                    ((c == EOF && d == '\n') ||
                    660:                                    (c == '\n' && d == EOF))) {
                    661:                                        break;
                    662:                                }
                    663:                                ctold++;
                    664:                                ctnew++;
                    665:                                if (bflag == 1 && isspace(c) && isspace(d)) {
                    666:                                        do {
                    667:                                                if (c == '\n')
                    668:                                                        break;
                    669:                                                ctold++;
                    670:                                        } while (isspace(c = getc(f1)));
                    671:                                        do {
                    672:                                                if (d == '\n')
                    673:                                                        break;
                    674:                                                ctnew++;
                    675:                                        } while (isspace(d = getc(f2)));
                    676:                                } else if (wflag == 1) {
                    677:                                        while (isspace(c) && c != '\n') {
                    678:                                                c = getc(f1);
                    679:                                                ctold++;
                    680:                                        }
                    681:                                        while (isspace(d) && d != '\n') {
                    682:                                                d = getc(f2);
                    683:                                                ctnew++;
                    684:                                        }
                    685:                                }
                    686:                                if (chrtran[c] != chrtran[d]) {
                    687:                                        jackpot++;
                    688:                                        J[i] = 0;
                    689:                                        if (c != '\n' && c != EOF)
                    690:                                                ctold += skipline(f1);
                    691:                                        if (d != '\n' && c != EOF)
                    692:                                                ctnew += skipline(f2);
                    693:                                        break;
                    694:                                }
                    695:                                if (c == '\n' || c == EOF)
                    696:                                        break;
                    697:                        }
                    698:                } else {
                    699:                        for (;;) {
                    700:                                ctold++;
                    701:                                ctnew++;
                    702:                                if ((c = getc(f1)) != (d = getc(f2))) {
                    703:                                        /* jackpot++; */
                    704:                                        J[i] = 0;
                    705:                                        if (c != '\n' && c != EOF)
                    706:                                                ctold += skipline(f1);
                    707:                                        if (d != '\n' && c != EOF)
                    708:                                                ctnew += skipline(f2);
                    709:                                        break;
                    710:                                }
                    711:                                if (c == '\n' || c == EOF)
                    712:                                        break;
                    713:                        }
                    714:                }
                    715:                ixold[i] = ctold;
                    716:                ixnew[j] = ctnew;
                    717:                j++;
                    718:        }
                    719:        for (; j <= diff_len[1]; j++)
                    720:                ixnew[j] = ctnew += skipline(f2);
                    721:        /*
                    722:         * if (jackpot != 0)
                    723:         *      cvs_printf("jackpot\n");
                    724:         */
                    725: }
                    726:
                    727: /* shellsort CACM #201 */
                    728: static void
                    729: sort(struct line *a, int n)
                    730: {
                    731:        struct line *ai, *aim, w;
                    732:        int j, m = 0, k;
                    733:
                    734:        if (n == 0)
                    735:                return;
                    736:        for (j = 1; j <= n; j *= 2)
                    737:                m = 2 * j - 1;
                    738:        for (m /= 2; m != 0; m /= 2) {
                    739:                k = n - m;
                    740:                for (j = 1; j <= k; j++) {
                    741:                        for (ai = &a[j]; ai > a; ai -= m) {
                    742:                                aim = &ai[m];
                    743:                                if (aim < ai)
                    744:                                        break;  /* wraparound */
                    745:                                if (aim->value > ai[0].value ||
                    746:                                    (aim->value == ai[0].value &&
                    747:                                        aim->serial > ai[0].serial))
                    748:                                        break;
                    749:                                w.value = ai[0].value;
                    750:                                ai[0].value = aim->value;
                    751:                                aim->value = w.value;
                    752:                                w.serial = ai[0].serial;
                    753:                                ai[0].serial = aim->serial;
                    754:                                aim->serial = w.serial;
                    755:                        }
                    756:                }
                    757:        }
                    758: }
                    759:
                    760: static void
                    761: unsort(struct line *f, int l, int *b)
                    762: {
                    763:        int *a, i;
                    764:
                    765:        a = xcalloc(l + 1, sizeof(*a));
                    766:        for (i = 1; i <= l; i++)
                    767:                a[f[i].serial] = f[i].value;
                    768:        for (i = 1; i <= l; i++)
                    769:                b[i] = a[i];
                    770:        xfree(a);
                    771: }
                    772:
                    773: static int
                    774: skipline(FILE *f)
                    775: {
                    776:        int i, c;
                    777:
                    778:        for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
                    779:                continue;
                    780:        return (i);
                    781: }
                    782:
                    783: static void
                    784: output(FILE *f1, FILE *f2)
                    785: {
                    786:        int m, i0, i1, j0, j1;
                    787:
                    788:        rewind(f1);
                    789:        rewind(f2);
                    790:        m = diff_len[0];
                    791:        J[0] = 0;
                    792:        J[m + 1] = diff_len[1] + 1;
                    793:        for (i0 = 1; i0 <= m; i0 = i1 + 1) {
                    794:                while (i0 <= m && J[i0] == J[i0 - 1] + 1)
                    795:                        i0++;
                    796:                j0 = J[i0 - 1] + 1;
                    797:                i1 = i0 - 1;
                    798:                while (i1 < m && J[i1 + 1] == 0)
                    799:                        i1++;
                    800:                j1 = J[i1 + 1] - 1;
                    801:                J[i1] = j1;
                    802:                change(f1, f2, i0, i1, j0, j1);
                    803:        }
                    804:        if (m == 0)
                    805:                change(f1, f2, 1, 0, 1, diff_len[1]);
                    806:        if (diff_format == D_IFDEF) {
                    807:                for (;;) {
                    808: #define        c i0
                    809:                        if ((c = getc(f1)) == EOF)
                    810:                                return;
                    811:                        diff_output("%c", c);
                    812:                }
                    813: #undef c
                    814:        }
                    815:        if (anychange != 0) {
                    816:                if (diff_format == D_CONTEXT)
                    817:                        dump_context_vec(f1, f2);
                    818:                else if (diff_format == D_UNIFIED)
                    819:                        dump_unified_vec(f1, f2);
                    820:        }
                    821: }
                    822:
                    823: static __inline void
                    824: range(int a, int b, char *separator)
                    825: {
                    826:        diff_output("%d", a > b ? b : a);
                    827:        if (a < b)
                    828:                diff_output("%s%d", separator, b);
                    829: }
                    830:
                    831: static __inline void
                    832: uni_range(int a, int b)
                    833: {
                    834:        if (a < b)
                    835:                diff_output("%d,%d", a, b - a + 1);
                    836:        else if (a == b)
                    837:                diff_output("%d", b);
                    838:        else
                    839:                diff_output("%d,0", b);
                    840: }
                    841:
                    842: static char *
                    843: preadline(int fd, size_t rlen, off_t off)
                    844: {
                    845:        char *line;
                    846:        ssize_t nr;
                    847:
                    848:        line = xmalloc(rlen + 1);
                    849:        if ((nr = pread(fd, line, rlen, off)) < 0) {
                    850:                cvs_log(LP_ERR, "preadline failed");
                    851:                return (NULL);
                    852:        }
                    853:        line[nr] = '\0';
                    854:        return (line);
                    855: }
                    856:
                    857: static int
                    858: ignoreline(char *line)
                    859: {
                    860:        int ret;
                    861:
                    862:        ret = regexec(&ignore_re, line, (size_t)0, NULL, 0);
                    863:        xfree(line);
                    864:        return (ret == 0);      /* if it matched, it should be ignored. */
                    865: }
                    866:
                    867: /*
                    868:  * Indicate that there is a difference between lines a and b of the from file
                    869:  * to get to lines c to d of the to file.  If a is greater then b then there
                    870:  * are no lines in the from file involved and this means that there were
                    871:  * lines appended (beginning at b).  If c is greater than d then there are
                    872:  * lines missing from the to file.
                    873:  */
                    874: static void
                    875: change(FILE *f1, FILE *f2, int a, int b, int c, int d)
                    876: {
                    877:        int i;
                    878:        static size_t max_context = 64;
                    879:        char buf[64];
                    880:        struct tm *t;
                    881:
                    882:        if (diff_format != D_IFDEF && a > b && c > d)
                    883:                return;
                    884:        if (ignore_pats != NULL) {
                    885:                char *line;
                    886:                /*
                    887:                 * All lines in the change, insert, or delete must
                    888:                 * match an ignore pattern for the change to be
                    889:                 * ignored.
                    890:                 */
                    891:                if (a <= b) {           /* Changes and deletes. */
                    892:                        for (i = a; i <= b; i++) {
                    893:                                line = preadline(fileno(f1),
                    894:                                    ixold[i] - ixold[i - 1], ixold[i - 1]);
                    895:                                if (!ignoreline(line))
                    896:                                        goto proceed;
                    897:                        }
                    898:                }
                    899:                if (a > b || c <= d) {  /* Changes and inserts. */
                    900:                        for (i = c; i <= d; i++) {
                    901:                                line = preadline(fileno(f2),
                    902:                                    ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
                    903:                                if (!ignoreline(line))
                    904:                                        goto proceed;
                    905:                        }
                    906:                }
                    907:                return;
                    908:        }
                    909: proceed:
                    910:        if (diff_format == D_CONTEXT || diff_format == D_UNIFIED) {
                    911:                /*
                    912:                 * Allocate change records as needed.
                    913:                 */
                    914:                if (context_vec_ptr == context_vec_end - 1) {
                    915:                        ptrdiff_t offset = context_vec_ptr - context_vec_start;
                    916:                        max_context <<= 1;
1.9       ray       917:                        context_vec_start = xrealloc(context_vec_start,
                    918:                            max_context, sizeof(*context_vec_start));
1.1       joris     919:                        context_vec_end = context_vec_start + max_context;
                    920:                        context_vec_ptr = context_vec_start + offset;
                    921:                }
                    922:                if (anychange == 0) {
                    923:                        /*
                    924:                         * Print the context/unidiff header first time through.
                    925:                         */
                    926:                        t = localtime(&stb1.st_mtime);
                    927:                        (void)strftime(buf, sizeof(buf),
                    928:                            "%d %b %G %H:%M:%S", t);
                    929:
                    930:                        diff_output("%s %s      %s",
                    931:                            diff_format == D_CONTEXT ? "***" : "---", diff_file,
                    932:                            buf);
                    933:
                    934:                        if (diff_rev1 != NULL) {
                    935:                                rcsnum_tostr(diff_rev1, buf, sizeof(buf));
                    936:                                diff_output("\t%s", buf);
                    937:                        }
                    938:
1.3       joris     939:                        diff_output("\n");
1.1       joris     940:
                    941:                        t = localtime(&stb2.st_mtime);
                    942:                        (void)strftime(buf, sizeof(buf),
                    943:                            "%d %b %G %H:%M:%S", t);
                    944:
                    945:                        diff_output("%s %s      %s",
                    946:                            diff_format == D_CONTEXT ? "---" : "+++", diff_file,
                    947:                            buf);
                    948:
                    949:                        if (diff_rev2 != NULL) {
                    950:                                rcsnum_tostr(diff_rev2, buf, sizeof(buf));
                    951:                                diff_output("\t%s", buf);
                    952:                        }
                    953:
1.3       joris     954:                        diff_output("\n");
1.1       joris     955:                        anychange = 1;
                    956:                } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
                    957:                    c > context_vec_ptr->d + (2 * context) + 1) {
                    958:                        /*
                    959:                         * If this change is more than 'context' lines from the
                    960:                         * previous change, dump the record and reset it.
                    961:                         */
                    962:                        if (diff_format == D_CONTEXT)
                    963:                                dump_context_vec(f1, f2);
                    964:                        else
                    965:                                dump_unified_vec(f1, f2);
                    966:                }
                    967:                context_vec_ptr++;
                    968:                context_vec_ptr->a = a;
                    969:                context_vec_ptr->b = b;
                    970:                context_vec_ptr->c = c;
                    971:                context_vec_ptr->d = d;
                    972:                return;
                    973:        }
                    974:        if (anychange == 0)
                    975:                anychange = 1;
                    976:        switch (diff_format) {
                    977:        case D_BRIEF:
                    978:                return;
                    979:        case D_NORMAL:
                    980:                range(a, b, ",");
                    981:                diff_output("%c", a > b ? 'a' : c > d ? 'd' : 'c');
                    982:                if (diff_format == D_NORMAL)
                    983:                        range(c, d, ",");
                    984:                diff_output("\n");
                    985:                break;
                    986:        case D_RCSDIFF:
                    987:                if (a > b)
                    988:                        diff_output("a%d %d\n", b, d - c + 1);
                    989:                else {
                    990:                        diff_output("d%d %d\n", a, b - a + 1);
                    991:
                    992:                        if (!(c > d))   /* add changed lines */
                    993:                                diff_output("a%d %d\n", b, d - c + 1);
                    994:                }
                    995:                break;
                    996:        }
                    997:        if (diff_format == D_NORMAL || diff_format == D_IFDEF) {
                    998:                fetch(ixold, a, b, f1, '<', 1);
                    999:                if (a <= b && c <= d && diff_format == D_NORMAL)
                   1000:                        diff_output("---\n");
                   1001:        }
                   1002:        fetch(ixnew, c, d, f2, diff_format == D_NORMAL ? '>' : '\0', 0);
                   1003:        if (inifdef) {
                   1004:                diff_output("#endif /* %s */\n", ifdefname);
                   1005:                inifdef = 0;
                   1006:        }
                   1007: }
                   1008:
                   1009: static void
                   1010: fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile)
                   1011: {
                   1012:        long j, nc;
                   1013:        int i, c, col;
                   1014:
                   1015:        /*
                   1016:         * When doing #ifdef's, copy down to current line
                   1017:         * if this is the first file, so that stuff makes it to output.
                   1018:         */
                   1019:        if (diff_format == D_IFDEF && oldfile) {
                   1020:                long curpos = ftell(lb);
                   1021:                /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
                   1022:                nc = f[a > b ? b : a - 1] - curpos;
                   1023:                for (i = 0; i < nc; i++)
                   1024:                        diff_output("%c", getc(lb));
                   1025:        }
                   1026:        if (a > b)
                   1027:                return;
                   1028:        if (diff_format == D_IFDEF) {
                   1029:                if (inifdef) {
                   1030:                        diff_output("#else /* %s%s */\n",
                   1031:                            oldfile == 1 ? "!" : "", ifdefname);
                   1032:                } else {
                   1033:                        if (oldfile)
                   1034:                                diff_output("#ifndef %s\n", ifdefname);
                   1035:                        else
                   1036:                                diff_output("#ifdef %s\n", ifdefname);
                   1037:                }
                   1038:                inifdef = 1 + oldfile;
                   1039:        }
                   1040:        for (i = a; i <= b; i++) {
                   1041:                fseek(lb, f[i - 1], SEEK_SET);
                   1042:                nc = f[i] - f[i - 1];
                   1043:                if (diff_format != D_IFDEF && ch != '\0') {
                   1044:                        diff_output("%c", ch);
                   1045:                        if (Tflag == 1 && (diff_format == D_NORMAL ||
                   1046:                            diff_format == D_CONTEXT ||
                   1047:                            diff_format == D_UNIFIED))
                   1048:                                diff_output("\t");
                   1049:                        else if (diff_format != D_UNIFIED)
                   1050:                                diff_output(" ");
                   1051:                }
                   1052:                col = 0;
                   1053:                for (j = 0; j < nc; j++) {
                   1054:                        if ((c = getc(lb)) == EOF) {
                   1055:                                if (diff_format == D_RCSDIFF)
                   1056:                                        cvs_log(LP_ERR,
                   1057:                                            "No newline at end of file");
                   1058:                                else
                   1059:                                        diff_output("\n\\ No newline at end of "
                   1060:                                            "file");
                   1061:                                return;
                   1062:                        }
                   1063:                        if (c == '\t' && tflag == 1) {
                   1064:                                do {
                   1065:                                        diff_output(" ");
                   1066:                                } while (++col & 7);
                   1067:                        } else {
                   1068:                                diff_output("%c", c);
                   1069:                                col++;
                   1070:                        }
                   1071:                }
                   1072:        }
                   1073: }
                   1074:
                   1075: /*
                   1076:  * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
                   1077:  */
                   1078: static int
                   1079: readhash(FILE *f)
                   1080: {
                   1081:        int i, t, space;
                   1082:        int sum;
                   1083:
                   1084:        sum = 1;
                   1085:        space = 0;
                   1086:        if (bflag != 1 && wflag != 1) {
                   1087:                if (iflag == 1)
                   1088:                        for (i = 0; (t = getc(f)) != '\n'; i++) {
                   1089:                                if (t == EOF) {
                   1090:                                        if (i == 0)
                   1091:                                                return (0);
                   1092:                                        break;
                   1093:                                }
                   1094:                                sum = sum * 127 + chrtran[t];
                   1095:                        }
                   1096:                else
                   1097:                        for (i = 0; (t = getc(f)) != '\n'; i++) {
                   1098:                                if (t == EOF) {
                   1099:                                        if (i == 0)
                   1100:                                                return (0);
                   1101:                                        break;
                   1102:                                }
                   1103:                                sum = sum * 127 + t;
                   1104:                        }
                   1105:        } else {
                   1106:                for (i = 0;;) {
                   1107:                        switch (t = getc(f)) {
                   1108:                        case '\t':
                   1109:                        case ' ':
                   1110:                                space++;
                   1111:                                continue;
                   1112:                        default:
                   1113:                                if (space != 0 && wflag != 1) {
                   1114:                                        i++;
                   1115:                                        space = 0;
                   1116:                                }
                   1117:                                sum = sum * 127 + chrtran[t];
                   1118:                                i++;
                   1119:                                continue;
                   1120:                        case EOF:
                   1121:                                if (i == 0)
                   1122:                                        return (0);
                   1123:                                /* FALLTHROUGH */
                   1124:                        case '\n':
                   1125:                                break;
                   1126:                        }
                   1127:                        break;
                   1128:                }
                   1129:        }
                   1130:        /*
                   1131:         * There is a remote possibility that we end up with a zero sum.
                   1132:         * Zero is used as an EOF marker, so return 1 instead.
                   1133:         */
                   1134:        return (sum == 0 ? 1 : sum);
                   1135: }
                   1136:
                   1137: static int
                   1138: asciifile(FILE *f)
                   1139: {
                   1140:        char buf[BUFSIZ];
                   1141:        size_t i, cnt;
                   1142:
                   1143:        if (aflag == 1 || f == NULL)
                   1144:                return (1);
                   1145:
                   1146:        rewind(f);
                   1147:        cnt = fread(buf, (size_t)1, sizeof(buf), f);
                   1148:        for (i = 0; i < cnt; i++)
                   1149:                if (!isprint(buf[i]) && !isspace(buf[i]))
                   1150:                        return (0);
                   1151:        return (1);
                   1152: }
                   1153:
1.5       xsa      1154: #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
                   1155:
1.1       joris    1156: static char*
                   1157: match_function(const long *f, int pos, FILE *fp)
                   1158: {
                   1159:        unsigned char buf[FUNCTION_CONTEXT_SIZE];
                   1160:        size_t nc;
                   1161:        int last = lastline;
                   1162:        char *p;
1.5       xsa      1163:        char *state = NULL;
1.1       joris    1164:
                   1165:        lastline = pos;
                   1166:        while (pos > last) {
                   1167:                fseek(fp, f[pos - 1], SEEK_SET);
                   1168:                nc = f[pos] - f[pos - 1];
                   1169:                if (nc >= sizeof(buf))
                   1170:                        nc = sizeof(buf) - 1;
                   1171:                nc = fread(buf, (size_t)1, nc, fp);
                   1172:                if (nc > 0) {
                   1173:                        buf[nc] = '\0';
                   1174:                        p = strchr((const char *)buf, '\n');
                   1175:                        if (p != NULL)
                   1176:                                *p = '\0';
                   1177:                        if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
1.5       xsa      1178:                                if (begins_with(buf, "private:")) {
                   1179:                                        if (!state)
                   1180:                                                state = " (private)";
                   1181:                                } else if (begins_with(buf, "protected:")) {
                   1182:                                        if (!state)
                   1183:                                                state = " (protected)";
                   1184:                                } else if (begins_with(buf, "public:")) {
                   1185:                                        if (!state)
                   1186:                                                state = " (public)";
                   1187:                                } else {
                   1188:                                        strlcpy(lastbuf, (const char *)buf,
                   1189:                                            sizeof lastbuf);
                   1190:                                        if (state)
                   1191:                                                strlcat(lastbuf, state,
                   1192:                                                    sizeof lastbuf);
                   1193:                                        lastmatchline = pos;
                   1194:                                        return lastbuf;
                   1195:                                }
1.1       joris    1196:                        }
                   1197:                }
                   1198:                pos--;
                   1199:        }
                   1200:        return (lastmatchline > 0) ? lastbuf : NULL;
                   1201: }
                   1202:
                   1203:
                   1204: /* dump accumulated "context" diff changes */
                   1205: static void
                   1206: dump_context_vec(FILE *f1, FILE *f2)
                   1207: {
                   1208:        struct context_vec *cvp = context_vec_start;
                   1209:        int lowa, upb, lowc, upd, do_output;
                   1210:        int a, b, c, d;
                   1211:        char ch, *f;
                   1212:
                   1213:        if (context_vec_start > context_vec_ptr)
                   1214:                return;
                   1215:
                   1216:        b = d = 0;              /* gcc */
                   1217:        lowa = MAX(1, cvp->a - context);
                   1218:        upb = MIN(diff_len[0], context_vec_ptr->b + context);
                   1219:        lowc = MAX(1, cvp->c - context);
                   1220:        upd = MIN(diff_len[1], context_vec_ptr->d + context);
                   1221:
                   1222:        diff_output("***************");
1.2       joris    1223:        if (diff_pflag == 1) {
1.1       joris    1224:                f = match_function(ixold, lowa - 1, f1);
                   1225:                if (f != NULL) {
                   1226:                        diff_output(" ");
                   1227:                        diff_output("%s", f);
                   1228:                }
                   1229:        }
                   1230:        diff_output("\n*** ");
                   1231:        range(lowa, upb, ",");
                   1232:        diff_output(" ****\n");
                   1233:
                   1234:        /*
                   1235:         * Output changes to the "old" file.  The first loop suppresses
                   1236:         * output if there were no changes to the "old" file (we'll see
                   1237:         * the "old" lines as context in the "new" list).
                   1238:         */
                   1239:        do_output = 0;
                   1240:        for (; cvp <= context_vec_ptr; cvp++)
                   1241:                if (cvp->a <= cvp->b) {
                   1242:                        cvp = context_vec_start;
                   1243:                        do_output++;
                   1244:                        break;
                   1245:                }
                   1246:        if (do_output != 0) {
                   1247:                while (cvp <= context_vec_ptr) {
                   1248:                        a = cvp->a;
                   1249:                        b = cvp->b;
                   1250:                        c = cvp->c;
                   1251:                        d = cvp->d;
                   1252:
                   1253:                        if (a <= b && c <= d)
                   1254:                                ch = 'c';
                   1255:                        else
                   1256:                                ch = (a <= b) ? 'd' : 'a';
                   1257:
                   1258:                        if (ch == 'a')
                   1259:                                fetch(ixold, lowa, b, f1, ' ', 0);
                   1260:                        else {
                   1261:                                fetch(ixold, lowa, a - 1, f1, ' ', 0);
                   1262:                                fetch(ixold, a, b, f1,
                   1263:                                    ch == 'c' ? '!' : '-', 0);
                   1264:                        }
                   1265:                        lowa = b + 1;
                   1266:                        cvp++;
                   1267:                }
                   1268:                fetch(ixold, b + 1, upb, f1, ' ', 0);
                   1269:        }
                   1270:        /* output changes to the "new" file */
                   1271:        diff_output("--- ");
                   1272:        range(lowc, upd, ",");
                   1273:        diff_output(" ----\n");
                   1274:
                   1275:        do_output = 0;
                   1276:        for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
                   1277:                if (cvp->c <= cvp->d) {
                   1278:                        cvp = context_vec_start;
                   1279:                        do_output++;
                   1280:                        break;
                   1281:                }
                   1282:        if (do_output != 0) {
                   1283:                while (cvp <= context_vec_ptr) {
                   1284:                        a = cvp->a;
                   1285:                        b = cvp->b;
                   1286:                        c = cvp->c;
                   1287:                        d = cvp->d;
                   1288:
                   1289:                        if (a <= b && c <= d)
                   1290:                                ch = 'c';
                   1291:                        else
                   1292:                                ch = (a <= b) ? 'd' : 'a';
                   1293:
                   1294:                        if (ch == 'd')
                   1295:                                fetch(ixnew, lowc, d, f2, ' ', 0);
                   1296:                        else {
                   1297:                                fetch(ixnew, lowc, c - 1, f2, ' ', 0);
                   1298:                                fetch(ixnew, c, d, f2,
                   1299:                                    ch == 'c' ? '!' : '+', 0);
                   1300:                        }
                   1301:                        lowc = d + 1;
                   1302:                        cvp++;
                   1303:                }
                   1304:                fetch(ixnew, d + 1, upd, f2, ' ', 0);
                   1305:        }
                   1306:        context_vec_ptr = context_vec_start - 1;
                   1307: }
                   1308:
                   1309: /* dump accumulated "unified" diff changes */
                   1310: static void
                   1311: dump_unified_vec(FILE *f1, FILE *f2)
                   1312: {
                   1313:        struct context_vec *cvp = context_vec_start;
                   1314:        int lowa, upb, lowc, upd;
                   1315:        int a, b, c, d;
                   1316:        char ch, *f;
                   1317:
                   1318:        if (context_vec_start > context_vec_ptr)
                   1319:                return;
                   1320:
                   1321:        b = d = 0;              /* gcc */
                   1322:        lowa = MAX(1, cvp->a - context);
                   1323:        upb = MIN(diff_len[0], context_vec_ptr->b + context);
                   1324:        lowc = MAX(1, cvp->c - context);
                   1325:        upd = MIN(diff_len[1], context_vec_ptr->d + context);
                   1326:
                   1327:        diff_output("@@ -");
                   1328:        uni_range(lowa, upb);
                   1329:        diff_output(" +");
                   1330:        uni_range(lowc, upd);
                   1331:        diff_output(" @@");
1.2       joris    1332:        if (diff_pflag == 1) {
1.1       joris    1333:                f = match_function(ixold, lowa - 1, f1);
                   1334:                if (f != NULL) {
                   1335:                        diff_output(" ");
                   1336:                        diff_output("%s", f);
                   1337:                }
                   1338:        }
                   1339:        diff_output("\n");
                   1340:
                   1341:        /*
                   1342:         * Output changes in "unified" diff format--the old and new lines
                   1343:         * are printed together.
                   1344:         */
                   1345:        for (; cvp <= context_vec_ptr; cvp++) {
                   1346:                a = cvp->a;
                   1347:                b = cvp->b;
                   1348:                c = cvp->c;
                   1349:                d = cvp->d;
                   1350:
                   1351:                /*
                   1352:                 * c: both new and old changes
                   1353:                 * d: only changes in the old file
                   1354:                 * a: only changes in the new file
                   1355:                 */
                   1356:                if (a <= b && c <= d)
                   1357:                        ch = 'c';
                   1358:                else
                   1359:                        ch = (a <= b) ? 'd' : 'a';
                   1360:
                   1361:                switch (ch) {
                   1362:                case 'c':
                   1363:                        fetch(ixold, lowa, a - 1, f1, ' ', 0);
                   1364:                        fetch(ixold, a, b, f1, '-', 0);
                   1365:                        fetch(ixnew, c, d, f2, '+', 0);
                   1366:                        break;
                   1367:                case 'd':
                   1368:                        fetch(ixold, lowa, a - 1, f1, ' ', 0);
                   1369:                        fetch(ixold, a, b, f1, '-', 0);
                   1370:                        break;
                   1371:                case 'a':
                   1372:                        fetch(ixnew, lowc, c - 1, f2, ' ', 0);
                   1373:                        fetch(ixnew, c, d, f2, '+', 0);
                   1374:                        break;
                   1375:                }
                   1376:                lowa = b + 1;
                   1377:                lowc = d + 1;
                   1378:        }
                   1379:        fetch(ixnew, d + 1, upd, f2, ' ', 0);
                   1380:
                   1381:        context_vec_ptr = context_vec_start - 1;
                   1382: }
                   1383:
                   1384: void
                   1385: diff_output(const char *fmt, ...)
                   1386: {
                   1387:        va_list vap;
                   1388:        int i;
                   1389:        char *str;
                   1390:
                   1391:        va_start(vap, fmt);
                   1392:        i = vasprintf(&str, fmt, vap);
                   1393:        va_end(vap);
                   1394:        if (i == -1)
                   1395:                fatal("diff_output: %s", strerror(errno));
                   1396:        if (diffbuf != NULL)
                   1397:                cvs_buf_append(diffbuf, str, strlen(str));
                   1398:        else
                   1399:                cvs_printf("%s", str);
                   1400:        xfree(str);
                   1401: }