maint: post-release administrivia
[diffutils.git] / src / side.c
blob38b6384bb994fba015c0b56e13030885a5744f0c
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. */
23 #include "diff.h"
25 #include <mcel.h>
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. */
35 void
36 print_sdiff_script (struct change *script)
38 begin_output ();
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. */
49 static intmax_t
50 tab_from_to (intmax_t from, intmax_t to)
52 FILE *out = outfile;
54 if (!expand_tabs)
56 intmax_t tab_size = tabsize;
57 for (intmax_t tab = from + tab_size - from % tab_size;
58 tab <= to; tab += tab_size)
60 putc ('\t', out);
61 from = tab;
64 while (from++ < to)
65 putc (' ', out);
66 return to;
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). */
74 static intmax_t
75 print_half_line (char const *const *line, intmax_t indent, intmax_t out_bound)
77 FILE *out = outfile;
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
82 at any moment. */
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++;
92 switch (c)
94 case '\t':
96 intmax_t spaces = tabsize - in_position % tabsize;
97 intmax_t tabstop;
98 if (ckd_add (&tabstop, in_position, spaces))
99 return out_position;
100 if (in_position == out_position)
102 if (expand_tabs)
104 if (out_bound < tabstop)
105 tabstop = out_bound;
106 for (; out_position < tabstop; out_position++)
107 putc (' ', out);
109 else
110 if (tabstop < out_bound)
112 out_position = tabstop;
113 putc (c, out);
116 in_position = tabstop;
118 break;
120 case '\r':
122 putc (c, out);
123 tab_from_to (0, indent);
124 in_position = out_position = 0;
126 break;
128 case '\b':
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++)
134 putc (' ', out);
135 else
137 out_position = in_position;
138 putc (c, out);
141 break;
143 default:
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))
151 return out_position;
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;
162 break;
164 /* Print width 1. */
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 '>':
171 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':
177 case 'Z':
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))
186 return out_position;
187 if (in_position <= out_bound)
189 out_position = in_position;
190 putc (c, out);
192 break;
194 /* Print width 0. */
195 case '\0': case '\a': case '\f': case '\v':
196 if (in_position <= out_bound)
197 putc (c, out);
198 break;
200 case '\n':
201 return out_position;
205 return out_position;
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. */
212 static void
213 print_1sdiff_line (char const *const *left, char sep,
214 char const *const *right)
216 FILE *out = outfile;
217 intmax_t hw = sdiff_half_width;
218 intmax_t c2o = sdiff_column2_offset;
219 intmax_t col = 0;
220 bool put_newline = false;
221 bool color_to_reset = false;
223 if (sep == '<')
225 set_color_context (DELETE_CONTEXT);
226 color_to_reset = true;
228 else if (sep == '>')
230 set_color_context (ADD_CONTEXT);
231 color_to_reset = true;
234 if (left)
236 put_newline |= left[1][-1] == '\n';
237 col = print_half_line (left, 0, hw);
240 if (sep != ' ')
242 col = tab_from_to (col, (hw + c2o - 1) >> 1) + 1;
243 if (sep == '|' && put_newline != (right[1][-1] == '\n'))
244 sep = put_newline ? '/' : '\\';
245 putc (sep, out);
248 if (right)
250 put_newline |= right[1][-1] == '\n';
251 if (**right != '\n')
253 col = tab_from_to (col, c2o);
254 print_half_line (right, col, hw);
258 if (put_newline)
259 putc ('\n', out);
261 if (color_to_reset)
262 set_color_context (RESET_CONTEXT);
265 /* Print lines common to both files in side-by-side format. */
266 static void
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);
276 if (!left_column)
278 while (i0 != limit0 && i1 != limit1)
279 print_1sdiff_line (&curr.file[0].linbuf[i0++], ' ',
280 &curr.file[1].linbuf[i1++]);
281 while (i1 != limit1)
282 print_1sdiff_line (0, ')', &curr.file[1].linbuf[i1++]);
284 while (i0 != limit0)
285 print_1sdiff_line (&curr.file[0].linbuf[i0++], '(', 0);
288 next0 = limit0;
289 next1 = limit1;
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. */
296 static void
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);
303 if (!changes)
304 return;
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",
311 last0 - first0 + 1,
312 last1 - first1 + 1);
314 /* Print "xxx | xxx " lines. */
315 if (changes == CHANGED)
317 lin i, j;
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);
322 next0 = first0 = i;
323 next1 = first1 = j;
326 /* Print " > xxx " lines. */
327 if (changes & NEW)
329 lin j;
330 for (j = first1; j <= last1; ++j)
331 print_1sdiff_line (0, '>', &curr.file[1].linbuf[j]);
332 next1 = j;
335 /* Print "xxx < " lines. */
336 if (changes & OLD)
338 lin i;
339 for (i = first0; i <= last0; ++i)
340 print_1sdiff_line (&curr.file[0].linbuf[i], '<', 0);
341 next0 = i;