raw
vtools_genesis          1 #include "system.h"
vtools_genesis 2 #include <stdio.h>
vtools_genesis 3 #include <string.h>
vtools_genesis 4 #include <sys/stat.h>
vtools_genesis 5
vtools_genesis 6 /* What kind of changes a hunk contains. */
vtools_genesis 7 enum changes {
vtools_genesis 8 /* No changes: lines common to both files. */
vtools_genesis 9 UNCHANGED,
vtools_genesis 10
vtools_genesis 11 /* Deletes only: lines taken from just the first file. */
vtools_genesis 12 OLD,
vtools_genesis 13
vtools_genesis 14 /* Inserts only: lines taken from just the second file. */
vtools_genesis 15 NEW,
vtools_genesis 16
vtools_genesis 17 /* Both deletes and inserts: a hunk containing both old and new lines. */
vtools_genesis 18 CHANGED
vtools_genesis 19 };
vtools_genesis 20
vtools_genesis 21 /* Variables for command line options */
vtools_genesis 22
vtools_genesis 23 #ifndef GDIFF_MAIN
vtools_genesis 24 # define XTERN extern
vtools_genesis 25 #else
vtools_genesis 26 # define XTERN
vtools_genesis 27 #endif
vtools_genesis 28
vtools_genesis 29 /* Number of lines of context to show in each set of diffs. This is
vtools_genesis 30 zero when context is not to be shown. */
vtools_genesis 31 XTERN lin context;
vtools_genesis 32
vtools_genesis 33 /* Consider all files as text files (-a). Don't interpret codes over
vtools_genesis 34 0177 as implying a "binary file". */
vtools_genesis 35 XTERN bool text;
vtools_genesis 36
vtools_genesis 37 /* Number of lines to keep in identical prefix and suffix. */
vtools_genesis 38 XTERN lin horizon_lines;
vtools_genesis 39
vtools_genesis 40 /* Files can be compared byte-by-byte, as if they were binary. This
vtools_genesis 41 depends on various options. */
vtools_genesis 42 XTERN bool files_can_be_treated_as_binary;
vtools_genesis 43
vtools_genesis 44 /* File labels for |-c| output headers (|--label|). */
vtools_genesis 45 XTERN char *file_label[2];
vtools_genesis 46
vtools_genesis 47 /* Say only whether files differ, not how (|-q|). */
vtools_genesis 48 XTERN bool brief;
vtools_genesis 49
vtools_genesis 50 /* Do not output an initial space or tab before the text of an empty line. */
vtools_genesis 51 XTERN bool suppress_blank_empty;
vtools_genesis 52
vtools_genesis 53 /* In directory comparison, specify file to start with (|-S|). This
vtools_genesis 54 is used for resuming an aborted comparison. All file names less
vtools_genesis 55 than this name are ignored. */
vtools_genesis 56 XTERN char const *starting_file;
vtools_genesis 57
vtools_genesis 58 /* String containing all the command options diff received, with
vtools_genesis 59 spaces between and at the beginning but none at the end. If there
vtools_genesis 60 were no options given, this string is empty. */
vtools_genesis 61 XTERN char *switch_string;
vtools_genesis 62
vtools_genesis 63 /* Use heuristics for better speed with large files with a small
vtools_genesis 64 density of changes. */
vtools_genesis 65 XTERN bool speed_large_files;
vtools_genesis 66
vtools_genesis 67 /* Don't discard lines. This makes things slower (sometimes much
vtools_genesis 68 slower) but will find a guaranteed minimal set of changes. */
vtools_genesis 69 XTERN bool minimal;
vtools_genesis 70
vtools_genesis 71 /* The result of comparison is an ``edit script'': a chain of |struct
vtools_genesis 72 change|. Each |struct change| represents one place where some
vtools_genesis 73 lines are deleted and some are inserted.
vtools_genesis 74
vtools_genesis 75 |line0| and |line1| are the first affected lines in the two files
vtools_genesis 76 (origin 0). |deleted| is the number of lines deleted here from file
vtools_genesis 77 0. |inserted| is the number of lines inserted here in file 1.
vtools_genesis 78
vtools_genesis 79 If |deleted| is 0 then |line0| is the number of the line before
vtools_genesis 80 which the insertion was done; vice versa for |inserted| and
vtools_genesis 81 |line1|. */
vtools_genesis 82
vtools_genesis 83 struct change {
vtools_genesis 84 struct change *link; /* Previous or next edit command */
vtools_genesis 85 lin inserted; /* \# lines of file 1 changed here. */
vtools_genesis 86 lin deleted; /* \# lines of file 0 changed here. */
vtools_genesis 87 lin line0; /* Line number of 1st deleted line. */
vtools_genesis 88 lin line1; /* Line number of 1st inserted line. */
vtools_genesis 89 bool ignore; /* Flag used in |context.c|. */
vtools_genesis 90 };
vtools_genesis 91
vtools_genesis 92 /* Structures that describe the input files. */
vtools_genesis 93
vtools_genesis 94 /* Data on one input file being compared. */
vtools_genesis 95
vtools_genesis 96 struct file_data {
vtools_genesis 97 int desc; /* File descriptor */
vtools_genesis 98 char const *name; /* File name */
vtools_genesis 99 struct stat stat; /* File status */
vtools_genesis 100
vtools_genesis 101 /* Buffer in which text of file is read. */
vtools_genesis 102 word *buffer;
vtools_genesis 103
vtools_genesis 104 /* Allocated size of buffer, in bytes. Always a multiple of
vtools_genesis 105 sizeof |*buffer|. */
vtools_genesis 106 size_t bufsize;
vtools_genesis 107
vtools_genesis 108 /* Number of valid bytes now in the buffer. */
vtools_genesis 109 size_t buffered;
vtools_genesis 110
vtools_genesis 111 /* Array of pointers to lines in the file. */
vtools_genesis 112 char const **linbuf;
vtools_genesis 113
vtools_genesis 114 /* |linbuf_base <= buffered_lines <= valid_lines <= alloc_lines|.
vtools_genesis 115 |linebuf[linbuf_base ... buffered_lines - 1]| are possibly differing.
vtools_genesis 116 |linebuf[linbuf_base ... valid_lines - 1]| contain valid data.
vtools_genesis 117 |linebuf[linbuf_base ... alloc_lines - 1]| are allocated. */
vtools_genesis 118 lin linbuf_base, buffered_lines, valid_lines, alloc_lines;
vtools_genesis 119
vtools_genesis 120 /* Pointer to end of prefix of this file to ignore when hashing. */
vtools_genesis 121 char const *prefix_end;
vtools_genesis 122
vtools_genesis 123 /* Count of lines in the prefix. There are this many lines in the
vtools_genesis 124 file before |linbuf[0]|. */
vtools_genesis 125 lin prefix_lines;
vtools_genesis 126
vtools_genesis 127 /* Pointer to start of suffix of this file to ignore when hashing. */
vtools_genesis 128 char const *suffix_begin;
vtools_genesis 129
vtools_genesis 130 /* Vector, indexed by line number, containing an equivalence code
vtools_genesis 131 for each line. It is this vector that is actually compared
vtools_genesis 132 with that of another file to generate differences. */
vtools_genesis 133 lin *equivs;
vtools_genesis 134
vtools_genesis 135 /* Vector, like the previous one except that the elements for
vtools_genesis 136 discarded lines have been squeezed out. */
vtools_genesis 137 lin *undiscarded;
vtools_genesis 138
vtools_genesis 139 /* Vector mapping virtual line numbers (not counting discarded
vtools_genesis 140 lines) to real ones (counting those lines). Both are
vtools_genesis 141 $origin-0$. */
vtools_genesis 142 lin *realindexes;
vtools_genesis 143
vtools_genesis 144 /* Total number of nondiscarded lines. */
vtools_genesis 145 lin nondiscarded_lines;
vtools_genesis 146
vtools_genesis 147 /* Vector, indexed by real $origin-0$ line number, containing 1
vtools_genesis 148 for a line that is an insertion or a deletion. The results of
vtools_genesis 149 comparison are stored here. */
vtools_genesis 150 char *changed;
vtools_genesis 151
vtools_genesis 152 /* 1 if file ends in a line with no final newline. */
vtools_genesis 153 bool missing_newline;
vtools_genesis 154
vtools_genesis 155 /* 1 if at end of file. */
vtools_genesis 156 bool eof;
vtools_genesis 157
vtools_genesis 158 /* 1 more than the maximum equivalence value used for this or its
vtools_genesis 159 sibling file. */
vtools_genesis 160 lin equiv_max;
vtools_genesis 161 };
vtools_genesis 162
vtools_genesis 163 /* The file buffer, considered as an array of bytes rather than as an
vtools_genesis 164 array of words. */
vtools_genesis 165 #define FILE_BUFFER(f) ((char *) (f)->buffer)
vtools_genesis 166
vtools_genesis 167 /* Data on two input files being compared. */
vtools_genesis 168
vtools_genesis 169 struct comparison {
vtools_genesis 170 struct file_data file[2];
vtools_genesis 171 struct comparison const *parent; /* parent, if a recursive comparison */
vtools_genesis 172 };
vtools_genesis 173
vtools_genesis 174 /* Describe the two files currently being compared. */
vtools_genesis 175
vtools_genesis 176 XTERN struct file_data files[2];
vtools_genesis 177
vtools_genesis 178 /* Stdio stream to output diffs to. */
vtools_genesis 179
vtools_genesis 180 XTERN FILE *outfile;
vtools_genesis 181
vtools_genesis 182 /* Declare various functions. */
vtools_genesis 183
vtools_genesis 184 /* analyze.c */
vtools_genesis 185 extern int diff_2_files(struct comparison *);
vtools_genesis 186
vtools_genesis 187 /* context.c */
vtools_genesis 188 extern void print_context_header(struct file_data[], char const *const *);
vtools_genesis 189
vtools_genesis 190 extern void print_context_script(struct change *);
vtools_genesis 191
vtools_genesis 192 /* dir.c */
vtools_genesis 193 extern int diff_dirs(struct comparison const *,
vtools_genesis 194 int (*)(struct comparison const *,
vtools_genesis 195 char const *, char const *));
vtools_genesis 196
vtools_genesis 197 extern char *find_dir_file_pathname(char const *, char const *);
vtools_genesis 198
vtools_genesis 199 /* diff.h */
vtools_genesis 200 extern struct change *find_hunk(struct change *);
vtools_genesis 201
vtools_genesis 202 extern void pr_unidiff_hunk(struct change *);
vtools_genesis 203
vtools_genesis 204 /* io.c */
vtools_genesis 205 extern void file_block_read(struct file_data *, size_t);
vtools_genesis 206
vtools_genesis 207 extern bool read_files(struct file_data[], bool);
vtools_genesis 208
vtools_genesis 209 /* util.c */
vtools_genesis 210
vtools_genesis 211 extern bool lines_differ(char const *, char const *);
vtools_genesis 212
vtools_genesis 213 extern lin translate_line_number(struct file_data const *, lin);
vtools_genesis 214
vtools_genesis 215 extern struct change *find_change(struct change *);
vtools_genesis 216
vtools_genesis 217 extern void *zalloc(size_t);
vtools_genesis 218
vtools_genesis 219 extern enum changes analyze_hunk(struct change *, lin *, lin *, lin *, lin *);
vtools_genesis 220
vtools_genesis 221 extern void begin_output(void);
vtools_genesis 222
vtools_genesis 223 extern void debug_script(struct change *);
vtools_genesis 224
vtools_genesis 225 extern void fatal(char const *) __attribute__((noreturn));
vtools_genesis 226
vtools_genesis 227 extern void finish_output(void);
vtools_genesis 228
vtools_genesis 229 extern void message(char const *, char const *, char const *);
vtools_genesis 230
vtools_genesis 231 extern void message5(char const *, char const *, char const *,
vtools_genesis 232 char const *, char const *);
vtools_genesis 233
vtools_genesis 234 extern void output_1_line(char const *, char const *);
vtools_genesis 235
vtools_genesis 236 extern void perror_with_name(char const *);
vtools_genesis 237
vtools_genesis 238 extern void pfatal_with_name(char const *) __attribute__((noreturn));
vtools_genesis 239
vtools_genesis 240 extern void print_1_line(char const *, char const *const *);
vtools_genesis 241
vtools_genesis 242 extern void print_1_line_nl(char const *, char const *const *, bool);
vtools_genesis 243
vtools_genesis 244 extern void print_script(struct change *);
vtools_genesis 245
vtools_genesis 246 extern void setup_output(char const *, char const *, bool);
vtools_genesis 247
vtools_genesis 248 extern void translate_range(struct file_data const *, lin, lin,
vtools_genesis 249 printint *, printint *);