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