home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume05 / ediff < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  5.9 KB

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