Blame src/diff.h

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