home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / diff / dist / io.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-24  |  16.5 KB  |  588 lines

  1. /* File I/O 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. #include "diff.h"
  21.  
  22. /* Rotate a value n bits to the left. */
  23. #define UINT_BIT (sizeof (unsigned) * CHAR_BIT)
  24. #define ROL(v, n) ((v) << (n) | (v) >> UINT_BIT - (n))
  25.  
  26. /* Given a hash value and a new character, return a new hash value. */
  27. #define HASH(h, c) ((c) + ROL (h, 7))
  28.  
  29. /* Current file under consideration. */
  30. struct file_data *current;
  31.  
  32. /* Check for binary files and compare them for exact identity.  */
  33.  
  34. /* Return 1 if BUF contains a 0 or a character with the 0200 bit set.
  35.    SIZE is the number of characters in BUF.  */
  36.  
  37. static int
  38. binary_file_p (buf, size)
  39.      char *buf;
  40.      int size;
  41. {
  42.   while (--size >= 0)
  43.     if (*buf == 0 || (*buf++ & 0200) != 0)
  44.       return 1;
  45.   return 0;
  46. }
  47.  
  48. int binary_file_threshold = 512;
  49.  
  50. /* Slurp the current file completely into core.
  51.    Return nonzero if it appears to be a binary file.  */
  52.  
  53. static int
  54. slurp ()
  55. {
  56.   /* If we have a nonexistent file at this stage, treat it as empty.  */
  57.   if (current->desc < 0)
  58.     {
  59.       current->bufsize = 0;
  60.       current->buffered_chars = 0;
  61.       current->buffer = 0;
  62.     }
  63.   /* If it's a regular file, we can just get the size out of the stat
  64.      block and slurp it in all at once. */
  65.   /* In all cases, we leave room in the buffer for 2 extra chars
  66.      beyond those that current->bufsize describes:
  67.      one for a newline (in case the text does not end with one)
  68.      and one for a sentinel in find_identical_ends.  */
  69.   else if ((current->stat.st_mode & S_IFMT) == S_IFREG)
  70.     {
  71.       current->bufsize = current->stat.st_size;
  72.       current->buffer = (char *) xmalloc (current->bufsize + 2);
  73.       current->buffered_chars
  74.     = read (current->desc, current->buffer, current->bufsize);
  75.       if (current->buffered_chars < 0)
  76.     pfatal_with_name (current->name);
  77.     }
  78.   else
  79.     {
  80.       int cc;
  81.  
  82.       current->bufsize = 4096;
  83.       current->buffer = (char *) xmalloc (current->bufsize + 2);
  84.       current->buffered_chars = 0;
  85.       
  86.       /* Not a regular file; read it in a little at a time, growing the
  87.      buffer as necessary. */
  88.       while ((cc = read (current->desc,
  89.              current->buffer + current->buffered_chars,
  90.              current->bufsize - current->buffered_chars))
  91.          > 0)
  92.     {
  93.       current->buffered_chars += cc;
  94.       if (current->buffered_chars == current->bufsize)
  95.         {
  96.           current->bufsize = current->bufsize * 2;
  97.           current->buffer = (char *) xrealloc (current->buffer,
  98.                            current->bufsize + 2);
  99.         }
  100.     }
  101.       if (cc < 0)
  102.     pfatal_with_name (current->name);
  103.     }
  104.   
  105.   /* Check first part of file to see if it's a binary file.  */
  106.   if (! always_text_flag
  107.       && binary_file_p (current->buffer,
  108.             min (current->buffered_chars, binary_file_threshold)))
  109.     return 1;
  110.  
  111.   /* If not binary, make sure text ends in a newline,
  112.      but remember that we had to add one.  */
  113.   if (current->buffered_chars > 0
  114.       && current->buffer[current->buffered_chars - 1] != '\n')
  115.     {
  116.       current->missing_newline = 1;
  117.       current->buffer[current->buffered_chars++] = '\n';
  118.     }
  119.   else
  120.     current->missing_newline = 0;
  121.  
  122.   /* Don't use uninitialized storage. */
  123.   if (current->buffer != 0)
  124.     current->buffer[current->buffered_chars] = '\0';
  125.  
  126.   return 0;
  127. }
  128.  
  129. /* Split the file into lines, simultaneously computing the hash codes for
  130.    each line. */
  131.  
  132. void
  133. find_and_hash_each_line ()
  134. {
  135.   unsigned h;
  136.   int i;
  137.   unsigned char *p = (unsigned char *) current->prefix_end, *ip, c;
  138.  
  139.   /* Attempt to get a good initial guess as to the number of lines. */
  140.   current->linbufsize = current->buffered_chars / 50 + 5;
  141.   current->linbuf
  142.     = (struct line_def *) xmalloc (current->linbufsize * sizeof (struct line_def));
  143.  
  144.   if (function_regexp || output_style == OUTPUT_IFDEF)
  145.     {
  146.       /* If the -C, -D or -F option is used, we need to find the lines
  147.      of the matching prefix.  At least we will need to find the last few,
  148.      but since we don't know how many, it's easiest to find them all.
  149.      If -D is specified, we need all the lines of the first file.  */
  150.       current->buffered_lines = 0;
  151.       p = (unsigned char *) current->buffer;
  152.     }
  153.   else
  154.     {
  155.       /* Skip the identical prefixes, except be prepared to handle context.
  156.      In fact, handle 1 more preceding line than the context says,
  157.      in case shift_boundaries moves things backwards in this file.  */
  158.       current->buffered_lines = current->prefix_lines - context - 1;
  159.       if (current->buffered_lines < 0)
  160.     current->buffered_lines = 0;
  161.       for (i = 0; i < context + 1; ++i)
  162.     /* Unless we are at the beginning, */
  163.     if ((char *) p != current->buffer)
  164.       /* Back up at least 1 char until at the start of a line.  */
  165.       while ((char *) --p != current->buffer && p[-1] != '\n')
  166.         ;
  167.     }
  168.  
  169.   while ((char *) p < current->suffix_begin)
  170.     {
  171.       h = 0;
  172.       ip = p;
  173.  
  174.       if (current->prefix_end <= (char *) p)
  175.     {
  176.       /* Hash this line until we find a newline. */
  177.       if (ignore_case_flag)
  178.         {
  179.           if (ignore_all_space_flag)
  180.         while ((c = *p) != '\n')
  181.           {
  182.             if (! isspace (c))
  183.               if (isupper (c))
  184.             h = HASH (h, tolower (c));
  185.               else
  186.             h = HASH (h, c);
  187.             ++p;
  188.           }
  189.           else if (ignore_space_change_flag)
  190.  
  191.         while ((c = *p) != '\n')
  192.           {
  193.             if (c == ' ' || c == '\t')
  194.               {
  195.             while ((c = *p) == ' ' || c == '\t')
  196.               ++p;
  197.             if (c == '\n')
  198.               break;
  199.             h = HASH (h, ' ');
  200.               }
  201.             /* C is now the first non-space.  */
  202.             if (isupper (c))
  203.               h = HASH (h, tolower (c));
  204.             else
  205.               h = HASH (h, c);
  206.             ++p;
  207.           }
  208.           else
  209.         while ((c = *p) != '\n')
  210.           {
  211.             if (isupper (c))
  212.               h = HASH (h, tolower (c));
  213.             else
  214.               h = HASH (h, c);
  215.             ++p;
  216.           }
  217.         }
  218.       else
  219.         {
  220.           if (ignore_all_space_flag)
  221.         while ((c = *p) != '\n')
  222.           {
  223.             if (! isspace (c))
  224.               h = HASH (h, c);
  225.             ++p;
  226.           }
  227.           else if (ignore_space_change_flag)
  228.         while ((c = *p) != '\n')
  229.           {
  230.             if (c == ' ' || c == '\t')
  231.               {
  232.             while ((c = *p) == ' ' || c == '\t')
  233.               ++p;
  234.             if (c == '\n')
  235.               break;
  236.             h = HASH (h, ' ');
  237.               }
  238.             /* C is not the first non-space.  */
  239.             h = HASH (h, c);
  240.             ++p;
  241.           }
  242.           else
  243.         while ((c = *p) != '\n')
  244.           {
  245.             h = HASH (h, c);
  246.             ++p;
  247.           }
  248.         }
  249.     }
  250.       else
  251.     /* This line is part of the matching prefix,
  252.        so we don't need to hash it.  */
  253.     while (*p != '\n')
  254.       ++p;
  255.       
  256.       /* Maybe increase the size of the line table. */
  257.       if (current->buffered_lines >= current->linbufsize)
  258.     {
  259.       while (current->buffered_lines >= current->linbufsize)
  260.         current->linbufsize *= 2;
  261.       current->linbuf
  262.         = (struct line_def *) xrealloc (current->linbuf,
  263.                         current->linbufsize
  264.                         * sizeof (struct line_def));
  265.     }
  266.       current->linbuf[current->buffered_lines].text = (char *) ip;
  267.       current->linbuf[current->buffered_lines].length = p - ip + 1;
  268.       current->linbuf[current->buffered_lines].hash = h;
  269.       ++current->buffered_lines;
  270.       ++p;
  271.     }
  272.  
  273.   if (output_style == OUTPUT_RCS && current->missing_newline)
  274.     --current->linbuf[current->buffered_lines - 1].length;
  275.  
  276.   i = 0;
  277.   while ((i < context || output_style == OUTPUT_IFDEF)
  278.      && (char *) p < current->buffer + current->buffered_chars)
  279.     {
  280.       ip = p;
  281.       while (*p++ != '\n')
  282.     ;
  283.       /* Maybe increase the size of the line table. */
  284.       if (current->buffered_lines >= current->linbufsize)
  285.     {
  286.       while (current->buffered_lines >= current->linbufsize)
  287.         current->linbufsize *= 2;
  288.       current->linbuf
  289.         = (struct line_def *) xrealloc (current->linbuf,
  290.                         current->linbufsize
  291.                         * sizeof (struct line_def));
  292.     }
  293.       current->linbuf[current->buffered_lines].text = (char *) ip;
  294.       current->linbuf[current->buffered_lines].length = p - ip;
  295.       current->linbuf[current->buffered_lines].hash = 0;
  296.       ++current->buffered_lines;
  297.       ++i;
  298.     }
  299. }
  300.  
  301. /* Given a vector of two file_data objects, find the identical
  302.    prefixes and suffixes of each object. */
  303.  
  304. static void
  305. find_identical_ends (filevec)
  306.      struct file_data filevec[];
  307. {
  308.   char *p0, *p1, *end0, *beg0;
  309.   int lines;
  310.  
  311.   if (filevec[0].buffered_chars == 0 || filevec[1].buffered_chars == 0)
  312.     {
  313.       filevec[0].prefix_end = filevec[0].buffer;
  314.       filevec[1].prefix_end = filevec[1].buffer;
  315.       filevec[0].prefix_lines = filevec[1].prefix_lines = 0;
  316.       filevec[0].suffix_begin = filevec[0].buffer + filevec[0].buffered_chars;
  317.       filevec[1].suffix_begin = filevec[1].buffer + filevec[1].buffered_chars;
  318.       filevec[0].suffix_lines = filevec[1].suffix_lines = 0;
  319.       return;
  320.     }
  321.  
  322.   /* Find identical prefix.  */
  323.  
  324.   p0 = filevec[0].buffer;
  325.   p1 = filevec[1].buffer;
  326.   lines = 0;
  327.  
  328.   /* Insert end "sentinels", in this case characters that are guarranteed
  329.      to make the equality test false, and thus terminate the loop.  */
  330.  
  331.   if (filevec[0].buffered_chars < filevec[1].buffered_chars)
  332.     p0[filevec[0].buffered_chars] = ~p1[filevec[0].buffered_chars];
  333.   else
  334.     p1[filevec[1].buffered_chars] = ~p0[filevec[1].buffered_chars];
  335.  
  336.   /* Loop until first mismatch, or to the sentinel characters.  */
  337.   while (1)
  338.     {
  339.       char c = *p0++;
  340.       if (c != *p1++)
  341.     break;
  342.       if (c == '\n')
  343.     ++lines;
  344.     }
  345.  
  346.   /* Don't count missing newline as part of prefix in RCS mode. */
  347.   if (output_style == OUTPUT_RCS &&
  348.       ((filevec[0].missing_newline
  349.     && p0 - filevec[0].buffer > filevec[0].buffered_chars) ||
  350.        (filevec[1].missing_newline
  351.     && p1 - filevec[1].buffer > filevec[1].buffered_chars)))
  352.     --p0, --p1, --lines;
  353.  
  354.   /* If the sentinel was passed, and lengths are equal, the
  355.      files are identical.  */
  356.   if (p0 - filevec[0].buffer > filevec[0].buffered_chars
  357.       && filevec[0].buffered_chars == filevec[1].buffered_chars)
  358.     {
  359.       filevec[0].prefix_end = p0 - 1;
  360.       filevec[1].prefix_end = p1 - 1;
  361.       filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
  362.       filevec[0].suffix_begin = filevec[0].buffer;
  363.       filevec[1].suffix_begin = filevec[1].buffer;
  364.       filevec[0].suffix_lines = filevec[1].suffix_lines = lines;
  365.       return;
  366.     }
  367.  
  368.   /* Point at first nonmatching characters.  */
  369.   --p0, --p1;
  370.  
  371.   /* Skip back to last line-beginning in the prefix.  */
  372.   while (p0 != filevec[0].buffer && p0[-1] != '\n')
  373.     --p0, --p1;
  374.  
  375.   /* Record the prefix.  */
  376.   filevec[0].prefix_end = p0;
  377.   filevec[1].prefix_end = p1;
  378.   filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
  379.   
  380.   /* Find identical suffix.  */
  381.  
  382.   /* P0 and P1 point beyond the last chars not yet compared.  */
  383.   p0 = filevec[0].buffer + filevec[0].buffered_chars;
  384.   p1 = filevec[1].buffer + filevec[1].buffered_chars;
  385.   lines = 0;
  386.  
  387.   if (output_style != OUTPUT_RCS || filevec[0].missing_newline == filevec[1].missing_newline)
  388.     {
  389.       end0 = p0;        /* Addr of last char in file 0.  */
  390.  
  391.       /* Get value of P0 at which we should stop scanning backward:
  392.      this is when either P0 or P1 points at the last char
  393.      of the identical prefix.  */
  394.       if (filevec[0].buffered_chars < filevec[1].buffered_chars)
  395.     beg0 = filevec[0].prefix_end - 1;
  396.       else
  397.     /* Figure out where P0 will be when P1 is at the end of the prefix.
  398.        Thus we only need to test P0.  */
  399.     beg0 = (filevec[0].prefix_end - 1
  400.         + filevec[0].buffered_chars - filevec[1].buffered_chars);
  401.  
  402.       /* Scan back until chars don't match or we reach that point.  */
  403.       while (p0 != beg0)
  404.     {
  405.       char c = *--p0;
  406.       if (c != *--p1)
  407.         break;
  408.       if (c == '\n')
  409.         ++lines;
  410.     }
  411.  
  412.       /* Make P0 and P1 point at the first char of the matching suffix.  */
  413.       ++p0, ++p1;
  414.  
  415.       /* Advance to next place that is a line-beginning in both files.  */
  416.       while (p0 != end0
  417.          && !((p0 == filevec[0].buffer || p0[-1] == '\n')
  418.           &&
  419.           (p1 == filevec[1].buffer || p1[-1] == '\n')))
  420.     ++p0, ++p1;
  421.  
  422.       /* Subtract one, since the last newline isn't followed by a line.  */
  423.       --lines;
  424.     }
  425.  
  426.   /* Record the suffix.  */
  427.   filevec[0].suffix_begin = p0;
  428.   filevec[1].suffix_begin = p1;
  429.   filevec[0].suffix_lines = filevec[1].suffix_lines = lines;
  430. }
  431.  
  432. /* Lines are put into equivalence classes (of lines that match in line_cmp).
  433.    Each equivalence class is represented by one of these structures,
  434.    but only while the classes are being computed.
  435.    Afterward, each class is represented by a number.  */
  436. struct equivclass
  437. {
  438.   struct equivclass *next;    /* Next item in this bucket. */
  439.   struct line_def line;    /* A line that fits this class. */
  440. };
  441.  
  442. /* Hash-table: array of buckets, each being a chain of equivalence classes.  */
  443. static struct equivclass **buckets;
  444.   
  445. /* Size of the bucket array. */
  446. static int nbuckets;
  447.  
  448. /* Array in which the equivalence classes are allocated.
  449.    The bucket-chains go through the elements in this array.
  450.    The number of an equivalence class is its index in this array.  */
  451. static struct equivclass *equivs;
  452.  
  453. /* Index of first free element in the array `equivs'.  */
  454. static int equivs_index;
  455.  
  456. /* Size allocated to the array `equivs'.  */
  457. static int equivs_alloc;
  458.  
  459. /* Largest primes less than some power of two, for nbuckets.  Values range
  460.    from useful to preposterous.  If one of these numbers isn't prime
  461.    after all, don't blame it on me, blame it on primes (6) . . . */
  462. static int primes[] =
  463. {
  464.   509,
  465.   1021,
  466.   2039,
  467.   4093,
  468.   8191,
  469.   16381,
  470.   32749,
  471.   65521,
  472.   131071,
  473.   262139,
  474.   524287,
  475.   1048573,
  476.   2097143,
  477.   4194301,
  478.   8388593,
  479.   16777213,
  480.   33554393,
  481.   67108859,            /* Preposterously large . . . */
  482.   -1
  483. };
  484.  
  485. /* Index of current nbuckets in primes. */
  486. static int primes_index;
  487.  
  488. /* Find the equiv class associated with line N of the current file.  */
  489.  
  490. static int
  491. find_equiv_class (n)
  492.      int n;
  493. {
  494.   int bucket;
  495.   struct equivclass *b, *p = NULL;
  496.  
  497.   /* Equivalence class 0 is permanently allocated to lines that were
  498.      not hashed because they were parts of identical prefixes or
  499.      suffixes. */
  500.   if (n < current->prefix_lines
  501.       || current->linbuf[n].text >= current->suffix_begin)
  502.     return 0;
  503.  
  504.   /* Check through the appropriate bucket to see if there isn't already
  505.      an equivalence class for this line. */
  506.   bucket = current->linbuf[n].hash % nbuckets;
  507.   b = buckets[bucket];
  508.   while (b)
  509.     {
  510.       if (b->line.hash == current->linbuf[n].hash
  511.       && (b->line.length == current->linbuf[n].length
  512.           /* Lines of different lengths can match with certain options.  */
  513.           || length_varies)
  514.       && !line_cmp (&b->line, ¤t->linbuf[n]))
  515.     return b - equivs;
  516.       p = b, b = b->next;
  517.     }
  518.  
  519.   /* Create a new equivalence class in this bucket. */
  520.  
  521.   p = &equivs[equivs_index++];
  522.   p->next = buckets[bucket];
  523.   buckets[bucket] = p;
  524.   p->line = current->linbuf[n];
  525.  
  526.   return equivs_index - 1;
  527. }
  528.  
  529. /* Given a vector of two file_data objects, read the file associated
  530.    with each one, and build the table of equivalence classes.
  531.    Return nonzero if either file appears to be a binary file.  */
  532.  
  533. int
  534. read_files (filevec)
  535.      struct file_data filevec[];
  536. {
  537.   int i, j;
  538.   int binary = 0;
  539.   int this_binary;
  540.  
  541.   current = &filevec[0];
  542.   binary = this_binary = slurp ();
  543.  
  544.   current = &filevec[1];
  545.   this_binary = slurp ();
  546.   if (binary || this_binary)
  547.     return 1;
  548.  
  549.   find_identical_ends (filevec);
  550.  
  551.   for (i = 0; i < 2; ++i)
  552.     {
  553.       current = &filevec[i];
  554.       find_and_hash_each_line ();
  555.     }
  556.  
  557.   /* This is guaranteed to be enough space.  */
  558.   equivs_alloc = filevec[0].buffered_lines + filevec[1].buffered_lines + 1;
  559.   equivs = (struct equivclass *) xmalloc (equivs_alloc * sizeof (struct equivclass));
  560.   /* Equivalence class 0 is permanently safe for lines that were not
  561.      hashed.  Real equivalence classes start at 1. */
  562.   equivs_index = 1;
  563.   
  564.   primes_index = 0;
  565.   while (primes[primes_index] < equivs_alloc / 3)
  566.     primes_index++;
  567.  
  568.   buckets = (struct equivclass **) xmalloc (primes[primes_index] * sizeof (struct equivclass *));
  569.   bzero (buckets, primes[primes_index] * sizeof (struct equivclass *));
  570.   nbuckets = primes[primes_index];
  571.  
  572.   for (i = 0; i < 2; ++i)
  573.     {
  574.       current = &filevec[i];
  575.       current->equivs
  576.     = (int *) xmalloc (current->buffered_lines * sizeof (int));
  577.       for (j = 0; j < current->buffered_lines; ++j)
  578.     current->equivs[j] = find_equiv_class (j);
  579.     }
  580.  
  581.   filevec[0].equiv_max = filevec[1].equiv_max = equivs_index;
  582.  
  583.   free (equivs);
  584.   free (buckets);
  585.  
  586.   return 0;
  587. }
  588.