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