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