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