home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / programming / utils / hwgrcs / hwgdiff / rcs.rcsfiles / util.c,v < prev    next >
Encoding:
Text File  |  1993-02-20  |  15.4 KB  |  694 lines

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