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