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