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

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