[comp.sources.unix] v24i020: GNU Diff, version 1.15, Part05/08

rsalz@uunet.uu.net (Rich Salz) (02/26/91)

Submitted-by: Paul Eggert <eggert@twinsun.com>
Posting-number: Volume 24, Issue 20
Archive-name: gnudiff1.15/part05

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 5 (of 8)."
# Contents:  analyze.c
# Wrapped by eggert@ata on Mon Jan  7 11:25:30 1991
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'analyze.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'analyze.c'\"
else
echo shar: Extracting \"'analyze.c'\" \(26295 characters\)
sed "s/^X//" >'analyze.c' <<'END_OF_FILE'
X/* Analyze file differences for GNU DIFF.
X   Copyright (C) 1988, 1989 Free Software Foundation, Inc.
X
XThis file is part of GNU DIFF.
X
XGNU DIFF is free software; you can redistribute it and/or modify
Xit under the terms of the GNU General Public License as published by
Xthe Free Software Foundation; either version 1, or (at your option)
Xany later version.
X
XGNU DIFF is distributed in the hope that it will be useful,
Xbut WITHOUT ANY WARRANTY; without even the implied warranty of
XMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
XGNU General Public License for more details.
X
XYou should have received a copy of the GNU General Public License
Xalong with GNU DIFF; see the file COPYING.  If not, write to
Xthe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
X
X/* The basic algorithm is described in: 
X   "An O(ND) Difference Algorithm and its Variations", Eugene Myers,
X   Algorithmica Vol. 1 No. 2, 1986, p 251.  */
X
X#include "diff.h"
X
Xstruct change *find_change ();
Xvoid finish_output ();
Xvoid print_context_header ();
Xvoid print_context_script ();
Xvoid print_ed_script ();
Xvoid print_ifdef_script ();
Xvoid print_normal_script ();
Xvoid print_rcs_script ();
Xvoid pr_forward_ed_script ();
Xvoid setup_output ();
X
Xextern int no_discards;
X
Xstatic int *xvec, *yvec;	/* Vectors being compared. */
Xstatic int *fdiag;		/* Vector, indexed by diagonal, containing
X				   the X coordinate of the point furthest
X				   along the given diagonal in the forward
X				   search of the edit matrix. */
Xstatic int *bdiag;		/* Vector, indexed by diagonal, containing
X				   the X coordinate of the point furthest
X				   along the given diagonal in the backward
X				   search of the edit matrix. */
X
X/* Find the midpoint of the shortest edit script for a specified
X   portion of the two files.
X
X   We scan from the beginnings of the files, and simultaneously from the ends,
X   doing a breadth-first search through the space of edit-sequence.
X   When the two searches meet, we have found the midpoint of the shortest
X   edit sequence.
X
X   The value returned is the number of the diagonal on which the midpoint lies.
X   The diagonal number equals the number of inserted lines minus the number
X   of deleted lines (counting only lines before the midpoint).
X   The edit cost is stored into *COST; this is the total number of
X   lines inserted or deleted (counting only lines before the midpoint).
X
X   This function assumes that the first lines of the specified portions
X   of the two files do not match, and likewise that the last lines do not
X   match.  The caller must trim matching lines from the beginning and end
X   of the portions it is going to specify.
X
X   Note that if we return the "wrong" diagonal value, or if
X   the value of bdiag at that diagonal is "wrong",
X   the worst this can do is cause suboptimal diff output.
X   It cannot cause incorrect diff output.  */
X
Xstatic int
Xdiag (xoff, xlim, yoff, ylim, cost)
X     int xoff, xlim, yoff, ylim;
X     int *cost;
X{
X  int *const fd = fdiag;	/* Give the compiler a chance. */
X  int *const bd = bdiag;	/* Additional help for the compiler. */
X  int *const xv = xvec;		/* Still more help for the compiler. */
X  int *const yv = yvec;		/* And more and more . . . */
X  const int dmin = xoff - ylim;	/* Minimum valid diagonal. */
X  const int dmax = xlim - yoff;	/* Maximum valid diagonal. */
X  const int fmid = xoff - yoff;	/* Center diagonal of top-down search. */
X  const int bmid = xlim - ylim;	/* Center diagonal of bottom-up search. */
X  int fmin = fmid, fmax = fmid;	/* Limits of top-down search. */
X  int bmin = bmid, bmax = bmid;	/* Limits of bottom-up search. */
X  int c;			/* Cost. */
X  int odd = fmid - bmid & 1;	/* True if southeast corner is on an odd
X				   diagonal with respect to the northwest. */
X
X  fd[fmid] = xoff;
X  bd[bmid] = xlim;
X
X  for (c = 1;; ++c)
X    {
X      int d;			/* Active diagonal. */
X      int big_snake = 0;
X
X      /* Extend the top-down search by an edit step in each diagonal. */
X      fmin > dmin ? fd[--fmin - 1] = -1 : ++fmin;
X      fmax < dmax ? fd[++fmax + 1] = -1 : --fmax;
X      for (d = fmax; d >= fmin; d -= 2)
X	{
X	  int x, y, oldx, tlo = fd[d - 1], thi = fd[d + 1];
X
X	  if (tlo >= thi)
X	    x = tlo + 1;
X	  else
X	    x = thi;
X	  oldx = x;
X	  y = x - d;
X	  while (x < xlim && y < ylim && xv[x] == yv[y])
X	    ++x, ++y;
X	  if (x - oldx > 20)
X	    big_snake = 1;
X	  fd[d] = x;
X	  if (odd && bmin <= d && d <= bmax && bd[d] <= fd[d])
X	    {
X	      *cost = 2 * c - 1;
X	      return d;
X	    }
X	}
X
X      /* Similar extend the bottom-up search. */
X      bmin > dmin ? bd[--bmin - 1] = INT_MAX : ++bmin;
X      bmax < dmax ? bd[++bmax + 1] = INT_MAX : --bmax;
X      for (d = bmax; d >= bmin; d -= 2)
X	{
X	  int x, y, oldx, tlo = bd[d - 1], thi = bd[d + 1];
X
X	  if (tlo < thi)
X	    x = tlo;
X	  else
X	    x = thi - 1;
X	  oldx = x;
X	  y = x - d;
X	  while (x > xoff && y > yoff && xv[x - 1] == yv[y - 1])
X	    --x, --y;
X	  if (oldx - x > 20)
X	    big_snake = 1;
X	  bd[d] = x;
X	  if (!odd && fmin <= d && d <= fmax && bd[d] <= fd[d])
X	    {
X	      *cost = 2 * c;
X	      return d;
X	    }
X	}
X
X      /* Heuristic: check occasionally for a diagonal that has made
X	 lots of progress compared with the edit distance.
X	 If we have any such, find the one that has made the most
X	 progress and return it as if it had succeeded.
X
X	 With this heuristic, for files with a constant small density
X	 of changes, the algorithm is linear in the file size.  */
X
X      if (c > 200 && big_snake && heuristic)
X	{
X	  int best;
X	  int bestpos;
X
X	  best = 0;
X	  for (d = fmax; d >= fmin; d -= 2)
X	    {
X	      int dd = d - fmid;
X	      if ((fd[d] - xoff)*2 - dd > 12 * (c + (dd > 0 ? dd : -dd)))
X		{
X		  if (fd[d] * 2 - dd > best
X		      && fd[d] - xoff > 20
X		      && fd[d] - d - yoff > 20)
X		    {
X		      int k;
X		      int x = fd[d];
X
X		      /* We have a good enough best diagonal;
X			 now insist that it end with a significant snake.  */
X		      for (k = 1; k <= 20; k++)
X			if (xvec[x - k] != yvec[x - d - k])
X			  break;
X
X		      if (k == 21)
X			{
X			  best = fd[d] * 2 - dd;
X			  bestpos = d;
X			}
X		    }
X		}
X	    }
X	  if (best > 0)
X	    {
X	      *cost = 2 * c - 1;
X	      return bestpos;
X	    }
X
X	  best = 0;
X	  for (d = bmax; d >= bmin; d -= 2)
X	    {
X	      int dd = d - bmid;
X	      if ((xlim - bd[d])*2 + dd > 12 * (c + (dd > 0 ? dd : -dd)))
X		{
X		  if ((xlim - bd[d]) * 2 + dd > best
X		      && xlim - bd[d] > 20
X		      && ylim - (bd[d] - d) > 20)
X		    {
X		      /* We have a good enough best diagonal;
X			 now insist that it end with a significant snake.  */
X		      int k;
X		      int x = bd[d];
X
X		      for (k = 0; k < 20; k++)
X			if (xvec[x + k] != yvec[x - d + k])
X			  break;
X		      if (k == 20)
X			{
X			  best = (xlim - bd[d]) * 2 + dd;
X			  bestpos = d;
X			}
X		    }
X		}
X	    }
X	  if (best > 0)
X	    {
X	      *cost = 2 * c - 1;
X	      return bestpos;
X	    }
X	}
X    }
X}
X
X/* Compare in detail contiguous subsequences of the two files
X   which are known, as a whole, to match each other.
X
X   The results are recorded in the vectors files[N].changed_flag, by
X   storing a 1 in the element for each line that is an insertion or deletion.
X
X   The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
X
X   Note that XLIM, YLIM are exclusive bounds.
X   All line numbers are origin-0 and discarded lines are not counted.  */
X
Xstatic void
Xcompareseq (xoff, xlim, yoff, ylim)
X     int xoff, xlim, yoff, ylim;
X{
X  /* Slide down the bottom initial diagonal. */
X  while (xoff < xlim && yoff < ylim && xvec[xoff] == yvec[yoff])
X    ++xoff, ++yoff;
X  /* Slide up the top initial diagonal. */
X  while (xlim > xoff && ylim > yoff && xvec[xlim - 1] == yvec[ylim - 1])
X    --xlim, --ylim;
X  
X  /* Handle simple cases. */
X  if (xoff == xlim)
X    while (yoff < ylim)
X      files[1].changed_flag[files[1].realindexes[yoff++]] = 1;
X  else if (yoff == ylim)
X    while (xoff < xlim)
X      files[0].changed_flag[files[0].realindexes[xoff++]] = 1;
X  else
X    {
X      int c, d, f, b;
X
X      /* Find a point of correspondence in the middle of the files.  */
X
X      d = diag (xoff, xlim, yoff, ylim, &c);
X      f = fdiag[d];
X      b = bdiag[d];
X
X      if (c == 1)
X	{
X	  /* This should be impossible, because it implies that
X	     one of the two subsequences is empty,
X	     and that case was handled above without calling `diag'.
X	     Let's verify that this is true.  */
X	  abort ();
X#if 0
X	  /* The two subsequences differ by a single insert or delete;
X	     record it and we are done.  */
X	  if (d < xoff - yoff)
X	    files[1].changed_flag[files[1].realindexes[b - d - 1]] = 1;
X	  else
X	    files[0].changed_flag[files[0].realindexes[b]] = 1;
X#endif
X	}
X      else
X	{
X	  /* Use that point to split this problem into two subproblems.  */
X	  compareseq (xoff, b, yoff, b - d);
X	  /* This used to use f instead of b,
X	     but that is incorrect!
X	     It is not necessarily the case that diagonal d
X	     has a snake from b to f.  */
X	  compareseq (b, xlim, b - d, ylim);
X	}
X    }
X}
X
X/* Discard lines from one file that have no matches in the other file.
X
X   A line which is discarded will not be considered by the actual
X   comparison algorithm; it will be as if that line were not in the file.
X   The file's `realindexes' table maps virtual line numbers
X   (which don't count the discarded lines) into real line numbers;
X   this is how the actual comparison algorithm produces results
X   that are comprehensible when the discarded lines are counted.
X
X   When we discard a line, we also mark it as a deletion or insertion
X   so that it will be printed in the output.  */
X
Xvoid
Xdiscard_confusing_lines (filevec)
X     struct file_data filevec[];
X{
X  unsigned int f, i;
X  char *discarded[2];
X  int *equiv_count[2];
X
X  /* Allocate our results.  */
X  for (f = 0; f < 2; f++)
X    {
X      filevec[f].undiscarded
X	= (int *) xmalloc (filevec[f].buffered_lines * sizeof (int));
X      filevec[f].realindexes
X	= (int *) xmalloc (filevec[f].buffered_lines * sizeof (int));
X    }
X
X  /* Set up equiv_count[F][I] as the number of lines in file F
X     that fall in equivalence class I.  */
X
X  equiv_count[0] = (int *) xmalloc (filevec[0].equiv_max * sizeof (int));
X  bzero (equiv_count[0], filevec[0].equiv_max * sizeof (int));
X  equiv_count[1] = (int *) xmalloc (filevec[1].equiv_max * sizeof (int));
X  bzero (equiv_count[1], filevec[1].equiv_max * sizeof (int));
X
X  for (i = 0; i < filevec[0].buffered_lines; ++i)
X    ++equiv_count[0][filevec[0].equivs[i]];
X  for (i = 0; i < filevec[1].buffered_lines; ++i)
X    ++equiv_count[1][filevec[1].equivs[i]];
X
X  /* Set up tables of which lines are going to be discarded.  */
X
X  discarded[0] = (char *) xmalloc (filevec[0].buffered_lines);
X  discarded[1] = (char *) xmalloc (filevec[1].buffered_lines);
X  bzero (discarded[0], filevec[0].buffered_lines);
X  bzero (discarded[1], filevec[1].buffered_lines);
X
X  /* Mark to be discarded each line that matches no line of the other file.
X     If a line matches many lines, mark it as provisionally discardable.  */
X
X  for (f = 0; f < 2; f++)
X    {
X      unsigned int end = filevec[f].buffered_lines;
X      char *discards = discarded[f];
X      int *counts = equiv_count[1 - f];
X      int *equivs = filevec[f].equivs;
X      unsigned int many = 5;
X      unsigned int tem = end / 64;
X
X      /* Multiply MANY by approximate square root of number of lines.
X	 That is the threshold for provisionally discardable lines.  */
X      while ((tem = tem >> 2) > 0)
X	many *= 2;
X
X      for (i = 0; i < end; i++)
X	{
X	  int nmatch;
X	  if (equivs[i] == 0)
X	    continue;
X	  nmatch = counts[equivs[i]];
X	  if (nmatch == 0)
X	    discards[i] = 1;
X	  else if (nmatch > many)
X	    discards[i] = 2;
X	}
X    }
X
X  /* Don't really discard the provisional lines except when they occur
X     in a run of discardables, with nonprovisionals at the beginning
X     and end.  */
X
X  for (f = 0; f < 2; f++)
X    {
X      unsigned int end = filevec[f].buffered_lines;
X      register char *discards = discarded[f];
X
X      for (i = 0; i < end; i++)
X	{
X	  /* Cancel provisional discards not in middle of run of discards.  */
X	  if (discards[i] == 2)
X	    discards[i] = 0;
X	  else if (discards[i] != 0)
X	    {
X	      /* We have found a nonprovisional discard.  */
X	      register int j;
X	      unsigned int length;
X	      unsigned int provisional = 0;
X
X	      /* Find end of this run of discardable lines.
X		 Count how many are provisionally discardable.  */
X	      for (j = i; j < end; j++)
X		{
X		  if (discards[j] == 0)
X		    break;
X		  if (discards[j] == 2)
X		    ++provisional;
X		}
X
X	      /* Cancel provisional discards at end, and shrink the run.  */
X	      while (j > i && discards[j - 1] == 2)
X		discards[--j] = 0, --provisional;
X
X	      /* Now we have the length of a run of discardable lines
X		 whose first and last are not provisional.  */
X	      length = j - i;
X
X	      /* If 1/4 of the lines in the run are provisional,
X		 cancel discarding of all provisional lines in the run.  */
X	      if (provisional * 4 > length)
X		{
X		  while (j > i)
X		    if (discards[--j] == 2)
X		      discards[j] = 0;
X		}
X	      else
X		{
X		  register unsigned int consec;
X		  unsigned int minimum = 1;
X		  unsigned int tem = length / 4;
X
X		  /* MINIMUM is approximate square root of LENGTH/4.
X		     A subrun of two or more provisionals can stand
X		     when LENGTH is at least 16.
X		     A subrun of 4 or more can stand when LENGTH >= 64.  */
X		  while ((tem = tem >> 2) > 0)
X		    minimum *= 2;
X		  minimum++;
X
X		  /* Cancel any subrun of MINIMUM or more provisionals
X		     within the larger run.  */
X		  for (j = 0, consec = 0; j < length; j++)
X		    if (discards[i + j] != 2)
X		      consec = 0;
X		    else if (minimum == ++consec)
X		      /* Back up to start of subrun, to cancel it all.  */
X		      j -= consec;
X		    else if (minimum < consec)
X		      discards[i + j] = 0;
X
X		  /* Scan from beginning of run
X		     until we find 3 or more nonprovisionals in a row
X		     or until the first nonprovisional at least 8 lines in.
X		     Until that point, cancel any provisionals.  */
X		  for (j = 0, consec = 0; j < length; j++)
X		    {
X		      if (j >= 8 && discards[i + j] == 1)
X			break;
X		      if (discards[i + j] == 2)
X			consec = 0, discards[i + j] = 0;
X		      else if (discards[i + j] == 0)
X			consec = 0;
X		      else
X			consec++;
X		      if (consec == 3)
X			break;
X		    }
X
X		  /* I advances to the last line of the run.  */
X		  i += length - 1;
X
X		  /* Same thing, from end.  */
X		  for (j = 0, consec = 0; j < length; j++)
X		    {
X		      if (j >= 8 && discards[i - j] == 1)
X			break;
X		      if (discards[i - j] == 2)
X			consec = 0, discards[i - j] = 0;
X		      else if (discards[i - j] == 0)
X			consec = 0;
X		      else
X			consec++;
X		      if (consec == 3)
X			break;
X		    }
X		}
X	    }
X	}
X    }
X
X  /* Actually discard the lines. */
X  for (f = 0; f < 2; f++)
X    {
X      char *discards = discarded[f];
X      unsigned int end = filevec[f].buffered_lines;
X      unsigned int j = 0;
X      for (i = 0; i < end; ++i)
X	if (no_discards || discards[i] == 0)
X	  {
X	    filevec[f].undiscarded[j] = filevec[f].equivs[i];
X	    filevec[f].realindexes[j++] = i;
X	  }
X	else
X	  filevec[f].changed_flag[i] = 1;
X      filevec[f].nondiscarded_lines = j;
X    }
X
X  free (discarded[1]);
X  free (discarded[0]);
X  free (equiv_count[1]);
X  free (equiv_count[0]);
X}
X
X/* Adjust inserts/deletes of blank lines to join changes
X   as much as possible.
X
X   We do something when a run of changed lines include a blank
X   line at one end and have an excluded blank line at the other.
X   We are free to choose which blank line is included.
X   `compareseq' always chooses the one at the beginning,
X   but usually it is cleaner to consider the following blank line
X   to be the "change".  The only exception is if the preceding blank line
X   would join this change to other changes.  */
X
Xint inhibit;
X
Xstatic void
Xshift_boundaries (filevec)
X     struct file_data filevec[];
X{
X  int f;
X
X  if (inhibit)
X    return;
X
X  for (f = 0; f < 2; f++)
X    {
X      char *changed = filevec[f].changed_flag;
X      char *other_changed = filevec[1-f].changed_flag;
X      int i = 0;
X      int j = 0;
X      int i_end = filevec[f].buffered_lines;
X      int preceding = -1;
X      int other_preceding = -1;
X
X      while (1)
X	{
X	  int start, end, other_start;
X
X	  /* Scan forwards to find beginning of another run of changes.
X	     Also keep track of the corresponding point in the other file.  */
X
X	  while (i < i_end && changed[i] == 0)
X	    {
X	      while (other_changed[j++])
X		/* Non-corresponding lines in the other file
X		   will count as the preceding batch of changes.  */
X		other_preceding = j;
X	      i++;
X	    }
X
X	  if (i == i_end)
X	    break;
X
X	  start = i;
X	  other_start = j;
X
X	  while (1)
X	    {
X	      /* Now find the end of this run of changes.  */
X
X	      while (i < i_end && changed[i] != 0) i++;
X	      end = i;
X
X	      /* If the first changed line matches the following unchanged one,
X		 and this run does not follow right after a previous run,
X		 and there are no lines deleted from the other file here,
X		 then classify the first changed line as unchanged
X		 and the following line as changed in its place.  */
X
X	      /* You might ask, how could this run follow right after another?
X		 Only because the previous run was shifted here.  */
X
X	      if (end != i_end
X		  && files[f].equivs[start] == files[f].equivs[end]
X		  && !other_changed[j]
X		  && end != i_end
X		  && !((preceding >= 0 && start == preceding)
X		       || (other_preceding >= 0
X			   && other_start == other_preceding)))
X		{
X		  changed[end++] = 1;
X		  changed[start++] = 0;
X		  ++i;
X		  /* Since one line-that-matches is now before this run
X		     instead of after, we must advance in the other file
X		     to keep in synch.  */
X		  ++j;
X		}
X	      else
X		break;
X	    }
X
X	  preceding = i;
X	  other_preceding = j;
X	}
X    }
X}
X
X/* Cons an additional entry onto the front of an edit script OLD.
X   LINE0 and LINE1 are the first affected lines in the two files (origin 0).
X   DELETED is the number of lines deleted here from file 0.
X   INSERTED is the number of lines inserted here in file 1.
X
X   If DELETED is 0 then LINE0 is the number of the line before
X   which the insertion was done; vice versa for INSERTED and LINE1.  */
X
Xstatic struct change *
Xadd_change (line0, line1, deleted, inserted, old)
X     int line0, line1, deleted, inserted;
X     struct change *old;
X{
X  struct change *new = (struct change *) xmalloc (sizeof (struct change));
X
X  new->line0 = line0;
X  new->line1 = line1;
X  new->inserted = inserted;
X  new->deleted = deleted;
X  new->link = old;
X  return new;
X}
X
X/* Scan the tables of which lines are inserted and deleted,
X   producing an edit script in reverse order.  */
X
Xstatic struct change *
Xbuild_reverse_script (filevec)
X     struct file_data filevec[];
X{
X  struct change *script = 0;
X  char *changed0 = filevec[0].changed_flag;
X  char *changed1 = filevec[1].changed_flag;
X  int len0 = filevec[0].buffered_lines;
X  int len1 = filevec[1].buffered_lines;
X
X  /* Note that changedN[len0] does exist, and contains 0.  */
X
X  int i0 = 0, i1 = 0;
X
X  while (i0 < len0 || i1 < len1)
X    {
X      if (changed0[i0] || changed1[i1])
X	{
X	  int line0 = i0, line1 = i1;
X
X	  /* Find # lines changed here in each file.  */
X	  while (changed0[i0]) ++i0;
X	  while (changed1[i1]) ++i1;
X
X	  /* Record this change.  */
X	  script = add_change (line0, line1, i0 - line0, i1 - line1, script);
X	}
X
X      /* We have reached lines in the two files that match each other.  */
X      i0++, i1++;
X    }
X
X  return script;
X}
X
X/* Scan the tables of which lines are inserted and deleted,
X   producing an edit script in forward order.  */
X
Xstatic struct change *
Xbuild_script (filevec)
X     struct file_data filevec[];
X{
X  struct change *script = 0;
X  char *changed0 = filevec[0].changed_flag;
X  char *changed1 = filevec[1].changed_flag;
X  int len0 = filevec[0].buffered_lines;
X  int len1 = filevec[1].buffered_lines;
X  int i0 = len0, i1 = len1;
X
X  /* Note that changedN[-1] does exist, and contains 0.  */
X
X#if 0 /* Unnecessary since a line includes its trailing newline.  */
X  /* In RCS comparisons, making the existence or nonexistence of trailing
X     newlines really matter. */
X  if (output_style == OUTPUT_RCS
X      && filevec[0].missing_newline != filevec[1].missing_newline)
X    changed0[len0 - 1] = changed1[len1 - 1] = 1;
X#endif
X
X  while (i0 >= 0 || i1 >= 0)
X    {
X      if (changed0[i0 - 1] || changed1[i1 - 1])
X	{
X	  int line0 = i0, line1 = i1;
X
X	  /* Find # lines changed here in each file.  */
X	  while (changed0[i0 - 1]) --i0;
X	  while (changed1[i1 - 1]) --i1;
X
X	  /* Record this change.  */
X	  script = add_change (i0, i1, line0 - i0, line1 - i1, script);
X	}
X
X      /* We have reached lines in the two files that match each other.  */
X      i0--, i1--;
X    }
X
X  return script;
X}
X
X/* Report the differences of two files.  DEPTH is the current directory
X   depth. */
Xint
Xdiff_2_files (filevec, depth)
X     struct file_data filevec[];
X     int depth;
X{
X  int diags;
X  int i;
X  struct change *e, *p;
X  struct change *script;
X  int binary;
X  int changes;
X
X  /* See if the two named files are actually the same physical file.
X     If so, we know they are identical without actually reading them.  */
X
X  if (output_style != OUTPUT_IFDEF
X      && filevec[0].stat.st_ino == filevec[1].stat.st_ino
X      && filevec[0].stat.st_dev == filevec[1].stat.st_dev)
X    return 0;
X
X  binary = read_files (filevec);
X
X  /* If we have detected that file 0 is a binary file,
X     compare the two files as binary.  This can happen
X     only when the first chunk is read.
X     Also, -q means treat all files as binary.  */
X
X  if (binary || no_details_flag)
X    {
X      int differs = (filevec[0].buffered_chars != filevec[1].buffered_chars
X		     || bcmp (filevec[0].buffer, filevec[1].buffer,
X			      filevec[1].buffered_chars));
X      if (differs) 
X	message (binary ? "Binary files %s and %s differ\n"
X		 : "Files %s and %s differ\n",
X		 filevec[0].name, filevec[1].name);
X
X      for (i = 0; i < 2; ++i)
X	if (filevec[i].buffer)
X	  free (filevec[i].buffer);
X      return differs;
X    }
X
X  /* Allocate vectors for the results of comparison:
X     a flag for each line of each file, saying whether that line
X     is an insertion or deletion.
X     Allocate an extra element, always zero, at each end of each vector.  */
X
X  filevec[0].changed_flag = (char *) xmalloc (filevec[0].buffered_lines + 2);
X  filevec[1].changed_flag = (char *) xmalloc (filevec[1].buffered_lines + 2);
X  bzero (filevec[0].changed_flag, filevec[0].buffered_lines + 2);
X  bzero (filevec[1].changed_flag, filevec[1].buffered_lines + 2);
X  filevec[0].changed_flag++;
X  filevec[1].changed_flag++;
X
X  /* Some lines are obviously insertions or deletions
X     because they don't match anything.  Detect them now,
X     and avoid even thinking about them in the main comparison algorithm.  */
X
X  discard_confusing_lines (filevec);
X
X  /* Now do the main comparison algorithm, considering just the
X     undiscarded lines.  */
X
X  xvec = filevec[0].undiscarded;
X  yvec = filevec[1].undiscarded;
X  diags = filevec[0].nondiscarded_lines + filevec[1].nondiscarded_lines + 3;
X  fdiag = (int *) xmalloc (diags * sizeof (int));
X  fdiag += filevec[1].nondiscarded_lines + 1;
X  bdiag = (int *) xmalloc (diags * sizeof (int));
X  bdiag += filevec[1].nondiscarded_lines + 1;
X
X  files[0] = filevec[0];
X  files[1] = filevec[1];
X
X  compareseq (0, filevec[0].nondiscarded_lines,
X	      0, filevec[1].nondiscarded_lines);
X
X  bdiag -= filevec[1].nondiscarded_lines + 1;
X  free (bdiag);
X  fdiag -= filevec[1].nondiscarded_lines + 1;
X  free (fdiag);
X
X  /* Modify the results slightly to make them prettier
X     in cases where that can validly be done.  */
X
X  shift_boundaries (filevec);
X
X  /* Get the results of comparison in the form of a chain
X     of `struct change's -- an edit script.  */
X
X  if (output_style == OUTPUT_ED)
X    script = build_reverse_script (filevec);
X  else
X    script = build_script (filevec);
X
X  if (script || output_style == OUTPUT_IFDEF)
X    {
X      setup_output (files[0].name, files[1].name, depth);
X
X      switch (output_style)
X	{
X	case OUTPUT_CONTEXT:
X	  print_context_header (files, 0);
X	  print_context_script (script, 0);
X	  break;
X
X	case OUTPUT_UNIFIED:
X	  print_context_header (files, 1);
X	  print_context_script (script, 1);
X	  break;
X
X	case OUTPUT_ED:
X	  print_ed_script (script);
X	  break;
X
X	case OUTPUT_FORWARD_ED:
X	  pr_forward_ed_script (script);
X	  break;
X
X	case OUTPUT_RCS:
X	  print_rcs_script (script);
X	  break;
X
X	case OUTPUT_NORMAL:
X	  print_normal_script (script);
X	  break;
X
X	case OUTPUT_IFDEF:
X	  print_ifdef_script (script);
X	  break;
X	}
X
X      finish_output ();
X    }
X
X  /* Set CHANGES if we had any diffs that were printed.
X     If some changes are being ignored, we must scan the script to decide.  */
X  if (ignore_blank_lines_flag || ignore_regexp)
X    {
X      struct change *next = script;
X      changes = 0;
X
X      while (next && changes == 0)
X	{
X	  struct change *this, *end;
X	  int first0, last0, first1, last1, deletes, inserts;
X
X	  /* Find a set of changes that belong together.  */
X	  this = next;
X	  end = find_change (next);
X
X	  /* Disconnect them from the rest of the changes,
X	     making them a hunk, and remember the rest for next iteration.  */
X	  next = end->link;
X	  end->link = NULL;
X
X	  /* Determine whether this hunk was printed.  */
X	  analyze_hunk (this, &first0, &last0, &first1, &last1,
X			&deletes, &inserts);
X
X	  /* Reconnect the script so it will all be freed properly.  */
X	  end->link = next;
X
X	  if (deletes || inserts)
X	    changes = 1;
X	}
X    }
X  else
X    changes = (script != 0);
X
X  for (i = 1; i >= 0; --i)
X    {
X      free (filevec[i].realindexes);
X      free (filevec[i].undiscarded);
X    }
X
X  for (i = 1; i >= 0; --i)
X    free (--filevec[i].changed_flag);
X
X  for (i = 1; i >= 0; --i)
X    free (filevec[i].equivs);
X
X  for (i = 0; i < 2; ++i)
X    {
X      if (filevec[i].buffer != 0)
X	free (filevec[i].buffer);
X      free (filevec[i].linbuf);
X    }
X
X  for (e = script; e; e = p)
X    {
X      p = e->link;
X      free (e);
X    }
X
X  if (! ROBUST_OUTPUT_STYLE (output_style)
X      /* For -D, invent newlines silently.  That's ok in C code.  */
X      && output_style != OUTPUT_IFDEF)
X    for (i = 0; i < 2; ++i)
X      if (filevec[i].missing_newline)
X	{
X	  error ("No newline at end of file %s", filevec[i].name, "");
X	  changes = 2;
X	}
X
X  return changes;
X}
END_OF_FILE
if test 26295 -ne `wc -c <'analyze.c'`; then
    echo shar: \"'analyze.c'\" unpacked with wrong size!
fi
# end of 'analyze.c'
fi
echo shar: End of archive 5 \(of 8\).
cp /dev/null ark5isdone
MISSING=""
for I in 1 2 3 4 5 6 7 8 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have unpacked all 8 archives.
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0

exit 0 # Just in case...
-- 
Please send comp.sources.unix-related mail to rsalz@uunet.uu.net.
Use a domain-based address or give alternate paths, or you may lose out.