1*a7c91847Schristos /* Context-format output routines for GNU DIFF.
2*a7c91847Schristos Copyright (C) 1988,1989,1991,1992,1993,1994,1998 Free Software Foundation, Inc.
3*a7c91847Schristos
4*a7c91847Schristos This file is part of GNU DIFF.
5*a7c91847Schristos
6*a7c91847Schristos GNU DIFF is free software; you can redistribute it and/or modify
7*a7c91847Schristos it under the terms of the GNU General Public License as published by
8*a7c91847Schristos the Free Software Foundation; either version 2, or (at your option)
9*a7c91847Schristos any later version.
10*a7c91847Schristos
11*a7c91847Schristos GNU DIFF is distributed in the hope that it will be useful,
12*a7c91847Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
13*a7c91847Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*a7c91847Schristos GNU General Public License for more details.
15*a7c91847Schristos
16*a7c91847Schristos */
17*a7c91847Schristos
18*a7c91847Schristos #include "diff.h"
19*a7c91847Schristos
20*a7c91847Schristos static struct change *find_hunk PARAMS((struct change *));
21*a7c91847Schristos static void find_function PARAMS((struct file_data const *, int, char const **, size_t *));
22*a7c91847Schristos static void mark_ignorable PARAMS((struct change *));
23*a7c91847Schristos static void pr_context_hunk PARAMS((struct change *));
24*a7c91847Schristos static void pr_unidiff_hunk PARAMS((struct change *));
25*a7c91847Schristos static void print_context_label PARAMS ((char const *, struct file_data *, char const *));
26*a7c91847Schristos static void print_context_number_range PARAMS((struct file_data const *, int, int));
27*a7c91847Schristos static void print_unidiff_number_range PARAMS((struct file_data const *, int, int));
28*a7c91847Schristos
29*a7c91847Schristos /* Last place find_function started searching from. */
30*a7c91847Schristos static int find_function_last_search;
31*a7c91847Schristos
32*a7c91847Schristos /* The value find_function returned when it started searching there. */
33*a7c91847Schristos static int find_function_last_match;
34*a7c91847Schristos
35*a7c91847Schristos /* Print a label for a context diff, with a file name and date or a label. */
36*a7c91847Schristos
37*a7c91847Schristos static void
print_context_label(mark,inf,label)38*a7c91847Schristos print_context_label (mark, inf, label)
39*a7c91847Schristos char const *mark;
40*a7c91847Schristos struct file_data *inf;
41*a7c91847Schristos char const *label;
42*a7c91847Schristos {
43*a7c91847Schristos if (label)
44*a7c91847Schristos printf_output ("%s %s\n", mark, label);
45*a7c91847Schristos else
46*a7c91847Schristos {
47*a7c91847Schristos char const *ct = ctime (&inf->stat.st_mtime);
48*a7c91847Schristos if (!ct)
49*a7c91847Schristos ct = "?\n";
50*a7c91847Schristos /* See Posix.2 section 4.17.6.1.4 for this format. */
51*a7c91847Schristos printf_output ("%s %s\t%s", mark, inf->name, ct);
52*a7c91847Schristos }
53*a7c91847Schristos }
54*a7c91847Schristos
55*a7c91847Schristos /* Print a header for a context diff, with the file names and dates. */
56*a7c91847Schristos
57*a7c91847Schristos void
print_context_header(inf,unidiff_flag)58*a7c91847Schristos print_context_header (inf, unidiff_flag)
59*a7c91847Schristos struct file_data inf[];
60*a7c91847Schristos int unidiff_flag;
61*a7c91847Schristos {
62*a7c91847Schristos if (unidiff_flag)
63*a7c91847Schristos {
64*a7c91847Schristos print_context_label ("---", &inf[0], file_label[0]);
65*a7c91847Schristos print_context_label ("+++", &inf[1], file_label[1]);
66*a7c91847Schristos }
67*a7c91847Schristos else
68*a7c91847Schristos {
69*a7c91847Schristos print_context_label ("***", &inf[0], file_label[0]);
70*a7c91847Schristos print_context_label ("---", &inf[1], file_label[1]);
71*a7c91847Schristos }
72*a7c91847Schristos }
73*a7c91847Schristos
74*a7c91847Schristos /* Print an edit script in context format. */
75*a7c91847Schristos
76*a7c91847Schristos void
print_context_script(script,unidiff_flag)77*a7c91847Schristos print_context_script (script, unidiff_flag)
78*a7c91847Schristos struct change *script;
79*a7c91847Schristos int unidiff_flag;
80*a7c91847Schristos {
81*a7c91847Schristos if (ignore_blank_lines_flag || ignore_regexp_list)
82*a7c91847Schristos mark_ignorable (script);
83*a7c91847Schristos else
84*a7c91847Schristos {
85*a7c91847Schristos struct change *e;
86*a7c91847Schristos for (e = script; e; e = e->link)
87*a7c91847Schristos e->ignore = 0;
88*a7c91847Schristos }
89*a7c91847Schristos
90*a7c91847Schristos find_function_last_search = - files[0].prefix_lines;
91*a7c91847Schristos find_function_last_match = find_function_last_search - 1;
92*a7c91847Schristos
93*a7c91847Schristos if (unidiff_flag)
94*a7c91847Schristos print_script (script, find_hunk, pr_unidiff_hunk);
95*a7c91847Schristos else
96*a7c91847Schristos print_script (script, find_hunk, pr_context_hunk);
97*a7c91847Schristos }
98*a7c91847Schristos
99*a7c91847Schristos /* Print a pair of line numbers with a comma, translated for file FILE.
100*a7c91847Schristos If the second number is not greater, use the first in place of it.
101*a7c91847Schristos
102*a7c91847Schristos Args A and B are internal line numbers.
103*a7c91847Schristos We print the translated (real) line numbers. */
104*a7c91847Schristos
105*a7c91847Schristos static void
print_context_number_range(file,a,b)106*a7c91847Schristos print_context_number_range (file, a, b)
107*a7c91847Schristos struct file_data const *file;
108*a7c91847Schristos int a, b;
109*a7c91847Schristos {
110*a7c91847Schristos int trans_a, trans_b;
111*a7c91847Schristos translate_range (file, a, b, &trans_a, &trans_b);
112*a7c91847Schristos
113*a7c91847Schristos /* Note: we can have B < A in the case of a range of no lines.
114*a7c91847Schristos In this case, we should print the line number before the range,
115*a7c91847Schristos which is B. */
116*a7c91847Schristos if (trans_b > trans_a)
117*a7c91847Schristos printf_output ("%d,%d", trans_a, trans_b);
118*a7c91847Schristos else
119*a7c91847Schristos printf_output ("%d", trans_b);
120*a7c91847Schristos }
121*a7c91847Schristos
122*a7c91847Schristos /* Print a portion of an edit script in context format.
123*a7c91847Schristos HUNK is the beginning of the portion to be printed.
124*a7c91847Schristos The end is marked by a `link' that has been nulled out.
125*a7c91847Schristos
126*a7c91847Schristos Prints out lines from both files, and precedes each
127*a7c91847Schristos line with the appropriate flag-character. */
128*a7c91847Schristos
129*a7c91847Schristos static void
pr_context_hunk(hunk)130*a7c91847Schristos pr_context_hunk (hunk)
131*a7c91847Schristos struct change *hunk;
132*a7c91847Schristos {
133*a7c91847Schristos int first0, last0, first1, last1, show_from, show_to, i;
134*a7c91847Schristos struct change *next;
135*a7c91847Schristos char const *prefix;
136*a7c91847Schristos char const *function;
137*a7c91847Schristos size_t function_length;
138*a7c91847Schristos
139*a7c91847Schristos /* Determine range of line numbers involved in each file. */
140*a7c91847Schristos
141*a7c91847Schristos analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
142*a7c91847Schristos
143*a7c91847Schristos if (!show_from && !show_to)
144*a7c91847Schristos return;
145*a7c91847Schristos
146*a7c91847Schristos /* Include a context's width before and after. */
147*a7c91847Schristos
148*a7c91847Schristos i = - files[0].prefix_lines;
149*a7c91847Schristos first0 = max (first0 - context, i);
150*a7c91847Schristos first1 = max (first1 - context, i);
151*a7c91847Schristos last0 = min (last0 + context, files[0].valid_lines - 1);
152*a7c91847Schristos last1 = min (last1 + context, files[1].valid_lines - 1);
153*a7c91847Schristos
154*a7c91847Schristos /* If desired, find the preceding function definition line in file 0. */
155*a7c91847Schristos function = 0;
156*a7c91847Schristos if (function_regexp_list)
157*a7c91847Schristos find_function (&files[0], first0, &function, &function_length);
158*a7c91847Schristos
159*a7c91847Schristos begin_output ();
160*a7c91847Schristos
161*a7c91847Schristos /* If we looked for and found a function this is part of,
162*a7c91847Schristos include its name in the header of the diff section. */
163*a7c91847Schristos printf_output ("***************");
164*a7c91847Schristos
165*a7c91847Schristos if (function)
166*a7c91847Schristos {
167*a7c91847Schristos printf_output (" ");
168*a7c91847Schristos write_output (function, min (function_length - 1, 40));
169*a7c91847Schristos }
170*a7c91847Schristos
171*a7c91847Schristos printf_output ("\n*** ");
172*a7c91847Schristos print_context_number_range (&files[0], first0, last0);
173*a7c91847Schristos printf_output (" ****\n");
174*a7c91847Schristos
175*a7c91847Schristos if (show_from)
176*a7c91847Schristos {
177*a7c91847Schristos next = hunk;
178*a7c91847Schristos
179*a7c91847Schristos for (i = first0; i <= last0; i++)
180*a7c91847Schristos {
181*a7c91847Schristos /* Skip past changes that apply (in file 0)
182*a7c91847Schristos only to lines before line I. */
183*a7c91847Schristos
184*a7c91847Schristos while (next && next->line0 + next->deleted <= i)
185*a7c91847Schristos next = next->link;
186*a7c91847Schristos
187*a7c91847Schristos /* Compute the marking for line I. */
188*a7c91847Schristos
189*a7c91847Schristos prefix = " ";
190*a7c91847Schristos if (next && next->line0 <= i)
191*a7c91847Schristos /* The change NEXT covers this line.
192*a7c91847Schristos If lines were inserted here in file 1, this is "changed".
193*a7c91847Schristos Otherwise it is "deleted". */
194*a7c91847Schristos prefix = (next->inserted > 0 ? "!" : "-");
195*a7c91847Schristos
196*a7c91847Schristos print_1_line (prefix, &files[0].linbuf[i]);
197*a7c91847Schristos }
198*a7c91847Schristos }
199*a7c91847Schristos
200*a7c91847Schristos printf_output ("--- ");
201*a7c91847Schristos print_context_number_range (&files[1], first1, last1);
202*a7c91847Schristos printf_output (" ----\n");
203*a7c91847Schristos
204*a7c91847Schristos if (show_to)
205*a7c91847Schristos {
206*a7c91847Schristos next = hunk;
207*a7c91847Schristos
208*a7c91847Schristos for (i = first1; i <= last1; i++)
209*a7c91847Schristos {
210*a7c91847Schristos /* Skip past changes that apply (in file 1)
211*a7c91847Schristos only to lines before line I. */
212*a7c91847Schristos
213*a7c91847Schristos while (next && next->line1 + next->inserted <= i)
214*a7c91847Schristos next = next->link;
215*a7c91847Schristos
216*a7c91847Schristos /* Compute the marking for line I. */
217*a7c91847Schristos
218*a7c91847Schristos prefix = " ";
219*a7c91847Schristos if (next && next->line1 <= i)
220*a7c91847Schristos /* The change NEXT covers this line.
221*a7c91847Schristos If lines were deleted here in file 0, this is "changed".
222*a7c91847Schristos Otherwise it is "inserted". */
223*a7c91847Schristos prefix = (next->deleted > 0 ? "!" : "+");
224*a7c91847Schristos
225*a7c91847Schristos print_1_line (prefix, &files[1].linbuf[i]);
226*a7c91847Schristos }
227*a7c91847Schristos }
228*a7c91847Schristos }
229*a7c91847Schristos
230*a7c91847Schristos /* Print a pair of line numbers with a comma, translated for file FILE.
231*a7c91847Schristos If the second number is smaller, use the first in place of it.
232*a7c91847Schristos If the numbers are equal, print just one number.
233*a7c91847Schristos
234*a7c91847Schristos Args A and B are internal line numbers.
235*a7c91847Schristos We print the translated (real) line numbers. */
236*a7c91847Schristos
237*a7c91847Schristos static void
print_unidiff_number_range(file,a,b)238*a7c91847Schristos print_unidiff_number_range (file, a, b)
239*a7c91847Schristos struct file_data const *file;
240*a7c91847Schristos int a, b;
241*a7c91847Schristos {
242*a7c91847Schristos int trans_a, trans_b;
243*a7c91847Schristos translate_range (file, a, b, &trans_a, &trans_b);
244*a7c91847Schristos
245*a7c91847Schristos /* Note: we can have B < A in the case of a range of no lines.
246*a7c91847Schristos In this case, we should print the line number before the range,
247*a7c91847Schristos which is B. */
248*a7c91847Schristos if (trans_b <= trans_a)
249*a7c91847Schristos printf_output (trans_b == trans_a ? "%d" : "%d,0", trans_b);
250*a7c91847Schristos else
251*a7c91847Schristos printf_output ("%d,%d", trans_a, trans_b - trans_a + 1);
252*a7c91847Schristos }
253*a7c91847Schristos
254*a7c91847Schristos /* Print a portion of an edit script in unidiff format.
255*a7c91847Schristos HUNK is the beginning of the portion to be printed.
256*a7c91847Schristos The end is marked by a `link' that has been nulled out.
257*a7c91847Schristos
258*a7c91847Schristos Prints out lines from both files, and precedes each
259*a7c91847Schristos line with the appropriate flag-character. */
260*a7c91847Schristos
261*a7c91847Schristos static void
pr_unidiff_hunk(hunk)262*a7c91847Schristos pr_unidiff_hunk (hunk)
263*a7c91847Schristos struct change *hunk;
264*a7c91847Schristos {
265*a7c91847Schristos int first0, last0, first1, last1, show_from, show_to, i, j, k;
266*a7c91847Schristos struct change *next;
267*a7c91847Schristos char const *function;
268*a7c91847Schristos size_t function_length;
269*a7c91847Schristos
270*a7c91847Schristos /* Determine range of line numbers involved in each file. */
271*a7c91847Schristos
272*a7c91847Schristos analyze_hunk (hunk, &first0, &last0, &first1, &last1, &show_from, &show_to);
273*a7c91847Schristos
274*a7c91847Schristos if (!show_from && !show_to)
275*a7c91847Schristos return;
276*a7c91847Schristos
277*a7c91847Schristos /* Include a context's width before and after. */
278*a7c91847Schristos
279*a7c91847Schristos i = - files[0].prefix_lines;
280*a7c91847Schristos first0 = max (first0 - context, i);
281*a7c91847Schristos first1 = max (first1 - context, i);
282*a7c91847Schristos last0 = min (last0 + context, files[0].valid_lines - 1);
283*a7c91847Schristos last1 = min (last1 + context, files[1].valid_lines - 1);
284*a7c91847Schristos
285*a7c91847Schristos /* If desired, find the preceding function definition line in file 0. */
286*a7c91847Schristos function = 0;
287*a7c91847Schristos if (function_regexp_list)
288*a7c91847Schristos find_function (&files[0], first0, &function, &function_length);
289*a7c91847Schristos
290*a7c91847Schristos begin_output ();
291*a7c91847Schristos
292*a7c91847Schristos printf_output ("@@ -");
293*a7c91847Schristos print_unidiff_number_range (&files[0], first0, last0);
294*a7c91847Schristos printf_output (" +");
295*a7c91847Schristos print_unidiff_number_range (&files[1], first1, last1);
296*a7c91847Schristos printf_output (" @@");
297*a7c91847Schristos
298*a7c91847Schristos /* If we looked for and found a function this is part of,
299*a7c91847Schristos include its name in the header of the diff section. */
300*a7c91847Schristos
301*a7c91847Schristos if (function)
302*a7c91847Schristos {
303*a7c91847Schristos write_output (" ", 1);
304*a7c91847Schristos write_output (function, min (function_length - 1, 40));
305*a7c91847Schristos }
306*a7c91847Schristos write_output ("\n", 1);
307*a7c91847Schristos
308*a7c91847Schristos next = hunk;
309*a7c91847Schristos i = first0;
310*a7c91847Schristos j = first1;
311*a7c91847Schristos
312*a7c91847Schristos while (i <= last0 || j <= last1)
313*a7c91847Schristos {
314*a7c91847Schristos
315*a7c91847Schristos /* If the line isn't a difference, output the context from file 0. */
316*a7c91847Schristos
317*a7c91847Schristos if (!next || i < next->line0)
318*a7c91847Schristos {
319*a7c91847Schristos write_output (tab_align_flag ? "\t" : " ", 1);
320*a7c91847Schristos print_1_line (0, &files[0].linbuf[i++]);
321*a7c91847Schristos j++;
322*a7c91847Schristos }
323*a7c91847Schristos else
324*a7c91847Schristos {
325*a7c91847Schristos /* For each difference, first output the deleted part. */
326*a7c91847Schristos
327*a7c91847Schristos k = next->deleted;
328*a7c91847Schristos while (k--)
329*a7c91847Schristos {
330*a7c91847Schristos write_output ("-", 1);
331*a7c91847Schristos if (tab_align_flag)
332*a7c91847Schristos write_output ("\t", 1);
333*a7c91847Schristos print_1_line (0, &files[0].linbuf[i++]);
334*a7c91847Schristos }
335*a7c91847Schristos
336*a7c91847Schristos /* Then output the inserted part. */
337*a7c91847Schristos
338*a7c91847Schristos k = next->inserted;
339*a7c91847Schristos while (k--)
340*a7c91847Schristos {
341*a7c91847Schristos write_output ("+", 1);
342*a7c91847Schristos if (tab_align_flag)
343*a7c91847Schristos write_output ("\t", 1);
344*a7c91847Schristos print_1_line (0, &files[1].linbuf[j++]);
345*a7c91847Schristos }
346*a7c91847Schristos
347*a7c91847Schristos /* We're done with this hunk, so on to the next! */
348*a7c91847Schristos
349*a7c91847Schristos next = next->link;
350*a7c91847Schristos }
351*a7c91847Schristos }
352*a7c91847Schristos }
353*a7c91847Schristos
354*a7c91847Schristos /* Scan a (forward-ordered) edit script for the first place that more than
355*a7c91847Schristos 2*CONTEXT unchanged lines appear, and return a pointer
356*a7c91847Schristos to the `struct change' for the last change before those lines. */
357*a7c91847Schristos
358*a7c91847Schristos static struct change *
find_hunk(start)359*a7c91847Schristos find_hunk (start)
360*a7c91847Schristos struct change *start;
361*a7c91847Schristos {
362*a7c91847Schristos struct change *prev;
363*a7c91847Schristos int top0, top1;
364*a7c91847Schristos int thresh;
365*a7c91847Schristos
366*a7c91847Schristos do
367*a7c91847Schristos {
368*a7c91847Schristos /* Compute number of first line in each file beyond this changed. */
369*a7c91847Schristos top0 = start->line0 + start->deleted;
370*a7c91847Schristos top1 = start->line1 + start->inserted;
371*a7c91847Schristos prev = start;
372*a7c91847Schristos start = start->link;
373*a7c91847Schristos /* Threshold distance is 2*CONTEXT between two non-ignorable changes,
374*a7c91847Schristos but only CONTEXT if one is ignorable. */
375*a7c91847Schristos thresh = ((prev->ignore || (start && start->ignore))
376*a7c91847Schristos ? context
377*a7c91847Schristos : 2 * context + 1);
378*a7c91847Schristos /* It is not supposed to matter which file we check in the end-test.
379*a7c91847Schristos If it would matter, crash. */
380*a7c91847Schristos if (start && start->line0 - top0 != start->line1 - top1)
381*a7c91847Schristos abort ();
382*a7c91847Schristos } while (start
383*a7c91847Schristos /* Keep going if less than THRESH lines
384*a7c91847Schristos elapse before the affected line. */
385*a7c91847Schristos && start->line0 < top0 + thresh);
386*a7c91847Schristos
387*a7c91847Schristos return prev;
388*a7c91847Schristos }
389*a7c91847Schristos
390*a7c91847Schristos /* Set the `ignore' flag properly in each change in SCRIPT.
391*a7c91847Schristos It should be 1 if all the lines inserted or deleted in that change
392*a7c91847Schristos are ignorable lines. */
393*a7c91847Schristos
394*a7c91847Schristos static void
mark_ignorable(script)395*a7c91847Schristos mark_ignorable (script)
396*a7c91847Schristos struct change *script;
397*a7c91847Schristos {
398*a7c91847Schristos while (script)
399*a7c91847Schristos {
400*a7c91847Schristos struct change *next = script->link;
401*a7c91847Schristos int first0, last0, first1, last1, deletes, inserts;
402*a7c91847Schristos
403*a7c91847Schristos /* Turn this change into a hunk: detach it from the others. */
404*a7c91847Schristos script->link = 0;
405*a7c91847Schristos
406*a7c91847Schristos /* Determine whether this change is ignorable. */
407*a7c91847Schristos analyze_hunk (script, &first0, &last0, &first1, &last1, &deletes, &inserts);
408*a7c91847Schristos /* Reconnect the chain as before. */
409*a7c91847Schristos script->link = next;
410*a7c91847Schristos
411*a7c91847Schristos /* If the change is ignorable, mark it. */
412*a7c91847Schristos script->ignore = (!deletes && !inserts);
413*a7c91847Schristos
414*a7c91847Schristos /* Advance to the following change. */
415*a7c91847Schristos script = next;
416*a7c91847Schristos }
417*a7c91847Schristos }
418*a7c91847Schristos
419*a7c91847Schristos /* Find the last function-header line in FILE prior to line number LINENUM.
420*a7c91847Schristos This is a line containing a match for the regexp in `function_regexp'.
421*a7c91847Schristos Store the address of the line text into LINEP and the length of the
422*a7c91847Schristos line into LENP.
423*a7c91847Schristos Do not store anything if no function-header is found. */
424*a7c91847Schristos
425*a7c91847Schristos static void
find_function(file,linenum,linep,lenp)426*a7c91847Schristos find_function (file, linenum, linep, lenp)
427*a7c91847Schristos struct file_data const *file;
428*a7c91847Schristos int linenum;
429*a7c91847Schristos char const **linep;
430*a7c91847Schristos size_t *lenp;
431*a7c91847Schristos {
432*a7c91847Schristos int i = linenum;
433*a7c91847Schristos int last = find_function_last_search;
434*a7c91847Schristos find_function_last_search = i;
435*a7c91847Schristos
436*a7c91847Schristos while (--i >= last)
437*a7c91847Schristos {
438*a7c91847Schristos /* See if this line is what we want. */
439*a7c91847Schristos struct regexp_list *r;
440*a7c91847Schristos char const *line = file->linbuf[i];
441*a7c91847Schristos size_t len = file->linbuf[i + 1] - line;
442*a7c91847Schristos
443*a7c91847Schristos for (r = function_regexp_list; r; r = r->next)
444*a7c91847Schristos if (0 <= re_search (&r->buf, line, len, 0, len, 0))
445*a7c91847Schristos {
446*a7c91847Schristos *linep = line;
447*a7c91847Schristos *lenp = len;
448*a7c91847Schristos find_function_last_match = i;
449*a7c91847Schristos return;
450*a7c91847Schristos }
451*a7c91847Schristos }
452*a7c91847Schristos /* If we search back to where we started searching the previous time,
453*a7c91847Schristos find the line we found last time. */
454*a7c91847Schristos if (find_function_last_match >= - file->prefix_lines)
455*a7c91847Schristos {
456*a7c91847Schristos i = find_function_last_match;
457*a7c91847Schristos *linep = file->linbuf[i];
458*a7c91847Schristos *lenp = file->linbuf[i + 1] - *linep;
459*a7c91847Schristos return;
460*a7c91847Schristos }
461*a7c91847Schristos return;
462*a7c91847Schristos }
463