ed.c 5.2 KB

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