xref: /openbsd-src/gnu/usr.bin/cvs/diff/diff.h (revision 2286d8ed900f26153a3cd5227a124b1c0adce72f)
1 /* Shared definitions for GNU DIFF
2    Copyright (C) 1988, 89, 91, 92, 93, 97 Free Software Foundation, Inc.
3 
4 This file is part of GNU DIFF.
5 
6 GNU DIFF is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU DIFF is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU DIFF; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19 
20 #include "system.h"
21 #include <stdio.h>
22 #include <setjmp.h>
23 #include "regex.h"
24 
25 #define TAB_WIDTH 8
26 
27 /* Variables for command line options */
28 
29 #ifndef GDIFF_MAIN
30 #define EXTERN extern
31 #else
32 #define EXTERN
33 #endif
34 
35 enum output_style {
36   /* Default output style.  */
37   OUTPUT_NORMAL,
38   /* Output the differences with lines of context before and after (-c).  */
39   OUTPUT_CONTEXT,
40   /* Output the differences in a unified context diff format (-u). */
41   OUTPUT_UNIFIED,
42   /* Output the differences as commands suitable for `ed' (-e).  */
43   OUTPUT_ED,
44   /* Output the diff as a forward ed script (-f).  */
45   OUTPUT_FORWARD_ED,
46   /* Like -f, but output a count of changed lines in each "command" (-n). */
47   OUTPUT_RCS,
48   /* Output merged #ifdef'd file (-D).  */
49   OUTPUT_IFDEF,
50   /* Output sdiff style (-y).  */
51   OUTPUT_SDIFF
52 };
53 
54 /* True for output styles that are robust,
55    i.e. can handle a file that ends in a non-newline.  */
56 #define ROBUST_OUTPUT_STYLE(S) ((S) != OUTPUT_ED && (S) != OUTPUT_FORWARD_ED)
57 
58 EXTERN enum output_style output_style;
59 
60 /* Nonzero if output cannot be generated for identical files.  */
61 EXTERN int no_diff_means_no_output;
62 
63 /* Number of lines of context to show in each set of diffs.
64    This is zero when context is not to be shown.  */
65 EXTERN int      context;
66 
67 /* Consider all files as text files (-a).
68    Don't interpret codes over 0177 as implying a "binary file".  */
69 EXTERN int	always_text_flag;
70 
71 /* Number of lines to keep in identical prefix and suffix.  */
72 EXTERN int      horizon_lines;
73 
74 /* Ignore changes in horizontal white space (-b).  */
75 EXTERN int      ignore_space_change_flag;
76 
77 /* Ignore all horizontal white space (-w).  */
78 EXTERN int      ignore_all_space_flag;
79 
80 /* Ignore changes that affect only blank lines (-B).  */
81 EXTERN int      ignore_blank_lines_flag;
82 
83 /* 1 if lines may match even if their contents do not match exactly.
84    This depends on various options.  */
85 EXTERN int      ignore_some_line_changes;
86 
87 /* 1 if files may match even if their contents are not byte-for-byte identical.
88    This depends on various options.  */
89 EXTERN int      ignore_some_changes;
90 
91 /* Ignore differences in case of letters (-i).  */
92 EXTERN int      ignore_case_flag;
93 
94 /* File labels for `-c' output headers (-L).  */
95 EXTERN char *file_label[2];
96 
97 struct regexp_list
98 {
99   struct re_pattern_buffer buf;
100   struct regexp_list *next;
101 };
102 
103 /* Regexp to identify function-header lines (-F).  */
104 EXTERN struct regexp_list *function_regexp_list;
105 
106 /* Ignore changes that affect only lines matching this regexp (-I).  */
107 EXTERN struct regexp_list *ignore_regexp_list;
108 
109 /* Say only whether files differ, not how (-q).  */
110 EXTERN int 	no_details_flag;
111 
112 /* Report files compared that match (-s).
113    Normally nothing is output when that happens.  */
114 EXTERN int      print_file_same_flag;
115 
116 /* Output the differences with exactly 8 columns added to each line
117    so that any tabs in the text line up properly (-T).  */
118 EXTERN int	tab_align_flag;
119 
120 /* Expand tabs in the output so the text lines up properly
121    despite the characters added to the front of each line (-t).  */
122 EXTERN int	tab_expand_flag;
123 
124 /* In directory comparison, specify file to start with (-S).
125    All file names less than this name are ignored.  */
126 EXTERN char	*dir_start_file;
127 
128 /* If a file is new (appears in only one dir)
129    include its entire contents (-N).
130    Then `patch' would create the file with appropriate contents.  */
131 EXTERN int	entire_new_file_flag;
132 
133 /* If a file is new (appears in only the second dir)
134    include its entire contents (-P).
135    Then `patch' would create the file with appropriate contents.  */
136 EXTERN int	unidirectional_new_file_flag;
137 
138 /* Pipe each file's output through pr (-l).  */
139 EXTERN int	paginate_flag;
140 
141 enum line_class {
142   /* Lines taken from just the first file.  */
143   OLD,
144   /* Lines taken from just the second file.  */
145   NEW,
146   /* Lines common to both files.  */
147   UNCHANGED,
148   /* A hunk containing both old and new lines (line groups only).  */
149   CHANGED
150 };
151 
152 /* Line group formats for old, new, unchanged, and changed groups.  */
153 EXTERN char *group_format[CHANGED + 1];
154 
155 /* Line formats for old, new, and unchanged lines.  */
156 EXTERN char *line_format[UNCHANGED + 1];
157 
158 /* If using OUTPUT_SDIFF print extra information to help the sdiff filter. */
159 EXTERN int sdiff_help_sdiff;
160 
161 /* Tell OUTPUT_SDIFF to show only the left version of common lines. */
162 EXTERN int sdiff_left_only;
163 
164 /* Tell OUTPUT_SDIFF to not show common lines. */
165 EXTERN int sdiff_skip_common_lines;
166 
167 /* The half line width and column 2 offset for OUTPUT_SDIFF.  */
168 EXTERN unsigned sdiff_half_width;
169 EXTERN unsigned sdiff_column2_offset;
170 
171 /* String containing all the command options diff received,
172    with spaces between and at the beginning but none at the end.
173    If there were no options given, this string is empty.  */
174 EXTERN char *	switch_string;
175 
176 /* Nonzero means use heuristics for better speed.  */
177 EXTERN int	heuristic;
178 
179 /* Name of program the user invoked (for error messages).  */
180 EXTERN char *diff_program_name;
181 
182 /* Jump buffer for nonlocal exits. */
183 EXTERN jmp_buf diff_abort_buf;
184 #define DIFF_ABORT(retval) longjmp(diff_abort_buf, retval)
185 
186 /* The result of comparison is an "edit script": a chain of `struct change'.
187    Each `struct change' represents one place where some lines are deleted
188    and some are inserted.
189 
190    LINE0 and LINE1 are the first affected lines in the two files (origin 0).
191    DELETED is the number of lines deleted here from file 0.
192    INSERTED is the number of lines inserted here in file 1.
193 
194    If DELETED is 0 then LINE0 is the number of the line before
195    which the insertion was done; vice versa for INSERTED and LINE1.  */
196 
197 struct change
198 {
199   struct change *link;		/* Previous or next edit command  */
200   int inserted;			/* # lines of file 1 changed here.  */
201   int deleted;			/* # lines of file 0 changed here.  */
202   int line0;			/* Line number of 1st deleted line.  */
203   int line1;			/* Line number of 1st inserted line.  */
204   char ignore;			/* Flag used in context.c */
205 };
206 
207 /* Structures that describe the input files.  */
208 
209 /* Data on one input file being compared.  */
210 
211 struct file_data {
212     int             desc;	/* File descriptor  */
213     char const      *name;	/* File name  */
214     struct stat     stat;	/* File status from fstat()  */
215     int             dir_p;	/* nonzero if file is a directory  */
216 
217     /* Buffer in which text of file is read.  */
218     char *	    buffer;
219     /* Allocated size of buffer.  */
220     size_t	    bufsize;
221     /* Number of valid characters now in the buffer. */
222     size_t	    buffered_chars;
223 
224     /* Array of pointers to lines in the file.  */
225     char const **linbuf;
226 
227     /* linbuf_base <= buffered_lines <= valid_lines <= alloc_lines.
228        linebuf[linbuf_base ... buffered_lines - 1] are possibly differing.
229        linebuf[linbuf_base ... valid_lines - 1] contain valid data.
230        linebuf[linbuf_base ... alloc_lines - 1] are allocated.  */
231     int linbuf_base, buffered_lines, valid_lines, alloc_lines;
232 
233     /* Pointer to end of prefix of this file to ignore when hashing. */
234     char const *prefix_end;
235 
236     /* Count of lines in the prefix.
237        There are this many lines in the file before linbuf[0].  */
238     int prefix_lines;
239 
240     /* Pointer to start of suffix of this file to ignore when hashing. */
241     char const *suffix_begin;
242 
243     /* Vector, indexed by line number, containing an equivalence code for
244        each line.  It is this vector that is actually compared with that
245        of another file to generate differences. */
246     int		   *equivs;
247 
248     /* Vector, like the previous one except that
249        the elements for discarded lines have been squeezed out.  */
250     int		   *undiscarded;
251 
252     /* Vector mapping virtual line numbers (not counting discarded lines)
253        to real ones (counting those lines).  Both are origin-0.  */
254     int		   *realindexes;
255 
256     /* Total number of nondiscarded lines. */
257     int		    nondiscarded_lines;
258 
259     /* Vector, indexed by real origin-0 line number,
260        containing 1 for a line that is an insertion or a deletion.
261        The results of comparison are stored here.  */
262     char	   *changed_flag;
263 
264     /* 1 if file ends in a line with no final newline. */
265     int		    missing_newline;
266 
267     /* 1 more than the maximum equivalence value used for this or its
268        sibling file. */
269     int equiv_max;
270 };
271 
272 /* Describe the two files currently being compared.  */
273 
274 EXTERN struct file_data files[2];
275 
276 /* Stdio stream to output diffs to.  */
277 
278 EXTERN FILE *outfile;
279 
280 /* Declare various functions.  */
281 
282 /* analyze.c */
283 int diff_2_files PARAMS((struct file_data[], int));
284 
285 /* context.c */
286 void print_context_header PARAMS((struct file_data[], int));
287 void print_context_script PARAMS((struct change *, int));
288 
289 /* diff.c */
290 int excluded_filename PARAMS((char const *));
291 
292 /* dir.c */
293 int diff_dirs PARAMS((struct file_data const[], int (*) PARAMS((char const *, char const *, char const *, char const *, int)), int));
294 
295 /* ed.c */
296 void print_ed_script PARAMS((struct change *));
297 void pr_forward_ed_script PARAMS((struct change *));
298 
299 /* ifdef.c */
300 void print_ifdef_script PARAMS((struct change *));
301 
302 /* io.c */
303 int read_files PARAMS((struct file_data[], int));
304 int sip PARAMS((struct file_data *, int));
305 void slurp PARAMS((struct file_data *));
306 
307 /* normal.c */
308 void print_normal_script PARAMS((struct change *));
309 
310 /* rcs.c */
311 void print_rcs_script PARAMS((struct change *));
312 
313 /* side.c */
314 void print_sdiff_script PARAMS((struct change *));
315 
316 /* util.c */
317 VOID *xmalloc PARAMS((size_t));
318 VOID *xrealloc PARAMS((VOID *, size_t));
319 char *concat PARAMS((char const *, char const *, char const *));
320 char *dir_file_pathname PARAMS((char const *, char const *));
321 int change_letter PARAMS((int, int));
322 int line_cmp PARAMS((char const *, char const *));
323 int translate_line_number PARAMS((struct file_data const *, int));
324 struct change *find_change PARAMS((struct change *));
325 struct change *find_reverse_change PARAMS((struct change *));
326 void analyze_hunk PARAMS((struct change *, int *, int *, int *, int *, int *, int *));
327 void begin_output PARAMS((void));
328 void debug_script PARAMS((struct change *));
329 void diff_error PARAMS((char const *, char const *, char const *));
330 void fatal PARAMS((char const *));
331 void finish_output PARAMS((void));
332 void message PARAMS((char const *, char const *, char const *));
333 void message5 PARAMS((char const *, char const *, char const *, char const *, char const *));
334 void output_1_line PARAMS((char const *, char const *, char const *, char const *));
335 void perror_with_name PARAMS((char const *));
336 void pfatal_with_name PARAMS((char const *));
337 void print_1_line PARAMS((char const *, char const * const *));
338 void print_message_queue PARAMS((void));
339 void print_number_range PARAMS((int, struct file_data *, int, int));
340 void print_script PARAMS((struct change *, struct change * (*) PARAMS((struct change *)), void (*) PARAMS((struct change *))));
341 void setup_output PARAMS((char const *, char const *, int));
342 void translate_range PARAMS((struct file_data const *, int, int, int *, int *));
343 
344 /* version.c */
345 extern char const diff_version_string[];
346