1 /* Context-format output routines for GNU DIFF.
3 Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, 2001,
4 2002, 2004 Free Software Foundation, Inc.
6 This file is part of GNU DIFF.
8 GNU DIFF 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 2, or (at your option)
13 GNU DIFF 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.
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING.
20 If not, write to the Free Software Foundation,
21 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 # define TIMESPEC_NS(timespec) ((timespec).ST_MTIM_NSEC)
29 # define TIMESPEC_NS(timespec) 0
32 size_t nstrftime (char *, size_t, char const *, struct tm const *, int, int);
34 static char const *find_function (char const * const *, lin);
35 static struct change *find_hunk (struct change *);
36 static void mark_ignorable (struct change *);
37 static void pr_context_hunk (struct change *);
38 static void pr_unidiff_hunk (struct change *);
40 /* Last place find_function started searching from. */
41 static lin find_function_last_search;
43 /* The value find_function returned when it started searching there. */
44 static lin find_function_last_match;
46 /* Print a label for a context diff, with a file name and date or a label. */
49 print_context_label (char const *mark,
50 struct file_data *inf,
54 fprintf (outfile, "%s %s\n", mark, label);
57 char buf[MAX (INT_STRLEN_BOUND (int) + 32,
58 INT_STRLEN_BOUND (time_t) + 11)];
59 struct tm const *tm = localtime (&inf->stat.st_mtime);
60 int nsec = TIMESPEC_NS (inf->stat.st_mtim);
61 if (! (tm && nstrftime (buf, sizeof buf, time_format, tm, 0, nsec)))
63 long int sec = inf->stat.st_mtime;
64 verify (info_preserved, sizeof inf->stat.st_mtime <= sizeof sec);
65 sprintf (buf, "%ld.%.9d", sec, nsec);
67 fprintf (outfile, "%s %s\t%s\n", mark, inf->name, buf);
71 /* Print a header for a context diff, with the file names and dates. */
74 print_context_header (struct file_data inf[], bool unidiff)
78 print_context_label ("---", &inf[0], file_label[0]);
79 print_context_label ("+++", &inf[1], file_label[1]);
83 print_context_label ("***", &inf[0], file_label[0]);
84 print_context_label ("---", &inf[1], file_label[1]);
88 /* Print an edit script in context format. */
91 print_context_script (struct change *script, bool unidiff)
93 if (ignore_blank_lines || ignore_regexp.fastmap)
94 mark_ignorable (script);
98 for (e = script; e; e = e->link)
102 find_function_last_search = - files[0].prefix_lines;
103 find_function_last_match = LIN_MAX;
106 print_script (script, find_hunk, pr_unidiff_hunk);
108 print_script (script, find_hunk, pr_context_hunk);
111 /* Print a pair of line numbers with a comma, translated for file FILE.
112 If the second number is not greater, use the first in place of it.
114 Args A and B are internal line numbers.
115 We print the translated (real) line numbers. */
118 print_context_number_range (struct file_data const *file, lin a, lin b)
120 long int trans_a, trans_b;
121 translate_range (file, a, b, &trans_a, &trans_b);
123 /* We can have B <= A in the case of a range of no lines.
124 In this case, we should print the line number before the range,
127 POSIX 1003.1-2001 requires two line numbers separated by a comma
128 even if the line numbers are the same. However, this does not
129 match existing practice and is surely an error in the
132 if (trans_b <= trans_a)
133 fprintf (outfile, "%ld", trans_b);
135 fprintf (outfile, "%ld,%ld", trans_a, trans_b);
138 /* Print FUNCTION in a context header. */
140 print_context_function (FILE *out, char const *function)
144 for (i = 0; i < 40 && function[i] != '\n'; i++)
146 fwrite (function, sizeof (char), i, out);
149 /* Print a portion of an edit script in context format.
150 HUNK is the beginning of the portion to be printed.
151 The end is marked by a `link' that has been nulled out.
153 Prints out lines from both files, and precedes each
154 line with the appropriate flag-character. */
157 pr_context_hunk (struct change *hunk)
159 lin first0, last0, first1, last1, i;
161 char const *function;
164 /* Determine range of line numbers involved in each file. */
166 enum changes changes = analyze_hunk (hunk, &first0, &last0, &first1, &last1);
170 /* Include a context's width before and after. */
172 i = - files[0].prefix_lines;
173 first0 = MAX (first0 - context, i);
174 first1 = MAX (first1 - context, i);
175 if (last0 < files[0].valid_lines - context)
178 last0 = files[0].valid_lines - 1;
179 if (last1 < files[1].valid_lines - context)
182 last1 = files[1].valid_lines - 1;
184 /* If desired, find the preceding function definition line in file 0. */
186 if (function_regexp.fastmap)
187 function = find_function (files[0].linbuf, first0);
192 fprintf (out, "***************");
195 print_context_function (out, function);
197 fprintf (out, "\n*** ");
198 print_context_number_range (&files[0], first0, last0);
199 fprintf (out, " ****\n");
203 struct change *next = hunk;
205 for (i = first0; i <= last0; i++)
207 /* Skip past changes that apply (in file 0)
208 only to lines before line I. */
210 while (next && next->line0 + next->deleted <= i)
213 /* Compute the marking for line I. */
216 if (next && next->line0 <= i)
217 /* The change NEXT covers this line.
218 If lines were inserted here in file 1, this is "changed".
219 Otherwise it is "deleted". */
220 prefix = (next->inserted > 0 ? "!" : "-");
222 print_1_line (prefix, &files[0].linbuf[i]);
226 fprintf (out, "--- ");
227 print_context_number_range (&files[1], first1, last1);
228 fprintf (out, " ----\n");
232 struct change *next = hunk;
234 for (i = first1; i <= last1; i++)
236 /* Skip past changes that apply (in file 1)
237 only to lines before line I. */
239 while (next && next->line1 + next->inserted <= i)
242 /* Compute the marking for line I. */
245 if (next && next->line1 <= i)
246 /* The change NEXT covers this line.
247 If lines were deleted here in file 0, this is "changed".
248 Otherwise it is "inserted". */
249 prefix = (next->deleted > 0 ? "!" : "+");
251 print_1_line (prefix, &files[1].linbuf[i]);
256 /* Print a pair of line numbers with a comma, translated for file FILE.
257 If the second number is smaller, use the first in place of it.
258 If the numbers are equal, print just one number.
260 Args A and B are internal line numbers.
261 We print the translated (real) line numbers. */
264 print_unidiff_number_range (struct file_data const *file, lin a, lin b)
266 long int trans_a, trans_b;
267 translate_range (file, a, b, &trans_a, &trans_b);
269 /* We can have B < A in the case of a range of no lines.
270 In this case, we print the line number before the range,
271 which is B. It would be more logical to print A, but
272 'patch' expects B in order to detect diffs against empty files. */
273 if (trans_b <= trans_a)
274 fprintf (outfile, trans_b < trans_a ? "%ld,0" : "%ld", trans_b);
276 fprintf (outfile, "%ld,%ld", trans_a, trans_b - trans_a + 1);
279 /* Print a portion of an edit script in unidiff format.
280 HUNK is the beginning of the portion to be printed.
281 The end is marked by a `link' that has been nulled out.
283 Prints out lines from both files, and precedes each
284 line with the appropriate flag-character. */
287 pr_unidiff_hunk (struct change *hunk)
289 lin first0, last0, first1, last1;
292 char const *function;
295 /* Determine range of line numbers involved in each file. */
297 if (! analyze_hunk (hunk, &first0, &last0, &first1, &last1))
300 /* Include a context's width before and after. */
302 i = - files[0].prefix_lines;
303 first0 = MAX (first0 - context, i);
304 first1 = MAX (first1 - context, i);
305 if (last0 < files[0].valid_lines - context)
308 last0 = files[0].valid_lines - 1;
309 if (last1 < files[1].valid_lines - context)
312 last1 = files[1].valid_lines - 1;
314 /* If desired, find the preceding function definition line in file 0. */
316 if (function_regexp.fastmap)
317 function = find_function (files[0].linbuf, first0);
322 fprintf (out, "@@ -");
323 print_unidiff_number_range (&files[0], first0, last0);
325 print_unidiff_number_range (&files[1], first1, last1);
326 fprintf (out, " @@");
329 print_context_function (out, function);
337 while (i <= last0 || j <= last1)
340 /* If the line isn't a difference, output the context from file 0. */
342 if (!next || i < next->line0)
344 putc (initial_tab ? '\t' : ' ', out);
345 print_1_line (0, &files[0].linbuf[i++]);
350 /* For each difference, first output the deleted part. */
358 print_1_line (0, &files[0].linbuf[i++]);
361 /* Then output the inserted part. */
369 print_1_line (0, &files[1].linbuf[j++]);
372 /* We're done with this hunk, so on to the next! */
379 /* Scan a (forward-ordered) edit script for the first place that more than
380 2*CONTEXT unchanged lines appear, and return a pointer
381 to the `struct change' for the last change before those lines. */
383 static struct change *
384 find_hunk (struct change *start)
390 /* Threshold distance is 2 * CONTEXT + 1 between two non-ignorable
391 changes, but only CONTEXT if one is ignorable. Watch out for
392 integer overflow, though. */
393 lin non_ignorable_threshold =
394 (LIN_MAX - 1) / 2 < context ? LIN_MAX : 2 * context + 1;
395 lin ignorable_threshold = context;
399 /* Compute number of first line in each file beyond this changed. */
400 top0 = start->line0 + start->deleted;
401 top1 = start->line1 + start->inserted;
404 thresh = (prev->ignore || (start && start->ignore)
405 ? ignorable_threshold
406 : non_ignorable_threshold);
407 /* It is not supposed to matter which file we check in the end-test.
408 If it would matter, crash. */
409 if (start && start->line0 - top0 != start->line1 - top1)
412 /* Keep going if less than THRESH lines
413 elapse before the affected line. */
414 && start->line0 - top0 < thresh);
419 /* Set the `ignore' flag properly in each change in SCRIPT.
420 It should be 1 if all the lines inserted or deleted in that change
421 are ignorable lines. */
424 mark_ignorable (struct change *script)
428 struct change *next = script->link;
429 lin first0, last0, first1, last1;
431 /* Turn this change into a hunk: detach it from the others. */
434 /* Determine whether this change is ignorable. */
435 script->ignore = ! analyze_hunk (script,
436 &first0, &last0, &first1, &last1);
438 /* Reconnect the chain as before. */
441 /* Advance to the following change. */
446 /* Find the last function-header line in LINBUF prior to line number LINENUM.
447 This is a line containing a match for the regexp in `function_regexp'.
448 Return the address of the text, or 0 if no function-header is found. */
451 find_function (char const * const *linbuf, lin linenum)
454 lin last = find_function_last_search;
455 find_function_last_search = i;
459 /* See if this line is what we want. */
460 char const *line = linbuf[i];
461 size_t linelen = linbuf[i + 1] - line - 1;
463 /* FIXME: re_search's size args should be size_t, not int. */
464 int len = MIN (linelen, INT_MAX);
466 if (0 <= re_search (&function_regexp, line, len, 0, len, 0))
468 find_function_last_match = i;
472 /* If we search back to where we started searching the previous time,
473 find the line we found last time. */
474 if (find_function_last_match != LIN_MAX)
475 return linbuf[find_function_last_match];