| 1 |
/* Context-format output routines for GNU DIFF. |
| 2 |
|
| 3 |
Copyright (C) 1988-1989, 1991-1995, 1998, 2001-2002, 2004, 2006, 2009-2013, |
| 4 |
2015-2025 Free Software Foundation, Inc. |
| 5 |
|
| 6 |
This file is part of GNU DIFF. |
| 7 |
|
| 8 |
This program is free software: you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation, either version 3 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#include "diff.h" |
| 22 |
#include <c-ctype.h> |
| 23 |
#include <stat-time.h> |
| 24 |
#include <strftime.h> |
| 25 |
|
| 26 |
static char const *find_function (char const *const *, lin); |
| 27 |
static struct change *find_hunk (struct change *); |
| 28 |
static void mark_ignorable (struct change *); |
| 29 |
static void pr_context_hunk (struct change *); |
| 30 |
static void pr_unidiff_hunk (struct change *); |
| 31 |
|
| 32 |
/* Last place find_function started searching from. */ |
| 33 |
static lin find_function_last_search; |
| 34 |
|
| 35 |
/* The value find_function returned when it started searching there. */ |
| 36 |
static lin find_function_last_match; |
| 37 |
|
| 38 |
/* Print a label for a context diff, with a file name and date or a label. */ |
| 39 |
|
| 40 |
static void |
| 41 |
print_context_label (char const *mark, |
| 42 |
struct file_data *inf, |
| 43 |
char const *name, |
| 44 |
char const *label) |
| 45 |
{ |
| 46 |
set_color_context (HEADER_CONTEXT); |
| 47 |
if (label) |
| 48 |
fprintf (outfile, "%s %s", mark, label); |
| 49 |
else |
| 50 |
{ |
| 51 |
/* POSIX requires current time for stdin. */ |
| 52 |
struct timespec ts; |
| 53 |
if (inf->desc == STDIN_FILENO) |
| 54 |
{ |
| 55 |
static struct timespec now; |
| 56 |
if (!now.tv_sec) |
| 57 |
timespec_get (&now, TIME_UTC); |
| 58 |
ts = now; |
| 59 |
} |
| 60 |
else |
| 61 |
ts = get_stat_mtime (&inf->stat); |
| 62 |
|
| 63 |
/* Buffer for nstftime output, big enough to handle any |
| 64 |
timestamp formatted according to time_format. |
| 65 |
Its size is an upper bound for the format "%Y-%m-%d %H:%M:%S.%N %z", |
| 66 |
with an int for year and a time_t for time zone hour. |
| 67 |
The format "%Y-%m-%d %H:%M:%S %z" generates fewer bytes, |
| 68 |
and although the format "%a %b %e %T %Y" could in theory |
| 69 |
generate more bytes in practice it never does. */ |
| 70 |
char buf[INT_STRLEN_BOUND (int) + INT_STRLEN_BOUND (time_t) |
| 71 |
+ sizeof "-%m-%d %H:%M:%S.000000000 +00"]; |
| 72 |
|
| 73 |
struct tm const *tm = localtime (&ts.tv_sec); |
| 74 |
int nsec = ts.tv_nsec; |
| 75 |
if (tm && nstrftime (buf, sizeof buf, time_format, tm, localtz, nsec)) |
| 76 |
fprintf (outfile, "%s %s\t%s", mark, name, buf); |
| 77 |
else if (TYPE_SIGNED (time_t)) |
| 78 |
{ |
| 79 |
intmax_t sec = inf->stat.st_mtime; |
| 80 |
fprintf (outfile, "%s %s\t%"PRIdMAX".%.9d", mark, name, sec, nsec); |
| 81 |
} |
| 82 |
else |
| 83 |
{ |
| 84 |
uintmax_t sec = inf->stat.st_mtime; |
| 85 |
fprintf (outfile, "%s %s\t%"PRIuMAX".%.9d", mark, name, sec, nsec); |
| 86 |
} |
| 87 |
} |
| 88 |
set_color_context (RESET_CONTEXT); |
| 89 |
putc ('\n', outfile); |
| 90 |
} |
| 91 |
|
| 92 |
/* Print a header for a context diff, with the file names and dates. */ |
| 93 |
|
| 94 |
void |
| 95 |
print_context_header (struct file_data inf[], char const *const *names, bool unidiff) |
| 96 |
{ |
| 97 |
if (unidiff) |
| 98 |
{ |
| 99 |
print_context_label ("---", &inf[0], names[0], file_label[0]); |
| 100 |
print_context_label ("+++", &inf[1], names[1], file_label[1]); |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
print_context_label ("***", &inf[0], names[0], file_label[0]); |
| 105 |
print_context_label ("---", &inf[1], names[1], file_label[1]); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/* Print an edit script in context format. */ |
| 110 |
|
| 111 |
void |
| 112 |
print_context_script (struct change *script, bool unidiff) |
| 113 |
{ |
| 114 |
if (ignore_blank_lines || ignore_regexp.fastmap) |
| 115 |
mark_ignorable (script); |
| 116 |
else |
| 117 |
for (struct change *e = script; e; e = e->link) |
| 118 |
e->ignore = false; |
| 119 |
|
| 120 |
find_function_last_search = - curr.file[0].prefix_lines; |
| 121 |
find_function_last_match = LIN_MAX; |
| 122 |
|
| 123 |
if (unidiff) |
| 124 |
print_script (script, find_hunk, pr_unidiff_hunk); |
| 125 |
else |
| 126 |
print_script (script, find_hunk, pr_context_hunk); |
| 127 |
} |
| 128 |
|
| 129 |
/* Print a pair of line numbers with a comma, translated for file FILE. |
| 130 |
If the second number is not greater, use the first in place of it. |
| 131 |
|
| 132 |
Args A and B are internal line numbers. |
| 133 |
We print the translated (real) line numbers. */ |
| 134 |
|
| 135 |
static void |
| 136 |
print_context_number_range (struct file_data const *file, lin a, lin b) |
| 137 |
{ |
| 138 |
lin trans_a, trans_b; |
| 139 |
translate_range (file, a, b, &trans_a, &trans_b); |
| 140 |
|
| 141 |
/* We can have B <= A in the case of a range of no lines. |
| 142 |
In this case, we should print the line number before the range, |
| 143 |
which is B. |
| 144 |
|
| 145 |
POSIX 1003.1-2001 requires two line numbers separated by a comma |
| 146 |
even if the line numbers are the same. However, this does not |
| 147 |
match existing practice and is surely an error in the |
| 148 |
specification. */ |
| 149 |
|
| 150 |
if (trans_b <= trans_a) |
| 151 |
fprintf (outfile, "%"pI"d", trans_b); |
| 152 |
else |
| 153 |
fprintf (outfile, "%"pI"d,%"pI"d", trans_a, trans_b); |
| 154 |
} |
| 155 |
|
| 156 |
/* Print FUNCTION in a context header. */ |
| 157 |
static void |
| 158 |
print_context_function (FILE *out, char const *function) |
| 159 |
{ |
| 160 |
int i, j; |
| 161 |
putc (' ', out); |
| 162 |
for (i = 0; c_isspace ((unsigned char) function[i]) && function[i] != '\n'; i++) |
| 163 |
continue; |
| 164 |
for (j = i; j < i + 40 && function[j] != '\n'; j++) |
| 165 |
continue; |
| 166 |
while (i < j && c_isspace ((unsigned char) function[j - 1])) |
| 167 |
j--; |
| 168 |
fwrite (function + i, sizeof (char), j - i, out); |
| 169 |
} |
| 170 |
|
| 171 |
/* Print a portion of an edit script in context format. |
| 172 |
HUNK is the beginning of the portion to be printed. |
| 173 |
The end is marked by a 'link' that has been nulled out. |
| 174 |
|
| 175 |
Prints out lines from both files, and precedes each |
| 176 |
line with the appropriate flag-character. */ |
| 177 |
|
| 178 |
static void |
| 179 |
pr_context_hunk (struct change *hunk) |
| 180 |
{ |
| 181 |
/* Determine range of line numbers involved in each file. */ |
| 182 |
lin first0, last0, first1, last1; |
| 183 |
enum changes changes = analyze_hunk (hunk, &first0, &last0, &first1, &last1); |
| 184 |
if (! changes) |
| 185 |
return; |
| 186 |
|
| 187 |
/* Include a context's width before and after. */ |
| 188 |
|
| 189 |
lin minus_prefix_lines = - curr.file[0].prefix_lines; |
| 190 |
first0 = MAX (first0 - context, minus_prefix_lines); |
| 191 |
first1 = MAX (first1 - context, minus_prefix_lines); |
| 192 |
if (last0 < curr.file[0].valid_lines - context) |
| 193 |
last0 += context; |
| 194 |
else |
| 195 |
last0 = curr.file[0].valid_lines - 1; |
| 196 |
if (last1 < curr.file[1].valid_lines - context) |
| 197 |
last1 += context; |
| 198 |
else |
| 199 |
last1 = curr.file[1].valid_lines - 1; |
| 200 |
|
| 201 |
/* If desired, find the preceding function definition line in file 0. */ |
| 202 |
char const *function = nullptr; |
| 203 |
if (function_regexp.fastmap) |
| 204 |
function = find_function (curr.file[0].linbuf, first0); |
| 205 |
|
| 206 |
begin_output (); |
| 207 |
FILE *out = outfile; |
| 208 |
|
| 209 |
fputs ("***************", out); |
| 210 |
|
| 211 |
if (function) |
| 212 |
print_context_function (out, function); |
| 213 |
|
| 214 |
putc ('\n', out); |
| 215 |
set_color_context (LINE_NUMBER_CONTEXT); |
| 216 |
fputs ("*** ", out); |
| 217 |
print_context_number_range (&curr.file[0], first0, last0); |
| 218 |
fputs (" ****", out); |
| 219 |
set_color_context (RESET_CONTEXT); |
| 220 |
putc ('\n', out); |
| 221 |
|
| 222 |
if (changes & OLD) |
| 223 |
{ |
| 224 |
struct change *next = hunk; |
| 225 |
|
| 226 |
for (lin i = first0; i <= last0; i++) |
| 227 |
{ |
| 228 |
set_color_context (DELETE_CONTEXT); |
| 229 |
|
| 230 |
/* Skip past changes that apply (in file 0) |
| 231 |
only to lines before line I. */ |
| 232 |
|
| 233 |
while (next && next->line0 + next->deleted <= i) |
| 234 |
next = next->link; |
| 235 |
|
| 236 |
/* Compute the marking for line I. */ |
| 237 |
|
| 238 |
char const *prefix = " "; |
| 239 |
if (next && next->line0 <= i) |
| 240 |
{ |
| 241 |
/* The change NEXT covers this line. |
| 242 |
If lines were inserted here in file 1, this is "changed". |
| 243 |
Otherwise it is "deleted". */ |
| 244 |
prefix = (next->inserted > 0 ? "!" : "-"); |
| 245 |
} |
| 246 |
print_1_line_nl (prefix, &curr.file[0].linbuf[i], true); |
| 247 |
set_color_context (RESET_CONTEXT); |
| 248 |
if (curr.file[0].linbuf[i + 1][-1] == '\n') |
| 249 |
putc ('\n', out); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
set_color_context (LINE_NUMBER_CONTEXT); |
| 254 |
fputs ("--- ", out); |
| 255 |
print_context_number_range (&curr.file[1], first1, last1); |
| 256 |
fputs (" ----", out); |
| 257 |
set_color_context (RESET_CONTEXT); |
| 258 |
putc ('\n', out); |
| 259 |
|
| 260 |
if (changes & NEW) |
| 261 |
{ |
| 262 |
struct change *next = hunk; |
| 263 |
|
| 264 |
for (lin i = first1; i <= last1; i++) |
| 265 |
{ |
| 266 |
set_color_context (ADD_CONTEXT); |
| 267 |
|
| 268 |
/* Skip past changes that apply (in file 1) |
| 269 |
only to lines before line I. */ |
| 270 |
|
| 271 |
while (next && next->line1 + next->inserted <= i) |
| 272 |
next = next->link; |
| 273 |
|
| 274 |
/* Compute the marking for line I. */ |
| 275 |
|
| 276 |
char const *prefix = " "; |
| 277 |
if (next && next->line1 <= i) |
| 278 |
{ |
| 279 |
/* The change NEXT covers this line. |
| 280 |
If lines were deleted here in file 0, this is "changed". |
| 281 |
Otherwise it is "inserted". */ |
| 282 |
prefix = (next->deleted > 0 ? "!" : "+"); |
| 283 |
} |
| 284 |
print_1_line_nl (prefix, &curr.file[1].linbuf[i], true); |
| 285 |
set_color_context (RESET_CONTEXT); |
| 286 |
if (curr.file[1].linbuf[i + 1][-1] == '\n') |
| 287 |
putc ('\n', out); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
/* Print a pair of line numbers with a comma, translated for file FILE. |
| 293 |
If the second number is smaller, use the first in place of it. |
| 294 |
If the numbers are equal, print just one number. |
| 295 |
|
| 296 |
Args A and B are internal line numbers. |
| 297 |
We print the translated (real) line numbers. */ |
| 298 |
|
| 299 |
static void |
| 300 |
print_unidiff_number_range (struct file_data const *file, lin a, lin b) |
| 301 |
{ |
| 302 |
lin trans_a, trans_b; |
| 303 |
translate_range (file, a, b, &trans_a, &trans_b); |
| 304 |
|
| 305 |
/* We can have B < A in the case of a range of no lines. |
| 306 |
In this case, we print the line number before the range, |
| 307 |
which is B. It would be more logical to print A, but |
| 308 |
'patch' expects B in order to detect diffs against empty files. */ |
| 309 |
if (trans_b <= trans_a) |
| 310 |
fprintf (outfile, trans_b < trans_a ? "%"pI"d,0" : "%"pI"d", trans_b); |
| 311 |
else |
| 312 |
fprintf (outfile, "%"pI"d,%"pI"d", trans_a, trans_b - trans_a + 1); |
| 313 |
} |
| 314 |
|
| 315 |
/* Print a portion of an edit script in unidiff format. |
| 316 |
HUNK is the beginning of the portion to be printed. |
| 317 |
The end is marked by a 'link' that has been nulled out. |
| 318 |
|
| 319 |
Prints out lines from both files, and precedes each |
| 320 |
line with the appropriate flag-character. */ |
| 321 |
|
| 322 |
static void |
| 323 |
pr_unidiff_hunk (struct change *hunk) |
| 324 |
{ |
| 325 |
/* Determine range of line numbers involved in each file. */ |
| 326 |
lin first0, last0, first1, last1; |
| 327 |
if (! analyze_hunk (hunk, &first0, &last0, &first1, &last1)) |
| 328 |
return; |
| 329 |
|
| 330 |
/* Include a context's width before and after. */ |
| 331 |
|
| 332 |
lin minus_prefix_lines = - curr.file[0].prefix_lines; |
| 333 |
first0 = MAX (first0 - context, minus_prefix_lines); |
| 334 |
first1 = MAX (first1 - context, minus_prefix_lines); |
| 335 |
if (last0 < curr.file[0].valid_lines - context) |
| 336 |
last0 += context; |
| 337 |
else |
| 338 |
last0 = curr.file[0].valid_lines - 1; |
| 339 |
if (last1 < curr.file[1].valid_lines - context) |
| 340 |
last1 += context; |
| 341 |
else |
| 342 |
last1 = curr.file[1].valid_lines - 1; |
| 343 |
|
| 344 |
/* If desired, find the preceding function definition line in file 0. */ |
| 345 |
char const *function = nullptr; |
| 346 |
if (function_regexp.fastmap) |
| 347 |
function = find_function (curr.file[0].linbuf, first0); |
| 348 |
|
| 349 |
begin_output (); |
| 350 |
FILE *out = outfile; |
| 351 |
|
| 352 |
set_color_context (LINE_NUMBER_CONTEXT); |
| 353 |
fputs ("@@ -", out); |
| 354 |
print_unidiff_number_range (&curr.file[0], first0, last0); |
| 355 |
fputs (" +", out); |
| 356 |
print_unidiff_number_range (&curr.file[1], first1, last1); |
| 357 |
fputs (" @@", out); |
| 358 |
set_color_context (RESET_CONTEXT); |
| 359 |
|
| 360 |
if (function) |
| 361 |
print_context_function (out, function); |
| 362 |
|
| 363 |
putc ('\n', out); |
| 364 |
|
| 365 |
struct change *next = hunk; |
| 366 |
lin i = first0; |
| 367 |
lin j = first1; |
| 368 |
|
| 369 |
while (i <= last0 || j <= last1) |
| 370 |
{ |
| 371 |
|
| 372 |
/* If the line isn't a difference, output the context from file 0. */ |
| 373 |
|
| 374 |
if (!next || i < next->line0) |
| 375 |
{ |
| 376 |
char const *const *line = &curr.file[0].linbuf[i++]; |
| 377 |
if (! (suppress_blank_empty && **line == '\n')) |
| 378 |
putc (initial_tab ? '\t' : ' ', out); |
| 379 |
print_1_line (nullptr, line); |
| 380 |
j++; |
| 381 |
} |
| 382 |
else |
| 383 |
{ |
| 384 |
/* For each difference, first output the deleted part. */ |
| 385 |
|
| 386 |
lin k = next->deleted; |
| 387 |
|
| 388 |
while (k--) |
| 389 |
{ |
| 390 |
char const *const *line = &curr.file[0].linbuf[i++]; |
| 391 |
set_color_context (DELETE_CONTEXT); |
| 392 |
putc ('-', out); |
| 393 |
if (initial_tab && ! (suppress_blank_empty && **line == '\n')) |
| 394 |
putc ('\t', out); |
| 395 |
print_1_line_nl (nullptr, line, true); |
| 396 |
|
| 397 |
set_color_context (RESET_CONTEXT); |
| 398 |
|
| 399 |
if (line[1][-1] == '\n') |
| 400 |
putc ('\n', out); |
| 401 |
} |
| 402 |
|
| 403 |
/* Then output the inserted part. */ |
| 404 |
|
| 405 |
k = next->inserted; |
| 406 |
|
| 407 |
while (k--) |
| 408 |
{ |
| 409 |
char const *const *line = &curr.file[1].linbuf[j++]; |
| 410 |
set_color_context (ADD_CONTEXT); |
| 411 |
putc ('+', out); |
| 412 |
if (initial_tab && ! (suppress_blank_empty && **line == '\n')) |
| 413 |
putc ('\t', out); |
| 414 |
print_1_line_nl (nullptr, line, true); |
| 415 |
|
| 416 |
set_color_context (RESET_CONTEXT); |
| 417 |
|
| 418 |
if (line[1][-1] == '\n') |
| 419 |
putc ('\n', out); |
| 420 |
} |
| 421 |
|
| 422 |
/* We're done with this hunk, so on to the next! */ |
| 423 |
|
| 424 |
next = next->link; |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
/* Scan a (forward-ordered) edit script for the first place that more than |
| 430 |
2*CONTEXT unchanged lines appear, and return a pointer |
| 431 |
to the 'struct change' for the last change before those lines. */ |
| 432 |
|
| 433 |
static struct change * ATTRIBUTE_PURE |
| 434 |
find_hunk (struct change *script) |
| 435 |
{ |
| 436 |
/* Threshold distance is CONTEXT if the second change is ignorable, |
| 437 |
min (2 * CONTEXT + 1, LIN_MAX) otherwise. */ |
| 438 |
lin ignorable_threshold = context; |
| 439 |
lin non_ignorable_threshold = (ckd_mul (&non_ignorable_threshold, context, 2) |
| 440 |
? LIN_MAX |
| 441 |
: non_ignorable_threshold + 1); |
| 442 |
|
| 443 |
for (struct change *next; ; script = next) |
| 444 |
{ |
| 445 |
next = script->link; |
| 446 |
/* Compute number of first line in each file beyond this changed. */ |
| 447 |
lin top0 = script->line0 + script->deleted; |
| 448 |
lin top1 = script->line1 + script->inserted; |
| 449 |
lin thresh = (next && next->ignore |
| 450 |
? ignorable_threshold |
| 451 |
: non_ignorable_threshold); |
| 452 |
/* It is not supposed to matter which file we check in the end-test. */ |
| 453 |
dassert (!next || next->line0 - top0 == next->line1 - top1); |
| 454 |
|
| 455 |
/* Keep going if less than THRESH lines elapse |
| 456 |
before the affected line. */ |
| 457 |
if (!next || thresh <= next->line0 - top0) |
| 458 |
return script; |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
/* Set the 'ignore' flag properly in each change in SCRIPT. |
| 463 |
It should be 1 if all the lines inserted or deleted in that change |
| 464 |
are ignorable lines. */ |
| 465 |
|
| 466 |
static void |
| 467 |
mark_ignorable (struct change *script) |
| 468 |
{ |
| 469 |
while (script) |
| 470 |
{ |
| 471 |
struct change *next = script->link; |
| 472 |
|
| 473 |
/* Turn this change into a hunk: detach it from the others. */ |
| 474 |
script->link = nullptr; |
| 475 |
|
| 476 |
/* Determine whether this change is ignorable. */ |
| 477 |
lin first0, last0, first1, last1; |
| 478 |
script->ignore = ! analyze_hunk (script, |
| 479 |
&first0, &last0, &first1, &last1); |
| 480 |
|
| 481 |
/* Reconnect the chain as before. */ |
| 482 |
script->link = next; |
| 483 |
|
| 484 |
/* Advance to the following change. */ |
| 485 |
script = next; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
/* Find the last function-header line in LINBUF prior to line number LINENUM. |
| 490 |
This is a line containing a match for the regexp in 'function_regexp'. |
| 491 |
Return the address of the text, or null if no function-header is found. */ |
| 492 |
|
| 493 |
static char const * |
| 494 |
find_function (char const *const *linbuf, lin linenum) |
| 495 |
{ |
| 496 |
lin i = linenum; |
| 497 |
lin last = find_function_last_search; |
| 498 |
find_function_last_search = i; |
| 499 |
|
| 500 |
while (last <= --i) |
| 501 |
{ |
| 502 |
/* See if this line is what we want. */ |
| 503 |
char const *line = linbuf[i]; |
| 504 |
idx_t linelen = linbuf[i + 1] - line - 1; |
| 505 |
|
| 506 |
/* This line is for documentation; in practice it's equivalent |
| 507 |
to LEN = LINELEN and no machine code is generated. */ |
| 508 |
regoff_t len = MIN (linelen, TYPE_MAXIMUM (regoff_t)); |
| 509 |
|
| 510 |
if (0 <= re_search (&function_regexp, line, len, 0, len, nullptr)) |
| 511 |
{ |
| 512 |
find_function_last_match = i; |
| 513 |
return line; |
| 514 |
} |
| 515 |
} |
| 516 |
/* If we search back to where we started searching the previous time, |
| 517 |
find the line we found last time. */ |
| 518 |
if (find_function_last_match != LIN_MAX) |
| 519 |
return linbuf[find_function_last_match]; |
| 520 |
|
| 521 |
return nullptr; |
| 522 |
} |