home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / dif115as.zip / ANALYZE.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  29KB  |  964 lines

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