1 /* sdiff-format output routines for GNU DIFF. 2 3 Copyright (C) 1991-1993, 1998, 2001-2002, 2004, 2009-2013, 2015-2018 Free 4 Software Foundation, Inc. 5 6 This file is part of GNU DIFF. 7 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. 14 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. */ 22 23 #include "diff.h" 24 25 #include <wchar.h> 26 27 static void print_sdiff_common_lines (lin, lin); 28 static void print_sdiff_hunk (struct change *); 29 30 /* Next line number to be printed in the two input files. */ 31 static lin next0, next1; 32 33 /* Print the edit-script SCRIPT as a sdiff style output. */ 34 35 void 36 print_sdiff_script (struct change *script) 37 { 38 begin_output (); 39 40 next0 = next1 = - files[0].prefix_lines; 41 print_script (script, find_change, print_sdiff_hunk); 42 43 print_sdiff_common_lines (files[0].valid_lines, files[1].valid_lines); 44 } 45 46 /* Tab from column FROM to column TO, where FROM <= TO. Yield TO. */ 47 48 static size_t 49 tab_from_to (size_t from, size_t to) 50 { 51 FILE *out = outfile; 52 size_t tab; 53 size_t tab_size = tabsize; 54 55 if (!expand_tabs) 56 for (tab = from + tab_size - from % tab_size; tab <= to; tab += tab_size) 57 { 58 putc ('\t', out); 59 from = tab; 60 } 61 while (from++ < to) 62 putc (' ', out); 63 return to; 64 } 65 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). */ 69 70 static size_t 71 print_half_line (char const *const *line, size_t indent, size_t out_bound) 72 { 73 FILE *out = outfile; 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 }; 79 80 while (text_pointer < text_limit) 81 { 82 char const *tp0 = text_pointer; 83 register char c = *text_pointer++; 84 85 switch (c) 86 { 87 case '\t': 88 { 89 size_t spaces = tabsize - in_position % tabsize; 90 if (in_position == out_position) 91 { 92 size_t tabstop = out_position + spaces; 93 if (expand_tabs) 94 { 95 if (out_bound < tabstop) 96 tabstop = out_bound; 97 for (; out_position < tabstop; out_position++) 98 putc (' ', out); 99 } 100 else 101 if (tabstop < out_bound) 102 { 103 out_position = tabstop; 104 putc (c, out); 105 } 106 } 107 in_position += spaces; 108 } 109 break; 110 111 case '\r': 112 { 113 putc (c, out); 114 tab_from_to (0, indent); 115 in_position = out_position = 0; 116 } 117 break; 118 119 case '\b': 120 if (in_position != 0 && --in_position < out_bound) 121 { 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++) 125 putc (' ', out); 126 else 127 { 128 out_position = in_position; 129 putc (c, out); 130 } 131 } 132 break; 133 134 default: 135 { 136 wchar_t wc; 137 size_t bytes = mbrtowc (&wc, tp0, text_limit - tp0, &mbstate); 138 139 if (0 < bytes && bytes < (size_t) -2) 140 { 141 int width = wcwidth (wc); 142 if (0 < width) 143 in_position += width; 144 if (in_position <= out_bound) 145 { 146 out_position = in_position; 147 fwrite (tp0, 1, bytes, stdout); 148 } 149 text_pointer = tp0 + bytes; 150 break; 151 } 152 } 153 FALLTHROUGH; 154 case '\f': 155 case '\v': 156 if (in_position < out_bound) 157 putc (c, out); 158 break; 159 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 '>': 166 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': 172 case 'Z': 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) 182 { 183 out_position = in_position; 184 putc (c, out); 185 } 186 break; 187 188 case '\n': 189 return out_position; 190 } 191 } 192 193 return out_position; 194 } 195 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. */ 199 200 static void 201 print_1sdiff_line (char const *const *left, char sep, 202 char const *const *right) 203 { 204 FILE *out = outfile; 205 size_t hw = sdiff_half_width; 206 size_t c2o = sdiff_column2_offset; 207 size_t col = 0; 208 bool put_newline = false; 209 bool color_to_reset = false; 210 211 if (sep == '<') 212 { 213 set_color_context (DELETE_CONTEXT); 214 color_to_reset = true; 215 } 216 else if (sep == '>') 217 { 218 set_color_context (ADD_CONTEXT); 219 color_to_reset = true; 220 } 221 222 if (left) 223 { 224 put_newline |= left[1][-1] == '\n'; 225 col = print_half_line (left, 0, hw); 226 } 227 228 if (sep != ' ') 229 { 230 col = tab_from_to (col, (hw + c2o - 1) / 2) + 1; 231 if (sep == '|' && put_newline != (right[1][-1] == '\n')) 232 sep = put_newline ? '/' : '\\'; 233 putc (sep, out); 234 } 235 236 if (right) 237 { 238 put_newline |= right[1][-1] == '\n'; 239 if (**right != '\n') 240 { 241 col = tab_from_to (col, c2o); 242 print_half_line (right, col, hw); 243 } 244 } 245 246 if (put_newline) 247 putc ('\n', out); 248 249 if (color_to_reset) 250 set_color_context (RESET_CONTEXT); 251 } 252 253 /* Print lines common to both files in side-by-side format. */ 254 static void 255 print_sdiff_common_lines (lin limit0, lin limit1) 256 { 257 lin i0 = next0, i1 = next1; 258 259 if (!suppress_common_lines && (i0 != limit0 || i1 != limit1)) 260 { 261 if (sdiff_merge_assist) 262 { 263 printint len0 = limit0 - i0; 264 printint len1 = limit1 - i1; 265 fprintf (outfile, "i%"pI"d,%"pI"d\n", len0, len1); 266 } 267 268 if (!left_column) 269 { 270 while (i0 != limit0 && i1 != limit1) 271 print_1sdiff_line (&files[0].linbuf[i0++], ' ', 272 &files[1].linbuf[i1++]); 273 while (i1 != limit1) 274 print_1sdiff_line (0, ')', &files[1].linbuf[i1++]); 275 } 276 while (i0 != limit0) 277 print_1sdiff_line (&files[0].linbuf[i0++], '(', 0); 278 } 279 280 next0 = limit0; 281 next1 = limit1; 282 } 283 284 /* Print a hunk of an sdiff diff. 285 This is a contiguous portion of a complete edit script, 286 describing changes in consecutive lines. */ 287 288 static void 289 print_sdiff_hunk (struct change *hunk) 290 { 291 lin first0, last0, first1, last1; 292 register lin i, j; 293 294 /* Determine range of line numbers involved in each file. */ 295 enum changes changes = 296 analyze_hunk (hunk, &first0, &last0, &first1, &last1); 297 if (!changes) 298 return; 299 300 /* Print out lines up to this change. */ 301 print_sdiff_common_lines (first0, first1); 302 303 if (sdiff_merge_assist) 304 { 305 printint len0 = last0 - first0 + 1; 306 printint len1 = last1 - first1 + 1; 307 fprintf (outfile, "c%"pI"d,%"pI"d\n", len0, len1); 308 } 309 310 /* Print "xxx | xxx " lines. */ 311 if (changes == CHANGED) 312 { 313 for (i = first0, j = first1; i <= last0 && j <= last1; i++, j++) 314 print_1sdiff_line (&files[0].linbuf[i], '|', &files[1].linbuf[j]); 315 changes = (i <= last0 ? OLD : 0) + (j <= last1 ? NEW : 0); 316 next0 = first0 = i; 317 next1 = first1 = j; 318 } 319 320 /* Print " > xxx " lines. */ 321 if (changes & NEW) 322 { 323 for (j = first1; j <= last1; ++j) 324 print_1sdiff_line (0, '>', &files[1].linbuf[j]); 325 next1 = j; 326 } 327 328 /* Print "xxx < " lines. */ 329 if (changes & OLD) 330 { 331 for (i = first0; i <= last0; ++i) 332 print_1sdiff_line (&files[0].linbuf[i], '<', 0); 333 next0 = i; 334 } 335 } 336