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

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