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

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