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