Blame src/diff.h

Packit 33f14e
/* Shared definitions for GNU DIFF
Packit 33f14e
Packit 33f14e
   Copyright (C) 1988-1989, 1991-1995, 1998, 2001-2002, 2004, 2009-2013,
Packit 33f14e
   2015-2017 Free Software Foundation, Inc.
Packit 33f14e
Packit 33f14e
   This file is part of GNU DIFF.
Packit 33f14e
Packit 33f14e
   This program is free software: you can redistribute it and/or modify
Packit 33f14e
   it under the terms of the GNU General Public License as published by
Packit 33f14e
   the Free Software Foundation, either version 3 of the License, or
Packit 33f14e
   (at your option) any later version.
Packit 33f14e
Packit 33f14e
   This program is distributed in the hope that it will be useful,
Packit 33f14e
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 33f14e
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 33f14e
   GNU General Public License for more details.
Packit 33f14e
Packit 33f14e
   You should have received a copy of the GNU General Public License
Packit 33f14e
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 33f14e
Packit 33f14e
#include "system.h"
Packit 33f14e
#include <regex.h>
Packit 33f14e
#include <stdio.h>
Packit 33f14e
#include <unlocked-io.h>
Packit 33f14e
Packit 33f14e
/* What kind of changes a hunk contains.  */
Packit 33f14e
enum changes
Packit 33f14e
{
Packit 33f14e
  /* No changes: lines common to both files.  */
Packit 33f14e
  UNCHANGED,
Packit 33f14e
Packit 33f14e
  /* Deletes only: lines taken from just the first file.  */
Packit 33f14e
  OLD,
Packit 33f14e
Packit 33f14e
  /* Inserts only: lines taken from just the second file.  */
Packit 33f14e
  NEW,
Packit 33f14e
Packit 33f14e
  /* Both deletes and inserts: a hunk containing both old and new lines.  */
Packit 33f14e
  CHANGED
Packit 33f14e
};
Packit 33f14e
Packit 33f14e
/* When colors should be used in the output.  */
Packit 33f14e
enum colors_style
Packit 33f14e
{
Packit 33f14e
  /* Never output colors.  */
Packit 33f14e
  NEVER,
Packit 33f14e
Packit 33f14e
  /* Output colors if the output is a terminal.  */
Packit 33f14e
  AUTO,
Packit 33f14e
Packit 33f14e
  /* Always output colors.  */
Packit 33f14e
  ALWAYS,
Packit 33f14e
};
Packit 33f14e

Packit 33f14e
/* Variables for command line options */
Packit 33f14e
Packit 33f14e
#ifndef GDIFF_MAIN
Packit 33f14e
# define XTERN extern
Packit 33f14e
#else
Packit 33f14e
# define XTERN
Packit 33f14e
#endif
Packit 33f14e
Packit 33f14e
enum output_style
Packit 33f14e
{
Packit 33f14e
  /* No output style specified.  */
Packit 33f14e
  OUTPUT_UNSPECIFIED,
Packit 33f14e
Packit 33f14e
  /* Default output style.  */
Packit 33f14e
  OUTPUT_NORMAL,
Packit 33f14e
Packit 33f14e
  /* Output the differences with lines of context before and after (-c).  */
Packit 33f14e
  OUTPUT_CONTEXT,
Packit 33f14e
Packit 33f14e
  /* Output the differences in a unified context diff format (-u).  */
Packit 33f14e
  OUTPUT_UNIFIED,
Packit 33f14e
Packit 33f14e
  /* Output the differences as commands suitable for 'ed' (-e).  */
Packit 33f14e
  OUTPUT_ED,
Packit 33f14e
Packit 33f14e
  /* Output the diff as a forward ed script (-f).  */
Packit 33f14e
  OUTPUT_FORWARD_ED,
Packit 33f14e
Packit 33f14e
  /* Like -f, but output a count of changed lines in each "command" (-n).  */
Packit 33f14e
  OUTPUT_RCS,
Packit 33f14e
Packit 33f14e
  /* Output merged #ifdef'd file (-D).  */
Packit 33f14e
  OUTPUT_IFDEF,
Packit 33f14e
Packit 33f14e
  /* Output sdiff style (-y).  */
Packit 33f14e
  OUTPUT_SDIFF
Packit 33f14e
};
Packit 33f14e
Packit 33f14e
/* True for output styles that are robust,
Packit 33f14e
   i.e. can handle a file that ends in a non-newline.  */
Packit 33f14e
#define ROBUST_OUTPUT_STYLE(S) ((S) != OUTPUT_ED && (S) != OUTPUT_FORWARD_ED)
Packit 33f14e
Packit 33f14e
XTERN enum output_style output_style;
Packit 33f14e
Packit 33f14e
/* Define the current color context used to print a line.  */
Packit 33f14e
XTERN enum colors_style colors_style;
Packit 33f14e
Packit 33f14e
/* Nonzero if output cannot be generated for identical files.  */
Packit 33f14e
XTERN bool no_diff_means_no_output;
Packit 33f14e
Packit 33f14e
/* Number of lines of context to show in each set of diffs.
Packit 33f14e
   This is zero when context is not to be shown.  */
Packit 33f14e
XTERN lin context;
Packit 33f14e
Packit 33f14e
/* Consider all files as text files (-a).
Packit 33f14e
   Don't interpret codes over 0177 as implying a "binary file".  */
Packit 33f14e
XTERN bool text;
Packit 33f14e
Packit 33f14e
/* Number of lines to keep in identical prefix and suffix.  */
Packit 33f14e
XTERN lin horizon_lines;
Packit 33f14e
Packit 33f14e
/* The significance of white space during comparisons.  */
Packit 33f14e
enum DIFF_white_space
Packit 33f14e
{
Packit 33f14e
  /* All white space is significant (the default).  */
Packit 33f14e
  IGNORE_NO_WHITE_SPACE,
Packit 33f14e
Packit 33f14e
  /* Ignore changes due to tab expansion (-E).  */
Packit 33f14e
  IGNORE_TAB_EXPANSION,
Packit 33f14e
Packit 33f14e
  /* Ignore changes in trailing horizontal white space (-Z).  */
Packit 33f14e
  IGNORE_TRAILING_SPACE,
Packit 33f14e
Packit 33f14e
  /* IGNORE_TAB_EXPANSION and IGNORE_TRAILING_SPACE are a special case
Packit 33f14e
     because they are independent and can be ORed together, yielding
Packit 33f14e
     IGNORE_TAB_EXPANSION_AND_TRAILING_SPACE.  */
Packit 33f14e
  IGNORE_TAB_EXPANSION_AND_TRAILING_SPACE,
Packit 33f14e
Packit 33f14e
  /* Ignore changes in horizontal white space (-b).  */
Packit 33f14e
  IGNORE_SPACE_CHANGE,
Packit 33f14e
Packit 33f14e
  /* Ignore all horizontal white space (-w).  */
Packit 33f14e
  IGNORE_ALL_SPACE
Packit 33f14e
};
Packit 33f14e
XTERN enum DIFF_white_space ignore_white_space;
Packit 33f14e
Packit 33f14e
/* Ignore changes that affect only blank lines (-B).  */
Packit 33f14e
XTERN bool ignore_blank_lines;
Packit 33f14e
Packit 33f14e
/* Files can be compared byte-by-byte, as if they were binary.
Packit 33f14e
   This depends on various options.  */
Packit 33f14e
XTERN bool files_can_be_treated_as_binary;
Packit 33f14e
Packit 33f14e
/* Ignore differences in case of letters (-i).  */
Packit 33f14e
XTERN bool ignore_case;
Packit 33f14e
Packit 33f14e
/* Ignore differences in case of letters in file names.  */
Packit 33f14e
XTERN bool ignore_file_name_case;
Packit 33f14e
Packit 33f14e
/* Act on symbolic links themselves rather than on their target
Packit 33f14e
   (--no-dereference).  */
Packit 33f14e
XTERN bool no_dereference_symlinks;
Packit 33f14e
Packit 33f14e
/* File labels for '-c' output headers (--label).  */
Packit 33f14e
XTERN char *file_label[2];
Packit 33f14e
Packit 33f14e
/* Regexp to identify function-header lines (-F).  */
Packit 33f14e
XTERN struct re_pattern_buffer function_regexp;
Packit 33f14e
Packit 33f14e
/* Ignore changes that affect only lines matching this regexp (-I).  */
Packit 33f14e
XTERN struct re_pattern_buffer ignore_regexp;
Packit 33f14e
Packit 33f14e
/* Say only whether files differ, not how (-q).  */
Packit 33f14e
XTERN bool brief;
Packit 33f14e
Packit 33f14e
/* Expand tabs in the output so the text lines up properly
Packit 33f14e
   despite the characters added to the front of each line (-t).  */
Packit 33f14e
XTERN bool expand_tabs;
Packit 33f14e
Packit 33f14e
/* Number of columns between tab stops.  */
Packit 33f14e
XTERN size_t tabsize;
Packit 33f14e
Packit 33f14e
/* Use a tab in the output, rather than a space, before the text of an
Packit 33f14e
   input line, so as to keep the proper alignment in the input line
Packit 33f14e
   without changing the characters in it (-T).  */
Packit 33f14e
XTERN bool initial_tab;
Packit 33f14e
Packit 33f14e
/* Do not output an initial space or tab before the text of an empty line.  */
Packit 33f14e
XTERN bool suppress_blank_empty;
Packit 33f14e
Packit 33f14e
/* Remove trailing carriage returns from input.  */
Packit 33f14e
XTERN bool strip_trailing_cr;
Packit 33f14e
Packit 33f14e
/* In directory comparison, specify file to start with (-S).
Packit 33f14e
   This is used for resuming an aborted comparison.
Packit 33f14e
   All file names less than this name are ignored.  */
Packit 33f14e
XTERN char const *starting_file;
Packit 33f14e
Packit 33f14e
/* Pipe each file's output through pr (-l).  */
Packit 33f14e
XTERN bool paginate;
Packit 33f14e
Packit 33f14e
/* Line group formats for unchanged, old, new, and changed groups.  */
Packit 33f14e
XTERN char const *group_format[CHANGED + 1];
Packit 33f14e
Packit 33f14e
/* Line formats for unchanged, old, and new lines.  */
Packit 33f14e
XTERN char const *line_format[NEW + 1];
Packit 33f14e
Packit 33f14e
/* If using OUTPUT_SDIFF print extra information to help the sdiff filter.  */
Packit 33f14e
XTERN bool sdiff_merge_assist;
Packit 33f14e
Packit 33f14e
/* Tell OUTPUT_SDIFF to show only the left version of common lines.  */
Packit 33f14e
XTERN bool left_column;
Packit 33f14e
Packit 33f14e
/* Tell OUTPUT_SDIFF to not show common lines.  */
Packit 33f14e
XTERN bool suppress_common_lines;
Packit 33f14e
Packit 33f14e
/* The half line width and column 2 offset for OUTPUT_SDIFF.  */
Packit 33f14e
XTERN size_t sdiff_half_width;
Packit 33f14e
XTERN size_t sdiff_column2_offset;
Packit 33f14e
Packit 33f14e
/* String containing all the command options diff received,
Packit 33f14e
   with spaces between and at the beginning but none at the end.
Packit 33f14e
   If there were no options given, this string is empty.  */
Packit 33f14e
XTERN char *switch_string;
Packit 33f14e
Packit 33f14e
/* Use heuristics for better speed with large files with a small
Packit 33f14e
   density of changes.  */
Packit 33f14e
XTERN bool speed_large_files;
Packit 33f14e
Packit 33f14e
/* Patterns that match file names to be excluded.  */
Packit 33f14e
XTERN struct exclude *excluded;
Packit 33f14e
Packit 33f14e
/* Don't discard lines.  This makes things slower (sometimes much
Packit 33f14e
   slower) but will find a guaranteed minimal set of changes.  */
Packit 33f14e
XTERN bool minimal;
Packit 33f14e
Packit 33f14e
/* The strftime format to use for time strings.  */
Packit 33f14e
XTERN char const *time_format;
Packit 33f14e

Packit 33f14e
/* The result of comparison is an "edit script": a chain of 'struct change'.
Packit 33f14e
   Each 'struct change' represents one place where some lines are deleted
Packit 33f14e
   and some are inserted.
Packit 33f14e
Packit 33f14e
   LINE0 and LINE1 are the first affected lines in the two files (origin 0).
Packit 33f14e
   DELETED is the number of lines deleted here from file 0.
Packit 33f14e
   INSERTED is the number of lines inserted here in file 1.
Packit 33f14e
Packit 33f14e
   If DELETED is 0 then LINE0 is the number of the line before
Packit 33f14e
   which the insertion was done; vice versa for INSERTED and LINE1.  */
Packit 33f14e
Packit 33f14e
struct change
Packit 33f14e
{
Packit 33f14e
  struct change *link;		/* Previous or next edit command  */
Packit 33f14e
  lin inserted;			/* # lines of file 1 changed here.  */
Packit 33f14e
  lin deleted;			/* # lines of file 0 changed here.  */
Packit 33f14e
  lin line0;			/* Line number of 1st deleted line.  */
Packit 33f14e
  lin line1;			/* Line number of 1st inserted line.  */
Packit 33f14e
  bool ignore;			/* Flag used in context.c.  */
Packit 33f14e
};
Packit 33f14e

Packit 33f14e
/* Structures that describe the input files.  */
Packit 33f14e
Packit 33f14e
/* Data on one input file being compared.  */
Packit 33f14e
Packit 33f14e
struct file_data {
Packit 33f14e
    int             desc;	/* File descriptor  */
Packit 33f14e
    char const      *name;	/* File name  */
Packit 33f14e
    struct stat     stat;	/* File status */
Packit 33f14e
Packit 33f14e
    /* Buffer in which text of file is read.  */
Packit 33f14e
    word *buffer;
Packit 33f14e
Packit 33f14e
    /* Allocated size of buffer, in bytes.  Always a multiple of
Packit 33f14e
       sizeof *buffer.  */
Packit 33f14e
    size_t bufsize;
Packit 33f14e
Packit 33f14e
    /* Number of valid bytes now in the buffer.  */
Packit 33f14e
    size_t buffered;
Packit 33f14e
Packit 33f14e
    /* Array of pointers to lines in the file.  */
Packit 33f14e
    char const **linbuf;
Packit 33f14e
Packit 33f14e
    /* linbuf_base <= buffered_lines <= valid_lines <= alloc_lines.
Packit 33f14e
       linebuf[linbuf_base ... buffered_lines - 1] are possibly differing.
Packit 33f14e
       linebuf[linbuf_base ... valid_lines - 1] contain valid data.
Packit 33f14e
       linebuf[linbuf_base ... alloc_lines - 1] are allocated.  */
Packit 33f14e
    lin linbuf_base, buffered_lines, valid_lines, alloc_lines;
Packit 33f14e
Packit 33f14e
    /* Pointer to end of prefix of this file to ignore when hashing.  */
Packit 33f14e
    char const *prefix_end;
Packit 33f14e
Packit 33f14e
    /* Count of lines in the prefix.
Packit 33f14e
       There are this many lines in the file before linbuf[0].  */
Packit 33f14e
    lin prefix_lines;
Packit 33f14e
Packit 33f14e
    /* Pointer to start of suffix of this file to ignore when hashing.  */
Packit 33f14e
    char const *suffix_begin;
Packit 33f14e
Packit 33f14e
    /* Vector, indexed by line number, containing an equivalence code for
Packit 33f14e
       each line.  It is this vector that is actually compared with that
Packit 33f14e
       of another file to generate differences.  */
Packit 33f14e
    lin *equivs;
Packit 33f14e
Packit 33f14e
    /* Vector, like the previous one except that
Packit 33f14e
       the elements for discarded lines have been squeezed out.  */
Packit 33f14e
    lin *undiscarded;
Packit 33f14e
Packit 33f14e
    /* Vector mapping virtual line numbers (not counting discarded lines)
Packit 33f14e
       to real ones (counting those lines).  Both are origin-0.  */
Packit 33f14e
    lin *realindexes;
Packit 33f14e
Packit 33f14e
    /* Total number of nondiscarded lines.  */
Packit 33f14e
    lin nondiscarded_lines;
Packit 33f14e
Packit 33f14e
    /* Vector, indexed by real origin-0 line number,
Packit 33f14e
       containing 1 for a line that is an insertion or a deletion.
Packit 33f14e
       The results of comparison are stored here.  */
Packit 33f14e
    char *changed;
Packit 33f14e
Packit 33f14e
    /* 1 if file ends in a line with no final newline.  */
Packit 33f14e
    bool missing_newline;
Packit 33f14e
Packit 33f14e
    /* 1 if at end of file.  */
Packit 33f14e
    bool eof;
Packit 33f14e
Packit 33f14e
    /* 1 more than the maximum equivalence value used for this or its
Packit 33f14e
       sibling file.  */
Packit 33f14e
    lin equiv_max;
Packit 33f14e
};
Packit 33f14e
Packit 33f14e
/* The file buffer, considered as an array of bytes rather than
Packit 33f14e
   as an array of words.  */
Packit 33f14e
#define FILE_BUFFER(f) ((char *) (f)->buffer)
Packit 33f14e
Packit 33f14e
/* Data on two input files being compared.  */
Packit 33f14e
Packit 33f14e
struct comparison
Packit 33f14e
  {
Packit 33f14e
    struct file_data file[2];
Packit 33f14e
    struct comparison const *parent;  /* parent, if a recursive comparison */
Packit 33f14e
  };
Packit 33f14e
Packit 33f14e
/* Describe the two files currently being compared.  */
Packit 33f14e
Packit 33f14e
XTERN struct file_data files[2];
Packit 33f14e

Packit 33f14e
/* Stdio stream to output diffs to.  */
Packit 33f14e
Packit 33f14e
XTERN FILE *outfile;
Packit 33f14e

Packit 33f14e
/* Declare various functions.  */
Packit 33f14e
Packit 33f14e
/* analyze.c */
Packit 33f14e
extern int diff_2_files (struct comparison *);
Packit 33f14e
Packit 33f14e
/* context.c */
Packit 33f14e
extern void print_context_header (struct file_data[], char const * const *, bool);
Packit 33f14e
extern void print_context_script (struct change *, bool);
Packit 33f14e
Packit 33f14e
/* dir.c */
Packit 33f14e
extern int diff_dirs (struct comparison const *,
Packit 33f14e
                      int (*) (struct comparison const *,
Packit 33f14e
                               char const *, char const *));
Packit 33f14e
extern char *find_dir_file_pathname (char const *, char const *);
Packit 33f14e
Packit 33f14e
/* ed.c */
Packit 33f14e
extern void print_ed_script (struct change *);
Packit 33f14e
extern void pr_forward_ed_script (struct change *);
Packit 33f14e
Packit 33f14e
/* ifdef.c */
Packit 33f14e
extern void print_ifdef_script (struct change *);
Packit 33f14e
Packit 33f14e
/* io.c */
Packit 33f14e
extern void file_block_read (struct file_data *, size_t);
Packit 33f14e
extern bool read_files (struct file_data[], bool);
Packit 33f14e
Packit 33f14e
/* normal.c */
Packit 33f14e
extern void print_normal_script (struct change *);
Packit 33f14e
Packit 33f14e
/* rcs.c */
Packit 33f14e
extern void print_rcs_script (struct change *);
Packit 33f14e
Packit 33f14e
/* side.c */
Packit 33f14e
extern void print_sdiff_script (struct change *);
Packit 33f14e
Packit 33f14e
/* util.c */
Packit 33f14e
extern char const change_letter[4];
Packit 33f14e
extern char const pr_program[];
Packit 33f14e
extern char *concat (char const *, char const *, char const *);
Packit Service 3a92f2
extern bool lines_differ (char const *, char const *) _GL_ATTRIBUTE_PURE;
Packit 33f14e
extern lin translate_line_number (struct file_data const *, lin);
Packit 33f14e
extern struct change *find_change (struct change *);
Packit 33f14e
extern struct change *find_reverse_change (struct change *);
Packit 33f14e
extern void *zalloc (size_t);
Packit 33f14e
extern enum changes analyze_hunk (struct change *, lin *, lin *, lin *, lin *);
Packit 33f14e
extern void begin_output (void);
Packit 33f14e
extern void debug_script (struct change *);
Packit 33f14e
extern void fatal (char const *) __attribute__((noreturn));
Packit 33f14e
extern void finish_output (void);
Packit 33f14e
extern void message (char const *, char const *, char const *);
Packit 33f14e
extern void message5 (char const *, char const *, char const *,
Packit 33f14e
                      char const *, char const *);
Packit 33f14e
extern void output_1_line (char const *, char const *, char const *,
Packit 33f14e
                           char const *);
Packit 33f14e
extern void perror_with_name (char const *);
Packit 33f14e
extern void pfatal_with_name (char const *) __attribute__((noreturn));
Packit 33f14e
extern void print_1_line (char const *, char const * const *);
Packit 33f14e
extern void print_1_line_nl (char const *, char const * const *, bool);
Packit 33f14e
extern void print_message_queue (void);
Packit 33f14e
extern void print_number_range (char, struct file_data *, lin, lin);
Packit 33f14e
extern void print_script (struct change *, struct change * (*) (struct change *),
Packit 33f14e
                          void (*) (struct change *));
Packit 33f14e
extern void setup_output (char const *, char const *, bool);
Packit 33f14e
extern void translate_range (struct file_data const *, lin, lin,
Packit 33f14e
                             printint *, printint *);
Packit 33f14e
Packit 33f14e
enum color_context
Packit 33f14e
{
Packit 33f14e
  HEADER_CONTEXT,
Packit 33f14e
  ADD_CONTEXT,
Packit 33f14e
  DELETE_CONTEXT,
Packit 33f14e
  RESET_CONTEXT,
Packit 33f14e
  LINE_NUMBER_CONTEXT,
Packit 33f14e
};
Packit 33f14e
Packit 33f14e
XTERN bool presume_output_tty;
Packit 33f14e
Packit 33f14e
extern void set_color_context (enum color_context color_context);
Packit 33f14e
extern void set_color_palette (char const *palette);