home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / GNU Diff Sources / GNU DIFF 1.15b Sources / diff.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-02  |  11.7 KB  |  443 lines  |  [TEXT/ALFA]

  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. #ifndef    THINK_C
  24.  
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #define    NEWLINE    '\n'
  28.  
  29. #else
  30.  
  31. #include    <unix.h>
  32. #include    <fcntl.h>
  33. #include    <stdlib.h>
  34. #include    <string.h>
  35. #include    <time.h>
  36. #include    <stddef.h>
  37. #include    "stat.h"
  38. #define    NEWLINE    '\r'
  39. #define    STDC_HEADERS
  40.  
  41. #define    DIRECT_DIFF    1
  42.  
  43. //pascal    void    Debugger(void) = { 0xA9FF };
  44. //pascal    void    DebugStr(unsigned char *) = { 0xABFF };
  45.  
  46. #endif    THINK_C
  47.  
  48. #define    LONG    long
  49. #define    SIZET_MAX    0x7FFFFFFF
  50.  
  51. #define    internal_err    -9999
  52. #define    memory_err    -108        /* Arbitrary */
  53. typedef    int    diff_err;
  54.  
  55. #ifdef notdef
  56. #ifdef USG
  57. #include <time.h>
  58. #ifdef HAVE_NDIR
  59. #ifdef NDIR_IN_SYS
  60. #include <sys/ndir.h>
  61. #else
  62. #include <ndir.h>
  63. #endif /* not NDIR_IN_SYS */
  64. #else
  65. #include <dirent.h>
  66. #endif /* not HAVE_NDIR */
  67. #include <fcntl.h>
  68. #ifndef HAVE_DIRECT
  69. #define direct dirent
  70. #endif
  71. #else /* not USG */
  72. #if !defined(THINK_C)
  73. #include <sys/time.h>
  74. #include <sys/dir.h>
  75. #include <sys/file.h>
  76. #endif
  77. #endif    /* USG */
  78. #endif notdef
  79.  
  80. #if defined(USG) || defined(THINK_C)
  81. /* Define needed BSD functions in terms of sysV library.  */
  82.  
  83. #define bcopy(s,d,n)    memcpy((d),(s),(n))
  84. #define bcmp(s1,s2,n)    memcmp((s1),(s2),(n))
  85. #define bzero(s,n)    memset((s),0,(n))
  86.  
  87. #ifndef XENIX
  88. #define dup2(f,t)    (close(t),fcntl((f),F_DUPFD,(t)))
  89. #endif
  90.  
  91. #define vfork    fork
  92. #define index    strchr
  93. #define rindex    strrchr
  94. #endif
  95.  
  96. #ifdef sparc
  97. /* vfork clobbers registers on the Sparc, so don't use it.  */
  98. #define vfork fork
  99. #endif
  100.  
  101. #include <errno.h>
  102. extern int      errno;
  103. extern int      sys_nerr;
  104. extern char    *sys_errlist[];
  105.  
  106. #define    EOS        (0)
  107. #ifndef    FALSE
  108. #define    FALSE        (0)
  109. #endif
  110. #ifndef    TRUE
  111. #define TRUE        1
  112. #endif
  113.  
  114. #define min(a,b) ((a) <= (b) ? (a) : (b))
  115. #define max(a,b) ((a) >= (b) ? (a) : (b))
  116.  
  117. #ifndef PR_FILE_NAME
  118. #define PR_FILE_NAME "/bin/pr"
  119. #endif
  120.  
  121. #ifdef    THINK_C
  122. #include    <limits.h>
  123. #else    THINK_C
  124.  
  125. /* Support old-fashioned C compilers.  */
  126. #if defined (__STDC__) || defined (__GNUC__)
  127. #include "limits.h"
  128. #else
  129. #define INT_MAX 2147483647
  130. #define CHAR_BIT 8
  131. #endif
  132.  
  133. #endif    THINK_C
  134.  
  135. /* Support old-fashioned C compilers.  */
  136. #if !defined (__STDC__) && !defined (__GNUC__)
  137. #define const
  138. #endif
  139.  
  140. #ifndef O_RDONLY
  141. #define O_RDONLY 0
  142. #endif
  143.  
  144. /* Variables for command line options */
  145.  
  146. #ifndef GDIFF_MAIN
  147. #define EXTERN extern
  148. #else
  149. #define EXTERN
  150. #endif
  151.  
  152. enum output_style {
  153.   /* Default output style.  */
  154.   OUTPUT_NORMAL,
  155.   /* Output the differences with lines of context before and after (-c).  */
  156.   OUTPUT_CONTEXT,
  157.   /* Output the differences in a unified context diff format (-u). */
  158.   OUTPUT_UNIFIED,
  159.   /* Output the differences as commands suitable for `ed' (-e).  */
  160.   OUTPUT_ED,
  161.   /* Output the diff as a forward ed script (-f).  */
  162.   OUTPUT_FORWARD_ED,
  163.   /* Like -f, but output a count of changed lines in each "command" (-n). */
  164.   OUTPUT_RCS,
  165.   /* Output merged #ifdef'd file (-D).  */
  166.   OUTPUT_IFDEF };
  167.  
  168. /* True for output styles that are robust,
  169.    i.e. can handle a file that ends in a non-newline.  */
  170. #define ROBUST_OUTPUT_STYLE(S) \
  171.  ((S) == OUTPUT_CONTEXT || (S) == OUTPUT_UNIFIED || (S) == OUTPUT_RCS \
  172.   || (S) == OUTPUT_NORMAL)
  173.  
  174. EXTERN enum output_style output_style;
  175.  
  176. /* Number of lines of context to show in each set of diffs.
  177.    This is zero when context is not to be shown.  */
  178. EXTERN LONG      context;
  179.  
  180. /* Consider all files as text files (-a).
  181.    Don't interpret codes over 0177 as implying a "binary file".  */
  182. EXTERN int    always_text_flag;
  183.  
  184. /* Ignore changes in horizontal whitespace (-b).  */
  185. EXTERN int      ignore_space_change_flag;
  186.  
  187. /* Ignore all horizontal whitespace (-w).  */
  188. EXTERN int      ignore_all_space_flag;
  189.  
  190. /* Ignore changes that affect only blank lines (-B).  */
  191. EXTERN int      ignore_blank_lines_flag;
  192.  
  193. /* Ignore changes that affect only lines matching this regexp (-I).  */
  194. EXTERN char    *ignore_regexp;
  195.  
  196. /* Result of regex-compilation of `ignore_regexp'.  */
  197. EXTERN struct re_pattern_buffer ignore_regexp_compiled;
  198.  
  199. /* 1 if lines may match even if their lengths are different.
  200.    This depends on various options.  */
  201. EXTERN int      length_varies;
  202.  
  203. /* Ignore differences in case of letters (-i).  */
  204. EXTERN int      ignore_case_flag;
  205.  
  206. /* File labels for `-c' output headers (-L).  */
  207. EXTERN char *file_label[2];
  208.  
  209. /* Regexp to identify function-header lines (-F).  */
  210. EXTERN char    *function_regexp;
  211.  
  212. /* Result of regex-compilation of `function_regexp'.  */
  213. EXTERN struct re_pattern_buffer function_regexp_compiled;
  214.  
  215. /* Say only whether files differ, not how (-q).  */
  216. EXTERN int     no_details_flag;
  217.  
  218. /* Report files compared that match (-s).
  219.    Normally nothing is output when that happens.  */
  220. EXTERN int      print_file_same_flag;
  221.  
  222. /* character that ends a line.  Currently this is always `\n'.  */
  223. EXTERN char     line_end_char;
  224.  
  225. /* Output the differences with exactly 8 columns added to each line
  226.    so that any tabs in the text line up properly (-T).  */
  227. EXTERN int    tab_align_flag;
  228.  
  229. /* Expand tabs in the output so the text lines up properly
  230.    despite the characters added to the front of each line (-t).  */
  231. EXTERN int    tab_expand_flag;
  232.  
  233. /* In directory comparison, specify file to start with (-S).
  234.    All file names less than this name are ignored.  */
  235. EXTERN char    *dir_start_file;
  236.  
  237. /* If a file is new (appears in only one dir)
  238.    include its entire contents (-N).
  239.    Then `patch' would create the file with appropriate contents.  */
  240. EXTERN int    entire_new_file_flag;
  241.  
  242. /* Pipe each file's output through pr (-l).  */
  243. EXTERN int    paginate_flag;
  244.  
  245. /* String to use for #ifdef (-D).  */
  246. EXTERN char *    ifdef_string;
  247.  
  248. /* String containing all the command options diff received,
  249.    with spaces between and at the beginning but none at the end.
  250.    If there were no options given, this string is empty.  */
  251. EXTERN char *    switch_string;
  252.  
  253. /* Nonzero means use heuristics for better speed.  */
  254. EXTERN int    heuristic;
  255.  
  256. /* Name of program the user invoked (for error messages).  */
  257. EXTERN char *    program;
  258.  
  259. /* The result of comparison is an "edit script": a chain of `struct change'.
  260.    Each `struct change' represents one place where some lines are deleted
  261.    and some are inserted.
  262.    
  263.    LINE0 and LINE1 are the first affected lines in the two files (origin 0).
  264.    DELETED is the number of lines deleted here from file 0.
  265.    INSERTED is the number of lines inserted here in file 1.
  266.  
  267.    If DELETED is 0 then LINE0 is the number of the line before
  268.    which the insertion was done; vice versa for INSERTED and LINE1.  */
  269.  
  270. /* The ranges are indexed by */
  271. #define    START    0
  272. #define    END    1
  273.  
  274. /*
  275.  * Note that if this is included in the "diff3" project, it is interchangeable
  276.  * with the (struct diff_block) and it can be typecasted to (struct diff3_block *)!
  277.  * If you change this structure, also make sure you change "struct diff3_block".
  278.  */
  279.  
  280. struct change
  281. {
  282.   struct change *link;        /* Previous or next edit command  */
  283.   char ignore;            /* Flag used in context.c */
  284.     LONG ranges[2][2];            /* Ranges are inclusive */
  285. #define    line0    ranges[0][START]                /* Line number of 1st deleted line.  */
  286. #define    line1    ranges[1][START]                /* Line number of 1st inserted line.  */
  287. #define    line0end    ranges[0][END]            /* # lines of file 0 changed here ("deleted").  */
  288. #define    line1end    ranges[1][END]            /* # lines of file 1 changed here ("inserted").  */
  289. };
  290.  
  291. /* Structures that describe the input files.  */
  292.  
  293. /* Data on one line of text.  */
  294.  
  295. struct line_def {
  296.     char        *text;
  297.     LONG    length;
  298.     unsigned    LONG    hash;
  299.     LONG    filler;    /* This is just to make the size be a power of two */
  300. };
  301.  
  302. /* Data on one input file being compared.  */
  303.  
  304. struct file_data {
  305.     int             desc;    /* File descriptor  */
  306.     char           *name;    /* File name  */
  307.     struct stat     stat;    /* File status from fstat()  */
  308.     int             dir_p;    /* 1 if file is a directory  */
  309.  
  310.     /* Buffer in which text of file is read.  */
  311.     char *        buffer;
  312.     /* Allocated size of buffer.  */
  313.     LONG            bufsize;
  314.     /* Number of valid characters now in the buffer. */
  315.     LONG            buffered_chars;
  316.  
  317.     /* Array of data on analyzed lines of this chunk of this file.  */
  318.     struct line_def *linbuf;
  319.  
  320.     /* Allocated size of linbuf array (# of elements).  */
  321.     LONG            linbufsize;
  322.  
  323.     /* Number of elements of linbuf containing valid data. */
  324.     LONG            buffered_lines;
  325.  
  326.     /* Pointer to end of prefix of this file to ignore when hashing. */
  327.     LONG prefix_end;
  328.  
  329.     /* Count of lines in the prefix. */
  330.     LONG prefix_lines;
  331.  
  332.     /* Pointer to start of suffix of this file to ignore when hashing. */
  333.     LONG    suffix_begin;
  334.  
  335.     /* Count of lines in the suffix. */
  336.     LONG suffix_lines;
  337.  
  338.     /* Vector, indexed by line number, containing an equivalence code for
  339.        each line.  It is this vector that is actually compared with that
  340.        of another file to generate differences. */
  341.     LONG           *equivs;
  342.  
  343.     /* Vector, like the previous one except that
  344.        the elements for discarded lines have been squeezed out.  */
  345.     LONG           *undiscarded;
  346.  
  347.     /* Vector mapping virtual line numbers (not counting discarded lines)
  348.        to real ones (counting those lines).  Both are origin-0.  */
  349.     LONG           *realindexes;
  350.  
  351.     /* Total number of nondiscarded lines. */
  352.     LONG            nondiscarded_lines;
  353.  
  354.     /* Vector, indexed by real origin-0 line number,
  355.        containing 1 for a line that is an insertion or a deletion.
  356.        The results of comparison are stored here.  */
  357.     char       *changed_flag;
  358.  
  359.     /* 1 if file ends in a line with no final newline. */
  360.     int            missing_newline;
  361.  
  362.     /* 1 more than the maximum equivalence value used for this or its
  363.        sibling file. */
  364.     LONG equiv_max;
  365.  
  366.     /* Table translating diff's internal line numbers 
  367.        to actual line numbers in the file.
  368.        This is needed only when some lines have been discarded.
  369.        The allocated size is always linbufsize
  370.        and the number of valid elements is buffered_lines.  */
  371.     LONG           *ltran;
  372.  
  373.      /* This flag is TRUE if the file has already been read in ("slurp" has been called). */
  374.          int    already_read_in;
  375. };
  376.  
  377. /* Describe the two files currently being compared.  */
  378.  
  379. EXTERN    struct file_data files[2];
  380.  
  381. /* Queue up one-line messages to be printed at the end,
  382.    when -l is specified.  Each message is recorded with a `struct msg'.  */
  383.  
  384. struct msg
  385. {
  386.   struct msg *next;
  387.   char *format;
  388.   char *arg1;
  389.   char *arg2;
  390. };
  391.  
  392. /* Head of the chain of queues messages.  */
  393.  
  394. EXTERN struct msg *msg_chain;
  395.  
  396. /* Tail of the chain of queues messages.  */
  397.  
  398. EXTERN struct msg *msg_chain_end;
  399.  
  400. /* Stdio stream to output diffs to.  */
  401.  
  402. EXTERN    FILE *outfile;
  403.  
  404. /* Declare various functions.  */
  405.  
  406. #ifdef __STDC__
  407. #define VOID void
  408. #else
  409. #define VOID char
  410. #endif
  411.  
  412. #ifdef    __STDC__
  413. #include "proto.h"
  414.  
  415. #endif    __STDC__
  416. VOID *xmalloc ();
  417. VOID *xrealloc ();
  418. char *concat ();
  419. void free ();
  420. char *rindex ();
  421. char *index ();
  422.  
  423. void analyze_hunk ();
  424. void error ();
  425. void fatal ();
  426. void message ();
  427. void perror_with_name ();
  428. void pfatal_with_name ();
  429. void print_1_line ();
  430. void print_message_queue ();
  431. void print_number_range ();
  432. void print_script ();
  433. void translate_range ();
  434.  
  435. #ifdef    THINK_C
  436. VOID *alloca (unsigned int size);
  437. #endif
  438.  
  439. int pout(const char *,...);
  440. int fpout(FILE *, const char *, ...);
  441. int petc(int);
  442. int petec(int, FILE *);
  443. size_t pwrite(const void *, size_t, size_t, FILE *);