Annotation of src/usr.bin/cvs/diff.c, Revision 1.19
1.19 ! jfb 1: /* $OpenBSD: diff.c,v 1.18 2005/01/14 00:35:15 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 *);
225: static void prepare(int, FILE *, off_t);
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 {
581: r1 = rcsnum_alloc();
1.4 jfb 582: rcsnum_aton(dap->rev1, NULL, r1);
1.1 jfb 583: }
584:
1.4 jfb 585: cvs_printf("retrieving revision %s\n",
1.1 jfb 586: rcsnum_tostr(r1, buf, sizeof(buf)));
587: b1 = rcs_getrev(rf, r1);
588:
1.4 jfb 589: if (dap->rev2 != NULL) {
590: cvs_printf("retrieving revision %s\n", dap->rev2);
1.1 jfb 591: r2 = rcsnum_alloc();
1.4 jfb 592: rcsnum_aton(dap->rev2, NULL, r2);
1.1 jfb 593: b2 = rcs_getrev(rf, r2);
1.10 deraadt 594: } else {
1.4 jfb 595: b2 = cvs_buf_load(diff_file, BUF_AUTOEXT);
1.1 jfb 596: }
597:
1.4 jfb 598: rcs_close(rf);
599:
1.2 jfb 600: printf("%s", diffargs);
601: printf(" -r%s", buf);
1.4 jfb 602: if (dap->rev2 != NULL)
603: printf(" -r%s", dap->rev2);
604: printf(" %s\n", diff_file);
1.12 djm 605: strlcpy(path_tmp1, "/tmp/diff1.XXXXXXXXXX", sizeof(path_tmp1));
606: if (cvs_buf_write_stmp(b1, path_tmp1, 0600) == -1)
607: return (-1);
1.19 ! jfb 608: strlcpy(path_tmp2, "/tmp/diff2.XXXXXXXXXX", sizeof(path_tmp2));
1.12 djm 609: if (cvs_buf_write_stmp(b2, path_tmp2, 0600) == -1) {
610: (void)unlink(path_tmp1);
611: return (-1);
612: }
613: cvs_diffreg(path_tmp1, path_tmp2);
614: (void)unlink(path_tmp1);
615: (void)unlink(path_tmp2);
1.1 jfb 616: }
617:
1.4 jfb 618: cvs_ent_free(entp);
1.1 jfb 619: return (0);
620: }
621:
622:
623: int
624: cvs_diffreg(const char *file1, const char *file2)
625: {
626: FILE *f1, *f2;
627: int i, rval;
628:
629: f1 = f2 = NULL;
630: rval = D_SAME;
631: anychange = 0;
632: lastline = 0;
633: lastmatchline = 0;
634: context_vec_ptr = context_vec_start - 1;
635: chrtran = (iflag ? cup2low : clow2low);
636:
637: f1 = fopen(file1, "r");
638: if (f1 == NULL) {
639: cvs_log(LP_ERRNO, "%s", file1);
640: status |= 2;
641: goto closem;
642: }
643:
644: f2 = fopen(file2, "r");
645: if (f2 == NULL) {
646: cvs_log(LP_ERRNO, "%s", file2);
647: status |= 2;
648: goto closem;
649: }
650:
651: switch (files_differ(f1, f2)) {
652: case 0:
653: goto closem;
654: case 1:
655: break;
656: default:
657: /* error */
658: status |= 2;
659: goto closem;
660: }
661:
662: if (!asciifile(f1) || !asciifile(f2)) {
663: rval = D_BINARY;
664: status |= 1;
665: goto closem;
666: }
667: prepare(0, f1, stb1.st_size);
668: prepare(1, f2, stb2.st_size);
669: prune();
670: sort(sfile[0], slen[0]);
671: sort(sfile[1], slen[1]);
672:
673: member = (int *)file[1];
674: equiv(sfile[0], slen[0], sfile[1], slen[1], member);
675: member = realloc(member, (slen[1] + 2) * sizeof(int));
676:
677: class = (int *)file[0];
678: unsort(sfile[0], slen[0], class);
679: class = realloc(class, (slen[0] + 2) * sizeof(int));
680:
681: klist = malloc((slen[0] + 2) * sizeof(int));
682: clen = 0;
683: clistlen = 100;
684: clist = malloc(clistlen * sizeof(cand));
685: i = stone(class, slen[0], member, klist);
686: free(member);
687: free(class);
688:
689: J = realloc(J, (len[0] + 2) * sizeof(int));
690: unravel(klist[i]);
691: free(clist);
692: free(klist);
693:
694: ixold = realloc(ixold, (len[0] + 2) * sizeof(long));
695: ixnew = realloc(ixnew, (len[1] + 2) * sizeof(long));
696: check(f1, f2);
697: output(file1, f1, file2, f2);
698:
699: closem:
700: if (anychange) {
701: status |= 1;
702: if (rval == D_SAME)
703: rval = D_DIFFER;
704: }
705: if (f1 != NULL)
706: fclose(f1);
707: if (f2 != NULL)
708: fclose(f2);
709: return (rval);
710: }
711:
712: /*
713: * Check to see if the given files differ.
714: * Returns 0 if they are the same, 1 if different, and -1 on error.
715: * XXX - could use code from cmp(1) [faster]
716: */
717: static int
718: files_differ(FILE *f1, FILE *f2)
719: {
720: char buf1[BUFSIZ], buf2[BUFSIZ];
721: size_t i, j;
722:
723: if (stb1.st_size != stb2.st_size)
724: return (1);
725: for (;;) {
726: i = fread(buf1, 1, sizeof(buf1), f1);
727: j = fread(buf2, 1, sizeof(buf2), f2);
728: if (i != j)
729: return (1);
730: if (i == 0 && j == 0) {
731: if (ferror(f1) || ferror(f2))
732: return (1);
733: return (0);
734: }
735: if (memcmp(buf1, buf2, i) != 0)
736: return (1);
737: }
738: }
739:
740:
741: char *
1.15 jfb 742: splice(char *dir, char *filename)
1.1 jfb 743: {
744: char *tail, *buf;
745:
1.15 jfb 746: if ((tail = strrchr(filename, '/')) == NULL)
747: tail = filename;
1.1 jfb 748: else
749: tail++;
750: asprintf(&buf, "%s/%s", dir, tail);
751: return (buf);
752: }
753:
754: static void
755: prepare(int i, FILE *fd, off_t filesize)
756: {
757: struct line *p;
758: int j, h;
759: size_t sz;
760:
761: rewind(fd);
762:
1.16 xsa 763: sz = ((size_t)filesize <= SIZE_MAX ? (size_t)filesize : SIZE_MAX) / 25;
1.1 jfb 764: if (sz < 100)
765: sz = 100;
766:
767: p = malloc((sz + 3) * sizeof(struct line));
768: for (j = 0; (h = readhash(fd));) {
769: if (j == (int)sz) {
770: sz = sz * 3 / 2;
771: p = realloc(p, (sz + 3) * sizeof(struct line));
772: }
773: p[++j].value = h;
774: }
775: len[i] = j;
776: file[i] = p;
777: }
778:
779: static void
780: prune(void)
781: {
782: int i, j;
783:
784: for (pref = 0; pref < len[0] && pref < len[1] &&
785: file[0][pref + 1].value == file[1][pref + 1].value;
786: pref++)
787: ;
788: for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
789: file[0][len[0] - suff].value == file[1][len[1] - suff].value;
790: suff++)
791: ;
792: for (j = 0; j < 2; j++) {
793: sfile[j] = file[j] + pref;
794: slen[j] = len[j] - pref - suff;
795: for (i = 0; i <= slen[j]; i++)
796: sfile[j][i].serial = i;
797: }
798: }
799:
800: static void
801: equiv(struct line *a, int n, struct line *b, int m, int *c)
802: {
803: int i, j;
804:
805: i = j = 1;
806: while (i <= n && j <= m) {
807: if (a[i].value < b[j].value)
808: a[i++].value = 0;
809: else if (a[i].value == b[j].value)
810: a[i++].value = j;
811: else
812: j++;
813: }
814: while (i <= n)
815: a[i++].value = 0;
816: b[m + 1].value = 0;
817: j = 0;
818: while (++j <= m) {
819: c[j] = -b[j].serial;
820: while (b[j + 1].value == b[j].value) {
821: j++;
822: c[j] = b[j].serial;
823: }
824: }
825: c[j] = -1;
826: }
827:
828: /* Code taken from ping.c */
829: static int
830: isqrt(int n)
831: {
832: int y, x = 1;
833:
834: if (n == 0)
1.18 jfb 835: return (0);
1.1 jfb 836:
837: do { /* newton was a stinker */
838: y = x;
839: x = n / x;
840: x += y;
841: x /= 2;
842: } while ((x - y) > 1 || (x - y) < -1);
843:
844: return (x);
845: }
846:
847: static int
848: stone(int *a, int n, int *b, int *c)
849: {
850: int i, k, y, j, l;
851: int oldc, tc, oldl;
852: u_int numtries;
853:
1.18 jfb 854: /* XXX move the isqrt() out of the macro to avoid multiple calls */
855: const u_int bound = dflag ? UINT_MAX : MAX(256, (u_int)isqrt(n));
1.1 jfb 856:
857: k = 0;
858: c[0] = newcand(0, 0, 0);
859: for (i = 1; i <= n; i++) {
860: j = a[i];
861: if (j == 0)
862: continue;
863: y = -b[j];
864: oldl = 0;
865: oldc = c[0];
866: numtries = 0;
867: do {
868: if (y <= clist[oldc].y)
869: continue;
870: l = search(c, k, y);
871: if (l != oldl + 1)
872: oldc = c[l - 1];
873: if (l <= k) {
874: if (clist[c[l]].y <= y)
875: continue;
876: tc = c[l];
877: c[l] = newcand(i, y, oldc);
878: oldc = tc;
879: oldl = l;
880: numtries++;
881: } else {
882: c[l] = newcand(i, y, oldc);
883: k++;
884: break;
885: }
886: } while ((y = b[++j]) > 0 && numtries < bound);
887: }
888: return (k);
889: }
890:
891: static int
892: newcand(int x, int y, int pred)
893: {
894: struct cand *q;
895:
896: if (clen == clistlen) {
897: clistlen = clistlen * 11 / 10;
898: clist = realloc(clist, clistlen * sizeof(cand));
899: }
900: q = clist + clen;
901: q->x = x;
902: q->y = y;
903: q->pred = pred;
904: return (clen++);
905: }
906:
907: static int
908: search(int *c, int k, int y)
909: {
910: int i, j, l, t;
911:
912: if (clist[c[k]].y < y) /* quick look for typical case */
913: return (k + 1);
914: i = 0;
915: j = k + 1;
916: while (1) {
917: l = i + j;
918: if ((l >>= 1) <= i)
919: break;
920: t = clist[c[l]].y;
921: if (t > y)
922: j = l;
923: else if (t < y)
924: i = l;
925: else
926: return (l);
927: }
928: return (l + 1);
929: }
930:
931: static void
932: unravel(int p)
933: {
934: struct cand *q;
935: int i;
936:
937: for (i = 0; i <= len[0]; i++)
938: J[i] = i <= pref ? i :
939: i > len[0] - suff ? i + len[1] - len[0] : 0;
940: for (q = clist + p; q->y != 0; q = clist + q->pred)
941: J[q->x + pref] = q->y + pref;
942: }
943:
944: /*
945: * Check does double duty:
946: * 1. ferret out any fortuitous correspondences due
947: * to confounding by hashing (which result in "jackpot")
948: * 2. collect random access indexes to the two files
949: */
950: static void
951: check(FILE *f1, FILE *f2)
952: {
953: int i, j, jackpot, c, d;
954: long ctold, ctnew;
955:
956: rewind(f1);
957: rewind(f2);
958: j = 1;
959: ixold[0] = ixnew[0] = 0;
960: jackpot = 0;
961: ctold = ctnew = 0;
962: for (i = 1; i <= len[0]; i++) {
963: if (J[i] == 0) {
964: ixold[i] = ctold += skipline(f1);
965: continue;
966: }
967: while (j < J[i]) {
968: ixnew[j] = ctnew += skipline(f2);
969: j++;
970: }
971: if (bflag || wflag || iflag) {
972: for (;;) {
973: c = getc(f1);
974: d = getc(f2);
975: /*
976: * GNU diff ignores a missing newline
977: * in one file if bflag || wflag.
978: */
979: if ((bflag || wflag) &&
980: ((c == EOF && d == '\n') ||
981: (c == '\n' && d == EOF))) {
982: break;
983: }
984: ctold++;
985: ctnew++;
986: if (bflag && isspace(c) && isspace(d)) {
987: do {
988: if (c == '\n')
989: break;
990: ctold++;
991: } while (isspace(c = getc(f1)));
992: do {
993: if (d == '\n')
994: break;
995: ctnew++;
996: } while (isspace(d = getc(f2)));
997: } else if (wflag) {
998: while (isspace(c) && c != '\n') {
999: c = getc(f1);
1000: ctold++;
1001: }
1002: while (isspace(d) && d != '\n') {
1003: d = getc(f2);
1004: ctnew++;
1005: }
1006: }
1007: if (chrtran[c] != chrtran[d]) {
1008: jackpot++;
1009: J[i] = 0;
1010: if (c != '\n' && c != EOF)
1011: ctold += skipline(f1);
1012: if (d != '\n' && c != EOF)
1013: ctnew += skipline(f2);
1014: break;
1015: }
1016: if (c == '\n' || c == EOF)
1017: break;
1018: }
1019: } else {
1020: for (;;) {
1021: ctold++;
1022: ctnew++;
1023: if ((c = getc(f1)) != (d = getc(f2))) {
1024: /* jackpot++; */
1025: J[i] = 0;
1026: if (c != '\n' && c != EOF)
1027: ctold += skipline(f1);
1028: if (d != '\n' && c != EOF)
1029: ctnew += skipline(f2);
1030: break;
1031: }
1032: if (c == '\n' || c == EOF)
1033: break;
1034: }
1035: }
1036: ixold[i] = ctold;
1037: ixnew[j] = ctnew;
1038: j++;
1039: }
1040: for (; j <= len[1]; j++)
1041: ixnew[j] = ctnew += skipline(f2);
1042: /*
1043: * if (jackpot)
1044: * fprintf(stderr, "jackpot\n");
1045: */
1046: }
1047:
1048: /* shellsort CACM #201 */
1049: static void
1050: sort(struct line *a, int n)
1051: {
1052: struct line *ai, *aim, w;
1053: int j, m = 0, k;
1054:
1055: if (n == 0)
1056: return;
1057: for (j = 1; j <= n; j *= 2)
1058: m = 2 * j - 1;
1059: for (m /= 2; m != 0; m /= 2) {
1060: k = n - m;
1061: for (j = 1; j <= k; j++) {
1062: for (ai = &a[j]; ai > a; ai -= m) {
1063: aim = &ai[m];
1064: if (aim < ai)
1065: break; /* wraparound */
1066: if (aim->value > ai[0].value ||
1067: (aim->value == ai[0].value &&
1068: aim->serial > ai[0].serial))
1069: break;
1070: w.value = ai[0].value;
1071: ai[0].value = aim->value;
1072: aim->value = w.value;
1073: w.serial = ai[0].serial;
1074: ai[0].serial = aim->serial;
1075: aim->serial = w.serial;
1076: }
1077: }
1078: }
1079: }
1080:
1081: static void
1082: unsort(struct line *f, int l, int *b)
1083: {
1084: int *a, i;
1085:
1086: a = malloc((l + 1) * sizeof(int));
1087: for (i = 1; i <= l; i++)
1088: a[f[i].serial] = f[i].value;
1089: for (i = 1; i <= l; i++)
1090: b[i] = a[i];
1091: free(a);
1092: }
1093:
1094: static int
1095: skipline(FILE *f)
1096: {
1097: int i, c;
1098:
1099: for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
1100: continue;
1101: return (i);
1102: }
1103:
1104: static void
1105: output(const char *file1, FILE *f1, const char *file2, FILE *f2)
1106: {
1107: int m, i0, i1, j0, j1;
1108:
1109: rewind(f1);
1110: rewind(f2);
1111: m = len[0];
1112: J[0] = 0;
1113: J[m + 1] = len[1] + 1;
1114: for (i0 = 1; i0 <= m; i0 = i1 + 1) {
1115: while (i0 <= m && J[i0] == J[i0 - 1] + 1)
1116: i0++;
1117: j0 = J[i0 - 1] + 1;
1118: i1 = i0 - 1;
1119: while (i1 < m && J[i1 + 1] == 0)
1120: i1++;
1121: j1 = J[i1 + 1] - 1;
1122: J[i1] = j1;
1123: change(file1, f1, file2, f2, i0, i1, j0, j1);
1124: }
1125: if (m == 0)
1126: change(file1, f1, file2, f2, 1, 0, 1, len[1]);
1127: if (format == D_IFDEF) {
1128: for (;;) {
1129: #define c i0
1130: if ((c = getc(f1)) == EOF)
1131: return;
1132: putchar(c);
1133: }
1134: #undef c
1135: }
1136: if (anychange != 0) {
1137: if (format == D_CONTEXT)
1138: dump_context_vec(f1, f2);
1139: else if (format == D_UNIFIED)
1140: dump_unified_vec(f1, f2);
1141: }
1142: }
1143:
1144: static __inline void
1145: range(int a, int b, char *separator)
1146: {
1147: printf("%d", a > b ? b : a);
1148: if (a < b)
1149: printf("%s%d", separator, b);
1150: }
1151:
1152: static __inline void
1153: uni_range(int a, int b)
1154: {
1155: if (a < b)
1156: printf("%d,%d", a, b - a + 1);
1157: else if (a == b)
1158: printf("%d", b);
1159: else
1160: printf("%d,0", b);
1161: }
1162:
1163: static char *
1.15 jfb 1164: preadline(int fd, size_t rlen, off_t off)
1.1 jfb 1165: {
1166: char *line;
1167: ssize_t nr;
1168:
1.15 jfb 1169: line = malloc(rlen + 1);
1.1 jfb 1170: if (line == NULL) {
1171: cvs_log(LP_ERRNO, "failed to allocate line");
1172: return (NULL);
1173: }
1.15 jfb 1174: if ((nr = pread(fd, line, rlen, off)) < 0) {
1.1 jfb 1175: cvs_log(LP_ERRNO, "preadline failed");
1176: return (NULL);
1177: }
1178: line[nr] = '\0';
1179: return (line);
1180: }
1181:
1182: static int
1183: ignoreline(char *line)
1184: {
1185: int ret;
1186:
1187: ret = regexec(&ignore_re, line, 0, NULL, 0);
1188: free(line);
1189: return (ret == 0); /* if it matched, it should be ignored. */
1190: }
1191:
1192: /*
1193: * Indicate that there is a difference between lines a and b of the from file
1194: * to get to lines c to d of the to file. If a is greater then b then there
1195: * are no lines in the from file involved and this means that there were
1196: * lines appended (beginning at b). If c is greater than d then there are
1197: * lines missing from the to file.
1198: */
1199: static void
1200: change(const char *file1, FILE *f1, const char *file2, FILE *f2,
1201: int a, int b, int c, int d)
1202: {
1203: static size_t max_context = 64;
1204: int i;
1205:
1206: if (format != D_IFDEF && a > b && c > d)
1207: return;
1208: if (ignore_pats != NULL) {
1209: char *line;
1210: /*
1211: * All lines in the change, insert, or delete must
1212: * match an ignore pattern for the change to be
1213: * ignored.
1214: */
1215: if (a <= b) { /* Changes and deletes. */
1216: for (i = a; i <= b; i++) {
1217: line = preadline(fileno(f1),
1218: ixold[i] - ixold[i - 1], ixold[i - 1]);
1219: if (!ignoreline(line))
1220: goto proceed;
1221: }
1222: }
1223: if (a > b || c <= d) { /* Changes and inserts. */
1224: for (i = c; i <= d; i++) {
1225: line = preadline(fileno(f2),
1226: ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
1227: if (!ignoreline(line))
1228: goto proceed;
1229: }
1230: }
1231: return;
1232: }
1233: proceed:
1234: if (format == D_CONTEXT || format == D_UNIFIED) {
1235: /*
1236: * Allocate change records as needed.
1237: */
1238: if (context_vec_ptr == context_vec_end - 1) {
1239: ptrdiff_t offset = context_vec_ptr - context_vec_start;
1240: max_context <<= 1;
1241: context_vec_start = realloc(context_vec_start,
1242: max_context * sizeof(struct context_vec));
1243: context_vec_end = context_vec_start + max_context;
1244: context_vec_ptr = context_vec_start + offset;
1245: }
1246: if (anychange == 0) {
1247: /*
1248: * Print the context/unidiff header first time through.
1249: */
1250: printf("%s %s %s",
1251: format == D_CONTEXT ? "***" : "---", diff_file,
1252: ctime(&stb1.st_mtime));
1253: printf("%s %s %s",
1254: format == D_CONTEXT ? "---" : "+++", diff_file,
1255: ctime(&stb2.st_mtime));
1256: anychange = 1;
1257: } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
1258: c > context_vec_ptr->d + (2 * context) + 1) {
1259: /*
1260: * If this change is more than 'context' lines from the
1261: * previous change, dump the record and reset it.
1262: */
1263: if (format == D_CONTEXT)
1264: dump_context_vec(f1, f2);
1265: else
1266: dump_unified_vec(f1, f2);
1267: }
1268: context_vec_ptr++;
1269: context_vec_ptr->a = a;
1270: context_vec_ptr->b = b;
1271: context_vec_ptr->c = c;
1272: context_vec_ptr->d = d;
1273: return;
1274: }
1275: if (anychange == 0)
1276: anychange = 1;
1277: switch (format) {
1278: case D_BRIEF:
1279: return;
1280: case D_NORMAL:
1281: range(a, b, ",");
1282: putchar(a > b ? 'a' : c > d ? 'd' : 'c');
1283: if (format == D_NORMAL)
1284: range(c, d, ",");
1285: putchar('\n');
1286: break;
1287: }
1288: if (format == D_NORMAL || format == D_IFDEF) {
1289: fetch(ixold, a, b, f1, '<', 1);
1290: if (a <= b && c <= d && format == D_NORMAL)
1291: puts("---");
1292: }
1293: i = fetch(ixnew, c, d, f2, format == D_NORMAL ? '>' : '\0', 0);
1294: if (inifdef) {
1295: printf("#endif /* %s */\n", ifdefname);
1296: inifdef = 0;
1297: }
1298: }
1299:
1300: static int
1301: fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile)
1302: {
1303: int i, j, c, lastc, col, nc;
1304:
1305: /*
1306: * When doing #ifdef's, copy down to current line
1307: * if this is the first file, so that stuff makes it to output.
1308: */
1309: if (format == D_IFDEF && oldfile) {
1310: long curpos = ftell(lb);
1311: /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1312: nc = f[a > b ? b : a - 1] - curpos;
1313: for (i = 0; i < nc; i++)
1314: putchar(getc(lb));
1315: }
1316: if (a > b)
1317: return (0);
1318: if (format == D_IFDEF) {
1319: if (inifdef) {
1320: printf("#else /* %s%s */\n",
1321: oldfile == 1 ? "!" : "", ifdefname);
1322: } else {
1323: if (oldfile)
1324: printf("#ifndef %s\n", ifdefname);
1325: else
1326: printf("#ifdef %s\n", ifdefname);
1327: }
1328: inifdef = 1 + oldfile;
1329: }
1330: for (i = a; i <= b; i++) {
1331: fseek(lb, f[i - 1], SEEK_SET);
1332: nc = f[i] - f[i - 1];
1333: if (format != D_IFDEF && ch != '\0') {
1334: putchar(ch);
1335: if (Tflag && (format == D_NORMAL || format == D_CONTEXT
1336: || format == D_UNIFIED))
1337: putchar('\t');
1338: else if (format != D_UNIFIED)
1339: putchar(' ');
1340: }
1341: col = 0;
1342: for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
1343: if ((c = getc(lb)) == EOF) {
1344: puts("\n\\ No newline at end of file");
1345: return (0);
1346: }
1347: if (c == '\t' && tflag) {
1348: do {
1349: putchar(' ');
1350: } while (++col & 7);
1351: } else {
1352: putchar(c);
1353: col++;
1354: }
1355: }
1356: }
1357: return (0);
1358: }
1359:
1360: /*
1361: * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
1362: */
1363: static int
1364: readhash(FILE *f)
1365: {
1366: int i, t, space;
1367: int sum;
1368:
1369: sum = 1;
1370: space = 0;
1371: if (!bflag && !wflag) {
1372: if (iflag)
1373: for (i = 0; (t = getc(f)) != '\n'; i++) {
1374: if (t == EOF) {
1375: if (i == 0)
1376: return (0);
1377: break;
1378: }
1379: sum = sum * 127 + chrtran[t];
1380: }
1381: else
1382: for (i = 0; (t = getc(f)) != '\n'; i++) {
1383: if (t == EOF) {
1384: if (i == 0)
1385: return (0);
1386: break;
1387: }
1388: sum = sum * 127 + t;
1389: }
1390: } else {
1391: for (i = 0;;) {
1392: switch (t = getc(f)) {
1393: case '\t':
1394: case ' ':
1395: space++;
1396: continue;
1397: default:
1398: if (space && !wflag) {
1399: i++;
1400: space = 0;
1401: }
1402: sum = sum * 127 + chrtran[t];
1403: i++;
1404: continue;
1405: case EOF:
1406: if (i == 0)
1407: return (0);
1408: /* FALLTHROUGH */
1409: case '\n':
1410: break;
1411: }
1412: break;
1413: }
1414: }
1415: /*
1416: * There is a remote possibility that we end up with a zero sum.
1417: * Zero is used as an EOF marker, so return 1 instead.
1418: */
1419: return (sum == 0 ? 1 : sum);
1420: }
1421:
1422: static int
1423: asciifile(FILE *f)
1424: {
1425: char buf[BUFSIZ];
1426: int i, cnt;
1427:
1428: if (aflag || f == NULL)
1429: return (1);
1430:
1431: rewind(f);
1432: cnt = fread(buf, 1, sizeof(buf), f);
1433: for (i = 0; i < cnt; i++)
1434: if (!isprint(buf[i]) && !isspace(buf[i]))
1435: return (0);
1436: return (1);
1437: }
1438:
1.17 jfb 1439: static char*
1440: match_function(const long *f, int pos, FILE *fp)
1441: {
1442: unsigned char buf[FUNCTION_CONTEXT_SIZE];
1443: size_t nc;
1444: int last = lastline;
1445: char *p;
1446:
1447: lastline = pos;
1448: while (pos > last) {
1449: fseek(fp, f[pos - 1], SEEK_SET);
1450: nc = f[pos] - f[pos - 1];
1451: if (nc >= sizeof(buf))
1452: nc = sizeof(buf) - 1;
1453: nc = fread(buf, 1, nc, fp);
1454: if (nc > 0) {
1455: buf[nc] = '\0';
1456: p = strchr(buf, '\n');
1457: if (p != NULL)
1458: *p = '\0';
1459: if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
1460: strlcpy(lastbuf, buf, sizeof lastbuf);
1461: lastmatchline = pos;
1462: return lastbuf;
1463: }
1464: }
1465: pos--;
1466: }
1467: return (lastmatchline > 0) ? lastbuf : NULL;
1468: }
1469:
1.1 jfb 1470:
1471: /* dump accumulated "context" diff changes */
1472: static void
1473: dump_context_vec(FILE *f1, FILE *f2)
1474: {
1475: struct context_vec *cvp = context_vec_start;
1476: int lowa, upb, lowc, upd, do_output;
1477: int a, b, c, d;
1.17 jfb 1478: char ch, *f;
1.1 jfb 1479:
1480: if (context_vec_start > context_vec_ptr)
1481: return;
1482:
1483: b = d = 0; /* gcc */
1.4 jfb 1484: lowa = MAX(1, cvp->a - context);
1485: upb = MIN(len[0], context_vec_ptr->b + context);
1486: lowc = MAX(1, cvp->c - context);
1487: upd = MIN(len[1], context_vec_ptr->d + context);
1.1 jfb 1488:
1489: printf("***************");
1.17 jfb 1490: if (pflag) {
1491: f = match_function(ixold, lowa - 1, f1);
1492: if (f != NULL) {
1493: putchar(' ');
1494: fputs(f, stdout);
1495: }
1496: }
1.1 jfb 1497: printf("\n*** ");
1498: range(lowa, upb, ",");
1499: printf(" ****\n");
1500:
1501: /*
1502: * Output changes to the "old" file. The first loop suppresses
1503: * output if there were no changes to the "old" file (we'll see
1504: * the "old" lines as context in the "new" list).
1505: */
1506: do_output = 0;
1507: for (; cvp <= context_vec_ptr; cvp++)
1508: if (cvp->a <= cvp->b) {
1509: cvp = context_vec_start;
1510: do_output++;
1511: break;
1512: }
1513: if (do_output) {
1514: while (cvp <= context_vec_ptr) {
1515: a = cvp->a;
1516: b = cvp->b;
1517: c = cvp->c;
1518: d = cvp->d;
1519:
1520: if (a <= b && c <= d)
1521: ch = 'c';
1522: else
1523: ch = (a <= b) ? 'd' : 'a';
1524:
1525: if (ch == 'a')
1526: fetch(ixold, lowa, b, f1, ' ', 0);
1527: else {
1528: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1529: fetch(ixold, a, b, f1,
1530: ch == 'c' ? '!' : '-', 0);
1531: }
1532: lowa = b + 1;
1533: cvp++;
1534: }
1535: fetch(ixold, b + 1, upb, f1, ' ', 0);
1536: }
1537: /* output changes to the "new" file */
1538: printf("--- ");
1539: range(lowc, upd, ",");
1540: printf(" ----\n");
1541:
1542: do_output = 0;
1543: for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
1544: if (cvp->c <= cvp->d) {
1545: cvp = context_vec_start;
1546: do_output++;
1547: break;
1548: }
1549: if (do_output) {
1550: while (cvp <= context_vec_ptr) {
1551: a = cvp->a;
1552: b = cvp->b;
1553: c = cvp->c;
1554: d = cvp->d;
1555:
1556: if (a <= b && c <= d)
1557: ch = 'c';
1558: else
1559: ch = (a <= b) ? 'd' : 'a';
1560:
1561: if (ch == 'd')
1562: fetch(ixnew, lowc, d, f2, ' ', 0);
1563: else {
1564: fetch(ixnew, lowc, c - 1, f2, ' ', 0);
1565: fetch(ixnew, c, d, f2,
1566: ch == 'c' ? '!' : '+', 0);
1567: }
1568: lowc = d + 1;
1569: cvp++;
1570: }
1571: fetch(ixnew, d + 1, upd, f2, ' ', 0);
1572: }
1573: context_vec_ptr = context_vec_start - 1;
1574: }
1575:
1576: /* dump accumulated "unified" diff changes */
1577: static void
1578: dump_unified_vec(FILE *f1, FILE *f2)
1579: {
1580: struct context_vec *cvp = context_vec_start;
1581: int lowa, upb, lowc, upd;
1582: int a, b, c, d;
1.17 jfb 1583: char ch, *f;
1.1 jfb 1584:
1585: if (context_vec_start > context_vec_ptr)
1586: return;
1587:
1588: b = d = 0; /* gcc */
1.4 jfb 1589: lowa = MAX(1, cvp->a - context);
1590: upb = MIN(len[0], context_vec_ptr->b + context);
1591: lowc = MAX(1, cvp->c - context);
1592: upd = MIN(len[1], context_vec_ptr->d + context);
1.1 jfb 1593:
1594: fputs("@@ -", stdout);
1595: uni_range(lowa, upb);
1596: fputs(" +", stdout);
1597: uni_range(lowc, upd);
1598: fputs(" @@", stdout);
1.17 jfb 1599: if (pflag) {
1600: f = match_function(ixold, lowa - 1, f1);
1601: if (f != NULL) {
1602: putchar(' ');
1603: fputs(f, stdout);
1604: }
1605: }
1.1 jfb 1606: putchar('\n');
1607:
1608: /*
1609: * Output changes in "unified" diff format--the old and new lines
1610: * are printed together.
1611: */
1612: for (; cvp <= context_vec_ptr; cvp++) {
1613: a = cvp->a;
1614: b = cvp->b;
1615: c = cvp->c;
1616: d = cvp->d;
1617:
1618: /*
1619: * c: both new and old changes
1620: * d: only changes in the old file
1621: * a: only changes in the new file
1622: */
1623: if (a <= b && c <= d)
1624: ch = 'c';
1625: else
1626: ch = (a <= b) ? 'd' : 'a';
1627:
1628: switch (ch) {
1629: case 'c':
1630: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1631: fetch(ixold, a, b, f1, '-', 0);
1632: fetch(ixnew, c, d, f2, '+', 0);
1633: break;
1634: case 'd':
1635: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1636: fetch(ixold, a, b, f1, '-', 0);
1637: break;
1638: case 'a':
1639: fetch(ixnew, lowc, c - 1, f2, ' ', 0);
1640: fetch(ixnew, c, d, f2, '+', 0);
1641: break;
1642: }
1643: lowa = b + 1;
1644: lowc = d + 1;
1645: }
1646: fetch(ixnew, d + 1, upd, f2, ' ', 0);
1647:
1648: context_vec_ptr = context_vec_start - 1;
1649: }