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

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