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