home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / forum7.lzh / C / EDIFF / ediff.c < prev   
C/C++ Source or Header  |  1988-11-03  |  6KB  |  203 lines

  1. /*
  2.  *
  3.  * ediff - translate diff output into plain English
  4.  *
  5.  * Translates only the normal diff output (not -c or -e/-f).
  6.  * Use as a filter.  Copies non-diff lines verbatim.  Deletes NULs.
  7.  *
  8.  * The Unix variant that runs on our Charles River Data Systems
  9.  * machines, UNOS, comes with, in addition to the standard diff program,
  10.  * a program called "difference" that produces more human-readable
  11.  * output.  Ediff provides the advantages of that program to users of other
  12.  * versions of Unix (soon to include us, as we're hoping to get rid of
  13.  * the CRDS machines before too long).
  14.  *
  15.  * Thanks to Mike Haertel for the following information.
  16.  * The exact meaning of the normal diff symbols is as follows
  17.  * (all line numbers are the ORIGINAL line numbers in each file);
  18.  * 
  19.  *     5,7c8,10
  20.  * Change lines 5-7 of file 1 to read as lines 8-10 of file 2 (or, if changing
  21.  * file 2 into file 1, change lines 8-10 of file 2 to read as lines 5-7
  22.  * of file 1).
  23.  * 
  24.  *     5,7d3
  25.  * Delete lines 5-7 of file 1 (alternatively, append lines 5-7 of file 1
  26.  * after line 3 of file 2).
  27.  * 
  28.  *     8a12,15
  29.  * Append lines 12-15 of file 2 after line 8 of file 1 (alternatively,
  30.  * delete lines 12-15 of file 2).
  31.  *
  32.  * David MacKenzie
  33.  * Latest revision: 10/19/88
  34.  */ 
  35.  
  36. #include <stdio.h>
  37.  
  38. #define UNUSED -1L
  39.  
  40. #ifdef __STDC__
  41. #define P(x) x
  42. #else
  43. #define P(x) ()
  44. #endif
  45.  
  46. void change P((long, long, long, long));
  47. void delete P((long, long, long));
  48. void append P((long, long, long));
  49. void copylines P((long, int));
  50. int readline P((char *, int, FILE *));
  51.  
  52. char buf[BUFSIZ]; /* Input line buffer. */
  53.  
  54. main(argc, argv)
  55. char *argv[];
  56. {
  57.   long f1n1, f1n2, f2n1, f2n2;  /* File 1, line number 1; etc. */
  58.   int res;  /* Result of readline. */
  59.     
  60.   if ((argc>1) && (argv[1]="-?")) {
  61.     fprintf(stderr,"Syntax   : 'ediff <file'  or  'diff <f1> <f2> ! ediff'\n");
  62.     fprintf(stderr,"Function : print output of 'diff' in plain text\n");
  63.     fprintf(stderr,"Options  : none\n");
  64.     exit(0);
  65.   }
  66.   while (res = readline (buf, BUFSIZ, stdin))
  67.     {
  68.       f1n2 = f2n2 = UNUSED;
  69.       if
  70.         (sscanf (buf, "%ld,%ldc%ld,%ld\n", &f1n1, &f1n2, &f2n1, &f2n2) == 4 ||
  71.         sscanf (buf, "%ld,%ldc%ld\n", &f1n1, &f1n2, &f2n1) == 3 ||
  72.         sscanf (buf, "%ldc%ld,%ld\n", &f1n1, &f2n1, &f2n2) == 3 ||
  73.         sscanf (buf, "%ldc%ld\n", &f1n1, &f2n1) == 2)
  74.           change (f1n1, f1n2, f2n1, f2n2);
  75.       else if
  76.         (sscanf (buf, "%ld,%ldd%ld\n", &f1n1, &f1n2, &f2n1) == 3 ||
  77.         sscanf (buf, "%ldd%ld\n", &f1n1, &f2n1) == 2)
  78.           delete (f1n1, f1n2, f2n1);
  79.       else if
  80.         (sscanf (buf, "%lda%ld,%ld\n", &f1n1, &f2n1, &f2n2) == 3 ||
  81.         sscanf (buf, "%lda%ld\n", &f1n1, &f2n1) == 2)
  82.           append (f1n1, f2n1, f2n2);
  83.       else
  84.         {
  85.           /* The line wasn't the start of a diff.  Copy it verbatim. */
  86.           fputs (buf, stdout);
  87.           /* If it was a long line, copy the remainder. */
  88.           while (res == -1)
  89.             {
  90.               res = readline (buf, BUFSIZ, stdin);
  91.               fputs (buf, stdout);
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97. void change (f1n1, f1n2, f2n1, f2n2)
  98.   long f1n1, f1n2, f2n1, f2n2;
  99. {
  100.   printf ("\n-------- ");
  101.   if (f1n2 == UNUSED && f2n2 == UNUSED)
  102.     printf ("1 line changed at %ld from:\n",
  103.       f1n1); /* 1c1 */
  104.   else if (f1n2 == UNUSED)
  105.     printf ("1 line changed to %ld lines at %ld from:\n",
  106.       f2n2 - f2n1 + 1L, f1n1); /* 1c1,2 */
  107.   else if (f2n2 == UNUSED)
  108.     printf ("%ld lines changed to 1 line at %ld-%ld from:\n",
  109.       f1n2 - f1n1 + 1L, f1n1, f1n2); /* 1,2c1 */
  110.   else if (f1n2 - f1n1 == f2n2 - f2n1)
  111.     printf ("%ld lines changed at %ld-%ld from:\n",
  112.       f1n2 - f1n1 + 1L, f1n1, f1n2); /* 1,2c1,2 */
  113.   else
  114.     printf ("%ld lines changed to %ld lines at %ld-%ld from:\n",
  115.       f1n2 - f1n1 + 1L, f2n2 - f2n1 + 1L, f1n1, f1n2); /* 1,2c1,3 */
  116.  
  117.   if (f1n2 == UNUSED)
  118.     copylines (1, 2); /* Skip the "< ". */
  119.   else
  120.     copylines (f1n2 - f1n1 + 1L, 2);
  121.  
  122.   printf ("-------- to:\n");
  123.   (void) readline (buf, BUFSIZ, stdin); /* Eat the "---" line. */
  124.   if (f2n2 == UNUSED)
  125.     copylines (1, 2); /* Skip the "> ". */
  126.   else
  127.     copylines (f2n2 - f2n1 + 1L, 2);
  128. }
  129.  
  130. /* ARGSUSED */
  131. void delete (f1n1, f1n2, f2n1)
  132.   long f1n1, f1n2, f2n1;
  133. {
  134.   printf ("\n-------- ");
  135.   if (f1n2 == UNUSED)
  136.     {
  137.       printf ("1 line deleted at %ld:\n", f1n1); /* 1d1 */
  138.       copylines (1, 2); /* Skip the "< ". */
  139.     }
  140.   else
  141.     {
  142.       printf ("%ld lines deleted at %ld:\n", f1n2 - f1n1 + 1L, f1n1); /* 1,2d1 */
  143.       copylines (f1n2 - f1n1 + 1L, 2);
  144.     }
  145. }
  146.  
  147. void append (f1n1, f2n1, f2n2)
  148.   long f1n1, f2n1, f2n2;
  149. {
  150.   printf ("\n-------- ");
  151.   if (f2n2 == UNUSED)
  152.     {
  153.       printf ("1 line added at %ld:\n", f1n1); /* 1a1 */
  154.       copylines (1, 2); /* Skip the "> ". */
  155.     }
  156.   else
  157.     {
  158.       printf ("%ld lines added at %ld:\n", f2n2 - f2n1 + 1L, f1n1); /* 1a1,2 */
  159.       copylines (f2n2 - f2n1 + 1L, 2);
  160.     }
  161. }
  162.  
  163. /*
  164.  * Copy nlines lines from stdin to stdout; start writing at position skip.
  165.  */
  166. void copylines (nlines, skip)
  167.   long nlines;
  168.   int skip;
  169. {
  170.   int res;
  171.   
  172.   while (nlines-- > 0L)
  173.     {
  174.       res = readline (buf, BUFSIZ, stdin);
  175.       fputs (&buf[skip], stdout);
  176.       /* If it was a long line, copy the remainder. */
  177.       while (res == -1)
  178.         {
  179.           res = readline (buf, BUFSIZ, stdin);
  180.           fputs (buf, stdout);
  181.         }
  182.     }
  183. }
  184.  
  185. /*
  186.  * Front end to fgets.
  187.  * Exit if EOF; return 1 if end of line read, -1 if partial line read.
  188.  */
  189. int readline (s, len, fp)
  190.   char *s;
  191.   int len;
  192.   FILE *fp;
  193. {
  194.   if (!fgets (s, len, fp))
  195.     exit (0);
  196.   if (s[strlen (s) - 1] == '\n')
  197.     return 1;
  198.   else
  199.     return -1;
  200. }
  201.  
  202.  
  203.