home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / diff / dist / diff.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-23  |  9.5 KB  |  343 lines

  1. /* Shared definitions for GNU DIFF
  2.    Copyright (C) 1988, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <ctype.h>
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25.  
  26. #ifdef USG
  27. #include <time.h>
  28. #ifdef HAVE_NDIR
  29. #include <ndir.h>
  30. #else
  31. #include <dirent.h>
  32. #endif /* not HAVE_NDIR */
  33. #include <fcntl.h>
  34. #ifndef HAVE_DIRECT
  35. #define direct dirent
  36. #endif
  37. #else /* not USG */
  38. #include <sys/time.h>
  39. #include <sys/dir.h>
  40. #include <sys/file.h>
  41. #endif
  42.  
  43. #ifdef USG
  44. /* Define needed BSD functions in terms of sysV library.  */
  45.  
  46. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  47. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  48. #define bzero(s,n)    memset((s),0,(n))
  49.  
  50. #define dup2(f,t)    (close(t),fcntl((f),F_DUPFD,(t)))
  51.  
  52. #define vfork    fork
  53. #define index    strchr
  54. #define rindex    strrchr
  55. #endif
  56.  
  57. #include <errno.h>
  58. extern int      errno;
  59. extern int      sys_nerr;
  60. extern char    *sys_errlist[];
  61.  
  62. #define    EOS        (0)
  63. #define    FALSE        (0)
  64. #define TRUE        1
  65.  
  66. #define min(a,b) ((a) <= (b) ? (a) : (b))
  67. #define max(a,b) ((a) >= (b) ? (a) : (b))
  68.  
  69. #ifndef PR_FILE_NAME
  70. #define PR_FILE_NAME "/bin/pr"
  71. #endif
  72.  
  73. /* Support old-fashioned C compilers.  */
  74. #if defined (__STDC__) || defined (__GNUC__)
  75. #include "limits.h"
  76. #else
  77. #define INT_MAX 2147483647
  78. #define CHAR_BIT 8
  79. #endif
  80.  
  81. /* Support old-fashioned C compilers.  */
  82. #if !defined (__STDC__) && !defined (__GNUC__)
  83. #define const
  84. #endif
  85.  
  86. /* Variables for command line options */
  87.  
  88. #ifndef GDIFF_MAIN
  89. #define EXTERN extern
  90. #else
  91. #define EXTERN
  92. #endif
  93.  
  94. enum output_style {
  95.   /* Default output style.  */
  96.   OUTPUT_NORMAL,
  97.   /* Output the differences with lines of context before and after (-c).  */
  98.   OUTPUT_CONTEXT,
  99.   /* Output the differences as commands suitable for `ed' (-e).  */
  100.   OUTPUT_ED,
  101.   /* Output the diff as a forward ed script (-f).  */
  102.   OUTPUT_FORWARD_ED,
  103.   /* Like -f, but output a count of changed lines in each "command" (-n). */
  104.   OUTPUT_RCS,
  105.   /* Output merged #ifdef'd file (-D).  */
  106.   OUTPUT_IFDEF };
  107.  
  108. EXTERN enum output_style output_style;
  109.  
  110. /* Number of lines of context to show in each set of diffs.
  111.    This is zero when context is not to be shown.  */
  112. EXTERN int      context;
  113.  
  114. /* Consider all files as text files (-a).
  115.    Don't interpret codes over 0177 as implying a "binary file".  */
  116. EXTERN int    always_text_flag;
  117.  
  118. /* Ignore changes in horizontal whitespace (-b).  */
  119. EXTERN int      ignore_space_change_flag;
  120.  
  121. /* Ignore all horizontal whitespace (-w).  */
  122. EXTERN int      ignore_all_space_flag;
  123.  
  124. /* Ignore changes that affect only blank lines (-B).  */
  125. EXTERN int      ignore_blank_lines_flag;
  126.  
  127. /* Ignore changes that affect only lines matching this regexp (-I).  */
  128. EXTERN char    *ignore_regexp;
  129.  
  130. /* Result of regex-compilation of `ignore_regexp'.  */
  131. EXTERN struct re_pattern_buffer ignore_regexp_compiled;
  132.  
  133. /* 1 if lines may match even if their lengths are different.
  134.    This depends on various options.  */
  135. EXTERN int      length_varies;
  136.  
  137. /* Ignore differences in case of letters (-i).  */
  138. EXTERN int      ignore_case_flag;
  139.  
  140. /* Regexp to identify function-header lines (-F).  */
  141. EXTERN char    *function_regexp;
  142.  
  143. /* Result of regex-compilation of `function_regexp'.  */
  144. EXTERN struct re_pattern_buffer function_regexp_compiled;
  145.  
  146. /* Say only whether files differ, not how (-q).  */
  147. EXTERN int     no_details_flag;
  148.  
  149. /* Report files compared that match (-s).
  150.    Normally nothing is output when that happens.  */
  151. EXTERN int      print_file_same_flag;
  152.  
  153. /* character that ends a line.  Currently this is always `\n'.  */
  154. EXTERN char     line_end_char;
  155.  
  156. /* Output the differences with exactly 8 columns added to each line
  157.    so that any tabs in the text line up properly (-T).  */
  158. EXTERN int    tab_align_flag;
  159.  
  160. /* Expand tabs in the output so the text lines up properly
  161.    despite the characters added to the front of each line (-t).  */
  162. EXTERN int    tab_expand_flag;
  163.  
  164. /* In directory comparison, specify file to start with (-S).
  165.    All file names less than this name are ignored.  */
  166. EXTERN char    *dir_start_file;
  167.  
  168. /* If a file is new (appears in only one dir)
  169.    include its entire contents (-N).
  170.    Then `patch' would create the file with appropriate contents.  */
  171. EXTERN int    entire_new_file_flag;
  172.  
  173. /* Pipe each file's output through pr (-l).  */
  174. EXTERN int    paginate_flag;
  175.  
  176. /* String to use for #ifdef (-D).  */
  177. EXTERN char *    ifdef_string;
  178.  
  179. /* String containing all the command options diff received,
  180.    with spaces between and at the beginning but none at the end.
  181.    If there were no options given, this string is empty.  */
  182. EXTERN char *    switch_string;
  183.  
  184. /* Nonzero means use heuristics for better speed.  */
  185. EXTERN int    heuristic;
  186.  
  187. /* Name of program the user invoked (for error messages).  */
  188. EXTERN char *    program;
  189.  
  190. /* The result of comparison is an "edit script": a chain of `struct change'.
  191.    Each `struct change' represents one place where some lines are deleted
  192.    and some are inserted.
  193.    
  194.    LINE0 and LINE1 are the first affected lines in the two files (origin 0).
  195.    DELETED is the number of lines deleted here from file 0.
  196.    INSERTED is the number of lines inserted here in file 1.
  197.  
  198.    If DELETED is 0 then LINE0 is the number of the line before
  199.    which the insertion was done; vice versa for INSERTED and LINE1.  */
  200.  
  201. struct change
  202. {
  203.   struct change *link;        /* Previous or next edit command  */
  204.   int inserted;            /* # lines of file 1 changed here.  */
  205.   int deleted;            /* # lines of file 0 changed here.  */
  206.   int line0;            /* Line number of 1st deleted line.  */
  207.   int line1;            /* Line number of 1st inserted line.  */
  208.   char ignore;            /* Flag used in context.c */
  209. };
  210.  
  211. /* Structures that describe the input files.  */
  212.  
  213. /* Data on one line of text.  */
  214.  
  215. struct line_def {
  216.     char        *text;
  217.     int         length;
  218.     unsigned    hash;
  219. };
  220.  
  221. /* Data on one input file being compared.  */
  222.  
  223. struct file_data {
  224.     int             desc;    /* File descriptor  */
  225.     char           *name;    /* File name  */
  226.     struct stat     stat;    /* File status from fstat()  */
  227.     int             dir_p;    /* 1 if file is a directory  */
  228.  
  229.     /* Buffer in which text of file is read.  */
  230.     char *        buffer;
  231.     /* Allocated size of buffer.  */
  232.     int            bufsize;
  233.     /* Number of valid characters now in the buffer. */
  234.     int            buffered_chars;
  235.  
  236.     /* Array of data on analyzed lines of this chunk of this file.  */
  237.     struct line_def *linbuf;
  238.  
  239.     /* Allocated size of linbuf array (# of elements).  */
  240.     int            linbufsize;
  241.  
  242.     /* Number of elements of linbuf containing valid data. */
  243.     int            buffered_lines;
  244.  
  245.     /* Pointer to end of prefix of this file to ignore when hashing. */
  246.     char *prefix_end;
  247.  
  248.     /* Count of lines in the prefix. */
  249.     int prefix_lines;
  250.  
  251.     /* Pointer to start of suffix of this file to ignore when hashing. */
  252.     char *suffix_begin;
  253.  
  254.     /* Count of lines in the suffix. */
  255.     int suffix_lines;
  256.  
  257.     /* Vector, indexed by line number, containing an equivalence code for
  258.        each line.  It is this vector that is actually compared with that
  259.        of another file to generate differences. */
  260.     int           *equivs;
  261.  
  262.     /* Vector, like the previous one except that
  263.        the elements for discarded lines have been squeezed out.  */
  264.     int           *undiscarded;
  265.  
  266.     /* Vector mapping virtual line numbers (not counting discarded lines)
  267.        to real ones (counting those lines).  Both are origin-0.  */
  268.     int           *realindexes;
  269.  
  270.     /* Total number of nondiscarded lines. */
  271.     int            nondiscarded_lines;
  272.  
  273.     /* Vector, indexed by real origin-0 line number,
  274.        containing 1 for a line that is an insertion or a deletion.
  275.        The results of comparison are stored here.  */
  276.     char       *changed_flag;
  277.  
  278.     /* 1 if file ends in a line with no final newline. */
  279.     int            missing_newline;
  280.  
  281.     /* 1 more than the maximum equivalence value used for this or its
  282.        sibling file. */
  283.     int equiv_max;
  284.  
  285.     /* Table translating diff's internal line numbers 
  286.        to actual line numbers in the file.
  287.        This is needed only when some lines have been discarded.
  288.        The allocated size is always linbufsize
  289.        and the number of valid elements is buffered_lines.  */
  290.     int           *ltran;
  291. };
  292.  
  293. /* Describe the two files currently being compared.  */
  294.  
  295. struct file_data files[2];
  296.  
  297. /* Queue up one-line messages to be printed at the end,
  298.    when -l is specified.  Each message is recorded with a `struct msg'.  */
  299.  
  300. struct msg
  301. {
  302.   struct msg *next;
  303.   char *format;
  304.   char *arg1;
  305.   char *arg2;
  306. };
  307.  
  308. /* Head of the chain of queues messages.  */
  309.  
  310. EXTERN struct msg *msg_chain;
  311.  
  312. /* Tail of the chain of queues messages.  */
  313.  
  314. EXTERN struct msg *msg_chain_end;
  315.  
  316. /* Stdio stream to output diffs to.  */
  317.  
  318. EXTERN FILE *outfile;
  319.  
  320. /* Declare various functions.  */
  321.  
  322. #ifdef __STDC__
  323. #define VOID void
  324. #else
  325. #define VOID char
  326. #endif
  327. VOID *xmalloc ();
  328. VOID *xrealloc ();
  329. char *concat ();
  330. void free ();
  331. char *rindex ();
  332. char *index ();
  333.  
  334. void analyze_hunk ();
  335. void error ();
  336. void fatal ();
  337. void message ();
  338. void perror_with_name ();
  339. void pfatal_with_name ();
  340. void print_1_line ();
  341. void print_message_queue ();
  342. void print_script ();
  343.