1 /* Context-format output routines for GNU DIFF.
2 Copyright (C) 1988,1989,1991,1992,1993,1994,1998 Free Software Foundation, Inc.
4 This file is part of GNU DIFF.
6 GNU DIFF is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU DIFF is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
20 static struct change *find_hunk PARAMS((struct change *));
21 static void find_function PARAMS((struct file_data const *, int, char const **, size_t *));
22 static void mark_ignorable PARAMS((struct change *));
23 static void pr_context_hunk PARAMS((struct change *));
24 static void pr_unidiff_hunk PARAMS((struct change *));
25 static void print_context_label PARAMS ((char const *, struct file_data *, char const *));
26 static void print_context_number_range PARAMS((struct file_data const *, int, int));
27 static void print_unidiff_number_range PARAMS((struct file_data const *, int, int));
29 /* Last place find_function started searching from. */
30 static int find_function_last_search;
32 /* The value find_function returned when it started searching there. */
33 static int find_function_last_match;
35 /* Print a label for a context diff, with a file name and date or a label. */
38 print_context_label (mark, inf, label)
40 struct file_data *inf;
44 printf_output ("%s %s\n", mark, label);
47 char const *ct = ctime (&inf->stat.st_mtime);
50 /* See Posix.2 section 4.17.6.1.4 for this format. */
51 printf_output ("%s %s\t%s", mark, inf->name, ct);
55 /* Print a header for a context diff, with the file names and dates. */
58 print_context_header (inf, unidiff_flag)
59 struct file_data inf[];
64 print_context_label ("---", &inf[0], file_label[0]);
65 print_context_label ("+++", &inf[1], file_label[1]);
69 print_context_label ("***", &inf[0], file_label[0]);
70 print_context_label ("---", &inf[1], file_label[1]);
74 /* Print an edit script in context format. */
77 print_context_script (script, unidiff_flag)
78 struct change *script;
81 if (ignore_blank_lines_flag || ignore_regexp_list)
82 mark_ignorable (script);
86 for (e = script; e; e = e->link)
90 find_function_last_search = - files[0].prefix_lines;
91 find_function_last_match = find_function_last_search - 1;
94 print_script (script, find_hunk, pr_unidiff_hunk);
96 print_script (script, find_hunk, pr_context_hunk);
99 /* Print a pair of line numbers with a comma, translated for file FILE.
100 If the second number is not greater, use the first in place of it.
102 Args A and B are internal line numbers.
103 We print the translated (real) line numbers. */
106 print_context_number_range (file, a, b)
107 struct file_data const *file;
110 int trans_a, trans_b;
111 translate_range (file, a, b, &trans_a, &trans_b);
113 /* Note: we can have B < A in the case of a range of no lines.
114 In this case, we should print the line number before the range,
116 if (trans_b > trans_a)
117 printf_output ("%d,%d", trans_a, trans_b);
119 printf_output ("%d", trans_b);
122 /* Print a portion of an edit script in context format.
123 HUNK is the beginning of the portion to be printed.
124 The end is marked by a `link' that has been nulled out.
126 Prints out lines from both files, and precedes each
127 line with the appropriate flag-character. */
130 pr_context_hunk (hunk)
133 int first0, last0, first1, last1, show_from, show_to, i;
136 char const *function;
137 size_t function_length;
139 /* Determine range of line numbers involved in each file. */
141 analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
143 if (!show_from && !show_to)
146 /* Include a context's width before and after. */
148 i = - files[0].prefix_lines;
149 first0 = max (first0 - context, i);
150 first1 = max (first1 - context, i);
151 last0 = min (last0 + context, files[0].valid_lines - 1);
152 last1 = min (last1 + context, files[1].valid_lines - 1);
154 /* If desired, find the preceding function definition line in file 0. */
156 if (function_regexp_list)
157 find_function (&files[0], first0, &function, &function_length);
161 /* If we looked for and found a function this is part of,
162 include its name in the header of the diff section. */
163 printf_output ("***************");
168 write_output (function, min (function_length - 1, 40));
171 printf_output ("\n*** ");
172 print_context_number_range (&files[0], first0, last0);
173 printf_output (" ****\n");
179 for (i = first0; i <= last0; i++)
181 /* Skip past changes that apply (in file 0)
182 only to lines before line I. */
184 while (next && next->line0 + next->deleted <= i)
187 /* Compute the marking for line I. */
190 if (next && next->line0 <= i)
191 /* The change NEXT covers this line.
192 If lines were inserted here in file 1, this is "changed".
193 Otherwise it is "deleted". */
194 prefix = (next->inserted > 0 ? "!" : "-");
196 print_1_line (prefix, &files[0].linbuf[i]);
200 printf_output ("--- ");
201 print_context_number_range (&files[1], first1, last1);
202 printf_output (" ----\n");
208 for (i = first1; i <= last1; i++)
210 /* Skip past changes that apply (in file 1)
211 only to lines before line I. */
213 while (next && next->line1 + next->inserted <= i)
216 /* Compute the marking for line I. */
219 if (next && next->line1 <= i)
220 /* The change NEXT covers this line.
221 If lines were deleted here in file 0, this is "changed".
222 Otherwise it is "inserted". */
223 prefix = (next->deleted > 0 ? "!" : "+");
225 print_1_line (prefix, &files[1].linbuf[i]);
230 /* Print a pair of line numbers with a comma, translated for file FILE.
231 If the second number is smaller, use the first in place of it.
232 If the numbers are equal, print just one number.
234 Args A and B are internal line numbers.
235 We print the translated (real) line numbers. */
238 print_unidiff_number_range (file, a, b)
239 struct file_data const *file;
242 int trans_a, trans_b;
243 translate_range (file, a, b, &trans_a, &trans_b);
245 /* Note: we can have B < A in the case of a range of no lines.
246 In this case, we should print the line number before the range,
248 if (trans_b <= trans_a)
249 printf_output (trans_b == trans_a ? "%d" : "%d,0", trans_b);
251 printf_output ("%d,%d", trans_a, trans_b - trans_a + 1);
254 /* Print a portion of an edit script in unidiff format.
255 HUNK is the beginning of the portion to be printed.
256 The end is marked by a `link' that has been nulled out.
258 Prints out lines from both files, and precedes each
259 line with the appropriate flag-character. */
262 pr_unidiff_hunk (hunk)
265 int first0, last0, first1, last1, show_from, show_to, i, j, k;
267 char const *function;
268 size_t function_length;
270 /* Determine range of line numbers involved in each file. */
272 analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
274 if (!show_from && !show_to)
277 /* Include a context's width before and after. */
279 i = - files[0].prefix_lines;
280 first0 = max (first0 - context, i);
281 first1 = max (first1 - context, i);
282 last0 = min (last0 + context, files[0].valid_lines - 1);
283 last1 = min (last1 + context, files[1].valid_lines - 1);
285 /* If desired, find the preceding function definition line in file 0. */
287 if (function_regexp_list)
288 find_function (&files[0], first0, &function, &function_length);
292 printf_output ("@@ -");
293 print_unidiff_number_range (&files[0], first0, last0);
294 printf_output (" +");
295 print_unidiff_number_range (&files[1], first1, last1);
296 printf_output (" @@");
298 /* If we looked for and found a function this is part of,
299 include its name in the header of the diff section. */
303 write_output (" ", 1);
304 write_output (function, min (function_length - 1, 40));
306 write_output ("\n", 1);
312 while (i <= last0 || j <= last1)
315 /* If the line isn't a difference, output the context from file 0. */
317 if (!next || i < next->line0)
319 write_output (tab_align_flag ? "\t" : " ", 1);
320 print_1_line (0, &files[0].linbuf[i++]);
325 /* For each difference, first output the deleted part. */
330 write_output ("-", 1);
332 write_output ("\t", 1);
333 print_1_line (0, &files[0].linbuf[i++]);
336 /* Then output the inserted part. */
341 write_output ("+", 1);
343 write_output ("\t", 1);
344 print_1_line (0, &files[1].linbuf[j++]);
347 /* We're done with this hunk, so on to the next! */
354 /* Scan a (forward-ordered) edit script for the first place that more than
355 2*CONTEXT unchanged lines appear, and return a pointer
356 to the `struct change' for the last change before those lines. */
358 static struct change *
360 struct change *start;
368 /* Compute number of first line in each file beyond this changed. */
369 top0 = start->line0 + start->deleted;
370 top1 = start->line1 + start->inserted;
373 /* Threshold distance is 2*CONTEXT between two non-ignorable changes,
374 but only CONTEXT if one is ignorable. */
375 thresh = ((prev->ignore || (start && start->ignore))
378 /* It is not supposed to matter which file we check in the end-test.
379 If it would matter, crash. */
380 if (start && start->line0 - top0 != start->line1 - top1)
383 /* Keep going if less than THRESH lines
384 elapse before the affected line. */
385 && start->line0 < top0 + thresh);
390 /* Set the `ignore' flag properly in each change in SCRIPT.
391 It should be 1 if all the lines inserted or deleted in that change
392 are ignorable lines. */
395 mark_ignorable (script)
396 struct change *script;
400 struct change *next = script->link;
401 int first0, last0, first1, last1, deletes, inserts;
403 /* Turn this change into a hunk: detach it from the others. */
406 /* Determine whether this change is ignorable. */
407 analyze_hunk (script, &first0, &last0, &first1, &last1, &deletes, &inserts);
408 /* Reconnect the chain as before. */
411 /* If the change is ignorable, mark it. */
412 script->ignore = (!deletes && !inserts);
414 /* Advance to the following change. */
419 /* Find the last function-header line in FILE prior to line number LINENUM.
420 This is a line containing a match for the regexp in `function_regexp'.
421 Store the address of the line text into LINEP and the length of the
423 Do not store anything if no function-header is found. */
426 find_function (file, linenum, linep, lenp)
427 struct file_data const *file;
433 int last = find_function_last_search;
434 find_function_last_search = i;
438 /* See if this line is what we want. */
439 struct regexp_list *r;
440 char const *line = file->linbuf[i];
441 size_t len = file->linbuf[i + 1] - line;
443 for (r = function_regexp_list; r; r = r->next)
444 if (0 <= re_search (&r->buf, line, len, 0, len, 0))
448 find_function_last_match = i;
452 /* If we search back to where we started searching the previous time,
453 find the line we found last time. */
454 if (find_function_last_match >= - file->prefix_lines)
456 i = find_function_last_match;
457 *linep = file->linbuf[i];
458 *lenp = file->linbuf[i + 1] - *linep;