Debian Codegraph @

src/diff.h

raw
1 /* Shared definitions for GNU DIFF
2
3 Copyright (C) 1988-1989, 1991-1995, 1998, 2001-2002, 2004, 2009-2013,
4 2015-2025 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 _GL_INLINE_HEADER_BEGIN
27
28 #ifndef DIFF_INLINE
29 # define DIFF_INLINE _GL_INLINE
30 #endif
31
32 /* What kind of changes a hunk contains. */
33 enum changes
34 {
35 /* No changes: lines common to both files. */
36 UNCHANGED,
37
38 /* Deletes only: lines taken from just the first file. */
39 OLD,
40
41 /* Inserts only: lines taken from just the second file. */
42 NEW,
43
44 /* Both deletes and inserts: a hunk containing both old and new lines. */
45 CHANGED
46 };
47
48 /* When colors should be used in the output. */
49 enum colors_style
50 {
51 /* Never output colors. */
52 NEVER,
53
54 /* Output colors if the output is a terminal. */
55 AUTO,
56
57 /* Always output colors. */
58 ALWAYS,
59 };
60
61 /* Variables for command line options */
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 This is indented unusually to pacify 'make syntax-check'. */
96 DIFF_INLINE bool robust_output_style (enum output_style s)
97 {
98 return s != OUTPUT_ED && s != OUTPUT_FORWARD_ED;
99 }
100
101 extern enum output_style output_style;
102
103 /* Define the current color context used to print a line. */
104 extern enum colors_style colors_style;
105
106 /* Nonzero if output cannot be generated for identical files. */
107 extern bool no_diff_means_no_output;
108
109 /* Number of lines of context to show in each set of diffs.
110 This is zero when context is not to be shown. */
111 extern lin context;
112
113 /* Consider all files as text files (-a).
114 Don't interpret codes over 0177 as implying a "binary file". */
115 extern bool text;
116
117 /* Number of lines to keep in identical prefix and suffix. */
118 extern lin horizon_lines;
119
120 /* The significance of white space during comparisons. */
121 enum DIFF_white_space
122 {
123 /* All white space is significant (the default). */
124 IGNORE_NO_WHITE_SPACE,
125
126 /* Ignore changes due to tab expansion (-E). */
127 IGNORE_TAB_EXPANSION,
128
129 /* Ignore changes in trailing horizontal white space (-Z). */
130 IGNORE_TRAILING_SPACE,
131
132 /* IGNORE_TAB_EXPANSION and IGNORE_TRAILING_SPACE are a special case
133 because they are independent and can be ORed together, yielding
134 IGNORE_TAB_EXPANSION_AND_TRAILING_SPACE. */
135 IGNORE_TAB_EXPANSION_AND_TRAILING_SPACE,
136
137 /* Ignore changes in horizontal white space (-b). */
138 IGNORE_SPACE_CHANGE,
139
140 /* Ignore all horizontal white space (-w). */
141 IGNORE_ALL_SPACE
142 };
143 extern enum DIFF_white_space ignore_white_space;
144
145 /* Ignore changes that affect only blank lines (-B). */
146 extern bool ignore_blank_lines;
147
148 /* Files can be compared byte-by-byte, as if they were binary.
149 This depends on various options. */
150 extern bool files_can_be_treated_as_binary;
151
152 /* Ignore differences in case of letters (-i). */
153 extern bool ignore_case;
154
155 /* Ignore differences in case of letters in file names. */
156 extern bool ignore_file_name_case;
157
158 /* Act on symbolic links themselves rather than on their target
159 (--no-dereference). */
160 extern bool no_dereference_symlinks;
161
162 /* Local timezone for 'c' output headers, if needed. */
163 #if HAVE_TM_GMTOFF
164 # define localtz 0 /* Placeholder since localtz is never needed. */
165 #else
166 extern timezone_t localtz;
167 #endif
168
169 /* File labels for '-c' output headers (--label). */
170 extern char *file_label[2];
171
172 /* Regexp to identify function-header lines (-F). */
173 extern struct re_pattern_buffer function_regexp;
174
175 /* Ignore changes that affect only lines matching this regexp (-I). */
176 extern struct re_pattern_buffer ignore_regexp;
177
178 /* Say only whether files differ, not how (-q). */
179 extern bool brief;
180
181 /* Expand tabs in the output so the text lines up properly
182 despite the characters added to the front of each line (-t). */
183 extern bool expand_tabs;
184
185 /* Number of columns between tab stops. */
186 extern intmax_t tabsize;
187
188 /* Use a tab in the output, rather than a space, before the text of an
189 input line, so as to keep the proper alignment in the input line
190 without changing the characters in it (-T). */
191 extern bool initial_tab;
192
193 /* Do not output an initial space or tab before the text of an empty line. */
194 extern bool suppress_blank_empty;
195
196 /* Remove trailing carriage returns from input. */
197 extern bool strip_trailing_cr;
198
199 /* In directory comparison, specify file to start with (-S).
200 This is used for resuming an aborted comparison.
201 All file names less than this name are ignored. */
202 extern char const *starting_file;
203
204 /* Pipe each file's output through pr (-l). */
205 extern bool paginate;
206
207 /* Line group formats for unchanged, old, new, and changed groups. */
208 extern char const *group_format[CHANGED + 1];
209
210 /* Line formats for unchanged, old, and new lines. */
211 extern char const *line_format[NEW + 1];
212
213 /* If using OUTPUT_SDIFF print extra information to help the sdiff filter. */
214 extern bool sdiff_merge_assist;
215
216 /* Tell OUTPUT_SDIFF to show only the left version of common lines. */
217 extern bool left_column;
218
219 /* Tell OUTPUT_SDIFF to not show common lines. */
220 extern bool suppress_common_lines;
221
222 /* The half line width and column 2 offset for OUTPUT_SDIFF. */
223 extern intmax_t sdiff_half_width;
224 extern intmax_t sdiff_column2_offset;
225
226 /* String containing all the command options diff received,
227 with spaces between and at the beginning but none at the end.
228 If there were no options given, this string is empty. */
229 extern char *switch_string;
230
231 /* Use heuristics for better speed with large files with a small
232 density of changes. */
233 extern bool speed_large_files;
234
235 /* Patterns that match file names to be excluded. */
236 extern struct exclude *excluded;
237
238 /* Don't discard lines. This makes things slower (sometimes much
239 slower) but will find a guaranteed minimal set of changes. */
240 extern bool minimal;
241
242 /* The strftime format to use for time strings. */
243 extern char const *time_format;
244
245 /* The result of comparison is an "edit script": a chain of 'struct change'.
246 Each 'struct change' represents one place where some lines are deleted
247 and some are inserted.
248
249 LINE0 and LINE1 are the first affected lines in the two files (origin 0).
250 DELETED is the number of lines deleted here from file 0.
251 INSERTED is the number of lines inserted here in file 1.
252
253 If DELETED is 0 then LINE0 is the number of the line before
254 which the insertion was done; vice versa for INSERTED and LINE1. */
255
256 struct change
257 {
258 struct change *link; /* Previous or next edit command */
259 lin inserted; /* # lines of file 1 changed here. */
260 lin deleted; /* # lines of file 0 changed here. */
261 lin line0; /* Line number of 1st deleted line. */
262 lin line1; /* Line number of 1st inserted line. */
263 bool ignore; /* Flag used in context.c. */
264 };
265
266 /* Structures that describe the input files. */
267
268 /* Directory entry types. Like dirent DT_* macros, but portable and
269 safe to use as 'char'. Use the same values as GNU/Linux, as this
270 may help compilers on that platform. */
271 enum detype
272 {
273 DE_UNKNOWN,
274 DE_FIFO,
275 DE_CHR,
276 DE_DIR = 4,
277 DE_BLK = 6,
278 DE_REG = 8,
279 DE_LNK = 10,
280 DE_SOCK = 12,
281 DE_WHT = 14,
282
283 /* This one is not in GNU/Linux; it means the directory entry
284 type has been determined but is none of the above. */
285 DE_OTHER
286 };
287
288 /* Data on one input file being compared. */
289
290 struct file_data {
291 int desc; /* File descriptor */
292 int openerr; /* openat errno, or 0 */
293 int err; /* openat or fstatat or fstat errno, or 0 */
294 char const *name; /* File name */
295 char const *filetype; /* file type as untranslated string */
296 struct stat stat; /* File status */
297
298 /* Directory stream corresponding to DESC, if it has been fdopendir'ed.
299 Null otherwise. */
300 DIR *dirstream;
301
302 /* Buffer in which text of file is read. */
303 word *buffer;
304
305 /* Allocated size of buffer, in bytes. Always a multiple of
306 sizeof *buffer. */
307 idx_t bufsize;
308
309 /* Number of valid bytes now in the buffer. */
310 idx_t buffered;
311
312 /* Array of pointers to lines in the file. */
313 char const **linbuf;
314
315 /* linbuf_base <= 0 <= buffered_lines <= valid_lines <= alloc_lines.
316 linbuf[0 ... buffered_lines - 1] are possibly differing.
317 linbuf[linbuf_base ... valid_lines - 1] contain valid data.
318 linbuf[linbuf_base ... alloc_lines - 1] are allocated. */
319 lin linbuf_base, buffered_lines, valid_lines, alloc_lines;
320
321 /* Pointer to end of prefix of this file to ignore when hashing. */
322 char const *prefix_end;
323
324 /* Count of lines in the prefix.
325 There are this many lines in the file before linbuf[0]. */
326 lin prefix_lines;
327
328 /* Pointer to start of suffix of this file to ignore when hashing. */
329 char const *suffix_begin;
330
331 /* Vector, indexed by line number, containing an equivalence code for
332 each line. It is this vector that is actually compared with that
333 of another file to generate differences. */
334 lin *equivs;
335
336 /* Vector, like the previous one except that
337 the elements for discarded lines have been squeezed out. */
338 lin *undiscarded;
339
340 /* Vector mapping virtual line numbers (not counting discarded lines)
341 to real ones (counting those lines). Both are origin-0. */
342 lin *realindexes;
343
344 /* Total number of nondiscarded lines. */
345 lin nondiscarded_lines;
346
347 /* Vector, indexed by real origin-0 line number,
348 containing true for a line that is an insertion or a deletion.
349 The results of comparison are stored here. */
350 bool *changed;
351
352 /* 1 if file ends in a line with no final newline. */
353 bool missing_newline;
354
355 /* 1 if at end of file. */
356 bool eof;
357
358 /* 1 more than the maximum equivalence value used for this or its
359 sibling file. */
360 lin equiv_max;
361 };
362
363 /* struct file_data.desc markers.
364 A top level parent directory desc can be AT_FDCWD;
365 it is OK if AT_FDCWD is one of these other values. */
366 enum { OPEN_FAILED = -1 }; /* open was attempted but failed */
367 enum { NONEXISTENT = -2 }; /* nonexistent file */
368 enum { UNOPENED = -3 }; /* unopened file (e.g., file type mismatch) */
369
370 /* Data on two input files being compared. */
371
372 struct comparison
373 {
374 /* The two files. */
375 struct file_data file[2];
376
377 /* The parent comparison, or &noparent if at the top level. */
378 struct comparison const *parent;
379 };
380
381 /* Describe the two files currently being compared. */
382
383 extern struct comparison curr;
384
385 /* A placeholder for the parent of the top level comparison.
386 Only the desc slots are used; although they are typically AT_FDCWD,
387 one might be nonnegative for a directory at the top level
388 for 'diff DIR FILE' or 'diff FILE DIR'. */
389
390 extern struct comparison noparent;
391
392 /* Stdio stream to output diffs to. */
393
394 extern FILE *outfile;
395
396 /* Declare various functions. */
397
398 /* analyze.c */
399 extern int diff_2_files (struct comparison *);
400
401 /* context.c */
402 extern void print_context_header (struct file_data[],
403 char const *const *, bool);
404 extern void print_context_script (struct change *, bool);
405
406 /* diff.c */
407 extern int compare_files (struct comparison const *, enum detype const[2],
408 char const *, char const *);
409
410 /* dir.c */
411 extern int diff_dirs (struct comparison *);
412 extern char *find_dir_file_pathname (struct file_data *, char const *,
413 enum detype *)
414 ATTRIBUTE_MALLOC ATTRIBUTE_DEALLOC_FREE
415 ATTRIBUTE_RETURNS_NONNULL;
416
417 /* ed.c */
418 extern void print_ed_script (struct change *);
419 extern void pr_forward_ed_script (struct change *);
420
421 /* ifdef.c */
422 extern void print_ifdef_script (struct change *);
423
424 /* io.c */
425 extern void file_block_read (struct file_data *, idx_t);
426 extern bool read_files (struct file_data[], bool);
427
428 /* normal.c */
429 extern void print_normal_script (struct change *);
430
431 /* rcs.c */
432 extern void print_rcs_script (struct change *);
433
434 /* side.c */
435 extern void print_sdiff_script (struct change *);
436
437 /* util.c */
438 extern char const change_letter[4];
439 extern char const pr_program[];
440 extern lin translate_line_number (struct file_data const *, lin)
441 ATTRIBUTE_PURE;
442 extern struct change *find_change (struct change *) ATTRIBUTE_CONST;
443 extern enum changes analyze_hunk (struct change *, lin *, lin *, lin *, lin *);
444 extern void begin_output (void);
445 extern void cleanup_signal_handlers (void);
446 extern void debug_script (struct change *);
447 extern _Noreturn void fatal (char const *);
448 extern void finish_output (void);
449 extern void message (char const *, ...) ATTRIBUTE_FORMAT ((printf, 1, 2));
450 extern void output_1_line (char const *, char const *, char const *,
451 char const *);
452 extern void perror_with_name (char const *);
453 extern _Noreturn void pfatal_with_name (char const *);
454 extern void print_1_line (char const *, char const *const *);
455 extern void print_1_line_nl (char const *, char const *const *, bool);
456 extern void print_message_queue (void);
457 extern void print_number_range (char, struct file_data *, lin, lin);
458 extern void print_script (struct change *, struct change * (*) (struct change *),
459 void (*) (struct change *));
460 extern void setup_output (char const *, char const *, bool);
461 extern void translate_range (struct file_data const *, lin, lin, lin *, lin *);
462
463 enum color_context
464 {
465 HEADER_CONTEXT,
466 ADD_CONTEXT,
467 DELETE_CONTEXT,
468 RESET_CONTEXT,
469 LINE_NUMBER_CONTEXT,
470 };
471
472 extern bool presume_output_tty;
473
474 extern void set_color_context (enum color_context color_context);
475 extern void set_color_palette (char *palette);
476
477 _GL_INLINE_HEADER_END