/contrib/cvs/diff/ed.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 198 lines · 120 code · 33 blank · 45 comment · 19 complexity · 16220baaa4e21fd3e948c29f17889581 MD5 · raw file

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