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

Annotation of src/usr.bin/cvs/diff.c, Revision 1.76

1.76    ! xsa         1: /*     $OpenBSD: diff.c,v 1.75 2006/01/02 08:11:56 xsa Exp $   */
1.1       jfb         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.75      xsa       129: #include "includes.h"
1.1       jfb       130:
1.41      xsa       131: #include "buf.h"
1.1       jfb       132: #include "cvs.h"
1.56      niallo    133: #include "diff.h"
1.1       jfb       134: #include "log.h"
1.4       jfb       135: #include "proto.h"
1.1       jfb       136:
                    137: struct cand {
1.53      xsa       138:        int     x;
                    139:        int     y;
                    140:        int     pred;
1.1       jfb       141: } cand;
                    142:
                    143: struct line {
1.53      xsa       144:        int     serial;
                    145:        int     value;
1.1       jfb       146: } *file[2];
                    147:
                    148: /*
1.58      niallo    149:  * The following struct is used to record change in formation when
1.1       jfb       150:  * doing a "context" or "unified" diff.  (see routine "change" to
                    151:  * understand the highly mnemonic field names)
                    152:  */
                    153: struct context_vec {
1.53      xsa       154:        int     a;      /* start line in old file */
                    155:        int     b;      /* end line in old file */
                    156:        int     c;      /* start line in new file */
                    157:        int     d;      /* end line in new file */
1.1       jfb       158: };
                    159:
1.4       jfb       160: struct diff_arg {
1.53      xsa       161:        char    *rev1;
                    162:        char    *rev2;
                    163:        char    *date1;
                    164:        char    *date2;
1.4       jfb       165: };
1.46      xsa       166:
1.56      niallo    167: #if !defined(RCSPROG)
1.64      xsa       168: static int     cvs_diff_init(struct cvs_cmd *, int, char **, int *);
1.53      xsa       169: static int     cvs_diff_remote(CVSFILE *, void *);
                    170: static int     cvs_diff_local(CVSFILE *, void *);
                    171: static int     cvs_diff_pre_exec(struct cvsroot *);
                    172: static int     cvs_diff_cleanup(void);
1.56      niallo    173: #endif
1.53      xsa       174:
                    175: static void     output(const char *, FILE *, const char *, FILE *);
                    176: static void     check(FILE *, FILE *);
                    177: static void     range(int, int, char *);
                    178: static void     uni_range(int, int);
                    179: static void     dump_context_vec(FILE *, FILE *);
                    180: static void     dump_unified_vec(FILE *, FILE *);
                    181: static int      prepare(int, FILE *, off_t);
                    182: static void     prune(void);
                    183: static void     equiv(struct line *, int, struct line *, int, int *);
                    184: static void     unravel(int);
                    185: static void     unsort(struct line *, int, int *);
                    186: static void     change(const char *, FILE *, const char *, FILE *, int,
                    187:                    int, int, int);
                    188: static void     sort(struct line *, int);
                    189: static int      ignoreline(char *);
                    190: static int      asciifile(FILE *);
                    191: static int      fetch(long *, int, int, FILE *, int, int);
                    192: static int      newcand(int, int, int);
                    193: static int      search(int *, int, int);
                    194: static int      skipline(FILE *);
                    195: static int      isqrt(int);
                    196: static int      stone(int *, int, int *, int *);
                    197: static int      readhash(FILE *);
                    198: static int      files_differ(FILE *, FILE *);
                    199: static char    *match_function(const long *, int, FILE *);
                    200: static char    *preadline(int, size_t, off_t);
1.1       jfb       201:
                    202:
1.56      niallo    203: #if !defined(RCSPROG)
                    204: static int Nflag;
                    205: #endif
                    206: static int aflag, bflag, dflag, iflag, pflag, tflag, Tflag, wflag;
1.68      niallo    207: static int context = 3;
1.58      niallo    208: int diff_format = D_NORMAL;
1.61      joris     209: char *diff_file = NULL;
1.66      xsa       210: char diffargs[128];
1.1       jfb       211: static struct stat stb1, stb2;
1.56      niallo    212: static char *ifdefname, *ignore_pats;
1.1       jfb       213: regex_t ignore_re;
                    214:
                    215: static int  *J;                        /* will be overlaid on class */
                    216: static int  *class;            /* will be overlaid on file[0] */
                    217: static int  *klist;            /* will be overlaid on file[0] after class */
                    218: static int  *member;           /* will be overlaid on file[1] */
                    219: static int   clen;
                    220: static int   inifdef;          /* whether or not we are in a #ifdef block */
1.36      jfb       221: static int   diff_len[2];
1.1       jfb       222: static int   pref, suff;       /* length of prefix and suffix */
                    223: static int   slen[2];
                    224: static int   anychange;
                    225: static long *ixnew;            /* will be overlaid on file[1] */
                    226: static long *ixold;            /* will be overlaid on klist */
                    227: static struct cand *clist;     /* merely a free storage pot for candidates */
                    228: static int   clistlen;         /* the length of clist */
                    229: static struct line *sfile[2];  /* shortened by pruning common prefix/suffix */
                    230: static u_char *chrtran;                /* translation table for case-folding */
                    231: static struct context_vec *context_vec_start;
                    232: static struct context_vec *context_vec_end;
                    233: static struct context_vec *context_vec_ptr;
                    234:
                    235: #define FUNCTION_CONTEXT_SIZE  41
1.17      jfb       236: static char lastbuf[FUNCTION_CONTEXT_SIZE];
                    237: static int  lastline;
                    238: static int  lastmatchline;
1.63      joris     239: BUF  *diffbuf = NULL;
1.58      niallo    240:
1.1       jfb       241: /*
                    242:  * chrtran points to one of 2 translation tables: cup2low if folding upper to
                    243:  * lower case clow2low if not folding case
                    244:  */
                    245: u_char clow2low[256] = {
                    246:        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
                    247:        0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                    248:        0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
                    249:        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
                    250:        0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
                    251:        0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
                    252:        0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
                    253:        0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
                    254:        0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62,
                    255:        0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
                    256:        0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
                    257:        0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
                    258:        0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
                    259:        0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
                    260:        0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
                    261:        0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
                    262:        0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
                    263:        0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
                    264:        0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
                    265:        0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
                    266:        0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
                    267:        0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
                    268:        0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
                    269:        0xfd, 0xfe, 0xff
                    270: };
                    271:
                    272: u_char cup2low[256] = {
                    273:        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
                    274:        0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                    275:        0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
                    276:        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
                    277:        0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
                    278:        0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x60, 0x61,
                    279:        0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
                    280:        0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
                    281:        0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x60, 0x61, 0x62,
                    282:        0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
                    283:        0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
                    284:        0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
                    285:        0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
                    286:        0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
                    287:        0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
                    288:        0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
                    289:        0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
                    290:        0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
                    291:        0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
                    292:        0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
                    293:        0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
                    294:        0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
                    295:        0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
                    296:        0xfd, 0xfe, 0xff
                    297: };
                    298:
1.56      niallo    299: #if !defined(RCSPROG)
1.36      jfb       300: struct cvs_cmd cvs_cmd_diff = {
                    301:        CVS_OP_DIFF, CVS_REQ_DIFF, "diff",
                    302:        { "di", "dif" },
                    303:        "Show differences between revisions",
1.43      xsa       304:        "[-cilNnpu] [[-D date] [-r rev] [-D date2 | -r rev2]] "
                    305:        "[-k mode] [file ...]",
                    306:        "cD:iklNnpr:Ru",
1.36      jfb       307:        NULL,
                    308:        CF_RECURSE | CF_IGNORE | CF_SORT | CF_KNOWN,
                    309:        cvs_diff_init,
                    310:        cvs_diff_pre_exec,
                    311:        cvs_diff_remote,
                    312:        cvs_diff_local,
                    313:        NULL,
                    314:        cvs_diff_cleanup,
                    315:        CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR
                    316: };
                    317:
                    318:
                    319: struct cvs_cmd cvs_cmd_rdiff = {
                    320:        CVS_OP_RDIFF, CVS_REQ_DIFF, "rdiff",
1.43      xsa       321:        { "pa", "patch" },
1.36      jfb       322:        "Create 'patch' format diffs between releases",
1.43      xsa       323:        "[-flR] [-c | -u] [-s | -t] [-V ver] -D date | -r rev "
                    324:        "[-D date2 | -rev2] module ...",
                    325:        "cD:flRr:stuV:",
1.23      joris     326:        NULL,
                    327:        CF_RECURSE | CF_IGNORE | CF_SORT | CF_KNOWN,
1.36      jfb       328:        cvs_diff_init,
                    329:        cvs_diff_pre_exec,
                    330:        cvs_diff_remote,
                    331:        cvs_diff_local,
                    332:        NULL,
                    333:        cvs_diff_cleanup,
1.29      joris     334:        CVS_CMD_SENDARGS2 | CVS_CMD_ALLOWSPEC | CVS_CMD_SENDDIR
1.23      joris     335: };
1.56      niallo    336: #endif
1.23      joris     337:
1.56      niallo    338: #if !defined(RCSPROG)
1.23      joris     339: static struct diff_arg *dap = NULL;
                    340: static int recurse;
1.1       jfb       341:
1.36      jfb       342: static int
                    343: cvs_diff_init(struct cvs_cmd *cmd, int argc, char **argv, int *arg)
1.1       jfb       344: {
1.23      joris     345:        int ch;
1.1       jfb       346:
1.71      joris     347:        dap = (struct diff_arg *)xmalloc(sizeof(*dap));
1.23      joris     348:        dap->date1 = dap->date2 = dap->rev1 = dap->rev2 = NULL;
1.2       jfb       349:        strlcpy(diffargs, argv[0], sizeof(diffargs));
1.1       jfb       350:
1.36      jfb       351:        while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1       jfb       352:                switch (ch) {
                    353:                case 'c':
1.2       jfb       354:                        strlcat(diffargs, " -c", sizeof(diffargs));
1.58      niallo    355:                        diff_format = D_CONTEXT;
1.1       jfb       356:                        break;
                    357:                case 'D':
1.23      joris     358:                        if (dap->date1 == NULL && dap->rev1 == NULL) {
                    359:                                dap->date1 = optarg;
                    360:                        } else if (dap->date2 == NULL && dap->rev2 == NULL) {
                    361:                                dap->date2 = optarg;
                    362:                        } else {
1.1       jfb       363:                                cvs_log(LP_ERR,
                    364:                                    "no more than two revisions/dates can "
                    365:                                    "be specified");
                    366:                        }
                    367:                        break;
                    368:                case 'l':
1.2       jfb       369:                        strlcat(diffargs, " -l", sizeof(diffargs));
1.1       jfb       370:                        recurse = 0;
1.36      jfb       371:                        cvs_cmd_diff.file_flags &= ~CF_RECURSE;
1.1       jfb       372:                        break;
                    373:                case 'i':
1.2       jfb       374:                        strlcat(diffargs, " -i", sizeof(diffargs));
1.1       jfb       375:                        iflag = 1;
                    376:                        break;
1.8       jfb       377:                case 'N':
                    378:                        strlcat(diffargs, " -N", sizeof(diffargs));
                    379:                        Nflag = 1;
                    380:                        break;
1.33      jfb       381:                case 'n':
                    382:                        strlcat(diffargs, " -n", sizeof(diffargs));
1.58      niallo    383:                        diff_format = D_RCSDIFF;
1.33      jfb       384:                        break;
1.17      jfb       385:                case 'p':
                    386:                        strlcat(diffargs, " -p", sizeof(diffargs));
                    387:                        pflag = 1;
                    388:                        break;
1.1       jfb       389:                case 'r':
1.23      joris     390:                        if ((dap->rev1 == NULL) && (dap->date1 == NULL)) {
                    391:                                dap->rev1 = optarg;
                    392:                        } else if ((dap->rev2 == NULL) &&
                    393:                            (dap->date2 == NULL)) {
                    394:                                dap->rev2 = optarg;
                    395:                        } else {
1.1       jfb       396:                                cvs_log(LP_ERR,
                    397:                                    "no more than two revisions/dates can "
                    398:                                    "be specified");
1.28      joris     399:                                return (CVS_EX_USAGE);
1.1       jfb       400:                        }
1.32      joris     401:                        break;
                    402:                case 'R':
1.36      jfb       403:                        cvs_cmd_diff.file_flags |= CF_RECURSE;
1.1       jfb       404:                        break;
                    405:                case 'u':
1.2       jfb       406:                        strlcat(diffargs, " -u", sizeof(diffargs));
1.58      niallo    407:                        diff_format = D_UNIFIED;
1.1       jfb       408:                        break;
                    409:                default:
1.28      joris     410:                        return (CVS_EX_USAGE);
1.1       jfb       411:                }
                    412:        }
                    413:
1.23      joris     414:        *arg = optind;
                    415:        return (0);
                    416: }
1.1       jfb       417:
1.23      joris     418: int
                    419: cvs_diff_cleanup(void)
                    420: {
1.36      jfb       421:        if (dap != NULL) {
1.71      joris     422:                xfree(dap);
1.36      jfb       423:                dap = NULL;
                    424:        }
1.4       jfb       425:        return (0);
                    426: }
1.1       jfb       427:
1.4       jfb       428: /*
1.36      jfb       429:  * cvs_diff_pre_exec()
1.4       jfb       430:  *
                    431:  */
                    432: int
1.36      jfb       433: cvs_diff_pre_exec(struct cvsroot *root)
1.4       jfb       434: {
1.36      jfb       435:        if (root->cr_method != CVS_METHOD_LOCAL) {
                    436:                /* send the flags */
1.74      joris     437:                if (Nflag == 1)
                    438:                        cvs_sendarg(root, "-N", 0);
                    439:                if (pflag == 1)
                    440:                        cvs_sendarg(root, "-p", 0);
                    441:
                    442:                if (diff_format == D_CONTEXT)
                    443:                        cvs_sendarg(root, "-c", 0);
                    444:                else if (diff_format == D_UNIFIED)
                    445:                        cvs_sendarg(root, "-u", 0);
1.17      jfb       446:
1.36      jfb       447:                if (dap->rev1 != NULL) {
1.74      joris     448:                        cvs_sendarg(root, "-r", 0);
                    449:                        cvs_sendarg(root, dap->rev1, 0);
1.36      jfb       450:                } else if (dap->date1 != NULL) {
1.74      joris     451:                        cvs_sendarg(root, "-D", 0);
                    452:                        cvs_sendarg(root, dap->date1, 0);
1.36      jfb       453:                }
                    454:                if (dap->rev2 != NULL) {
1.74      joris     455:                        cvs_sendarg(root, "-r", 0);
                    456:                        cvs_sendarg(root, dap->rev2, 0);
1.36      jfb       457:                } else if (dap->date2 != NULL) {
1.74      joris     458:                        cvs_sendarg(root, "-D", 0);
                    459:                        cvs_sendarg(root, dap->date2, 0);
1.36      jfb       460:                }
1.1       jfb       461:        }
                    462:
                    463:        return (0);
                    464: }
                    465:
                    466:
                    467: /*
                    468:  * cvs_diff_file()
                    469:  *
                    470:  * Diff a single file.
                    471:  */
1.36      jfb       472: static int
                    473: cvs_diff_remote(struct cvs_file *cfp, void *arg)
1.1       jfb       474: {
1.36      jfb       475:        char *dir, *repo;
                    476:        char fpath[MAXPATHLEN], dfpath[MAXPATHLEN];
1.4       jfb       477:        struct cvsroot *root;
1.1       jfb       478:
1.4       jfb       479:        if (cfp->cf_type == DT_DIR) {
1.7       jfb       480:                if (cfp->cf_cvstat == CVS_FST_UNKNOWN) {
1.31      jfb       481:                        root = cfp->cf_parent->cf_root;
1.52      joris     482:                        cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cfp->cf_name);
1.10      deraadt   483:                } else {
1.31      jfb       484:                        root = cfp->cf_root;
1.23      joris     485: #if 0
1.7       jfb       486:                        if ((cfp->cf_parent == NULL) ||
1.31      jfb       487:                            (root != cfp->cf_parent->cf_root)) {
1.7       jfb       488:                                cvs_connect(root);
1.36      jfb       489:                                cvs_diff_pre_exec(root);
1.7       jfb       490:                        }
1.23      joris     491: #endif
1.7       jfb       492:
                    493:                        cvs_senddir(root, cfp);
1.4       jfb       494:                }
1.1       jfb       495:
1.13      jfb       496:                return (0);
                    497:        }
                    498:
                    499:        if (cfp->cf_cvstat == CVS_FST_LOST) {
1.52      joris     500:                cvs_log(LP_WARN, "cannot find file %s", cfp->cf_name);
1.4       jfb       501:                return (0);
1.1       jfb       502:        }
                    503:
1.8       jfb       504:        diff_file = cvs_file_getpath(cfp, fpath, sizeof(fpath));
1.7       jfb       505:
1.4       jfb       506:        if (cfp->cf_parent != NULL) {
1.8       jfb       507:                dir = cvs_file_getpath(cfp->cf_parent, dfpath, sizeof(dfpath));
1.31      jfb       508:                root = cfp->cf_parent->cf_root;
                    509:                repo = cfp->cf_parent->cf_repo;
1.10      deraadt   510:        } else {
1.4       jfb       511:                dir = ".";
1.7       jfb       512:                root = NULL;
1.4       jfb       513:                repo = NULL;
1.1       jfb       514:        }
                    515:
1.4       jfb       516:        if (cfp->cf_cvstat == CVS_FST_UNKNOWN) {
1.36      jfb       517:                cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cfp->cf_name);
1.4       jfb       518:                return (0);
1.1       jfb       519:        }
                    520:
1.74      joris     521:        cvs_sendentry(root, cfp);
1.1       jfb       522:
1.4       jfb       523:        if (cfp->cf_cvstat == CVS_FST_UPTODATE) {
1.36      jfb       524:                cvs_sendreq(root, CVS_REQ_UNCHANGED, cfp->cf_name);
1.1       jfb       525:                return (0);
                    526:        }
                    527:
                    528:        /* at this point, the file is modified */
1.74      joris     529:        cvs_sendreq(root, CVS_REQ_MODIFIED, cfp->cf_name);
                    530:        cvs_sendfile(root, diff_file);
1.36      jfb       531:
                    532:        return (0);
                    533: }
                    534:
                    535: static int
                    536: cvs_diff_local(CVSFILE *cf, void *arg)
                    537: {
                    538:        int len;
                    539:        char *repo, buf[64];
                    540:        char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
                    541:        char path_tmp1[MAXPATHLEN], path_tmp2[MAXPATHLEN];
                    542:        BUF *b1, *b2;
                    543:        RCSNUM *r1, *r2;
                    544:        RCSFILE *rf;
                    545:        struct cvsroot *root;
1.69      niallo    546:        struct timeval tv[2], tv2[2];
                    547:
                    548:        memset(&tv, 0, sizeof(tv));
                    549:        memset(&tv2, 0, sizeof(tv2));
1.1       jfb       550:
1.36      jfb       551:        rf = NULL;
                    552:        root = CVS_DIR_ROOT(cf);
                    553:        repo = CVS_DIR_REPO(cf);
                    554:        diff_file = cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1       jfb       555:
1.36      jfb       556:        if (cf->cf_type == DT_DIR) {
1.46      xsa       557:                if (verbosity > 1)
1.54      xsa       558:                        cvs_log(LP_NOTICE, "Diffing %s", fpath);
1.36      jfb       559:                return (0);
                    560:        }
1.1       jfb       561:
1.36      jfb       562:        if (cf->cf_cvstat == CVS_FST_LOST) {
                    563:                cvs_log(LP_WARN, "cannot find file %s", cf->cf_name);
                    564:                return (0);
                    565:        }
1.1       jfb       566:
1.36      jfb       567:        if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
                    568:                cvs_log(LP_WARN, "I know nothing about %s", diff_file);
                    569:                return (0);
1.45      niallo    570:        } else if (cf->cf_cvstat == CVS_FST_UPTODATE)
1.36      jfb       571:                return (0);
1.1       jfb       572:
1.36      jfb       573:        /* at this point, the file is modified */
                    574:        len = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
                    575:            root->cr_dir, repo, diff_file, RCS_FILE_EXT);
                    576:        if (len == -1 || len >= (int)sizeof(rcspath)) {
                    577:                errno = ENAMETOOLONG;
                    578:                cvs_log(LP_ERRNO, "%s", rcspath);
                    579:                return (CVS_EX_DATA);
                    580:        }
1.20      jfb       581:
1.36      jfb       582:        rf = rcs_open(rcspath, RCS_READ);
                    583:        if (rf == NULL) {
                    584:                return (CVS_EX_DATA);
                    585:        }
1.1       jfb       586:
1.36      jfb       587:        cvs_printf("Index: %s\n%s\nRCS file: %s\n", diff_file,
                    588:            RCS_DIFF_DIV, rcspath);
1.4       jfb       589:
1.36      jfb       590:        if (dap->rev1 == NULL)
                    591:                r1 = cf->cf_lrev;
                    592:        else {
                    593:                if ((r1 = rcsnum_parse(dap->rev1)) == NULL) {
1.62      joris     594:                        rcs_close(rf);
1.28      joris     595:                        return (CVS_EX_DATA);
1.20      jfb       596:                }
1.36      jfb       597:        }
                    598:
                    599:        cvs_printf("retrieving revision %s\n",
                    600:            rcsnum_tostr(r1, buf, sizeof(buf)));
                    601:        b1 = rcs_getrev(rf, r1);
                    602:
1.39      joris     603:        if (b1 == NULL) {
1.40      xsa       604:                cvs_log(LP_ERR, "failed to retrieve revision %s\n",
1.39      joris     605:                    rcsnum_tostr(r1, buf, sizeof(buf)));
                    606:                if (r1 != cf->cf_lrev)
                    607:                        rcsnum_free(r1);
1.62      joris     608:                rcs_close(rf);
1.39      joris     609:                return (CVS_EX_DATA);
                    610:        }
1.69      niallo    611:        tv[0].tv_sec = (long)rcs_rev_getdate(rf, r1);
                    612:        tv[1].tv_sec = tv[0].tv_sec;
1.39      joris     613:
1.36      jfb       614:        if (r1 != cf->cf_lrev)
                    615:                rcsnum_free(r1);
1.20      jfb       616:
1.36      jfb       617:        if (dap->rev2 != NULL) {
                    618:                cvs_printf("retrieving revision %s\n", dap->rev2);
                    619:                if ((r2 = rcsnum_parse(dap->rev2)) == NULL) {
1.62      joris     620:                        rcs_close(rf);
1.28      joris     621:                        return (CVS_EX_DATA);
1.12      djm       622:                }
1.36      jfb       623:                b2 = rcs_getrev(rf, r2);
1.70      niallo    624:                tv2[0].tv_sec = (long)rcs_rev_getdate(rf, r2);
                    625:                tv2[1].tv_sec = tv2[0].tv_sec;
1.36      jfb       626:                rcsnum_free(r2);
                    627:        } else {
1.70      niallo    628:                struct stat st;
                    629:                if (stat(diff_file, &st) < 0) {
                    630:                        cvs_log(LP_ERR, "failed to retrieve revision %s\n",
                    631:                            dap->rev2);
                    632:                        cvs_buf_free(b1);
                    633:                        return (CVS_EX_DATA);
                    634:                }
1.36      jfb       635:                b2 = cvs_buf_load(diff_file, BUF_AUTOEXT);
1.70      niallo    636:                tv2[0].tv_sec = st.st_mtime;
                    637:                tv2[1].tv_sec = st.st_mtime;
1.36      jfb       638:        }
                    639:
                    640:        rcs_close(rf);
1.39      joris     641:
                    642:        if (b2 == NULL) {
1.40      xsa       643:                cvs_log(LP_ERR, "failed to retrieve revision %s\n",
1.39      joris     644:                    dap->rev2);
                    645:                cvs_buf_free(b1);
                    646:                return (CVS_EX_DATA);
                    647:        }
1.36      jfb       648:
1.42      joris     649:        cvs_printf("%s", diffargs);
                    650:        cvs_printf(" -r%s", buf);
1.36      jfb       651:        if (dap->rev2 != NULL)
1.42      joris     652:                cvs_printf(" -r%s", dap->rev2);
                    653:        cvs_printf(" %s\n", diff_file);
1.67      xsa       654:        strlcpy(path_tmp1, cvs_tmpdir, sizeof(path_tmp1));
                    655:        strlcat(path_tmp1, "/diff1.XXXXXXXXXX", sizeof(path_tmp1));
1.72      xsa       656:        cvs_buf_write_stmp(b1, path_tmp1, 0600);
1.36      jfb       657:        cvs_buf_free(b1);
1.69      niallo    658:        if (utimes(path_tmp1, (const struct timeval *)&tv) < 0)
                    659:                cvs_log(LP_ERRNO, "error setting utimes");
1.20      jfb       660:
1.67      xsa       661:        strlcpy(path_tmp2, cvs_tmpdir, sizeof(path_tmp2));
                    662:        strlcat(path_tmp2, "/diff2.XXXXXXXXXX", sizeof(path_tmp2));
1.73      xsa       663:        cvs_buf_write_stmp(b2, path_tmp2, 0600);
1.36      jfb       664:        cvs_buf_free(b2);
1.69      niallo    665:        if (utimes(path_tmp2, (const struct timeval *)&tv2) < 0)
                    666:                cvs_log(LP_ERRNO, "error setting utimes");
1.36      jfb       667:
1.58      niallo    668:        cvs_diffreg(path_tmp1, path_tmp2, NULL);
1.36      jfb       669:        (void)unlink(path_tmp1);
                    670:        (void)unlink(path_tmp2);
1.1       jfb       671:
                    672:        return (0);
                    673: }
1.56      niallo    674: #endif
1.1       jfb       675:
                    676:
                    677: int
1.58      niallo    678: cvs_diffreg(const char *file1, const char *file2, BUF *out)
1.1       jfb       679: {
                    680:        FILE *f1, *f2;
                    681:        int i, rval;
1.20      jfb       682:        void *tmp;
1.1       jfb       683:
                    684:        f1 = f2 = NULL;
                    685:        rval = D_SAME;
                    686:        anychange = 0;
                    687:        lastline = 0;
                    688:        lastmatchline = 0;
                    689:        context_vec_ptr = context_vec_start - 1;
                    690:        chrtran = (iflag ? cup2low : clow2low);
1.58      niallo    691:        if (out != NULL)
                    692:                diffbuf = out;
1.1       jfb       693:
                    694:        f1 = fopen(file1, "r");
                    695:        if (f1 == NULL) {
                    696:                cvs_log(LP_ERRNO, "%s", file1);
                    697:                goto closem;
                    698:        }
                    699:
                    700:        f2 = fopen(file2, "r");
                    701:        if (f2 == NULL) {
                    702:                cvs_log(LP_ERRNO, "%s", file2);
                    703:                goto closem;
                    704:        }
                    705:
1.69      niallo    706:        if (stat(file1, &stb1) < 0) {
                    707:                cvs_log(LP_ERRNO, "%s", file1);
                    708:                goto closem;
                    709:        }
                    710:        if (stat(file2, &stb2) < 0) {
                    711:                cvs_log(LP_ERRNO, "%s", file2);
                    712:                goto closem;
                    713:        }
1.1       jfb       714:        switch (files_differ(f1, f2)) {
                    715:        case 0:
                    716:                goto closem;
                    717:        case 1:
                    718:                break;
                    719:        default:
                    720:                /* error */
                    721:                goto closem;
                    722:        }
                    723:
                    724:        if (!asciifile(f1) || !asciifile(f2)) {
                    725:                rval = D_BINARY;
                    726:                goto closem;
                    727:        }
1.20      jfb       728:        if ((prepare(0, f1, stb1.st_size) < 0) ||
                    729:            (prepare(1, f2, stb2.st_size) < 0)) {
                    730:                goto closem;
                    731:        }
1.1       jfb       732:        prune();
                    733:        sort(sfile[0], slen[0]);
                    734:        sort(sfile[1], slen[1]);
                    735:
                    736:        member = (int *)file[1];
                    737:        equiv(sfile[0], slen[0], sfile[1], slen[1], member);
1.71      joris     738:        tmp = xrealloc(member, (slen[1] + 2) * sizeof(int));
1.20      jfb       739:        member = (int *)tmp;
1.1       jfb       740:
                    741:        class = (int *)file[0];
                    742:        unsort(sfile[0], slen[0], class);
1.71      joris     743:        tmp = xrealloc(class, (slen[0] + 2) * sizeof(int));
1.20      jfb       744:        class = (int *)tmp;
1.1       jfb       745:
1.71      joris     746:        klist = xmalloc((slen[0] + 2) * sizeof(int));
1.1       jfb       747:        clen = 0;
                    748:        clistlen = 100;
1.71      joris     749:        clist = xmalloc(clistlen * sizeof(cand));
1.48      joris     750:
                    751:        if ((i = stone(class, slen[0], member, klist)) < 0)
                    752:                goto closem;
                    753:
1.71      joris     754:        xfree(member);
                    755:        xfree(class);
1.1       jfb       756:
1.71      joris     757:        tmp = xrealloc(J, (diff_len[0] + 2) * sizeof(int));
1.49      niallo    758:        J = (int *)tmp;
1.1       jfb       759:        unravel(klist[i]);
1.71      joris     760:        xfree(clist);
                    761:        xfree(klist);
1.1       jfb       762:
1.71      joris     763:        tmp = xrealloc(ixold, (diff_len[0] + 2) * sizeof(long));
1.49      niallo    764:        ixold = (long *)tmp;
1.71      joris     765:
                    766:        tmp = xrealloc(ixnew, (diff_len[1] + 2) * sizeof(long));
1.49      niallo    767:        ixnew = (long *)tmp;
1.1       jfb       768:        check(f1, f2);
                    769:        output(file1, f1, file2, f2);
                    770:
                    771: closem:
1.65      xsa       772:        if (anychange == 1) {
1.1       jfb       773:                if (rval == D_SAME)
                    774:                        rval = D_DIFFER;
                    775:        }
                    776:        if (f1 != NULL)
                    777:                fclose(f1);
                    778:        if (f2 != NULL)
                    779:                fclose(f2);
1.20      jfb       780:
1.1       jfb       781:        return (rval);
                    782: }
                    783:
                    784: /*
                    785:  * Check to see if the given files differ.
                    786:  * Returns 0 if they are the same, 1 if different, and -1 on error.
                    787:  * XXX - could use code from cmp(1) [faster]
                    788:  */
                    789: static int
                    790: files_differ(FILE *f1, FILE *f2)
                    791: {
                    792:        char buf1[BUFSIZ], buf2[BUFSIZ];
                    793:        size_t i, j;
                    794:
                    795:        if (stb1.st_size != stb2.st_size)
                    796:                return (1);
                    797:        for (;;) {
1.55      xsa       798:                i = fread(buf1, (size_t)1, sizeof(buf1), f1);
                    799:                j = fread(buf2, (size_t)1, sizeof(buf2), f2);
1.1       jfb       800:                if (i != j)
                    801:                        return (1);
1.65      xsa       802:                if ((i == 0) && (j == 0)) {
1.1       jfb       803:                        if (ferror(f1) || ferror(f2))
                    804:                                return (1);
                    805:                        return (0);
                    806:                }
                    807:                if (memcmp(buf1, buf2, i) != 0)
                    808:                        return (1);
                    809:        }
                    810: }
                    811:
1.20      jfb       812: static int
1.1       jfb       813: prepare(int i, FILE *fd, off_t filesize)
                    814: {
1.20      jfb       815:        void *tmp;
1.1       jfb       816:        struct line *p;
                    817:        int j, h;
                    818:        size_t sz;
                    819:
                    820:        rewind(fd);
                    821:
1.16      xsa       822:        sz = ((size_t)filesize <= SIZE_MAX ? (size_t)filesize : SIZE_MAX) / 25;
1.1       jfb       823:        if (sz < 100)
                    824:                sz = 100;
                    825:
1.71      joris     826:        p = (struct line *)xmalloc((sz + 3) * sizeof(struct line));
1.1       jfb       827:        for (j = 0; (h = readhash(fd));) {
                    828:                if (j == (int)sz) {
                    829:                        sz = sz * 3 / 2;
1.71      joris     830:                        tmp = xrealloc(p, (sz + 3) * sizeof(struct line));
1.20      jfb       831:                        p = (struct line *)tmp;
1.1       jfb       832:                }
                    833:                p[++j].value = h;
                    834:        }
1.36      jfb       835:        diff_len[i] = j;
1.1       jfb       836:        file[i] = p;
1.20      jfb       837:
                    838:        return (0);
1.1       jfb       839: }
                    840:
                    841: static void
                    842: prune(void)
                    843: {
                    844:        int i, j;
                    845:
1.36      jfb       846:        for (pref = 0; pref < diff_len[0] && pref < diff_len[1] &&
1.1       jfb       847:            file[0][pref + 1].value == file[1][pref + 1].value;
                    848:            pref++)
                    849:                ;
1.36      jfb       850:        for (suff = 0;
                    851:            (suff < diff_len[0] - pref) && (suff < diff_len[1] - pref) &&
                    852:            (file[0][diff_len[0] - suff].value ==
                    853:            file[1][diff_len[1] - suff].value);
1.1       jfb       854:            suff++)
                    855:                ;
                    856:        for (j = 0; j < 2; j++) {
                    857:                sfile[j] = file[j] + pref;
1.36      jfb       858:                slen[j] = diff_len[j] - pref - suff;
1.1       jfb       859:                for (i = 0; i <= slen[j]; i++)
                    860:                        sfile[j][i].serial = i;
                    861:        }
                    862: }
                    863:
                    864: static void
                    865: equiv(struct line *a, int n, struct line *b, int m, int *c)
                    866: {
                    867:        int i, j;
                    868:
                    869:        i = j = 1;
                    870:        while (i <= n && j <= m) {
                    871:                if (a[i].value < b[j].value)
                    872:                        a[i++].value = 0;
                    873:                else if (a[i].value == b[j].value)
                    874:                        a[i++].value = j;
                    875:                else
                    876:                        j++;
                    877:        }
                    878:        while (i <= n)
                    879:                a[i++].value = 0;
                    880:        b[m + 1].value = 0;
                    881:        j = 0;
                    882:        while (++j <= m) {
                    883:                c[j] = -b[j].serial;
                    884:                while (b[j + 1].value == b[j].value) {
                    885:                        j++;
                    886:                        c[j] = b[j].serial;
                    887:                }
                    888:        }
                    889:        c[j] = -1;
                    890: }
                    891:
                    892: /* Code taken from ping.c */
                    893: static int
                    894: isqrt(int n)
                    895: {
                    896:        int y, x = 1;
                    897:
                    898:        if (n == 0)
1.18      jfb       899:                return (0);
1.1       jfb       900:
                    901:        do { /* newton was a stinker */
                    902:                y = x;
                    903:                x = n / x;
                    904:                x += y;
                    905:                x /= 2;
                    906:        } while ((x - y) > 1 || (x - y) < -1);
                    907:
                    908:        return (x);
                    909: }
                    910:
                    911: static int
                    912: stone(int *a, int n, int *b, int *c)
                    913: {
1.48      joris     914:        int ret;
1.1       jfb       915:        int i, k, y, j, l;
                    916:        int oldc, tc, oldl;
                    917:        u_int numtries;
                    918:
1.18      jfb       919:        /* XXX move the isqrt() out of the macro to avoid multiple calls */
                    920:        const u_int bound = dflag ? UINT_MAX : MAX(256, (u_int)isqrt(n));
1.1       jfb       921:
                    922:        k = 0;
1.48      joris     923:        if ((ret = newcand(0, 0, 0)) < 0)
                    924:                return (-1);
                    925:        c[0] = ret;
1.1       jfb       926:        for (i = 1; i <= n; i++) {
                    927:                j = a[i];
                    928:                if (j == 0)
                    929:                        continue;
                    930:                y = -b[j];
                    931:                oldl = 0;
                    932:                oldc = c[0];
                    933:                numtries = 0;
                    934:                do {
                    935:                        if (y <= clist[oldc].y)
                    936:                                continue;
                    937:                        l = search(c, k, y);
                    938:                        if (l != oldl + 1)
                    939:                                oldc = c[l - 1];
                    940:                        if (l <= k) {
                    941:                                if (clist[c[l]].y <= y)
                    942:                                        continue;
                    943:                                tc = c[l];
1.48      joris     944:                                if ((ret = newcand(i, y, oldc)) < 0)
                    945:                                        return (-1);
                    946:                                c[l] = ret;
1.1       jfb       947:                                oldc = tc;
                    948:                                oldl = l;
                    949:                                numtries++;
                    950:                        } else {
1.48      joris     951:                                if ((ret = newcand(i, y, oldc)) < 0)
                    952:                                        return (-1);
                    953:                                c[l] = ret;
1.1       jfb       954:                                k++;
                    955:                                break;
                    956:                        }
                    957:                } while ((y = b[++j]) > 0 && numtries < bound);
                    958:        }
                    959:        return (k);
                    960: }
                    961:
                    962: static int
                    963: newcand(int x, int y, int pred)
                    964: {
1.49      niallo    965:        struct cand *q, *tmp;
                    966:        int newclistlen;
1.1       jfb       967:
                    968:        if (clen == clistlen) {
1.49      niallo    969:                newclistlen = clistlen * 11 / 10;
1.71      joris     970:                tmp = xrealloc(clist, newclistlen * sizeof(cand));
1.49      niallo    971:                clist = tmp;
                    972:                clistlen = newclistlen;
1.1       jfb       973:        }
                    974:        q = clist + clen;
                    975:        q->x = x;
                    976:        q->y = y;
                    977:        q->pred = pred;
                    978:        return (clen++);
                    979: }
                    980:
                    981: static int
                    982: search(int *c, int k, int y)
                    983: {
                    984:        int i, j, l, t;
                    985:
                    986:        if (clist[c[k]].y < y)  /* quick look for typical case */
                    987:                return (k + 1);
                    988:        i = 0;
                    989:        j = k + 1;
                    990:        while (1) {
                    991:                l = i + j;
                    992:                if ((l >>= 1) <= i)
                    993:                        break;
                    994:                t = clist[c[l]].y;
                    995:                if (t > y)
                    996:                        j = l;
                    997:                else if (t < y)
                    998:                        i = l;
                    999:                else
                   1000:                        return (l);
                   1001:        }
                   1002:        return (l + 1);
                   1003: }
                   1004:
                   1005: static void
                   1006: unravel(int p)
                   1007: {
                   1008:        struct cand *q;
                   1009:        int i;
                   1010:
1.36      jfb      1011:        for (i = 0; i <= diff_len[0]; i++)
1.1       jfb      1012:                J[i] = i <= pref ? i :
1.36      jfb      1013:                    i > diff_len[0] - suff ? i + diff_len[1] - diff_len[0] : 0;
1.1       jfb      1014:        for (q = clist + p; q->y != 0; q = clist + q->pred)
                   1015:                J[q->x + pref] = q->y + pref;
                   1016: }
                   1017:
                   1018: /*
                   1019:  * Check does double duty:
                   1020:  *  1. ferret out any fortuitous correspondences due
                   1021:  *     to confounding by hashing (which result in "jackpot")
                   1022:  *  2.  collect random access indexes to the two files
                   1023:  */
                   1024: static void
                   1025: check(FILE *f1, FILE *f2)
                   1026: {
                   1027:        int i, j, jackpot, c, d;
                   1028:        long ctold, ctnew;
                   1029:
                   1030:        rewind(f1);
                   1031:        rewind(f2);
                   1032:        j = 1;
                   1033:        ixold[0] = ixnew[0] = 0;
                   1034:        jackpot = 0;
                   1035:        ctold = ctnew = 0;
1.36      jfb      1036:        for (i = 1; i <= diff_len[0]; i++) {
1.1       jfb      1037:                if (J[i] == 0) {
                   1038:                        ixold[i] = ctold += skipline(f1);
                   1039:                        continue;
                   1040:                }
                   1041:                while (j < J[i]) {
                   1042:                        ixnew[j] = ctnew += skipline(f2);
                   1043:                        j++;
                   1044:                }
1.64      xsa      1045:                if ((bflag == 1)|| (wflag == 1) || (iflag == 1)) {
1.1       jfb      1046:                        for (;;) {
                   1047:                                c = getc(f1);
                   1048:                                d = getc(f2);
                   1049:                                /*
                   1050:                                 * GNU diff ignores a missing newline
                   1051:                                 * in one file if bflag || wflag.
                   1052:                                 */
1.64      xsa      1053:                                if (((bflag == 1) || (wflag == 1)) &&
1.1       jfb      1054:                                    ((c == EOF && d == '\n') ||
                   1055:                                    (c == '\n' && d == EOF))) {
                   1056:                                        break;
                   1057:                                }
                   1058:                                ctold++;
                   1059:                                ctnew++;
1.64      xsa      1060:                                if ((bflag == 1) && isspace(c) && isspace(d)) {
1.1       jfb      1061:                                        do {
                   1062:                                                if (c == '\n')
                   1063:                                                        break;
                   1064:                                                ctold++;
                   1065:                                        } while (isspace(c = getc(f1)));
                   1066:                                        do {
                   1067:                                                if (d == '\n')
                   1068:                                                        break;
                   1069:                                                ctnew++;
                   1070:                                        } while (isspace(d = getc(f2)));
1.64      xsa      1071:                                } else if (wflag == 1) {
1.1       jfb      1072:                                        while (isspace(c) && c != '\n') {
                   1073:                                                c = getc(f1);
                   1074:                                                ctold++;
                   1075:                                        }
                   1076:                                        while (isspace(d) && d != '\n') {
                   1077:                                                d = getc(f2);
                   1078:                                                ctnew++;
                   1079:                                        }
                   1080:                                }
                   1081:                                if (chrtran[c] != chrtran[d]) {
                   1082:                                        jackpot++;
                   1083:                                        J[i] = 0;
1.65      xsa      1084:                                        if ((c != '\n') && (c != EOF))
1.1       jfb      1085:                                                ctold += skipline(f1);
1.65      xsa      1086:                                        if ((d != '\n') && (c != EOF))
1.1       jfb      1087:                                                ctnew += skipline(f2);
                   1088:                                        break;
                   1089:                                }
1.65      xsa      1090:                                if ((c == '\n') || (c == EOF))
1.1       jfb      1091:                                        break;
                   1092:                        }
                   1093:                } else {
                   1094:                        for (;;) {
                   1095:                                ctold++;
                   1096:                                ctnew++;
                   1097:                                if ((c = getc(f1)) != (d = getc(f2))) {
                   1098:                                        /* jackpot++; */
                   1099:                                        J[i] = 0;
1.65      xsa      1100:                                        if ((c != '\n') && (c != EOF))
1.1       jfb      1101:                                                ctold += skipline(f1);
1.65      xsa      1102:                                        if ((d != '\n') && (c != EOF))
1.1       jfb      1103:                                                ctnew += skipline(f2);
                   1104:                                        break;
                   1105:                                }
1.65      xsa      1106:                                if ((c == '\n') || (c == EOF))
1.1       jfb      1107:                                        break;
                   1108:                        }
                   1109:                }
                   1110:                ixold[i] = ctold;
                   1111:                ixnew[j] = ctnew;
                   1112:                j++;
                   1113:        }
1.36      jfb      1114:        for (; j <= diff_len[1]; j++)
1.1       jfb      1115:                ixnew[j] = ctnew += skipline(f2);
                   1116:        /*
1.65      xsa      1117:         * if (jackpot != 0)
1.42      joris    1118:         *      cvs_printf("jackpot\n");
1.1       jfb      1119:         */
                   1120: }
                   1121:
                   1122: /* shellsort CACM #201 */
                   1123: static void
                   1124: sort(struct line *a, int n)
                   1125: {
                   1126:        struct line *ai, *aim, w;
                   1127:        int j, m = 0, k;
                   1128:
                   1129:        if (n == 0)
                   1130:                return;
                   1131:        for (j = 1; j <= n; j *= 2)
                   1132:                m = 2 * j - 1;
                   1133:        for (m /= 2; m != 0; m /= 2) {
                   1134:                k = n - m;
                   1135:                for (j = 1; j <= k; j++) {
                   1136:                        for (ai = &a[j]; ai > a; ai -= m) {
                   1137:                                aim = &ai[m];
                   1138:                                if (aim < ai)
                   1139:                                        break;  /* wraparound */
                   1140:                                if (aim->value > ai[0].value ||
                   1141:                                    (aim->value == ai[0].value &&
                   1142:                                        aim->serial > ai[0].serial))
                   1143:                                        break;
                   1144:                                w.value = ai[0].value;
                   1145:                                ai[0].value = aim->value;
                   1146:                                aim->value = w.value;
                   1147:                                w.serial = ai[0].serial;
                   1148:                                ai[0].serial = aim->serial;
                   1149:                                aim->serial = w.serial;
                   1150:                        }
                   1151:                }
                   1152:        }
                   1153: }
                   1154:
                   1155: static void
                   1156: unsort(struct line *f, int l, int *b)
                   1157: {
                   1158:        int *a, i;
                   1159:
1.71      joris    1160:        a = (int *)xmalloc((l + 1) * sizeof(int));
1.1       jfb      1161:        for (i = 1; i <= l; i++)
                   1162:                a[f[i].serial] = f[i].value;
                   1163:        for (i = 1; i <= l; i++)
                   1164:                b[i] = a[i];
1.71      joris    1165:        xfree(a);
1.1       jfb      1166: }
                   1167:
                   1168: static int
                   1169: skipline(FILE *f)
                   1170: {
                   1171:        int i, c;
                   1172:
                   1173:        for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
                   1174:                continue;
                   1175:        return (i);
                   1176: }
                   1177:
                   1178: static void
                   1179: output(const char *file1, FILE *f1, const char *file2, FILE *f2)
                   1180: {
                   1181:        int m, i0, i1, j0, j1;
                   1182:
                   1183:        rewind(f1);
                   1184:        rewind(f2);
1.36      jfb      1185:        m = diff_len[0];
1.1       jfb      1186:        J[0] = 0;
1.36      jfb      1187:        J[m + 1] = diff_len[1] + 1;
1.1       jfb      1188:        for (i0 = 1; i0 <= m; i0 = i1 + 1) {
                   1189:                while (i0 <= m && J[i0] == J[i0 - 1] + 1)
                   1190:                        i0++;
                   1191:                j0 = J[i0 - 1] + 1;
                   1192:                i1 = i0 - 1;
                   1193:                while (i1 < m && J[i1 + 1] == 0)
                   1194:                        i1++;
                   1195:                j1 = J[i1 + 1] - 1;
                   1196:                J[i1] = j1;
                   1197:                change(file1, f1, file2, f2, i0, i1, j0, j1);
                   1198:        }
                   1199:        if (m == 0)
1.36      jfb      1200:                change(file1, f1, file2, f2, 1, 0, 1, diff_len[1]);
1.58      niallo   1201:        if (diff_format == D_IFDEF) {
1.1       jfb      1202:                for (;;) {
                   1203: #define        c i0
                   1204:                        if ((c = getc(f1)) == EOF)
                   1205:                                return;
1.58      niallo   1206:                        diff_output("%c", c);
1.1       jfb      1207:                }
                   1208: #undef c
                   1209:        }
                   1210:        if (anychange != 0) {
1.58      niallo   1211:                if (diff_format == D_CONTEXT)
1.1       jfb      1212:                        dump_context_vec(f1, f2);
1.58      niallo   1213:                else if (diff_format == D_UNIFIED)
1.1       jfb      1214:                        dump_unified_vec(f1, f2);
                   1215:        }
                   1216: }
                   1217:
                   1218: static __inline void
                   1219: range(int a, int b, char *separator)
                   1220: {
1.58      niallo   1221:        diff_output("%d", a > b ? b : a);
1.1       jfb      1222:        if (a < b)
1.58      niallo   1223:                diff_output("%s%d", separator, b);
1.1       jfb      1224: }
                   1225:
                   1226: static __inline void
                   1227: uni_range(int a, int b)
                   1228: {
                   1229:        if (a < b)
1.58      niallo   1230:                diff_output("%d,%d", a, b - a + 1);
1.1       jfb      1231:        else if (a == b)
1.58      niallo   1232:                diff_output("%d", b);
1.1       jfb      1233:        else
1.58      niallo   1234:                diff_output("%d,0", b);
1.1       jfb      1235: }
                   1236:
                   1237: static char *
1.15      jfb      1238: preadline(int fd, size_t rlen, off_t off)
1.1       jfb      1239: {
                   1240:        char *line;
                   1241:        ssize_t nr;
                   1242:
1.71      joris    1243:        line = xmalloc(rlen + 1);
1.15      jfb      1244:        if ((nr = pread(fd, line, rlen, off)) < 0) {
1.1       jfb      1245:                cvs_log(LP_ERRNO, "preadline failed");
                   1246:                return (NULL);
                   1247:        }
                   1248:        line[nr] = '\0';
                   1249:        return (line);
                   1250: }
                   1251:
                   1252: static int
                   1253: ignoreline(char *line)
                   1254: {
                   1255:        int ret;
                   1256:
1.55      xsa      1257:        ret = regexec(&ignore_re, line, (size_t)0, NULL, 0);
1.71      joris    1258:        xfree(line);
1.1       jfb      1259:        return (ret == 0);      /* if it matched, it should be ignored. */
                   1260: }
                   1261:
                   1262: /*
                   1263:  * Indicate that there is a difference between lines a and b of the from file
                   1264:  * to get to lines c to d of the to file.  If a is greater then b then there
                   1265:  * are no lines in the from file involved and this means that there were
                   1266:  * lines appended (beginning at b).  If c is greater than d then there are
                   1267:  * lines missing from the to file.
                   1268:  */
                   1269: static void
                   1270: change(const char *file1, FILE *f1, const char *file2, FILE *f2,
                   1271:        int a, int b, int c, int d)
                   1272: {
                   1273:        static size_t max_context = 64;
                   1274:        int i;
                   1275:
1.58      niallo   1276:        if (diff_format != D_IFDEF && a > b && c > d)
1.1       jfb      1277:                return;
                   1278:        if (ignore_pats != NULL) {
                   1279:                char *line;
                   1280:                /*
                   1281:                 * All lines in the change, insert, or delete must
                   1282:                 * match an ignore pattern for the change to be
                   1283:                 * ignored.
                   1284:                 */
                   1285:                if (a <= b) {           /* Changes and deletes. */
                   1286:                        for (i = a; i <= b; i++) {
                   1287:                                line = preadline(fileno(f1),
                   1288:                                    ixold[i] - ixold[i - 1], ixold[i - 1]);
                   1289:                                if (!ignoreline(line))
                   1290:                                        goto proceed;
                   1291:                        }
                   1292:                }
1.65      xsa      1293:                if ((a > b) || (c <= d)) {      /* Changes and inserts. */
1.1       jfb      1294:                        for (i = c; i <= d; i++) {
                   1295:                                line = preadline(fileno(f2),
                   1296:                                    ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
                   1297:                                if (!ignoreline(line))
                   1298:                                        goto proceed;
                   1299:                        }
                   1300:                }
                   1301:                return;
                   1302:        }
                   1303: proceed:
1.58      niallo   1304:        if (diff_format == D_CONTEXT || diff_format == D_UNIFIED) {
1.1       jfb      1305:                /*
                   1306:                 * Allocate change records as needed.
                   1307:                 */
                   1308:                if (context_vec_ptr == context_vec_end - 1) {
1.49      niallo   1309:                        struct context_vec *tmp;
1.1       jfb      1310:                        ptrdiff_t offset = context_vec_ptr - context_vec_start;
                   1311:                        max_context <<= 1;
1.71      joris    1312:                        tmp = xrealloc(context_vec_start, max_context *
                   1313:                            sizeof(struct context_vec));
1.49      niallo   1314:                        context_vec_start = tmp;
1.1       jfb      1315:                        context_vec_end = context_vec_start + max_context;
                   1316:                        context_vec_ptr = context_vec_start + offset;
                   1317:                }
                   1318:                if (anychange == 0) {
                   1319:                        /*
                   1320:                         * Print the context/unidiff header first time through.
                   1321:                         */
1.58      niallo   1322:                        diff_output("%s %s      %s",
                   1323:                            diff_format == D_CONTEXT ? "***" : "---", diff_file,
1.1       jfb      1324:                            ctime(&stb1.st_mtime));
1.58      niallo   1325:                        diff_output("%s %s      %s",
                   1326:                            diff_format == D_CONTEXT ? "---" : "+++", diff_file,
1.1       jfb      1327:                            ctime(&stb2.st_mtime));
                   1328:                        anychange = 1;
                   1329:                } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
                   1330:                    c > context_vec_ptr->d + (2 * context) + 1) {
                   1331:                        /*
                   1332:                         * If this change is more than 'context' lines from the
                   1333:                         * previous change, dump the record and reset it.
                   1334:                         */
1.58      niallo   1335:                        if (diff_format == D_CONTEXT)
1.1       jfb      1336:                                dump_context_vec(f1, f2);
                   1337:                        else
                   1338:                                dump_unified_vec(f1, f2);
                   1339:                }
                   1340:                context_vec_ptr++;
                   1341:                context_vec_ptr->a = a;
                   1342:                context_vec_ptr->b = b;
                   1343:                context_vec_ptr->c = c;
                   1344:                context_vec_ptr->d = d;
                   1345:                return;
                   1346:        }
                   1347:        if (anychange == 0)
                   1348:                anychange = 1;
1.58      niallo   1349:        switch (diff_format) {
1.1       jfb      1350:        case D_BRIEF:
                   1351:                return;
                   1352:        case D_NORMAL:
                   1353:                range(a, b, ",");
1.58      niallo   1354:                diff_output("%c", a > b ? 'a' : c > d ? 'd' : 'c');
                   1355:                if (diff_format == D_NORMAL)
1.1       jfb      1356:                        range(c, d, ",");
1.58      niallo   1357:                diff_output("\n");
1.1       jfb      1358:                break;
1.33      jfb      1359:        case D_RCSDIFF:
                   1360:                if (a > b)
1.58      niallo   1361:                        diff_output("a%d %d\n", b, d - c + 1);
1.33      jfb      1362:                else {
1.58      niallo   1363:                        diff_output("d%d %d\n", a, b - a + 1);
1.33      jfb      1364:
                   1365:                        if (!(c > d))   /* add changed lines */
1.58      niallo   1366:                                diff_output("a%d %d\n", b, d - c + 1);
1.33      jfb      1367:                }
                   1368:                break;
1.1       jfb      1369:        }
1.58      niallo   1370:        if (diff_format == D_NORMAL || diff_format == D_IFDEF) {
1.1       jfb      1371:                fetch(ixold, a, b, f1, '<', 1);
1.58      niallo   1372:                if (a <= b && c <= d && diff_format == D_NORMAL)
1.60      joris    1373:                        diff_output("---\n");
1.1       jfb      1374:        }
1.58      niallo   1375:        i = fetch(ixnew, c, d, f2, diff_format == D_NORMAL ? '>' : '\0', 0);
1.1       jfb      1376:        if (inifdef) {
1.58      niallo   1377:                diff_output("#endif /* %s */\n", ifdefname);
1.1       jfb      1378:                inifdef = 0;
                   1379:        }
                   1380: }
                   1381:
                   1382: static int
                   1383: fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile)
                   1384: {
                   1385:        int i, j, c, lastc, col, nc;
                   1386:
                   1387:        /*
                   1388:         * When doing #ifdef's, copy down to current line
                   1389:         * if this is the first file, so that stuff makes it to output.
                   1390:         */
1.58      niallo   1391:        if (diff_format == D_IFDEF && oldfile) {
1.1       jfb      1392:                long curpos = ftell(lb);
                   1393:                /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
                   1394:                nc = f[a > b ? b : a - 1] - curpos;
                   1395:                for (i = 0; i < nc; i++)
1.58      niallo   1396:                        diff_output("%c", getc(lb));
1.1       jfb      1397:        }
                   1398:        if (a > b)
                   1399:                return (0);
1.58      niallo   1400:        if (diff_format == D_IFDEF) {
1.1       jfb      1401:                if (inifdef) {
1.58      niallo   1402:                        diff_output("#else /* %s%s */\n",
1.1       jfb      1403:                            oldfile == 1 ? "!" : "", ifdefname);
                   1404:                } else {
                   1405:                        if (oldfile)
1.58      niallo   1406:                                diff_output("#ifndef %s\n", ifdefname);
1.1       jfb      1407:                        else
1.58      niallo   1408:                                diff_output("#ifdef %s\n", ifdefname);
1.1       jfb      1409:                }
                   1410:                inifdef = 1 + oldfile;
                   1411:        }
                   1412:        for (i = a; i <= b; i++) {
                   1413:                fseek(lb, f[i - 1], SEEK_SET);
                   1414:                nc = f[i] - f[i - 1];
1.58      niallo   1415:                if (diff_format != D_IFDEF && ch != '\0') {
                   1416:                        diff_output("%c", ch);
1.64      xsa      1417:                        if ((Tflag == 1 ) && (diff_format == D_NORMAL ||
1.59      joris    1418:                            diff_format == D_CONTEXT ||
                   1419:                            diff_format == D_UNIFIED))
1.58      niallo   1420:                                diff_output("\t");
                   1421:                        else if (diff_format != D_UNIFIED)
                   1422:                                diff_output(" ");
1.1       jfb      1423:                }
                   1424:                col = 0;
                   1425:                for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
                   1426:                        if ((c = getc(lb)) == EOF) {
1.58      niallo   1427:                                if (diff_format == D_RCSDIFF)
1.76    ! xsa      1428:                                        cvs_log(LP_WARN,
        !          1429:                                            "No newline at end of file");
1.33      jfb      1430:                                else
1.59      joris    1431:                                        diff_output("\n\\ No newline at end of "
                   1432:                                            "file");
1.1       jfb      1433:                                return (0);
                   1434:                        }
1.64      xsa      1435:                        if ((c == '\t') && (tflag == 1)) {
1.1       jfb      1436:                                do {
1.58      niallo   1437:                                        diff_output(" ");
1.1       jfb      1438:                                } while (++col & 7);
                   1439:                        } else {
1.58      niallo   1440:                                diff_output("%c", c);
1.1       jfb      1441:                                col++;
                   1442:                        }
                   1443:                }
                   1444:        }
                   1445:        return (0);
                   1446: }
                   1447:
                   1448: /*
                   1449:  * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
                   1450:  */
                   1451: static int
                   1452: readhash(FILE *f)
                   1453: {
                   1454:        int i, t, space;
                   1455:        int sum;
                   1456:
                   1457:        sum = 1;
                   1458:        space = 0;
1.64      xsa      1459:        if ((bflag != 1) && (wflag != 1)) {
                   1460:                if (iflag == 1)
1.1       jfb      1461:                        for (i = 0; (t = getc(f)) != '\n'; i++) {
                   1462:                                if (t == EOF) {
                   1463:                                        if (i == 0)
                   1464:                                                return (0);
                   1465:                                        break;
                   1466:                                }
                   1467:                                sum = sum * 127 + chrtran[t];
                   1468:                        }
                   1469:                else
                   1470:                        for (i = 0; (t = getc(f)) != '\n'; i++) {
                   1471:                                if (t == EOF) {
                   1472:                                        if (i == 0)
                   1473:                                                return (0);
                   1474:                                        break;
                   1475:                                }
                   1476:                                sum = sum * 127 + t;
                   1477:                        }
                   1478:        } else {
                   1479:                for (i = 0;;) {
                   1480:                        switch (t = getc(f)) {
                   1481:                        case '\t':
                   1482:                        case ' ':
                   1483:                                space++;
                   1484:                                continue;
                   1485:                        default:
1.64      xsa      1486:                                if ((space != 0) && (wflag != 1)) {
1.1       jfb      1487:                                        i++;
                   1488:                                        space = 0;
                   1489:                                }
                   1490:                                sum = sum * 127 + chrtran[t];
                   1491:                                i++;
                   1492:                                continue;
                   1493:                        case EOF:
                   1494:                                if (i == 0)
                   1495:                                        return (0);
                   1496:                                /* FALLTHROUGH */
                   1497:                        case '\n':
                   1498:                                break;
                   1499:                        }
                   1500:                        break;
                   1501:                }
                   1502:        }
                   1503:        /*
                   1504:         * There is a remote possibility that we end up with a zero sum.
                   1505:         * Zero is used as an EOF marker, so return 1 instead.
                   1506:         */
                   1507:        return (sum == 0 ? 1 : sum);
                   1508: }
                   1509:
                   1510: static int
                   1511: asciifile(FILE *f)
                   1512: {
                   1513:        char buf[BUFSIZ];
                   1514:        int i, cnt;
                   1515:
1.64      xsa      1516:        if ((aflag == 1) || (f == NULL))
1.1       jfb      1517:                return (1);
                   1518:
                   1519:        rewind(f);
1.55      xsa      1520:        cnt = fread(buf, (size_t)1, sizeof(buf), f);
1.1       jfb      1521:        for (i = 0; i < cnt; i++)
                   1522:                if (!isprint(buf[i]) && !isspace(buf[i]))
                   1523:                        return (0);
                   1524:        return (1);
                   1525: }
                   1526:
1.17      jfb      1527: static char*
                   1528: match_function(const long *f, int pos, FILE *fp)
                   1529: {
                   1530:        unsigned char buf[FUNCTION_CONTEXT_SIZE];
                   1531:        size_t nc;
                   1532:        int last = lastline;
                   1533:        char *p;
                   1534:
                   1535:        lastline = pos;
                   1536:        while (pos > last) {
                   1537:                fseek(fp, f[pos - 1], SEEK_SET);
                   1538:                nc = f[pos] - f[pos - 1];
                   1539:                if (nc >= sizeof(buf))
                   1540:                        nc = sizeof(buf) - 1;
1.55      xsa      1541:                nc = fread(buf, (size_t)1, nc, fp);
1.17      jfb      1542:                if (nc > 0) {
                   1543:                        buf[nc] = '\0';
1.44      niallo   1544:                        p = strchr((const char *)buf, '\n');
1.17      jfb      1545:                        if (p != NULL)
                   1546:                                *p = '\0';
                   1547:                        if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
1.57      reyk     1548:                                strlcpy(lastbuf, (const char *)buf,
                   1549:                                    sizeof lastbuf);
1.17      jfb      1550:                                lastmatchline = pos;
                   1551:                                return lastbuf;
                   1552:                        }
                   1553:                }
                   1554:                pos--;
                   1555:        }
                   1556:        return (lastmatchline > 0) ? lastbuf : NULL;
                   1557: }
                   1558:
1.1       jfb      1559:
                   1560: /* dump accumulated "context" diff changes */
                   1561: static void
                   1562: dump_context_vec(FILE *f1, FILE *f2)
                   1563: {
                   1564:        struct context_vec *cvp = context_vec_start;
                   1565:        int lowa, upb, lowc, upd, do_output;
                   1566:        int a, b, c, d;
1.17      jfb      1567:        char ch, *f;
1.1       jfb      1568:
                   1569:        if (context_vec_start > context_vec_ptr)
                   1570:                return;
                   1571:
                   1572:        b = d = 0;              /* gcc */
1.4       jfb      1573:        lowa = MAX(1, cvp->a - context);
1.36      jfb      1574:        upb = MIN(diff_len[0], context_vec_ptr->b + context);
1.4       jfb      1575:        lowc = MAX(1, cvp->c - context);
1.36      jfb      1576:        upd = MIN(diff_len[1], context_vec_ptr->d + context);
1.1       jfb      1577:
1.58      niallo   1578:        diff_output("***************");
1.64      xsa      1579:        if (pflag == 1) {
1.17      jfb      1580:                f = match_function(ixold, lowa - 1, f1);
                   1581:                if (f != NULL) {
1.58      niallo   1582:                        diff_output(" ");
                   1583:                        diff_output("%s", f);
1.17      jfb      1584:                }
                   1585:        }
1.58      niallo   1586:        diff_output("\n*** ");
1.1       jfb      1587:        range(lowa, upb, ",");
1.58      niallo   1588:        diff_output(" ****\n");
1.1       jfb      1589:
                   1590:        /*
                   1591:         * Output changes to the "old" file.  The first loop suppresses
                   1592:         * output if there were no changes to the "old" file (we'll see
                   1593:         * the "old" lines as context in the "new" list).
                   1594:         */
                   1595:        do_output = 0;
                   1596:        for (; cvp <= context_vec_ptr; cvp++)
                   1597:                if (cvp->a <= cvp->b) {
                   1598:                        cvp = context_vec_start;
                   1599:                        do_output++;
                   1600:                        break;
                   1601:                }
1.65      xsa      1602:        if (do_output != 0) {
1.1       jfb      1603:                while (cvp <= context_vec_ptr) {
                   1604:                        a = cvp->a;
                   1605:                        b = cvp->b;
                   1606:                        c = cvp->c;
                   1607:                        d = cvp->d;
                   1608:
                   1609:                        if (a <= b && c <= d)
                   1610:                                ch = 'c';
                   1611:                        else
                   1612:                                ch = (a <= b) ? 'd' : 'a';
                   1613:
                   1614:                        if (ch == 'a')
                   1615:                                fetch(ixold, lowa, b, f1, ' ', 0);
                   1616:                        else {
                   1617:                                fetch(ixold, lowa, a - 1, f1, ' ', 0);
                   1618:                                fetch(ixold, a, b, f1,
                   1619:                                    ch == 'c' ? '!' : '-', 0);
                   1620:                        }
                   1621:                        lowa = b + 1;
                   1622:                        cvp++;
                   1623:                }
                   1624:                fetch(ixold, b + 1, upb, f1, ' ', 0);
                   1625:        }
                   1626:        /* output changes to the "new" file */
1.58      niallo   1627:        diff_output("--- ");
1.1       jfb      1628:        range(lowc, upd, ",");
1.58      niallo   1629:        diff_output(" ----\n");
1.1       jfb      1630:
                   1631:        do_output = 0;
                   1632:        for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
                   1633:                if (cvp->c <= cvp->d) {
                   1634:                        cvp = context_vec_start;
                   1635:                        do_output++;
                   1636:                        break;
                   1637:                }
1.65      xsa      1638:        if (do_output != 0) {
1.1       jfb      1639:                while (cvp <= context_vec_ptr) {
                   1640:                        a = cvp->a;
                   1641:                        b = cvp->b;
                   1642:                        c = cvp->c;
                   1643:                        d = cvp->d;
                   1644:
                   1645:                        if (a <= b && c <= d)
                   1646:                                ch = 'c';
                   1647:                        else
                   1648:                                ch = (a <= b) ? 'd' : 'a';
                   1649:
                   1650:                        if (ch == 'd')
                   1651:                                fetch(ixnew, lowc, d, f2, ' ', 0);
                   1652:                        else {
                   1653:                                fetch(ixnew, lowc, c - 1, f2, ' ', 0);
                   1654:                                fetch(ixnew, c, d, f2,
                   1655:                                    ch == 'c' ? '!' : '+', 0);
                   1656:                        }
                   1657:                        lowc = d + 1;
                   1658:                        cvp++;
                   1659:                }
                   1660:                fetch(ixnew, d + 1, upd, f2, ' ', 0);
                   1661:        }
                   1662:        context_vec_ptr = context_vec_start - 1;
                   1663: }
                   1664:
                   1665: /* dump accumulated "unified" diff changes */
                   1666: static void
                   1667: dump_unified_vec(FILE *f1, FILE *f2)
                   1668: {
                   1669:        struct context_vec *cvp = context_vec_start;
                   1670:        int lowa, upb, lowc, upd;
                   1671:        int a, b, c, d;
1.17      jfb      1672:        char ch, *f;
1.1       jfb      1673:
                   1674:        if (context_vec_start > context_vec_ptr)
                   1675:                return;
                   1676:
                   1677:        b = d = 0;              /* gcc */
1.4       jfb      1678:        lowa = MAX(1, cvp->a - context);
1.36      jfb      1679:        upb = MIN(diff_len[0], context_vec_ptr->b + context);
1.4       jfb      1680:        lowc = MAX(1, cvp->c - context);
1.36      jfb      1681:        upd = MIN(diff_len[1], context_vec_ptr->d + context);
1.1       jfb      1682:
1.58      niallo   1683:        diff_output("@@ -");
1.1       jfb      1684:        uni_range(lowa, upb);
1.58      niallo   1685:        diff_output(" +");
1.1       jfb      1686:        uni_range(lowc, upd);
1.58      niallo   1687:        diff_output(" @@");
1.64      xsa      1688:        if (pflag == 1) {
1.17      jfb      1689:                f = match_function(ixold, lowa - 1, f1);
                   1690:                if (f != NULL) {
1.58      niallo   1691:                        diff_output(" ");
                   1692:                        diff_output("%s", f);
1.17      jfb      1693:                }
                   1694:        }
1.58      niallo   1695:        diff_output("\n");
1.1       jfb      1696:
                   1697:        /*
                   1698:         * Output changes in "unified" diff format--the old and new lines
                   1699:         * are printed together.
                   1700:         */
                   1701:        for (; cvp <= context_vec_ptr; cvp++) {
                   1702:                a = cvp->a;
                   1703:                b = cvp->b;
                   1704:                c = cvp->c;
                   1705:                d = cvp->d;
                   1706:
                   1707:                /*
                   1708:                 * c: both new and old changes
                   1709:                 * d: only changes in the old file
                   1710:                 * a: only changes in the new file
                   1711:                 */
                   1712:                if (a <= b && c <= d)
                   1713:                        ch = 'c';
                   1714:                else
                   1715:                        ch = (a <= b) ? 'd' : 'a';
                   1716:
                   1717:                switch (ch) {
                   1718:                case 'c':
                   1719:                        fetch(ixold, lowa, a - 1, f1, ' ', 0);
                   1720:                        fetch(ixold, a, b, f1, '-', 0);
                   1721:                        fetch(ixnew, c, d, f2, '+', 0);
                   1722:                        break;
                   1723:                case 'd':
                   1724:                        fetch(ixold, lowa, a - 1, f1, ' ', 0);
                   1725:                        fetch(ixold, a, b, f1, '-', 0);
                   1726:                        break;
                   1727:                case 'a':
                   1728:                        fetch(ixnew, lowc, c - 1, f2, ' ', 0);
                   1729:                        fetch(ixnew, c, d, f2, '+', 0);
                   1730:                        break;
                   1731:                }
                   1732:                lowa = b + 1;
                   1733:                lowc = d + 1;
                   1734:        }
                   1735:        fetch(ixnew, d + 1, upd, f2, ' ', 0);
                   1736:
                   1737:        context_vec_ptr = context_vec_start - 1;
1.58      niallo   1738: }
                   1739:
1.63      joris    1740: void
1.58      niallo   1741: diff_output(const char *fmt, ...)
                   1742: {
                   1743:        va_list vap;
                   1744:        char *str;
                   1745:
                   1746:        va_start(vap, fmt);
                   1747:        vasprintf(&str, fmt, vap);
                   1748:        if (diffbuf != NULL)
                   1749:                cvs_buf_append(diffbuf, str, strlen(str));
                   1750:        else
                   1751:                cvs_printf("%s", str);
1.71      joris    1752:        xfree(str);
1.58      niallo   1753:        va_end(vap);
1.1       jfb      1754: }