home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / rcs57pc3.zip / diff16 / context.c < prev    next >
C/C++ Source or Header  |  1994-06-25  |  14KB  |  482 lines

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