home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff281.lzh / Diff / util.c < prev   
C/C++ Source or Header  |  1989-11-20  |  15KB  |  638 lines

  1. /* Support routines 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. /* Use when a system call returns non-zero status.
  23.    TEXT should normally be the file name.  */
  24.  
  25. void
  26. perror_with_name (text)
  27.      char *text;
  28. {
  29.   fprintf (stderr, "%s: ", program);
  30.   perror (text);
  31. }
  32.  
  33. /* Use when a system call returns non-zero status and that is fatal.  */
  34.  
  35. void
  36. pfatal_with_name (text)
  37.      char *text;
  38. {
  39.   print_message_queue ();
  40.   fprintf (stderr, "%s: ", program);
  41.   perror (text);
  42.   exit (2);
  43. }
  44.  
  45. /* Print an error message from the format-string FORMAT
  46.    with args ARG1 and ARG2.  */
  47.  
  48. void
  49. error (format, arg, arg1)
  50.      char *format;
  51.      char *arg;
  52.      char *arg1;
  53. {
  54.   fprintf (stderr, "%s: ", program);
  55.   fprintf (stderr, format, arg, arg1);
  56.   fprintf (stderr, "\n");
  57. }
  58.  
  59. /* Print an error message containing the string TEXT, then exit.  */
  60.  
  61. void
  62. fatal (message)
  63.      char *message;
  64. {
  65.   print_message_queue ();
  66.   error (message, "");
  67.   exit (2);
  68. }
  69.  
  70. /* Like printf, except if -l in effect then save the message and print later.
  71.    This is used for things like "binary files differ" and "Only in ...".  */
  72.  
  73. void
  74. message (format, arg1, arg2)
  75.      char *format, *arg1, *arg2;
  76. {
  77.   if (paginate_flag)
  78.     {
  79.       struct msg *new = (struct msg *) xmalloc (sizeof (struct msg));
  80.       if (msg_chain_end == 0)
  81.     msg_chain = msg_chain_end = new;
  82.       else
  83.     {
  84.       msg_chain_end->next = new;
  85.       msg_chain_end = new;
  86.     }
  87.       new->format = format;
  88.       new->arg1 = concat (arg1, "", "");
  89.       new->arg2 = concat (arg2, "", "");
  90.       new->next = 0;
  91.     }
  92.   else
  93.     printf (format, arg1, arg2);
  94. }
  95.  
  96. /* Output all the messages that were saved up by calls to `message'.  */
  97.  
  98. void
  99. print_message_queue ()
  100. {
  101.   struct msg *m;
  102.  
  103.   for (m = msg_chain; m; m = m->next)
  104.     printf (m->format, m->arg1, m->arg2);
  105. }
  106.  
  107. /* Call before outputting the results of comparing files NAME0 and NAME1
  108.    to set up OUTFILE, the stdio stream for the output to go to.
  109.  
  110.    Usually, OUTFILE is just stdout.  But when -l was specified
  111.    we fork off a `pr' and make OUTFILE a pipe to it.
  112.    `pr' then outputs to our stdout.  */
  113.  
  114. void
  115. setup_output (name0, name1, depth)
  116.      char *name0, *name1;
  117.      int depth;
  118. {
  119.   char *name;
  120.  
  121.   /* Construct the header of this piece of diff.  */
  122.   name = (char *) xmalloc (strlen (name0) + strlen (name1)
  123.                + strlen (switch_string) + 15);
  124.  
  125.   strcpy (name, "diff");
  126.   strcat (name, switch_string);
  127.   strcat (name, " ");
  128.   strcat (name, name0);
  129.   strcat (name, " ");
  130.   strcat (name, name1);
  131.  
  132.   if (paginate_flag)
  133.     {
  134. #ifndef AMIGA
  135.       int pipes[2];
  136.       int desc;
  137.  
  138.       /* For a `pr' and make OUTFILE a pipe to it.  */
  139.       pipe (pipes);
  140.  
  141.       fflush (stdout);
  142.  
  143.       desc = vfork ();
  144.       if (desc < 0)
  145.     pfatal_with_name ("vfork");
  146.  
  147.       if (desc == 0)
  148.     {
  149.       close (pipes[1]);
  150.       close (fileno (stdin));
  151.       if (dup2 (pipes[0], fileno (stdin)) < 0)
  152.         pfatal_with_name ("dup2");
  153.       close (pipes[0]);
  154.  
  155.       if (execl (PR_FILE_NAME, PR_FILE_NAME, "-f", "-h", name, 0) < 0)
  156.         pfatal_with_name (PR_FILE_NAME);
  157.     }
  158.       else
  159.     {
  160.       close (pipes[0]);
  161.       outfile = fdopen (pipes[1], "w");
  162.     }
  163. #endif
  164.     }
  165.   else
  166.     {
  167.  
  168.       /* If -l was not specified, output the diff straight to `stdout'.  */
  169.  
  170.       outfile = stdout;
  171.  
  172.       /* If handling multiple files (because scanning a directory),
  173.      print which files the following output is about.  */
  174.       if (depth > 0)
  175.     printf ("%s\n", name);
  176.     }
  177.  
  178.   free (name);
  179. }
  180.  
  181. /* Call after the end of output of diffs for one file.
  182.    Close OUTFILE and get rid of the `pr' subfork.  */
  183.  
  184. void
  185. finish_output ()
  186. {
  187.   if (outfile != stdout)
  188.     {
  189.       fclose (outfile);
  190. #ifndef AMIGA
  191.       wait (0);
  192. #endif
  193.     }
  194. }
  195.  
  196. /* Compare two lines (typically one from each input file)
  197.    according to the command line options.
  198.    Each line is described by a `struct line_def'.
  199.    Return 1 if the lines differ, like `bcmp'.  */
  200.  
  201. int
  202. line_cmp (s1, s2)
  203.      struct line_def *s1, *s2;
  204. {
  205.   register char *t1, *t2;
  206.   register char end_char = line_end_char;
  207.   int savechar;
  208.  
  209.   /* Check first for exact identity.
  210.      If that is true, return 0 immediately.
  211.      This detects the common case of exact identity
  212.      faster than complete comparison would.  */
  213.  
  214.   t1 = s1->text;
  215.   t2 = s2->text;
  216.  
  217.   /* Alter the character following line 1 so it doesn't
  218.      match that following line 2.  */
  219.   savechar = s1->text[s1->length];
  220.   s1->text[s1->length] = s2->text[s2->length] + 1;
  221.  
  222.   /* Now find the first mismatch; this won't go past the
  223.      character we just changed.  */
  224.   while (*t1++ == *t2++);
  225.  
  226.   /* Undo the alteration.  */
  227.   s1->text[s1->length] = savechar;
  228.  
  229.   /* If the comparison stopped at the alteration,
  230.      the two lines are identical.  */
  231.   if (t1 == s1->text + s1->length + 1)
  232.     return 0;
  233.  
  234.   /* Not exactly identical, but perhaps they match anyway
  235.      when case or whitespace is ignored.  */
  236.  
  237.   if (ignore_case_flag || ignore_space_change_flag || ignore_all_space_flag)
  238.     {
  239.       t1 = s1->text;
  240.       t2 = s2->text;
  241.  
  242.       while (1)
  243.     {
  244.       register char c1 = *t1++;
  245.       register char c2 = *t2++;
  246.  
  247.       /* Ignore horizontal whitespace if -b or -w is specified.  */
  248.  
  249.       if (ignore_all_space_flag)
  250.         {
  251.           /* For -w, just skip past any spaces or tabs.  */
  252.           while (c1 == ' ' || c1 == '\t') c1 = *t1++;
  253.           while (c2 == ' ' || c2 == '\t') c2 = *t2++;
  254.         }
  255.       else if (ignore_space_change_flag)
  256.         {
  257.           /* For -b, advance past any sequence of whitespace in line 1
  258.          and consider it just one Space, or nothing at all
  259.          if it is at the end of the line.  */
  260.           if (c1 == ' ' || c1 == '\t')
  261.         {
  262.           while (1)
  263.             {
  264.               c1 = *t1++;
  265.               if (c1 == end_char)
  266.             break;
  267.               if (c1 != ' ' && c1 != '\t')
  268.             {
  269.               --t1;
  270.               c1 = ' ';
  271.               break;
  272.             }
  273.             }
  274.         }
  275.  
  276.           /* Likewise for line 2.  */
  277.           if (c2 == ' ' || c2 == '\t')
  278.         {
  279.           while (1)
  280.             {
  281.               c2 = *t2++;
  282.               if (c2 == end_char)
  283.             break;
  284.               if (c2 != ' ' && c2 != '\t')
  285.             {
  286.               --t2;
  287.               c2 = ' ';
  288.               break;
  289.             }
  290.             }
  291.         }
  292.         }
  293.  
  294.       /* Upcase all letters if -i is specified.  */
  295.  
  296.       if (ignore_case_flag)
  297.         {
  298.           if (islower (c1))
  299.         c1 = toupper (c1);
  300.           if (islower (c2))
  301.         c2 = toupper (c2);
  302.         }
  303.  
  304.       if (c1 != c2)
  305.         break;
  306.       if (c1 == end_char)
  307.         return 0;
  308.     }
  309.     }
  310.  
  311.   return (1);
  312. }
  313.  
  314. /* Find the consecutive changes at the start of the script START.
  315.    Return the last link before the first gap.  */
  316.  
  317. struct change *
  318. find_change (start)
  319.      struct change *start;
  320. {
  321.   return start;
  322. }
  323.  
  324. struct change *
  325. find_reverse_change (start)
  326.      struct change *start;
  327. {
  328.   return start;
  329. }
  330.  
  331. /* Divide SCRIPT into pieces by calling HUNKFUN and
  332.    print each piece with PRINTFUN.
  333.    Both functions take one arg, an edit script.
  334.  
  335.    HUNKFUN is called with the tail of the script
  336.    and returns the last link that belongs together with the start
  337.    of the tail.
  338.  
  339.    PRINTFUN takes a subscript which belongs together (with a null
  340.    link at the end) and prints it.  */
  341.  
  342. void
  343. print_script (script, hunkfun, printfun)
  344.      struct change *script;
  345.      struct change * (*hunkfun) ();
  346.      void (*printfun) ();
  347. {
  348.   struct change *next = script;
  349.  
  350.   while (next)
  351.     {
  352.       struct change *this, *end;
  353.  
  354.       /* Find a set of changes that belong together.  */
  355.       this = next;
  356.       end = (*hunkfun) (next);
  357.  
  358.       /* Disconnect them from the rest of the changes,
  359.      making them a hunk, and remember the rest for next iteration.  */
  360.       next = end->link;
  361.       end->link = NULL;
  362. #ifdef MYDEBUG
  363.       debug_script (this);
  364. #endif
  365.  
  366.       /* Print this hunk.  */
  367.       (*printfun) (this);
  368.  
  369.       /* Reconnect the script so it will all be freed properly.  */
  370.       end->link = next;
  371.     }
  372. }
  373.  
  374. /* Print the text of a single line LINE,
  375.    flagging it with the characters in LINE_FLAG (which say whether
  376.    the line is inserted, deleted, changed, etc.).  */
  377.  
  378. void
  379. print_1_line (line_flag, line)
  380.      char *line_flag;
  381.      struct line_def *line;
  382. {
  383.   fprintf (outfile, "%s", line_flag);
  384.  
  385.   /* If -T was specified, use a Tab between the line-flag and the text.
  386.      Otherwise use a Space (as Unix diff does).
  387.      Print neither space nor tab if line-flags are empty.  */
  388.  
  389.   if (line_flag[0] != 0)
  390.     {
  391.       if (tab_align_flag)
  392.     fprintf (outfile, "\t");
  393.       else
  394.     fprintf (outfile, " ");
  395.     }
  396.  
  397.   /* Now output the contents of the line.
  398.      If -t was specified, expand tabs to spaces.
  399.      Otherwise output verbatim.  */
  400.  
  401.   if (tab_expand_flag)
  402.     {
  403.       register int column = 0;
  404.       register int i;
  405.       for (i = 0; i <= line->length; i++)
  406.     {
  407.       register char c = line->text[i];
  408.       if (c == '\t')
  409.         {
  410.           putc (' ', outfile);
  411.           column++;
  412.           while (column & 7)
  413.         {
  414.           putc (' ', outfile);
  415.           column++;
  416.         }
  417.         }
  418.       else if (c == '\b')
  419.         {
  420.           column--;
  421.           putc (c, outfile);
  422.         }
  423.       else
  424.         {
  425.           column++;
  426.           putc (c, outfile);
  427.         }
  428.     }
  429.     }
  430.   else
  431.     fwrite (line->text, sizeof (char), line->length + 1, outfile);
  432.  
  433.   if (line_end_char != '\n')
  434.     putc ('\n', outfile);
  435. }
  436.  
  437. change_letter (inserts, deletes)
  438.      int inserts, deletes;
  439. {
  440.   if (!inserts)
  441.     return 'd';
  442.   else if (!deletes)
  443.     return 'a';
  444.   else
  445.     return 'c';
  446. }
  447.  
  448. /* Translate an internal line number (an index into diff's table of lines)
  449.    into an actual line number in the input file.
  450.    The internal line number is LNUM.  FILE points to the data on the file.
  451.  
  452.    Internal line numbers count from 0 within the current chunk.
  453.    Actual line numbers count from 1 within the entire file;
  454.    in addition, they include lines ignored for comparison purposes.
  455.  
  456.    The `ltran' feature is no longer in use.  */
  457.  
  458. int
  459. translate_line_number (file, lnum)
  460.      struct file_data *file;
  461.      int lnum;
  462. {
  463.   return lnum + 1;
  464. }
  465.  
  466. void
  467. translate_range (file, a, b, aptr, bptr)
  468.      struct file_data *file;
  469.      int a, b;
  470.      int *aptr, *bptr;
  471. {
  472.   *aptr = translate_line_number (file, a - 1) + 1;
  473.   *bptr = translate_line_number (file, b + 1) - 1;
  474. }
  475.  
  476. /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
  477.    If the two numbers are identical, print just one number.
  478.  
  479.    Args A and B are internal line numbers.
  480.    We print the translated (real) line numbers.  */
  481.  
  482. void
  483. print_number_range (sepchar, file, a, b)
  484.      char sepchar;
  485.      struct file_data *file;
  486.      int a, b;
  487. {
  488.   int trans_a, trans_b;
  489.   translate_range (file, a, b, &trans_a, &trans_b);
  490.  
  491.   /* Note: we can have B < A in the case of a range of no lines.
  492.      In this case, we should print the line number before the range,
  493.      which is B.  */
  494.   if (trans_b > trans_a)
  495.     fprintf (outfile, "%d%c%d", trans_a, sepchar, trans_b);
  496.   else
  497.     fprintf (outfile, "%d", trans_b);
  498. }
  499.  
  500. /* Look at a hunk of edit script and report the range of lines in each file
  501.    that it applies to.  HUNK is the start of the hunk, which is a chain
  502.    of `struct change'.  The first and last line numbers of file 0 are stored in
  503.    *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1. 
  504.    Note that these are internal line numbers that count from 0.
  505.  
  506.    If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
  507.  
  508.    Also set *DELETES nonzero if any lines of file 0 are deleted
  509.    and set *INSERTS nonzero if any lines of file 1 are inserted.
  510.    If only ignorable lines are inserted or deleted, both are
  511.    set to 0.  */
  512.  
  513. void
  514. analyze_hunk (hunk, first0, last0, first1, last1, deletes, inserts)
  515.      struct change *hunk;
  516.      int *first0, *last0, *first1, *last1;
  517.      int *deletes, *inserts;
  518. {
  519.   int f0, l0, f1, l1, show_from, show_to;
  520.   int i;
  521.   int nontrivial = !(ignore_blank_lines_flag || ignore_regexp);
  522.   struct change *next;
  523.  
  524.   show_from = show_to = 0;
  525.  
  526.   f0 = hunk->line0;
  527.   f1 = hunk->line1;
  528.  
  529.   for (next = hunk; next; next = next->link)
  530.     {
  531.       l0 = next->line0 + next->deleted - 1;
  532.       l1 = next->line1 + next->inserted - 1;
  533.       show_from += next->deleted;
  534.       show_to += next->inserted;
  535.  
  536.       for (i = next->line0; i <= l0 && ! nontrivial; i++)
  537.     if ((!ignore_blank_lines_flag || files[0].linbuf[i].length > 1)
  538.         && (!ignore_regexp
  539.         || 0 > re_search (&ignore_regexp_compiled,
  540.                   files[0].linbuf[i].text,
  541.                   files[0].linbuf[i].length, 0,
  542.                   files[0].linbuf[i].length, 0)))
  543.       nontrivial = 1;
  544.  
  545.       for (i = next->line1; i <= l1 && ! nontrivial; i++)
  546.     if ((!ignore_blank_lines_flag || files[1].linbuf[i].length > 1)
  547.         && (!ignore_regexp
  548.         || 0 > re_search (&ignore_regexp_compiled,
  549.                   files[1].linbuf[i].text,
  550.                   files[1].linbuf[i].length, 0,
  551.                   files[1].linbuf[i].length, 0)))
  552.       nontrivial = 1;
  553.     }
  554.  
  555.   *first0 = f0;
  556.   *last0 = l0;
  557.   *first1 = f1;
  558.   *last1 = l1;
  559.  
  560.   /* If all inserted or deleted lines are ignorable,
  561.      tell the caller to ignore this hunk.  */
  562.  
  563.   if (!nontrivial)
  564.     show_from = show_to = 0;
  565.  
  566.   *deletes = show_from;
  567.   *inserts = show_to;
  568. }
  569.  
  570. /* malloc a block of memory, with fatal error message if we can't do it. */
  571.  
  572. void *
  573. xmalloc (size)
  574.      unsigned size;
  575. {
  576.   register void *value;
  577.  
  578.   if (!size) size = 4;
  579.  
  580.   if (!(value = (void *) malloc (size)))
  581.     fatal ("virtual memory exhausted");
  582.   return value;
  583. }
  584.  
  585. /* realloc a block of memory, with fatal error message if we can't do it. */
  586.  
  587. void *
  588. xrealloc (old, size)
  589.      void *old;
  590.      unsigned int size;
  591. {
  592.   register void *value;
  593.  
  594.   if (!size) size = 4;
  595.  
  596.   if (!(value = (void *) realloc (old, size)))
  597.     fatal ("virtual memory exhausted");
  598.   return value;
  599. }
  600.  
  601. void *
  602. xcalloc (nitems, size)
  603.      int nitems, size;
  604. {
  605.   void *value;
  606.  
  607.   if (!size) size = 4;
  608.  
  609.   if (!(value = (void *) calloc (nitems, size)))
  610.     fatal ("virtual memory exhausted");
  611.   return value;
  612. }
  613.  
  614. /* Concatenate three strings, returning a newly malloc'd string.  */
  615.  
  616. char *
  617. concat (s1, s2, s3)
  618.      char *s1, *s2, *s3;
  619. {
  620.   int len = strlen (s1) + strlen (s2) + strlen (s3);
  621.   char *new = (char *) xmalloc (len + 1);
  622.   strcpy (new, s1);
  623.   strcat (new, s2);
  624.   strcat (new, s3);
  625.   return new;
  626. }
  627.  
  628. debug_script (sp)
  629.      struct change *sp;
  630. {
  631.   fflush (stdout);
  632.   for (; sp; sp = sp->link)
  633.     fprintf (stderr, "%3d %3d delete %d insert %d\n",
  634.          sp->line0, sp->line1, sp->deleted, sp->inserted);
  635.   fflush (stderr);
  636.   return(0);
  637. }
  638.