home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / GNU Diff Sources / GNU DIFF 1.15b Sources / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-08  |  15.1 KB  |  668 lines  |  [TEXT/ALFA]

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