Annotation of src/usr.bin/cvs/diff.c, Revision 1.67
1.67 ! xsa 1: /* $OpenBSD: diff.c,v 1.66 2005/11/21 15:01:10 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.48 joris 220: static int context;
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.23 joris 360: dap = (struct diff_arg *)malloc(sizeof(*dap));
361: if (dap == NULL)
1.28 joris 362: return (CVS_EX_DATA);
1.23 joris 363: dap->date1 = dap->date2 = dap->rev1 = dap->rev2 = NULL;
1.2 jfb 364: strlcpy(diffargs, argv[0], sizeof(diffargs));
1.1 jfb 365:
1.36 jfb 366: while ((ch = getopt(argc, argv, cmd->cmd_opts)) != -1) {
1.1 jfb 367: switch (ch) {
368: case 'c':
1.2 jfb 369: strlcat(diffargs, " -c", sizeof(diffargs));
1.58 niallo 370: diff_format = D_CONTEXT;
1.1 jfb 371: break;
372: case 'D':
1.23 joris 373: if (dap->date1 == NULL && dap->rev1 == NULL) {
374: dap->date1 = optarg;
375: } else if (dap->date2 == NULL && dap->rev2 == NULL) {
376: dap->date2 = optarg;
377: } else {
1.1 jfb 378: cvs_log(LP_ERR,
379: "no more than two revisions/dates can "
380: "be specified");
381: }
382: break;
383: case 'l':
1.2 jfb 384: strlcat(diffargs, " -l", sizeof(diffargs));
1.1 jfb 385: recurse = 0;
1.36 jfb 386: cvs_cmd_diff.file_flags &= ~CF_RECURSE;
1.1 jfb 387: break;
388: case 'i':
1.2 jfb 389: strlcat(diffargs, " -i", sizeof(diffargs));
1.1 jfb 390: iflag = 1;
391: break;
1.8 jfb 392: case 'N':
393: strlcat(diffargs, " -N", sizeof(diffargs));
394: Nflag = 1;
395: break;
1.33 jfb 396: case 'n':
397: strlcat(diffargs, " -n", sizeof(diffargs));
1.58 niallo 398: diff_format = D_RCSDIFF;
1.33 jfb 399: break;
1.17 jfb 400: case 'p':
401: strlcat(diffargs, " -p", sizeof(diffargs));
402: pflag = 1;
403: break;
1.1 jfb 404: case 'r':
1.23 joris 405: if ((dap->rev1 == NULL) && (dap->date1 == NULL)) {
406: dap->rev1 = optarg;
407: } else if ((dap->rev2 == NULL) &&
408: (dap->date2 == NULL)) {
409: dap->rev2 = optarg;
410: } else {
1.1 jfb 411: cvs_log(LP_ERR,
412: "no more than two revisions/dates can "
413: "be specified");
1.28 joris 414: return (CVS_EX_USAGE);
1.1 jfb 415: }
1.32 joris 416: break;
417: case 'R':
1.36 jfb 418: cvs_cmd_diff.file_flags |= CF_RECURSE;
1.1 jfb 419: break;
420: case 'u':
1.2 jfb 421: strlcat(diffargs, " -u", sizeof(diffargs));
1.58 niallo 422: diff_format = D_UNIFIED;
1.1 jfb 423: break;
424: default:
1.28 joris 425: return (CVS_EX_USAGE);
1.1 jfb 426: }
427: }
428:
1.23 joris 429: *arg = optind;
430: return (0);
431: }
1.1 jfb 432:
1.23 joris 433: int
434: cvs_diff_cleanup(void)
435: {
1.36 jfb 436: if (dap != NULL) {
1.23 joris 437: free(dap);
1.36 jfb 438: dap = NULL;
439: }
1.4 jfb 440: return (0);
441: }
1.1 jfb 442:
1.4 jfb 443: /*
1.36 jfb 444: * cvs_diff_pre_exec()
1.4 jfb 445: *
446: */
447: int
1.36 jfb 448: cvs_diff_pre_exec(struct cvsroot *root)
1.4 jfb 449: {
1.36 jfb 450: if (root->cr_method != CVS_METHOD_LOCAL) {
451: /* send the flags */
1.64 xsa 452: if ((Nflag == 1) && (cvs_sendarg(root, "-N", 0) < 0))
1.36 jfb 453: return (CVS_EX_PROTO);
1.64 xsa 454: if ((pflag ==1) && (cvs_sendarg(root, "-p", 0) < 0))
1.36 jfb 455: return (CVS_EX_PROTO);
456:
1.58 niallo 457: if (diff_format == D_CONTEXT) {
1.38 xsa 458: if (cvs_sendarg(root, "-c", 0) < 0)
459: return (CVS_EX_PROTO);
1.58 niallo 460: } else if (diff_format == D_UNIFIED) {
1.38 xsa 461: if (cvs_sendarg(root, "-u", 0) < 0)
462: return (CVS_EX_PROTO);
463: }
1.17 jfb 464:
1.36 jfb 465: if (dap->rev1 != NULL) {
1.38 xsa 466: if ((cvs_sendarg(root, "-r", 0) < 0) ||
467: (cvs_sendarg(root, dap->rev1, 0) < 0))
468: return (CVS_EX_PROTO);
1.36 jfb 469: } else if (dap->date1 != NULL) {
1.38 xsa 470: if ((cvs_sendarg(root, "-D", 0) < 0) ||
471: (cvs_sendarg(root, dap->date1, 0) < 0))
472: return (CVS_EX_PROTO);
1.36 jfb 473: }
474: if (dap->rev2 != NULL) {
1.38 xsa 475: if ((cvs_sendarg(root, "-r", 0) < 0) ||
476: (cvs_sendarg(root, dap->rev2, 0) < 0))
477: return (CVS_EX_PROTO);
1.36 jfb 478: } else if (dap->date2 != NULL) {
1.38 xsa 479: if ((cvs_sendarg(root, "-D", 0) < 0) ||
480: (cvs_sendarg(root, dap->date2, 0) < 0))
481: return (CVS_EX_PROTO);
1.36 jfb 482: }
1.1 jfb 483: }
484:
485: return (0);
486: }
487:
488:
489: /*
490: * cvs_diff_file()
491: *
492: * Diff a single file.
493: */
1.36 jfb 494: static int
495: cvs_diff_remote(struct cvs_file *cfp, void *arg)
1.1 jfb 496: {
1.36 jfb 497: char *dir, *repo;
498: char fpath[MAXPATHLEN], dfpath[MAXPATHLEN];
1.4 jfb 499: struct cvsroot *root;
1.1 jfb 500:
1.4 jfb 501: if (cfp->cf_type == DT_DIR) {
1.7 jfb 502: if (cfp->cf_cvstat == CVS_FST_UNKNOWN) {
1.31 jfb 503: root = cfp->cf_parent->cf_root;
1.52 joris 504: cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cfp->cf_name);
1.10 deraadt 505: } else {
1.31 jfb 506: root = cfp->cf_root;
1.23 joris 507: #if 0
1.7 jfb 508: if ((cfp->cf_parent == NULL) ||
1.31 jfb 509: (root != cfp->cf_parent->cf_root)) {
1.7 jfb 510: cvs_connect(root);
1.36 jfb 511: cvs_diff_pre_exec(root);
1.7 jfb 512: }
1.23 joris 513: #endif
1.7 jfb 514:
515: cvs_senddir(root, cfp);
1.4 jfb 516: }
1.1 jfb 517:
1.13 jfb 518: return (0);
519: }
520:
521: if (cfp->cf_cvstat == CVS_FST_LOST) {
1.52 joris 522: cvs_log(LP_WARN, "cannot find file %s", cfp->cf_name);
1.4 jfb 523: return (0);
1.1 jfb 524: }
525:
1.8 jfb 526: diff_file = cvs_file_getpath(cfp, fpath, sizeof(fpath));
1.7 jfb 527:
1.4 jfb 528: if (cfp->cf_parent != NULL) {
1.8 jfb 529: dir = cvs_file_getpath(cfp->cf_parent, dfpath, sizeof(dfpath));
1.31 jfb 530: root = cfp->cf_parent->cf_root;
531: repo = cfp->cf_parent->cf_repo;
1.10 deraadt 532: } else {
1.4 jfb 533: dir = ".";
1.7 jfb 534: root = NULL;
1.4 jfb 535: repo = NULL;
1.1 jfb 536: }
537:
1.4 jfb 538: if (cfp->cf_cvstat == CVS_FST_UNKNOWN) {
1.36 jfb 539: cvs_sendreq(root, CVS_REQ_QUESTIONABLE, cfp->cf_name);
1.4 jfb 540: return (0);
1.1 jfb 541: }
542:
1.36 jfb 543: if (cvs_sendentry(root, cfp) < 0)
544: return (CVS_EX_PROTO);
1.1 jfb 545:
1.4 jfb 546: if (cfp->cf_cvstat == CVS_FST_UPTODATE) {
1.36 jfb 547: cvs_sendreq(root, CVS_REQ_UNCHANGED, cfp->cf_name);
1.1 jfb 548: return (0);
549: }
550:
551: /* at this point, the file is modified */
1.36 jfb 552: if ((cvs_sendreq(root, CVS_REQ_MODIFIED, cfp->cf_name) < 0) ||
553: (cvs_sendfile(root, diff_file) < 0))
554: return (CVS_EX_PROTO);
555:
556: return (0);
557: }
558:
559: static int
560: cvs_diff_local(CVSFILE *cf, void *arg)
561: {
562: int len;
563: char *repo, buf[64];
564: char fpath[MAXPATHLEN], rcspath[MAXPATHLEN];
565: char path_tmp1[MAXPATHLEN], path_tmp2[MAXPATHLEN];
566: BUF *b1, *b2;
567: RCSNUM *r1, *r2;
568: RCSFILE *rf;
569: struct cvsroot *root;
1.1 jfb 570:
1.36 jfb 571: rf = NULL;
572: root = CVS_DIR_ROOT(cf);
573: repo = CVS_DIR_REPO(cf);
574: diff_file = cvs_file_getpath(cf, fpath, sizeof(fpath));
1.1 jfb 575:
1.36 jfb 576: if (cf->cf_type == DT_DIR) {
1.46 xsa 577: if (verbosity > 1)
1.54 xsa 578: cvs_log(LP_NOTICE, "Diffing %s", fpath);
1.36 jfb 579: return (0);
580: }
1.1 jfb 581:
1.36 jfb 582: if (cf->cf_cvstat == CVS_FST_LOST) {
583: cvs_log(LP_WARN, "cannot find file %s", cf->cf_name);
584: return (0);
585: }
1.1 jfb 586:
1.36 jfb 587: if (cf->cf_cvstat == CVS_FST_UNKNOWN) {
588: cvs_log(LP_WARN, "I know nothing about %s", diff_file);
589: return (0);
1.45 niallo 590: } else if (cf->cf_cvstat == CVS_FST_UPTODATE)
1.36 jfb 591: return (0);
1.1 jfb 592:
1.36 jfb 593: /* at this point, the file is modified */
594: len = snprintf(rcspath, sizeof(rcspath), "%s/%s/%s%s",
595: root->cr_dir, repo, diff_file, RCS_FILE_EXT);
596: if (len == -1 || len >= (int)sizeof(rcspath)) {
597: errno = ENAMETOOLONG;
598: cvs_log(LP_ERRNO, "%s", rcspath);
599: return (CVS_EX_DATA);
600: }
1.20 jfb 601:
1.36 jfb 602: rf = rcs_open(rcspath, RCS_READ);
603: if (rf == NULL) {
604: return (CVS_EX_DATA);
605: }
1.1 jfb 606:
1.36 jfb 607: cvs_printf("Index: %s\n%s\nRCS file: %s\n", diff_file,
608: RCS_DIFF_DIV, rcspath);
1.4 jfb 609:
1.36 jfb 610: if (dap->rev1 == NULL)
611: r1 = cf->cf_lrev;
612: else {
613: if ((r1 = rcsnum_parse(dap->rev1)) == NULL) {
1.62 joris 614: rcs_close(rf);
1.28 joris 615: return (CVS_EX_DATA);
1.20 jfb 616: }
1.36 jfb 617: }
618:
619: cvs_printf("retrieving revision %s\n",
620: rcsnum_tostr(r1, buf, sizeof(buf)));
621: b1 = rcs_getrev(rf, r1);
622:
1.39 joris 623: if (b1 == NULL) {
1.40 xsa 624: cvs_log(LP_ERR, "failed to retrieve revision %s\n",
1.39 joris 625: rcsnum_tostr(r1, buf, sizeof(buf)));
626: if (r1 != cf->cf_lrev)
627: rcsnum_free(r1);
1.62 joris 628: rcs_close(rf);
1.39 joris 629: return (CVS_EX_DATA);
630: }
631:
1.36 jfb 632: if (r1 != cf->cf_lrev)
633: rcsnum_free(r1);
1.20 jfb 634:
1.36 jfb 635: if (dap->rev2 != NULL) {
636: cvs_printf("retrieving revision %s\n", dap->rev2);
637: if ((r2 = rcsnum_parse(dap->rev2)) == NULL) {
1.62 joris 638: rcs_close(rf);
1.28 joris 639: return (CVS_EX_DATA);
1.12 djm 640: }
1.36 jfb 641: b2 = rcs_getrev(rf, r2);
642: rcsnum_free(r2);
643: } else {
644: b2 = cvs_buf_load(diff_file, BUF_AUTOEXT);
645: }
646:
647: rcs_close(rf);
1.39 joris 648:
649: if (b2 == NULL) {
1.40 xsa 650: cvs_log(LP_ERR, "failed to retrieve revision %s\n",
1.39 joris 651: dap->rev2);
652: cvs_buf_free(b1);
653: return (CVS_EX_DATA);
654: }
1.36 jfb 655:
1.42 joris 656: cvs_printf("%s", diffargs);
657: cvs_printf(" -r%s", buf);
1.36 jfb 658: if (dap->rev2 != NULL)
1.42 joris 659: cvs_printf(" -r%s", dap->rev2);
660: cvs_printf(" %s\n", diff_file);
1.67 ! xsa 661: strlcpy(path_tmp1, cvs_tmpdir, sizeof(path_tmp1));
! 662: strlcat(path_tmp1, "/diff1.XXXXXXXXXX", sizeof(path_tmp1));
1.36 jfb 663: if (cvs_buf_write_stmp(b1, path_tmp1, 0600) == -1) {
664: cvs_buf_free(b1);
1.20 jfb 665: cvs_buf_free(b2);
1.36 jfb 666: return (CVS_EX_DATA);
667: }
668: cvs_buf_free(b1);
1.20 jfb 669:
1.67 ! xsa 670: strlcpy(path_tmp2, cvs_tmpdir, sizeof(path_tmp2));
! 671: strlcat(path_tmp2, "/diff2.XXXXXXXXXX", sizeof(path_tmp2));
1.36 jfb 672: if (cvs_buf_write_stmp(b2, path_tmp2, 0600) == -1) {
673: cvs_buf_free(b2);
1.12 djm 674: (void)unlink(path_tmp1);
1.36 jfb 675: return (CVS_EX_DATA);
1.1 jfb 676: }
1.36 jfb 677: cvs_buf_free(b2);
678:
1.58 niallo 679: cvs_diffreg(path_tmp1, path_tmp2, NULL);
1.36 jfb 680: (void)unlink(path_tmp1);
681: (void)unlink(path_tmp2);
1.1 jfb 682:
683: return (0);
684: }
1.56 niallo 685: #endif
1.1 jfb 686:
687:
688: int
1.58 niallo 689: cvs_diffreg(const char *file1, const char *file2, BUF *out)
1.1 jfb 690: {
691: FILE *f1, *f2;
692: int i, rval;
1.20 jfb 693: void *tmp;
1.1 jfb 694:
695: f1 = f2 = NULL;
696: rval = D_SAME;
697: anychange = 0;
698: lastline = 0;
699: lastmatchline = 0;
700: context_vec_ptr = context_vec_start - 1;
701: chrtran = (iflag ? cup2low : clow2low);
1.58 niallo 702: if (out != NULL)
703: diffbuf = out;
1.1 jfb 704:
705: f1 = fopen(file1, "r");
706: if (f1 == NULL) {
707: cvs_log(LP_ERRNO, "%s", file1);
708: goto closem;
709: }
710:
711: f2 = fopen(file2, "r");
712: if (f2 == NULL) {
713: cvs_log(LP_ERRNO, "%s", file2);
714: goto closem;
715: }
716:
717: switch (files_differ(f1, f2)) {
718: case 0:
719: goto closem;
720: case 1:
721: break;
722: default:
723: /* error */
724: goto closem;
725: }
726:
727: if (!asciifile(f1) || !asciifile(f2)) {
728: rval = D_BINARY;
729: goto closem;
730: }
1.20 jfb 731: if ((prepare(0, f1, stb1.st_size) < 0) ||
732: (prepare(1, f2, stb2.st_size) < 0)) {
733: goto closem;
734: }
1.1 jfb 735: prune();
736: sort(sfile[0], slen[0]);
737: sort(sfile[1], slen[1]);
738:
739: member = (int *)file[1];
740: equiv(sfile[0], slen[0], sfile[1], slen[1], member);
1.49 niallo 741: if ((tmp = realloc(member, (slen[1] + 2) * sizeof(int))) == NULL) {
742: free(member);
743: member = NULL;
744: cvs_log(LP_ERRNO, "failed to resize member");
1.20 jfb 745: goto closem;
1.49 niallo 746: }
1.20 jfb 747: member = (int *)tmp;
1.1 jfb 748:
749: class = (int *)file[0];
750: unsort(sfile[0], slen[0], class);
1.49 niallo 751: if ((tmp = realloc(class, (slen[0] + 2) * sizeof(int))) == NULL) {
752: free(class);
753: class = NULL;
754: cvs_log(LP_ERRNO, "failed to resize class");
1.20 jfb 755: goto closem;
1.49 niallo 756: }
1.20 jfb 757: class = (int *)tmp;
1.1 jfb 758:
1.20 jfb 759: if ((klist = malloc((slen[0] + 2) * sizeof(int))) == NULL) {
760: cvs_log(LP_ERRNO, "failed to allocate klist");
761: goto closem;
762: }
1.1 jfb 763: clen = 0;
764: clistlen = 100;
1.20 jfb 765: if ((clist = malloc(clistlen * sizeof(cand))) == NULL) {
766: cvs_log(LP_ERRNO, "failed to allocate clist");
767: goto closem;
768: }
1.48 joris 769:
770: if ((i = stone(class, slen[0], member, klist)) < 0)
771: goto closem;
772:
1.1 jfb 773: free(member);
774: free(class);
775:
1.49 niallo 776: if ((tmp = realloc(J, (diff_len[0] + 2) * sizeof(int))) == NULL) {
777: free(J);
778: J = NULL;
779: cvs_log(LP_ERRNO, "failed to resize J");
780: goto closem;
781: }
782: J = (int *)tmp;
1.1 jfb 783: unravel(klist[i]);
784: free(clist);
785: free(klist);
786:
1.49 niallo 787: if ((tmp = realloc(ixold, (diff_len[0] + 2) * sizeof(long))) == NULL) {
788: free(ixold);
789: ixold = NULL;
790: cvs_log(LP_ERRNO, "failed to resize ixold");
791: goto closem;
792: }
793: ixold = (long *)tmp;
794: if ((tmp = realloc(ixnew, (diff_len[1] + 2) * sizeof(long))) == NULL) {
795: free(ixnew);
796: ixnew = NULL;
797: cvs_log(LP_ERRNO, "failed to resize ixnew");
798: goto closem;
799: }
800: ixnew = (long *)tmp;
1.1 jfb 801: check(f1, f2);
802: output(file1, f1, file2, f2);
803:
804: closem:
1.65 xsa 805: if (anychange == 1) {
1.1 jfb 806: if (rval == D_SAME)
807: rval = D_DIFFER;
808: }
809: if (f1 != NULL)
810: fclose(f1);
811: if (f2 != NULL)
812: fclose(f2);
1.20 jfb 813:
1.1 jfb 814: return (rval);
815: }
816:
817: /*
818: * Check to see if the given files differ.
819: * Returns 0 if they are the same, 1 if different, and -1 on error.
820: * XXX - could use code from cmp(1) [faster]
821: */
822: static int
823: files_differ(FILE *f1, FILE *f2)
824: {
825: char buf1[BUFSIZ], buf2[BUFSIZ];
826: size_t i, j;
827:
828: if (stb1.st_size != stb2.st_size)
829: return (1);
830: for (;;) {
1.55 xsa 831: i = fread(buf1, (size_t)1, sizeof(buf1), f1);
832: j = fread(buf2, (size_t)1, sizeof(buf2), f2);
1.1 jfb 833: if (i != j)
834: return (1);
1.65 xsa 835: if ((i == 0) && (j == 0)) {
1.1 jfb 836: if (ferror(f1) || ferror(f2))
837: return (1);
838: return (0);
839: }
840: if (memcmp(buf1, buf2, i) != 0)
841: return (1);
842: }
843: }
844:
1.20 jfb 845: static int
1.1 jfb 846: prepare(int i, FILE *fd, off_t filesize)
847: {
1.20 jfb 848: void *tmp;
1.1 jfb 849: struct line *p;
850: int j, h;
851: size_t sz;
852:
853: rewind(fd);
854:
1.16 xsa 855: sz = ((size_t)filesize <= SIZE_MAX ? (size_t)filesize : SIZE_MAX) / 25;
1.1 jfb 856: if (sz < 100)
857: sz = 100;
858:
1.20 jfb 859: p = (struct line *)malloc((sz + 3) * sizeof(struct line));
860: if (p == NULL) {
861: cvs_log(LP_ERRNO, "failed to prepare line array");
862: return (-1);
863: }
1.1 jfb 864: for (j = 0; (h = readhash(fd));) {
865: if (j == (int)sz) {
866: sz = sz * 3 / 2;
1.20 jfb 867: tmp = realloc(p, (sz + 3) * sizeof(struct line));
868: if (tmp == NULL) {
869: cvs_log(LP_ERRNO, "failed to grow line array");
870: free(p);
871: return (-1);
872: }
873: p = (struct line *)tmp;
1.1 jfb 874: }
875: p[++j].value = h;
876: }
1.36 jfb 877: diff_len[i] = j;
1.1 jfb 878: file[i] = p;
1.20 jfb 879:
880: return (0);
1.1 jfb 881: }
882:
883: static void
884: prune(void)
885: {
886: int i, j;
887:
1.36 jfb 888: for (pref = 0; pref < diff_len[0] && pref < diff_len[1] &&
1.1 jfb 889: file[0][pref + 1].value == file[1][pref + 1].value;
890: pref++)
891: ;
1.36 jfb 892: for (suff = 0;
893: (suff < diff_len[0] - pref) && (suff < diff_len[1] - pref) &&
894: (file[0][diff_len[0] - suff].value ==
895: file[1][diff_len[1] - suff].value);
1.1 jfb 896: suff++)
897: ;
898: for (j = 0; j < 2; j++) {
899: sfile[j] = file[j] + pref;
1.36 jfb 900: slen[j] = diff_len[j] - pref - suff;
1.1 jfb 901: for (i = 0; i <= slen[j]; i++)
902: sfile[j][i].serial = i;
903: }
904: }
905:
906: static void
907: equiv(struct line *a, int n, struct line *b, int m, int *c)
908: {
909: int i, j;
910:
911: i = j = 1;
912: while (i <= n && j <= m) {
913: if (a[i].value < b[j].value)
914: a[i++].value = 0;
915: else if (a[i].value == b[j].value)
916: a[i++].value = j;
917: else
918: j++;
919: }
920: while (i <= n)
921: a[i++].value = 0;
922: b[m + 1].value = 0;
923: j = 0;
924: while (++j <= m) {
925: c[j] = -b[j].serial;
926: while (b[j + 1].value == b[j].value) {
927: j++;
928: c[j] = b[j].serial;
929: }
930: }
931: c[j] = -1;
932: }
933:
934: /* Code taken from ping.c */
935: static int
936: isqrt(int n)
937: {
938: int y, x = 1;
939:
940: if (n == 0)
1.18 jfb 941: return (0);
1.1 jfb 942:
943: do { /* newton was a stinker */
944: y = x;
945: x = n / x;
946: x += y;
947: x /= 2;
948: } while ((x - y) > 1 || (x - y) < -1);
949:
950: return (x);
951: }
952:
953: static int
954: stone(int *a, int n, int *b, int *c)
955: {
1.48 joris 956: int ret;
1.1 jfb 957: int i, k, y, j, l;
958: int oldc, tc, oldl;
959: u_int numtries;
960:
1.18 jfb 961: /* XXX move the isqrt() out of the macro to avoid multiple calls */
962: const u_int bound = dflag ? UINT_MAX : MAX(256, (u_int)isqrt(n));
1.1 jfb 963:
964: k = 0;
1.48 joris 965: if ((ret = newcand(0, 0, 0)) < 0)
966: return (-1);
967: c[0] = ret;
1.1 jfb 968: for (i = 1; i <= n; i++) {
969: j = a[i];
970: if (j == 0)
971: continue;
972: y = -b[j];
973: oldl = 0;
974: oldc = c[0];
975: numtries = 0;
976: do {
977: if (y <= clist[oldc].y)
978: continue;
979: l = search(c, k, y);
980: if (l != oldl + 1)
981: oldc = c[l - 1];
982: if (l <= k) {
983: if (clist[c[l]].y <= y)
984: continue;
985: tc = c[l];
1.48 joris 986: if ((ret = newcand(i, y, oldc)) < 0)
987: return (-1);
988: c[l] = ret;
1.1 jfb 989: oldc = tc;
990: oldl = l;
991: numtries++;
992: } else {
1.48 joris 993: if ((ret = newcand(i, y, oldc)) < 0)
994: return (-1);
995: c[l] = ret;
1.1 jfb 996: k++;
997: break;
998: }
999: } while ((y = b[++j]) > 0 && numtries < bound);
1000: }
1001: return (k);
1002: }
1003:
1004: static int
1005: newcand(int x, int y, int pred)
1006: {
1.49 niallo 1007: struct cand *q, *tmp;
1008: int newclistlen;
1.1 jfb 1009:
1010: if (clen == clistlen) {
1.49 niallo 1011: newclistlen = clistlen * 11 / 10;
1012: tmp = realloc(clist, newclistlen * sizeof(cand));
1013: if (tmp == NULL) {
1.20 jfb 1014: cvs_log(LP_ERRNO, "failed to resize clist");
1015: return (-1);
1016: }
1.49 niallo 1017: clist = tmp;
1018: clistlen = newclistlen;
1.1 jfb 1019: }
1020: q = clist + clen;
1021: q->x = x;
1022: q->y = y;
1023: q->pred = pred;
1024: return (clen++);
1025: }
1026:
1027: static int
1028: search(int *c, int k, int y)
1029: {
1030: int i, j, l, t;
1031:
1032: if (clist[c[k]].y < y) /* quick look for typical case */
1033: return (k + 1);
1034: i = 0;
1035: j = k + 1;
1036: while (1) {
1037: l = i + j;
1038: if ((l >>= 1) <= i)
1039: break;
1040: t = clist[c[l]].y;
1041: if (t > y)
1042: j = l;
1043: else if (t < y)
1044: i = l;
1045: else
1046: return (l);
1047: }
1048: return (l + 1);
1049: }
1050:
1051: static void
1052: unravel(int p)
1053: {
1054: struct cand *q;
1055: int i;
1056:
1.36 jfb 1057: for (i = 0; i <= diff_len[0]; i++)
1.1 jfb 1058: J[i] = i <= pref ? i :
1.36 jfb 1059: i > diff_len[0] - suff ? i + diff_len[1] - diff_len[0] : 0;
1.1 jfb 1060: for (q = clist + p; q->y != 0; q = clist + q->pred)
1061: J[q->x + pref] = q->y + pref;
1062: }
1063:
1064: /*
1065: * Check does double duty:
1066: * 1. ferret out any fortuitous correspondences due
1067: * to confounding by hashing (which result in "jackpot")
1068: * 2. collect random access indexes to the two files
1069: */
1070: static void
1071: check(FILE *f1, FILE *f2)
1072: {
1073: int i, j, jackpot, c, d;
1074: long ctold, ctnew;
1075:
1076: rewind(f1);
1077: rewind(f2);
1078: j = 1;
1079: ixold[0] = ixnew[0] = 0;
1080: jackpot = 0;
1081: ctold = ctnew = 0;
1.36 jfb 1082: for (i = 1; i <= diff_len[0]; i++) {
1.1 jfb 1083: if (J[i] == 0) {
1084: ixold[i] = ctold += skipline(f1);
1085: continue;
1086: }
1087: while (j < J[i]) {
1088: ixnew[j] = ctnew += skipline(f2);
1089: j++;
1090: }
1.64 xsa 1091: if ((bflag == 1)|| (wflag == 1) || (iflag == 1)) {
1.1 jfb 1092: for (;;) {
1093: c = getc(f1);
1094: d = getc(f2);
1095: /*
1096: * GNU diff ignores a missing newline
1097: * in one file if bflag || wflag.
1098: */
1.64 xsa 1099: if (((bflag == 1) || (wflag == 1)) &&
1.1 jfb 1100: ((c == EOF && d == '\n') ||
1101: (c == '\n' && d == EOF))) {
1102: break;
1103: }
1104: ctold++;
1105: ctnew++;
1.64 xsa 1106: if ((bflag == 1) && isspace(c) && isspace(d)) {
1.1 jfb 1107: do {
1108: if (c == '\n')
1109: break;
1110: ctold++;
1111: } while (isspace(c = getc(f1)));
1112: do {
1113: if (d == '\n')
1114: break;
1115: ctnew++;
1116: } while (isspace(d = getc(f2)));
1.64 xsa 1117: } else if (wflag == 1) {
1.1 jfb 1118: while (isspace(c) && c != '\n') {
1119: c = getc(f1);
1120: ctold++;
1121: }
1122: while (isspace(d) && d != '\n') {
1123: d = getc(f2);
1124: ctnew++;
1125: }
1126: }
1127: if (chrtran[c] != chrtran[d]) {
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: } else {
1140: for (;;) {
1141: ctold++;
1142: ctnew++;
1143: if ((c = getc(f1)) != (d = getc(f2))) {
1144: /* jackpot++; */
1145: J[i] = 0;
1.65 xsa 1146: if ((c != '\n') && (c != EOF))
1.1 jfb 1147: ctold += skipline(f1);
1.65 xsa 1148: if ((d != '\n') && (c != EOF))
1.1 jfb 1149: ctnew += skipline(f2);
1150: break;
1151: }
1.65 xsa 1152: if ((c == '\n') || (c == EOF))
1.1 jfb 1153: break;
1154: }
1155: }
1156: ixold[i] = ctold;
1157: ixnew[j] = ctnew;
1158: j++;
1159: }
1.36 jfb 1160: for (; j <= diff_len[1]; j++)
1.1 jfb 1161: ixnew[j] = ctnew += skipline(f2);
1162: /*
1.65 xsa 1163: * if (jackpot != 0)
1.42 joris 1164: * cvs_printf("jackpot\n");
1.1 jfb 1165: */
1166: }
1167:
1168: /* shellsort CACM #201 */
1169: static void
1170: sort(struct line *a, int n)
1171: {
1172: struct line *ai, *aim, w;
1173: int j, m = 0, k;
1174:
1175: if (n == 0)
1176: return;
1177: for (j = 1; j <= n; j *= 2)
1178: m = 2 * j - 1;
1179: for (m /= 2; m != 0; m /= 2) {
1180: k = n - m;
1181: for (j = 1; j <= k; j++) {
1182: for (ai = &a[j]; ai > a; ai -= m) {
1183: aim = &ai[m];
1184: if (aim < ai)
1185: break; /* wraparound */
1186: if (aim->value > ai[0].value ||
1187: (aim->value == ai[0].value &&
1188: aim->serial > ai[0].serial))
1189: break;
1190: w.value = ai[0].value;
1191: ai[0].value = aim->value;
1192: aim->value = w.value;
1193: w.serial = ai[0].serial;
1194: ai[0].serial = aim->serial;
1195: aim->serial = w.serial;
1196: }
1197: }
1198: }
1199: }
1200:
1201: static void
1202: unsort(struct line *f, int l, int *b)
1203: {
1204: int *a, i;
1205:
1.20 jfb 1206: if ((a = (int *)malloc((l + 1) * sizeof(int))) == NULL) {
1207: cvs_log(LP_ERRNO, "failed to allocate sort array");
1208: return;
1209: }
1.1 jfb 1210: for (i = 1; i <= l; i++)
1211: a[f[i].serial] = f[i].value;
1212: for (i = 1; i <= l; i++)
1213: b[i] = a[i];
1214: free(a);
1215: }
1216:
1217: static int
1218: skipline(FILE *f)
1219: {
1220: int i, c;
1221:
1222: for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
1223: continue;
1224: return (i);
1225: }
1226:
1227: static void
1228: output(const char *file1, FILE *f1, const char *file2, FILE *f2)
1229: {
1230: int m, i0, i1, j0, j1;
1231:
1232: rewind(f1);
1233: rewind(f2);
1.36 jfb 1234: m = diff_len[0];
1.1 jfb 1235: J[0] = 0;
1.36 jfb 1236: J[m + 1] = diff_len[1] + 1;
1.1 jfb 1237: for (i0 = 1; i0 <= m; i0 = i1 + 1) {
1238: while (i0 <= m && J[i0] == J[i0 - 1] + 1)
1239: i0++;
1240: j0 = J[i0 - 1] + 1;
1241: i1 = i0 - 1;
1242: while (i1 < m && J[i1 + 1] == 0)
1243: i1++;
1244: j1 = J[i1 + 1] - 1;
1245: J[i1] = j1;
1246: change(file1, f1, file2, f2, i0, i1, j0, j1);
1247: }
1248: if (m == 0)
1.36 jfb 1249: change(file1, f1, file2, f2, 1, 0, 1, diff_len[1]);
1.58 niallo 1250: if (diff_format == D_IFDEF) {
1.1 jfb 1251: for (;;) {
1252: #define c i0
1253: if ((c = getc(f1)) == EOF)
1254: return;
1.58 niallo 1255: diff_output("%c", c);
1.1 jfb 1256: }
1257: #undef c
1258: }
1259: if (anychange != 0) {
1.58 niallo 1260: if (diff_format == D_CONTEXT)
1.1 jfb 1261: dump_context_vec(f1, f2);
1.58 niallo 1262: else if (diff_format == D_UNIFIED)
1.1 jfb 1263: dump_unified_vec(f1, f2);
1264: }
1265: }
1266:
1267: static __inline void
1268: range(int a, int b, char *separator)
1269: {
1.58 niallo 1270: diff_output("%d", a > b ? b : a);
1.1 jfb 1271: if (a < b)
1.58 niallo 1272: diff_output("%s%d", separator, b);
1.1 jfb 1273: }
1274:
1275: static __inline void
1276: uni_range(int a, int b)
1277: {
1278: if (a < b)
1.58 niallo 1279: diff_output("%d,%d", a, b - a + 1);
1.1 jfb 1280: else if (a == b)
1.58 niallo 1281: diff_output("%d", b);
1.1 jfb 1282: else
1.58 niallo 1283: diff_output("%d,0", b);
1.1 jfb 1284: }
1285:
1286: static char *
1.15 jfb 1287: preadline(int fd, size_t rlen, off_t off)
1.1 jfb 1288: {
1289: char *line;
1290: ssize_t nr;
1291:
1.15 jfb 1292: line = malloc(rlen + 1);
1.1 jfb 1293: if (line == NULL) {
1294: cvs_log(LP_ERRNO, "failed to allocate line");
1295: return (NULL);
1296: }
1.15 jfb 1297: if ((nr = pread(fd, line, rlen, off)) < 0) {
1.1 jfb 1298: cvs_log(LP_ERRNO, "preadline failed");
1299: return (NULL);
1300: }
1301: line[nr] = '\0';
1302: return (line);
1303: }
1304:
1305: static int
1306: ignoreline(char *line)
1307: {
1308: int ret;
1309:
1.55 xsa 1310: ret = regexec(&ignore_re, line, (size_t)0, NULL, 0);
1.1 jfb 1311: free(line);
1312: return (ret == 0); /* if it matched, it should be ignored. */
1313: }
1314:
1315: /*
1316: * Indicate that there is a difference between lines a and b of the from file
1317: * to get to lines c to d of the to file. If a is greater then b then there
1318: * are no lines in the from file involved and this means that there were
1319: * lines appended (beginning at b). If c is greater than d then there are
1320: * lines missing from the to file.
1321: */
1322: static void
1323: change(const char *file1, FILE *f1, const char *file2, FILE *f2,
1324: int a, int b, int c, int d)
1325: {
1326: static size_t max_context = 64;
1327: int i;
1328:
1.58 niallo 1329: if (diff_format != D_IFDEF && a > b && c > d)
1.1 jfb 1330: return;
1331: if (ignore_pats != NULL) {
1332: char *line;
1333: /*
1334: * All lines in the change, insert, or delete must
1335: * match an ignore pattern for the change to be
1336: * ignored.
1337: */
1338: if (a <= b) { /* Changes and deletes. */
1339: for (i = a; i <= b; i++) {
1340: line = preadline(fileno(f1),
1341: ixold[i] - ixold[i - 1], ixold[i - 1]);
1342: if (!ignoreline(line))
1343: goto proceed;
1344: }
1345: }
1.65 xsa 1346: if ((a > b) || (c <= d)) { /* Changes and inserts. */
1.1 jfb 1347: for (i = c; i <= d; i++) {
1348: line = preadline(fileno(f2),
1349: ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
1350: if (!ignoreline(line))
1351: goto proceed;
1352: }
1353: }
1354: return;
1355: }
1356: proceed:
1.58 niallo 1357: if (diff_format == D_CONTEXT || diff_format == D_UNIFIED) {
1.1 jfb 1358: /*
1359: * Allocate change records as needed.
1360: */
1361: if (context_vec_ptr == context_vec_end - 1) {
1.49 niallo 1362: struct context_vec *tmp;
1.1 jfb 1363: ptrdiff_t offset = context_vec_ptr - context_vec_start;
1364: max_context <<= 1;
1.57 reyk 1365: if ((tmp = realloc(context_vec_start, max_context *
1366: sizeof(struct context_vec))) == NULL) {
1.49 niallo 1367: free(context_vec_start);
1368: context_vec_start = NULL;
1369: cvs_log(LP_ERRNO,
1370: "failed to resize context_vec_start");
1371: return;
1372: }
1373: context_vec_start = tmp;
1.1 jfb 1374: context_vec_end = context_vec_start + max_context;
1375: context_vec_ptr = context_vec_start + offset;
1376: }
1377: if (anychange == 0) {
1378: /*
1379: * Print the context/unidiff header first time through.
1380: */
1.58 niallo 1381: diff_output("%s %s %s",
1382: diff_format == D_CONTEXT ? "***" : "---", diff_file,
1.1 jfb 1383: ctime(&stb1.st_mtime));
1.58 niallo 1384: diff_output("%s %s %s",
1385: diff_format == D_CONTEXT ? "---" : "+++", diff_file,
1.1 jfb 1386: ctime(&stb2.st_mtime));
1387: anychange = 1;
1388: } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
1389: c > context_vec_ptr->d + (2 * context) + 1) {
1390: /*
1391: * If this change is more than 'context' lines from the
1392: * previous change, dump the record and reset it.
1393: */
1.58 niallo 1394: if (diff_format == D_CONTEXT)
1.1 jfb 1395: dump_context_vec(f1, f2);
1396: else
1397: dump_unified_vec(f1, f2);
1398: }
1399: context_vec_ptr++;
1400: context_vec_ptr->a = a;
1401: context_vec_ptr->b = b;
1402: context_vec_ptr->c = c;
1403: context_vec_ptr->d = d;
1404: return;
1405: }
1406: if (anychange == 0)
1407: anychange = 1;
1.58 niallo 1408: switch (diff_format) {
1.1 jfb 1409: case D_BRIEF:
1410: return;
1411: case D_NORMAL:
1412: range(a, b, ",");
1.58 niallo 1413: diff_output("%c", a > b ? 'a' : c > d ? 'd' : 'c');
1414: if (diff_format == D_NORMAL)
1.1 jfb 1415: range(c, d, ",");
1.58 niallo 1416: diff_output("\n");
1.1 jfb 1417: break;
1.33 jfb 1418: case D_RCSDIFF:
1419: if (a > b)
1.58 niallo 1420: diff_output("a%d %d\n", b, d - c + 1);
1.33 jfb 1421: else {
1.58 niallo 1422: diff_output("d%d %d\n", a, b - a + 1);
1.33 jfb 1423:
1424: if (!(c > d)) /* add changed lines */
1.58 niallo 1425: diff_output("a%d %d\n", b, d - c + 1);
1.33 jfb 1426: }
1427: break;
1.1 jfb 1428: }
1.58 niallo 1429: if (diff_format == D_NORMAL || diff_format == D_IFDEF) {
1.1 jfb 1430: fetch(ixold, a, b, f1, '<', 1);
1.58 niallo 1431: if (a <= b && c <= d && diff_format == D_NORMAL)
1.60 joris 1432: diff_output("---\n");
1.1 jfb 1433: }
1.58 niallo 1434: i = fetch(ixnew, c, d, f2, diff_format == D_NORMAL ? '>' : '\0', 0);
1.1 jfb 1435: if (inifdef) {
1.58 niallo 1436: diff_output("#endif /* %s */\n", ifdefname);
1.1 jfb 1437: inifdef = 0;
1438: }
1439: }
1440:
1441: static int
1442: fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile)
1443: {
1444: int i, j, c, lastc, col, nc;
1445:
1446: /*
1447: * When doing #ifdef's, copy down to current line
1448: * if this is the first file, so that stuff makes it to output.
1449: */
1.58 niallo 1450: if (diff_format == D_IFDEF && oldfile) {
1.1 jfb 1451: long curpos = ftell(lb);
1452: /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1453: nc = f[a > b ? b : a - 1] - curpos;
1454: for (i = 0; i < nc; i++)
1.58 niallo 1455: diff_output("%c", getc(lb));
1.1 jfb 1456: }
1457: if (a > b)
1458: return (0);
1.58 niallo 1459: if (diff_format == D_IFDEF) {
1.1 jfb 1460: if (inifdef) {
1.58 niallo 1461: diff_output("#else /* %s%s */\n",
1.1 jfb 1462: oldfile == 1 ? "!" : "", ifdefname);
1463: } else {
1464: if (oldfile)
1.58 niallo 1465: diff_output("#ifndef %s\n", ifdefname);
1.1 jfb 1466: else
1.58 niallo 1467: diff_output("#ifdef %s\n", ifdefname);
1.1 jfb 1468: }
1469: inifdef = 1 + oldfile;
1470: }
1471: for (i = a; i <= b; i++) {
1472: fseek(lb, f[i - 1], SEEK_SET);
1473: nc = f[i] - f[i - 1];
1.58 niallo 1474: if (diff_format != D_IFDEF && ch != '\0') {
1475: diff_output("%c", ch);
1.64 xsa 1476: if ((Tflag == 1 ) && (diff_format == D_NORMAL ||
1.59 joris 1477: diff_format == D_CONTEXT ||
1478: diff_format == D_UNIFIED))
1.58 niallo 1479: diff_output("\t");
1480: else if (diff_format != D_UNIFIED)
1481: diff_output(" ");
1.1 jfb 1482: }
1483: col = 0;
1484: for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
1485: if ((c = getc(lb)) == EOF) {
1.58 niallo 1486: if (diff_format == D_RCSDIFF)
1.33 jfb 1487: warnx("No newline at end of file");
1488: else
1.59 joris 1489: diff_output("\n\\ No newline at end of "
1490: "file");
1.1 jfb 1491: return (0);
1492: }
1.64 xsa 1493: if ((c == '\t') && (tflag == 1)) {
1.1 jfb 1494: do {
1.58 niallo 1495: diff_output(" ");
1.1 jfb 1496: } while (++col & 7);
1497: } else {
1.58 niallo 1498: diff_output("%c", c);
1.1 jfb 1499: col++;
1500: }
1501: }
1502: }
1503: return (0);
1504: }
1505:
1506: /*
1507: * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
1508: */
1509: static int
1510: readhash(FILE *f)
1511: {
1512: int i, t, space;
1513: int sum;
1514:
1515: sum = 1;
1516: space = 0;
1.64 xsa 1517: if ((bflag != 1) && (wflag != 1)) {
1518: if (iflag == 1)
1.1 jfb 1519: for (i = 0; (t = getc(f)) != '\n'; i++) {
1520: if (t == EOF) {
1521: if (i == 0)
1522: return (0);
1523: break;
1524: }
1525: sum = sum * 127 + chrtran[t];
1526: }
1527: else
1528: for (i = 0; (t = getc(f)) != '\n'; i++) {
1529: if (t == EOF) {
1530: if (i == 0)
1531: return (0);
1532: break;
1533: }
1534: sum = sum * 127 + t;
1535: }
1536: } else {
1537: for (i = 0;;) {
1538: switch (t = getc(f)) {
1539: case '\t':
1540: case ' ':
1541: space++;
1542: continue;
1543: default:
1.64 xsa 1544: if ((space != 0) && (wflag != 1)) {
1.1 jfb 1545: i++;
1546: space = 0;
1547: }
1548: sum = sum * 127 + chrtran[t];
1549: i++;
1550: continue;
1551: case EOF:
1552: if (i == 0)
1553: return (0);
1554: /* FALLTHROUGH */
1555: case '\n':
1556: break;
1557: }
1558: break;
1559: }
1560: }
1561: /*
1562: * There is a remote possibility that we end up with a zero sum.
1563: * Zero is used as an EOF marker, so return 1 instead.
1564: */
1565: return (sum == 0 ? 1 : sum);
1566: }
1567:
1568: static int
1569: asciifile(FILE *f)
1570: {
1571: char buf[BUFSIZ];
1572: int i, cnt;
1573:
1.64 xsa 1574: if ((aflag == 1) || (f == NULL))
1.1 jfb 1575: return (1);
1576:
1577: rewind(f);
1.55 xsa 1578: cnt = fread(buf, (size_t)1, sizeof(buf), f);
1.1 jfb 1579: for (i = 0; i < cnt; i++)
1580: if (!isprint(buf[i]) && !isspace(buf[i]))
1581: return (0);
1582: return (1);
1583: }
1584:
1.17 jfb 1585: static char*
1586: match_function(const long *f, int pos, FILE *fp)
1587: {
1588: unsigned char buf[FUNCTION_CONTEXT_SIZE];
1589: size_t nc;
1590: int last = lastline;
1591: char *p;
1592:
1593: lastline = pos;
1594: while (pos > last) {
1595: fseek(fp, f[pos - 1], SEEK_SET);
1596: nc = f[pos] - f[pos - 1];
1597: if (nc >= sizeof(buf))
1598: nc = sizeof(buf) - 1;
1.55 xsa 1599: nc = fread(buf, (size_t)1, nc, fp);
1.17 jfb 1600: if (nc > 0) {
1601: buf[nc] = '\0';
1.44 niallo 1602: p = strchr((const char *)buf, '\n');
1.17 jfb 1603: if (p != NULL)
1604: *p = '\0';
1605: if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
1.57 reyk 1606: strlcpy(lastbuf, (const char *)buf,
1607: sizeof lastbuf);
1.17 jfb 1608: lastmatchline = pos;
1609: return lastbuf;
1610: }
1611: }
1612: pos--;
1613: }
1614: return (lastmatchline > 0) ? lastbuf : NULL;
1615: }
1616:
1.1 jfb 1617:
1618: /* dump accumulated "context" diff changes */
1619: static void
1620: dump_context_vec(FILE *f1, FILE *f2)
1621: {
1622: struct context_vec *cvp = context_vec_start;
1623: int lowa, upb, lowc, upd, do_output;
1624: int a, b, c, d;
1.17 jfb 1625: char ch, *f;
1.1 jfb 1626:
1627: if (context_vec_start > context_vec_ptr)
1628: return;
1629:
1630: b = d = 0; /* gcc */
1.4 jfb 1631: lowa = MAX(1, cvp->a - context);
1.36 jfb 1632: upb = MIN(diff_len[0], context_vec_ptr->b + context);
1.4 jfb 1633: lowc = MAX(1, cvp->c - context);
1.36 jfb 1634: upd = MIN(diff_len[1], context_vec_ptr->d + context);
1.1 jfb 1635:
1.58 niallo 1636: diff_output("***************");
1.64 xsa 1637: if (pflag == 1) {
1.17 jfb 1638: f = match_function(ixold, lowa - 1, f1);
1639: if (f != NULL) {
1.58 niallo 1640: diff_output(" ");
1641: diff_output("%s", f);
1.17 jfb 1642: }
1643: }
1.58 niallo 1644: diff_output("\n*** ");
1.1 jfb 1645: range(lowa, upb, ",");
1.58 niallo 1646: diff_output(" ****\n");
1.1 jfb 1647:
1648: /*
1649: * Output changes to the "old" file. The first loop suppresses
1650: * output if there were no changes to the "old" file (we'll see
1651: * the "old" lines as context in the "new" list).
1652: */
1653: do_output = 0;
1654: for (; cvp <= context_vec_ptr; cvp++)
1655: if (cvp->a <= cvp->b) {
1656: cvp = context_vec_start;
1657: do_output++;
1658: break;
1659: }
1.65 xsa 1660: if (do_output != 0) {
1.1 jfb 1661: while (cvp <= context_vec_ptr) {
1662: a = cvp->a;
1663: b = cvp->b;
1664: c = cvp->c;
1665: d = cvp->d;
1666:
1667: if (a <= b && c <= d)
1668: ch = 'c';
1669: else
1670: ch = (a <= b) ? 'd' : 'a';
1671:
1672: if (ch == 'a')
1673: fetch(ixold, lowa, b, f1, ' ', 0);
1674: else {
1675: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1676: fetch(ixold, a, b, f1,
1677: ch == 'c' ? '!' : '-', 0);
1678: }
1679: lowa = b + 1;
1680: cvp++;
1681: }
1682: fetch(ixold, b + 1, upb, f1, ' ', 0);
1683: }
1684: /* output changes to the "new" file */
1.58 niallo 1685: diff_output("--- ");
1.1 jfb 1686: range(lowc, upd, ",");
1.58 niallo 1687: diff_output(" ----\n");
1.1 jfb 1688:
1689: do_output = 0;
1690: for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
1691: if (cvp->c <= cvp->d) {
1692: cvp = context_vec_start;
1693: do_output++;
1694: break;
1695: }
1.65 xsa 1696: if (do_output != 0) {
1.1 jfb 1697: while (cvp <= context_vec_ptr) {
1698: a = cvp->a;
1699: b = cvp->b;
1700: c = cvp->c;
1701: d = cvp->d;
1702:
1703: if (a <= b && c <= d)
1704: ch = 'c';
1705: else
1706: ch = (a <= b) ? 'd' : 'a';
1707:
1708: if (ch == 'd')
1709: fetch(ixnew, lowc, d, f2, ' ', 0);
1710: else {
1711: fetch(ixnew, lowc, c - 1, f2, ' ', 0);
1712: fetch(ixnew, c, d, f2,
1713: ch == 'c' ? '!' : '+', 0);
1714: }
1715: lowc = d + 1;
1716: cvp++;
1717: }
1718: fetch(ixnew, d + 1, upd, f2, ' ', 0);
1719: }
1720: context_vec_ptr = context_vec_start - 1;
1721: }
1722:
1723: /* dump accumulated "unified" diff changes */
1724: static void
1725: dump_unified_vec(FILE *f1, FILE *f2)
1726: {
1727: struct context_vec *cvp = context_vec_start;
1728: int lowa, upb, lowc, upd;
1729: int a, b, c, d;
1.17 jfb 1730: char ch, *f;
1.1 jfb 1731:
1732: if (context_vec_start > context_vec_ptr)
1733: return;
1734:
1735: b = d = 0; /* gcc */
1.4 jfb 1736: lowa = MAX(1, cvp->a - context);
1.36 jfb 1737: upb = MIN(diff_len[0], context_vec_ptr->b + context);
1.4 jfb 1738: lowc = MAX(1, cvp->c - context);
1.36 jfb 1739: upd = MIN(diff_len[1], context_vec_ptr->d + context);
1.1 jfb 1740:
1.58 niallo 1741: diff_output("@@ -");
1.1 jfb 1742: uni_range(lowa, upb);
1.58 niallo 1743: diff_output(" +");
1.1 jfb 1744: uni_range(lowc, upd);
1.58 niallo 1745: diff_output(" @@");
1.64 xsa 1746: if (pflag == 1) {
1.17 jfb 1747: f = match_function(ixold, lowa - 1, f1);
1748: if (f != NULL) {
1.58 niallo 1749: diff_output(" ");
1750: diff_output("%s", f);
1.17 jfb 1751: }
1752: }
1.58 niallo 1753: diff_output("\n");
1.1 jfb 1754:
1755: /*
1756: * Output changes in "unified" diff format--the old and new lines
1757: * are printed together.
1758: */
1759: for (; cvp <= context_vec_ptr; cvp++) {
1760: a = cvp->a;
1761: b = cvp->b;
1762: c = cvp->c;
1763: d = cvp->d;
1764:
1765: /*
1766: * c: both new and old changes
1767: * d: only changes in the old file
1768: * a: only changes in the new file
1769: */
1770: if (a <= b && c <= d)
1771: ch = 'c';
1772: else
1773: ch = (a <= b) ? 'd' : 'a';
1774:
1775: switch (ch) {
1776: case 'c':
1777: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1778: fetch(ixold, a, b, f1, '-', 0);
1779: fetch(ixnew, c, d, f2, '+', 0);
1780: break;
1781: case 'd':
1782: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1783: fetch(ixold, a, b, f1, '-', 0);
1784: break;
1785: case 'a':
1786: fetch(ixnew, lowc, c - 1, f2, ' ', 0);
1787: fetch(ixnew, c, d, f2, '+', 0);
1788: break;
1789: }
1790: lowa = b + 1;
1791: lowc = d + 1;
1792: }
1793: fetch(ixnew, d + 1, upd, f2, ' ', 0);
1794:
1795: context_vec_ptr = context_vec_start - 1;
1.58 niallo 1796: }
1797:
1.63 joris 1798: void
1.58 niallo 1799: diff_output(const char *fmt, ...)
1800: {
1801: va_list vap;
1802: char *str;
1803:
1804: va_start(vap, fmt);
1805: vasprintf(&str, fmt, vap);
1806: if (diffbuf != NULL)
1807: cvs_buf_append(diffbuf, str, strlen(str));
1808: else
1809: cvs_printf("%s", str);
1810: free(str);
1811: va_end(vap);
1.1 jfb 1812: }