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