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