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

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