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