home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / rcs567s.zip / diff16 / diff.h < prev    next >
C/C++ Source or Header  |  1994-06-25  |  13KB  |  477 lines

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