home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 317a.lha / RCS / diff / context.c < prev    next >
C/C++ Source or Header  |  1989-12-05  |  9KB  |  304 lines

  1. /* Context-format output routines 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. #include "diff.h"
  21.  
  22. static void pr_context_hunk ();
  23. static struct change *find_hunk ();
  24. static void mark_ignorable ();
  25. static int find_function ();
  26.  
  27. /* Last place find_function started searching from.  */
  28. static int find_function_last_search;
  29.  
  30. /* The value find_function returned when it started searching there.  */
  31. static int find_function_last_match;
  32.  
  33. /* Print a header for a context diff, with the file names and dates.  */
  34.  
  35. void
  36. print_context_header (inf)
  37.      struct file_data *inf;
  38. {
  39.   fprintf (outfile, "*** %s\t%s", inf[0].name,
  40.        ctime (&inf[0].stat.st_mtime));
  41.   fprintf (outfile, "--- %s\t%s", inf[1].name,
  42.        ctime (&inf[1].stat.st_mtime));
  43. }
  44.  
  45. /* Print an edit script in context format.  */
  46.  
  47. void
  48. print_context_script (script)
  49.      struct change *script;
  50. {
  51.   if (ignore_blank_lines_flag || ignore_regexp)
  52.     mark_ignorable (script);
  53.   else
  54.     {
  55.       struct change *e;
  56.       for (e = script; e; e = e->link)
  57.     e->ignore = 0;
  58.     }
  59.  
  60.   find_function_last_search = 0;
  61.   find_function_last_match = -1;
  62.  
  63.   print_script (script, find_hunk, pr_context_hunk);
  64. }
  65.  
  66. /* Print a pair of line numbers with a comma, translated for file FILE.
  67.    If the second number is smaller, use the first in place of it.
  68.  
  69.    Args A and B are internal line numbers.
  70.    We print the translated (real) line numbers.  */
  71.  
  72. static void
  73. print_context_number_range (file, a, b)
  74.      struct file_data *file;
  75.      int a, b;
  76. {
  77.   int trans_a, trans_b;
  78.   translate_range (file, a, b, &trans_a, &trans_b);
  79.  
  80.   /* Note: we can have B < A in the case of a range of no lines.
  81.      In this case, we should print the line number before the range,
  82.      which is B.  */
  83.   if (trans_b >= trans_a)
  84.     fprintf (outfile, "%d,%d", trans_a, trans_b);
  85.   else
  86.     fprintf (outfile, "%d", trans_b);
  87. }
  88.  
  89. /* Print a portion of an edit script in context format.
  90.    HUNK is the beginning of the portion to be printed.
  91.    The end is marked by a `link' that has been nulled out.
  92.  
  93.    Prints out lines from both files, and precedes each
  94.    line with the appropriate flag-character.  */
  95.  
  96. static void
  97. pr_context_hunk (hunk)
  98.      struct change *hunk;
  99. {
  100.   int first0, last0, first1, last1, show_from, show_to, i;
  101.   struct change *next;
  102.   char *prefix;
  103.   char *function;
  104.   int function_length;
  105.  
  106.   /* Determine range of line numbers involved in each file.  */
  107.  
  108.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  109.  
  110.   if (!show_from && !show_to)
  111.     return;
  112.  
  113.   /* Include a context's width before and after.  */
  114.  
  115.   first0 = max (first0 - context, 0);
  116.   first1 = max (first1 - context, 0);
  117.   last0 = min (last0 + context, files[0].buffered_lines - 1);
  118.   last1 = min (last1 + context, files[1].buffered_lines - 1);
  119.  
  120.   /* If desired, find the preceding function definition line in file 0.  */
  121.   function = 0;
  122.   if (function_regexp)
  123.     find_function (&files[0], first0, &function, &function_length);
  124.  
  125.   /* If we looked for and found a function this is part of,
  126.      include its name in the header of the diff section.  */
  127.   fprintf (outfile, "***************");
  128.  
  129.   if (function)
  130.     {
  131.       fprintf (outfile, " ");
  132.       fwrite (function, 1, min (function_length - 1, 40), outfile);
  133.     }
  134.  
  135.   fprintf (outfile, "\n*** ");
  136.   print_context_number_range (&files[0], first0, last0);
  137.   fprintf (outfile, " ****\n");
  138.  
  139.   if (show_from)
  140.     {
  141.       next = hunk;
  142.  
  143.       for (i = first0; i <= last0; i++)
  144.     {
  145.       /* Skip past changes that apply (in file 0)
  146.          only to lines before line I.  */
  147.  
  148.       while (next && next->line0 + next->deleted <= i)
  149.         next = next->link;
  150.  
  151.       /* Compute the marking for line I.  */
  152.  
  153.       prefix = " ";
  154.       if (next && next->line0 <= i)
  155.         /* The change NEXT covers this line.
  156.            If lines were inserted here in file 1, this is "changed".
  157.            Otherwise it is "deleted".  */
  158.         prefix = (next->inserted > 0 ? "!" : "-");
  159.  
  160.       print_1_line (prefix, &files[0].linbuf[i]);
  161.     }
  162.     }
  163.  
  164.   fprintf (outfile, "--- ");
  165.   print_context_number_range (&files[1], first1, last1);
  166.   fprintf (outfile, " ----\n");
  167.  
  168.   if (show_to)
  169.     {
  170.       next = hunk;
  171.  
  172.       for (i = first1; i <= last1; i++)
  173.     {
  174.       /* Skip past changes that apply (in file 1)
  175.          only to lines before line I.  */
  176.  
  177.       while (next && next->line1 + next->inserted <= i)
  178.         next = next->link;
  179.  
  180.       /* Compute the marking for line I.  */
  181.  
  182.       prefix = " ";
  183.       if (next && next->line1 <= i)
  184.         /* The change NEXT covers this line.
  185.            If lines were deleted here in file 0, this is "changed".
  186.            Otherwise it is "inserted".  */
  187.         prefix = (next->deleted > 0 ? "!" : "+");
  188.  
  189.       print_1_line (prefix, &files[1].linbuf[i]);
  190.     }
  191.     }
  192. }
  193.  
  194. /* Scan a (forward-ordered) edit script for the first place that at least
  195.    2*CONTEXT unchanged lines appear, and return a pointer
  196.    to the `struct change' for the last change before those lines.  */
  197.  
  198. static struct change *
  199. find_hunk (start)
  200.      struct change *start;
  201. {
  202.   struct change *prev;
  203.   int top0, top1;
  204.   int thresh;
  205.  
  206.   do
  207.     {
  208.       /* Computer number of first line in each file beyond this changed.  */
  209.       top0 = start->line0 + start->deleted;
  210.       top1 = start->line1 + start->inserted;
  211.       prev = start;
  212.       start = start->link;
  213.       /* Threshold distance is 2*CONTEXT between two non-ignorable changes,
  214.      but only CONTEXT if one is ignorable.  */
  215.       thresh = ((prev->ignore || (start && start->ignore))
  216.         ? context
  217.         : 2 * context);
  218.       /* It is not supposed to matter which file we check in the end-test.
  219.      If it would matter, crash.  */
  220.       if (start && start->line0 - top0 != start->line1 - top1)
  221.     abort ();
  222.     } while (start
  223.          /* Keep going if less than THRESH lines
  224.         elapse before the affected line.  */
  225.          && start->line0 < top0 + thresh);
  226.  
  227.   return prev;
  228. }
  229.  
  230. /* Set the `ignore' flag properly in each change in SCRIPT.
  231.    It should be 1 if all the lines inserted or deleted in that change
  232.    are ignorable lines.  */
  233.  
  234. static void
  235. mark_ignorable (script)
  236.      struct change *script;
  237. {
  238.   while (script)
  239.     {
  240.       struct change *next = script->link;
  241.       int first0, last0, first1, last1, deletes, inserts;
  242.  
  243.       /* Turn this change into a hunk: detach it from the others.  */
  244.       script->link = 0;
  245.  
  246.       /* Determine whether this change is ignorable.  */
  247.       analyze_hunk (script, &first0, &last0, &first1, &last1, &deletes, &insert
  248. );
  249.       /* Reconnect the chain as before.  */
  250.       script->link = next;
  251.  
  252.       /* If the change is ignorable, mark it.  */
  253.       script->ignore = (!deletes && !inserts);
  254.  
  255.       /* Advance to the following change.  */
  256.       script = next;
  257.     }
  258. }
  259.  
  260. /* Find the last function-header line in FILE prior to line number LINENUM.
  261.    This is a line containing a match for the regexp in `function_regexp'.
  262.    Store the address of the line text into LINEP and the length of the
  263.    line into LENP.
  264.    Do not store anything if no function-header is found.  */
  265.  
  266. static int
  267. find_function (file, linenum, linep, lenp)
  268.      struct file_data *file;
  269.      int linenum;
  270.      char **linep;
  271.      int *lenp;
  272. {
  273.   int i = linenum;
  274.   int last = find_function_last_search;
  275.   find_function_last_search = i;
  276.  
  277.   while (--i >= last)
  278.     {
  279.       /* See if this line is what we want.  */
  280.  
  281.       if (0 <= re_search (&function_regexp_compiled,
  282.               files[0].linbuf[i].text,
  283.               files[0].linbuf[i].length,
  284.               0, files[0].linbuf[i].length,
  285.               0))
  286.     {
  287.       *linep = files[0].linbuf[i].text;
  288.       *lenp = files[0].linbuf[i].length;
  289.       find_function_last_match = i;
  290.       return 1;
  291.     }
  292.     }
  293.   /* If we search back to where we started searching the previous time,
  294.      find the line we found last time.  */
  295.   if (find_function_last_match >= 0)
  296.     {
  297.       i = find_function_last_match;
  298.       *linep = files[0].linbuf[i].text;
  299.       *lenp = files[0].linbuf[i].length;
  300.       return 1;
  301.     }
  302.   return 0;
  303. }
  304.