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