1 /* Context-format output routines for GNU DIFF.
2 Copyright (C) 1988,1989,1991,1992,1993,1994 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.
16 You should have received a copy of the GNU General Public License
17 along with GNU DIFF; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 static struct change *find_hunk PARAMS((struct change *));
23 static void find_function PARAMS((struct file_data const *, int, char const **, size_t *));
24 static void mark_ignorable PARAMS((struct change *));
25 static void pr_context_hunk PARAMS((struct change *));
26 static void pr_unidiff_hunk PARAMS((struct change *));
27 static void print_context_label PARAMS ((char const *, struct file_data *, char const *));
28 static void print_context_number_range PARAMS((struct file_data const *, int, int));
29 static void print_unidiff_number_range PARAMS((struct file_data const *, int, int));
31 /* Last place find_function started searching from. */
32 static int find_function_last_search;
34 /* The value find_function returned when it started searching there. */
35 static int find_function_last_match;
37 /* Print a label for a context diff, with a file name and date or a label. */
40 print_context_label (mark, inf, label)
42 struct file_data *inf;
46 fprintf (outfile, "%s %s\n", mark, label);
49 char const *ct = ctime (&inf->stat.st_mtime);
52 /* See Posix.2 section 4.17.6.1.4 for this format. */
53 fprintf (outfile, "%s %s\t%s", mark, inf->name, ct);
57 /* Print a header for a context diff, with the file names and dates. */
60 print_context_header (inf, unidiff_flag)
61 struct file_data inf[];
66 print_context_label ("---", &inf[0], file_label[0]);
67 print_context_label ("+++", &inf[1], file_label[1]);
71 print_context_label ("***", &inf[0], file_label[0]);
72 print_context_label ("---", &inf[1], file_label[1]);
76 /* Print an edit script in context format. */
79 print_context_script (script, unidiff_flag)
80 struct change *script;
83 if (ignore_blank_lines_flag || ignore_regexp_list)
84 mark_ignorable (script);
88 for (e = script; e; e = e->link)
92 find_function_last_search = - files[0].prefix_lines;
93 find_function_last_match = find_function_last_search - 1;
96 print_script (script, find_hunk, pr_unidiff_hunk);
98 print_script (script, find_hunk, pr_context_hunk);
101 /* Print a pair of line numbers with a comma, translated for file FILE.
102 If the second number is not greater, use the first in place of it.
104 Args A and B are internal line numbers.
105 We print the translated (real) line numbers. */
108 print_context_number_range (file, a, b)
109 struct file_data const *file;
112 int trans_a, trans_b;
113 translate_range (file, a, b, &trans_a, &trans_b);
115 /* Note: we can have B < A in the case of a range of no lines.
116 In this case, we should print the line number before the range,
118 if (trans_b > trans_a)
119 fprintf (outfile, "%d,%d", trans_a, trans_b);
121 fprintf (outfile, "%d", trans_b);
124 /* Print a portion of an edit script in context format.
125 HUNK is the beginning of the portion to be printed.
126 The end is marked by a `link' that has been nulled out.
128 Prints out lines from both files, and precedes each
129 line with the appropriate flag-character. */
132 pr_context_hunk (hunk)
135 int first0, last0, first1, last1, show_from, show_to, i;
138 char const *function;
139 size_t function_length;
142 /* Determine range of line numbers involved in each file. */
144 analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
146 if (!show_from && !show_to)
149 /* Include a context's width before and after. */
151 i = - files[0].prefix_lines;
152 first0 = max (first0 - context, i);
153 first1 = max (first1 - context, i);
154 last0 = min (last0 + context, files[0].valid_lines - 1);
155 last1 = min (last1 + context, files[1].valid_lines - 1);
157 /* If desired, find the preceding function definition line in file 0. */
159 if (function_regexp_list)
160 find_function (&files[0], first0, &function, &function_length);
165 /* If we looked for and found a function this is part of,
166 include its name in the header of the diff section. */
167 fprintf (out, "***************");
172 fwrite (function, 1, min (function_length - 1, 40), out);
175 fprintf (out, "\n*** ");
176 print_context_number_range (&files[0], first0, last0);
177 fprintf (out, " ****\n");
183 for (i = first0; i <= last0; i++)
185 /* Skip past changes that apply (in file 0)
186 only to lines before line I. */
188 while (next && next->line0 + next->deleted <= i)
191 /* Compute the marking for line I. */
194 if (next && next->line0 <= i)
195 /* The change NEXT covers this line.
196 If lines were inserted here in file 1, this is "changed".
197 Otherwise it is "deleted". */
198 prefix = (next->inserted > 0 ? "!" : "-");
200 print_1_line (prefix, &files[0].linbuf[i]);
204 fprintf (out, "--- ");
205 print_context_number_range (&files[1], first1, last1);
206 fprintf (out, " ----\n");
212 for (i = first1; i <= last1; i++)
214 /* Skip past changes that apply (in file 1)
215 only to lines before line I. */
217 while (next && next->line1 + next->inserted <= i)
220 /* Compute the marking for line I. */
223 if (next && next->line1 <= i)
224 /* The change NEXT covers this line.
225 If lines were deleted here in file 0, this is "changed".
226 Otherwise it is "inserted". */
227 prefix = (next->deleted > 0 ? "!" : "+");
229 print_1_line (prefix, &files[1].linbuf[i]);
234 /* Print a pair of line numbers with a comma, translated for file FILE.
235 If the second number is smaller, use the first in place of it.
236 If the numbers are equal, print just one number.
238 Args A and B are internal line numbers.
239 We print the translated (real) line numbers. */
242 print_unidiff_number_range (file, a, b)
243 struct file_data const *file;
246 int trans_a, trans_b;
247 translate_range (file, a, b, &trans_a, &trans_b);
249 /* Note: we can have B < A in the case of a range of no lines.
250 In this case, we should print the line number before the range,
252 if (trans_b <= trans_a)
253 fprintf (outfile, trans_b == trans_a ? "%d" : "%d,0", trans_b);
255 fprintf (outfile, "%d,%d", trans_a, trans_b - trans_a + 1);
258 /* Print a portion of an edit script in unidiff format.
259 HUNK is the beginning of the portion to be printed.
260 The end is marked by a `link' that has been nulled out.
262 Prints out lines from both files, and precedes each
263 line with the appropriate flag-character. */
266 pr_unidiff_hunk (hunk)
269 int first0, last0, first1, last1, show_from, show_to, i, j, k;
271 char const *function;
272 size_t function_length;
275 /* Determine range of line numbers involved in each file. */
277 analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
279 if (!show_from && !show_to)
282 /* Include a context's width before and after. */
284 i = - files[0].prefix_lines;
285 first0 = max (first0 - context, i);
286 first1 = max (first1 - context, i);
287 last0 = min (last0 + context, files[0].valid_lines - 1);
288 last1 = min (last1 + context, files[1].valid_lines - 1);
290 /* If desired, find the preceding function definition line in file 0. */
292 if (function_regexp_list)
293 find_function (&files[0], first0, &function, &function_length);
298 fprintf (out, "@@ -");
299 print_unidiff_number_range (&files[0], first0, last0);
301 print_unidiff_number_range (&files[1], first1, last1);
302 fprintf (out, " @@");
304 /* If we looked for and found a function this is part of,
305 include its name in the header of the diff section. */
310 fwrite (function, 1, min (function_length - 1, 40), out);
318 while (i <= last0 || j <= last1)
321 /* If the line isn't a difference, output the context from file 0. */
323 if (!next || i < next->line0)
325 putc (tab_align_flag ? '\t' : ' ', out);
326 print_1_line (0, &files[0].linbuf[i++]);
331 /* For each difference, first output the deleted part. */
339 print_1_line (0, &files[0].linbuf[i++]);
342 /* Then output the inserted part. */
350 print_1_line (0, &files[1].linbuf[j++]);
353 /* We're done with this hunk, so on to the next! */
360 /* Scan a (forward-ordered) edit script for the first place that more than
361 2*CONTEXT unchanged lines appear, and return a pointer
362 to the `struct change' for the last change before those lines. */
364 static struct change *
366 struct change *start;
374 /* Compute number of first line in each file beyond this changed. */
375 top0 = start->line0 + start->deleted;
376 top1 = start->line1 + start->inserted;
379 /* Threshold distance is 2*CONTEXT between two non-ignorable changes,
380 but only CONTEXT if one is ignorable. */
381 thresh = ((prev->ignore || (start && start->ignore))
384 /* It is not supposed to matter which file we check in the end-test.
385 If it would matter, crash. */
386 if (start && start->line0 - top0 != start->line1 - top1)
389 /* Keep going if less than THRESH lines
390 elapse before the affected line. */
391 && start->line0 < top0 + thresh);
396 /* Set the `ignore' flag properly in each change in SCRIPT.
397 It should be 1 if all the lines inserted or deleted in that change
398 are ignorable lines. */
401 mark_ignorable (script)
402 struct change *script;
406 struct change *next = script->link;
407 int first0, last0, first1, last1, deletes, inserts;
409 /* Turn this change into a hunk: detach it from the others. */
412 /* Determine whether this change is ignorable. */
413 analyze_hunk (script, &first0, &last0, &first1, &last1, &deletes, &inserts);
414 /* Reconnect the chain as before. */
417 /* If the change is ignorable, mark it. */
418 script->ignore = (!deletes && !inserts);
420 /* Advance to the following change. */
425 /* Find the last function-header line in FILE prior to line number LINENUM.
426 This is a line containing a match for the regexp in `function_regexp'.
427 Store the address of the line text into LINEP and the length of the
429 Do not store anything if no function-header is found. */
432 find_function (file, linenum, linep, lenp)
433 struct file_data const *file;
439 int last = find_function_last_search;
440 find_function_last_search = i;
444 /* See if this line is what we want. */
445 struct regexp_list *r;
446 char const *line = file->linbuf[i];
447 size_t len = file->linbuf[i + 1] - line;
449 for (r = function_regexp_list; r; r = r->next)
450 if (0 <= re_search (&r->buf, line, len, 0, len, 0))
454 find_function_last_match = i;
458 /* If we search back to where we started searching the previous time,
459 find the line we found last time. */
460 if (find_function_last_match >= - file->prefix_lines)
462 i = find_function_last_match;
463 *linep = file->linbuf[i];
464 *lenp = file->linbuf[i + 1] - *linep;