Annotation of src/usr.bin/cvs/diff_internals.c, Revision 1.17
1.17 ! tobias 1: /* $OpenBSD: diff_internals.c,v 1.16 2008/01/31 20:11:28 tobias Exp $ */
1.1 joris 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: */
1.14 ray 66:
67: #include <sys/param.h>
68: #include <sys/stat.h>
69:
70: #include <ctype.h>
71: #include <errno.h>
72: #include <regex.h>
73: #include <stddef.h>
1.16 tobias 74: #include <stdio.h>
1.14 ray 75: #include <string.h>
76: #include <unistd.h>
77:
78: #include "cvs.h"
79: #include "diff.h"
80:
81: /*
82: * diff - compare two files.
83: */
84:
1.1 joris 85: /*
86: * Uses an algorithm due to Harold Stone, which finds
87: * a pair of longest identical subsequences in the two
88: * files.
89: *
90: * The major goal is to generate the match vector J.
91: * J[i] is the index of the line in file1 corresponding
92: * to line i file0. J[i] = 0 if there is no
93: * such line in file1.
94: *
95: * Lines are hashed so as to work in core. All potential
96: * matches are located by sorting the lines of each file
97: * on the hash (called ``value''). In particular, this
98: * collects the equivalence classes in file1 together.
99: * Subroutine equiv replaces the value of each line in
100: * file0 by the index of the first element of its
101: * matching equivalence in (the reordered) file1.
102: * To save space equiv squeezes file1 into a single
103: * array member in which the equivalence classes
104: * are simply concatenated, except that their first
105: * members are flagged by changing sign.
106: *
107: * Next the indices that point into member are unsorted into
108: * array class according to the original order of file0.
109: *
110: * The cleverness lies in routine stone. This marches
111: * through the lines of file0, developing a vector klist
112: * of "k-candidates". At step i a k-candidate is a matched
113: * pair of lines x,y (x in file0 y in file1) such that
114: * there is a common subsequence of length k
115: * between the first i lines of file0 and the first y
116: * lines of file1, but there is no such subsequence for
117: * any smaller y. x is the earliest possible mate to y
118: * that occurs in such a subsequence.
119: *
120: * Whenever any of the members of the equivalence class of
121: * lines in file1 matable to a line in file0 has serial number
122: * less than the y of some k-candidate, that k-candidate
123: * with the smallest such y is replaced. The new
124: * k-candidate is chained (via pred) to the current
125: * k-1 candidate so that the actual subsequence can
126: * be recovered. When a member has serial number greater
127: * that the y of all k-candidates, the klist is extended.
128: * At the end, the longest subsequence is pulled out
129: * and placed in the array J by unravel
130: *
131: * With J in hand, the matches there recorded are
132: * check'ed against reality to assure that no spurious
133: * matches have crept in due to hashing. If they have,
134: * they are broken, and "jackpot" is recorded--a harmless
135: * matter except that a true match for a spuriously
136: * mated line may now be unnecessarily reported as a change.
137: *
138: * Much of the complexity of the program comes simply
139: * from trying to minimize core utilization and
140: * maximize the range of doable problems by dynamically
141: * allocating what is needed and reusing what is not.
142: * The core requirements for problems larger than somewhat
143: * are (in words) 2*length(file0) + length(file1) +
144: * 3*(number of k-candidates installed), typically about
145: * 6n words for files of length n.
146: */
147:
148: struct cand {
149: int x;
150: int y;
151: int pred;
1.13 ray 152: };
1.1 joris 153:
154: struct line {
155: int serial;
156: int value;
157: } *file[2];
158:
159: /*
1.7 ray 160: * The following struct is used to record change information when
1.1 joris 161: * doing a "context" or "unified" diff. (see routine "change" to
162: * understand the highly mnemonic field names)
163: */
164: struct context_vec {
1.6 ray 165: int a; /* start line in old file */
166: int b; /* end line in old file */
167: int c; /* start line in new file */
168: int d; /* end line in new file */
1.1 joris 169: };
170:
171: static void output(FILE *, FILE *);
172: static void check(FILE *, FILE *);
173: static void range(int, int, char *);
174: static void uni_range(int, int);
175: static void dump_context_vec(FILE *, FILE *);
176: static void dump_unified_vec(FILE *, FILE *);
1.14 ray 177: static void prepare(int, FILE *, off_t);
1.1 joris 178: static void prune(void);
179: static void equiv(struct line *, int, struct line *, int, int *);
180: static void unravel(int);
181: static void unsort(struct line *, int, int *);
1.17 ! tobias 182: static void diff_head(void);
! 183: static void rdiff_head(void);
1.1 joris 184: static void change(FILE *, FILE *, int, int, int, int);
185: static void sort(struct line *, int);
186: static int ignoreline(char *);
187: static int asciifile(FILE *);
188: static void fetch(long *, int, int, FILE *, int, int);
189: static int newcand(int, int, int);
190: static int search(int *, int, int);
191: static int skipline(FILE *);
192: static int isqrt(int);
193: static int stone(int *, int, int *, int *);
194: static int readhash(FILE *);
195: static int files_differ(FILE *, FILE *);
196: static char *match_function(const long *, int, FILE *);
197: static char *preadline(int, size_t, off_t);
198:
1.17 ! tobias 199: static int aflag, bflag, dflag, tflag, Tflag, wflag;
1.1 joris 200: static int context = 3;
201: int diff_format = D_NORMAL;
1.17 ! tobias 202: int diff_iflag = 0;
1.2 joris 203: int diff_pflag = 0;
1.1 joris 204: char *diff_file = NULL;
205: RCSNUM *diff_rev1 = NULL;
206: RCSNUM *diff_rev2 = NULL;
207: char diffargs[128];
208: static struct stat stb1, stb2;
209: static char *ifdefname, *ignore_pats;
210: regex_t ignore_re;
211:
212: static int *J; /* will be overlaid on class */
213: static int *class; /* will be overlaid on file[0] */
214: static int *klist; /* will be overlaid on file[0] after class */
215: static int *member; /* will be overlaid on file[1] */
216: static int clen;
217: static int inifdef; /* whether or not we are in a #ifdef block */
1.14 ray 218: static int len[2];
1.1 joris 219: static int pref, suff; /* length of prefix and suffix */
220: static int slen[2];
221: static int anychange;
222: static long *ixnew; /* will be overlaid on file[1] */
223: static long *ixold; /* will be overlaid on klist */
224: static struct cand *clist; /* merely a free storage pot for candidates */
225: static int clistlen; /* the length of clist */
226: static struct line *sfile[2]; /* shortened by pruning common prefix/suffix */
227: static u_char *chrtran; /* translation table for case-folding */
228: static struct context_vec *context_vec_start;
229: static struct context_vec *context_vec_end;
230: static struct context_vec *context_vec_ptr;
231:
1.5 xsa 232: #define FUNCTION_CONTEXT_SIZE 55
1.1 joris 233: static char lastbuf[FUNCTION_CONTEXT_SIZE];
1.8 ray 234: static int lastline;
235: static int lastmatchline;
1.1 joris 236: BUF *diffbuf = NULL;
237:
1.14 ray 238:
1.1 joris 239: /*
240: * chrtran points to one of 2 translation tables: cup2low if folding upper to
241: * lower case clow2low if not folding case
242: */
243: u_char clow2low[256] = {
244: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
245: 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
246: 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
247: 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
248: 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
249: 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x41,
250: 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
251: 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
252: 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62,
253: 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
254: 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
255: 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
256: 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
257: 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
258: 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
259: 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
260: 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
261: 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
262: 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
263: 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
264: 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
265: 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
266: 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
267: 0xfd, 0xfe, 0xff
268: };
269:
270: u_char cup2low[256] = {
271: 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
272: 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
273: 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
274: 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
275: 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
276: 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x60, 0x61,
277: 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
278: 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
279: 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x60, 0x61, 0x62,
280: 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d,
281: 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
282: 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
283: 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
284: 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
285: 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4,
286: 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
287: 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
288: 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5,
289: 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0,
290: 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
291: 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
292: 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1,
293: 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc,
294: 0xfd, 0xfe, 0xff
295: };
296:
297: int
298: cvs_diffreg(const char *file1, const char *file2, BUF *out)
299: {
300: FILE *f1, *f2;
301: int i, rval;
302:
303: f1 = f2 = NULL;
304: rval = D_SAME;
305: anychange = 0;
306: lastline = 0;
307: lastmatchline = 0;
308: context_vec_ptr = context_vec_start - 1;
1.17 ! tobias 309: chrtran = (diff_iflag ? cup2low : clow2low);
1.1 joris 310: if (out != NULL)
311: diffbuf = out;
312:
313: f1 = fopen(file1, "r");
314: if (f1 == NULL) {
315: cvs_log(LP_ERR, "%s", file1);
316: goto closem;
317: }
318:
319: f2 = fopen(file2, "r");
320: if (f2 == NULL) {
321: cvs_log(LP_ERR, "%s", file2);
322: goto closem;
323: }
324:
1.11 ray 325: if (fstat(fileno(f1), &stb1) < 0) {
1.1 joris 326: cvs_log(LP_ERR, "%s", file1);
327: goto closem;
328: }
1.11 ray 329: if (fstat(fileno(f2), &stb2) < 0) {
1.1 joris 330: cvs_log(LP_ERR, "%s", file2);
331: goto closem;
332: }
1.14 ray 333:
1.1 joris 334: switch (files_differ(f1, f2)) {
335: case 0:
336: goto closem;
337: case 1:
338: break;
339: default:
340: /* error */
341: goto closem;
342: }
343:
344: if (!asciifile(f1) || !asciifile(f2)) {
345: rval = D_BINARY;
346: goto closem;
347: }
1.14 ray 348:
349: prepare(0, f1, stb1.st_size);
350: prepare(1, f2, stb2.st_size);
351:
1.1 joris 352: prune();
353: sort(sfile[0], slen[0]);
354: sort(sfile[1], slen[1]);
355:
356: member = (int *)file[1];
357: equiv(sfile[0], slen[0], sfile[1], slen[1], member);
1.9 ray 358: member = xrealloc(member, slen[1] + 2, sizeof(*member));
1.1 joris 359:
360: class = (int *)file[0];
361: unsort(sfile[0], slen[0], class);
1.9 ray 362: class = xrealloc(class, slen[0] + 2, sizeof(*class));
1.1 joris 363:
364: klist = xcalloc(slen[0] + 2, sizeof(*klist));
365: clen = 0;
366: clistlen = 100;
367: clist = xcalloc(clistlen, sizeof(*clist));
1.14 ray 368: i = stone(class, slen[0], member, klist);
1.1 joris 369: xfree(member);
370: xfree(class);
371:
1.14 ray 372: J = xrealloc(J, len[0] + 2, sizeof(*J));
1.1 joris 373: unravel(klist[i]);
374: xfree(clist);
375: xfree(klist);
376:
1.14 ray 377: ixold = xrealloc(ixold, len[0] + 2, sizeof(*ixold));
378: ixnew = xrealloc(ixnew, len[1] + 2, sizeof(*ixnew));
1.1 joris 379: check(f1, f2);
380: output(f1, f2);
381:
382: closem:
1.14 ray 383: if (anychange) {
1.1 joris 384: if (rval == D_SAME)
385: rval = D_DIFFER;
386: }
387: if (f1 != NULL)
388: fclose(f1);
389: if (f2 != NULL)
390: fclose(f2);
391:
392: return (rval);
393: }
394:
395: /*
396: * Check to see if the given files differ.
397: * Returns 0 if they are the same, 1 if different, and -1 on error.
398: * XXX - could use code from cmp(1) [faster]
399: */
400: static int
401: files_differ(FILE *f1, FILE *f2)
402: {
403: char buf1[BUFSIZ], buf2[BUFSIZ];
404: size_t i, j;
405:
406: if (stb1.st_size != stb2.st_size)
407: return (1);
408: for (;;) {
1.11 ray 409: i = fread(buf1, 1, sizeof(buf1), f1);
410: j = fread(buf2, 1, sizeof(buf2), f2);
1.1 joris 411: if (i != j)
412: return (1);
413: if (i == 0 && j == 0) {
414: if (ferror(f1) || ferror(f2))
415: return (1);
416: return (0);
417: }
418: if (memcmp(buf1, buf2, i) != 0)
419: return (1);
420: }
421: }
422:
1.14 ray 423: static void
1.1 joris 424: prepare(int i, FILE *fd, off_t filesize)
425: {
426: struct line *p;
427: int j, h;
428: size_t sz;
429:
430: rewind(fd);
431:
1.11 ray 432: sz = (filesize <= SIZE_MAX ? filesize : SIZE_MAX) / 25;
1.1 joris 433: if (sz < 100)
434: sz = 100;
435:
436: p = xcalloc(sz + 3, sizeof(*p));
437: for (j = 0; (h = readhash(fd));) {
1.14 ray 438: if (j == sz) {
1.1 joris 439: sz = sz * 3 / 2;
1.9 ray 440: p = xrealloc(p, sz + 3, sizeof(*p));
1.1 joris 441: }
442: p[++j].value = h;
443: }
1.14 ray 444: len[i] = j;
1.1 joris 445: file[i] = p;
446: }
447:
448: static void
449: prune(void)
450: {
451: int i, j;
452:
1.14 ray 453: for (pref = 0; pref < len[0] && pref < len[1] &&
1.1 joris 454: file[0][pref + 1].value == file[1][pref + 1].value;
455: pref++)
456: ;
1.14 ray 457: for (suff = 0; suff < len[0] - pref && suff < len[1] - pref &&
458: file[0][len[0] - suff].value == file[1][len[1] - suff].value;
1.1 joris 459: suff++)
460: ;
461: for (j = 0; j < 2; j++) {
462: sfile[j] = file[j] + pref;
1.14 ray 463: slen[j] = len[j] - pref - suff;
1.1 joris 464: for (i = 0; i <= slen[j]; i++)
465: sfile[j][i].serial = i;
466: }
467: }
468:
469: static void
470: equiv(struct line *a, int n, struct line *b, int m, int *c)
471: {
472: int i, j;
473:
474: i = j = 1;
475: while (i <= n && j <= m) {
476: if (a[i].value < b[j].value)
477: a[i++].value = 0;
478: else if (a[i].value == b[j].value)
479: a[i++].value = j;
480: else
481: j++;
482: }
483: while (i <= n)
484: a[i++].value = 0;
485: b[m + 1].value = 0;
486: j = 0;
487: while (++j <= m) {
488: c[j] = -b[j].serial;
489: while (b[j + 1].value == b[j].value) {
490: j++;
491: c[j] = b[j].serial;
492: }
493: }
494: c[j] = -1;
495: }
496:
497: /* Code taken from ping.c */
498: static int
499: isqrt(int n)
500: {
501: int y, x = 1;
502:
503: if (n == 0)
504: return (0);
505:
506: do { /* newton was a stinker */
507: y = x;
508: x = n / x;
509: x += y;
510: x /= 2;
1.14 ray 511: } while ((x - y) > 1 || (x - y) < -1);
1.1 joris 512:
513: return (x);
514: }
515:
516: static int
517: stone(int *a, int n, int *b, int *c)
518: {
519: int i, k, y, j, l;
520: int oldc, tc, oldl;
521: u_int numtries;
522:
523: /* XXX move the isqrt() out of the macro to avoid multiple calls */
524: const u_int bound = dflag ? UINT_MAX : MAX(256, (u_int)isqrt(n));
525:
526: k = 0;
1.14 ray 527: c[0] = newcand(0, 0, 0);
1.1 joris 528: for (i = 1; i <= n; i++) {
529: j = a[i];
530: if (j == 0)
531: continue;
532: y = -b[j];
533: oldl = 0;
534: oldc = c[0];
535: numtries = 0;
536: do {
537: if (y <= clist[oldc].y)
538: continue;
539: l = search(c, k, y);
540: if (l != oldl + 1)
541: oldc = c[l - 1];
542: if (l <= k) {
543: if (clist[c[l]].y <= y)
544: continue;
545: tc = c[l];
1.14 ray 546: c[l] = newcand(i, y, oldc);
1.1 joris 547: oldc = tc;
548: oldl = l;
549: numtries++;
550: } else {
1.14 ray 551: c[l] = newcand(i, y, oldc);
1.1 joris 552: k++;
553: break;
554: }
555: } while ((y = b[++j]) > 0 && numtries < bound);
556: }
557: return (k);
558: }
559:
560: static int
561: newcand(int x, int y, int pred)
562: {
1.9 ray 563: struct cand *q;
1.1 joris 564:
565: if (clen == clistlen) {
1.10 ray 566: clistlen = clistlen * 11 / 10;
567: clist = xrealloc(clist, clistlen, sizeof(*clist));
1.1 joris 568: }
569: q = clist + clen;
570: q->x = x;
571: q->y = y;
572: q->pred = pred;
573: return (clen++);
574: }
575:
576: static int
577: search(int *c, int k, int y)
578: {
579: int i, j, l, t;
580:
581: if (clist[c[k]].y < y) /* quick look for typical case */
582: return (k + 1);
583: i = 0;
584: j = k + 1;
585: for (;;) {
586: l = (i + j) / 2;
587: if (l <= i)
588: break;
589: t = clist[c[l]].y;
590: if (t > y)
591: j = l;
592: else if (t < y)
593: i = l;
594: else
595: return (l);
596: }
597: return (l + 1);
598: }
599:
600: static void
601: unravel(int p)
602: {
603: struct cand *q;
604: int i;
605:
1.14 ray 606: for (i = 0; i <= len[0]; i++)
1.1 joris 607: J[i] = i <= pref ? i :
1.14 ray 608: i > len[0] - suff ? i + len[1] - len[0] : 0;
1.1 joris 609: for (q = clist + p; q->y != 0; q = clist + q->pred)
610: J[q->x + pref] = q->y + pref;
611: }
612:
613: /*
614: * Check does double duty:
615: * 1. ferret out any fortuitous correspondences due
616: * to confounding by hashing (which result in "jackpot")
617: * 2. collect random access indexes to the two files
618: */
619: static void
620: check(FILE *f1, FILE *f2)
621: {
622: int i, j, jackpot, c, d;
623: long ctold, ctnew;
624:
625: rewind(f1);
626: rewind(f2);
627: j = 1;
628: ixold[0] = ixnew[0] = 0;
629: jackpot = 0;
630: ctold = ctnew = 0;
1.14 ray 631: for (i = 1; i <= len[0]; i++) {
1.1 joris 632: if (J[i] == 0) {
633: ixold[i] = ctold += skipline(f1);
634: continue;
635: }
636: while (j < J[i]) {
637: ixnew[j] = ctnew += skipline(f2);
638: j++;
639: }
1.17 ! tobias 640: if (bflag == 1 || wflag == 1 || diff_iflag == 1) {
1.1 joris 641: for (;;) {
642: c = getc(f1);
643: d = getc(f2);
644: /*
645: * GNU diff ignores a missing newline
1.14 ray 646: * in one file for -b or -w.
1.1 joris 647: */
648: if ((bflag == 1 || wflag == 1) &&
649: ((c == EOF && d == '\n') ||
650: (c == '\n' && d == EOF))) {
651: break;
652: }
653: ctold++;
654: ctnew++;
655: if (bflag == 1 && isspace(c) && isspace(d)) {
656: do {
657: if (c == '\n')
658: break;
659: ctold++;
660: } while (isspace(c = getc(f1)));
661: do {
662: if (d == '\n')
663: break;
664: ctnew++;
665: } while (isspace(d = getc(f2)));
666: } else if (wflag == 1) {
667: while (isspace(c) && c != '\n') {
668: c = getc(f1);
669: ctold++;
670: }
671: while (isspace(d) && d != '\n') {
672: d = getc(f2);
673: ctnew++;
674: }
675: }
676: if (chrtran[c] != chrtran[d]) {
677: jackpot++;
678: J[i] = 0;
679: if (c != '\n' && c != EOF)
680: ctold += skipline(f1);
681: if (d != '\n' && c != EOF)
682: ctnew += skipline(f2);
683: break;
684: }
685: if (c == '\n' || c == EOF)
686: break;
687: }
688: } else {
689: for (;;) {
690: ctold++;
691: ctnew++;
692: if ((c = getc(f1)) != (d = getc(f2))) {
693: /* jackpot++; */
694: J[i] = 0;
695: if (c != '\n' && c != EOF)
696: ctold += skipline(f1);
697: if (d != '\n' && c != EOF)
698: ctnew += skipline(f2);
699: break;
700: }
701: if (c == '\n' || c == EOF)
702: break;
703: }
704: }
705: ixold[i] = ctold;
706: ixnew[j] = ctnew;
707: j++;
708: }
1.14 ray 709: for (; j <= len[1]; j++)
1.1 joris 710: ixnew[j] = ctnew += skipline(f2);
711: /*
1.14 ray 712: * if (jackpot)
713: * fprintf(stderr, "jackpot\n");
1.1 joris 714: */
715: }
716:
717: /* shellsort CACM #201 */
718: static void
719: sort(struct line *a, int n)
720: {
721: struct line *ai, *aim, w;
722: int j, m = 0, k;
723:
724: if (n == 0)
725: return;
726: for (j = 1; j <= n; j *= 2)
727: m = 2 * j - 1;
728: for (m /= 2; m != 0; m /= 2) {
729: k = n - m;
730: for (j = 1; j <= k; j++) {
731: for (ai = &a[j]; ai > a; ai -= m) {
732: aim = &ai[m];
733: if (aim < ai)
734: break; /* wraparound */
735: if (aim->value > ai[0].value ||
736: (aim->value == ai[0].value &&
737: aim->serial > ai[0].serial))
738: break;
739: w.value = ai[0].value;
740: ai[0].value = aim->value;
741: aim->value = w.value;
742: w.serial = ai[0].serial;
743: ai[0].serial = aim->serial;
744: aim->serial = w.serial;
745: }
746: }
747: }
748: }
749:
750: static void
751: unsort(struct line *f, int l, int *b)
752: {
753: int *a, i;
754:
755: a = xcalloc(l + 1, sizeof(*a));
756: for (i = 1; i <= l; i++)
757: a[f[i].serial] = f[i].value;
758: for (i = 1; i <= l; i++)
759: b[i] = a[i];
760: xfree(a);
761: }
762:
763: static int
764: skipline(FILE *f)
765: {
766: int i, c;
767:
768: for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
769: continue;
770: return (i);
771: }
772:
773: static void
774: output(FILE *f1, FILE *f2)
775: {
776: int m, i0, i1, j0, j1;
777:
778: rewind(f1);
779: rewind(f2);
1.14 ray 780: m = len[0];
1.1 joris 781: J[0] = 0;
1.14 ray 782: J[m + 1] = len[1] + 1;
1.1 joris 783: for (i0 = 1; i0 <= m; i0 = i1 + 1) {
784: while (i0 <= m && J[i0] == J[i0 - 1] + 1)
785: i0++;
786: j0 = J[i0 - 1] + 1;
787: i1 = i0 - 1;
788: while (i1 < m && J[i1 + 1] == 0)
789: i1++;
790: j1 = J[i1 + 1] - 1;
791: J[i1] = j1;
792: change(f1, f2, i0, i1, j0, j1);
793: }
794: if (m == 0)
1.14 ray 795: change(f1, f2, 1, 0, 1, len[1]);
1.1 joris 796: if (diff_format == D_IFDEF) {
797: for (;;) {
798: #define c i0
799: if ((c = getc(f1)) == EOF)
800: return;
801: diff_output("%c", c);
802: }
803: #undef c
804: }
805: if (anychange != 0) {
806: if (diff_format == D_CONTEXT)
807: dump_context_vec(f1, f2);
808: else if (diff_format == D_UNIFIED)
809: dump_unified_vec(f1, f2);
810: }
811: }
812:
1.11 ray 813: static void
1.1 joris 814: range(int a, int b, char *separator)
815: {
816: diff_output("%d", a > b ? b : a);
817: if (a < b)
818: diff_output("%s%d", separator, b);
819: }
820:
1.11 ray 821: static void
1.1 joris 822: uni_range(int a, int b)
823: {
824: if (a < b)
825: diff_output("%d,%d", a, b - a + 1);
826: else if (a == b)
827: diff_output("%d", b);
828: else
829: diff_output("%d,0", b);
830: }
831:
832: static char *
833: preadline(int fd, size_t rlen, off_t off)
834: {
835: char *line;
836: ssize_t nr;
837:
838: line = xmalloc(rlen + 1);
839: if ((nr = pread(fd, line, rlen, off)) < 0) {
840: cvs_log(LP_ERR, "preadline failed");
1.11 ray 841: xfree(line);
1.1 joris 842: return (NULL);
843: }
844: line[nr] = '\0';
845: return (line);
846: }
847:
848: static int
849: ignoreline(char *line)
850: {
851: int ret;
852:
1.11 ray 853: ret = regexec(&ignore_re, line, 0, NULL, 0);
1.1 joris 854: xfree(line);
855: return (ret == 0); /* if it matched, it should be ignored. */
856: }
857:
1.17 ! tobias 858: static void
! 859: diff_head(void)
! 860: {
! 861: char buf[64];
! 862: struct tm *t;
! 863: time_t curr_time;
! 864:
! 865: if (diff_rev1 != NULL) {
! 866: t = gmtime(&stb1.st_mtime);
! 867: } else {
! 868: time(&curr_time);
! 869: t = localtime(&curr_time);
! 870: }
! 871:
! 872: (void)strftime(buf, sizeof(buf), "%d %b %G %H:%M:%S -0000", t);
! 873: diff_output("%s %s %s", diff_format == D_CONTEXT ? "***" : "---",
! 874: diff_file, buf);
! 875:
! 876: if (diff_rev1 != NULL) {
! 877: rcsnum_tostr(diff_rev1, buf, sizeof(buf));
! 878: diff_output("\t%s", buf);
! 879: }
! 880:
! 881: diff_output("\n");
! 882:
! 883: if (diff_rev2 != NULL)
! 884: t = gmtime(&stb2.st_mtime);
! 885: else {
! 886: time(&curr_time);
! 887: t = localtime(&curr_time);
! 888: }
! 889:
! 890: (void)strftime(buf, sizeof(buf), "%d %b %G %H:%M:%S -0000", t);
! 891: diff_output("%s %s %s", diff_format == D_CONTEXT ? "---" : "+++",
! 892: diff_file, buf);
! 893:
! 894: if (diff_rev2 != NULL) {
! 895: rcsnum_tostr(diff_rev2, buf, sizeof(buf));
! 896: diff_output("\t%s", buf);
! 897: }
! 898:
! 899: diff_output("\n");
! 900: }
! 901:
! 902: static void
! 903: rdiff_head(void)
! 904: {
! 905: char buf[64];
! 906: struct tm *t;
! 907: time_t curr_time;
! 908:
! 909: if (diff_rev1 != NULL) {
! 910: t = localtime(&stb1.st_mtime);
! 911: } else {
! 912: time(&curr_time);
! 913: t = localtime(&curr_time);
! 914: }
! 915:
! 916: diff_output("%s ", diff_format == D_CONTEXT ? "***" : "---");
! 917:
! 918: if (diff_rev1 == NULL) {
! 919: diff_output("%s", CVS_PATH_DEVNULL);
! 920: t = gmtime(&stb1.st_atime);
! 921: } else {
! 922: rcsnum_tostr(diff_rev1, buf, sizeof(buf));
! 923: diff_output("%s:%s", diff_file, buf);
! 924: }
! 925:
! 926: (void)strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %G", t);
! 927: diff_output("\t%s\n", buf);
! 928:
! 929: if (diff_rev2 != NULL) {
! 930: t = localtime(&stb2.st_mtime);
! 931: } else {
! 932: time(&curr_time);
! 933: t = localtime(&curr_time);
! 934: }
! 935:
! 936: (void)strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %G", t);
! 937:
! 938: diff_output("%s %s %s\n", diff_format == D_CONTEXT ? "---" : "+++",
! 939: diff_file, buf);
! 940: }
! 941:
1.1 joris 942: /*
943: * Indicate that there is a difference between lines a and b of the from file
944: * to get to lines c to d of the to file. If a is greater then b then there
945: * are no lines in the from file involved and this means that there were
946: * lines appended (beginning at b). If c is greater than d then there are
947: * lines missing from the to file.
948: */
949: static void
950: change(FILE *f1, FILE *f2, int a, int b, int c, int d)
951: {
952: static size_t max_context = 64;
1.14 ray 953: int i;
1.1 joris 954:
955: if (diff_format != D_IFDEF && a > b && c > d)
956: return;
957: if (ignore_pats != NULL) {
958: char *line;
959: /*
960: * All lines in the change, insert, or delete must
961: * match an ignore pattern for the change to be
962: * ignored.
963: */
964: if (a <= b) { /* Changes and deletes. */
965: for (i = a; i <= b; i++) {
966: line = preadline(fileno(f1),
967: ixold[i] - ixold[i - 1], ixold[i - 1]);
968: if (!ignoreline(line))
969: goto proceed;
970: }
971: }
972: if (a > b || c <= d) { /* Changes and inserts. */
973: for (i = c; i <= d; i++) {
974: line = preadline(fileno(f2),
975: ixnew[i] - ixnew[i - 1], ixnew[i - 1]);
976: if (!ignoreline(line))
977: goto proceed;
978: }
979: }
980: return;
981: }
982: proceed:
983: if (diff_format == D_CONTEXT || diff_format == D_UNIFIED) {
984: /*
985: * Allocate change records as needed.
986: */
987: if (context_vec_ptr == context_vec_end - 1) {
988: ptrdiff_t offset = context_vec_ptr - context_vec_start;
989: max_context <<= 1;
1.9 ray 990: context_vec_start = xrealloc(context_vec_start,
991: max_context, sizeof(*context_vec_start));
1.1 joris 992: context_vec_end = context_vec_start + max_context;
993: context_vec_ptr = context_vec_start + offset;
994: }
995: if (anychange == 0) {
996: /*
997: * Print the context/unidiff header first time through.
998: */
1.17 ! tobias 999: if (cvs_cmdop == CVS_OP_RDIFF)
! 1000: rdiff_head();
! 1001: else
! 1002: diff_head();
1.1 joris 1003:
1004: anychange = 1;
1005: } else if (a > context_vec_ptr->b + (2 * context) + 1 &&
1006: c > context_vec_ptr->d + (2 * context) + 1) {
1007: /*
1008: * If this change is more than 'context' lines from the
1009: * previous change, dump the record and reset it.
1010: */
1011: if (diff_format == D_CONTEXT)
1012: dump_context_vec(f1, f2);
1013: else
1014: dump_unified_vec(f1, f2);
1015: }
1016: context_vec_ptr++;
1017: context_vec_ptr->a = a;
1018: context_vec_ptr->b = b;
1019: context_vec_ptr->c = c;
1020: context_vec_ptr->d = d;
1021: return;
1022: }
1023: if (anychange == 0)
1024: anychange = 1;
1025: switch (diff_format) {
1026: case D_BRIEF:
1027: return;
1028: case D_NORMAL:
1029: range(a, b, ",");
1030: diff_output("%c", a > b ? 'a' : c > d ? 'd' : 'c');
1031: if (diff_format == D_NORMAL)
1032: range(c, d, ",");
1033: diff_output("\n");
1034: break;
1035: case D_RCSDIFF:
1036: if (a > b)
1037: diff_output("a%d %d\n", b, d - c + 1);
1038: else {
1039: diff_output("d%d %d\n", a, b - a + 1);
1040:
1041: if (!(c > d)) /* add changed lines */
1042: diff_output("a%d %d\n", b, d - c + 1);
1043: }
1044: break;
1045: }
1046: if (diff_format == D_NORMAL || diff_format == D_IFDEF) {
1047: fetch(ixold, a, b, f1, '<', 1);
1048: if (a <= b && c <= d && diff_format == D_NORMAL)
1049: diff_output("---\n");
1050: }
1051: fetch(ixnew, c, d, f2, diff_format == D_NORMAL ? '>' : '\0', 0);
1052: if (inifdef) {
1053: diff_output("#endif /* %s */\n", ifdefname);
1054: inifdef = 0;
1055: }
1056: }
1057:
1058: static void
1059: fetch(long *f, int a, int b, FILE *lb, int ch, int oldfile)
1060: {
1061: long j, nc;
1062: int i, c, col;
1063:
1064: /*
1065: * When doing #ifdef's, copy down to current line
1066: * if this is the first file, so that stuff makes it to output.
1067: */
1068: if (diff_format == D_IFDEF && oldfile) {
1069: long curpos = ftell(lb);
1070: /* print through if append (a>b), else to (nb: 0 vs 1 orig) */
1071: nc = f[a > b ? b : a - 1] - curpos;
1072: for (i = 0; i < nc; i++)
1073: diff_output("%c", getc(lb));
1074: }
1075: if (a > b)
1076: return;
1077: if (diff_format == D_IFDEF) {
1078: if (inifdef) {
1079: diff_output("#else /* %s%s */\n",
1080: oldfile == 1 ? "!" : "", ifdefname);
1081: } else {
1082: if (oldfile)
1083: diff_output("#ifndef %s\n", ifdefname);
1084: else
1085: diff_output("#ifdef %s\n", ifdefname);
1086: }
1087: inifdef = 1 + oldfile;
1088: }
1089: for (i = a; i <= b; i++) {
1090: fseek(lb, f[i - 1], SEEK_SET);
1091: nc = f[i] - f[i - 1];
1092: if (diff_format != D_IFDEF && ch != '\0') {
1093: diff_output("%c", ch);
1094: if (Tflag == 1 && (diff_format == D_NORMAL ||
1095: diff_format == D_CONTEXT ||
1096: diff_format == D_UNIFIED))
1097: diff_output("\t");
1098: else if (diff_format != D_UNIFIED)
1099: diff_output(" ");
1100: }
1101: col = 0;
1102: for (j = 0; j < nc; j++) {
1103: if ((c = getc(lb)) == EOF) {
1104: if (diff_format == D_RCSDIFF)
1105: cvs_log(LP_ERR,
1106: "No newline at end of file");
1107: else
1108: diff_output("\n\\ No newline at end of "
1.15 joris 1109: "file\n");
1.1 joris 1110: return;
1111: }
1112: if (c == '\t' && tflag == 1) {
1113: do {
1114: diff_output(" ");
1115: } while (++col & 7);
1116: } else {
1117: diff_output("%c", c);
1118: col++;
1119: }
1120: }
1121: }
1122: }
1123:
1124: /*
1125: * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
1126: */
1127: static int
1128: readhash(FILE *f)
1129: {
1130: int i, t, space;
1131: int sum;
1132:
1133: sum = 1;
1134: space = 0;
1135: if (bflag != 1 && wflag != 1) {
1.17 ! tobias 1136: if (diff_iflag == 1)
1.1 joris 1137: for (i = 0; (t = getc(f)) != '\n'; i++) {
1138: if (t == EOF) {
1139: if (i == 0)
1140: return (0);
1141: break;
1142: }
1143: sum = sum * 127 + chrtran[t];
1144: }
1145: else
1146: for (i = 0; (t = getc(f)) != '\n'; i++) {
1147: if (t == EOF) {
1148: if (i == 0)
1149: return (0);
1150: break;
1151: }
1152: sum = sum * 127 + t;
1153: }
1154: } else {
1155: for (i = 0;;) {
1156: switch (t = getc(f)) {
1157: case '\t':
1.14 ray 1158: case '\r':
1159: case '\v':
1160: case '\f':
1.1 joris 1161: case ' ':
1162: space++;
1163: continue;
1164: default:
1165: if (space != 0 && wflag != 1) {
1166: i++;
1167: space = 0;
1168: }
1169: sum = sum * 127 + chrtran[t];
1170: i++;
1171: continue;
1172: case EOF:
1173: if (i == 0)
1174: return (0);
1175: /* FALLTHROUGH */
1176: case '\n':
1177: break;
1178: }
1179: break;
1180: }
1181: }
1182: /*
1183: * There is a remote possibility that we end up with a zero sum.
1184: * Zero is used as an EOF marker, so return 1 instead.
1185: */
1186: return (sum == 0 ? 1 : sum);
1187: }
1188:
1189: static int
1190: asciifile(FILE *f)
1191: {
1.14 ray 1192: unsigned char buf[BUFSIZ];
1.1 joris 1193: size_t i, cnt;
1194:
1195: if (aflag == 1 || f == NULL)
1196: return (1);
1197:
1198: rewind(f);
1.11 ray 1199: cnt = fread(buf, 1, sizeof(buf), f);
1.1 joris 1200: for (i = 0; i < cnt; i++)
1201: if (!isprint(buf[i]) && !isspace(buf[i]))
1202: return (0);
1203: return (1);
1204: }
1205:
1.5 xsa 1206: #define begins_with(s, pre) (strncmp(s, pre, sizeof(pre)-1) == 0)
1207:
1.14 ray 1208: static char *
1.1 joris 1209: match_function(const long *f, int pos, FILE *fp)
1210: {
1211: unsigned char buf[FUNCTION_CONTEXT_SIZE];
1212: size_t nc;
1213: int last = lastline;
1214: char *p;
1.5 xsa 1215: char *state = NULL;
1.1 joris 1216:
1217: lastline = pos;
1218: while (pos > last) {
1219: fseek(fp, f[pos - 1], SEEK_SET);
1220: nc = f[pos] - f[pos - 1];
1221: if (nc >= sizeof(buf))
1222: nc = sizeof(buf) - 1;
1.11 ray 1223: nc = fread(buf, 1, nc, fp);
1.1 joris 1224: if (nc > 0) {
1225: buf[nc] = '\0';
1226: p = strchr((const char *)buf, '\n');
1227: if (p != NULL)
1228: *p = '\0';
1229: if (isalpha(buf[0]) || buf[0] == '_' || buf[0] == '$') {
1.5 xsa 1230: if (begins_with(buf, "private:")) {
1231: if (!state)
1232: state = " (private)";
1233: } else if (begins_with(buf, "protected:")) {
1234: if (!state)
1235: state = " (protected)";
1236: } else if (begins_with(buf, "public:")) {
1237: if (!state)
1238: state = " (public)";
1239: } else {
1.14 ray 1240: strlcpy(lastbuf, buf,
1.5 xsa 1241: sizeof lastbuf);
1242: if (state)
1243: strlcat(lastbuf, state,
1244: sizeof lastbuf);
1245: lastmatchline = pos;
1246: return lastbuf;
1247: }
1.1 joris 1248: }
1249: }
1250: pos--;
1251: }
1.14 ray 1252: return lastmatchline > 0 ? lastbuf : NULL;
1.1 joris 1253: }
1254:
1255: /* dump accumulated "context" diff changes */
1256: static void
1257: dump_context_vec(FILE *f1, FILE *f2)
1258: {
1259: struct context_vec *cvp = context_vec_start;
1260: int lowa, upb, lowc, upd, do_output;
1261: int a, b, c, d;
1262: char ch, *f;
1263:
1264: if (context_vec_start > context_vec_ptr)
1265: return;
1266:
1267: b = d = 0; /* gcc */
1268: lowa = MAX(1, cvp->a - context);
1.14 ray 1269: upb = MIN(len[0], context_vec_ptr->b + context);
1.1 joris 1270: lowc = MAX(1, cvp->c - context);
1.14 ray 1271: upd = MIN(len[1], context_vec_ptr->d + context);
1.1 joris 1272:
1273: diff_output("***************");
1.2 joris 1274: if (diff_pflag == 1) {
1.14 ray 1275: f = match_function(ixold, lowa-1, f1);
1.1 joris 1276: if (f != NULL) {
1277: diff_output(" ");
1278: diff_output("%s", f);
1279: }
1280: }
1281: diff_output("\n*** ");
1282: range(lowa, upb, ",");
1283: diff_output(" ****\n");
1284:
1285: /*
1286: * Output changes to the "old" file. The first loop suppresses
1287: * output if there were no changes to the "old" file (we'll see
1288: * the "old" lines as context in the "new" list).
1289: */
1290: do_output = 0;
1291: for (; cvp <= context_vec_ptr; cvp++)
1292: if (cvp->a <= cvp->b) {
1293: cvp = context_vec_start;
1294: do_output++;
1295: break;
1296: }
1.14 ray 1297: if (do_output) {
1.1 joris 1298: while (cvp <= context_vec_ptr) {
1299: a = cvp->a;
1300: b = cvp->b;
1301: c = cvp->c;
1302: d = cvp->d;
1303:
1304: if (a <= b && c <= d)
1305: ch = 'c';
1306: else
1307: ch = (a <= b) ? 'd' : 'a';
1308:
1309: if (ch == 'a')
1310: fetch(ixold, lowa, b, f1, ' ', 0);
1311: else {
1312: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1313: fetch(ixold, a, b, f1,
1314: ch == 'c' ? '!' : '-', 0);
1315: }
1316: lowa = b + 1;
1317: cvp++;
1318: }
1319: fetch(ixold, b + 1, upb, f1, ' ', 0);
1320: }
1321: /* output changes to the "new" file */
1322: diff_output("--- ");
1323: range(lowc, upd, ",");
1324: diff_output(" ----\n");
1325:
1326: do_output = 0;
1327: for (cvp = context_vec_start; cvp <= context_vec_ptr; cvp++)
1328: if (cvp->c <= cvp->d) {
1329: cvp = context_vec_start;
1330: do_output++;
1331: break;
1332: }
1.14 ray 1333: if (do_output) {
1.1 joris 1334: while (cvp <= context_vec_ptr) {
1335: a = cvp->a;
1336: b = cvp->b;
1337: c = cvp->c;
1338: d = cvp->d;
1339:
1340: if (a <= b && c <= d)
1341: ch = 'c';
1342: else
1343: ch = (a <= b) ? 'd' : 'a';
1344:
1345: if (ch == 'd')
1346: fetch(ixnew, lowc, d, f2, ' ', 0);
1347: else {
1348: fetch(ixnew, lowc, c - 1, f2, ' ', 0);
1349: fetch(ixnew, c, d, f2,
1350: ch == 'c' ? '!' : '+', 0);
1351: }
1352: lowc = d + 1;
1353: cvp++;
1354: }
1355: fetch(ixnew, d + 1, upd, f2, ' ', 0);
1356: }
1357: context_vec_ptr = context_vec_start - 1;
1358: }
1359:
1360: /* dump accumulated "unified" diff changes */
1361: static void
1362: dump_unified_vec(FILE *f1, FILE *f2)
1363: {
1364: struct context_vec *cvp = context_vec_start;
1365: int lowa, upb, lowc, upd;
1366: int a, b, c, d;
1367: char ch, *f;
1368:
1369: if (context_vec_start > context_vec_ptr)
1370: return;
1371:
1372: b = d = 0; /* gcc */
1373: lowa = MAX(1, cvp->a - context);
1.14 ray 1374: upb = MIN(len[0], context_vec_ptr->b + context);
1.1 joris 1375: lowc = MAX(1, cvp->c - context);
1.14 ray 1376: upd = MIN(len[1], context_vec_ptr->d + context);
1.1 joris 1377:
1378: diff_output("@@ -");
1379: uni_range(lowa, upb);
1380: diff_output(" +");
1381: uni_range(lowc, upd);
1382: diff_output(" @@");
1.2 joris 1383: if (diff_pflag == 1) {
1.14 ray 1384: f = match_function(ixold, lowa-1, f1);
1.1 joris 1385: if (f != NULL) {
1386: diff_output(" ");
1387: diff_output("%s", f);
1388: }
1389: }
1390: diff_output("\n");
1391:
1392: /*
1393: * Output changes in "unified" diff format--the old and new lines
1394: * are printed together.
1395: */
1396: for (; cvp <= context_vec_ptr; cvp++) {
1397: a = cvp->a;
1398: b = cvp->b;
1399: c = cvp->c;
1400: d = cvp->d;
1401:
1402: /*
1403: * c: both new and old changes
1404: * d: only changes in the old file
1405: * a: only changes in the new file
1406: */
1407: if (a <= b && c <= d)
1408: ch = 'c';
1409: else
1410: ch = (a <= b) ? 'd' : 'a';
1411:
1412: switch (ch) {
1413: case 'c':
1414: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1415: fetch(ixold, a, b, f1, '-', 0);
1416: fetch(ixnew, c, d, f2, '+', 0);
1417: break;
1418: case 'd':
1419: fetch(ixold, lowa, a - 1, f1, ' ', 0);
1420: fetch(ixold, a, b, f1, '-', 0);
1421: break;
1422: case 'a':
1423: fetch(ixnew, lowc, c - 1, f2, ' ', 0);
1424: fetch(ixnew, c, d, f2, '+', 0);
1425: break;
1426: }
1427: lowa = b + 1;
1428: lowc = d + 1;
1429: }
1430: fetch(ixnew, d + 1, upd, f2, ' ', 0);
1431:
1432: context_vec_ptr = context_vec_start - 1;
1433: }
1434:
1435: void
1436: diff_output(const char *fmt, ...)
1437: {
1438: va_list vap;
1439: int i;
1440: char *str;
1441:
1442: va_start(vap, fmt);
1443: i = vasprintf(&str, fmt, vap);
1444: va_end(vap);
1445: if (i == -1)
1446: fatal("diff_output: %s", strerror(errno));
1447: if (diffbuf != NULL)
1448: cvs_buf_append(diffbuf, str, strlen(str));
1449: else
1450: cvs_printf("%s", str);
1451: xfree(str);
1452: }