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

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