1 /* sdiff-format output routines for GNU DIFF.
2 Copyright (C) 1991, 1992, 1993, 1998 Free Software Foundation, Inc.
4 This file is part of GNU DIFF.
6 GNU DIFF is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY. No author or distributor
8 accepts responsibility to anyone for the consequences of using it
9 or for whether it serves any particular purpose or works at all,
10 unless he says so in writing. Refer to the GNU DIFF General Public
11 License for full details.
13 Everyone is granted permission to copy, modify and redistribute
14 GNU DIFF, but only under the conditions described in the
15 GNU DIFF General Public License. A copy of this license is
16 supposed to have been given to you along with GNU DIFF so you
17 can know your rights and responsibilities. It should be in a
18 file named COPYING. Among other things, the copyright notice
19 and this notice must be preserved on all copies. */
24 static unsigned print_half_line PARAMS((char const * const *, unsigned, unsigned));
25 static unsigned tab_from_to PARAMS((unsigned, unsigned));
26 static void print_1sdiff_line PARAMS((char const * const *, int, char const * const *));
27 static void print_sdiff_common_lines PARAMS((int, int));
28 static void print_sdiff_hunk PARAMS((struct change *));
30 /* Next line number to be printed in the two input files. */
31 static int next0, next1;
33 /* Print the edit-script SCRIPT as a sdiff style output. */
36 print_sdiff_script (script)
37 struct change *script;
41 next0 = next1 = - files[0].prefix_lines;
42 print_script (script, find_change, print_sdiff_hunk);
44 print_sdiff_common_lines (files[0].valid_lines, files[1].valid_lines);
47 /* Tab from column FROM to column TO, where FROM <= TO. Yield TO. */
50 tab_from_to (from, to)
55 if (! tab_expand_flag)
56 for (tab = from + TAB_WIDTH - from % TAB_WIDTH; tab <= to; tab += TAB_WIDTH)
58 write_output ("\t", 1);
62 write_output (" ", 1);
67 * Print the text for half an sdiff line. This means truncate to width
68 * observing tabs, and trim a trailing newline. Returns the last column
69 * written (not the number of chars).
72 print_half_line (line, indent, out_bound)
73 char const * const *line;
74 unsigned indent, out_bound;
76 register unsigned in_position = 0, out_position = 0;
78 *text_pointer = line[0],
79 *text_limit = line[1];
81 while (text_pointer < text_limit)
83 register unsigned char c = *text_pointer++;
84 /* We use CC to avoid taking the address of the register
92 unsigned spaces = TAB_WIDTH - in_position % TAB_WIDTH;
93 if (in_position == out_position)
95 unsigned tabstop = out_position + spaces;
98 if (out_bound < tabstop)
100 for (; out_position < tabstop; out_position++)
101 write_output (" ", 1);
104 if (tabstop < out_bound)
106 out_position = tabstop;
108 write_output (&cc, 1);
111 in_position += spaces;
118 write_output (&cc, 1);
119 tab_from_to (0, indent);
120 in_position = out_position = 0;
125 if (in_position != 0 && --in_position < out_bound)
126 if (out_position <= in_position)
127 /* Add spaces to make up for suppressed tab past out_bound. */
128 for (; out_position < in_position; out_position++)
129 write_output (" ", 1);
132 out_position = in_position;
134 write_output (&cc, 1);
141 if (in_position < out_bound)
144 write_output (&cc, 1);
153 if (in_position++ < out_bound)
155 out_position = in_position;
157 write_output (&cc, 1);
170 * Print side by side lines with a separator in the middle.
171 * 0 parameters are taken to indicate white space text.
172 * Blank lines that can easily be caught are reduced to a single newline.
176 print_1sdiff_line (left, sep, right)
177 char const * const *left;
179 char const * const *right;
181 unsigned hw = sdiff_half_width, c2o = sdiff_column2_offset;
187 if (left[1][-1] == '\n')
189 col = print_half_line (left, 0, hw);
196 col = tab_from_to (col, (hw + c2o - 1) / 2) + 1;
197 if (sep == '|' && put_newline != (right[1][-1] == '\n'))
198 sep = put_newline ? '/' : '\\';
200 write_output (&cc, 1);
205 if (right[1][-1] == '\n')
209 col = tab_from_to (col, c2o);
210 print_half_line (right, col, hw);
215 write_output ("\n", 1);
218 /* Print lines common to both files in side-by-side format. */
220 print_sdiff_common_lines (limit0, limit1)
223 int i0 = next0, i1 = next1;
225 if (! sdiff_skip_common_lines && (i0 != limit0 || i1 != limit1))
227 if (sdiff_help_sdiff)
228 printf_output ("i%d,%d\n", limit0 - i0, limit1 - i1);
230 if (! sdiff_left_only)
232 while (i0 != limit0 && i1 != limit1)
233 print_1sdiff_line (&files[0].linbuf[i0++], ' ', &files[1].linbuf[i1++]);
235 print_1sdiff_line (0, ')', &files[1].linbuf[i1++]);
238 print_1sdiff_line (&files[0].linbuf[i0++], '(', 0);
245 /* Print a hunk of an sdiff diff.
246 This is a contiguous portion of a complete edit script,
247 describing changes in consecutive lines. */
250 print_sdiff_hunk (hunk)
253 int first0, last0, first1, last1, deletes, inserts;
256 /* Determine range of line numbers involved in each file. */
257 analyze_hunk (hunk, &first0, &last0, &first1, &last1, &deletes, &inserts);
258 if (!deletes && !inserts)
261 /* Print out lines up to this change. */
262 print_sdiff_common_lines (first0, first1);
264 if (sdiff_help_sdiff)
265 printf_output ("c%d,%d\n", last0 - first0 + 1, last1 - first1 + 1);
267 /* Print ``xxx | xxx '' lines */
268 if (inserts && deletes)
270 for (i = first0, j = first1; i <= last0 && j <= last1; ++i, ++j)
271 print_1sdiff_line (&files[0].linbuf[i], '|', &files[1].linbuf[j]);
272 deletes = i <= last0;
273 inserts = j <= last1;
279 /* Print `` > xxx '' lines */
282 for (j = first1; j <= last1; ++j)
283 print_1sdiff_line (0, '>', &files[1].linbuf[j]);
287 /* Print ``xxx < '' lines */
290 for (i = first0; i <= last0; ++i)
291 print_1sdiff_line (&files[0].linbuf[i], '<', 0);