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