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