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

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