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