1 /* sdiff-format output routines for GNU DIFF.
3 Copyright (C) 1991-1993, 1998, 2001-2002, 2004, 2009-2010 Free Software
6 This file is part of GNU DIFF.
8 GNU DIFF is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY. No author or distributor
10 accepts responsibility to anyone for the consequences of using it
11 or for whether it serves any particular purpose or works at all,
12 unless he says so in writing. Refer to the GNU General Public
13 License for full details.
15 Everyone is granted permission to copy, modify and redistribute
16 GNU DIFF, but only under the conditions described in the
17 GNU General Public License. A copy of this license is
18 supposed to have been given to you along with GNU DIFF so you
19 can know your rights and responsibilities. It should be in a
20 file named COPYING. Among other things, the copyright notice
21 and this notice must be preserved on all copies. */
27 static void print_sdiff_common_lines (lin, lin);
28 static void print_sdiff_hunk (struct change *);
30 /* Next line number to be printed in the two input files. */
31 static lin next0, next1;
33 /* Print the edit-script SCRIPT as a sdiff style output. */
36 print_sdiff_script (struct change *script)
40 next0 = next1 = - files[0].prefix_lines;
41 print_script (script, find_change, print_sdiff_hunk);
43 print_sdiff_common_lines (files[0].valid_lines, files[1].valid_lines);
46 /* Tab from column FROM to column TO, where FROM <= TO. Yield TO. */
49 tab_from_to (size_t from, size_t to)
53 size_t tab_size = tabsize;
56 for (tab = from + tab_size - from % tab_size; tab <= to; tab += tab_size)
66 /* Print the text for half an sdiff line. This means truncate to
67 width observing tabs, and trim a trailing newline. Return the
68 last column written (not the number of chars). */
71 print_half_line (char const *const *line, size_t indent, size_t out_bound)
74 register size_t in_position = 0;
75 register size_t out_position = 0;
76 register char const *text_pointer = line[0];
77 register char const *text_limit = line[1];
78 mbstate_t mbstate = { 0 };
80 while (text_pointer < text_limit)
82 char const *tp0 = text_pointer;
83 register char c = *text_pointer++;
89 size_t spaces = tabsize - in_position % tabsize;
90 if (in_position == out_position)
92 size_t tabstop = out_position + spaces;
95 if (out_bound < tabstop)
97 for (; out_position < tabstop; out_position++)
101 if (tabstop < out_bound)
103 out_position = tabstop;
107 in_position += spaces;
114 tab_from_to (0, indent);
115 in_position = out_position = 0;
120 if (in_position != 0 && --in_position < out_bound)
122 if (out_position <= in_position)
123 /* Add spaces to make up for suppressed tab past out_bound. */
124 for (; out_position < in_position; out_position++)
128 out_position = in_position;
137 size_t bytes = mbrtowc (&wc, tp0, text_limit - tp0, &mbstate);
139 if (0 < bytes && bytes < (size_t) -2)
141 int width = wcwidth (wc);
143 in_position += width;
144 if (in_position <= out_bound)
146 out_position = in_position;
147 fwrite (tp0, 1, bytes, stdout);
149 text_pointer = tp0 + bytes;
156 if (in_position < out_bound)
160 case ' ': case '!': case '"': case '#': case '%':
161 case '&': case '\'': case '(': case ')': case '*':
162 case '+': case ',': case '-': case '.': case '/':
163 case '0': case '1': case '2': case '3': case '4':
164 case '5': case '6': case '7': case '8': case '9':
165 case ':': case ';': case '<': case '=': case '>':
167 case 'A': case 'B': case 'C': case 'D': case 'E':
168 case 'F': case 'G': case 'H': case 'I': case 'J':
169 case 'K': case 'L': case 'M': case 'N': case 'O':
170 case 'P': case 'Q': case 'R': case 'S': case 'T':
171 case 'U': case 'V': case 'W': case 'X': case 'Y':
173 case '[': case '\\': case ']': case '^': case '_':
174 case 'a': case 'b': case 'c': case 'd': case 'e':
175 case 'f': case 'g': case 'h': case 'i': case 'j':
176 case 'k': case 'l': case 'm': case 'n': case 'o':
177 case 'p': case 'q': case 'r': case 's': case 't':
178 case 'u': case 'v': case 'w': case 'x': case 'y':
179 case 'z': case '{': case '|': case '}': case '~':
180 /* These characters are printable ASCII characters. */
181 if (in_position++ < out_bound)
183 out_position = in_position;
196 /* Print side by side lines with a separator in the middle.
197 0 parameters are taken to indicate white space text.
198 Blank lines that can easily be caught are reduced to a single newline. */
201 print_1sdiff_line (char const *const *left, char sep,
202 char const *const *right)
205 size_t hw = sdiff_half_width;
206 size_t c2o = sdiff_column2_offset;
208 bool put_newline = false;
212 put_newline |= left[1][-1] == '\n';
213 col = print_half_line (left, 0, hw);
218 col = tab_from_to (col, (hw + c2o - 1) / 2) + 1;
219 if (sep == '|' && put_newline != (right[1][-1] == '\n'))
220 sep = put_newline ? '/' : '\\';
226 put_newline |= right[1][-1] == '\n';
229 col = tab_from_to (col, c2o);
230 print_half_line (right, col, hw);
238 /* Print lines common to both files in side-by-side format. */
240 print_sdiff_common_lines (lin limit0, lin limit1)
242 lin i0 = next0, i1 = next1;
244 if (!suppress_common_lines && (i0 != limit0 || i1 != limit1))
246 if (sdiff_merge_assist)
248 long int len0 = limit0 - i0;
249 long int len1 = limit1 - i1;
250 fprintf (outfile, "i%ld,%ld\n", len0, len1);
255 while (i0 != limit0 && i1 != limit1)
256 print_1sdiff_line (&files[0].linbuf[i0++], ' ',
257 &files[1].linbuf[i1++]);
259 print_1sdiff_line (0, ')', &files[1].linbuf[i1++]);
262 print_1sdiff_line (&files[0].linbuf[i0++], '(', 0);
269 /* Print a hunk of an sdiff diff.
270 This is a contiguous portion of a complete edit script,
271 describing changes in consecutive lines. */
274 print_sdiff_hunk (struct change *hunk)
276 lin first0, last0, first1, last1;
279 /* Determine range of line numbers involved in each file. */
280 enum changes changes =
281 analyze_hunk (hunk, &first0, &last0, &first1, &last1);
285 /* Print out lines up to this change. */
286 print_sdiff_common_lines (first0, first1);
288 if (sdiff_merge_assist)
290 long int len0 = last0 - first0 + 1;
291 long int len1 = last1 - first1 + 1;
292 fprintf (outfile, "c%ld,%ld\n", len0, len1);
295 /* Print ``xxx | xxx '' lines */
296 if (changes == CHANGED)
298 for (i = first0, j = first1; i <= last0 && j <= last1; i++, j++)
299 print_1sdiff_line (&files[0].linbuf[i], '|', &files[1].linbuf[j]);
300 changes = (i <= last0 ? OLD : 0) + (j <= last1 ? NEW : 0);
305 /* Print `` > xxx '' lines */
308 for (j = first1; j <= last1; ++j)
309 print_1sdiff_line (0, '>', &files[1].linbuf[j]);
313 /* Print ``xxx < '' lines */
316 for (i = first0; i <= last0; ++i)
317 print_1sdiff_line (&files[0].linbuf[i], '<', 0);