1*86d7f5d3SJohn Marino /* Analyze file differences for GNU DIFF.
2*86d7f5d3SJohn Marino Copyright (C) 1988, 1989, 1992, 1993, 1997 Free Software Foundation, Inc.
3*86d7f5d3SJohn Marino
4*86d7f5d3SJohn Marino This file is part of GNU DIFF.
5*86d7f5d3SJohn Marino
6*86d7f5d3SJohn Marino GNU DIFF is free software; you can redistribute it and/or modify
7*86d7f5d3SJohn Marino it under the terms of the GNU General Public License as published by
8*86d7f5d3SJohn Marino the Free Software Foundation; either version 2, or (at your option)
9*86d7f5d3SJohn Marino any later version.
10*86d7f5d3SJohn Marino
11*86d7f5d3SJohn Marino GNU DIFF is distributed in the hope that it will be useful,
12*86d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
13*86d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*86d7f5d3SJohn Marino GNU General Public License for more details.
15*86d7f5d3SJohn Marino
16*86d7f5d3SJohn Marino */
17*86d7f5d3SJohn Marino
18*86d7f5d3SJohn Marino /* The basic algorithm is described in:
19*86d7f5d3SJohn Marino "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
20*86d7f5d3SJohn Marino Algorithmica Vol. 1 No. 2, 1986, pp. 251-266;
21*86d7f5d3SJohn Marino see especially section 4.2, which describes the variation used below.
22*86d7f5d3SJohn Marino Unless the --minimal option is specified, this code uses the TOO_EXPENSIVE
23*86d7f5d3SJohn Marino heuristic, by Paul Eggert, to limit the cost to O(N**1.5 log N)
24*86d7f5d3SJohn Marino at the price of producing suboptimal output for large inputs with
25*86d7f5d3SJohn Marino many differences.
26*86d7f5d3SJohn Marino
27*86d7f5d3SJohn Marino The basic algorithm was independently discovered as described in:
28*86d7f5d3SJohn Marino "Algorithms for Approximate String Matching", E. Ukkonen,
29*86d7f5d3SJohn Marino Information and Control Vol. 64, 1985, pp. 100-118. */
30*86d7f5d3SJohn Marino
31*86d7f5d3SJohn Marino #include "diff.h"
32*86d7f5d3SJohn Marino #include "cmpbuf.h"
33*86d7f5d3SJohn Marino
34*86d7f5d3SJohn Marino extern int no_discards;
35*86d7f5d3SJohn Marino
36*86d7f5d3SJohn Marino static int *xvec, *yvec; /* Vectors being compared. */
37*86d7f5d3SJohn Marino static int *fdiag; /* Vector, indexed by diagonal, containing
38*86d7f5d3SJohn Marino 1 + the X coordinate of the point furthest
39*86d7f5d3SJohn Marino along the given diagonal in the forward
40*86d7f5d3SJohn Marino search of the edit matrix. */
41*86d7f5d3SJohn Marino static int *bdiag; /* Vector, indexed by diagonal, containing
42*86d7f5d3SJohn Marino the X coordinate of the point furthest
43*86d7f5d3SJohn Marino along the given diagonal in the backward
44*86d7f5d3SJohn Marino search of the edit matrix. */
45*86d7f5d3SJohn Marino static int too_expensive; /* Edit scripts longer than this are too
46*86d7f5d3SJohn Marino expensive to compute. */
47*86d7f5d3SJohn Marino
48*86d7f5d3SJohn Marino #define SNAKE_LIMIT 20 /* Snakes bigger than this are considered `big'. */
49*86d7f5d3SJohn Marino
50*86d7f5d3SJohn Marino struct partition
51*86d7f5d3SJohn Marino {
52*86d7f5d3SJohn Marino int xmid, ymid; /* Midpoints of this partition. */
53*86d7f5d3SJohn Marino int lo_minimal; /* Nonzero if low half will be analyzed minimally. */
54*86d7f5d3SJohn Marino int hi_minimal; /* Likewise for high half. */
55*86d7f5d3SJohn Marino };
56*86d7f5d3SJohn Marino
57*86d7f5d3SJohn Marino static int diag PARAMS((int, int, int, int, int, struct partition *));
58*86d7f5d3SJohn Marino static struct change *add_change PARAMS((int, int, int, int, struct change *));
59*86d7f5d3SJohn Marino static struct change *build_reverse_script PARAMS((struct file_data const[]));
60*86d7f5d3SJohn Marino static struct change *build_script PARAMS((struct file_data const[]));
61*86d7f5d3SJohn Marino static void briefly_report PARAMS((int, struct file_data const[]));
62*86d7f5d3SJohn Marino static void compareseq PARAMS((int, int, int, int, int));
63*86d7f5d3SJohn Marino static void discard_confusing_lines PARAMS((struct file_data[]));
64*86d7f5d3SJohn Marino static void shift_boundaries PARAMS((struct file_data[]));
65*86d7f5d3SJohn Marino
66*86d7f5d3SJohn Marino /* Find the midpoint of the shortest edit script for a specified
67*86d7f5d3SJohn Marino portion of the two files.
68*86d7f5d3SJohn Marino
69*86d7f5d3SJohn Marino Scan from the beginnings of the files, and simultaneously from the ends,
70*86d7f5d3SJohn Marino doing a breadth-first search through the space of edit-sequence.
71*86d7f5d3SJohn Marino When the two searches meet, we have found the midpoint of the shortest
72*86d7f5d3SJohn Marino edit sequence.
73*86d7f5d3SJohn Marino
74*86d7f5d3SJohn Marino If MINIMAL is nonzero, find the minimal edit script regardless
75*86d7f5d3SJohn Marino of expense. Otherwise, if the search is too expensive, use
76*86d7f5d3SJohn Marino heuristics to stop the search and report a suboptimal answer.
77*86d7f5d3SJohn Marino
78*86d7f5d3SJohn Marino Set PART->(XMID,YMID) to the midpoint (XMID,YMID). The diagonal number
79*86d7f5d3SJohn Marino XMID - YMID equals the number of inserted lines minus the number
80*86d7f5d3SJohn Marino of deleted lines (counting only lines before the midpoint).
81*86d7f5d3SJohn Marino Return the approximate edit cost; this is the total number of
82*86d7f5d3SJohn Marino lines inserted or deleted (counting only lines before the midpoint),
83*86d7f5d3SJohn Marino unless a heuristic is used to terminate the search prematurely.
84*86d7f5d3SJohn Marino
85*86d7f5d3SJohn Marino Set PART->LEFT_MINIMAL to nonzero iff the minimal edit script for the
86*86d7f5d3SJohn Marino left half of the partition is known; similarly for PART->RIGHT_MINIMAL.
87*86d7f5d3SJohn Marino
88*86d7f5d3SJohn Marino This function assumes that the first lines of the specified portions
89*86d7f5d3SJohn Marino of the two files do not match, and likewise that the last lines do not
90*86d7f5d3SJohn Marino match. The caller must trim matching lines from the beginning and end
91*86d7f5d3SJohn Marino of the portions it is going to specify.
92*86d7f5d3SJohn Marino
93*86d7f5d3SJohn Marino If we return the "wrong" partitions,
94*86d7f5d3SJohn Marino the worst this can do is cause suboptimal diff output.
95*86d7f5d3SJohn Marino It cannot cause incorrect diff output. */
96*86d7f5d3SJohn Marino
97*86d7f5d3SJohn Marino static int
diag(xoff,xlim,yoff,ylim,minimal,part)98*86d7f5d3SJohn Marino diag (xoff, xlim, yoff, ylim, minimal, part)
99*86d7f5d3SJohn Marino int xoff, xlim, yoff, ylim, minimal;
100*86d7f5d3SJohn Marino struct partition *part;
101*86d7f5d3SJohn Marino {
102*86d7f5d3SJohn Marino int *const fd = fdiag; /* Give the compiler a chance. */
103*86d7f5d3SJohn Marino int *const bd = bdiag; /* Additional help for the compiler. */
104*86d7f5d3SJohn Marino int const *const xv = xvec; /* Still more help for the compiler. */
105*86d7f5d3SJohn Marino int const *const yv = yvec; /* And more and more . . . */
106*86d7f5d3SJohn Marino int const dmin = xoff - ylim; /* Minimum valid diagonal. */
107*86d7f5d3SJohn Marino int const dmax = xlim - yoff; /* Maximum valid diagonal. */
108*86d7f5d3SJohn Marino int const fmid = xoff - yoff; /* Center diagonal of top-down search. */
109*86d7f5d3SJohn Marino int const bmid = xlim - ylim; /* Center diagonal of bottom-up search. */
110*86d7f5d3SJohn Marino int fmin = fmid, fmax = fmid; /* Limits of top-down search. */
111*86d7f5d3SJohn Marino int bmin = bmid, bmax = bmid; /* Limits of bottom-up search. */
112*86d7f5d3SJohn Marino int c; /* Cost. */
113*86d7f5d3SJohn Marino int odd = (fmid - bmid) & 1; /* True if southeast corner is on an odd
114*86d7f5d3SJohn Marino diagonal with respect to the northwest. */
115*86d7f5d3SJohn Marino
116*86d7f5d3SJohn Marino fd[fmid] = xoff;
117*86d7f5d3SJohn Marino bd[bmid] = xlim;
118*86d7f5d3SJohn Marino
119*86d7f5d3SJohn Marino for (c = 1;; ++c)
120*86d7f5d3SJohn Marino {
121*86d7f5d3SJohn Marino int d; /* Active diagonal. */
122*86d7f5d3SJohn Marino int big_snake = 0;
123*86d7f5d3SJohn Marino
124*86d7f5d3SJohn Marino /* Extend the top-down search by an edit step in each diagonal. */
125*86d7f5d3SJohn Marino fmin > dmin ? fd[--fmin - 1] = -1 : ++fmin;
126*86d7f5d3SJohn Marino fmax < dmax ? fd[++fmax + 1] = -1 : --fmax;
127*86d7f5d3SJohn Marino for (d = fmax; d >= fmin; d -= 2)
128*86d7f5d3SJohn Marino {
129*86d7f5d3SJohn Marino int x, y, oldx, tlo = fd[d - 1], thi = fd[d + 1];
130*86d7f5d3SJohn Marino
131*86d7f5d3SJohn Marino if (tlo >= thi)
132*86d7f5d3SJohn Marino x = tlo + 1;
133*86d7f5d3SJohn Marino else
134*86d7f5d3SJohn Marino x = thi;
135*86d7f5d3SJohn Marino oldx = x;
136*86d7f5d3SJohn Marino y = x - d;
137*86d7f5d3SJohn Marino while (x < xlim && y < ylim && xv[x] == yv[y])
138*86d7f5d3SJohn Marino ++x, ++y;
139*86d7f5d3SJohn Marino if (x - oldx > SNAKE_LIMIT)
140*86d7f5d3SJohn Marino big_snake = 1;
141*86d7f5d3SJohn Marino fd[d] = x;
142*86d7f5d3SJohn Marino if (odd && bmin <= d && d <= bmax && bd[d] <= x)
143*86d7f5d3SJohn Marino {
144*86d7f5d3SJohn Marino part->xmid = x;
145*86d7f5d3SJohn Marino part->ymid = y;
146*86d7f5d3SJohn Marino part->lo_minimal = part->hi_minimal = 1;
147*86d7f5d3SJohn Marino return 2 * c - 1;
148*86d7f5d3SJohn Marino }
149*86d7f5d3SJohn Marino }
150*86d7f5d3SJohn Marino
151*86d7f5d3SJohn Marino /* Similarly extend the bottom-up search. */
152*86d7f5d3SJohn Marino bmin > dmin ? bd[--bmin - 1] = INT_MAX : ++bmin;
153*86d7f5d3SJohn Marino bmax < dmax ? bd[++bmax + 1] = INT_MAX : --bmax;
154*86d7f5d3SJohn Marino for (d = bmax; d >= bmin; d -= 2)
155*86d7f5d3SJohn Marino {
156*86d7f5d3SJohn Marino int x, y, oldx, tlo = bd[d - 1], thi = bd[d + 1];
157*86d7f5d3SJohn Marino
158*86d7f5d3SJohn Marino if (tlo < thi)
159*86d7f5d3SJohn Marino x = tlo;
160*86d7f5d3SJohn Marino else
161*86d7f5d3SJohn Marino x = thi - 1;
162*86d7f5d3SJohn Marino oldx = x;
163*86d7f5d3SJohn Marino y = x - d;
164*86d7f5d3SJohn Marino while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1])
165*86d7f5d3SJohn Marino --x, --y;
166*86d7f5d3SJohn Marino if (oldx - x > SNAKE_LIMIT)
167*86d7f5d3SJohn Marino big_snake = 1;
168*86d7f5d3SJohn Marino bd[d] = x;
169*86d7f5d3SJohn Marino if (!odd && fmin <= d && d <= fmax && x <= fd[d])
170*86d7f5d3SJohn Marino {
171*86d7f5d3SJohn Marino part->xmid = x;
172*86d7f5d3SJohn Marino part->ymid = y;
173*86d7f5d3SJohn Marino part->lo_minimal = part->hi_minimal = 1;
174*86d7f5d3SJohn Marino return 2 * c;
175*86d7f5d3SJohn Marino }
176*86d7f5d3SJohn Marino }
177*86d7f5d3SJohn Marino
178*86d7f5d3SJohn Marino if (minimal)
179*86d7f5d3SJohn Marino continue;
180*86d7f5d3SJohn Marino
181*86d7f5d3SJohn Marino /* Heuristic: check occasionally for a diagonal that has made
182*86d7f5d3SJohn Marino lots of progress compared with the edit distance.
183*86d7f5d3SJohn Marino If we have any such, find the one that has made the most
184*86d7f5d3SJohn Marino progress and return it as if it had succeeded.
185*86d7f5d3SJohn Marino
186*86d7f5d3SJohn Marino With this heuristic, for files with a constant small density
187*86d7f5d3SJohn Marino of changes, the algorithm is linear in the file size. */
188*86d7f5d3SJohn Marino
189*86d7f5d3SJohn Marino if (c > 200 && big_snake && heuristic)
190*86d7f5d3SJohn Marino {
191*86d7f5d3SJohn Marino int best;
192*86d7f5d3SJohn Marino
193*86d7f5d3SJohn Marino best = 0;
194*86d7f5d3SJohn Marino for (d = fmax; d >= fmin; d -= 2)
195*86d7f5d3SJohn Marino {
196*86d7f5d3SJohn Marino int dd = d - fmid;
197*86d7f5d3SJohn Marino int x = fd[d];
198*86d7f5d3SJohn Marino int y = x - d;
199*86d7f5d3SJohn Marino int v = (x - xoff) * 2 - dd;
200*86d7f5d3SJohn Marino if (v > 12 * (c + (dd < 0 ? -dd : dd)))
201*86d7f5d3SJohn Marino {
202*86d7f5d3SJohn Marino if (v > best
203*86d7f5d3SJohn Marino && xoff + SNAKE_LIMIT <= x && x < xlim
204*86d7f5d3SJohn Marino && yoff + SNAKE_LIMIT <= y && y < ylim)
205*86d7f5d3SJohn Marino {
206*86d7f5d3SJohn Marino /* We have a good enough best diagonal;
207*86d7f5d3SJohn Marino now insist that it end with a significant snake. */
208*86d7f5d3SJohn Marino int k;
209*86d7f5d3SJohn Marino
210*86d7f5d3SJohn Marino for (k = 1; xv[x - k] == yv[y - k]; k++)
211*86d7f5d3SJohn Marino if (k == SNAKE_LIMIT)
212*86d7f5d3SJohn Marino {
213*86d7f5d3SJohn Marino best = v;
214*86d7f5d3SJohn Marino part->xmid = x;
215*86d7f5d3SJohn Marino part->ymid = y;
216*86d7f5d3SJohn Marino break;
217*86d7f5d3SJohn Marino }
218*86d7f5d3SJohn Marino }
219*86d7f5d3SJohn Marino }
220*86d7f5d3SJohn Marino }
221*86d7f5d3SJohn Marino if (best > 0)
222*86d7f5d3SJohn Marino {
223*86d7f5d3SJohn Marino part->lo_minimal = 1;
224*86d7f5d3SJohn Marino part->hi_minimal = 0;
225*86d7f5d3SJohn Marino return 2 * c - 1;
226*86d7f5d3SJohn Marino }
227*86d7f5d3SJohn Marino
228*86d7f5d3SJohn Marino best = 0;
229*86d7f5d3SJohn Marino for (d = bmax; d >= bmin; d -= 2)
230*86d7f5d3SJohn Marino {
231*86d7f5d3SJohn Marino int dd = d - bmid;
232*86d7f5d3SJohn Marino int x = bd[d];
233*86d7f5d3SJohn Marino int y = x - d;
234*86d7f5d3SJohn Marino int v = (xlim - x) * 2 + dd;
235*86d7f5d3SJohn Marino if (v > 12 * (c + (dd < 0 ? -dd : dd)))
236*86d7f5d3SJohn Marino {
237*86d7f5d3SJohn Marino if (v > best
238*86d7f5d3SJohn Marino && xoff < x && x <= xlim - SNAKE_LIMIT
239*86d7f5d3SJohn Marino && yoff < y && y <= ylim - SNAKE_LIMIT)
240*86d7f5d3SJohn Marino {
241*86d7f5d3SJohn Marino /* We have a good enough best diagonal;
242*86d7f5d3SJohn Marino now insist that it end with a significant snake. */
243*86d7f5d3SJohn Marino int k;
244*86d7f5d3SJohn Marino
245*86d7f5d3SJohn Marino for (k = 0; xv[x + k] == yv[y + k]; k++)
246*86d7f5d3SJohn Marino if (k == SNAKE_LIMIT - 1)
247*86d7f5d3SJohn Marino {
248*86d7f5d3SJohn Marino best = v;
249*86d7f5d3SJohn Marino part->xmid = x;
250*86d7f5d3SJohn Marino part->ymid = y;
251*86d7f5d3SJohn Marino break;
252*86d7f5d3SJohn Marino }
253*86d7f5d3SJohn Marino }
254*86d7f5d3SJohn Marino }
255*86d7f5d3SJohn Marino }
256*86d7f5d3SJohn Marino if (best > 0)
257*86d7f5d3SJohn Marino {
258*86d7f5d3SJohn Marino part->lo_minimal = 0;
259*86d7f5d3SJohn Marino part->hi_minimal = 1;
260*86d7f5d3SJohn Marino return 2 * c - 1;
261*86d7f5d3SJohn Marino }
262*86d7f5d3SJohn Marino }
263*86d7f5d3SJohn Marino
264*86d7f5d3SJohn Marino /* Heuristic: if we've gone well beyond the call of duty,
265*86d7f5d3SJohn Marino give up and report halfway between our best results so far. */
266*86d7f5d3SJohn Marino if (c >= too_expensive)
267*86d7f5d3SJohn Marino {
268*86d7f5d3SJohn Marino int fxybest, fxbest;
269*86d7f5d3SJohn Marino int bxybest, bxbest;
270*86d7f5d3SJohn Marino
271*86d7f5d3SJohn Marino fxbest = bxbest = 0; /* Pacify `gcc -Wall'. */
272*86d7f5d3SJohn Marino
273*86d7f5d3SJohn Marino /* Find forward diagonal that maximizes X + Y. */
274*86d7f5d3SJohn Marino fxybest = -1;
275*86d7f5d3SJohn Marino for (d = fmax; d >= fmin; d -= 2)
276*86d7f5d3SJohn Marino {
277*86d7f5d3SJohn Marino int x = min (fd[d], xlim);
278*86d7f5d3SJohn Marino int y = x - d;
279*86d7f5d3SJohn Marino if (ylim < y)
280*86d7f5d3SJohn Marino x = ylim + d, y = ylim;
281*86d7f5d3SJohn Marino if (fxybest < x + y)
282*86d7f5d3SJohn Marino {
283*86d7f5d3SJohn Marino fxybest = x + y;
284*86d7f5d3SJohn Marino fxbest = x;
285*86d7f5d3SJohn Marino }
286*86d7f5d3SJohn Marino }
287*86d7f5d3SJohn Marino
288*86d7f5d3SJohn Marino /* Find backward diagonal that minimizes X + Y. */
289*86d7f5d3SJohn Marino bxybest = INT_MAX;
290*86d7f5d3SJohn Marino for (d = bmax; d >= bmin; d -= 2)
291*86d7f5d3SJohn Marino {
292*86d7f5d3SJohn Marino int x = max (xoff, bd[d]);
293*86d7f5d3SJohn Marino int y = x - d;
294*86d7f5d3SJohn Marino if (y < yoff)
295*86d7f5d3SJohn Marino x = yoff + d, y = yoff;
296*86d7f5d3SJohn Marino if (x + y < bxybest)
297*86d7f5d3SJohn Marino {
298*86d7f5d3SJohn Marino bxybest = x + y;
299*86d7f5d3SJohn Marino bxbest = x;
300*86d7f5d3SJohn Marino }
301*86d7f5d3SJohn Marino }
302*86d7f5d3SJohn Marino
303*86d7f5d3SJohn Marino /* Use the better of the two diagonals. */
304*86d7f5d3SJohn Marino if ((xlim + ylim) - bxybest < fxybest - (xoff + yoff))
305*86d7f5d3SJohn Marino {
306*86d7f5d3SJohn Marino part->xmid = fxbest;
307*86d7f5d3SJohn Marino part->ymid = fxybest - fxbest;
308*86d7f5d3SJohn Marino part->lo_minimal = 1;
309*86d7f5d3SJohn Marino part->hi_minimal = 0;
310*86d7f5d3SJohn Marino }
311*86d7f5d3SJohn Marino else
312*86d7f5d3SJohn Marino {
313*86d7f5d3SJohn Marino part->xmid = bxbest;
314*86d7f5d3SJohn Marino part->ymid = bxybest - bxbest;
315*86d7f5d3SJohn Marino part->lo_minimal = 0;
316*86d7f5d3SJohn Marino part->hi_minimal = 1;
317*86d7f5d3SJohn Marino }
318*86d7f5d3SJohn Marino return 2 * c - 1;
319*86d7f5d3SJohn Marino }
320*86d7f5d3SJohn Marino }
321*86d7f5d3SJohn Marino }
322*86d7f5d3SJohn Marino
323*86d7f5d3SJohn Marino /* Compare in detail contiguous subsequences of the two files
324*86d7f5d3SJohn Marino which are known, as a whole, to match each other.
325*86d7f5d3SJohn Marino
326*86d7f5d3SJohn Marino The results are recorded in the vectors files[N].changed_flag, by
327*86d7f5d3SJohn Marino storing a 1 in the element for each line that is an insertion or deletion.
328*86d7f5d3SJohn Marino
329*86d7f5d3SJohn Marino The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
330*86d7f5d3SJohn Marino
331*86d7f5d3SJohn Marino Note that XLIM, YLIM are exclusive bounds.
332*86d7f5d3SJohn Marino All line numbers are origin-0 and discarded lines are not counted.
333*86d7f5d3SJohn Marino
334*86d7f5d3SJohn Marino If MINIMAL is nonzero, find a minimal difference no matter how
335*86d7f5d3SJohn Marino expensive it is. */
336*86d7f5d3SJohn Marino
337*86d7f5d3SJohn Marino static void
compareseq(xoff,xlim,yoff,ylim,minimal)338*86d7f5d3SJohn Marino compareseq (xoff, xlim, yoff, ylim, minimal)
339*86d7f5d3SJohn Marino int xoff, xlim, yoff, ylim, minimal;
340*86d7f5d3SJohn Marino {
341*86d7f5d3SJohn Marino int * const xv = xvec; /* Help the compiler. */
342*86d7f5d3SJohn Marino int * const yv = yvec;
343*86d7f5d3SJohn Marino
344*86d7f5d3SJohn Marino /* Slide down the bottom initial diagonal. */
345*86d7f5d3SJohn Marino while (xoff < xlim && yoff < ylim && xv[xoff] == yv[yoff])
346*86d7f5d3SJohn Marino ++xoff, ++yoff;
347*86d7f5d3SJohn Marino /* Slide up the top initial diagonal. */
348*86d7f5d3SJohn Marino while (xlim > xoff && ylim > yoff && xv[xlim - 1] == yv[ylim - 1])
349*86d7f5d3SJohn Marino --xlim, --ylim;
350*86d7f5d3SJohn Marino
351*86d7f5d3SJohn Marino /* Handle simple cases. */
352*86d7f5d3SJohn Marino if (xoff == xlim)
353*86d7f5d3SJohn Marino while (yoff < ylim)
354*86d7f5d3SJohn Marino files[1].changed_flag[files[1].realindexes[yoff++]] = 1;
355*86d7f5d3SJohn Marino else if (yoff == ylim)
356*86d7f5d3SJohn Marino while (xoff < xlim)
357*86d7f5d3SJohn Marino files[0].changed_flag[files[0].realindexes[xoff++]] = 1;
358*86d7f5d3SJohn Marino else
359*86d7f5d3SJohn Marino {
360*86d7f5d3SJohn Marino int c;
361*86d7f5d3SJohn Marino struct partition part;
362*86d7f5d3SJohn Marino
363*86d7f5d3SJohn Marino /* Find a point of correspondence in the middle of the files. */
364*86d7f5d3SJohn Marino
365*86d7f5d3SJohn Marino c = diag (xoff, xlim, yoff, ylim, minimal, &part);
366*86d7f5d3SJohn Marino
367*86d7f5d3SJohn Marino if (c == 1)
368*86d7f5d3SJohn Marino {
369*86d7f5d3SJohn Marino /* This should be impossible, because it implies that
370*86d7f5d3SJohn Marino one of the two subsequences is empty,
371*86d7f5d3SJohn Marino and that case was handled above without calling `diag'.
372*86d7f5d3SJohn Marino Let's verify that this is true. */
373*86d7f5d3SJohn Marino abort ();
374*86d7f5d3SJohn Marino #if 0
375*86d7f5d3SJohn Marino /* The two subsequences differ by a single insert or delete;
376*86d7f5d3SJohn Marino record it and we are done. */
377*86d7f5d3SJohn Marino if (part.xmid - part.ymid < xoff - yoff)
378*86d7f5d3SJohn Marino files[1].changed_flag[files[1].realindexes[part.ymid - 1]] = 1;
379*86d7f5d3SJohn Marino else
380*86d7f5d3SJohn Marino files[0].changed_flag[files[0].realindexes[part.xmid]] = 1;
381*86d7f5d3SJohn Marino #endif
382*86d7f5d3SJohn Marino }
383*86d7f5d3SJohn Marino else
384*86d7f5d3SJohn Marino {
385*86d7f5d3SJohn Marino /* Use the partitions to split this problem into subproblems. */
386*86d7f5d3SJohn Marino compareseq (xoff, part.xmid, yoff, part.ymid, part.lo_minimal);
387*86d7f5d3SJohn Marino compareseq (part.xmid, xlim, part.ymid, ylim, part.hi_minimal);
388*86d7f5d3SJohn Marino }
389*86d7f5d3SJohn Marino }
390*86d7f5d3SJohn Marino }
391*86d7f5d3SJohn Marino
392*86d7f5d3SJohn Marino /* Discard lines from one file that have no matches in the other file.
393*86d7f5d3SJohn Marino
394*86d7f5d3SJohn Marino A line which is discarded will not be considered by the actual
395*86d7f5d3SJohn Marino comparison algorithm; it will be as if that line were not in the file.
396*86d7f5d3SJohn Marino The file's `realindexes' table maps virtual line numbers
397*86d7f5d3SJohn Marino (which don't count the discarded lines) into real line numbers;
398*86d7f5d3SJohn Marino this is how the actual comparison algorithm produces results
399*86d7f5d3SJohn Marino that are comprehensible when the discarded lines are counted.
400*86d7f5d3SJohn Marino
401*86d7f5d3SJohn Marino When we discard a line, we also mark it as a deletion or insertion
402*86d7f5d3SJohn Marino so that it will be printed in the output. */
403*86d7f5d3SJohn Marino
404*86d7f5d3SJohn Marino static void
discard_confusing_lines(filevec)405*86d7f5d3SJohn Marino discard_confusing_lines (filevec)
406*86d7f5d3SJohn Marino struct file_data filevec[];
407*86d7f5d3SJohn Marino {
408*86d7f5d3SJohn Marino unsigned int f, i;
409*86d7f5d3SJohn Marino char *discarded[2];
410*86d7f5d3SJohn Marino int *equiv_count[2];
411*86d7f5d3SJohn Marino int *p;
412*86d7f5d3SJohn Marino
413*86d7f5d3SJohn Marino /* Allocate our results. */
414*86d7f5d3SJohn Marino p = (int *) xmalloc ((filevec[0].buffered_lines + filevec[1].buffered_lines)
415*86d7f5d3SJohn Marino * (2 * sizeof (int)));
416*86d7f5d3SJohn Marino for (f = 0; f < 2; f++)
417*86d7f5d3SJohn Marino {
418*86d7f5d3SJohn Marino filevec[f].undiscarded = p; p += filevec[f].buffered_lines;
419*86d7f5d3SJohn Marino filevec[f].realindexes = p; p += filevec[f].buffered_lines;
420*86d7f5d3SJohn Marino }
421*86d7f5d3SJohn Marino
422*86d7f5d3SJohn Marino /* Set up equiv_count[F][I] as the number of lines in file F
423*86d7f5d3SJohn Marino that fall in equivalence class I. */
424*86d7f5d3SJohn Marino
425*86d7f5d3SJohn Marino p = (int *) xmalloc (filevec[0].equiv_max * (2 * sizeof (int)));
426*86d7f5d3SJohn Marino equiv_count[0] = p;
427*86d7f5d3SJohn Marino equiv_count[1] = p + filevec[0].equiv_max;
428*86d7f5d3SJohn Marino bzero (p, filevec[0].equiv_max * (2 * sizeof (int)));
429*86d7f5d3SJohn Marino
430*86d7f5d3SJohn Marino for (i = 0; i < filevec[0].buffered_lines; ++i)
431*86d7f5d3SJohn Marino ++equiv_count[0][filevec[0].equivs[i]];
432*86d7f5d3SJohn Marino for (i = 0; i < filevec[1].buffered_lines; ++i)
433*86d7f5d3SJohn Marino ++equiv_count[1][filevec[1].equivs[i]];
434*86d7f5d3SJohn Marino
435*86d7f5d3SJohn Marino /* Set up tables of which lines are going to be discarded. */
436*86d7f5d3SJohn Marino
437*86d7f5d3SJohn Marino discarded[0] = xmalloc (sizeof (char)
438*86d7f5d3SJohn Marino * (filevec[0].buffered_lines
439*86d7f5d3SJohn Marino + filevec[1].buffered_lines));
440*86d7f5d3SJohn Marino discarded[1] = discarded[0] + filevec[0].buffered_lines;
441*86d7f5d3SJohn Marino bzero (discarded[0], sizeof (char) * (filevec[0].buffered_lines
442*86d7f5d3SJohn Marino + filevec[1].buffered_lines));
443*86d7f5d3SJohn Marino
444*86d7f5d3SJohn Marino /* Mark to be discarded each line that matches no line of the other file.
445*86d7f5d3SJohn Marino If a line matches many lines, mark it as provisionally discardable. */
446*86d7f5d3SJohn Marino
447*86d7f5d3SJohn Marino for (f = 0; f < 2; f++)
448*86d7f5d3SJohn Marino {
449*86d7f5d3SJohn Marino unsigned int end = filevec[f].buffered_lines;
450*86d7f5d3SJohn Marino char *discards = discarded[f];
451*86d7f5d3SJohn Marino int *counts = equiv_count[1 - f];
452*86d7f5d3SJohn Marino int *equivs = filevec[f].equivs;
453*86d7f5d3SJohn Marino unsigned int many = 5;
454*86d7f5d3SJohn Marino unsigned int tem = end / 64;
455*86d7f5d3SJohn Marino
456*86d7f5d3SJohn Marino /* Multiply MANY by approximate square root of number of lines.
457*86d7f5d3SJohn Marino That is the threshold for provisionally discardable lines. */
458*86d7f5d3SJohn Marino while ((tem = tem >> 2) > 0)
459*86d7f5d3SJohn Marino many *= 2;
460*86d7f5d3SJohn Marino
461*86d7f5d3SJohn Marino for (i = 0; i < end; i++)
462*86d7f5d3SJohn Marino {
463*86d7f5d3SJohn Marino int nmatch;
464*86d7f5d3SJohn Marino if (equivs[i] == 0)
465*86d7f5d3SJohn Marino continue;
466*86d7f5d3SJohn Marino nmatch = counts[equivs[i]];
467*86d7f5d3SJohn Marino if (nmatch == 0)
468*86d7f5d3SJohn Marino discards[i] = 1;
469*86d7f5d3SJohn Marino else if (nmatch > many)
470*86d7f5d3SJohn Marino discards[i] = 2;
471*86d7f5d3SJohn Marino }
472*86d7f5d3SJohn Marino }
473*86d7f5d3SJohn Marino
474*86d7f5d3SJohn Marino /* Don't really discard the provisional lines except when they occur
475*86d7f5d3SJohn Marino in a run of discardables, with nonprovisionals at the beginning
476*86d7f5d3SJohn Marino and end. */
477*86d7f5d3SJohn Marino
478*86d7f5d3SJohn Marino for (f = 0; f < 2; f++)
479*86d7f5d3SJohn Marino {
480*86d7f5d3SJohn Marino unsigned int end = filevec[f].buffered_lines;
481*86d7f5d3SJohn Marino register char *discards = discarded[f];
482*86d7f5d3SJohn Marino
483*86d7f5d3SJohn Marino for (i = 0; i < end; i++)
484*86d7f5d3SJohn Marino {
485*86d7f5d3SJohn Marino /* Cancel provisional discards not in middle of run of discards. */
486*86d7f5d3SJohn Marino if (discards[i] == 2)
487*86d7f5d3SJohn Marino discards[i] = 0;
488*86d7f5d3SJohn Marino else if (discards[i] != 0)
489*86d7f5d3SJohn Marino {
490*86d7f5d3SJohn Marino /* We have found a nonprovisional discard. */
491*86d7f5d3SJohn Marino register int j;
492*86d7f5d3SJohn Marino unsigned int length;
493*86d7f5d3SJohn Marino unsigned int provisional = 0;
494*86d7f5d3SJohn Marino
495*86d7f5d3SJohn Marino /* Find end of this run of discardable lines.
496*86d7f5d3SJohn Marino Count how many are provisionally discardable. */
497*86d7f5d3SJohn Marino for (j = i; j < end; j++)
498*86d7f5d3SJohn Marino {
499*86d7f5d3SJohn Marino if (discards[j] == 0)
500*86d7f5d3SJohn Marino break;
501*86d7f5d3SJohn Marino if (discards[j] == 2)
502*86d7f5d3SJohn Marino ++provisional;
503*86d7f5d3SJohn Marino }
504*86d7f5d3SJohn Marino
505*86d7f5d3SJohn Marino /* Cancel provisional discards at end, and shrink the run. */
506*86d7f5d3SJohn Marino while (j > i && discards[j - 1] == 2)
507*86d7f5d3SJohn Marino discards[--j] = 0, --provisional;
508*86d7f5d3SJohn Marino
509*86d7f5d3SJohn Marino /* Now we have the length of a run of discardable lines
510*86d7f5d3SJohn Marino whose first and last are not provisional. */
511*86d7f5d3SJohn Marino length = j - i;
512*86d7f5d3SJohn Marino
513*86d7f5d3SJohn Marino /* If 1/4 of the lines in the run are provisional,
514*86d7f5d3SJohn Marino cancel discarding of all provisional lines in the run. */
515*86d7f5d3SJohn Marino if (provisional * 4 > length)
516*86d7f5d3SJohn Marino {
517*86d7f5d3SJohn Marino while (j > i)
518*86d7f5d3SJohn Marino if (discards[--j] == 2)
519*86d7f5d3SJohn Marino discards[j] = 0;
520*86d7f5d3SJohn Marino }
521*86d7f5d3SJohn Marino else
522*86d7f5d3SJohn Marino {
523*86d7f5d3SJohn Marino register unsigned int consec;
524*86d7f5d3SJohn Marino unsigned int minimum = 1;
525*86d7f5d3SJohn Marino unsigned int tem = length / 4;
526*86d7f5d3SJohn Marino
527*86d7f5d3SJohn Marino /* MINIMUM is approximate square root of LENGTH/4.
528*86d7f5d3SJohn Marino A subrun of two or more provisionals can stand
529*86d7f5d3SJohn Marino when LENGTH is at least 16.
530*86d7f5d3SJohn Marino A subrun of 4 or more can stand when LENGTH >= 64. */
531*86d7f5d3SJohn Marino while ((tem = tem >> 2) > 0)
532*86d7f5d3SJohn Marino minimum *= 2;
533*86d7f5d3SJohn Marino minimum++;
534*86d7f5d3SJohn Marino
535*86d7f5d3SJohn Marino /* Cancel any subrun of MINIMUM or more provisionals
536*86d7f5d3SJohn Marino within the larger run. */
537*86d7f5d3SJohn Marino for (j = 0, consec = 0; j < length; j++)
538*86d7f5d3SJohn Marino if (discards[i + j] != 2)
539*86d7f5d3SJohn Marino consec = 0;
540*86d7f5d3SJohn Marino else if (minimum == ++consec)
541*86d7f5d3SJohn Marino /* Back up to start of subrun, to cancel it all. */
542*86d7f5d3SJohn Marino j -= consec;
543*86d7f5d3SJohn Marino else if (minimum < consec)
544*86d7f5d3SJohn Marino discards[i + j] = 0;
545*86d7f5d3SJohn Marino
546*86d7f5d3SJohn Marino /* Scan from beginning of run
547*86d7f5d3SJohn Marino until we find 3 or more nonprovisionals in a row
548*86d7f5d3SJohn Marino or until the first nonprovisional at least 8 lines in.
549*86d7f5d3SJohn Marino Until that point, cancel any provisionals. */
550*86d7f5d3SJohn Marino for (j = 0, consec = 0; j < length; j++)
551*86d7f5d3SJohn Marino {
552*86d7f5d3SJohn Marino if (j >= 8 && discards[i + j] == 1)
553*86d7f5d3SJohn Marino break;
554*86d7f5d3SJohn Marino if (discards[i + j] == 2)
555*86d7f5d3SJohn Marino consec = 0, discards[i + j] = 0;
556*86d7f5d3SJohn Marino else if (discards[i + j] == 0)
557*86d7f5d3SJohn Marino consec = 0;
558*86d7f5d3SJohn Marino else
559*86d7f5d3SJohn Marino consec++;
560*86d7f5d3SJohn Marino if (consec == 3)
561*86d7f5d3SJohn Marino break;
562*86d7f5d3SJohn Marino }
563*86d7f5d3SJohn Marino
564*86d7f5d3SJohn Marino /* I advances to the last line of the run. */
565*86d7f5d3SJohn Marino i += length - 1;
566*86d7f5d3SJohn Marino
567*86d7f5d3SJohn Marino /* Same thing, from end. */
568*86d7f5d3SJohn Marino for (j = 0, consec = 0; j < length; j++)
569*86d7f5d3SJohn Marino {
570*86d7f5d3SJohn Marino if (j >= 8 && discards[i - j] == 1)
571*86d7f5d3SJohn Marino break;
572*86d7f5d3SJohn Marino if (discards[i - j] == 2)
573*86d7f5d3SJohn Marino consec = 0, discards[i - j] = 0;
574*86d7f5d3SJohn Marino else if (discards[i - j] == 0)
575*86d7f5d3SJohn Marino consec = 0;
576*86d7f5d3SJohn Marino else
577*86d7f5d3SJohn Marino consec++;
578*86d7f5d3SJohn Marino if (consec == 3)
579*86d7f5d3SJohn Marino break;
580*86d7f5d3SJohn Marino }
581*86d7f5d3SJohn Marino }
582*86d7f5d3SJohn Marino }
583*86d7f5d3SJohn Marino }
584*86d7f5d3SJohn Marino }
585*86d7f5d3SJohn Marino
586*86d7f5d3SJohn Marino /* Actually discard the lines. */
587*86d7f5d3SJohn Marino for (f = 0; f < 2; f++)
588*86d7f5d3SJohn Marino {
589*86d7f5d3SJohn Marino char *discards = discarded[f];
590*86d7f5d3SJohn Marino unsigned int end = filevec[f].buffered_lines;
591*86d7f5d3SJohn Marino unsigned int j = 0;
592*86d7f5d3SJohn Marino for (i = 0; i < end; ++i)
593*86d7f5d3SJohn Marino if (no_discards || discards[i] == 0)
594*86d7f5d3SJohn Marino {
595*86d7f5d3SJohn Marino filevec[f].undiscarded[j] = filevec[f].equivs[i];
596*86d7f5d3SJohn Marino filevec[f].realindexes[j++] = i;
597*86d7f5d3SJohn Marino }
598*86d7f5d3SJohn Marino else
599*86d7f5d3SJohn Marino filevec[f].changed_flag[i] = 1;
600*86d7f5d3SJohn Marino filevec[f].nondiscarded_lines = j;
601*86d7f5d3SJohn Marino }
602*86d7f5d3SJohn Marino
603*86d7f5d3SJohn Marino free (discarded[0]);
604*86d7f5d3SJohn Marino free (equiv_count[0]);
605*86d7f5d3SJohn Marino }
606*86d7f5d3SJohn Marino
607*86d7f5d3SJohn Marino /* Adjust inserts/deletes of identical lines to join changes
608*86d7f5d3SJohn Marino as much as possible.
609*86d7f5d3SJohn Marino
610*86d7f5d3SJohn Marino We do something when a run of changed lines include a
611*86d7f5d3SJohn Marino line at one end and have an excluded, identical line at the other.
612*86d7f5d3SJohn Marino We are free to choose which identical line is included.
613*86d7f5d3SJohn Marino `compareseq' usually chooses the one at the beginning,
614*86d7f5d3SJohn Marino but usually it is cleaner to consider the following identical line
615*86d7f5d3SJohn Marino to be the "change". */
616*86d7f5d3SJohn Marino
617*86d7f5d3SJohn Marino int inhibit;
618*86d7f5d3SJohn Marino
619*86d7f5d3SJohn Marino static void
shift_boundaries(filevec)620*86d7f5d3SJohn Marino shift_boundaries (filevec)
621*86d7f5d3SJohn Marino struct file_data filevec[];
622*86d7f5d3SJohn Marino {
623*86d7f5d3SJohn Marino int f;
624*86d7f5d3SJohn Marino
625*86d7f5d3SJohn Marino if (inhibit)
626*86d7f5d3SJohn Marino return;
627*86d7f5d3SJohn Marino
628*86d7f5d3SJohn Marino for (f = 0; f < 2; f++)
629*86d7f5d3SJohn Marino {
630*86d7f5d3SJohn Marino char *changed = filevec[f].changed_flag;
631*86d7f5d3SJohn Marino char const *other_changed = filevec[1-f].changed_flag;
632*86d7f5d3SJohn Marino int const *equivs = filevec[f].equivs;
633*86d7f5d3SJohn Marino int i = 0;
634*86d7f5d3SJohn Marino int j = 0;
635*86d7f5d3SJohn Marino int i_end = filevec[f].buffered_lines;
636*86d7f5d3SJohn Marino
637*86d7f5d3SJohn Marino while (1)
638*86d7f5d3SJohn Marino {
639*86d7f5d3SJohn Marino int runlength, start, corresponding;
640*86d7f5d3SJohn Marino
641*86d7f5d3SJohn Marino /* Scan forwards to find beginning of another run of changes.
642*86d7f5d3SJohn Marino Also keep track of the corresponding point in the other file. */
643*86d7f5d3SJohn Marino
644*86d7f5d3SJohn Marino while (i < i_end && changed[i] == 0)
645*86d7f5d3SJohn Marino {
646*86d7f5d3SJohn Marino while (other_changed[j++])
647*86d7f5d3SJohn Marino continue;
648*86d7f5d3SJohn Marino i++;
649*86d7f5d3SJohn Marino }
650*86d7f5d3SJohn Marino
651*86d7f5d3SJohn Marino if (i == i_end)
652*86d7f5d3SJohn Marino break;
653*86d7f5d3SJohn Marino
654*86d7f5d3SJohn Marino start = i;
655*86d7f5d3SJohn Marino
656*86d7f5d3SJohn Marino /* Find the end of this run of changes. */
657*86d7f5d3SJohn Marino
658*86d7f5d3SJohn Marino while (changed[++i])
659*86d7f5d3SJohn Marino continue;
660*86d7f5d3SJohn Marino while (other_changed[j])
661*86d7f5d3SJohn Marino j++;
662*86d7f5d3SJohn Marino
663*86d7f5d3SJohn Marino do
664*86d7f5d3SJohn Marino {
665*86d7f5d3SJohn Marino /* Record the length of this run of changes, so that
666*86d7f5d3SJohn Marino we can later determine whether the run has grown. */
667*86d7f5d3SJohn Marino runlength = i - start;
668*86d7f5d3SJohn Marino
669*86d7f5d3SJohn Marino /* Move the changed region back, so long as the
670*86d7f5d3SJohn Marino previous unchanged line matches the last changed one.
671*86d7f5d3SJohn Marino This merges with previous changed regions. */
672*86d7f5d3SJohn Marino
673*86d7f5d3SJohn Marino while (start && equivs[start - 1] == equivs[i - 1])
674*86d7f5d3SJohn Marino {
675*86d7f5d3SJohn Marino changed[--start] = 1;
676*86d7f5d3SJohn Marino changed[--i] = 0;
677*86d7f5d3SJohn Marino while (changed[start - 1])
678*86d7f5d3SJohn Marino start--;
679*86d7f5d3SJohn Marino while (other_changed[--j])
680*86d7f5d3SJohn Marino continue;
681*86d7f5d3SJohn Marino }
682*86d7f5d3SJohn Marino
683*86d7f5d3SJohn Marino /* Set CORRESPONDING to the end of the changed run, at the last
684*86d7f5d3SJohn Marino point where it corresponds to a changed run in the other file.
685*86d7f5d3SJohn Marino CORRESPONDING == I_END means no such point has been found. */
686*86d7f5d3SJohn Marino corresponding = other_changed[j - 1] ? i : i_end;
687*86d7f5d3SJohn Marino
688*86d7f5d3SJohn Marino /* Move the changed region forward, so long as the
689*86d7f5d3SJohn Marino first changed line matches the following unchanged one.
690*86d7f5d3SJohn Marino This merges with following changed regions.
691*86d7f5d3SJohn Marino Do this second, so that if there are no merges,
692*86d7f5d3SJohn Marino the changed region is moved forward as far as possible. */
693*86d7f5d3SJohn Marino
694*86d7f5d3SJohn Marino while (i != i_end && equivs[start] == equivs[i])
695*86d7f5d3SJohn Marino {
696*86d7f5d3SJohn Marino changed[start++] = 0;
697*86d7f5d3SJohn Marino changed[i++] = 1;
698*86d7f5d3SJohn Marino while (changed[i])
699*86d7f5d3SJohn Marino i++;
700*86d7f5d3SJohn Marino while (other_changed[++j])
701*86d7f5d3SJohn Marino corresponding = i;
702*86d7f5d3SJohn Marino }
703*86d7f5d3SJohn Marino }
704*86d7f5d3SJohn Marino while (runlength != i - start);
705*86d7f5d3SJohn Marino
706*86d7f5d3SJohn Marino /* If possible, move the fully-merged run of changes
707*86d7f5d3SJohn Marino back to a corresponding run in the other file. */
708*86d7f5d3SJohn Marino
709*86d7f5d3SJohn Marino while (corresponding < i)
710*86d7f5d3SJohn Marino {
711*86d7f5d3SJohn Marino changed[--start] = 1;
712*86d7f5d3SJohn Marino changed[--i] = 0;
713*86d7f5d3SJohn Marino while (other_changed[--j])
714*86d7f5d3SJohn Marino continue;
715*86d7f5d3SJohn Marino }
716*86d7f5d3SJohn Marino }
717*86d7f5d3SJohn Marino }
718*86d7f5d3SJohn Marino }
719*86d7f5d3SJohn Marino
720*86d7f5d3SJohn Marino /* Cons an additional entry onto the front of an edit script OLD.
721*86d7f5d3SJohn Marino LINE0 and LINE1 are the first affected lines in the two files (origin 0).
722*86d7f5d3SJohn Marino DELETED is the number of lines deleted here from file 0.
723*86d7f5d3SJohn Marino INSERTED is the number of lines inserted here in file 1.
724*86d7f5d3SJohn Marino
725*86d7f5d3SJohn Marino If DELETED is 0 then LINE0 is the number of the line before
726*86d7f5d3SJohn Marino which the insertion was done; vice versa for INSERTED and LINE1. */
727*86d7f5d3SJohn Marino
728*86d7f5d3SJohn Marino static struct change *
add_change(line0,line1,deleted,inserted,old)729*86d7f5d3SJohn Marino add_change (line0, line1, deleted, inserted, old)
730*86d7f5d3SJohn Marino int line0, line1, deleted, inserted;
731*86d7f5d3SJohn Marino struct change *old;
732*86d7f5d3SJohn Marino {
733*86d7f5d3SJohn Marino struct change *new = (struct change *) xmalloc (sizeof (struct change));
734*86d7f5d3SJohn Marino
735*86d7f5d3SJohn Marino new->line0 = line0;
736*86d7f5d3SJohn Marino new->line1 = line1;
737*86d7f5d3SJohn Marino new->inserted = inserted;
738*86d7f5d3SJohn Marino new->deleted = deleted;
739*86d7f5d3SJohn Marino new->link = old;
740*86d7f5d3SJohn Marino return new;
741*86d7f5d3SJohn Marino }
742*86d7f5d3SJohn Marino
743*86d7f5d3SJohn Marino /* Scan the tables of which lines are inserted and deleted,
744*86d7f5d3SJohn Marino producing an edit script in reverse order. */
745*86d7f5d3SJohn Marino
746*86d7f5d3SJohn Marino static struct change *
build_reverse_script(filevec)747*86d7f5d3SJohn Marino build_reverse_script (filevec)
748*86d7f5d3SJohn Marino struct file_data const filevec[];
749*86d7f5d3SJohn Marino {
750*86d7f5d3SJohn Marino struct change *script = 0;
751*86d7f5d3SJohn Marino char *changed0 = filevec[0].changed_flag;
752*86d7f5d3SJohn Marino char *changed1 = filevec[1].changed_flag;
753*86d7f5d3SJohn Marino int len0 = filevec[0].buffered_lines;
754*86d7f5d3SJohn Marino int len1 = filevec[1].buffered_lines;
755*86d7f5d3SJohn Marino
756*86d7f5d3SJohn Marino /* Note that changedN[len0] does exist, and contains 0. */
757*86d7f5d3SJohn Marino
758*86d7f5d3SJohn Marino int i0 = 0, i1 = 0;
759*86d7f5d3SJohn Marino
760*86d7f5d3SJohn Marino while (i0 < len0 || i1 < len1)
761*86d7f5d3SJohn Marino {
762*86d7f5d3SJohn Marino if (changed0[i0] || changed1[i1])
763*86d7f5d3SJohn Marino {
764*86d7f5d3SJohn Marino int line0 = i0, line1 = i1;
765*86d7f5d3SJohn Marino
766*86d7f5d3SJohn Marino /* Find # lines changed here in each file. */
767*86d7f5d3SJohn Marino while (changed0[i0]) ++i0;
768*86d7f5d3SJohn Marino while (changed1[i1]) ++i1;
769*86d7f5d3SJohn Marino
770*86d7f5d3SJohn Marino /* Record this change. */
771*86d7f5d3SJohn Marino script = add_change (line0, line1, i0 - line0, i1 - line1, script);
772*86d7f5d3SJohn Marino }
773*86d7f5d3SJohn Marino
774*86d7f5d3SJohn Marino /* We have reached lines in the two files that match each other. */
775*86d7f5d3SJohn Marino i0++, i1++;
776*86d7f5d3SJohn Marino }
777*86d7f5d3SJohn Marino
778*86d7f5d3SJohn Marino return script;
779*86d7f5d3SJohn Marino }
780*86d7f5d3SJohn Marino
781*86d7f5d3SJohn Marino /* Scan the tables of which lines are inserted and deleted,
782*86d7f5d3SJohn Marino producing an edit script in forward order. */
783*86d7f5d3SJohn Marino
784*86d7f5d3SJohn Marino static struct change *
build_script(filevec)785*86d7f5d3SJohn Marino build_script (filevec)
786*86d7f5d3SJohn Marino struct file_data const filevec[];
787*86d7f5d3SJohn Marino {
788*86d7f5d3SJohn Marino struct change *script = 0;
789*86d7f5d3SJohn Marino char *changed0 = filevec[0].changed_flag;
790*86d7f5d3SJohn Marino char *changed1 = filevec[1].changed_flag;
791*86d7f5d3SJohn Marino int i0 = filevec[0].buffered_lines, i1 = filevec[1].buffered_lines;
792*86d7f5d3SJohn Marino
793*86d7f5d3SJohn Marino /* Note that changedN[-1] does exist, and contains 0. */
794*86d7f5d3SJohn Marino
795*86d7f5d3SJohn Marino while (i0 >= 0 || i1 >= 0)
796*86d7f5d3SJohn Marino {
797*86d7f5d3SJohn Marino if (changed0[i0 - 1] || changed1[i1 - 1])
798*86d7f5d3SJohn Marino {
799*86d7f5d3SJohn Marino int line0 = i0, line1 = i1;
800*86d7f5d3SJohn Marino
801*86d7f5d3SJohn Marino /* Find # lines changed here in each file. */
802*86d7f5d3SJohn Marino while (changed0[i0 - 1]) --i0;
803*86d7f5d3SJohn Marino while (changed1[i1 - 1]) --i1;
804*86d7f5d3SJohn Marino
805*86d7f5d3SJohn Marino /* Record this change. */
806*86d7f5d3SJohn Marino script = add_change (i0, i1, line0 - i0, line1 - i1, script);
807*86d7f5d3SJohn Marino }
808*86d7f5d3SJohn Marino
809*86d7f5d3SJohn Marino /* We have reached lines in the two files that match each other. */
810*86d7f5d3SJohn Marino i0--, i1--;
811*86d7f5d3SJohn Marino }
812*86d7f5d3SJohn Marino
813*86d7f5d3SJohn Marino return script;
814*86d7f5d3SJohn Marino }
815*86d7f5d3SJohn Marino
816*86d7f5d3SJohn Marino /* If CHANGES, briefly report that two files differed. */
817*86d7f5d3SJohn Marino static void
briefly_report(changes,filevec)818*86d7f5d3SJohn Marino briefly_report (changes, filevec)
819*86d7f5d3SJohn Marino int changes;
820*86d7f5d3SJohn Marino struct file_data const filevec[];
821*86d7f5d3SJohn Marino {
822*86d7f5d3SJohn Marino if (changes)
823*86d7f5d3SJohn Marino message (no_details_flag ? "Files %s and %s differ\n"
824*86d7f5d3SJohn Marino : "Binary files %s and %s differ\n",
825*86d7f5d3SJohn Marino filevec[0].name, filevec[1].name);
826*86d7f5d3SJohn Marino }
827*86d7f5d3SJohn Marino
828*86d7f5d3SJohn Marino /* Report the differences of two files. DEPTH is the current directory
829*86d7f5d3SJohn Marino depth. */
830*86d7f5d3SJohn Marino int
diff_2_files(filevec,depth)831*86d7f5d3SJohn Marino diff_2_files (filevec, depth)
832*86d7f5d3SJohn Marino struct file_data filevec[];
833*86d7f5d3SJohn Marino int depth;
834*86d7f5d3SJohn Marino {
835*86d7f5d3SJohn Marino int diags;
836*86d7f5d3SJohn Marino int i;
837*86d7f5d3SJohn Marino struct change *e, *p;
838*86d7f5d3SJohn Marino struct change *script;
839*86d7f5d3SJohn Marino int changes;
840*86d7f5d3SJohn Marino
841*86d7f5d3SJohn Marino
842*86d7f5d3SJohn Marino /* If we have detected that either file is binary,
843*86d7f5d3SJohn Marino compare the two files as binary. This can happen
844*86d7f5d3SJohn Marino only when the first chunk is read.
845*86d7f5d3SJohn Marino Also, --brief without any --ignore-* options means
846*86d7f5d3SJohn Marino we can speed things up by treating the files as binary. */
847*86d7f5d3SJohn Marino
848*86d7f5d3SJohn Marino if (read_files (filevec, no_details_flag & ~ignore_some_changes))
849*86d7f5d3SJohn Marino {
850*86d7f5d3SJohn Marino /* Files with different lengths must be different. */
851*86d7f5d3SJohn Marino if (filevec[0].stat.st_size != filevec[1].stat.st_size
852*86d7f5d3SJohn Marino && (filevec[0].desc < 0 || S_ISREG (filevec[0].stat.st_mode))
853*86d7f5d3SJohn Marino && (filevec[1].desc < 0 || S_ISREG (filevec[1].stat.st_mode)))
854*86d7f5d3SJohn Marino changes = 1;
855*86d7f5d3SJohn Marino
856*86d7f5d3SJohn Marino /* Standard input equals itself. */
857*86d7f5d3SJohn Marino else if (filevec[0].desc == filevec[1].desc)
858*86d7f5d3SJohn Marino changes = 0;
859*86d7f5d3SJohn Marino
860*86d7f5d3SJohn Marino else
861*86d7f5d3SJohn Marino /* Scan both files, a buffer at a time, looking for a difference. */
862*86d7f5d3SJohn Marino {
863*86d7f5d3SJohn Marino /* Allocate same-sized buffers for both files. */
864*86d7f5d3SJohn Marino size_t buffer_size = buffer_lcm (STAT_BLOCKSIZE (filevec[0].stat),
865*86d7f5d3SJohn Marino STAT_BLOCKSIZE (filevec[1].stat));
866*86d7f5d3SJohn Marino for (i = 0; i < 2; i++)
867*86d7f5d3SJohn Marino filevec[i].buffer = xrealloc (filevec[i].buffer, buffer_size);
868*86d7f5d3SJohn Marino
869*86d7f5d3SJohn Marino for (;; filevec[0].buffered_chars = filevec[1].buffered_chars = 0)
870*86d7f5d3SJohn Marino {
871*86d7f5d3SJohn Marino /* Read a buffer's worth from both files. */
872*86d7f5d3SJohn Marino for (i = 0; i < 2; i++)
873*86d7f5d3SJohn Marino if (0 <= filevec[i].desc)
874*86d7f5d3SJohn Marino while (filevec[i].buffered_chars != buffer_size)
875*86d7f5d3SJohn Marino {
876*86d7f5d3SJohn Marino int r = read (filevec[i].desc,
877*86d7f5d3SJohn Marino filevec[i].buffer
878*86d7f5d3SJohn Marino + filevec[i].buffered_chars,
879*86d7f5d3SJohn Marino buffer_size - filevec[i].buffered_chars);
880*86d7f5d3SJohn Marino if (r == 0)
881*86d7f5d3SJohn Marino break;
882*86d7f5d3SJohn Marino if (r < 0)
883*86d7f5d3SJohn Marino pfatal_with_name (filevec[i].name);
884*86d7f5d3SJohn Marino filevec[i].buffered_chars += r;
885*86d7f5d3SJohn Marino }
886*86d7f5d3SJohn Marino
887*86d7f5d3SJohn Marino /* If the buffers differ, the files differ. */
888*86d7f5d3SJohn Marino if (filevec[0].buffered_chars != filevec[1].buffered_chars
889*86d7f5d3SJohn Marino || (filevec[0].buffered_chars != 0
890*86d7f5d3SJohn Marino && memcmp (filevec[0].buffer,
891*86d7f5d3SJohn Marino filevec[1].buffer,
892*86d7f5d3SJohn Marino filevec[0].buffered_chars) != 0))
893*86d7f5d3SJohn Marino {
894*86d7f5d3SJohn Marino changes = 1;
895*86d7f5d3SJohn Marino break;
896*86d7f5d3SJohn Marino }
897*86d7f5d3SJohn Marino
898*86d7f5d3SJohn Marino /* If we reach end of file, the files are the same. */
899*86d7f5d3SJohn Marino if (filevec[0].buffered_chars != buffer_size)
900*86d7f5d3SJohn Marino {
901*86d7f5d3SJohn Marino changes = 0;
902*86d7f5d3SJohn Marino break;
903*86d7f5d3SJohn Marino }
904*86d7f5d3SJohn Marino }
905*86d7f5d3SJohn Marino }
906*86d7f5d3SJohn Marino
907*86d7f5d3SJohn Marino briefly_report (changes, filevec);
908*86d7f5d3SJohn Marino }
909*86d7f5d3SJohn Marino else
910*86d7f5d3SJohn Marino {
911*86d7f5d3SJohn Marino /* Allocate vectors for the results of comparison:
912*86d7f5d3SJohn Marino a flag for each line of each file, saying whether that line
913*86d7f5d3SJohn Marino is an insertion or deletion.
914*86d7f5d3SJohn Marino Allocate an extra element, always zero, at each end of each vector. */
915*86d7f5d3SJohn Marino
916*86d7f5d3SJohn Marino size_t s = filevec[0].buffered_lines + filevec[1].buffered_lines + 4;
917*86d7f5d3SJohn Marino filevec[0].changed_flag = xmalloc (s);
918*86d7f5d3SJohn Marino bzero (filevec[0].changed_flag, s);
919*86d7f5d3SJohn Marino filevec[0].changed_flag++;
920*86d7f5d3SJohn Marino filevec[1].changed_flag = filevec[0].changed_flag
921*86d7f5d3SJohn Marino + filevec[0].buffered_lines + 2;
922*86d7f5d3SJohn Marino
923*86d7f5d3SJohn Marino /* Some lines are obviously insertions or deletions
924*86d7f5d3SJohn Marino because they don't match anything. Detect them now, and
925*86d7f5d3SJohn Marino avoid even thinking about them in the main comparison algorithm. */
926*86d7f5d3SJohn Marino
927*86d7f5d3SJohn Marino discard_confusing_lines (filevec);
928*86d7f5d3SJohn Marino
929*86d7f5d3SJohn Marino /* Now do the main comparison algorithm, considering just the
930*86d7f5d3SJohn Marino undiscarded lines. */
931*86d7f5d3SJohn Marino
932*86d7f5d3SJohn Marino xvec = filevec[0].undiscarded;
933*86d7f5d3SJohn Marino yvec = filevec[1].undiscarded;
934*86d7f5d3SJohn Marino diags = filevec[0].nondiscarded_lines + filevec[1].nondiscarded_lines + 3;
935*86d7f5d3SJohn Marino fdiag = (int *) xmalloc (diags * (2 * sizeof (int)));
936*86d7f5d3SJohn Marino bdiag = fdiag + diags;
937*86d7f5d3SJohn Marino fdiag += filevec[1].nondiscarded_lines + 1;
938*86d7f5d3SJohn Marino bdiag += filevec[1].nondiscarded_lines + 1;
939*86d7f5d3SJohn Marino
940*86d7f5d3SJohn Marino /* Set TOO_EXPENSIVE to be approximate square root of input size,
941*86d7f5d3SJohn Marino bounded below by 256. */
942*86d7f5d3SJohn Marino too_expensive = 1;
943*86d7f5d3SJohn Marino for (i = filevec[0].nondiscarded_lines + filevec[1].nondiscarded_lines;
944*86d7f5d3SJohn Marino i != 0; i >>= 2)
945*86d7f5d3SJohn Marino too_expensive <<= 1;
946*86d7f5d3SJohn Marino too_expensive = max (256, too_expensive);
947*86d7f5d3SJohn Marino
948*86d7f5d3SJohn Marino files[0] = filevec[0];
949*86d7f5d3SJohn Marino files[1] = filevec[1];
950*86d7f5d3SJohn Marino
951*86d7f5d3SJohn Marino compareseq (0, filevec[0].nondiscarded_lines,
952*86d7f5d3SJohn Marino 0, filevec[1].nondiscarded_lines, no_discards);
953*86d7f5d3SJohn Marino
954*86d7f5d3SJohn Marino free (fdiag - (filevec[1].nondiscarded_lines + 1));
955*86d7f5d3SJohn Marino
956*86d7f5d3SJohn Marino /* Modify the results slightly to make them prettier
957*86d7f5d3SJohn Marino in cases where that can validly be done. */
958*86d7f5d3SJohn Marino
959*86d7f5d3SJohn Marino shift_boundaries (filevec);
960*86d7f5d3SJohn Marino
961*86d7f5d3SJohn Marino /* Get the results of comparison in the form of a chain
962*86d7f5d3SJohn Marino of `struct change's -- an edit script. */
963*86d7f5d3SJohn Marino
964*86d7f5d3SJohn Marino if (output_style == OUTPUT_ED)
965*86d7f5d3SJohn Marino script = build_reverse_script (filevec);
966*86d7f5d3SJohn Marino else
967*86d7f5d3SJohn Marino script = build_script (filevec);
968*86d7f5d3SJohn Marino
969*86d7f5d3SJohn Marino /* Set CHANGES if we had any diffs.
970*86d7f5d3SJohn Marino If some changes are ignored, we must scan the script to decide. */
971*86d7f5d3SJohn Marino if (ignore_blank_lines_flag || ignore_regexp_list)
972*86d7f5d3SJohn Marino {
973*86d7f5d3SJohn Marino struct change *next = script;
974*86d7f5d3SJohn Marino changes = 0;
975*86d7f5d3SJohn Marino
976*86d7f5d3SJohn Marino while (next && changes == 0)
977*86d7f5d3SJohn Marino {
978*86d7f5d3SJohn Marino struct change *this, *end;
979*86d7f5d3SJohn Marino int first0, last0, first1, last1, deletes, inserts;
980*86d7f5d3SJohn Marino
981*86d7f5d3SJohn Marino /* Find a set of changes that belong together. */
982*86d7f5d3SJohn Marino this = next;
983*86d7f5d3SJohn Marino end = find_change (next);
984*86d7f5d3SJohn Marino
985*86d7f5d3SJohn Marino /* Disconnect them from the rest of the changes, making them
986*86d7f5d3SJohn Marino a hunk, and remember the rest for next iteration. */
987*86d7f5d3SJohn Marino next = end->link;
988*86d7f5d3SJohn Marino end->link = 0;
989*86d7f5d3SJohn Marino
990*86d7f5d3SJohn Marino /* Determine whether this hunk is really a difference. */
991*86d7f5d3SJohn Marino analyze_hunk (this, &first0, &last0, &first1, &last1,
992*86d7f5d3SJohn Marino &deletes, &inserts);
993*86d7f5d3SJohn Marino
994*86d7f5d3SJohn Marino /* Reconnect the script so it will all be freed properly. */
995*86d7f5d3SJohn Marino end->link = next;
996*86d7f5d3SJohn Marino
997*86d7f5d3SJohn Marino if (deletes || inserts)
998*86d7f5d3SJohn Marino changes = 1;
999*86d7f5d3SJohn Marino }
1000*86d7f5d3SJohn Marino }
1001*86d7f5d3SJohn Marino else
1002*86d7f5d3SJohn Marino changes = (script != 0);
1003*86d7f5d3SJohn Marino
1004*86d7f5d3SJohn Marino if (no_details_flag)
1005*86d7f5d3SJohn Marino briefly_report (changes, filevec);
1006*86d7f5d3SJohn Marino else
1007*86d7f5d3SJohn Marino {
1008*86d7f5d3SJohn Marino if (changes || ! no_diff_means_no_output)
1009*86d7f5d3SJohn Marino {
1010*86d7f5d3SJohn Marino /* Record info for starting up output,
1011*86d7f5d3SJohn Marino to be used if and when we have some output to print. */
1012*86d7f5d3SJohn Marino setup_output (files[0].name, files[1].name, depth);
1013*86d7f5d3SJohn Marino
1014*86d7f5d3SJohn Marino switch (output_style)
1015*86d7f5d3SJohn Marino {
1016*86d7f5d3SJohn Marino case OUTPUT_CONTEXT:
1017*86d7f5d3SJohn Marino print_context_script (script, 0);
1018*86d7f5d3SJohn Marino break;
1019*86d7f5d3SJohn Marino
1020*86d7f5d3SJohn Marino case OUTPUT_UNIFIED:
1021*86d7f5d3SJohn Marino print_context_script (script, 1);
1022*86d7f5d3SJohn Marino break;
1023*86d7f5d3SJohn Marino
1024*86d7f5d3SJohn Marino case OUTPUT_ED:
1025*86d7f5d3SJohn Marino print_ed_script (script);
1026*86d7f5d3SJohn Marino break;
1027*86d7f5d3SJohn Marino
1028*86d7f5d3SJohn Marino case OUTPUT_FORWARD_ED:
1029*86d7f5d3SJohn Marino pr_forward_ed_script (script);
1030*86d7f5d3SJohn Marino break;
1031*86d7f5d3SJohn Marino
1032*86d7f5d3SJohn Marino case OUTPUT_RCS:
1033*86d7f5d3SJohn Marino print_rcs_script (script);
1034*86d7f5d3SJohn Marino break;
1035*86d7f5d3SJohn Marino
1036*86d7f5d3SJohn Marino case OUTPUT_NORMAL:
1037*86d7f5d3SJohn Marino print_normal_script (script);
1038*86d7f5d3SJohn Marino break;
1039*86d7f5d3SJohn Marino
1040*86d7f5d3SJohn Marino case OUTPUT_IFDEF:
1041*86d7f5d3SJohn Marino print_ifdef_script (script);
1042*86d7f5d3SJohn Marino break;
1043*86d7f5d3SJohn Marino
1044*86d7f5d3SJohn Marino case OUTPUT_SDIFF:
1045*86d7f5d3SJohn Marino print_sdiff_script (script);
1046*86d7f5d3SJohn Marino }
1047*86d7f5d3SJohn Marino
1048*86d7f5d3SJohn Marino finish_output ();
1049*86d7f5d3SJohn Marino }
1050*86d7f5d3SJohn Marino }
1051*86d7f5d3SJohn Marino
1052*86d7f5d3SJohn Marino free (filevec[0].undiscarded);
1053*86d7f5d3SJohn Marino
1054*86d7f5d3SJohn Marino free (filevec[0].changed_flag - 1);
1055*86d7f5d3SJohn Marino
1056*86d7f5d3SJohn Marino for (i = 1; i >= 0; --i)
1057*86d7f5d3SJohn Marino free (filevec[i].equivs);
1058*86d7f5d3SJohn Marino
1059*86d7f5d3SJohn Marino for (i = 0; i < 2; ++i)
1060*86d7f5d3SJohn Marino free (filevec[i].linbuf + filevec[i].linbuf_base);
1061*86d7f5d3SJohn Marino
1062*86d7f5d3SJohn Marino for (e = script; e; e = p)
1063*86d7f5d3SJohn Marino {
1064*86d7f5d3SJohn Marino p = e->link;
1065*86d7f5d3SJohn Marino free (e);
1066*86d7f5d3SJohn Marino }
1067*86d7f5d3SJohn Marino
1068*86d7f5d3SJohn Marino if (! ROBUST_OUTPUT_STYLE (output_style))
1069*86d7f5d3SJohn Marino for (i = 0; i < 2; ++i)
1070*86d7f5d3SJohn Marino if (filevec[i].missing_newline)
1071*86d7f5d3SJohn Marino {
1072*86d7f5d3SJohn Marino diff_error ("No newline at end of file %s", filevec[i].name, "");
1073*86d7f5d3SJohn Marino changes = 2;
1074*86d7f5d3SJohn Marino }
1075*86d7f5d3SJohn Marino }
1076*86d7f5d3SJohn Marino
1077*86d7f5d3SJohn Marino if (filevec[0].buffer != filevec[1].buffer)
1078*86d7f5d3SJohn Marino free (filevec[0].buffer);
1079*86d7f5d3SJohn Marino free (filevec[1].buffer);
1080*86d7f5d3SJohn Marino
1081*86d7f5d3SJohn Marino return changes;
1082*86d7f5d3SJohn Marino }
1083