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