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

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