/* Shared definitions for GNU DIFF12Copyright (C) 1988, 1989, 1991, 1992, 1993, 1994, 1995, 1998, 2001,32002, 2004 Free Software Foundation, Inc.45This file is part of GNU DIFF.67GNU DIFF is free software; you can redistribute it and/or modify8it under the terms of the GNU General Public License as published by9the Free Software Foundation; either version 2, or (at your option)10any later version.1112GNU DIFF is distributed in the hope that it will be useful,13but WITHOUT ANY WARRANTY; without even the implied warranty of14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15GNU General Public License for more details.1617You should have received a copy of the GNU General Public License18along with this program; see the file COPYING.19If not, write to the Free Software Foundation,2059 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */2122#include "system.h"23#include <regex.h>24#include <stdio.h>25#include <unlocked-io.h>2627/* What kind of changes a hunk contains. */28enum changes29{30/* No changes: lines common to both files. */31UNCHANGED,3233/* Deletes only: lines taken from just the first file. */34OLD,3536/* Inserts only: lines taken from just the second file. */37NEW,3839/* Both deletes and inserts: a hunk containing both old and new lines. */40CHANGED41};4243/* Variables for command line options */4445#ifndef GDIFF_MAIN46# define XTERN extern47#else48# define XTERN49#endif5051enum output_style52{53/* No output style specified. */54OUTPUT_UNSPECIFIED,5556/* Default output style. */57OUTPUT_NORMAL,5859/* Output the differences with lines of context before and after (-c). */60OUTPUT_CONTEXT,6162/* Output the differences in a unified context diff format (-u). */63OUTPUT_UNIFIED,6465/* Output the differences as commands suitable for `ed' (-e). */66OUTPUT_ED,6768/* Output the diff as a forward ed script (-f). */69OUTPUT_FORWARD_ED,7071/* Like -f, but output a count of changed lines in each "command" (-n). */72OUTPUT_RCS,7374/* Output merged #ifdef'd file (-D). */75OUTPUT_IFDEF,7677/* Output sdiff style (-y). */78OUTPUT_SDIFF79};8081/* True for output styles that are robust,82i.e. can handle a file that ends in a non-newline. */83#define ROBUST_OUTPUT_STYLE(S) ((S) != OUTPUT_ED && (S) != OUTPUT_FORWARD_ED)8485XTERN enum output_style output_style;8687/* Nonzero if output cannot be generated for identical files. */88XTERN bool no_diff_means_no_output;8990/* Number of lines of context to show in each set of diffs.91This is zero when context is not to be shown. */92XTERN lin context;9394/* Consider all files as text files (-a).95Don't interpret codes over 0177 as implying a "binary file". */96XTERN bool text;9798/* Number of lines to keep in identical prefix and suffix. */99XTERN lin horizon_lines;100101/* The significance of white space during comparisons. */102XTERN enum103{104/* All white space is significant (the default). */105IGNORE_NO_WHITE_SPACE,106107/* Ignore changes due to tab expansion (-E). */108IGNORE_TAB_EXPANSION,109110/* Ignore changes in horizontal white space (-b). */111IGNORE_SPACE_CHANGE,112113/* Ignore all horizontal white space (-w). */114IGNORE_ALL_SPACE115} ignore_white_space;116117/* Ignore changes that affect only blank lines (-B). */118XTERN bool ignore_blank_lines;119120/* Files can be compared byte-by-byte, as if they were binary.121This depends on various options. */122XTERN bool files_can_be_treated_as_binary;123124/* Ignore differences in case of letters (-i). */125XTERN bool ignore_case;126127/* Ignore differences in case of letters in file names. */128XTERN bool ignore_file_name_case;129130/* File labels for `-c' output headers (--label). */131XTERN char *file_label[2];132133/* Regexp to identify function-header lines (-F). */134XTERN struct re_pattern_buffer function_regexp;135136/* Ignore changes that affect only lines matching this regexp (-I). */137XTERN struct re_pattern_buffer ignore_regexp;138139/* Say only whether files differ, not how (-q). */140XTERN bool brief;141142/* Expand tabs in the output so the text lines up properly143despite the characters added to the front of each line (-t). */144XTERN bool expand_tabs;145146/* Number of columns between tab stops. */147XTERN size_t tabsize;148149/* Use a tab in the output, rather than a space, before the text of an150input line, so as to keep the proper alignment in the input line151without changing the characters in it (-T). */152XTERN bool initial_tab;153154/* Remove trailing carriage returns from input. */155XTERN bool strip_trailing_cr;156157/* In directory comparison, specify file to start with (-S).158This is used for resuming an aborted comparison.159All file names less than this name are ignored. */160XTERN char const *starting_file;161162/* Pipe each file's output through pr (-l). */163XTERN bool paginate;164165/* Line group formats for unchanged, old, new, and changed groups. */166XTERN char const *group_format[CHANGED + 1];167168/* Line formats for unchanged, old, and new lines. */169XTERN char const *line_format[NEW + 1];170171/* If using OUTPUT_SDIFF print extra information to help the sdiff filter. */172XTERN bool sdiff_merge_assist;173174/* Tell OUTPUT_SDIFF to show only the left version of common lines. */175XTERN bool left_column;176177/* Tell OUTPUT_SDIFF to not show common lines. */178XTERN bool suppress_common_lines;179180/* The half line width and column 2 offset for OUTPUT_SDIFF. */181XTERN size_t sdiff_half_width;182XTERN size_t sdiff_column2_offset;183184/* String containing all the command options diff received,185with spaces between and at the beginning but none at the end.186If there were no options given, this string is empty. */187XTERN char *switch_string;188189/* Use heuristics for better speed with large files with a small190density of changes. */191XTERN bool speed_large_files;192193/* Patterns that match file names to be excluded. */194XTERN struct exclude *excluded;195196/* Don't discard lines. This makes things slower (sometimes much197slower) but will find a guaranteed minimal set of changes. */198XTERN bool minimal;199200/* Name of program the user invoked (for error messages). */201XTERN char *program_name;202203/* The strftime format to use for time strings. */204XTERN char const *time_format;205206/* The result of comparison is an "edit script": a chain of `struct change'.207Each `struct change' represents one place where some lines are deleted208and some are inserted.209210LINE0 and LINE1 are the first affected lines in the two files (origin 0).211DELETED is the number of lines deleted here from file 0.212INSERTED is the number of lines inserted here in file 1.213214If DELETED is 0 then LINE0 is the number of the line before215which the insertion was done; vice versa for INSERTED and LINE1. */216217struct change218{219struct change *link; /* Previous or next edit command */220lin inserted; /* # lines of file 1 changed here. */221lin deleted; /* # lines of file 0 changed here. */222lin line0; /* Line number of 1st deleted line. */223lin line1; /* Line number of 1st inserted line. */224bool ignore; /* Flag used in context.c. */225};226227/* Structures that describe the input files. */228229/* Data on one input file being compared. */230231struct file_data {232int desc; /* File descriptor */233char const *name; /* File name */234struct stat stat; /* File status */235236/* Buffer in which text of file is read. */237word *buffer;238239/* Allocated size of buffer, in bytes. Always a multiple of240sizeof *buffer. */241size_t bufsize;242243/* Number of valid bytes now in the buffer. */244size_t buffered;245246/* Array of pointers to lines in the file. */247char const **linbuf;248249/* linbuf_base <= buffered_lines <= valid_lines <= alloc_lines.250linebuf[linbuf_base ... buffered_lines - 1] are possibly differing.251linebuf[linbuf_base ... valid_lines - 1] contain valid data.252linebuf[linbuf_base ... alloc_lines - 1] are allocated. */253lin linbuf_base, buffered_lines, valid_lines, alloc_lines;254255/* Pointer to end of prefix of this file to ignore when hashing. */256char const *prefix_end;257258/* Count of lines in the prefix.259There are this many lines in the file before linbuf[0]. */260lin prefix_lines;261262/* Pointer to start of suffix of this file to ignore when hashing. */263char const *suffix_begin;264265/* Vector, indexed by line number, containing an equivalence code for266each line. It is this vector that is actually compared with that267of another file to generate differences. */268lin *equivs;269270/* Vector, like the previous one except that271the elements for discarded lines have been squeezed out. */272lin *undiscarded;273274/* Vector mapping virtual line numbers (not counting discarded lines)275to real ones (counting those lines). Both are origin-0. */276lin *realindexes;277278/* Total number of nondiscarded lines. */279lin nondiscarded_lines;280281/* Vector, indexed by real origin-0 line number,282containing 1 for a line that is an insertion or a deletion.283The results of comparison are stored here. */284char *changed;285286/* 1 if file ends in a line with no final newline. */287bool missing_newline;288289/* 1 if at end of file. */290bool eof;291292/* 1 more than the maximum equivalence value used for this or its293sibling file. */294lin equiv_max;295};296297/* The file buffer, considered as an array of bytes rather than298as an array of words. */299#define FILE_BUFFER(f) ((char *) (f)->buffer)300301/* Data on two input files being compared. */302303struct comparison304{305struct file_data file[2];306struct comparison const *parent; /* parent, if a recursive comparison */307};308309/* Describe the two files currently being compared. */310311XTERN struct file_data files[2];312313/* Stdio stream to output diffs to. */314315XTERN FILE *outfile;316317/* Declare various functions. */318319/* analyze.c */320int diff_2_files (struct comparison *);321322/* context.c */323void print_context_header (struct file_data[], bool);324void print_context_script (struct change *, bool);325326/* dir.c */327int diff_dirs (struct comparison const *, int (*) (struct comparison const *, char const *, char const *));328329/* ed.c */330void print_ed_script (struct change *);331void pr_forward_ed_script (struct change *);332333/* ifdef.c */334void print_ifdef_script (struct change *);335336/* io.c */337void file_block_read (struct file_data *, size_t);338bool read_files (struct file_data[], bool);339340/* normal.c */341void print_normal_script (struct change *);342343/* rcs.c */344void print_rcs_script (struct change *);345346/* side.c */347void print_sdiff_script (struct change *);348349/* util.c */350extern char const change_letter[4];351extern char const pr_program[];352char *concat (char const *, char const *, char const *);353char *dir_file_pathname (char const *, char const *);354bool lines_differ (char const *, char const *);355lin translate_line_number (struct file_data const *, lin);356struct change *find_change (struct change *);357struct change *find_reverse_change (struct change *);358void *zalloc (size_t);359enum changes analyze_hunk (struct change *, lin *, lin *, lin *, lin *);360void begin_output (void);361void debug_script (struct change *);362void fatal (char const *) __attribute__((noreturn));363void finish_output (void);364void message (char const *, char const *, char const *);365void message5 (char const *, char const *, char const *, char const *, char const *);366void output_1_line (char const *, char const *, char const *, char const *);367void perror_with_name (char const *);368void pfatal_with_name (char const *) __attribute__((noreturn));369void print_1_line (char const *, char const * const *);370void print_message_queue (void);371void print_number_range (char, struct file_data *, lin, lin);372void print_script (struct change *, struct change * (*) (struct change *), void (*) (struct change *));373void setup_output (char const *, char const *, bool);374void translate_range (struct file_data const *, lin, lin, long int *, long int *);375376377