home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff281.lzh / Diff / ed.c < prev    next >
C/C++ Source or Header  |  1989-11-20  |  5KB  |  199 lines

  1. /* Output routines for ed-script format.
  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 print_rcs_hunk ();
  23. static void print_ed_hunk ();
  24. static void pr_forward_ed_hunk ();
  25. static void print_range_length ();
  26. void translate_range ();
  27. struct change *find_change ();
  28. struct change *find_reverse_change ();
  29.  
  30. /* Print our script as ed commands.  */
  31.  
  32. void
  33. print_ed_script (script)
  34.     struct change *script;
  35. {
  36.   print_script (script, find_reverse_change, print_ed_hunk);
  37. }
  38.  
  39. /* Print a hunk of an ed diff */
  40.  
  41. static void
  42. print_ed_hunk (hunk)
  43.      struct change *hunk; 
  44. {
  45.   int f0, l0, f1, l1;
  46.   int deletes, inserts;
  47.  
  48. #if 0
  49.   hunk = flip_script (hunk);
  50. #endif
  51. #ifdef MYDEBUG
  52.   debug_script (hunk);
  53. #endif
  54.  
  55.   /* Determine range of line numbers involved in each file.  */
  56.   analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
  57.   if (!deletes && !inserts)
  58.     return;
  59.  
  60.   /* Print out the line number header for this hunk */
  61.   print_number_range (',', &files[0], f0, l0);
  62.   fprintf (outfile, "%c\n", change_letter (inserts, deletes));
  63.  
  64.   /* Print new/changed lines from second file, if needed */
  65.   if (inserts)
  66.     {
  67.       int i;
  68.       int inserting = 1;
  69.       for (i = f1; i <= l1; i++)
  70.     {
  71.       /* Resume the insert, if we stopped.  */
  72.       if (! inserting)
  73.         fprintf (outfile, "%da\n",
  74.              i - f1 + translate_line_number (&files[0], f0) - 1);
  75.       inserting = 1;
  76.  
  77.       /* If the file's line is just a dot, it would confuse `ed'.
  78.          So output it with a double dot, and set the flag LEADING_DOT
  79.          so that we will output another ed-command later
  80.          to change the double dot into a single dot.  */
  81.  
  82.       if (files[1].linbuf[i].text[0] == '.'
  83.           && files[1].linbuf[i].text[1] == '\n')
  84.         {
  85.           fprintf (outfile, "..\n");
  86.           fprintf (outfile, ".\n");
  87.           /* Now change that double dot to the desired single dot.  */
  88.           fprintf (outfile, "%ds/^\\.\\././\n",
  89.                i - f1 + translate_line_number (&files[0], f0));
  90.           inserting = 0;
  91.         }
  92.       else
  93.         /* Line is not `.', so output it unmodified.  */
  94.         print_1_line ("", &files[1].linbuf[i]);
  95.     }
  96.  
  97.       /* End insert mode, if we are still in it.  */
  98.       if (inserting)
  99.     fprintf (outfile, ".\n");
  100.     }
  101. }
  102.  
  103. /* Print change script in the style of ed commands,
  104.    but print the changes in the order they appear in the input files,
  105.    which means that the commands are not truly useful with ed.  */
  106.  
  107. void
  108. pr_forward_ed_script (script)
  109.      struct change *script;
  110. {
  111.   print_script (script, find_change, pr_forward_ed_hunk);
  112. }
  113.  
  114. static void
  115. pr_forward_ed_hunk (hunk)
  116.      struct change *hunk;
  117. {
  118.   int i;
  119.   int f0, l0, f1, l1;
  120.   int deletes, inserts;
  121.  
  122.   /* Determine range of line numbers involved in each file.  */
  123.   analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
  124.   if (!deletes && !inserts)
  125.     return;
  126.  
  127.   fprintf (outfile, "%c", change_letter (inserts, deletes));
  128.   print_number_range (' ', files, f0, l0);
  129.   fprintf (outfile, "\n");
  130.  
  131.   /* If deletion only, print just the number range.  */
  132.  
  133.   if (!inserts)
  134.     return;
  135.  
  136.   /* For insertion (with or without deletion), print the number range
  137.      and the lines from file 2.  */
  138.  
  139.   for (i = f1; i <= l1; i++)
  140.     print_1_line ("", &files[1].linbuf[i]);
  141.  
  142.   fprintf (outfile, ".\n");
  143. }
  144.  
  145. /* Print in a format somewhat like ed commands
  146.    except that each insert command states the number of lines it inserts.
  147.    This format is used for RCS.  */
  148.  
  149. void
  150. print_rcs_script (script)
  151.      struct change *script;
  152. {
  153.   print_script (script, find_change, print_rcs_hunk);
  154. }
  155.  
  156. /* Print a hunk of an RCS diff */
  157.  
  158. static void
  159. print_rcs_hunk (hunk)
  160.      struct change *hunk;
  161. {
  162.   int i;
  163.   int f0, l0, f1, l1;
  164.   int deletes, inserts;
  165.   int tf0, tl0, tf1, tl1;
  166.  
  167.   /* Determine range of line numbers involved in each file.  */
  168.   analyze_hunk (hunk, &f0, &l0, &f1, &l1, &deletes, &inserts);
  169.   if (!deletes && !inserts)
  170.     return;
  171.  
  172.   translate_range (&files[0], f0, l0, &tf0, &tl0);
  173.  
  174.   if (deletes)
  175.     {
  176.       fprintf (outfile, "d");
  177.       /* For deletion, print just the starting line number from file 0
  178.      and the number of lines deleted.  */
  179.       fprintf (outfile, "%d %d\n",
  180.            tf0,
  181.            (tl0 >= tf0 ? tl0 - tf0 + 1 : 1));         
  182.     }
  183.  
  184.   if (inserts)
  185.     {
  186.       fprintf (outfile, "a");
  187.  
  188.       /* Take last-line-number from file 0 and # lines from file 1.  */
  189.       translate_range (&files[1], f1, l1, &tf1, &tl1);
  190.       fprintf (outfile, "%d %d\n",
  191.            tl0,
  192.            (tl1 >= tf1 ? tl1 - tf1 + 1 : 1));         
  193.  
  194.       /* Print the inserted lines.  */
  195.       for (i = f1; i <= l1; i++)
  196.     print_1_line ("", &files[1].linbuf[i]);
  197.     }
  198. }
  199.