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