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