home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / rcs567s.zip / diff / context.c < prev    next >
C/C++ Source or Header  |  1993-11-13  |  14KB  |  483 lines

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