home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / rcs57pc3.zip / diff / context.c < prev    next >
C/C++ Source or Header  |  1994-10-31  |  13KB  |  459 lines

  1. /* Context-format output routines for GNU DIFF.
  2.    Copyright (C) 1988,1989,1991,1992,1993,1994 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 2, 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 char const *find_function PARAMS((char const * const *, int));
  23. static struct change *find_hunk PARAMS((struct change *));
  24. static void mark_ignorable PARAMS((struct change *));
  25. static void pr_context_hunk PARAMS((struct change *));
  26. static void pr_unidiff_hunk PARAMS((struct change *));
  27. static void print_context_label PARAMS ((char const *, struct file_data *, char const *));
  28. static void print_context_number_range PARAMS((struct file_data const *, int, int));
  29. static void print_unidiff_number_range PARAMS((struct file_data const *, int, int));
  30.  
  31. /* Last place find_function started searching from.  */
  32. static int find_function_last_search;
  33.  
  34. /* The value find_function returned when it started searching there.  */
  35. static int find_function_last_match;
  36.  
  37. /* Print a label for a context diff, with a file name and date or a label.  */
  38.  
  39. static void
  40. print_context_label (mark, inf, label)
  41.      char const *mark;
  42.      struct file_data *inf;
  43.      char const *label;
  44. {
  45.   if (label)
  46.     fprintf (outfile, "%s %s\n", mark, label);
  47.   else
  48.     {
  49.       char const *ct = ctime (&inf->stat.st_mtime);
  50.       if (!ct)
  51.     ct = "?\n";
  52.       /* See Posix.2 section 4.17.6.1.4 for this format.  */
  53.       fprintf (outfile, "%s %s\t%s", mark, inf->name, ct);
  54.     }
  55. }
  56.  
  57. /* Print a header for a context diff, with the file names and dates.  */
  58.  
  59. void
  60. print_context_header (inf, unidiff_flag)
  61.      struct file_data inf[];
  62.      int unidiff_flag;
  63. {
  64.   if (unidiff_flag)
  65.     {
  66.       print_context_label ("---", &inf[0], file_label[0]);
  67.       print_context_label ("+++", &inf[1], file_label[1]);
  68.     }
  69.   else
  70.     {
  71.       print_context_label ("***", &inf[0], file_label[0]);
  72.       print_context_label ("---", &inf[1], file_label[1]);
  73.     }
  74. }
  75.  
  76. /* Print an edit script in context format.  */
  77.  
  78. void
  79. print_context_script (script, unidiff_flag)
  80.      struct change *script;
  81.      int unidiff_flag;
  82. {
  83.   if (ignore_blank_lines_flag || ignore_regexp.fastmap)
  84.     mark_ignorable (script);
  85.   else
  86.     {
  87.       struct change *e;
  88.       for (e = script; e; e = e->link)
  89.     e->ignore = 0;
  90.     }
  91.  
  92.   find_function_last_search = - files[0].prefix_lines;
  93.   find_function_last_match = INT_MAX;
  94.  
  95.   if (unidiff_flag)
  96.     print_script (script, find_hunk, pr_unidiff_hunk);
  97.   else
  98.     print_script (script, find_hunk, pr_context_hunk);
  99. }
  100.  
  101. /* Print a pair of line numbers with a comma, translated for file FILE.
  102.    If the second number is not greater, use the first in place of it.
  103.  
  104.    Args A and B are internal line numbers.
  105.    We print the translated (real) line numbers.  */
  106.  
  107. static void
  108. print_context_number_range (file, a, b)
  109.      struct file_data const *file;
  110.      int a, b;
  111. {
  112.   int trans_a, trans_b;
  113.   translate_range (file, a, b, &trans_a, &trans_b);
  114.  
  115.   /* Note: we can have B < A in the case of a range of no lines.
  116.      In this case, we should print the line number before the range,
  117.      which is B.  */
  118.   if (trans_b > trans_a)
  119.     fprintf (outfile, "%d,%d", trans_a, trans_b);
  120.   else
  121.     fprintf (outfile, "%d", trans_b);
  122. }
  123.  
  124. /* Print a portion of an edit script in context format.
  125.    HUNK is the beginning of the portion to be printed.
  126.    The end is marked by a `link' that has been nulled out.
  127.  
  128.    Prints out lines from both files, and precedes each
  129.    line with the appropriate flag-character.  */
  130.  
  131. static void
  132. pr_context_hunk (hunk)
  133.      struct change *hunk;
  134. {
  135.   int first0, last0, first1, last1, show_from, show_to, i;
  136.   struct change *next;
  137.   char const *prefix;
  138.   char const *function;
  139.   FILE *out;
  140.  
  141.   /* Determine range of line numbers involved in each file.  */
  142.  
  143.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  144.  
  145.   if (!show_from && !show_to)
  146.     return;
  147.  
  148.   /* Include a context's width before and after.  */
  149.  
  150.   i = - files[0].prefix_lines;
  151.   first0 = max (first0 - context, i);
  152.   first1 = max (first1 - context, i);
  153.   last0 = min (last0 + context, files[0].valid_lines - 1);
  154.   last1 = min (last1 + context, files[1].valid_lines - 1);
  155.  
  156.   /* If desired, find the preceding function definition line in file 0.  */
  157.   function = 0;
  158.   if (function_regexp.fastmap)
  159.     function = find_function (files[0].linbuf, first0);
  160.  
  161.   begin_output ();
  162.   out = outfile;
  163.  
  164.   /* If we looked for and found a function this is part of,
  165.      include its name in the header of the diff section.  */
  166.   fprintf (out, "***************");
  167.  
  168.   if (function)
  169.     {
  170.       putc (' ', out);
  171.       for (i = 0;  i < 40 && function[i] != '\n';  i++)
  172.     continue;
  173.       fwrite (function, 1, i, out);
  174.     }
  175.  
  176.   fprintf (out, "\n*** ");
  177.   print_context_number_range (&files[0], first0, last0);
  178.   fprintf (out, " ****\n");
  179.  
  180.   if (show_from)
  181.     {
  182.       next = hunk;
  183.  
  184.       for (i = first0; i <= last0; i++)
  185.     {
  186.       /* Skip past changes that apply (in file 0)
  187.          only to lines before line I.  */
  188.  
  189.       while (next && next->line0 + next->deleted <= i)
  190.         next = next->link;
  191.  
  192.       /* Compute the marking for line I.  */
  193.  
  194.       prefix = " ";
  195.       if (next && next->line0 <= i)
  196.         /* The change NEXT covers this line.
  197.            If lines were inserted here in file 1, this is "changed".
  198.            Otherwise it is "deleted".  */
  199.         prefix = (next->inserted > 0 ? "!" : "-");
  200.  
  201.       print_1_line (prefix, &files[0].linbuf[i]);
  202.     }
  203.     }
  204.  
  205.   fprintf (out, "--- ");
  206.   print_context_number_range (&files[1], first1, last1);
  207.   fprintf (out, " ----\n");
  208.  
  209.   if (show_to)
  210.     {
  211.       next = hunk;
  212.  
  213.       for (i = first1; i <= last1; i++)
  214.     {
  215.       /* Skip past changes that apply (in file 1)
  216.          only to lines before line I.  */
  217.  
  218.       while (next && next->line1 + next->inserted <= i)
  219.         next = next->link;
  220.  
  221.       /* Compute the marking for line I.  */
  222.  
  223.       prefix = " ";
  224.       if (next && next->line1 <= i)
  225.         /* The change NEXT covers this line.
  226.            If lines were deleted here in file 0, this is "changed".
  227.            Otherwise it is "inserted".  */
  228.         prefix = (next->deleted > 0 ? "!" : "+");
  229.  
  230.       print_1_line (prefix, &files[1].linbuf[i]);
  231.     }
  232.     }
  233. }
  234.  
  235. /* Print a pair of line numbers with a comma, translated for file FILE.
  236.    If the second number is smaller, use the first in place of it.
  237.    If the numbers are equal, print just one number.
  238.  
  239.    Args A and B are internal line numbers.
  240.    We print the translated (real) line numbers.  */
  241.  
  242. static void
  243. print_unidiff_number_range (file, a, b)
  244.      struct file_data const *file;
  245.      int a, b;
  246. {
  247.   int trans_a, trans_b;
  248.   translate_range (file, a, b, &trans_a, &trans_b);
  249.  
  250.   /* Note: we can have B < A in the case of a range of no lines.
  251.      In this case, we should print the line number before the range,
  252.      which is B.  */
  253.   if (trans_b <= trans_a)
  254.     fprintf (outfile, trans_b == trans_a ? "%d" : "%d,0", trans_b);
  255.   else
  256.     fprintf (outfile, "%d,%d", trans_a, trans_b - trans_a + 1);
  257. }
  258.  
  259. /* Print a portion of an edit script in unidiff format.
  260.    HUNK is the beginning of the portion to be printed.
  261.    The end is marked by a `link' that has been nulled out.
  262.  
  263.    Prints out lines from both files, and precedes each
  264.    line with the appropriate flag-character.  */
  265.  
  266. static void
  267. pr_unidiff_hunk (hunk)
  268.      struct change *hunk;
  269. {
  270.   int first0, last0, first1, last1, show_from, show_to, i, j, k;
  271.   struct change *next;
  272.   char const *function;
  273.   FILE *out;
  274.  
  275.   /* Determine range of line numbers involved in each file.  */
  276.  
  277.   analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
  278.  
  279.   if (!show_from && !show_to)
  280.     return;
  281.  
  282.   /* Include a context's width before and after.  */
  283.  
  284.   i = - files[0].prefix_lines;
  285.   first0 = max (first0 - context, i);
  286.   first1 = max (first1 - context, i);
  287.   last0 = min (last0 + context, files[0].valid_lines - 1);
  288.   last1 = min (last1 + context, files[1].valid_lines - 1);
  289.  
  290.   /* If desired, find the preceding function definition line in file 0.  */
  291.   function = 0;
  292.   if (function_regexp.fastmap)
  293.     function = find_function (files[0].linbuf, first0);
  294.  
  295.   begin_output ();
  296.   out = outfile;
  297.  
  298.   fprintf (out, "@@ -");
  299.   print_unidiff_number_range (&files[0], first0, last0);
  300.   fprintf (out, " +");
  301.   print_unidiff_number_range (&files[1], first1, last1);
  302.   fprintf (out, " @@");
  303.  
  304.   /* If we looked for and found a function this is part of,
  305.      include its name in the header of the diff section.  */
  306.  
  307.   if (function)
  308.     {
  309.       putc (' ', out);
  310.       for (i = 0;  i < 40 && function[i] != '\n';  i++)
  311.     continue;
  312.       fwrite (function, 1, i, out);
  313.     }
  314.   putc ('\n', out);
  315.  
  316.   next = hunk;
  317.   i = first0;
  318.   j = first1;
  319.  
  320.   while (i <= last0 || j <= last1)
  321.     {
  322.  
  323.       /* If the line isn't a difference, output the context from file 0. */
  324.  
  325.       if (!next || i < next->line0)
  326.     {
  327.       putc (tab_align_flag ? '\t' : ' ', out);
  328.       print_1_line (0, &files[0].linbuf[i++]);
  329.       j++;
  330.     }
  331.       else
  332.     {
  333.       /* For each difference, first output the deleted part. */
  334.  
  335.       k = next->deleted;
  336.       while (k--)
  337.         {
  338.           putc ('-', out);
  339.           if (tab_align_flag)
  340.         putc ('\t', out);
  341.           print_1_line (0, &files[0].linbuf[i++]);
  342.         }
  343.  
  344.       /* Then output the inserted part. */
  345.  
  346.       k = next->inserted;
  347.       while (k--)
  348.         {
  349.           putc ('+', out);
  350.           if (tab_align_flag)
  351.         putc ('\t', out);
  352.           print_1_line (0, &files[1].linbuf[j++]);
  353.         }
  354.  
  355.       /* We're done with this hunk, so on to the next! */
  356.  
  357.       next = next->link;
  358.     }
  359.     }
  360. }
  361.  
  362. /* Scan a (forward-ordered) edit script for the first place that more than
  363.    2*CONTEXT unchanged lines appear, and return a pointer
  364.    to the `struct change' for the last change before those lines.  */
  365.  
  366. static struct change *
  367. find_hunk (start)
  368.      struct change *start;
  369. {
  370.   struct change *prev;
  371.   int top0, top1;
  372.   int thresh;
  373.  
  374.   do
  375.     {
  376.       /* Compute number of first line in each file beyond this changed.  */
  377.       top0 = start->line0 + start->deleted;
  378.       top1 = start->line1 + start->inserted;
  379.       prev = start;
  380.       start = start->link;
  381.       /* Threshold distance is 2*CONTEXT between two non-ignorable changes,
  382.      but only CONTEXT if one is ignorable.  */
  383.       thresh = ((prev->ignore || (start && start->ignore))
  384.         ? context
  385.         : 2 * context + 1);
  386.       /* It is not supposed to matter which file we check in the end-test.
  387.      If it would matter, crash.  */
  388.       if (start && start->line0 - top0 != start->line1 - top1)
  389.     abort ();
  390.     } while (start
  391.          /* Keep going if less than THRESH lines
  392.         elapse before the affected line.  */
  393.          && start->line0 < top0 + thresh);
  394.  
  395.   return prev;
  396. }
  397.  
  398. /* Set the `ignore' flag properly in each change in SCRIPT.
  399.    It should be 1 if all the lines inserted or deleted in that change
  400.    are ignorable lines.  */
  401.  
  402. static void
  403. mark_ignorable (script)
  404.      struct change *script;
  405. {
  406.   while (script)
  407.     {
  408.       struct change *next = script->link;
  409.       int first0, last0, first1, last1, deletes, inserts;
  410.  
  411.       /* Turn this change into a hunk: detach it from the others.  */
  412.       script->link = 0;
  413.  
  414.       /* Determine whether this change is ignorable.  */
  415.       analyze_hunk (script, &first0, &last0, &first1, &last1, &deletes, &inserts);
  416.       /* Reconnect the chain as before.  */
  417.       script->link = next;
  418.  
  419.       /* If the change is ignorable, mark it.  */
  420.       script->ignore = (!deletes && !inserts);
  421.  
  422.       /* Advance to the following change.  */
  423.       script = next;
  424.     }
  425. }
  426.  
  427. /* Find the last function-header line in LINBUF prior to line number LINENUM.
  428.    This is a line containing a match for the regexp in `function_regexp'.
  429.    Return the address of the text, or 0 if no function-header is found.  */
  430.  
  431. static char const *
  432. find_function (linbuf, linenum)
  433.      char const * const *linbuf;
  434.      int linenum;
  435. {
  436.   int i = linenum;
  437.   int last = find_function_last_search;
  438.   find_function_last_search = i;
  439.  
  440.   while (last <= --i)
  441.     {
  442.       /* See if this line is what we want.  */
  443.       char const *line = linbuf[i];
  444.       int len = linbuf[i + 1] - line - 1;
  445.  
  446.       if (0 <= re_search (&function_regexp, line, len, 0, len, 0))
  447.     {
  448.       find_function_last_match = i;
  449.       return line;
  450.     }
  451.     }
  452.   /* If we search back to where we started searching the previous time,
  453.      find the line we found last time.  */
  454.   if (find_function_last_match != INT_MAX)
  455.     return linbuf[find_function_last_match];
  456.  
  457.   return 0;
  458. }
  459.