/contrib/cvs/diff/side.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 294 lines · 209 code · 40 blank · 45 comment · 61 complexity · 7921ee357480e8de6cb5f9aec5c15fc4 MD5 · raw file

  1. /* sdiff-format output routines for GNU DIFF.
  2. Copyright (C) 1991, 1992, 1993, 1998 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. unsigned tab;
  41. if (! tab_expand_flag)
  42. for (tab = from + TAB_WIDTH - from % TAB_WIDTH; tab <= to; tab += TAB_WIDTH)
  43. {
  44. write_output ("\t", 1);
  45. from = tab;
  46. }
  47. while (from++ < to)
  48. write_output (" ", 1);
  49. return to;
  50. }
  51. /*
  52. * Print the text for half an sdiff line. This means truncate to width
  53. * observing tabs, and trim a trailing newline. Returns the last column
  54. * written (not the number of chars).
  55. */
  56. static unsigned
  57. print_half_line (line, indent, out_bound)
  58. char const * const *line;
  59. unsigned indent, out_bound;
  60. {
  61. register unsigned in_position = 0, out_position = 0;
  62. register char const
  63. *text_pointer = line[0],
  64. *text_limit = line[1];
  65. while (text_pointer < text_limit)
  66. {
  67. register unsigned char c = *text_pointer++;
  68. /* We use CC to avoid taking the address of the register
  69. variable C. */
  70. char cc;
  71. switch (c)
  72. {
  73. case '\t':
  74. {
  75. unsigned spaces = TAB_WIDTH - in_position % TAB_WIDTH;
  76. if (in_position == out_position)
  77. {
  78. unsigned tabstop = out_position + spaces;
  79. if (tab_expand_flag)
  80. {
  81. if (out_bound < tabstop)
  82. tabstop = out_bound;
  83. for (; out_position < tabstop; out_position++)
  84. write_output (" ", 1);
  85. }
  86. else
  87. if (tabstop < out_bound)
  88. {
  89. out_position = tabstop;
  90. cc = c;
  91. write_output (&cc, 1);
  92. }
  93. }
  94. in_position += spaces;
  95. }
  96. break;
  97. case '\r':
  98. {
  99. cc = c;
  100. write_output (&cc, 1);
  101. tab_from_to (0, indent);
  102. in_position = out_position = 0;
  103. }
  104. break;
  105. case '\b':
  106. if (in_position != 0 && --in_position < out_bound)
  107. if (out_position <= in_position)
  108. /* Add spaces to make up for suppressed tab past out_bound. */
  109. for (; out_position < in_position; out_position++)
  110. write_output (" ", 1);
  111. else
  112. {
  113. out_position = in_position;
  114. cc = c;
  115. write_output (&cc, 1);
  116. }
  117. break;
  118. case '\f':
  119. case '\v':
  120. control_char:
  121. if (in_position < out_bound)
  122. {
  123. cc = c;
  124. write_output (&cc, 1);
  125. }
  126. break;
  127. default:
  128. if (! ISPRINT (c))
  129. goto control_char;
  130. /* falls through */
  131. case ' ':
  132. if (in_position++ < out_bound)
  133. {
  134. out_position = in_position;
  135. cc = c;
  136. write_output (&cc, 1);
  137. }
  138. break;
  139. case '\n':
  140. return out_position;
  141. }
  142. }
  143. return out_position;
  144. }
  145. /*
  146. * Print side by side lines with a separator in the middle.
  147. * 0 parameters are taken to indicate white space text.
  148. * Blank lines that can easily be caught are reduced to a single newline.
  149. */
  150. static void
  151. print_1sdiff_line (left, sep, right)
  152. char const * const *left;
  153. int sep;
  154. char const * const *right;
  155. {
  156. unsigned hw = sdiff_half_width, c2o = sdiff_column2_offset;
  157. unsigned col = 0;
  158. int put_newline = 0;
  159. if (left)
  160. {
  161. if (left[1][-1] == '\n')
  162. put_newline = 1;
  163. col = print_half_line (left, 0, hw);
  164. }
  165. if (sep != ' ')
  166. {
  167. char cc;
  168. col = tab_from_to (col, (hw + c2o - 1) / 2) + 1;
  169. if (sep == '|' && put_newline != (right[1][-1] == '\n'))
  170. sep = put_newline ? '/' : '\\';
  171. cc = sep;
  172. write_output (&cc, 1);
  173. }
  174. if (right)
  175. {
  176. if (right[1][-1] == '\n')
  177. put_newline = 1;
  178. if (**right != '\n')
  179. {
  180. col = tab_from_to (col, c2o);
  181. print_half_line (right, col, hw);
  182. }
  183. }
  184. if (put_newline)
  185. write_output ("\n", 1);
  186. }
  187. /* Print lines common to both files in side-by-side format. */
  188. static void
  189. print_sdiff_common_lines (limit0, limit1)
  190. int limit0, limit1;
  191. {
  192. int i0 = next0, i1 = next1;
  193. if (! sdiff_skip_common_lines && (i0 != limit0 || i1 != limit1))
  194. {
  195. if (sdiff_help_sdiff)
  196. printf_output ("i%d,%d\n", limit0 - i0, limit1 - i1);
  197. if (! sdiff_left_only)
  198. {
  199. while (i0 != limit0 && i1 != limit1)
  200. print_1sdiff_line (&files[0].linbuf[i0++], ' ', &files[1].linbuf[i1++]);
  201. while (i1 != limit1)
  202. print_1sdiff_line (0, ')', &files[1].linbuf[i1++]);
  203. }
  204. while (i0 != limit0)
  205. print_1sdiff_line (&files[0].linbuf[i0++], '(', 0);
  206. }
  207. next0 = limit0;
  208. next1 = limit1;
  209. }
  210. /* Print a hunk of an sdiff diff.
  211. This is a contiguous portion of a complete edit script,
  212. describing changes in consecutive lines. */
  213. static void
  214. print_sdiff_hunk (hunk)
  215. struct change *hunk;
  216. {
  217. int first0, last0, first1, last1, deletes, inserts;
  218. register int i, j;
  219. /* Determine range of line numbers involved in each file. */
  220. analyze_hunk (hunk, &first0, &last0, &first1, &last1, &deletes, &inserts);
  221. if (!deletes && !inserts)
  222. return;
  223. /* Print out lines up to this change. */
  224. print_sdiff_common_lines (first0, first1);
  225. if (sdiff_help_sdiff)
  226. printf_output ("c%d,%d\n", last0 - first0 + 1, last1 - first1 + 1);
  227. /* Print ``xxx | xxx '' lines */
  228. if (inserts && deletes)
  229. {
  230. for (i = first0, j = first1; i <= last0 && j <= last1; ++i, ++j)
  231. print_1sdiff_line (&files[0].linbuf[i], '|', &files[1].linbuf[j]);
  232. deletes = i <= last0;
  233. inserts = j <= last1;
  234. next0 = first0 = i;
  235. next1 = first1 = j;
  236. }
  237. /* Print `` > xxx '' lines */
  238. if (inserts)
  239. {
  240. for (j = first1; j <= last1; ++j)
  241. print_1sdiff_line (0, '>', &files[1].linbuf[j]);
  242. next1 = j;
  243. }
  244. /* Print ``xxx < '' lines */
  245. if (deletes)
  246. {
  247. for (i = first0; i <= last0; ++i)
  248. print_1sdiff_line (&files[0].linbuf[i], '<', 0);
  249. next0 = i;
  250. }
  251. }