Annotation of src/usr.bin/cvs/diff.c, Revision 1.71
1.71 ! joris 1: /* $OpenBSD: diff.c,v 1.70 2005/12/05 19:53:00 niallo 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.36 jfb 678: if (cvs_buf_write_stmp(b1, path_tmp1, 0600) == -1) {
679: cvs_buf_free(b1);
1.20 jfb 680: cvs_buf_free(b2);
1.36 jfb 681: return (CVS_EX_DATA);
682: }
683: cvs_buf_free(b1);
1.69 niallo 684: if (utimes(path_tmp1, (const struct timeval *)&tv) < 0)
685: cvs_log(LP_ERRNO, "error setting utimes");
1.20 jfb 686:
1.67 xsa 687: strlcpy(path_tmp2, cvs_tmpdir, sizeof(path_tmp2));
688: strlcat(path_tmp2, "/diff2.XXXXXXXXXX", sizeof(path_tmp2));
1.36 jfb 689: if (cvs_buf_write_stmp(b2, path_tmp2, 0600) == -1) {
690: cvs_buf_free(b2);
1.12 djm 691: (void)unlink(path_tmp1);
1.36 jfb 692: return (CVS_EX_DATA);
1.1 jfb 693: }
1.36 jfb 694: cvs_buf_free(b2);
1.69 niallo 695: if (utimes(path_tmp2, (const struct timeval *)&tv2) < 0)
696: cvs_log(LP_ERRNO, "error setting utimes");
1.36 jfb 697:
1.58 niallo 698: cvs_diffreg(path_tmp1, path_tmp2, NULL);
1.36 jfb 699: (void)unlink(path_tmp1);
700: (void)unlink(path_tmp2);
1.1 jfb 701:
702: return (0);
703: }
1.56 niallo 704: #endif
1.1 jfb 705:
706:
707: int
1.58 niallo 708: cvs_diffreg(const char *file1, const char *file2, BUF *out)
1.1 jfb 709: {
710: FILE *f1, *f2;
711: int i, rval;
1.20 jfb 712: void *tmp;
1.1 jfb 713:
714: f1 = f2 = NULL;
715: rval = D_SAME;
716: anychange = 0;
717: lastline = 0;
718: lastmatchline = 0;
719: context_vec_ptr = context_vec_start - 1;
720: chrtran = (iflag ? cup2low : clow2low);
1.58 niallo 721: if (out != NULL)
722: diffbuf = out;
1.1 jfb 723:
724: f1 = fopen(file1, "r");
725: if (f1 == NULL) {
726: cvs_log(LP_ERRNO, "%s", file1);
727: goto closem;
728: }
729:
730: f2 = fopen(file2, "r");
731: if (f2 == NULL) {
732: cvs_log(LP_ERRNO, "%s", file2);
733: goto closem;
734: }
735:
1.69 niallo 736: if (stat(file1, &stb1) < 0) {
737: cvs_log(LP_ERRNO, "%s", file1);
738: goto closem;
739: }
740: if (stat(file2, &stb2) < 0) {
741: cvs_log(LP_ERRNO, "%s", file2);
742: goto closem;
743: }
1.1 jfb 744: switch (files_differ(f1, f2)) {
745: case 0:
746: goto closem;
747: case 1:
748: break;
749: default:
750: /* error */
751: goto closem;
752: }
753:
754: if (!asciifile(f1) || !asciifile(f2)) {
755: rval = D_BINARY;
756: goto closem;
757: }
1.20 jfb 758: if ((prepare(0, f1, stb1.st_size) < 0) ||
759: (prepare(1, f2, stb2.st_size) < 0)) {
760: goto closem;
761: }
1.1 jfb 762: prune();
763: sort(sfile[0], slen[0]);
764: sort(sfile[1], slen[1]);
765:
766: member = (int *)file[1];
767: equiv(sfile[0], slen[0], sfile[1], slen[1], member);
1.71 ! joris 768: tmp = xrealloc(member, (slen[1] + 2) * sizeof(int));
1.20 jfb 769: member = (int *)tmp;
1.1 jfb 770:
771: class = (int *)file[0];
772: unsort(sfile[0], slen[0], class);
1.71 ! joris 773: tmp = xrealloc(class, (slen[0] + 2) * sizeof(int));
1.20 jfb 774: class = (int *)tmp;
1.1 jfb 775:
1.71 ! joris 776: klist = xmalloc((slen[0] + 2) * sizeof(int));
1.1 jfb 777: clen = 0;
778: clistlen = 100;
1.71 ! joris 779: clist = xmalloc(clistlen * sizeof(cand));
1.48 joris 780:
781: if ((i = stone(class, slen[0], member, klist)) < 0)
782: goto closem;
783:
1.71 ! joris 784: xfree(member);
! 785: xfree(class);
1.1 jfb 786:
1.71 ! joris 787: tmp = xrealloc(J, (diff_len[0] + 2) * sizeof(int));
1.49 niallo 788: J = (int *)tmp;
1.1 jfb 789: unravel(klist[i]);
1.71 ! joris 790: xfree(clist);
! 791: xfree(klist);
1.1 jfb 792:
1.71 ! joris 793: tmp = xrealloc(ixold, (diff_len[0] + 2) * sizeof(long));
1.49 niallo 794: ixold = (long *)tmp;
1.71 ! joris 795:
! 796: tmp = xrealloc(ixnew, (diff_len[1] + 2) * sizeof(long));
1.49 niallo 797: ixnew = (long *)tmp;
1.1 jfb 798: check(f1, f2);
799: output(file1, f1, file2, f2);
800:
801: closem:
1.65 xsa 802: if (anychange == 1) {
1.1 jfb 803: if (rval == D_SAME)
804: rval = D_DIFFER;
805: }
806: if (f1 != NULL)
807: fclose(f1);
808: if (f2 != NULL)
809: fclose(f2);
1.20 jfb 810:
1.1 jfb 811: return (rval);
812: }
813:
814: /*
815: * Check to see if the given files differ.
816: * Returns 0 if they are the same, 1 if different, and -1 on error.
817: * XXX - could use code from cmp(1) [faster]
818: */
819: static int
820: files_differ(FILE *f1, FILE *f2)
821: {
822: char buf1[BUFSIZ], buf2[BUFSIZ];
823: size_t i, j;
824:
825: if (stb1.st_size != stb2.st_size)
826: return (1);
827: for (;;) {
1.55 xsa 828: i = fread(buf1, (size_t)1, sizeof(buf1), f1);
829: j = fread(buf2, (size_t)1, sizeof(buf2), f2);
1.1 jfb 830: if (i != j)
831: return (1);
1.65 xsa 832: if ((i == 0) && (j == 0)) {
1.1 jfb 833: if (ferror(f1) || ferror(f2))
834: return (1);
835: return (0);
836: }
837: if (memcmp(buf1, buf2, i) != 0)
838: return (1);
839: }
840: }
841:
1.20 jfb 842: static int
1.1 jfb 843: prepare(int i, FILE *fd, off_t filesize)
844: {
1.20 jfb 845: void *tmp;
1.1 jfb 846: struct line *p;
847: int j, h;
848: size_t sz;
849:
850: rewind(fd);
851:
1.16 xsa 852: sz = ((size_t)filesize <= SIZE_MAX ? (size_t)filesize : SIZE_MAX) / 25;
1.1 jfb 853: if (sz < 100)
854: sz = 100;
855:
1.71 ! joris 856: p = (struct line *)xmalloc((sz + 3) * sizeof(struct line));
1.1 jfb 857: for (j = 0; (h = readhash(fd));) {
858: if (j == (int)sz) {
859: sz = sz * 3 / 2;
1.71 ! joris 860: tmp = xrealloc(p, (sz + 3) * sizeof(struct line));
1.20 jfb 861: p = (struct line *)tmp;
1.1 jfb 862: }
863: p[++j].value = h;
864: }
1.36 jfb 865: diff_len[i] = j;
1.1 jfb 866: file[i] = p;
1.20 jfb 867:
868: return (0);
1.1 jfb 869: }
870:
871: static void
872: prune(void)
873: {
874: int i, j;
875:
1.36 jfb 876: for (pref = 0; pref < diff_len[0] && pref < diff_len[1] &&
1.1 jfb 877: file[0][pref + 1].value == file[1][pref + 1].value;
878: pref++)
879: ;
1.36 jfb 880: for (suff = 0;
881: (suff < diff_len[0] - pref) && (suff < diff_len[1] - pref) &&
882: (file[0][diff_len[0] - suff].value ==
883: file[1][diff_len[1] - suff].value);
1.1 jfb 884: suff++)
885: ;
886: for (j = 0; j < 2; j++) {
887: sfile[j] = file[j] + pref;
1.36 jfb 888: slen[j] = diff_len[j] - pref - suff;
1.1 jfb 889: for (i = 0; i <= slen[j]; i++)
890: sfile[j][i].serial = i;
891: }
892: }
893:
894: static void
895: equiv(struct line *a, int n, struct line *b, int m, int *c)
896: {
897: int i, j;
898:
899: i = j = 1;
900: while (i <= n && j <= m) {
901: if (a[i].value < b[j].value)
902: a[i++].value = 0;
903: else if (a[i].value == b[j].value)
904: a[i++].value = j;
905: else
906: j++;
907: }
908: while (i <= n)
909: a[i++].value = 0;
910: b[m + 1].value = 0;
911: j = 0;
912: while (++j <= m) {
913: c[j] = -b[j].serial;
914: while (b[j + 1].value == b[j].value) {
915: j++;
916: c[j] = b[j].serial;
917: }
918: }
919: c[j] = -1;
920: }
921:
922: /* Code taken from ping.c */
923: static int
924: isqrt(int n)
925: {
926: int y, x = 1;
927:
928: if (n == 0)
1.18 jfb 929: return (0);
1.1 jfb 930:
931: do { /* newton was a stinker */
932: y = x;
933: x = n / x;
934: x += y;
935: x /= 2;
936: } while ((x - y) > 1 || (x - y) < -1);
937:
938: return (x);
939: }
940:
941: static int
942: stone(int *a, int n, int *b, int *c)
943: {
1.48 joris 944: int ret;
1.1 jfb 945: int i, k, y, j, l;
946: int oldc, tc, oldl;
947: u_int numtries;
948:
1.18 jfb 949: /* XXX move the isqrt() out of the macro to avoid multiple calls */
950: const u_int bound = dflag ? UINT_MAX : MAX(256, (u_int)isqrt(n));
1.1 jfb 951:
952: k = 0;
1.48 joris 953: if ((ret = newcand(0, 0, 0)) < 0)
954: return (-1);
955: c[0] = ret;
1.1 jfb 956: for (i = 1; i <= n; i++) {
957: j = a[i];
958: if (j == 0)
959: continue;
960: y = -b[j];
961: oldl = 0;
962: oldc = c[0];
963: numtries = 0;
964: do {
965: if (y <= clist[oldc].y)
966: continue;
967: l = search(c, k, y);
968: if (l != oldl + 1)
969: oldc = c[l - 1];
970: if (l <= k) {
971: if (clist[c[l]].y <= y)
972: continue;
973: tc = c[l];
1.48 joris 974: if ((ret = newcand(i, y, oldc)) < 0)
975: return (-1);
976: c[l] = ret;
1.1 jfb 977: oldc = tc;
978: oldl = l;
979: numtries++;
980: } else {
1.48 joris 981: if ((ret = newcand(i, y, oldc)) < 0)
982: return (-1);
983: c[l] = ret;
1.1 jfb 984: k++;
985: break;
986: }
987: } while ((y = b[++j]) > 0 && numtries < bound);
988: }
989: return (k);
990: }
991:
992: static int
993: newcand(int x, int y, int pred)
994: {
1.49 niallo 995: struct cand *q, *tmp;
996: int newclistlen;
1.1 jfb 997:
998: if (clen == clistlen) {
1.49 niallo 999: newclistlen = clistlen * 11 / 10;
1.71 ! joris 1000: tmp = xrealloc(clist, newclistlen * sizeof(cand));
1.49 niallo 1001: clist = tmp;
1002: clistlen = newclistlen;
1.1 jfb 1003: }
1004: q = clist + clen;
1005: q->x = x;
1006: q->y = y;
1007: q->pred = pred;
1008: return (clen++);
1009: }
1010:
1011: static int
1012: search(int *c, int k, int y)
1013: {
1014: int i, j, l, t;
1015:
1016: if (clist[c[k]].y < y) /* quick look for typical case */
1017: return (k + 1);
1018: i = 0;
1019: j = k + 1;
1020: while (1) {
1021: l = i + j;
1022: if ((l >>= 1) <= i)
1023: break;
1024: t = clist[c[l]].y;
1025: if (t > y)
1026: j = l;
1027: else if (t < y)
1028: i = l;
1029: else
1030: return (l);
1031: }
1032: return (l + 1);
1033: }
1034:
1035: static void
1036: unravel(int p)
1037: {
1038: struct cand *q;
1039: int i;
1040:
1.36 jfb 1041: for (i = 0; i <= diff_len[0]; i++)
1.1 jfb 1042: J[i] = i <= pref ? i :
1.36 jfb 1043: i > diff_len[0] - suff ? i + diff_len[1] - diff_len[0] : 0;
1.1 jfb 1044: for (q = clist + p; q->y != 0; q = clist + q->pred)
1045: J[q->x + pref] = q->y + pref;
1046: }
1047:
1048: /*
1049: * Check does double duty:
1050: * 1. ferret out any fortuitous correspondences due
1051: * to confounding by hashing (which result in "jackpot")
1052: * 2. collect random access indexes to the two files
1053: */
1054: static void
1055: check(FILE *f1, FILE *f2)
1056: {
1057: int i, j, jackpot, c, d;
1058: long ctold, ctnew;
1059:
1060: rewind(f1);
1061: rewind(f2);
1062: j = 1;
1063: ixold[0] = ixnew[0] = 0;
1064: jackpot = 0;
1065: ctold = ctnew = 0;
1.36 jfb 1066: for (i = 1; i <= diff_len[0]; i++) {
1.1 jfb 1067: if (J[i] == 0) {
1068: ixold[i] = ctold += skipline(f1);
1069: continue;
1070: }
1071: while (j < J[i]) {
1072: ixnew[j] = ctnew += skipline(f2);
1073: j++;
1074: }
1.64 xsa 1075: if ((bflag == 1)|| (wflag == 1) || (iflag == 1)) {
1.1 jfb 1076: for (;;) {
1077: c = getc(f1);
1078: d = getc(f2);
1079: /*
1080: * GNU diff ignores a missing newline
1081: * in one file if bflag || wflag.
1082: */
1.64 xsa 1083: if (((bflag == 1) || (wflag == 1)) &&
1.1 jfb 1084: ((c == EOF && d == '\n') ||
1085: (c == '\n' && d == EOF))) {
1086: break;
1087: }
1088: ctold++;
1089: ctnew++;
1.64 xsa 1090: if ((bflag == 1) && isspace(c) && isspace(d)) {
1.1 jfb 1091: do {
1092: if (c == '\n')
1093: break;
1094: ctold++;
1095: } while (isspace(c = getc(f1)));
1096: do {
1097: if (d == '\n')
1098: break;
1099: ctnew++;
1100: } while (isspace(d = getc(f2)));
1.64 xsa 1101: } else if (wflag == 1) {
1.1 jfb 1102: while (isspace(c) && c != '\n') {
1103: c = getc(f1);
1104: ctold++;
1105: }
1106: while (isspace(d) && d != '\n') {
1107: d = getc(f2);
1108: ctnew++;
1109: }
1110: }
1111: if (chrtran[c] != chrtran[d]) {
1112: jackpot++;
1113: J[i] = 0;
1.65 xsa 1114: if ((c != '\n') && (c != EOF))
1.1 jfb 1115: ctold += skipline(f1);
1.65 xsa 1116: if ((d != '\n') && (c != EOF))
1.1 jfb 1117: ctnew += skipline(f2);
1118: break;
1119: }
1.65 xsa 1120: if ((c == '\n') || (c == EOF))
1.1 jfb 1121: break;
1122: }
1123: } else {
1124: for (;;) {
1125: ctold++;
1126: ctnew++;
1127: if ((c = getc(f1)) != (d = getc(f2))) {
1128: /* jackpot++; */
1129: J[i] = 0;
1.65 xsa 1130: if ((c != '\n') && (c != EOF))
1.1 jfb 1131: ctold += skipline(f1);
1.65 xsa 1132: if ((d != '\n') && (c != EOF))
1.1 jfb 1133: ctnew += skipline(f2);
1134: break;
1135: }
1.65 xsa 1136: if ((c == '\n') || (c == EOF))
1.1 jfb 1137: break;
1138: }
1139: }
1140: ixold[i] = ctold;
1141: ixnew[j] = ctnew;
1142: j++;
1143: }
1.36 jfb 1144: for (; j <= diff_len[1]; j++)
1.1 jfb 1145: ixnew[j] = ctnew += skipline(f2);
1146: /*
1.65 xsa 1147: * if (jackpot != 0)
1.42 joris 1148: * cvs_printf("jackpot\n");
1.1 jfb 1149: */
1150: }
1151:
1152: /* shellsort CACM #201 */
1153: static void
1154: sort(struct line *a, int n)
1155: {
1156: struct line *ai, *aim, w;
1157: int j, m = 0, k;
1158:
1159: if (n == 0)
1160: return;
1161: for (j = 1; j <= n; j *= 2)
1162: m = 2 * j - 1;
1163: for (m /= 2; m != 0; m /= 2) {
1164: k = n - m;
1165: for (j = 1; j <= k; j++) {
1166: for (ai = &a[j]; ai > a; ai -= m) {
1167: aim = &ai[m];
1168: if (aim < ai)
1169: break; /* wraparound */
1170: if (aim->value > ai[0].value ||
1171: (aim->value == ai[0].value &&
1172: aim->serial > ai[0].serial))
1173: break;
1174: w.value = ai[0].value;
1175: ai[0].value = aim->value;
1176: aim->value = w.value;
1177: w.serial = ai[0].serial;
1178: ai[0].serial = aim->serial;
1179: aim->serial = w.serial;
1180: }
1181: }
1182: }
1183: }
1184:
1185: static void
1186: unsort(struct line *f, int l, int *b)
1187: {
1188: int *a, i;
1189:
1.71 ! joris 1190: a = (int *)xmalloc((l + 1) * sizeof(int));
1.1 jfb 1191: for (i = 1; i <= l; i++)
1192: a[f[i].serial] = f[i].value;
1193: for (i = 1; i <= l; i++)
1194: b[i] = a[i];
1.71 ! joris 1195: xfree(a);
1.1 jfb 1196: }
1197:
1198: static int
1199: skipline(FILE *f)
1200: {
1201: int i, c;
1202:
1203: for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
1204: continue;
1205: return (i);
1206: }
1207:
1208: static void
1209: output(const char *file1, FILE *f1, const char *file2, FILE *f2)
1210: {
1211: int m, i0, i1, j0, j1;
1212:
1213: rewind(f1);
1214: rewind(f2);
1.36 jfb 1215: m = diff_len[0];
1.1 jfb 1216: J[0] = 0;
1.36 jfb 1217: J[m + 1] = diff_len[1] + 1;
1.1 jfb 1218: for (i0 = 1; i0 <= m; i0 = i1 + 1) {
1219: while (i0 <= m && J[i0] == J[i0 - 1] + 1)
1220: i0++;
1221: j0 = J[i0 - 1] + 1;
1222: i1 = i0 - 1;
1223: while (i1 < m && J[i1 + 1] == 0)
1224: i1++;
1225: j1 = J[i1 + 1] - 1;
1226: J[i1] = j1;
1227: change(file1, f1, file2, f2, i0, i1, j0, j1);
1228: }
1229: if (m == 0)
1.36 jfb 1230: change(file1, f1, file2, f2, 1, 0, 1, diff_len[1]);
1.58 niallo 1231: if (diff_format == D_IFDEF) {
1.1 jfb 1232: for (;;) {
1233: #define c i0
1234: if ((c = getc(f1)) == EOF)
1235: return;
1.58 niallo 1236: diff_output("%c", c);
1.1 jfb 1237: }
1238: #undef c
1239: }
1240: if (anychange != 0) {
1.58 niallo 1241: if (diff_format == D_CONTEXT)
1.1 jfb 1242: dump_context_vec(f1, f2);
1.58 niallo 1243: else if (diff_format == D_UNIFIED)
1.1 jfb 1244: dump_unified_vec(f1, f2);
1245: }
1246: }
1247:
1248: static __inline void
1249: range(int a, int b, char *separator)
1250: {
1.58 niallo 1251: diff_output("%d", a > b ? b : a);
1.1 jfb 1252: if (a < b)
1.58 niallo 1253: diff_output("%s%d", separator, b);
1.1 jfb 1254: }
1255:
1256: static __inline void
1257: uni_range(int a, int b)
1258: {
1259: if (a < b)
1.58 niallo 1260: diff_output("%d,%d", a, b - a + 1);
1.1 jfb 1261: else if (a == b)
1.58 niallo 1262: diff_output("%d", b);
1.1 jfb 1263: else
1.58 niallo 1264: diff_output("%d,0", b);
1.1 jfb 1265: }
1266:
1267: static char *
1.15 jfb 1268: preadline(int fd, size_t rlen, off_t off)
1.1 jfb 1269: {
1270: char *line;
1271: ssize_t nr;
1272:
1.71 ! joris 1273: line = xmalloc(rlen + 1);
1.15 jfb 1274: if ((nr = pread(fd, line, rlen, off)) < 0) {
1.1 jfb 1275: cvs_log(LP_ERRNO, "preadline failed");
1276: return (NULL);
1277: }
1278: line[nr] = '\0';
1279: return (line);
1280: }
1281:
1282: static int
1283: ignoreline(char *line)
1284: {
1285: int ret;
1286:
1.55 xsa 1287: ret = regexec(&ignore_re, line, (size_t)0, NULL, 0);
1.71 ! joris 1288: xfree(line);
1.1 jfb 1289: return (ret == 0); /* if it matched, it should be ignored. */
1290: }
1291:
1292: /*
1293: * Indicate that there is a difference between lines a and b of the from file
1294: * to get to lines c to d of the to file. If a is greater then b then there
1295: * are no lines in the from file involved and this means that there were
1296: * lines appended (beginning at b). If c is greater than d then there are
1297: * lines missing from the to file.
1298: */
1299: static void
1300: change(const char *file1, FILE *f1, const char *file2, FILE *f2,
1301: int a, int b, int c, int d)
1302: {
1303: static size_t max_context = 64;
1304: int i;
1305:
1.58 niallo 1306: if (diff_format != D_IFDEF && a > b && c > d)
1.1 jfb 1307: return;
1308: if (ignore_pats != NULL) {
1309: char *line;
1310: /*
1311: * All lines in the change, insert, or delete must
1312: * match an ignore pattern for the change to be
1313: * ignored.
1314: */
1315: if (a <= b) { /* Changes and deletes. */
1316: for (i = a; i <= b; i++) {
1317: line = preadline(fileno(f1),
1318: ixold[i] - ixold[i - 1], ixold[i - 1]);
1319: if (!ignoreline(line))
1320: goto proceed;
1321: }
1322: }
1.65 xsa 1323: if ((a > b) || (c <= d)) { /* Changes and inserts. */
1.1 jfb 1324: for (i = c; i <= d; i++) {
1325: line = preadline(fileno(f2),
1326: ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
1327: if (!ignoreline(line))
1328: goto proceed;
1329: }
1330: }
1331: return;
1332: }
1333: proceed:
1.58 niallo 1334: if (diff_format == D_CONTEXT || diff_format == D_UNIFIED) {
1.1 jfb 1335: /*
1336: * Allocate change records as needed.
1337: */
1338: if (context_vec_ptr == context_vec_end - 1) {
1.49 niallo 1339: struct context_vec *tmp;
1.1 jfb 1340: ptrdiff_t offset = context_vec_ptr - context_vec_start;
1341: max_context <<= 1;
1.71 ! joris 1342: tmp = xrealloc(context_vec_start, max_context *
! 1343: sizeof(struct context_vec));
1.49 niallo 1344: context_vec_start = tmp;
1.1 jfb 1345: context_vec_end = context_vec_start + max_context;
1346: context_vec_ptr = context_vec_start + offset;
1347: }
1348: if (anychange == 0) {
1349: /*
1350: * Print the context/unidiff header first time through.
1351: */
1.58 niallo 1352: diff_output("%s %s %s",
1353: diff_format == D_CONTEXT ? "***" : "---", diff_file,
1.1 jfb 1354: ctime(&stb1.st_mtime));
1.58 niallo 1355: diff_output("%s %s %s",
1356: diff_format == D_CONTEXT ? "---" : "+++", diff_file,
1.1 jfb 1357: ctime(&stb2.st_mtime));
1358: anychange = 1;
1359: } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
1360: c > context_vec_ptr->d + (2 * context) + 1) {
1361: /*
1362: * If this change is more than 'context' lines from the
1363: * previous change, dump the record and reset it.
1364: */
1.58 niallo 1365: if (diff_format == D_CONTEXT)
1.1 jfb 1366: dump_context_vec(f1, f2);
1367: else
1368: dump_unified_vec(f1, f2);
1369: }
1370: context_vec_ptr++;
1371: context_vec_ptr->a = a;
1372: context_vec_ptr->b = b;
1373: context_vec_ptr->c = c;
1374: context_vec_ptr->d = d;
1375: return;
1376: }
1377: if (anychange == 0)
1378: anychange = 1;
1.58 niallo 1379: switch (diff_format) {
1.1 jfb 1380: case D_BRIEF:
1381: return;
1382: case D_NORMAL:
1383: range(a, b, ",");
1.58 niallo 1384: diff_output("%c", a > b ? 'a' : c > d ? 'd' : 'c');
1385: if (diff_format == D_NORMAL)
1.1 jfb 1386: range(c, d, ",");
1.58 niallo 1387: diff_output("\n");
1.1 jfb 1388: break;
1.33 jfb 1389: case D_RCSDIFF:
1390: if (a > b)
1.58 niallo 1391: diff_output("a%d %d\n", b, d - c + 1);
1.33 jfb 1392: else {
1.58 niallo 1393: diff_output("d%d %d\n", a, b - a + 1);
1.33 jfb 1394:
1395: if (!(c > d)) /* add changed lines */
1.58 niallo 1396: diff_output("a%d %d\n", b, d - c + 1);
1.33 jfb 1397: }
1398: break;
1.1 jfb 1399: }
1.58 niallo 1400: if (diff_format == D_NORMAL || diff_format == D_IFDEF) {
1.1 jfb 1401: fetch(ixold, a, b, f1, '<', 1);
1.58 niallo 1402: if (a <= b && c <= d && diff_format == D_NORMAL)
1.60 joris 1403: diff_output("---\n");
1.1 jfb 1404: }
1.58 niallo 1405: i = fetch(ixnew, c, d, f2, diff_format == D_NORMAL ? '>' : '\0', 0);
1.1 jfb 1406: if (inifdef) {
1.58 niallo 1407: diff_output("#endif /* %s */\n", ifdefname);
1.1 jfb 1408: inifdef = 0;
1409: }
1410: }
1411:
1412: static int
1413: fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile)
1414: {
1415: int i, j, c, lastc, col, nc;
1416:
1417: /*
1418: * When doing #ifdef's, copy down to current line
1419: * if this is the first file, so that stuff makes it to output.
1420: */
1.58 niallo 1421: if (diff_format == D_IFDEF && oldfile) {
1.1 jfb 1422: long curpos = ftell(lb);
1423: /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1424: nc = f[a > b ? b : a - 1] - curpos;
1425: for (i = 0; i < nc; i++)
1.58 niallo 1426: diff_output("%c", getc(lb));
1.1 jfb 1427: }
1428: if (a > b)
1429: return (0);
1.58 niallo 1430: if (diff_format == D_IFDEF) {
1.1 jfb 1431: if (inifdef) {
1.58 niallo 1432: diff_output("#else /* %s%s */\n",
1.1 jfb 1433: oldfile == 1 ? "!" : "", ifdefname);
1434: } else {
1435: if (oldfile)
1.58 niallo 1436: diff_output("#ifndef %s\n", ifdefname);
1.1 jfb 1437: else
1.58 niallo 1438: diff_output("#ifdef %s\n", ifdefname);
1.1 jfb 1439: }
1440: inifdef = 1 + oldfile;
1441: }
1442: for (i = a; i <= b; i++) {
1443: fseek(lb, f[i - 1], SEEK_SET);
1444: nc = f[i] - f[i - 1];
1.58 niallo 1445: if (diff_format != D_IFDEF && ch != '\0') {
1446: diff_output("%c", ch);
1.64 xsa 1447: if ((Tflag == 1 ) && (diff_format == D_NORMAL ||
1.59 joris 1448: diff_format == D_CONTEXT ||
1449: diff_format == D_UNIFIED))
1.58 niallo 1450: diff_output("\t");
1451: else if (diff_format != D_UNIFIED)
1452: diff_output(" ");
1.1 jfb 1453: }
1454: col = 0;
1455: for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
1456: if ((c = getc(lb)) == EOF) {
1.58 niallo 1457: if (diff_format == D_RCSDIFF)
1.33 jfb 1458: warnx("No newline at end of file");
1459: else
1.59 joris 1460: diff_output("\n\\ No newline at end of "
1461: "file");
1.1 jfb 1462: return (0);
1463: }
1.64 xsa 1464: if ((c == '\t') && (tflag == 1)) {
1.1 jfb 1465: do {
1.58 niallo 1466: diff_output(" ");
1.1 jfb 1467: } while (++col & 7);
1468: } else {
1.58 niallo 1469: diff_output("%c", c);
1.1 jfb 1470: col++;
1471: }
1472: }
1473: }
1474: return (0);
1475: }
1476:
1477: /*
1478: * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
1479: */
1480: static int
1481: readhash(FILE *f)
1482: {
1483: int i, t, space;
1484: int sum;
1485:
1486: sum = 1;
1487: space = 0;
1.64 xsa 1488: if ((bflag != 1) && (wflag != 1)) {
1489: if (iflag == 1)
1.1 jfb 1490: for (i = 0; (t = getc(f)) != '\n'; i++) {
1491: if (t == EOF) {
1492: if (i == 0)
1493: return (0);
1494: break;
1495: }
1496: sum = sum * 127 + chrtran[t];
1497: }
1498: else
1499: for (i = 0; (t = getc(f)) != '\n'; i++) {
1500: if (t == EOF) {
1501: if (i == 0)
1502: return (0);
1503: break;
1504: }
1505: sum = sum * 127 + t;
1506: }
1507: } else {
1508: for (i = 0;;) {
1509: switch (t = getc(f)) {
1510: case '\t':
1511: case ' ':
1512: space++;
1513: continue;
1514: default:
1.64 xsa 1515: if ((space != 0) && (wflag != 1)) {
1.1 jfb 1516: i++;
1517: space = 0;
1518: }
1519: sum = sum * 127 + chrtran[t];
1520: i++;
1521: continue;
1522: case EOF:
1523: if (i == 0)
1524: return (0);
1525: /* FALLTHROUGH */
1526: case '\n':
1527: break;
1528: }
1529: break;
1530: }
1531: }
1532: /*
1533: * There is a remote possibility that we end up with a zero sum.
1534: * Zero is used as an EOF marker, so return 1 instead.
1535: */
1536: return (sum == 0 ? 1 : sum);
1537: }
1538:
1539: static int
1540: asciifile(FILE *f)
1541: {
1542: char buf[BUFSIZ];
1543: int i, cnt;
1544:
1.64 xsa 1545: if ((aflag == 1) || (f == NULL))
1.1 jfb 1546: return (1);
1547:
1548: rewind(f);
1.55 xsa 1549: cnt = fread(buf, (size_t)1, sizeof(buf), f);
1.1 jfb 1550: for (i = 0; i < cnt; i++)
1551: if (!isprint(buf[i]) && !isspace(buf[i]))
1552: return (0);
1553: return (1);
1554: }
1555:
1.17 jfb 1556: static char*
1557: match_function(const long *f, int pos, FILE *fp)
1558: {
1559: unsigned char buf[FUNCTION_CONTEXT_SIZE];
1560: size_t nc;
1561: int last = lastline;
1562: char *p;
1563:
1564: lastline = pos;
1565: while (pos > last) {
1566: fseek(fp, f[pos - 1], SEEK_SET);
1567: nc = f[pos] - f[pos - 1];
1568: if (nc >= sizeof(buf))
1569: nc = sizeof(buf) - 1;
1.55 xsa 1570: nc = fread(buf, (size_t)1, nc, fp);
1.17 jfb 1571: if (nc > 0) {
1572: buf[nc] = '\0';
1.44 niallo 1573: p = strchr((const char *)buf, '\n');
1.17 jfb 1574: if (p != NULL)
1575: *p = '\0';
1576: if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
1.57 reyk 1577: strlcpy(lastbuf, (const char *)buf,
1578: sizeof lastbuf);
1.17 jfb 1579: lastmatchline = pos;
1580: return lastbuf;
1581: }
1582: }
1583: pos--;
1584: }
1585: return (lastmatchline > 0) ? lastbuf : NULL;
1586: }
1587:
1.1 jfb 1588:
1589: /* dump accumulated "context" diff changes */
1590: static void
1591: dump_context_vec(FILE *f1, FILE *f2)
1592: {
1593: struct context_vec *cvp = context_vec_start;
1594: int lowa, upb, lowc, upd, do_output;
1595: int a, b, c, d;
1.17 jfb 1596: char ch, *f;
1.1 jfb 1597:
1598: if (context_vec_start > context_vec_ptr)
1599: return;
1600:
1601: b = d = 0; /* gcc */
1.4 jfb 1602: lowa = MAX(1, cvp->a - context);
1.36 jfb 1603: upb = MIN(diff_len[0], context_vec_ptr->b + context);
1.4 jfb 1604: lowc = MAX(1, cvp->c - context);
1.36 jfb 1605: upd = MIN(diff_len[1], context_vec_ptr->d + context);
1.1 jfb 1606:
1.58 niallo 1607: diff_output("***************");
1.64 xsa 1608: if (pflag == 1) {
1.17 jfb 1609: f = match_function(ixold, lowa - 1, f1);
1610: if (f != NULL) {
1.58 niallo 1611: diff_output(" ");
1612: diff_output("%s", f);
1.17 jfb 1613: }
1614: }
1.58 niallo 1615: diff_output("\n*** ");
1.1 jfb 1616: range(lowa, upb, ",");
1.58 niallo 1617: diff_output(" ****\n");
1.1 jfb 1618:
1619: /*
1620: * Output changes to the "old" file. The first loop suppresses
1621: * output if there were no changes to the "old" file (we'll see
1622: * the "old" lines as context in the "new" list).
1623: */
1624: do_output = 0;
1625: for (; cvp <= context_vec_ptr; cvp++)
1626: if (cvp->a <= cvp->b) {
1627: cvp = context_vec_start;
1628: do_output++;
1629: break;
1630: }
1.65 xsa 1631: if (do_output != 0) {
1.1 jfb 1632: while (cvp <= context_vec_ptr) {
1633: a = cvp->a;
1634: b = cvp->b;
1635: c = cvp->c;
1636: d = cvp->d;
1637:
1638: if (a <= b && c <= d)
1639: ch = 'c';
1640: else
1641: ch = (a <= b) ? 'd' : 'a';
1642:
1643: if (ch == 'a')
1644: fetch(ixold, lowa, b, f1, ' ', 0);
1645: else {
1646: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1647: fetch(ixold, a, b, f1,
1648: ch == 'c' ? '!' : '-', 0);
1649: }
1650: lowa = b + 1;
1651: cvp++;
1652: }
1653: fetch(ixold, b + 1, upb, f1, ' ', 0);
1654: }
1655: /* output changes to the "new" file */
1.58 niallo 1656: diff_output("--- ");
1.1 jfb 1657: range(lowc, upd, ",");
1.58 niallo 1658: diff_output(" ----\n");
1.1 jfb 1659:
1660: do_output = 0;
1661: for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
1662: if (cvp->c <= cvp->d) {
1663: cvp = context_vec_start;
1664: do_output++;
1665: break;
1666: }
1.65 xsa 1667: if (do_output != 0) {
1.1 jfb 1668: while (cvp <= context_vec_ptr) {
1669: a = cvp->a;
1670: b = cvp->b;
1671: c = cvp->c;
1672: d = cvp->d;
1673:
1674: if (a <= b && c <= d)
1675: ch = 'c';
1676: else
1677: ch = (a <= b) ? 'd' : 'a';
1678:
1679: if (ch == 'd')
1680: fetch(ixnew, lowc, d, f2, ' ', 0);
1681: else {
1682: fetch(ixnew, lowc, c - 1, f2, ' ', 0);
1683: fetch(ixnew, c, d, f2,
1684: ch == 'c' ? '!' : '+', 0);
1685: }
1686: lowc = d + 1;
1687: cvp++;
1688: }
1689: fetch(ixnew, d + 1, upd, f2, ' ', 0);
1690: }
1691: context_vec_ptr = context_vec_start - 1;
1692: }
1693:
1694: /* dump accumulated "unified" diff changes */
1695: static void
1696: dump_unified_vec(FILE *f1, FILE *f2)
1697: {
1698: struct context_vec *cvp = context_vec_start;
1699: int lowa, upb, lowc, upd;
1700: int a, b, c, d;
1.17 jfb 1701: char ch, *f;
1.1 jfb 1702:
1703: if (context_vec_start > context_vec_ptr)
1704: return;
1705:
1706: b = d = 0; /* gcc */
1.4 jfb 1707: lowa = MAX(1, cvp->a - context);
1.36 jfb 1708: upb = MIN(diff_len[0], context_vec_ptr->b + context);
1.4 jfb 1709: lowc = MAX(1, cvp->c - context);
1.36 jfb 1710: upd = MIN(diff_len[1], context_vec_ptr->d + context);
1.1 jfb 1711:
1.58 niallo 1712: diff_output("@@ -");
1.1 jfb 1713: uni_range(lowa, upb);
1.58 niallo 1714: diff_output(" +");
1.1 jfb 1715: uni_range(lowc, upd);
1.58 niallo 1716: diff_output(" @@");
1.64 xsa 1717: if (pflag == 1) {
1.17 jfb 1718: f = match_function(ixold, lowa - 1, f1);
1719: if (f != NULL) {
1.58 niallo 1720: diff_output(" ");
1721: diff_output("%s", f);
1.17 jfb 1722: }
1723: }
1.58 niallo 1724: diff_output("\n");
1.1 jfb 1725:
1726: /*
1727: * Output changes in "unified" diff format--the old and new lines
1728: * are printed together.
1729: */
1730: for (; cvp <= context_vec_ptr; cvp++) {
1731: a = cvp->a;
1732: b = cvp->b;
1733: c = cvp->c;
1734: d = cvp->d;
1735:
1736: /*
1737: * c: both new and old changes
1738: * d: only changes in the old file
1739: * a: only changes in the new file
1740: */
1741: if (a <= b && c <= d)
1742: ch = 'c';
1743: else
1744: ch = (a <= b) ? 'd' : 'a';
1745:
1746: switch (ch) {
1747: case 'c':
1748: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1749: fetch(ixold, a, b, f1, '-', 0);
1750: fetch(ixnew, c, d, f2, '+', 0);
1751: break;
1752: case 'd':
1753: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1754: fetch(ixold, a, b, f1, '-', 0);
1755: break;
1756: case 'a':
1757: fetch(ixnew, lowc, c - 1, f2, ' ', 0);
1758: fetch(ixnew, c, d, f2, '+', 0);
1759: break;
1760: }
1761: lowa = b + 1;
1762: lowc = d + 1;
1763: }
1764: fetch(ixnew, d + 1, upd, f2, ' ', 0);
1765:
1766: context_vec_ptr = context_vec_start - 1;
1.58 niallo 1767: }
1768:
1.63 joris 1769: void
1.58 niallo 1770: diff_output(const char *fmt, ...)
1771: {
1772: va_list vap;
1773: char *str;
1774:
1775: va_start(vap, fmt);
1776: vasprintf(&str, fmt, vap);
1777: if (diffbuf != NULL)
1778: cvs_buf_append(diffbuf, str, strlen(str));
1779: else
1780: cvs_printf("%s", str);
1.71 ! joris 1781: xfree(str);
1.58 niallo 1782: va_end(vap);
1.1 jfb 1783: }