Annotation of src/usr.bin/cvs/diff.c, Revision 1.8
1.1 jfb 1: /* $OpenBSD$ */
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.4 jfb 217: int cvs_diff_file (struct cvs_file *, void *);
1.1 jfb 218: static void output(const char *, FILE *, const char *, FILE *);
219: static void check(FILE *, FILE *);
220: static void range(int, int, char *);
221: static void uni_range(int, int);
222: static void dump_context_vec(FILE *, FILE *);
223: static void dump_unified_vec(FILE *, FILE *);
224: static void prepare(int, FILE *, off_t);
225: static void prune(void);
226: static void equiv(struct line *, int, struct line *, int, int *);
227: static void unravel(int);
228: static void unsort(struct line *, int, int *);
229: static void change(const char *, FILE *, const char *, FILE *, int, int, int, int);
230: static void sort(struct line *, int);
231: static int ignoreline(char *);
232: static int asciifile(FILE *);
233: static int fetch(long *, int, int, FILE *, int, int);
234: static int newcand(int, int, int);
235: static int search(int *, int, int);
236: static int skipline(FILE *);
237: static int isqrt(int);
238: static int stone(int *, int, int *, int *);
239: static int readhash(FILE *);
240: static int files_differ(FILE *, FILE *);
241: static char *preadline(int, size_t, off_t);
242:
243:
244:
245: extern int cvs_client;
246:
1.8 ! jfb 247: static int aflag, bflag, dflag, iflag, Nflag, tflag, Tflag, wflag;
1.1 jfb 248: static int context, status;
249: static int format = D_NORMAL;
250: static struct stat stb1, stb2;
1.2 jfb 251: static char *ifdefname, *ignore_pats, diffargs[128];
252: static const char *diff_file;
1.1 jfb 253: regex_t ignore_re;
254:
255: static int *J; /* will be overlaid on class */
256: static int *class; /* will be overlaid on file[0] */
257: static int *klist; /* will be overlaid on file[0] after class */
258: static int *member; /* will be overlaid on file[1] */
259: static int clen;
260: static int inifdef; /* whether or not we are in a #ifdef block */
261: static int len[2];
262: static int pref, suff; /* length of prefix and suffix */
263: static int slen[2];
264: static int anychange;
265: static long *ixnew; /* will be overlaid on file[1] */
266: static long *ixold; /* will be overlaid on klist */
267: static struct cand *clist; /* merely a free storage pot for candidates */
268: static int clistlen; /* the length of clist */
269: static struct line *sfile[2]; /* shortened by pruning common prefix/suffix */
270: static u_char *chrtran; /* translation table for case-folding */
271: static struct context_vec *context_vec_start;
272: static struct context_vec *context_vec_end;
273: static struct context_vec *context_vec_ptr;
274:
275: #define FUNCTION_CONTEXT_SIZE 41
276: static int lastline;
277: static int lastmatchline;
278:
279:
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: /*
343: * cvs_diff()
344: *
345: * Handler for the `cvs diff' command.
346: *
347: * SYNOPSIS: cvs [args] diff [-clipu] [-D date] [-r rev]
348: */
349:
350: int
351: cvs_diff(int argc, char **argv)
352: {
1.4 jfb 353: int ch, recurse, flags;
354: struct diff_arg darg;
355: struct cvsroot *root;
1.1 jfb 356:
357: context = CVS_DIFF_DEFCTX;
1.4 jfb 358: flags = CF_RECURSE|CF_IGNORE|CF_SORT|CF_KNOWN;
359: recurse = 1;
360:
361: memset(&darg, 0, sizeof(darg));
1.2 jfb 362: strlcpy(diffargs, argv[0], sizeof(diffargs));
1.1 jfb 363:
1.8 ! jfb 364: while ((ch = getopt(argc, argv, "cD:liN:r:u")) != -1) {
1.1 jfb 365: switch (ch) {
366: case 'c':
1.2 jfb 367: strlcat(diffargs, " -c", sizeof(diffargs));
1.1 jfb 368: format = D_CONTEXT;
369: break;
370: case 'D':
1.4 jfb 371: if (darg.date1 == NULL && darg.rev1 == NULL)
372: darg.date1 = optarg;
373: else if (darg.date2 == NULL && darg.rev2 == NULL)
374: darg.date2 = optarg;
1.1 jfb 375: else {
376: cvs_log(LP_ERR,
377: "no more than two revisions/dates can "
378: "be specified");
379: }
380: break;
381: case 'l':
1.2 jfb 382: strlcat(diffargs, " -l", sizeof(diffargs));
1.1 jfb 383: recurse = 0;
1.4 jfb 384: flags &= ~CF_RECURSE;
1.1 jfb 385: break;
386: case 'i':
1.2 jfb 387: strlcat(diffargs, " -i", sizeof(diffargs));
1.1 jfb 388: iflag = 1;
389: break;
1.8 ! jfb 390: case 'N':
! 391: strlcat(diffargs, " -N", sizeof(diffargs));
! 392: Nflag = 1;
! 393: break;
1.1 jfb 394: case 'r':
1.4 jfb 395: if ((darg.rev1 == NULL) && (darg.date1 == NULL))
396: darg.rev1 = optarg;
397: else if ((darg.rev2 == NULL) && (darg.date2 == NULL))
398: darg.rev2 = optarg;
1.1 jfb 399: else {
400: cvs_log(LP_ERR,
401: "no more than two revisions/dates can "
402: "be specified");
403: return (EX_USAGE);
404: }
405: break;
406: case 'u':
1.2 jfb 407: strlcat(diffargs, " -u", sizeof(diffargs));
1.1 jfb 408: format = D_UNIFIED;
409: break;
410: default:
411: return (EX_USAGE);
412: }
413: }
414:
415: argc -= optind;
416: argv += optind;
417:
418: if (argc == 0) {
1.6 jfb 419: cvs_files = cvs_file_get(".", flags);
1.4 jfb 420: }
421: else
1.6 jfb 422: cvs_files = cvs_file_getspec(argv, argc, 0);
1.7 jfb 423: if (cvs_files == NULL)
424: return (EX_DATAERR);
1.1 jfb 425:
1.6 jfb 426: cvs_file_examine(cvs_files, cvs_diff_file, &darg);
1.1 jfb 427:
1.6 jfb 428: root = cvs_files->cf_ddat->cd_root;
1.5 jfb 429: if (root->cr_method != CVS_METHOD_LOCAL) {
1.6 jfb 430: cvs_senddir(root, cvs_files);
1.4 jfb 431: cvs_sendreq(root, CVS_REQ_DIFF, NULL);
1.5 jfb 432: }
1.1 jfb 433:
1.4 jfb 434: return (0);
435: }
1.1 jfb 436:
437:
1.4 jfb 438: /*
439: * cvs_diff_sendflags()
440: *
441: */
1.1 jfb 442:
1.4 jfb 443: int
444: cvs_diff_sendflags(struct cvsroot *root, struct diff_arg *dap)
445: {
446: /* send the flags */
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);
454: cvs_sendarg(root, dap->rev1, 1);
455: }
456: else if (dap->date1 != NULL) {
457: cvs_sendarg(root, "-D", 0);
458: cvs_sendarg(root, dap->date1, 1);
459: }
460: if (dap->rev2 != NULL) {
461: cvs_sendarg(root, "-r", 0);
462: cvs_sendarg(root, dap->rev2, 1);
463: }
464: else if (dap->date2 != NULL) {
465: cvs_sendarg(root, "-D", 0);
466: cvs_sendarg(root, dap->date2, 1);
1.1 jfb 467: }
468:
469: return (0);
470: }
471:
472:
473: /*
474: * cvs_diff_file()
475: *
476: * Diff a single file.
477: */
478:
479: int
1.4 jfb 480: cvs_diff_file(struct cvs_file *cfp, void *arg)
1.1 jfb 481: {
1.8 ! jfb 482: char *dir, *repo, buf[64];
! 483: char fpath[MAXPATHLEN], dfpath[MAXPATHLEN], rcspath[MAXPATHLEN];
1.1 jfb 484: BUF *b1, *b2;
485: RCSNUM *r1, *r2;
486: RCSFILE *rf;
1.4 jfb 487: struct diff_arg *dap;
1.1 jfb 488: struct cvs_ent *entp;
1.4 jfb 489: struct cvsroot *root;
1.1 jfb 490:
1.4 jfb 491: dap = (struct diff_arg *)arg;
1.1 jfb 492:
1.4 jfb 493: if (cfp->cf_type == DT_DIR) {
1.7 jfb 494: if (cfp->cf_cvstat == CVS_FST_UNKNOWN) {
495: root = cfp->cf_parent->cf_ddat->cd_root;
1.8 ! jfb 496: cvs_sendreq(root, CVS_REQ_QUESTIONABLE,
! 497: CVS_FILE_NAME(cfp));
1.7 jfb 498: }
499: else {
500: root = cfp->cf_ddat->cd_root;
501: if ((cfp->cf_parent == NULL) ||
502: (root != cfp->cf_parent->cf_ddat->cd_root)) {
503: cvs_connect(root);
504: cvs_diff_sendflags(root, dap);
505: }
506:
507: cvs_senddir(root, cfp);
1.4 jfb 508: }
1.1 jfb 509:
1.4 jfb 510: return (0);
1.1 jfb 511: }
512:
1.4 jfb 513: rf = NULL;
1.8 ! jfb 514: diff_file = cvs_file_getpath(cfp, fpath, sizeof(fpath));
1.7 jfb 515:
1.4 jfb 516: if (cfp->cf_parent != NULL) {
1.8 ! jfb 517: dir = cvs_file_getpath(cfp->cf_parent, dfpath, sizeof(dfpath));
1.7 jfb 518: root = cfp->cf_parent->cf_ddat->cd_root;
1.4 jfb 519: repo = cfp->cf_parent->cf_ddat->cd_repo;
520: }
521: else {
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.1 jfb 559: }
560: else {
561: snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
1.4 jfb 562: root->cr_dir, repo, diff_file, RCS_FILE_EXT);
1.1 jfb 563:
564: rf = rcs_open(rcspath, RCS_MODE_READ);
1.4 jfb 565: if (rf == NULL) {
566: cvs_ent_free(entp);
1.1 jfb 567: return (-1);
1.4 jfb 568: }
1.1 jfb 569:
1.4 jfb 570: cvs_printf("Index: %s\n%s\nRCS file: %s\n", diff_file,
1.1 jfb 571: RCS_DIFF_DIV, rcspath);
572:
1.4 jfb 573: if (dap->rev1 == NULL)
1.1 jfb 574: r1 = entp->ce_rev;
575: else {
576: r1 = rcsnum_alloc();
1.4 jfb 577: rcsnum_aton(dap->rev1, NULL, r1);
1.1 jfb 578: }
579:
1.4 jfb 580: cvs_printf("retrieving revision %s\n",
1.1 jfb 581: rcsnum_tostr(r1, buf, sizeof(buf)));
582: b1 = rcs_getrev(rf, r1);
583:
1.4 jfb 584: if (dap->rev2 != NULL) {
585: cvs_printf("retrieving revision %s\n", dap->rev2);
1.1 jfb 586: r2 = rcsnum_alloc();
1.4 jfb 587: rcsnum_aton(dap->rev2, NULL, r2);
1.1 jfb 588: b2 = rcs_getrev(rf, r2);
589: }
590: else {
1.4 jfb 591: b2 = cvs_buf_load(diff_file, BUF_AUTOEXT);
1.1 jfb 592: }
593:
1.4 jfb 594: rcs_close(rf);
595:
1.2 jfb 596: printf("%s", diffargs);
597: printf(" -r%s", buf);
1.4 jfb 598: if (dap->rev2 != NULL)
599: printf(" -r%s", dap->rev2);
600: printf(" %s\n", diff_file);
1.1 jfb 601: cvs_buf_write(b1, "/tmp/diff1", 0600);
602: cvs_buf_write(b2, "/tmp/diff2", 0600);
603: cvs_diffreg("/tmp/diff1", "/tmp/diff2");
604: }
605:
1.4 jfb 606: cvs_ent_free(entp);
1.1 jfb 607: return (0);
608: }
609:
610:
611: int
612: cvs_diffreg(const char *file1, const char *file2)
613: {
614: FILE *f1, *f2;
615: int i, rval;
616:
617: f1 = f2 = NULL;
618: rval = D_SAME;
619: anychange = 0;
620: lastline = 0;
621: lastmatchline = 0;
622: context_vec_ptr = context_vec_start - 1;
623: chrtran = (iflag ? cup2low : clow2low);
624:
625: f1 = fopen(file1, "r");
626: if (f1 == NULL) {
627: cvs_log(LP_ERRNO, "%s", file1);
628: status |= 2;
629: goto closem;
630: }
631:
632: f2 = fopen(file2, "r");
633: if (f2 == NULL) {
634: cvs_log(LP_ERRNO, "%s", file2);
635: status |= 2;
636: goto closem;
637: }
638:
639: switch (files_differ(f1, f2)) {
640: case 0:
641: goto closem;
642: case 1:
643: break;
644: default:
645: /* error */
646: status |= 2;
647: goto closem;
648: }
649:
650: if (!asciifile(f1) || !asciifile(f2)) {
651: rval = D_BINARY;
652: status |= 1;
653: goto closem;
654: }
655: prepare(0, f1, stb1.st_size);
656: prepare(1, f2, stb2.st_size);
657: prune();
658: sort(sfile[0], slen[0]);
659: sort(sfile[1], slen[1]);
660:
661: member = (int *)file[1];
662: equiv(sfile[0], slen[0], sfile[1], slen[1], member);
663: member = realloc(member, (slen[1] + 2) * sizeof(int));
664:
665: class = (int *)file[0];
666: unsort(sfile[0], slen[0], class);
667: class = realloc(class, (slen[0] + 2) * sizeof(int));
668:
669: klist = malloc((slen[0] + 2) * sizeof(int));
670: clen = 0;
671: clistlen = 100;
672: clist = malloc(clistlen * sizeof(cand));
673: i = stone(class, slen[0], member, klist);
674: free(member);
675: free(class);
676:
677: J = realloc(J, (len[0] + 2) * sizeof(int));
678: unravel(klist[i]);
679: free(clist);
680: free(klist);
681:
682: ixold = realloc(ixold, (len[0] + 2) * sizeof(long));
683: ixnew = realloc(ixnew, (len[1] + 2) * sizeof(long));
684: check(f1, f2);
685: output(file1, f1, file2, f2);
686:
687: closem:
688: if (anychange) {
689: status |= 1;
690: if (rval == D_SAME)
691: rval = D_DIFFER;
692: }
693: if (f1 != NULL)
694: fclose(f1);
695: if (f2 != NULL)
696: fclose(f2);
697: return (rval);
698: }
699:
700: /*
701: * Check to see if the given files differ.
702: * Returns 0 if they are the same, 1 if different, and -1 on error.
703: * XXX - could use code from cmp(1) [faster]
704: */
705: static int
706: files_differ(FILE *f1, FILE *f2)
707: {
708: char buf1[BUFSIZ], buf2[BUFSIZ];
709: size_t i, j;
710:
711: if (stb1.st_size != stb2.st_size)
712: return (1);
713: for (;;) {
714: i = fread(buf1, 1, sizeof(buf1), f1);
715: j = fread(buf2, 1, sizeof(buf2), f2);
716: if (i != j)
717: return (1);
718: if (i == 0 && j == 0) {
719: if (ferror(f1) || ferror(f2))
720: return (1);
721: return (0);
722: }
723: if (memcmp(buf1, buf2, i) != 0)
724: return (1);
725: }
726: }
727:
728:
729: char *
730: splice(char *dir, char *file)
731: {
732: char *tail, *buf;
733:
734: if ((tail = strrchr(file, '/')) == NULL)
735: tail = file;
736: else
737: tail++;
738: asprintf(&buf, "%s/%s", dir, tail);
739: return (buf);
740: }
741:
742: static void
743: prepare(int i, FILE *fd, off_t filesize)
744: {
745: struct line *p;
746: int j, h;
747: size_t sz;
748:
749: rewind(fd);
750:
751: sz = (filesize <= SIZE_MAX ? filesize : SIZE_MAX) / 25;
752: if (sz < 100)
753: sz = 100;
754:
755: p = malloc((sz + 3) * sizeof(struct line));
756: for (j = 0; (h = readhash(fd));) {
757: if (j == (int)sz) {
758: sz = sz * 3 / 2;
759: p = realloc(p, (sz + 3) * sizeof(struct line));
760: }
761: p[++j].value = h;
762: }
763: len[i] = j;
764: file[i] = p;
765: }
766:
767: static void
768: prune(void)
769: {
770: int i, j;
771:
772: for (pref = 0; pref < len[0] && pref < len[1] &&
773: file[0][pref + 1].value == file[1][pref + 1].value;
774: pref++)
775: ;
776: for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
777: file[0][len[0] - suff].value == file[1][len[1] - suff].value;
778: suff++)
779: ;
780: for (j = 0; j < 2; j++) {
781: sfile[j] = file[j] + pref;
782: slen[j] = len[j] - pref - suff;
783: for (i = 0; i <= slen[j]; i++)
784: sfile[j][i].serial = i;
785: }
786: }
787:
788: static void
789: equiv(struct line *a, int n, struct line *b, int m, int *c)
790: {
791: int i, j;
792:
793: i = j = 1;
794: while (i <= n && j <= m) {
795: if (a[i].value < b[j].value)
796: a[i++].value = 0;
797: else if (a[i].value == b[j].value)
798: a[i++].value = j;
799: else
800: j++;
801: }
802: while (i <= n)
803: a[i++].value = 0;
804: b[m + 1].value = 0;
805: j = 0;
806: while (++j <= m) {
807: c[j] = -b[j].serial;
808: while (b[j + 1].value == b[j].value) {
809: j++;
810: c[j] = b[j].serial;
811: }
812: }
813: c[j] = -1;
814: }
815:
816: /* Code taken from ping.c */
817: static int
818: isqrt(int n)
819: {
820: int y, x = 1;
821:
822: if (n == 0)
823: return(0);
824:
825: do { /* newton was a stinker */
826: y = x;
827: x = n / x;
828: x += y;
829: x /= 2;
830: } while ((x - y) > 1 || (x - y) < -1);
831:
832: return (x);
833: }
834:
835: static int
836: stone(int *a, int n, int *b, int *c)
837: {
838: int i, k, y, j, l;
839: int oldc, tc, oldl;
840: u_int numtries;
841:
1.4 jfb 842: const u_int bound = dflag ? UINT_MAX : MAX(256, isqrt(n));
1.1 jfb 843:
844: k = 0;
845: c[0] = newcand(0, 0, 0);
846: for (i = 1; i <= n; i++) {
847: j = a[i];
848: if (j == 0)
849: continue;
850: y = -b[j];
851: oldl = 0;
852: oldc = c[0];
853: numtries = 0;
854: do {
855: if (y <= clist[oldc].y)
856: continue;
857: l = search(c, k, y);
858: if (l != oldl + 1)
859: oldc = c[l - 1];
860: if (l <= k) {
861: if (clist[c[l]].y <= y)
862: continue;
863: tc = c[l];
864: c[l] = newcand(i, y, oldc);
865: oldc = tc;
866: oldl = l;
867: numtries++;
868: } else {
869: c[l] = newcand(i, y, oldc);
870: k++;
871: break;
872: }
873: } while ((y = b[++j]) > 0 && numtries < bound);
874: }
875: return (k);
876: }
877:
878: static int
879: newcand(int x, int y, int pred)
880: {
881: struct cand *q;
882:
883: if (clen == clistlen) {
884: clistlen = clistlen * 11 / 10;
885: clist = realloc(clist, clistlen * sizeof(cand));
886: }
887: q = clist + clen;
888: q->x = x;
889: q->y = y;
890: q->pred = pred;
891: return (clen++);
892: }
893:
894: static int
895: search(int *c, int k, int y)
896: {
897: int i, j, l, t;
898:
899: if (clist[c[k]].y < y) /* quick look for typical case */
900: return (k + 1);
901: i = 0;
902: j = k + 1;
903: while (1) {
904: l = i + j;
905: if ((l >>= 1) <= i)
906: break;
907: t = clist[c[l]].y;
908: if (t > y)
909: j = l;
910: else if (t < y)
911: i = l;
912: else
913: return (l);
914: }
915: return (l + 1);
916: }
917:
918: static void
919: unravel(int p)
920: {
921: struct cand *q;
922: int i;
923:
924: for (i = 0; i <= len[0]; i++)
925: J[i] = i <= pref ? i :
926: i > len[0] - suff ? i + len[1] - len[0] : 0;
927: for (q = clist + p; q->y != 0; q = clist + q->pred)
928: J[q->x + pref] = q->y + pref;
929: }
930:
931: /*
932: * Check does double duty:
933: * 1. ferret out any fortuitous correspondences due
934: * to confounding by hashing (which result in "jackpot")
935: * 2. collect random access indexes to the two files
936: */
937: static void
938: check(FILE *f1, FILE *f2)
939: {
940: int i, j, jackpot, c, d;
941: long ctold, ctnew;
942:
943: rewind(f1);
944: rewind(f2);
945: j = 1;
946: ixold[0] = ixnew[0] = 0;
947: jackpot = 0;
948: ctold = ctnew = 0;
949: for (i = 1; i <= len[0]; i++) {
950: if (J[i] == 0) {
951: ixold[i] = ctold += skipline(f1);
952: continue;
953: }
954: while (j < J[i]) {
955: ixnew[j] = ctnew += skipline(f2);
956: j++;
957: }
958: if (bflag || wflag || iflag) {
959: for (;;) {
960: c = getc(f1);
961: d = getc(f2);
962: /*
963: * GNU diff ignores a missing newline
964: * in one file if bflag || wflag.
965: */
966: if ((bflag || wflag) &&
967: ((c == EOF && d == '\n') ||
968: (c == '\n' && d == EOF))) {
969: break;
970: }
971: ctold++;
972: ctnew++;
973: if (bflag && isspace(c) && isspace(d)) {
974: do {
975: if (c == '\n')
976: break;
977: ctold++;
978: } while (isspace(c = getc(f1)));
979: do {
980: if (d == '\n')
981: break;
982: ctnew++;
983: } while (isspace(d = getc(f2)));
984: } else if (wflag) {
985: while (isspace(c) && c != '\n') {
986: c = getc(f1);
987: ctold++;
988: }
989: while (isspace(d) && d != '\n') {
990: d = getc(f2);
991: ctnew++;
992: }
993: }
994: if (chrtran[c] != chrtran[d]) {
995: jackpot++;
996: J[i] = 0;
997: if (c != '\n' && c != EOF)
998: ctold += skipline(f1);
999: if (d != '\n' && c != EOF)
1000: ctnew += skipline(f2);
1001: break;
1002: }
1003: if (c == '\n' || c == EOF)
1004: break;
1005: }
1006: } else {
1007: for (;;) {
1008: ctold++;
1009: ctnew++;
1010: if ((c = getc(f1)) != (d = getc(f2))) {
1011: /* jackpot++; */
1012: J[i] = 0;
1013: if (c != '\n' && c != EOF)
1014: ctold += skipline(f1);
1015: if (d != '\n' && c != EOF)
1016: ctnew += skipline(f2);
1017: break;
1018: }
1019: if (c == '\n' || c == EOF)
1020: break;
1021: }
1022: }
1023: ixold[i] = ctold;
1024: ixnew[j] = ctnew;
1025: j++;
1026: }
1027: for (; j <= len[1]; j++)
1028: ixnew[j] = ctnew += skipline(f2);
1029: /*
1030: * if (jackpot)
1031: * fprintf(stderr, "jackpot\n");
1032: */
1033: }
1034:
1035: /* shellsort CACM #201 */
1036: static void
1037: sort(struct line *a, int n)
1038: {
1039: struct line *ai, *aim, w;
1040: int j, m = 0, k;
1041:
1042: if (n == 0)
1043: return;
1044: for (j = 1; j <= n; j *= 2)
1045: m = 2 * j - 1;
1046: for (m /= 2; m != 0; m /= 2) {
1047: k = n - m;
1048: for (j = 1; j <= k; j++) {
1049: for (ai = &a[j]; ai > a; ai -= m) {
1050: aim = &ai[m];
1051: if (aim < ai)
1052: break; /* wraparound */
1053: if (aim->value > ai[0].value ||
1054: (aim->value == ai[0].value &&
1055: aim->serial > ai[0].serial))
1056: break;
1057: w.value = ai[0].value;
1058: ai[0].value = aim->value;
1059: aim->value = w.value;
1060: w.serial = ai[0].serial;
1061: ai[0].serial = aim->serial;
1062: aim->serial = w.serial;
1063: }
1064: }
1065: }
1066: }
1067:
1068: static void
1069: unsort(struct line *f, int l, int *b)
1070: {
1071: int *a, i;
1072:
1073: a = malloc((l + 1) * sizeof(int));
1074: for (i = 1; i <= l; i++)
1075: a[f[i].serial] = f[i].value;
1076: for (i = 1; i <= l; i++)
1077: b[i] = a[i];
1078: free(a);
1079: }
1080:
1081: static int
1082: skipline(FILE *f)
1083: {
1084: int i, c;
1085:
1086: for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
1087: continue;
1088: return (i);
1089: }
1090:
1091: static void
1092: output(const char *file1, FILE *f1, const char *file2, FILE *f2)
1093: {
1094: int m, i0, i1, j0, j1;
1095:
1096: rewind(f1);
1097: rewind(f2);
1098: m = len[0];
1099: J[0] = 0;
1100: J[m + 1] = len[1] + 1;
1101: for (i0 = 1; i0 <= m; i0 = i1 + 1) {
1102: while (i0 <= m && J[i0] == J[i0 - 1] + 1)
1103: i0++;
1104: j0 = J[i0 - 1] + 1;
1105: i1 = i0 - 1;
1106: while (i1 < m && J[i1 + 1] == 0)
1107: i1++;
1108: j1 = J[i1 + 1] - 1;
1109: J[i1] = j1;
1110: change(file1, f1, file2, f2, i0, i1, j0, j1);
1111: }
1112: if (m == 0)
1113: change(file1, f1, file2, f2, 1, 0, 1, len[1]);
1114: if (format == D_IFDEF) {
1115: for (;;) {
1116: #define c i0
1117: if ((c = getc(f1)) == EOF)
1118: return;
1119: putchar(c);
1120: }
1121: #undef c
1122: }
1123: if (anychange != 0) {
1124: if (format == D_CONTEXT)
1125: dump_context_vec(f1, f2);
1126: else if (format == D_UNIFIED)
1127: dump_unified_vec(f1, f2);
1128: }
1129: }
1130:
1131: static __inline void
1132: range(int a, int b, char *separator)
1133: {
1134: printf("%d", a > b ? b : a);
1135: if (a < b)
1136: printf("%s%d", separator, b);
1137: }
1138:
1139: static __inline void
1140: uni_range(int a, int b)
1141: {
1142: if (a < b)
1143: printf("%d,%d", a, b - a + 1);
1144: else if (a == b)
1145: printf("%d", b);
1146: else
1147: printf("%d,0", b);
1148: }
1149:
1150: static char *
1151: preadline(int fd, size_t len, off_t off)
1152: {
1153: char *line;
1154: ssize_t nr;
1155:
1156: line = malloc(len + 1);
1157: if (line == NULL) {
1158: cvs_log(LP_ERRNO, "failed to allocate line");
1159: return (NULL);
1160: }
1161: if ((nr = pread(fd, line, len, off)) < 0) {
1162: cvs_log(LP_ERRNO, "preadline failed");
1163: return (NULL);
1164: }
1165: line[nr] = '\0';
1166: return (line);
1167: }
1168:
1169: static int
1170: ignoreline(char *line)
1171: {
1172: int ret;
1173:
1174: ret = regexec(&ignore_re, line, 0, NULL, 0);
1175: free(line);
1176: return (ret == 0); /* if it matched, it should be ignored. */
1177: }
1178:
1179: /*
1180: * Indicate that there is a difference between lines a and b of the from file
1181: * to get to lines c to d of the to file. If a is greater then b then there
1182: * are no lines in the from file involved and this means that there were
1183: * lines appended (beginning at b). If c is greater than d then there are
1184: * lines missing from the to file.
1185: */
1186: static void
1187: change(const char *file1, FILE *f1, const char *file2, FILE *f2,
1188: int a, int b, int c, int d)
1189: {
1190: static size_t max_context = 64;
1191: int i;
1192:
1193: if (format != D_IFDEF && a > b && c > d)
1194: return;
1195: if (ignore_pats != NULL) {
1196: char *line;
1197: /*
1198: * All lines in the change, insert, or delete must
1199: * match an ignore pattern for the change to be
1200: * ignored.
1201: */
1202: if (a <= b) { /* Changes and deletes. */
1203: for (i = a; i <= b; i++) {
1204: line = preadline(fileno(f1),
1205: ixold[i] - ixold[i - 1], ixold[i - 1]);
1206: if (!ignoreline(line))
1207: goto proceed;
1208: }
1209: }
1210: if (a > b || c <= d) { /* Changes and inserts. */
1211: for (i = c; i <= d; i++) {
1212: line = preadline(fileno(f2),
1213: ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
1214: if (!ignoreline(line))
1215: goto proceed;
1216: }
1217: }
1218: return;
1219: }
1220: proceed:
1221: if (format == D_CONTEXT || format == D_UNIFIED) {
1222: /*
1223: * Allocate change records as needed.
1224: */
1225: if (context_vec_ptr == context_vec_end - 1) {
1226: ptrdiff_t offset = context_vec_ptr - context_vec_start;
1227: max_context <<= 1;
1228: context_vec_start = realloc(context_vec_start,
1229: max_context * sizeof(struct context_vec));
1230: context_vec_end = context_vec_start + max_context;
1231: context_vec_ptr = context_vec_start + offset;
1232: }
1233: if (anychange == 0) {
1234: /*
1235: * Print the context/unidiff header first time through.
1236: */
1237: printf("%s %s %s",
1238: format == D_CONTEXT ? "***" : "---", diff_file,
1239: ctime(&stb1.st_mtime));
1240: printf("%s %s %s",
1241: format == D_CONTEXT ? "---" : "+++", diff_file,
1242: ctime(&stb2.st_mtime));
1243: anychange = 1;
1244: } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
1245: c > context_vec_ptr->d + (2 * context) + 1) {
1246: /*
1247: * If this change is more than 'context' lines from the
1248: * previous change, dump the record and reset it.
1249: */
1250: if (format == D_CONTEXT)
1251: dump_context_vec(f1, f2);
1252: else
1253: dump_unified_vec(f1, f2);
1254: }
1255: context_vec_ptr++;
1256: context_vec_ptr->a = a;
1257: context_vec_ptr->b = b;
1258: context_vec_ptr->c = c;
1259: context_vec_ptr->d = d;
1260: return;
1261: }
1262: if (anychange == 0)
1263: anychange = 1;
1264: switch (format) {
1265: case D_BRIEF:
1266: return;
1267: case D_NORMAL:
1268: range(a, b, ",");
1269: putchar(a > b ? 'a' : c > d ? 'd' : 'c');
1270: if (format == D_NORMAL)
1271: range(c, d, ",");
1272: putchar('\n');
1273: break;
1274: }
1275: if (format == D_NORMAL || format == D_IFDEF) {
1276: fetch(ixold, a, b, f1, '<', 1);
1277: if (a <= b && c <= d && format == D_NORMAL)
1278: puts("---");
1279: }
1280: i = fetch(ixnew, c, d, f2, format == D_NORMAL ? '>' : '\0', 0);
1281: if (inifdef) {
1282: printf("#endif /* %s */\n", ifdefname);
1283: inifdef = 0;
1284: }
1285: }
1286:
1287: static int
1288: fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile)
1289: {
1290: int i, j, c, lastc, col, nc;
1291:
1292: /*
1293: * When doing #ifdef's, copy down to current line
1294: * if this is the first file, so that stuff makes it to output.
1295: */
1296: if (format == D_IFDEF && oldfile) {
1297: long curpos = ftell(lb);
1298: /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1299: nc = f[a > b ? b : a - 1] - curpos;
1300: for (i = 0; i < nc; i++)
1301: putchar(getc(lb));
1302: }
1303: if (a > b)
1304: return (0);
1305: if (format == D_IFDEF) {
1306: if (inifdef) {
1307: printf("#else /* %s%s */\n",
1308: oldfile == 1 ? "!" : "", ifdefname);
1309: } else {
1310: if (oldfile)
1311: printf("#ifndef %s\n", ifdefname);
1312: else
1313: printf("#ifdef %s\n", ifdefname);
1314: }
1315: inifdef = 1 + oldfile;
1316: }
1317: for (i = a; i <= b; i++) {
1318: fseek(lb, f[i - 1], SEEK_SET);
1319: nc = f[i] - f[i - 1];
1320: if (format != D_IFDEF && ch != '\0') {
1321: putchar(ch);
1322: if (Tflag && (format == D_NORMAL || format == D_CONTEXT
1323: || format == D_UNIFIED))
1324: putchar('\t');
1325: else if (format != D_UNIFIED)
1326: putchar(' ');
1327: }
1328: col = 0;
1329: for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
1330: if ((c = getc(lb)) == EOF) {
1331: puts("\n\\ No newline at end of file");
1332: return (0);
1333: }
1334: if (c == '\t' && tflag) {
1335: do {
1336: putchar(' ');
1337: } while (++col & 7);
1338: } else {
1339: putchar(c);
1340: col++;
1341: }
1342: }
1343: }
1344: return (0);
1345: }
1346:
1347: /*
1348: * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
1349: */
1350: static int
1351: readhash(FILE *f)
1352: {
1353: int i, t, space;
1354: int sum;
1355:
1356: sum = 1;
1357: space = 0;
1358: if (!bflag && !wflag) {
1359: if (iflag)
1360: for (i = 0; (t = getc(f)) != '\n'; i++) {
1361: if (t == EOF) {
1362: if (i == 0)
1363: return (0);
1364: break;
1365: }
1366: sum = sum * 127 + chrtran[t];
1367: }
1368: else
1369: for (i = 0; (t = getc(f)) != '\n'; i++) {
1370: if (t == EOF) {
1371: if (i == 0)
1372: return (0);
1373: break;
1374: }
1375: sum = sum * 127 + t;
1376: }
1377: } else {
1378: for (i = 0;;) {
1379: switch (t = getc(f)) {
1380: case '\t':
1381: case ' ':
1382: space++;
1383: continue;
1384: default:
1385: if (space && !wflag) {
1386: i++;
1387: space = 0;
1388: }
1389: sum = sum * 127 + chrtran[t];
1390: i++;
1391: continue;
1392: case EOF:
1393: if (i == 0)
1394: return (0);
1395: /* FALLTHROUGH */
1396: case '\n':
1397: break;
1398: }
1399: break;
1400: }
1401: }
1402: /*
1403: * There is a remote possibility that we end up with a zero sum.
1404: * Zero is used as an EOF marker, so return 1 instead.
1405: */
1406: return (sum == 0 ? 1 : sum);
1407: }
1408:
1409: static int
1410: asciifile(FILE *f)
1411: {
1412: char buf[BUFSIZ];
1413: int i, cnt;
1414:
1415: if (aflag || f == NULL)
1416: return (1);
1417:
1418: rewind(f);
1419: cnt = fread(buf, 1, sizeof(buf), f);
1420: for (i = 0; i < cnt; i++)
1421: if (!isprint(buf[i]) && !isspace(buf[i]))
1422: return (0);
1423: return (1);
1424: }
1425:
1426:
1427: /* dump accumulated "context" diff changes */
1428: static void
1429: dump_context_vec(FILE *f1, FILE *f2)
1430: {
1431: struct context_vec *cvp = context_vec_start;
1432: int lowa, upb, lowc, upd, do_output;
1433: int a, b, c, d;
1.4 jfb 1434: char ch;
1.1 jfb 1435:
1436: if (context_vec_start > context_vec_ptr)
1437: return;
1438:
1439: b = d = 0; /* gcc */
1.4 jfb 1440: lowa = MAX(1, cvp->a - context);
1441: upb = MIN(len[0], context_vec_ptr->b + context);
1442: lowc = MAX(1, cvp->c - context);
1443: upd = MIN(len[1], context_vec_ptr->d + context);
1.1 jfb 1444:
1445: printf("***************");
1446: printf("\n*** ");
1447: range(lowa, upb, ",");
1448: printf(" ****\n");
1449:
1450: /*
1451: * Output changes to the "old" file. The first loop suppresses
1452: * output if there were no changes to the "old" file (we'll see
1453: * the "old" lines as context in the "new" list).
1454: */
1455: do_output = 0;
1456: for (; cvp <= context_vec_ptr; cvp++)
1457: if (cvp->a <= cvp->b) {
1458: cvp = context_vec_start;
1459: do_output++;
1460: break;
1461: }
1462: if (do_output) {
1463: while (cvp <= context_vec_ptr) {
1464: a = cvp->a;
1465: b = cvp->b;
1466: c = cvp->c;
1467: d = cvp->d;
1468:
1469: if (a <= b && c <= d)
1470: ch = 'c';
1471: else
1472: ch = (a <= b) ? 'd' : 'a';
1473:
1474: if (ch == 'a')
1475: fetch(ixold, lowa, b, f1, ' ', 0);
1476: else {
1477: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1478: fetch(ixold, a, b, f1,
1479: ch == 'c' ? '!' : '-', 0);
1480: }
1481: lowa = b + 1;
1482: cvp++;
1483: }
1484: fetch(ixold, b + 1, upb, f1, ' ', 0);
1485: }
1486: /* output changes to the "new" file */
1487: printf("--- ");
1488: range(lowc, upd, ",");
1489: printf(" ----\n");
1490:
1491: do_output = 0;
1492: for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
1493: if (cvp->c <= cvp->d) {
1494: cvp = context_vec_start;
1495: do_output++;
1496: break;
1497: }
1498: if (do_output) {
1499: while (cvp <= context_vec_ptr) {
1500: a = cvp->a;
1501: b = cvp->b;
1502: c = cvp->c;
1503: d = cvp->d;
1504:
1505: if (a <= b && c <= d)
1506: ch = 'c';
1507: else
1508: ch = (a <= b) ? 'd' : 'a';
1509:
1510: if (ch == 'd')
1511: fetch(ixnew, lowc, d, f2, ' ', 0);
1512: else {
1513: fetch(ixnew, lowc, c - 1, f2, ' ', 0);
1514: fetch(ixnew, c, d, f2,
1515: ch == 'c' ? '!' : '+', 0);
1516: }
1517: lowc = d + 1;
1518: cvp++;
1519: }
1520: fetch(ixnew, d + 1, upd, f2, ' ', 0);
1521: }
1522: context_vec_ptr = context_vec_start - 1;
1523: }
1524:
1525: /* dump accumulated "unified" diff changes */
1526: static void
1527: dump_unified_vec(FILE *f1, FILE *f2)
1528: {
1529: struct context_vec *cvp = context_vec_start;
1530: int lowa, upb, lowc, upd;
1531: int a, b, c, d;
1.4 jfb 1532: char ch;
1.1 jfb 1533:
1534: if (context_vec_start > context_vec_ptr)
1535: return;
1536:
1537: b = d = 0; /* gcc */
1.4 jfb 1538: lowa = MAX(1, cvp->a - context);
1539: upb = MIN(len[0], context_vec_ptr->b + context);
1540: lowc = MAX(1, cvp->c - context);
1541: upd = MIN(len[1], context_vec_ptr->d + context);
1.1 jfb 1542:
1543: fputs("@@ -", stdout);
1544: uni_range(lowa, upb);
1545: fputs(" +", stdout);
1546: uni_range(lowc, upd);
1547: fputs(" @@", stdout);
1548: putchar('\n');
1549:
1550: /*
1551: * Output changes in "unified" diff format--the old and new lines
1552: * are printed together.
1553: */
1554: for (; cvp <= context_vec_ptr; cvp++) {
1555: a = cvp->a;
1556: b = cvp->b;
1557: c = cvp->c;
1558: d = cvp->d;
1559:
1560: /*
1561: * c: both new and old changes
1562: * d: only changes in the old file
1563: * a: only changes in the new file
1564: */
1565: if (a <= b && c <= d)
1566: ch = 'c';
1567: else
1568: ch = (a <= b) ? 'd' : 'a';
1569:
1570: switch (ch) {
1571: case 'c':
1572: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1573: fetch(ixold, a, b, f1, '-', 0);
1574: fetch(ixnew, c, d, f2, '+', 0);
1575: break;
1576: case 'd':
1577: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1578: fetch(ixold, a, b, f1, '-', 0);
1579: break;
1580: case 'a':
1581: fetch(ixnew, lowc, c - 1, f2, ' ', 0);
1582: fetch(ixnew, c, d, f2, '+', 0);
1583: break;
1584: }
1585: lowa = b + 1;
1586: lowc = d + 1;
1587: }
1588: fetch(ixnew, d + 1, upd, f2, ' ', 0);
1589:
1590: context_vec_ptr = context_vec_start - 1;
1591: }