xref: /dragonfly/contrib/diffutils/src/diff.h (revision 92fc8b5c)
1 /* Shared definitions for GNU DIFF
2 
3    Copyright (C) 1988-1989, 1991-1995, 1998, 2001-2002, 2004, 2009-2010 Free
4    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 "system.h"
22 #include <regex.h>
23 #include <stdio.h>
24 #include <unlocked-io.h>
25 
26 /* What kind of changes a hunk contains.  */
27 enum changes
28 {
29   /* No changes: lines common to both files.  */
30   UNCHANGED,
31 
32   /* Deletes only: lines taken from just the first file.  */
33   OLD,
34 
35   /* Inserts only: lines taken from just the second file.  */
36   NEW,
37 
38   /* Both deletes and inserts: a hunk containing both old and new lines.  */
39   CHANGED
40 };
41 
42 /* Variables for command line options */
43 
44 #ifndef GDIFF_MAIN
45 # define XTERN extern
46 #else
47 # define XTERN
48 #endif
49 
50 enum output_style
51 {
52   /* No output style specified.  */
53   OUTPUT_UNSPECIFIED,
54 
55   /* Default output style.  */
56   OUTPUT_NORMAL,
57 
58   /* Output the differences with lines of context before and after (-c).  */
59   OUTPUT_CONTEXT,
60 
61   /* Output the differences in a unified context diff format (-u).  */
62   OUTPUT_UNIFIED,
63 
64   /* Output the differences as commands suitable for `ed' (-e).  */
65   OUTPUT_ED,
66 
67   /* Output the diff as a forward ed script (-f).  */
68   OUTPUT_FORWARD_ED,
69 
70   /* Like -f, but output a count of changed lines in each "command" (-n).  */
71   OUTPUT_RCS,
72 
73   /* Output merged #ifdef'd file (-D).  */
74   OUTPUT_IFDEF,
75 
76   /* Output sdiff style (-y).  */
77   OUTPUT_SDIFF
78 };
79 
80 /* True for output styles that are robust,
81    i.e. can handle a file that ends in a non-newline.  */
82 #define ROBUST_OUTPUT_STYLE(S) ((S) != OUTPUT_ED && (S) != OUTPUT_FORWARD_ED)
83 
84 XTERN enum output_style output_style;
85 
86 /* Nonzero if output cannot be generated for identical files.  */
87 XTERN bool no_diff_means_no_output;
88 
89 /* Number of lines of context to show in each set of diffs.
90    This is zero when context is not to be shown.  */
91 XTERN lin context;
92 
93 /* Consider all files as text files (-a).
94    Don't interpret codes over 0177 as implying a "binary file".  */
95 XTERN bool text;
96 
97 /* Number of lines to keep in identical prefix and suffix.  */
98 XTERN lin horizon_lines;
99 
100 /* The significance of white space during comparisons.  */
101 XTERN enum
102 {
103   /* All white space is significant (the default).  */
104   IGNORE_NO_WHITE_SPACE,
105 
106   /* Ignore changes due to tab expansion (-E).  */
107   IGNORE_TAB_EXPANSION,
108 
109   /* Ignore changes in horizontal white space (-b).  */
110   IGNORE_SPACE_CHANGE,
111 
112   /* Ignore all horizontal white space (-w).  */
113   IGNORE_ALL_SPACE
114 } ignore_white_space;
115 
116 /* Ignore changes that affect only blank lines (-B).  */
117 XTERN bool ignore_blank_lines;
118 
119 /* Files can be compared byte-by-byte, as if they were binary.
120    This depends on various options.  */
121 XTERN bool files_can_be_treated_as_binary;
122 
123 /* Ignore differences in case of letters (-i).  */
124 XTERN bool ignore_case;
125 
126 /* Ignore differences in case of letters in file names.  */
127 XTERN bool ignore_file_name_case;
128 
129 /* File labels for `-c' output headers (--label).  */
130 XTERN char *file_label[2];
131 
132 /* Regexp to identify function-header lines (-F).  */
133 XTERN struct re_pattern_buffer function_regexp;
134 
135 /* Ignore changes that affect only lines matching this regexp (-I).  */
136 XTERN struct re_pattern_buffer ignore_regexp;
137 
138 /* Say only whether files differ, not how (-q).  */
139 XTERN bool brief;
140 
141 /* Expand tabs in the output so the text lines up properly
142    despite the characters added to the front of each line (-t).  */
143 XTERN bool expand_tabs;
144 
145 /* Number of columns between tab stops.  */
146 XTERN size_t tabsize;
147 
148 /* Use a tab in the output, rather than a space, before the text of an
149    input line, so as to keep the proper alignment in the input line
150    without changing the characters in it (-T).  */
151 XTERN bool initial_tab;
152 
153 /* Do not output an initial space or tab before the text of an empty line.  */
154 XTERN bool suppress_blank_empty;
155 
156 /* Remove trailing carriage returns from input.  */
157 XTERN bool strip_trailing_cr;
158 
159 /* In directory comparison, specify file to start with (-S).
160    This is used for resuming an aborted comparison.
161    All file names less than this name are ignored.  */
162 XTERN char const *starting_file;
163 
164 /* Pipe each file's output through pr (-l).  */
165 XTERN bool paginate;
166 
167 /* Line group formats for unchanged, old, new, and changed groups.  */
168 XTERN char const *group_format[CHANGED + 1];
169 
170 /* Line formats for unchanged, old, and new lines.  */
171 XTERN char const *line_format[NEW + 1];
172 
173 /* If using OUTPUT_SDIFF print extra information to help the sdiff filter.  */
174 XTERN bool sdiff_merge_assist;
175 
176 /* Tell OUTPUT_SDIFF to show only the left version of common lines.  */
177 XTERN bool left_column;
178 
179 /* Tell OUTPUT_SDIFF to not show common lines.  */
180 XTERN bool suppress_common_lines;
181 
182 /* The half line width and column 2 offset for OUTPUT_SDIFF.  */
183 XTERN size_t sdiff_half_width;
184 XTERN size_t sdiff_column2_offset;
185 
186 /* String containing all the command options diff received,
187    with spaces between and at the beginning but none at the end.
188    If there were no options given, this string is empty.  */
189 XTERN char *switch_string;
190 
191 /* Use heuristics for better speed with large files with a small
192    density of changes.  */
193 XTERN bool speed_large_files;
194 
195 /* Patterns that match file names to be excluded.  */
196 XTERN struct exclude *excluded;
197 
198 /* Don't discard lines.  This makes things slower (sometimes much
199    slower) but will find a guaranteed minimal set of changes.  */
200 XTERN bool minimal;
201 
202 /* The strftime format to use for time strings.  */
203 XTERN char const *time_format;
204 
205 /* The result of comparison is an "edit script": a chain of `struct change'.
206    Each `struct change' represents one place where some lines are deleted
207    and some are inserted.
208 
209    LINE0 and LINE1 are the first affected lines in the two files (origin 0).
210    DELETED is the number of lines deleted here from file 0.
211    INSERTED is the number of lines inserted here in file 1.
212 
213    If DELETED is 0 then LINE0 is the number of the line before
214    which the insertion was done; vice versa for INSERTED and LINE1.  */
215 
216 struct change
217 {
218   struct change *link;		/* Previous or next edit command  */
219   lin inserted;			/* # lines of file 1 changed here.  */
220   lin deleted;			/* # lines of file 0 changed here.  */
221   lin line0;			/* Line number of 1st deleted line.  */
222   lin line1;			/* Line number of 1st inserted line.  */
223   bool ignore;			/* Flag used in context.c.  */
224 };
225 
226 /* Structures that describe the input files.  */
227 
228 /* Data on one input file being compared.  */
229 
230 struct file_data {
231     int             desc;	/* File descriptor  */
232     char const      *name;	/* File name  */
233     struct stat     stat;	/* File status */
234 
235     /* Buffer in which text of file is read.  */
236     word *buffer;
237 
238     /* Allocated size of buffer, in bytes.  Always a multiple of
239        sizeof *buffer.  */
240     size_t bufsize;
241 
242     /* Number of valid bytes now in the buffer.  */
243     size_t buffered;
244 
245     /* Array of pointers to lines in the file.  */
246     char const **linbuf;
247 
248     /* linbuf_base <= buffered_lines <= valid_lines <= alloc_lines.
249        linebuf[linbuf_base ... buffered_lines - 1] are possibly differing.
250        linebuf[linbuf_base ... valid_lines - 1] contain valid data.
251        linebuf[linbuf_base ... alloc_lines - 1] are allocated.  */
252     lin linbuf_base, buffered_lines, valid_lines, alloc_lines;
253 
254     /* Pointer to end of prefix of this file to ignore when hashing.  */
255     char const *prefix_end;
256 
257     /* Count of lines in the prefix.
258        There are this many lines in the file before linbuf[0].  */
259     lin prefix_lines;
260 
261     /* Pointer to start of suffix of this file to ignore when hashing.  */
262     char const *suffix_begin;
263 
264     /* Vector, indexed by line number, containing an equivalence code for
265        each line.  It is this vector that is actually compared with that
266        of another file to generate differences.  */
267     lin *equivs;
268 
269     /* Vector, like the previous one except that
270        the elements for discarded lines have been squeezed out.  */
271     lin *undiscarded;
272 
273     /* Vector mapping virtual line numbers (not counting discarded lines)
274        to real ones (counting those lines).  Both are origin-0.  */
275     lin *realindexes;
276 
277     /* Total number of nondiscarded lines.  */
278     lin nondiscarded_lines;
279 
280     /* Vector, indexed by real origin-0 line number,
281        containing 1 for a line that is an insertion or a deletion.
282        The results of comparison are stored here.  */
283     char *changed;
284 
285     /* 1 if file ends in a line with no final newline.  */
286     bool missing_newline;
287 
288     /* 1 if at end of file.  */
289     bool eof;
290 
291     /* 1 more than the maximum equivalence value used for this or its
292        sibling file.  */
293     lin equiv_max;
294 };
295 
296 /* The file buffer, considered as an array of bytes rather than
297    as an array of words.  */
298 #define FILE_BUFFER(f) ((char *) (f)->buffer)
299 
300 /* Data on two input files being compared.  */
301 
302 struct comparison
303   {
304     struct file_data file[2];
305     struct comparison const *parent;  /* parent, if a recursive comparison */
306   };
307 
308 /* Describe the two files currently being compared.  */
309 
310 XTERN struct file_data files[2];
311 
312 /* Stdio stream to output diffs to.  */
313 
314 XTERN FILE *outfile;
315 
316 /* Declare various functions.  */
317 
318 /* analyze.c */
319 int diff_2_files (struct comparison *);
320 
321 /* context.c */
322 void print_context_header (struct file_data[], bool);
323 void print_context_script (struct change *, bool);
324 
325 /* dir.c */
326 int diff_dirs (struct comparison const *, int (*) (struct comparison const *, char const *, char const *));
327 
328 /* ed.c */
329 void print_ed_script (struct change *);
330 void pr_forward_ed_script (struct change *);
331 
332 /* ifdef.c */
333 void print_ifdef_script (struct change *);
334 
335 /* io.c */
336 void file_block_read (struct file_data *, size_t);
337 bool read_files (struct file_data[], bool);
338 
339 /* normal.c */
340 void print_normal_script (struct change *);
341 
342 /* rcs.c */
343 void print_rcs_script (struct change *);
344 
345 /* side.c */
346 void print_sdiff_script (struct change *);
347 
348 /* util.c */
349 extern char const change_letter[4];
350 extern char const pr_program[];
351 char *concat (char const *, char const *, char const *);
352 char *dir_file_pathname (char const *, char const *);
353 bool lines_differ (char const *, char const *);
354 lin translate_line_number (struct file_data const *, lin);
355 struct change *find_change (struct change *);
356 struct change *find_reverse_change (struct change *);
357 void *zalloc (size_t);
358 enum changes analyze_hunk (struct change *, lin *, lin *, lin *, lin *);
359 void begin_output (void);
360 void debug_script (struct change *);
361 void fatal (char const *) __attribute__((noreturn));
362 void finish_output (void);
363 void message (char const *, char const *, char const *);
364 void message5 (char const *, char const *, char const *, char const *, char const *);
365 void output_1_line (char const *, char const *, char const *, char const *);
366 void perror_with_name (char const *);
367 void pfatal_with_name (char const *) __attribute__((noreturn));
368 void print_1_line (char const *, char const * const *);
369 void print_message_queue (void);
370 void print_number_range (char, struct file_data *, lin, lin);
371 void print_script (struct change *, struct change * (*) (struct change *), void (*) (struct change *));
372 void setup_output (char const *, char const *, bool);
373 void translate_range (struct file_data const *, lin, lin, long int *, long int *);
374