1 /* sdiff-format output routines for GNU DIFF.
3 Copyright (C) 1991-1993, 1998, 2001-2002, 2004, 2009-2013, 2015-2025 Free
4 Software Foundation, Inc.
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
= - curr
.file
[0].prefix_lines
;
41 print_script (script
, find_change
, print_sdiff_hunk
);
43 print_sdiff_common_lines (curr
.file
[0].valid_lines
,
44 curr
.file
[1].valid_lines
);
47 /* Tab from column FROM to column TO, where FROM <= TO. Yield TO. */
50 tab_from_to (intmax_t from
, intmax_t to
)
56 intmax_t tab_size
= tabsize
;
57 for (intmax_t tab
= from
+ tab_size
- from
% tab_size
;
58 tab
<= to
; tab
+= tab_size
)
69 /* Print the text for half an sdiff line. This means truncate to
70 OUT_BOUND columns, observing tabs, and trim a trailing newline.
71 Return the presumed column position on the output device after
72 the write (not the number of chars). */
75 print_half_line (char const *const *line
, intmax_t indent
, intmax_t out_bound
)
78 /* IN_POSITION is the current column position if we were outputting the
79 entire line, i.e. ignoring OUT_BOUND. */
80 intmax_t in_position
= 0;
81 /* OUT_POSITION is the current column position. It stays <= OUT_BOUND
83 intmax_t out_position
= 0;
84 char const *text_pointer
= line
[0];
85 char const *text_limit
= line
[1];
87 while (text_pointer
< text_limit
)
89 char const *tp0
= text_pointer
;
90 char c
= *text_pointer
++;
96 intmax_t spaces
= tabsize
- in_position
% tabsize
;
98 if (ckd_add (&tabstop
, in_position
, spaces
))
100 if (in_position
== out_position
)
104 if (out_bound
< tabstop
)
106 for (; out_position
< tabstop
; out_position
++)
110 if (tabstop
< out_bound
)
112 out_position
= tabstop
;
116 in_position
= tabstop
;
123 tab_from_to (0, indent
);
124 in_position
= out_position
= 0;
129 if (in_position
!= 0 && --in_position
< out_bound
)
131 if (out_position
<= in_position
)
132 /* Add spaces to make up for suppressed tab past out_bound. */
133 for (; out_position
< in_position
; out_position
++)
137 out_position
= in_position
;
145 /* A byte that might start a multibyte character.
146 Increase TEXT_POINTER, counting columns.
147 Assume encoding errors have print width 1. */
148 mcel_t g
= mcel_scan (tp0
, text_limit
);
149 int width
= g
.err
? 1 : c32width (g
.ch
);
150 if (0 < width
&& ckd_add (&in_position
, in_position
, width
))
153 /* If there is room, output the bytes since TP0. */
154 if (in_position
<= out_bound
)
156 out_position
= in_position
;
157 fwrite (tp0
, 1, g
.len
, out
);
160 text_pointer
= tp0
+ g
.len
;
165 case ' ': case '!': case '"': case '#': case '$': case '%':
166 case '&': case '\'': case '(': case ')': case '*':
167 case '+': case ',': case '-': case '.': case '/':
168 case '0': case '1': case '2': case '3': case '4':
169 case '5': case '6': case '7': case '8': case '9':
170 case ':': case ';': case '<': case '=': case '>':
172 case 'A': case 'B': case 'C': case 'D': case 'E':
173 case 'F': case 'G': case 'H': case 'I': case 'J':
174 case 'K': case 'L': case 'M': case 'N': case 'O':
175 case 'P': case 'Q': case 'R': case 'S': case 'T':
176 case 'U': case 'V': case 'W': case 'X': case 'Y':
178 case '[': case '\\': case ']': case '^': case '_': case '`':
179 case 'a': case 'b': case 'c': case 'd': case 'e':
180 case 'f': case 'g': case 'h': case 'i': case 'j':
181 case 'k': case 'l': case 'm': case 'n': case 'o':
182 case 'p': case 'q': case 'r': case 's': case 't':
183 case 'u': case 'v': case 'w': case 'x': case 'y':
184 case 'z': case '{': case '|': case '}': case '~':
185 if (ckd_add (&in_position
, in_position
, 1))
187 if (in_position
<= out_bound
)
189 out_position
= in_position
;
195 case '\0': case '\a': case '\f': case '\v':
196 if (in_position
<= out_bound
)
208 /* Print side by side lines with a separator in the middle.
209 0 parameters are taken to indicate white space text.
210 Blank lines that can easily be caught are reduced to a single newline. */
213 print_1sdiff_line (char const *const *left
, char sep
,
214 char const *const *right
)
217 intmax_t hw
= sdiff_half_width
;
218 intmax_t c2o
= sdiff_column2_offset
;
220 bool put_newline
= false;
221 bool color_to_reset
= false;
225 set_color_context (DELETE_CONTEXT
);
226 color_to_reset
= true;
230 set_color_context (ADD_CONTEXT
);
231 color_to_reset
= true;
236 put_newline
|= left
[1][-1] == '\n';
237 col
= print_half_line (left
, 0, hw
);
242 col
= tab_from_to (col
, (hw
+ c2o
- 1) >> 1) + 1;
243 if (sep
== '|' && put_newline
!= (right
[1][-1] == '\n'))
244 sep
= put_newline
? '/' : '\\';
250 put_newline
|= right
[1][-1] == '\n';
253 col
= tab_from_to (col
, c2o
);
254 print_half_line (right
, col
, hw
);
262 set_color_context (RESET_CONTEXT
);
265 /* Print lines common to both files in side-by-side format. */
267 print_sdiff_common_lines (lin limit0
, lin limit1
)
269 lin i0
= next0
, i1
= next1
;
271 if (!suppress_common_lines
&& (i0
!= limit0
|| i1
!= limit1
))
273 if (sdiff_merge_assist
)
274 fprintf (outfile
, "i%"pI
"d,%"pI
"d\n", limit0
- i0
, limit1
- i1
);
278 while (i0
!= limit0
&& i1
!= limit1
)
279 print_1sdiff_line (&curr
.file
[0].linbuf
[i0
++], ' ',
280 &curr
.file
[1].linbuf
[i1
++]);
282 print_1sdiff_line (0, ')', &curr
.file
[1].linbuf
[i1
++]);
285 print_1sdiff_line (&curr
.file
[0].linbuf
[i0
++], '(', 0);
292 /* Print a hunk of an sdiff diff.
293 This is a contiguous portion of a complete edit script,
294 describing changes in consecutive lines. */
297 print_sdiff_hunk (struct change
*hunk
)
299 /* Determine range of line numbers involved in each file. */
300 lin first0
, last0
, first1
, last1
;
301 enum changes changes
=
302 analyze_hunk (hunk
, &first0
, &last0
, &first1
, &last1
);
306 /* Print out lines up to this change. */
307 print_sdiff_common_lines (first0
, first1
);
309 if (sdiff_merge_assist
)
310 fprintf (outfile
, "c%"pI
"d,%"pI
"d\n",
314 /* Print "xxx | xxx " lines. */
315 if (changes
== CHANGED
)
318 for (i
= first0
, j
= first1
; i
<= last0
&& j
<= last1
; i
++, j
++)
319 print_1sdiff_line (&curr
.file
[0].linbuf
[i
], '|',
320 &curr
.file
[1].linbuf
[j
]);
321 changes
= (i
<= last0
? OLD
: 0) + (j
<= last1
? NEW
: 0);
326 /* Print " > xxx " lines. */
330 for (j
= first1
; j
<= last1
; ++j
)
331 print_1sdiff_line (0, '>', &curr
.file
[1].linbuf
[j
]);
335 /* Print "xxx < " lines. */
339 for (i
= first0
; i
<= last0
; ++i
)
340 print_1sdiff_line (&curr
.file
[0].linbuf
[i
], '<', 0);