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