side.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* sdiff-format output routines for GNU DIFF.
  2. Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  3. This file is part of GNU DIFF.
  4. GNU DIFF is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU DIFF General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU DIFF, but only under the conditions described in the
  12. GNU DIFF General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU DIFF so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. #include "diff.h"
  18. static unsigned print_half_line PARAMS((char const * const *, unsigned, unsigned));
  19. static unsigned tab_from_to PARAMS((unsigned, unsigned));
  20. static void print_1sdiff_line PARAMS((char const * const *, int, char const * const *));
  21. static void print_sdiff_common_lines PARAMS((int, int));
  22. static void print_sdiff_hunk PARAMS((struct change *));
  23. /* Next line number to be printed in the two input files. */
  24. static int next0, next1;
  25. /* Print the edit-script SCRIPT as a sdiff style output. */
  26. void
  27. print_sdiff_script (script)
  28. struct change *script;
  29. {
  30. begin_output ();
  31. next0 = next1 = - files[0].prefix_lines;
  32. print_script (script, find_change, print_sdiff_hunk);
  33. print_sdiff_common_lines (files[0].valid_lines, files[1].valid_lines);
  34. }
  35. /* Tab from column FROM to column TO, where FROM <= TO. Yield TO. */
  36. static unsigned
  37. tab_from_to (from, to)
  38. unsigned from, to;
  39. {
  40. FILE *out = outfile;
  41. unsigned tab;
  42. if (! tab_expand_flag)
  43. for (tab = from + TAB_WIDTH - from % TAB_WIDTH; tab <= to; tab += TAB_WIDTH)
  44. {
  45. putc ('\t', out);
  46. from = tab;
  47. }
  48. while (from++ < to)
  49. putc (' ', out);
  50. return to;
  51. }
  52. /*
  53. * Print the text for half an sdiff line. This means truncate to width
  54. * observing tabs, and trim a trailing newline. Returns the last column
  55. * written (not the number of chars).
  56. */
  57. static unsigned
  58. print_half_line (line, indent, out_bound)
  59. char const * const *line;
  60. unsigned indent, out_bound;
  61. {
  62. FILE *out = outfile;
  63. register unsigned in_position = 0, out_position = 0;
  64. register char const
  65. *text_pointer = line[0],
  66. *text_limit = line[1];
  67. while (text_pointer < text_limit)
  68. {
  69. register unsigned char c = *text_pointer++;
  70. switch (c)
  71. {
  72. case '\t':
  73. {
  74. unsigned spaces = TAB_WIDTH - in_position % TAB_WIDTH;
  75. if (in_position == out_position)
  76. {
  77. unsigned tabstop = out_position + spaces;
  78. if (tab_expand_flag)
  79. {
  80. if (out_bound < tabstop)
  81. tabstop = out_bound;
  82. for (; out_position < tabstop; out_position++)
  83. putc (' ', out);
  84. }
  85. else
  86. if (tabstop < out_bound)
  87. {
  88. out_position = tabstop;
  89. putc (c, out);
  90. }
  91. }
  92. in_position += spaces;
  93. }
  94. break;
  95. case '\r':
  96. {
  97. putc (c, out);
  98. tab_from_to (0, indent);
  99. in_position = out_position = 0;
  100. }
  101. break;
  102. case '\b':
  103. if (in_position != 0 && --in_position < out_bound)
  104. if (out_position <= in_position)
  105. /* Add spaces to make up for suppressed tab past out_bound. */
  106. for (; out_position < in_position; out_position++)
  107. putc (' ', out);
  108. else
  109. {
  110. out_position = in_position;
  111. putc (c, out);
  112. }
  113. break;
  114. case '\f':
  115. case '\v':
  116. control_char:
  117. if (in_position < out_bound)
  118. putc (c, out);
  119. break;
  120. default:
  121. if (! ISPRINT (c))
  122. goto control_char;
  123. /* falls through */
  124. case ' ':
  125. if (in_position++ < out_bound)
  126. {
  127. out_position = in_position;
  128. putc (c, out);
  129. }
  130. break;
  131. case '\n':
  132. return out_position;
  133. }
  134. }
  135. return out_position;
  136. }
  137. /*
  138. * Print side by side lines with a separator in the middle.
  139. * 0 parameters are taken to indicate white space text.
  140. * Blank lines that can easily be caught are reduced to a single newline.
  141. */
  142. static void
  143. print_1sdiff_line (left, sep, right)
  144. char const * const *left;
  145. int sep;
  146. char const * const *right;
  147. {
  148. FILE *out = outfile;
  149. unsigned hw = sdiff_half_width, c2o = sdiff_column2_offset;
  150. unsigned col = 0;
  151. int put_newline = 0;
  152. if (left)
  153. {
  154. if (left[1][-1] == '\n')
  155. put_newline = 1;
  156. col = print_half_line (left, 0, hw);
  157. }
  158. if (sep != ' ')
  159. {
  160. col = tab_from_to (col, (hw + c2o - 1) / 2) + 1;
  161. if (sep == '|' && put_newline != (right[1][-1] == '\n'))
  162. sep = put_newline ? '/' : '\\';
  163. putc (sep, out);
  164. }
  165. if (right)
  166. {
  167. if (right[1][-1] == '\n')
  168. put_newline = 1;
  169. if (**right != '\n')
  170. {
  171. col = tab_from_to (col, c2o);
  172. print_half_line (right, col, hw);
  173. }
  174. }
  175. if (put_newline)
  176. putc ('\n', out);
  177. }
  178. /* Print lines common to both files in side-by-side format. */
  179. static void
  180. print_sdiff_common_lines (limit0, limit1)
  181. int limit0, limit1;
  182. {
  183. int i0 = next0, i1 = next1;
  184. if (! sdiff_skip_common_lines && (i0 != limit0 || i1 != limit1))
  185. {
  186. if (sdiff_help_sdiff)
  187. fprintf (outfile, "i%d,%d\n", limit0 - i0, limit1 - i1);
  188. if (! sdiff_left_only)
  189. {
  190. while (i0 != limit0 && i1 != limit1)
  191. print_1sdiff_line (&files[0].linbuf[i0++], ' ', &files[1].linbuf[i1++]);
  192. while (i1 != limit1)
  193. print_1sdiff_line (0, ')', &files[1].linbuf[i1++]);
  194. }
  195. while (i0 != limit0)
  196. print_1sdiff_line (&files[0].linbuf[i0++], '(', 0);
  197. }
  198. next0 = limit0;
  199. next1 = limit1;
  200. }
  201. /* Print a hunk of an sdiff diff.
  202. This is a contiguous portion of a complete edit script,
  203. describing changes in consecutive lines. */
  204. static void
  205. print_sdiff_hunk (hunk)
  206. struct change *hunk;
  207. {
  208. int first0, last0, first1, last1, deletes, inserts;
  209. register int i, j;
  210. /* Determine range of line numbers involved in each file. */
  211. analyze_hunk (hunk, &first0, &last0, &first1, &last1, &deletes, &inserts);
  212. if (!deletes && !inserts)
  213. return;
  214. /* Print out lines up to this change. */
  215. print_sdiff_common_lines (first0, first1);
  216. if (sdiff_help_sdiff)
  217. fprintf (outfile, "c%d,%d\n", last0 - first0 + 1, last1 - first1 + 1);
  218. /* Print ``xxx | xxx '' lines */
  219. if (inserts && deletes)
  220. {
  221. for (i = first0, j = first1; i <= last0 && j <= last1; ++i, ++j)
  222. print_1sdiff_line (&files[0].linbuf[i], '|', &files[1].linbuf[j]);
  223. deletes = i <= last0;
  224. inserts = j <= last1;
  225. next0 = first0 = i;
  226. next1 = first1 = j;
  227. }
  228. /* Print `` > xxx '' lines */
  229. if (inserts)
  230. {
  231. for (j = first1; j <= last1; ++j)
  232. print_1sdiff_line (0, '>', &files[1].linbuf[j]);
  233. next1 = j;
  234. }
  235. /* Print ``xxx < '' lines */
  236. if (deletes)
  237. {
  238. for (i = first0; i <= last0; ++i)
  239. print_1sdiff_line (&files[0].linbuf[i], '<', 0);
  240. next0 = i;
  241. }
  242. }