home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / rcs57pc3.zip / diff / util.c < prev    next >
C/C++ Source or Header  |  1994-11-14  |  18KB  |  712 lines

  1. /* Support routines for GNU DIFF.
  2.    Copyright (C) 1988, 1989, 1992, 1993, 1994 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 2, 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. #ifndef PR_PROGRAM
  23. #define PR_PROGRAM "/bin/pr"
  24. #endif
  25. static char const pr_program[] = PR_PROGRAM;
  26.  
  27. /* Queue up one-line messages to be printed at the end,
  28.    when -l is specified.  Each message is recorded with a `struct msg'.  */
  29.  
  30. struct msg
  31. {
  32.   struct msg *next;
  33.   char args[1]; /* Format + 4 args, each '\0' terminated, concatenated.  */
  34. };
  35.  
  36. /* Head of the chain of queues messages.  */
  37.  
  38. static struct msg *msg_chain;
  39.  
  40. /* Tail of the chain of queues messages.  */
  41.  
  42. static struct msg **msg_chain_end = &msg_chain;
  43.  
  44. /* Use when a system call returns non-zero status.
  45.    TEXT should normally be the file name.  */
  46.  
  47. void
  48. perror_with_name (text)
  49.      char const *text;
  50. {
  51.   error (0, errno, "%s", text);
  52. }
  53.  
  54. /* Use when a system call returns non-zero status and that is fatal.  */
  55.  
  56. void
  57. pfatal_with_name (text)
  58.      char const *text;
  59. {
  60.   int e = errno;
  61.   print_message_queue ();
  62.   error (2, e, "%s", text);
  63. }
  64.  
  65. /* Print an error message containing MSGID, then exit.  */
  66.  
  67. void
  68. fatal (msgid)
  69.      char const *msgid;
  70. {
  71.   print_message_queue ();
  72.   error (2, 0, "%s", gettext (msgid));
  73. }
  74.  
  75. /* Like printf, except if -l in effect then save the message and print later.
  76.    This is used for things like "Only in ...".  */
  77.  
  78. void
  79. message (format_msgid, arg1, arg2)
  80.      char const *format_msgid, *arg1, *arg2;
  81. {
  82.   message5 (format_msgid, arg1, arg2, 0, 0);
  83. }
  84.  
  85. void
  86. message5 (format_msgid, arg1, arg2, arg3, arg4)
  87.      char const *format_msgid, *arg1, *arg2, *arg3, *arg4;
  88. {
  89.   if (paginate_flag)
  90.     {
  91.       char *p;
  92.       char const *arg[5];
  93.       int i;
  94.       size_t size[5];
  95.       size_t total_size = sizeof (struct msg);
  96.       struct msg *new;
  97.  
  98.       arg[0] = format_msgid;
  99.       arg[1] = arg1;
  100.       arg[2] = arg2;
  101.       arg[3] = arg3 ? arg3 : "";
  102.       arg[4] = arg4 ? arg4 : "";
  103.  
  104.       for (i = 0;  i < 5;  i++)
  105.     total_size += size[i] = strlen (arg[i]) + 1;
  106.  
  107.       new = (struct msg *) xmalloc (total_size);
  108.  
  109.       for (i = 0, p = new->args;  i < 5;  p += size[i++])
  110.     memcpy (p, arg[i], size[i]);
  111.  
  112.       *msg_chain_end = new;
  113.       new->next = 0;
  114.       msg_chain_end = &new->next;
  115.     }
  116.   else
  117.     {
  118.       if (sdiff_help_sdiff)
  119.     putchar (' ');
  120.       printf (gettext (format_msgid), arg1, arg2, arg3, arg4);
  121.     }
  122. }
  123.  
  124. /* Output all the messages that were saved up by calls to `message'.  */
  125.  
  126. void
  127. print_message_queue ()
  128. {
  129.   char const *arg[5];
  130.   int i;
  131.   struct msg *m;
  132.  
  133.   for (m = msg_chain; m; m = m->next)
  134.     {
  135.       arg[0] = m->args;
  136.       for (i = 0;  i < 4;  i++)
  137.     arg[i + 1] = arg[i] + strlen (arg[i]) + 1;
  138.       printf (gettext (arg[0]), arg[1], arg[2], arg[3], arg[4]);
  139.     }
  140. }
  141.  
  142. /* Call before outputting the results of comparing files NAME0 and NAME1
  143.    to set up OUTFILE, the stdio stream for the output to go to.
  144.  
  145.    Usually, OUTFILE is just stdout.  But when -l was specified
  146.    we fork off a `pr' and make OUTFILE a pipe to it.
  147.    `pr' then outputs to our stdout.  */
  148.  
  149. static char const *current_name0;
  150. static char const *current_name1;
  151. static int current_depth;
  152.  
  153. void
  154. setup_output (name0, name1, depth)
  155.      char const *name0, *name1;
  156.      int depth;
  157. {
  158.   current_name0 = name0;
  159.   current_name1 = name1;
  160.   current_depth = depth;
  161.   outfile = 0;
  162. }
  163.  
  164. #if HAVE_FORK
  165. static pid_t pr_pid;
  166. #endif
  167.  
  168. void
  169. begin_output ()
  170. {
  171.   char *name;
  172.  
  173.   if (outfile != 0)
  174.     return;
  175.  
  176.   /* Construct the header of this piece of diff.  */
  177.   name = xmalloc (strlen (current_name0) + strlen (current_name1)
  178.           + strlen (switch_string) + 7);
  179.   /* Posix.2 section 4.17.6.1.1 specifies this format.  But there are some
  180.      bugs in the first printing (IEEE Std 1003.2-1992 p 251 l 3304):
  181.      it says that we must print only the last component of the pathnames,
  182.      and it requires two spaces after "diff" if there are no options.
  183.      These requirements are silly and do not match historical practice.  */
  184.   sprintf (name, "diff%s %s %s", switch_string, current_name0, current_name1);
  185.  
  186.   if (paginate_flag)
  187.     {
  188.       /* Make OUTFILE a pipe to a subsidiary `pr'.  */
  189.  
  190. #if HAVE_FORK
  191.       int pipes[2];
  192.  
  193.       if (pipe (pipes) != 0)
  194.     pfatal_with_name ("pipe");
  195.  
  196.       fflush (stdout);
  197.  
  198.       pr_pid = vfork ();
  199.       if (pr_pid < 0)
  200.     pfatal_with_name ("fork");
  201.  
  202.       if (pr_pid == 0)
  203.     {
  204.       close (pipes[1]);
  205.       if (pipes[0] != STDIN_FILENO)
  206.         {
  207.           if (dup2 (pipes[0], STDIN_FILENO) < 0)
  208.         pfatal_with_name ("dup2");
  209.           close (pipes[0]);
  210.         }
  211.  
  212.       execl (pr_program, pr_program, "-f", "-h", name, 0);
  213.       pfatal_with_name (pr_program);
  214.     }
  215.       else
  216.     {
  217.       close (pipes[0]);
  218.       outfile = fdopen (pipes[1], "w");
  219.       if (!outfile)
  220.         pfatal_with_name ("fdopen");
  221.     }
  222. #else /* ! HAVE_FORK */
  223.       char *command = xmalloc (sizeof (pr_program) - 1 + 7
  224.                    + system_quote_arg ((char *) 0, name) + 1);
  225.       char *p;
  226.       sprintf (command, "%s -f -h ", pr_program);
  227.       p = command + sizeof (pr_program) - 1 + 7;
  228.       p += system_quote_arg (p, name);
  229.       *p = 0;
  230.       outfile = popen (command, "w");
  231.       if (!outfile)
  232.     pfatal_with_name (command);
  233.       free (command);
  234. #endif /* ! HAVE_FORK */
  235.     }
  236.   else
  237.     {
  238.  
  239.       /* If -l was not specified, output the diff straight to `stdout'.  */
  240.  
  241.       outfile = stdout;
  242.  
  243.       /* If handling multiple files (because scanning a directory),
  244.      print which files the following output is about.  */
  245.       if (current_depth > 0)
  246.     printf ("%s\n", name);
  247.     }
  248.  
  249.   free (name);
  250.  
  251.   /* A special header is needed at the beginning of context output.  */
  252.   switch (output_style)
  253.     {
  254.     case OUTPUT_CONTEXT:
  255.       print_context_header (files, 0);
  256.       break;
  257.  
  258.     case OUTPUT_UNIFIED:
  259.       print_context_header (files, 1);
  260.       break;
  261.  
  262.     default:
  263.       break;
  264.     }
  265. }
  266.  
  267. /* Call after the end of output of diffs for one file.
  268.    Close OUTFILE and get rid of the `pr' subfork.  */
  269.  
  270. void
  271. finish_output ()
  272. {
  273.   if (outfile != 0 && outfile != stdout)
  274.     {
  275.       int wstatus;
  276.       if (ferror (outfile))
  277.     fatal ("write failed");
  278. #if ! HAVE_FORK
  279.       wstatus = pclose (outfile);
  280. #else /* HAVE_FORK */
  281.       if (fclose (outfile) != 0)
  282.     pfatal_with_name (gettext ("write failed"));
  283.       if (waitpid (pr_pid, &wstatus, 0) < 0)
  284.     pfatal_with_name ("waitpid");
  285. #endif /* HAVE_FORK */
  286.       if (wstatus != 0)
  287.     fatal ("subsidiary program failed");
  288.     }
  289.  
  290.   outfile = 0;
  291. }
  292.  
  293. /* Compare two lines (typically one from each input file)
  294.    according to the command line options.
  295.    For efficiency, this is invoked only when the lines do not match exactly
  296.    but an option like -i might cause us to ignore the difference.
  297.    Return nonzero if the lines differ.  */
  298.  
  299. int
  300. line_cmp (s1, s2)
  301.      char const *s1, *s2;
  302. {
  303.   register unsigned char const *t1 = (unsigned char const *) s1;
  304.   register unsigned char const *t2 = (unsigned char const *) s2;
  305.  
  306.   while (1)
  307.     {
  308.       register unsigned char c1 = *t1++;
  309.       register unsigned char c2 = *t2++;
  310.  
  311.       /* Test for exact char equality first, since it's a common case.  */
  312.       if (c1 != c2)
  313.     {
  314.       /* Ignore horizontal white space if -b or -w is specified.  */
  315.  
  316.       if (ignore_all_space_flag)
  317.         {
  318.           /* For -w, just skip past any white space.  */
  319.           while (ISSPACE (c1) && c1 != '\n') c1 = *t1++;
  320.           while (ISSPACE (c2) && c2 != '\n') c2 = *t2++;
  321.         }
  322.       else if (ignore_space_change_flag)
  323.         {
  324.           /* For -b, advance past any sequence of white space in line 1
  325.          and consider it just one Space, or nothing at all
  326.          if it is at the end of the line.  */
  327.           if (ISSPACE (c1))
  328.         {
  329.           while (c1 != '\n')
  330.             {
  331.               c1 = *t1++;
  332.               if (! ISSPACE (c1))
  333.             {
  334.               --t1;
  335.               c1 = ' ';
  336.               break;
  337.             }
  338.             }
  339.         }
  340.  
  341.           /* Likewise for line 2.  */
  342.           if (ISSPACE (c2))
  343.         {
  344.           while (c2 != '\n')
  345.             {
  346.               c2 = *t2++;
  347.               if (! ISSPACE (c2))
  348.             {
  349.               --t2;
  350.               c2 = ' ';
  351.               break;
  352.             }
  353.             }
  354.         }
  355.  
  356.           if (c1 != c2)
  357.         {
  358.           /* If we went too far when doing the simple test
  359.              for equality, go back to the first non-white-space
  360.              character in both sides and try again.  */
  361.           if (c2 == ' ' && c1 != '\n'
  362.               && (unsigned char const *) s1 + 1 < t1
  363.               && ISSPACE (t1[-2]))
  364.             {
  365.               --t1;
  366.               continue;
  367.             }
  368.           if (c1 == ' ' && c2 != '\n'
  369.               && (unsigned char const *) s2 + 1 < t2
  370.               && ISSPACE (t2[-2]))
  371.             {
  372.               --t2;
  373.               continue;
  374.             }
  375.         }
  376.         }
  377.  
  378.       /* Lowercase all letters if -i is specified.  */
  379.  
  380.       if (ignore_case_flag)
  381.         {
  382.           if (ISUPPER (c1))
  383.         c1 = _tolower (c1);
  384.           if (ISUPPER (c2))
  385.         c2 = _tolower (c2);
  386.         }
  387.  
  388.       if (c1 != c2)
  389.         break;
  390.     }
  391.       if (c1 == '\n')
  392.     return 0;
  393.     }
  394.  
  395.   return 1;
  396. }
  397.  
  398. /* Find the consecutive changes at the start of the script START.
  399.    Return the last link before the first gap.  */
  400.  
  401. struct change *
  402. find_change (start)
  403.      struct change *start;
  404. {
  405.   return start;
  406. }
  407.  
  408. struct change *
  409. find_reverse_change (start)
  410.      struct change *start;
  411. {
  412.   return start;
  413. }
  414.  
  415. /* Divide SCRIPT into pieces by calling HUNKFUN and
  416.    print each piece with PRINTFUN.
  417.    Both functions take one arg, an edit script.
  418.  
  419.    HUNKFUN is called with the tail of the script
  420.    and returns the last link that belongs together with the start
  421.    of the tail.
  422.  
  423.    PRINTFUN takes a subscript which belongs together (with a null
  424.    link at the end) and prints it.  */
  425.  
  426. void
  427. print_script (script, hunkfun, printfun)
  428.      struct change *script;
  429.      struct change * (*hunkfun) PARAMS((struct change *));
  430.      void (*printfun) PARAMS((struct change *));
  431. {
  432.   struct change *next = script;
  433.  
  434.   while (next)
  435.     {
  436.       struct change *this, *end;
  437.  
  438.       /* Find a set of changes that belong together.  */
  439.       this = next;
  440.       end = (*hunkfun) (next);
  441.  
  442.       /* Disconnect them from the rest of the changes,
  443.      making them a hunk, and remember the rest for next iteration.  */
  444.       next = end->link;
  445.       end->link = 0;
  446. #ifdef DEBUG
  447.       debug_script (this);
  448. #endif
  449.  
  450.       /* Print this hunk.  */
  451.       (*printfun) (this);
  452.  
  453.       /* Reconnect the script so it will all be freed properly.  */
  454.       end->link = next;
  455.     }
  456. }
  457.  
  458. /* Print the text of a single line LINE,
  459.    flagging it with the characters in LINE_FLAG (which say whether
  460.    the line is inserted, deleted, changed, etc.).  */
  461.  
  462. void
  463. print_1_line (line_flag, line)
  464.      char const *line_flag;
  465.      char const * const *line;
  466. {
  467.   char const *text = line[0], *limit = line[1]; /* Help the compiler.  */
  468.   FILE *out = outfile; /* Help the compiler some more.  */
  469.   char const *flag_format = 0;
  470.  
  471.   /* If -T was specified, use a Tab between the line-flag and the text.
  472.      Otherwise use a Space (as Unix diff does).
  473.      Print neither space nor tab if line-flags are empty.  */
  474.  
  475.   if (line_flag && *line_flag)
  476.     {
  477.       flag_format = tab_align_flag ? "%s\t" : "%s ";
  478.       fprintf (out, flag_format, line_flag);
  479.     }
  480.  
  481.   output_1_line (text, limit, flag_format, line_flag);
  482.  
  483.   if ((!line_flag || line_flag[0]) && limit[-1] != '\n')
  484.     fprintf (out, "\n\\ %s\n", gettext ("No newline at end of file"));
  485. }
  486.  
  487. /* Output a line from TEXT up to LIMIT.  Without -t, output verbatim.
  488.    With -t, expand white space characters to spaces, and if FLAG_FORMAT
  489.    is nonzero, output it with argument LINE_FLAG after every
  490.    internal carriage return, so that tab stops continue to line up.  */
  491.  
  492. void
  493. output_1_line (text, limit, flag_format, line_flag)
  494.      char const *text, *limit, *flag_format, *line_flag;
  495. {
  496.   if (!tab_expand_flag)
  497.     fwrite (text, sizeof (char), limit - text, outfile);
  498.   else
  499.     {
  500.       register FILE *out = outfile;
  501.       register unsigned char c;
  502.       register char const *t = text;
  503.       register unsigned column = 0;
  504.  
  505.       while (t < limit)
  506.     switch ((c = *t++))
  507.       {
  508.       case '\t':
  509.         {
  510.           unsigned spaces = TAB_WIDTH - column % TAB_WIDTH;
  511.           column += spaces;
  512.           do
  513.         putc (' ', out);
  514.           while (--spaces);
  515.         }
  516.         break;
  517.  
  518.       case '\r':
  519.         putc (c, out);
  520.         if (flag_format && t < limit && *t != '\n')
  521.           fprintf (out, flag_format, line_flag);
  522.         column = 0;
  523.         break;
  524.  
  525.       case '\b':
  526.         if (column == 0)
  527.           continue;
  528.         column--;
  529.         putc (c, out);
  530.         break;
  531.  
  532.       default:
  533.         if (ISPRINT (c))
  534.           column++;
  535.         putc (c, out);
  536.         break;
  537.       }
  538.     }
  539. }
  540.  
  541. int
  542. change_letter (inserts, deletes)
  543.      int inserts, deletes;
  544. {
  545.   if (!inserts)
  546.     return 'd';
  547.   else if (!deletes)
  548.     return 'a';
  549.   else
  550.     return 'c';
  551. }
  552.  
  553. /* Translate an internal line number (an index into diff's table of lines)
  554.    into an actual line number in the input file.
  555.    The internal line number is LNUM.  FILE points to the data on the file.
  556.  
  557.    Internal line numbers count from 0 starting after the prefix.
  558.    Actual line numbers count from 1 within the entire file.  */
  559.  
  560. int
  561. translate_line_number (file, lnum)
  562.      struct file_data const *file;
  563.      int lnum;
  564. {
  565.   return lnum + file->prefix_lines + 1;
  566. }
  567.  
  568. void
  569. translate_range (file, a, b, aptr, bptr)
  570.      struct file_data const *file;
  571.      int a, b;
  572.      int *aptr, *bptr;
  573. {
  574.   *aptr = translate_line_number (file, a - 1) + 1;
  575.   *bptr = translate_line_number (file, b + 1) - 1;
  576. }
  577.  
  578. /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
  579.    If the two numbers are identical, print just one number.
  580.  
  581.    Args A and B are internal line numbers.
  582.    We print the translated (real) line numbers.  */
  583.  
  584. void
  585. print_number_range (sepchar, file, a, b)
  586.      int sepchar;
  587.      struct file_data *file;
  588.      int a, b;
  589. {
  590.   int trans_a, trans_b;
  591.   translate_range (file, a, b, &trans_a, &trans_b);
  592.  
  593.   /* Note: we can have B < A in the case of a range of no lines.
  594.      In this case, we should print the line number before the range,
  595.      which is B.  */
  596.   if (trans_b > trans_a)
  597.     fprintf (outfile, "%d%c%d", trans_a, sepchar, trans_b);
  598.   else
  599.     fprintf (outfile, "%d", trans_b);
  600. }
  601.  
  602. /* Look at a hunk of edit script and report the range of lines in each file
  603.    that it applies to.  HUNK is the start of the hunk, which is a chain
  604.    of `struct change'.  The first and last line numbers of file 0 are stored in
  605.    *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1.
  606.    Note that these are internal line numbers that count from 0.
  607.  
  608.    If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
  609.  
  610.    Also set *DELETES nonzero if any lines of file 0 are deleted
  611.    and set *INSERTS nonzero if any lines of file 1 are inserted.
  612.    If only ignorable lines are inserted or deleted, both are
  613.    set to 0.  */
  614.  
  615. void
  616. analyze_hunk (hunk, first0, last0, first1, last1, deletes, inserts)
  617.      struct change *hunk;
  618.      int *first0, *last0, *first1, *last1;
  619.      int *deletes, *inserts;
  620. {
  621.   struct change *next;
  622.   int l0, l1, show_from, show_to;
  623.   int i;
  624.   int trivial = ignore_blank_lines_flag || ignore_regexp.fastmap;
  625.   int trivial_length = ignore_blank_lines_flag - 1;
  626.     /* If 0, ignore zero-length lines;
  627.        if -1, do not ignore any lines just because of their length.  */
  628.  
  629.   char const * const *linbuf0 = files[0].linbuf;  /* Help the compiler.  */
  630.   char const * const *linbuf1 = files[1].linbuf;
  631.  
  632.   show_from = show_to = 0;
  633.  
  634.   *first0 = hunk->line0;
  635.   *first1 = hunk->line1;
  636.  
  637.   next = hunk;
  638.   do
  639.     {
  640.       l0 = next->line0 + next->deleted - 1;
  641.       l1 = next->line1 + next->inserted - 1;
  642.       show_from += next->deleted;
  643.       show_to += next->inserted;
  644.  
  645.       for (i = next->line0; i <= l0 && trivial; i++)
  646.     {
  647.       char const *line = linbuf0[i];
  648.       int len = linbuf0[i + 1] - line - 1;
  649.       if (len != trivial_length
  650.           && (! ignore_regexp.fastmap
  651.           || re_search (&ignore_regexp, line, len, 0, len, 0) < 0))
  652.         trivial = 0;
  653.     }
  654.  
  655.       for (i = next->line1; i <= l1 && trivial; i++)
  656.     {
  657.       char const *line = linbuf1[i];
  658.       int len = linbuf1[i + 1] - line - 1;
  659.       if (len != trivial_length
  660.           && (! ignore_regexp.fastmap
  661.           || re_search (&ignore_regexp, line, len, 0, len, 0) < 0))
  662.         trivial = 0;
  663.     }
  664.     }
  665.   while ((next = next->link) != 0);
  666.  
  667.   *last0 = l0;
  668.   *last1 = l1;
  669.  
  670.   /* If all inserted or deleted lines are ignorable,
  671.      tell the caller to ignore this hunk.  */
  672.  
  673.   if (trivial)
  674.     show_from = show_to = 0;
  675.  
  676.   *deletes = show_from;
  677.   *inserts = show_to;
  678. }
  679.  
  680. /* Concatenate three strings, returning a newly malloc'd string.  */
  681.  
  682. char *
  683. concat (s1, s2, s3)
  684.      char const *s1, *s2, *s3;
  685. {
  686.   char *new = xmalloc (strlen (s1) + strlen (s2) + strlen (s3) + 1);
  687.   sprintf (new, "%s%s%s", s1, s2, s3);
  688.   return new;
  689. }
  690.  
  691. /* Yield the newly malloc'd pathname
  692.    of the file in DIR whose filename is FILE.  */
  693.  
  694. char *
  695. dir_file_pathname (dir, file)
  696.      char const *dir, *file;
  697. {
  698.   char const *p = filename_lastdirchar (dir);
  699.   return concat (dir, "/" + (p && !p[1]), file);
  700. }
  701.  
  702. void
  703. debug_script (sp)
  704.      struct change *sp;
  705. {
  706.   fflush (stdout);
  707.   for (; sp; sp = sp->link)
  708.     fprintf (stderr, "%3d %3d delete %d insert %d\n",
  709.          sp->line0, sp->line1, sp->deleted, sp->inserted);
  710.   fflush (stderr);
  711. }
  712.