-
+ 8389679F305C168B019ADB6AE495F60C1B71EFC35052D94B6672829B437E0607E77D66D457C81128110B6ECEFCFC41ADB55EFAB8AF91E9B7C76DAF5597EF08F6
vtools/src/diff.h
(0 . 0)(1 . 249)
3889 #include "system.h"
3890 #include <stdio.h>
3891 #include <string.h>
3892 #include <sys/stat.h>
3893
3894 /* What kind of changes a hunk contains. */
3895 enum changes {
3896 /* No changes: lines common to both files. */
3897 UNCHANGED,
3898
3899 /* Deletes only: lines taken from just the first file. */
3900 OLD,
3901
3902 /* Inserts only: lines taken from just the second file. */
3903 NEW,
3904
3905 /* Both deletes and inserts: a hunk containing both old and new lines. */
3906 CHANGED
3907 };
3908
3909 /* Variables for command line options */
3910
3911 #ifndef GDIFF_MAIN
3912 # define XTERN extern
3913 #else
3914 # define XTERN
3915 #endif
3916
3917 /* Number of lines of context to show in each set of diffs. This is
3918 zero when context is not to be shown. */
3919 XTERN lin context;
3920
3921 /* Consider all files as text files (-a). Don't interpret codes over
3922 0177 as implying a "binary file". */
3923 XTERN bool text;
3924
3925 /* Number of lines to keep in identical prefix and suffix. */
3926 XTERN lin horizon_lines;
3927
3928 /* Files can be compared byte-by-byte, as if they were binary. This
3929 depends on various options. */
3930 XTERN bool files_can_be_treated_as_binary;
3931
3932 /* File labels for |-c| output headers (|--label|). */
3933 XTERN char *file_label[2];
3934
3935 /* Say only whether files differ, not how (|-q|). */
3936 XTERN bool brief;
3937
3938 /* Do not output an initial space or tab before the text of an empty line. */
3939 XTERN bool suppress_blank_empty;
3940
3941 /* In directory comparison, specify file to start with (|-S|). This
3942 is used for resuming an aborted comparison. All file names less
3943 than this name are ignored. */
3944 XTERN char const *starting_file;
3945
3946 /* String containing all the command options diff received, with
3947 spaces between and at the beginning but none at the end. If there
3948 were no options given, this string is empty. */
3949 XTERN char *switch_string;
3950
3951 /* Use heuristics for better speed with large files with a small
3952 density of changes. */
3953 XTERN bool speed_large_files;
3954
3955 /* Don't discard lines. This makes things slower (sometimes much
3956 slower) but will find a guaranteed minimal set of changes. */
3957 XTERN bool minimal;
3958
3959 /* The result of comparison is an ``edit script'': a chain of |struct
3960 change|. Each |struct change| represents one place where some
3961 lines are deleted and some are inserted.
3962
3963 |line0| and |line1| are the first affected lines in the two files
3964 (origin 0). |deleted| is the number of lines deleted here from file
3965 0. |inserted| is the number of lines inserted here in file 1.
3966
3967 If |deleted| is 0 then |line0| is the number of the line before
3968 which the insertion was done; vice versa for |inserted| and
3969 |line1|. */
3970
3971 struct change {
3972 struct change *link; /* Previous or next edit command */
3973 lin inserted; /* \# lines of file 1 changed here. */
3974 lin deleted; /* \# lines of file 0 changed here. */
3975 lin line0; /* Line number of 1st deleted line. */
3976 lin line1; /* Line number of 1st inserted line. */
3977 bool ignore; /* Flag used in |context.c|. */
3978 };
3979
3980 /* Structures that describe the input files. */
3981
3982 /* Data on one input file being compared. */
3983
3984 struct file_data {
3985 int desc; /* File descriptor */
3986 char const *name; /* File name */
3987 struct stat stat; /* File status */
3988
3989 /* Buffer in which text of file is read. */
3990 word *buffer;
3991
3992 /* Allocated size of buffer, in bytes. Always a multiple of
3993 sizeof |*buffer|. */
3994 size_t bufsize;
3995
3996 /* Number of valid bytes now in the buffer. */
3997 size_t buffered;
3998
3999 /* Array of pointers to lines in the file. */
4000 char const **linbuf;
4001
4002 /* |linbuf_base <= buffered_lines <= valid_lines <= alloc_lines|.
4003 |linebuf[linbuf_base ... buffered_lines - 1]| are possibly differing.
4004 |linebuf[linbuf_base ... valid_lines - 1]| contain valid data.
4005 |linebuf[linbuf_base ... alloc_lines - 1]| are allocated. */
4006 lin linbuf_base, buffered_lines, valid_lines, alloc_lines;
4007
4008 /* Pointer to end of prefix of this file to ignore when hashing. */
4009 char const *prefix_end;
4010
4011 /* Count of lines in the prefix. There are this many lines in the
4012 file before |linbuf[0]|. */
4013 lin prefix_lines;
4014
4015 /* Pointer to start of suffix of this file to ignore when hashing. */
4016 char const *suffix_begin;
4017
4018 /* Vector, indexed by line number, containing an equivalence code
4019 for each line. It is this vector that is actually compared
4020 with that of another file to generate differences. */
4021 lin *equivs;
4022
4023 /* Vector, like the previous one except that the elements for
4024 discarded lines have been squeezed out. */
4025 lin *undiscarded;
4026
4027 /* Vector mapping virtual line numbers (not counting discarded
4028 lines) to real ones (counting those lines). Both are
4029 $origin-0$. */
4030 lin *realindexes;
4031
4032 /* Total number of nondiscarded lines. */
4033 lin nondiscarded_lines;
4034
4035 /* Vector, indexed by real $origin-0$ line number, containing 1
4036 for a line that is an insertion or a deletion. The results of
4037 comparison are stored here. */
4038 char *changed;
4039
4040 /* 1 if file ends in a line with no final newline. */
4041 bool missing_newline;
4042
4043 /* 1 if at end of file. */
4044 bool eof;
4045
4046 /* 1 more than the maximum equivalence value used for this or its
4047 sibling file. */
4048 lin equiv_max;
4049 };
4050
4051 /* The file buffer, considered as an array of bytes rather than as an
4052 array of words. */
4053 #define FILE_BUFFER(f) ((char *) (f)->buffer)
4054
4055 /* Data on two input files being compared. */
4056
4057 struct comparison {
4058 struct file_data file[2];
4059 struct comparison const *parent; /* parent, if a recursive comparison */
4060 };
4061
4062 /* Describe the two files currently being compared. */
4063
4064 XTERN struct file_data files[2];
4065
4066 /* Stdio stream to output diffs to. */
4067
4068 XTERN FILE *outfile;
4069
4070 /* Declare various functions. */
4071
4072 /* analyze.c */
4073 extern int diff_2_files(struct comparison *);
4074
4075 /* context.c */
4076 extern void print_context_header(struct file_data[], char const *const *);
4077
4078 extern void print_context_script(struct change *);
4079
4080 /* dir.c */
4081 extern int diff_dirs(struct comparison const *,
4082 int (*)(struct comparison const *,
4083 char const *, char const *));
4084
4085 extern char *find_dir_file_pathname(char const *, char const *);
4086
4087 /* diff.h */
4088 extern struct change *find_hunk(struct change *);
4089
4090 extern void pr_unidiff_hunk(struct change *);
4091
4092 /* io.c */
4093 extern void file_block_read(struct file_data *, size_t);
4094
4095 extern bool read_files(struct file_data[], bool);
4096
4097 /* util.c */
4098
4099 extern bool lines_differ(char const *, char const *);
4100
4101 extern lin translate_line_number(struct file_data const *, lin);
4102
4103 extern struct change *find_change(struct change *);
4104
4105 extern void *zalloc(size_t);
4106
4107 extern enum changes analyze_hunk(struct change *, lin *, lin *, lin *, lin *);
4108
4109 extern void begin_output(void);
4110
4111 extern void debug_script(struct change *);
4112
4113 extern void fatal(char const *) __attribute__((noreturn));
4114
4115 extern void finish_output(void);
4116
4117 extern void message(char const *, char const *, char const *);
4118
4119 extern void message5(char const *, char const *, char const *,
4120 char const *, char const *);
4121
4122 extern void output_1_line(char const *, char const *);
4123
4124 extern void perror_with_name(char const *);
4125
4126 extern void pfatal_with_name(char const *) __attribute__((noreturn));
4127
4128 extern void print_1_line(char const *, char const *const *);
4129
4130 extern void print_1_line_nl(char const *, char const *const *, bool);
4131
4132 extern void print_script(struct change *);
4133
4134 extern void setup_output(char const *, char const *, bool);
4135
4136 extern void translate_range(struct file_data const *, lin, lin,
4137 printint *, printint *);