home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / utils / hwgrcs / hwgdiff / rcs.rcsfiles / ed.c,v < prev    next >
Encoding:
Text File  |  1993-02-20  |  5.4 KB  |  224 lines

  1. head    1.1;
  2. access;
  3. symbols
  4.     HWGDIFF_Fish:1.1
  5.     HWGDIFF:1.1;
  6. locks; strict;
  7. comment    @ * @;
  8.  
  9.  
  10. 1.1
  11. date    93.01.19.14.04.33;    author heinz;    state Exp;
  12. branches;
  13. next    ;
  14.  
  15.  
  16. desc
  17. @RCS for the first time ...
  18. @
  19.  
  20.  
  21. 1.1
  22. log
  23. @Initial revision
  24. @
  25. text
  26. @/* Output routines for ed-script format.
  27.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  28.  
  29. This file is part of GNU DIFF.
  30.  
  31. GNU DIFF is free software; you can redistribute it and/or modify
  32. it under the terms of the GNU General Public License as published by
  33. the Free Software Foundation; either version 1, or (at your option)
  34. any later version.
  35.  
  36. GNU DIFF is distributed in the hope that it will be useful,
  37. but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  39. GNU General Public License for more details.
  40.  
  41. You should have received a copy of the GNU General Public License
  42. along with GNU DIFF; see the file COPYING.  If not, write to
  43. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  44.  
  45. #include "diff.h"
  46.  
  47. static void print_rcs_hunk ();
  48. static void print_ed_hunk ();
  49. static void pr_forward_ed_hunk ();
  50. void translate_range ();
  51. struct change *find_change ();
  52. struct change *find_reverse_change ();
  53.  
  54. /* Print our script as ed commands.  */
  55.  
  56. void
  57. print_ed_script (script)
  58.     struct change *script;
  59. {
  60.   print_script (script, find_reverse_change, print_ed_hunk);
  61. }
  62.  
  63. /* Print a hunk of an ed diff */
  64.  
  65. static void
  66. print_ed_hunk (hunk)
  67.      struct change *hunk; 
  68. {
  69.   int f0, l0, f1, l1;
  70.   int deletes, inserts;
  71.  
  72. #if 0
  73.   hunk = flip_script (hunk);
  74. #endif
  75. #ifdef DEBUG
  76.   debug_script (hunk);
  77. #endif
  78.  
  79.   /* Determine range of line numbers involved in each file.  */
  80.   analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
  81.   if (!deletes && !inserts)
  82.     return;
  83.  
  84.   /* Print out the line number header for this hunk */
  85.   print_number_range (',', &files[0], f0, l0);
  86.   fprintf (outfile, "%c\n", change_letter (inserts, deletes));
  87.  
  88.   /* Print new/changed lines from second file, if needed */
  89.   if (inserts)
  90.     {
  91.       int i;
  92.       int inserting = 1;
  93.       for (i = f1; i <= l1; i++)
  94.     {
  95.       /* Resume the insert, if we stopped.  */
  96.       if (! inserting)
  97.         fprintf (outfile, "%da\n",
  98.              i - f1 + translate_line_number (&files[0], f0) - 1);
  99.       inserting = 1;
  100.  
  101.       /* If the file's line is just a dot, it would confuse `ed'.
  102.          So output it with a double dot, and set the flag LEADING_DOT
  103.          so that we will output another ed-command later
  104.          to change the double dot into a single dot.  */
  105.  
  106.       if (files[1].linbuf[i].text[0] == '.'
  107.           && files[1].linbuf[i].text[1] == '\n')
  108.         {
  109.           fprintf (outfile, "..\n");
  110.           fprintf (outfile, ".\n");
  111.           /* Now change that double dot to the desired single dot.  */
  112.           fprintf (outfile, "%ds/^\\.\\././\n",
  113.                i - f1 + translate_line_number (&files[0], f0));
  114.           inserting = 0;
  115.         }
  116.       else
  117.         /* Line is not `.', so output it unmodified.  */
  118.         print_1_line ("", &files[1].linbuf[i]);
  119.     }
  120.  
  121.       /* End insert mode, if we are still in it.  */
  122.       if (inserting)
  123.     fprintf (outfile, ".\n");
  124.     }
  125. }
  126.  
  127. /* Print change script in the style of ed commands,
  128.    but print the changes in the order they appear in the input files,
  129.    which means that the commands are not truly useful with ed.  */
  130.  
  131. void
  132. pr_forward_ed_script (script)
  133.      struct change *script;
  134. {
  135.   print_script (script, find_change, pr_forward_ed_hunk);
  136. }
  137.  
  138. static void
  139. pr_forward_ed_hunk (hunk)
  140.      struct change *hunk;
  141. {
  142.   int i;
  143.   int f0, l0, f1, l1;
  144.   int deletes, inserts;
  145.  
  146.   /* Determine range of line numbers involved in each file.  */
  147.   analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
  148.   if (!deletes && !inserts)
  149.     return;
  150.  
  151.   fprintf (outfile, "%c", change_letter (inserts, deletes));
  152.   print_number_range (' ', files, f0, l0);
  153.   fprintf (outfile, "\n");
  154.  
  155.   /* If deletion only, print just the number range.  */
  156.  
  157.   if (!inserts)
  158.     return;
  159.  
  160.   /* For insertion (with or without deletion), print the number range
  161.      and the lines from file 2.  */
  162.  
  163.   for (i = f1; i <= l1; i++)
  164.     print_1_line ("", &files[1].linbuf[i]);
  165.  
  166.   fprintf (outfile, ".\n");
  167. }
  168.  
  169. /* Print in a format somewhat like ed commands
  170.    except that each insert command states the number of lines it inserts.
  171.    This format is used for RCS.  */
  172.  
  173. void
  174. print_rcs_script (script)
  175.      struct change *script;
  176. {
  177.   print_script (script, find_change, print_rcs_hunk);
  178. }
  179.  
  180. /* Print a hunk of an RCS diff */
  181.  
  182. static void
  183. print_rcs_hunk (hunk)
  184.      struct change *hunk;
  185. {
  186.   int i;
  187.   int f0, l0, f1, l1;
  188.   int deletes, inserts;
  189.   int tf0, tl0, tf1, tl1;
  190.  
  191.   /* Determine range of line numbers involved in each file.  */
  192.   analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
  193.   if (!deletes && !inserts)
  194.     return;
  195.  
  196.   translate_range (&files[0], f0, l0, &tf0, &tl0);
  197.  
  198.   if (deletes)
  199.     {
  200.       fprintf (outfile, "d");
  201.       /* For deletion, print just the starting line number from file 0
  202.      and the number of lines deleted.  */
  203.       fprintf (outfile, "%d %d\n",
  204.            tf0,
  205.            (tl0 >= tf0 ? tl0 - tf0 + 1 : 1));         
  206.     }
  207.  
  208.   if (inserts)
  209.     {
  210.       fprintf (outfile, "a");
  211.  
  212.       /* Take last-line-number from file 0 and # lines from file 1.  */
  213.       translate_range (&files[1], f1, l1, &tf1, &tl1);
  214.       fprintf (outfile, "%d %d\n",
  215.            tl0,
  216.            (tl1 >= tf1 ? tl1 - tf1 + 1 : 1));         
  217.  
  218.       /* Print the inserted lines.  */
  219.       for (i = f1; i <= l1; i++)
  220.     print_1_line ("", &files[1].linbuf[i]);
  221.     }
  222. }
  223. @
  224.