home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / ediff-v2 < prev    next >
Text File  |  1989-02-03  |  8KB  |  275 lines

  1. Path: xanth!ukma!rutgers!ucsd!ucbvax!decwrl!wyse!uunet!allbery
  2. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  3. Newsgroups: comp.sources.misc
  4. Subject: v06i010: diff to English translator, improved
  5. Message-ID: <47275@uunet.UU.NET>
  6. Date: 24 Jan 89 03:01:48 GMT
  7. Sender: allbery@uunet.UU.NET
  8. Reply-To: edf@ROCKY2.ROCKEFELLER.EDU (David MacKenzie)
  9. Lines: 263
  10. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  11.  
  12. Posting-number: Volume 6, Issue 10
  13. Submitted-by: edf@ROCKY2.ROCKEFELLER.EDU (David MacKenzie)
  14. Archive-name: ediff-v2
  15.  
  16. This is a new version of the diff-to-English translator I posted to this
  17. group a few months ago.  It contains a manual page, thanks to 
  18. R. P. C. Rodgers (rodgers@maxwell.mmwb.ucsf.edu), and a bugfix which
  19. affects machines where sizeof (long) != sizeof (int), submitted by 
  20. Brian Renaud (uunet!umix!huron!bdr).  I have also clarified the source
  21. code somewhat.  Other than those changes, it should work the same, but
  22. since the program is short I decided that I might just as well repost it.
  23.  
  24. -----
  25. David MacKenzie
  26. Environmental Defense Fund
  27. edf@rocky2.rockefeller.edu (...rutgers!cmcl2!rocky2!edf)
  28.  
  29. # This is a shell archive.  Remove anything before this line,
  30. # then unpack it by saving it in a file and typing "sh file".
  31. #
  32. # Wrapped by rocky2.rockefeller.edu!edf on Fri Dec 30 22:27:34 EST 1988
  33. # Contents:  ediff.1 ediff.c
  34.  
  35. echo x - ediff.1
  36. sed 's/^@//' > "ediff.1" <<'@//E*O*F ediff.1//'
  37. @.TH EDIFF 1L "30 December 1988"
  38. @.SH NAME
  39. ediff \- translate diff output into plain English
  40. @.SH SYNOPSIS
  41. @.B ediff
  42. @.SH DESCRIPTION
  43. @.I Ediff
  44. is a filter which translates normal
  45. @.IR diff (1)
  46. output (not output produced with the
  47. @.BR \-c ,
  48. @.BR \-e ,
  49. or
  50. @.B \-f
  51. @.I diff
  52. options) into more readable English descriptions.
  53. It copies non-diff material verbatim.
  54. @.SH "SEE ALSO"
  55. comm(1), diff(1), uniq(1)
  56. @.SH AUTHOR
  57. @.nf
  58. David MacKenzie (edf@rocky2.rockefeller.edu).
  59. Manual page written with the help of R. P. C. Rodgers
  60. (rodgers@maxwell.mmwb.ucsf.edu).
  61. @//E*O*F ediff.1//
  62. chmod u=rw,g=r,o=r ediff.1
  63.  
  64. echo x - ediff.c
  65. sed 's/^@//' > "ediff.c" <<'@//E*O*F ediff.c//'
  66. /*
  67. % cc -O ediff.c -o ediff && strip ediff
  68.  
  69.    ediff - translate diff output into plain English
  70.  
  71.    Translates only the normal diff output (not -c or -e/-f).
  72.    Use as a filter.  Copies non-diff lines verbatim.  Deletes NULs.
  73.  
  74.    The Unix variant that runs on our Charles River Data Systems
  75.    machines, UNOS, comes with, in addition to the standard diff program,
  76.    a program called "difference" that produces more human-readable
  77.    output.  Ediff provides the advantages of that program to users of other
  78.    versions of Unix (soon to include us, as we're hoping to get rid of
  79.    the CRDS machines before too long).
  80.  
  81.    Thanks to Mike Haertel for the following information.
  82.    The exact meaning of the normal diff symbols is as follows
  83.    (all line numbers are the ORIGINAL line numbers in each file):
  84.  
  85.        5,7c8,10
  86.    Change lines 5-7 of file 1 to read as lines 8-10 of file 2 (or, if changing
  87.    file 2 into file 1, change lines 8-10 of file 2 to read as lines 5-7
  88.    of file 1).
  89.  
  90.        5,7d3
  91.    Delete lines 5-7 of file 1 (alternatively, append lines 5-7 of file 1
  92.    after line 3 of file 2).
  93.  
  94.        8a12,15
  95.    Append lines 12-15 of file 2 after line 8 of file 1 (alternatively,
  96.    delete lines 12-15 of file 2).
  97.  
  98.    David MacKenzie
  99.    edf@rocky2.rockefeller.edu
  100.  
  101.    Latest revision: 12/30/88 */
  102.  
  103. #include <stdio.h>
  104.  
  105. /* Magic value meaning that a line number is missing from the input. */
  106. #define UNUSED -1L
  107.  
  108. #ifdef __STDC__
  109. #define P(x) x
  110. #else
  111. #define P(x) ()
  112. #endif
  113.  
  114. void exit P((int));
  115.  
  116. void change P ((long, long, long, long));
  117. void delete P ((long, long, long));
  118. void append P ((long, long, long));
  119. void copylines P ((long, int));
  120. int readline P ((char *, int, FILE *));
  121.  
  122. char line[BUFSIZ];        /* Input line. */
  123.  
  124. main ()
  125. {
  126.   long f1n1, f1n2, f2n1, f2n2;    /* File 1, line number 1; etc. */
  127.   int result;            /* Result of readline. */
  128.  
  129.   for (;;)
  130.     {
  131.       result = readline (line, BUFSIZ, stdin);
  132.       f1n2 = f2n2 = UNUSED;
  133.       if
  134.     (sscanf (line, "%ld,%ldc%ld,%ld\n", &f1n1, &f1n2, &f2n1, &f2n2) == 4 ||
  135.     sscanf (line, "%ld,%ldc%ld\n", &f1n1, &f1n2, &f2n1) == 3 ||
  136.     sscanf (line, "%ldc%ld,%ld\n", &f1n1, &f2n1, &f2n2) == 3 ||
  137.     sscanf (line, "%ldc%ld\n", &f1n1, &f2n1) == 2)
  138.     change (f1n1, f1n2, f2n1, f2n2);
  139.       else if
  140.       (sscanf (line, "%ld,%ldd%ld\n", &f1n1, &f1n2, &f2n1) == 3 ||
  141.     sscanf (line, "%ldd%ld\n", &f1n1, &f2n1) == 2)
  142.     delete (f1n1, f1n2, f2n1);
  143.       else if
  144.       (sscanf (line, "%lda%ld,%ld\n", &f1n1, &f2n1, &f2n2) == 3 ||
  145.     sscanf (line, "%lda%ld\n", &f1n1, &f2n1) == 2)
  146.     append (f1n1, f2n1, f2n2);
  147.       else
  148.     {
  149.       /* The line wasn't the start of a diff.  Copy it verbatim. */
  150.       fputs (line, stdout);
  151.       /* If it was a long line, copy the remainder. */
  152.       while (result == -1)
  153.         {
  154.           result = readline (line, BUFSIZ, stdin);
  155.           fputs (line, stdout);
  156.         }
  157.     }
  158.     }
  159. }
  160.  
  161. void change (f1n1, f1n2, f2n1, f2n2)
  162.   long f1n1, f1n2, f2n1, f2n2;
  163. {
  164.   printf ("\n-------- ");
  165.   if (f1n2 == UNUSED && f2n2 == UNUSED)
  166.     /* 1c1 */
  167.     printf ("1 line changed at %ld from:\n",
  168.       f1n1);
  169.   else if (f1n2 == UNUSED)
  170.     /* 1c1,2 */
  171.     printf ("1 line changed to %ld lines at %ld from:\n",
  172.       f2n2 - f2n1 + 1L, f1n1);
  173.   else if (f2n2 == UNUSED)
  174.     /* 1,2c1 */
  175.     printf ("%ld lines changed to 1 line at %ld-%ld from:\n",
  176.       f1n2 - f1n1 + 1L, f1n1, f1n2);
  177.   else if (f1n2 - f1n1 == f2n2 - f2n1)
  178.     /* 1,2c1,2 */
  179.     printf ("%ld lines changed at %ld-%ld from:\n",
  180.       f1n2 - f1n1 + 1L, f1n1, f1n2);
  181.   else
  182.     /* 1,2c1,3 */
  183.     printf ("%ld lines changed to %ld lines at %ld-%ld from:\n",
  184.       f1n2 - f1n1 + 1L, f2n2 - f2n1 + 1L, f1n1, f1n2);
  185.  
  186.   if (f1n2 == UNUSED)
  187.     copylines (1L, 2);        /* Skip the "< ". */
  188.   else
  189.     copylines (f1n2 - f1n1 + 1L, 2);
  190.  
  191.   printf ("-------- to:\n");
  192.   (void) readline (line, BUFSIZ, stdin);    /* Eat the "---" line. */
  193.   if (f2n2 == UNUSED)
  194.     copylines (1L, 2);        /* Skip the "> ". */
  195.   else
  196.     copylines (f2n2 - f2n1 + 1L, 2);
  197. }
  198.  
  199. /* ARGSUSED */
  200. void delete (f1n1, f1n2, f2n1)
  201.   long f1n1, f1n2, f2n1;
  202. {
  203.   printf ("\n-------- ");
  204.   if (f1n2 == UNUSED)
  205.     {
  206.       /* 1d1 */
  207.       printf ("1 line deleted at %ld:\n", f1n1);
  208.       copylines (1L, 2);    /* Skip the "< ". */
  209.     }
  210.   else
  211.     {
  212.       /* 1,2d1 */
  213.       printf ("%ld lines deleted at %ld:\n", f1n2 - f1n1 + 1L, f1n1);
  214.       copylines (f1n2 - f1n1 + 1L, 2);
  215.     }
  216. }
  217.  
  218. void append (f1n1, f2n1, f2n2)
  219.   long f1n1, f2n1, f2n2;
  220. {
  221.   printf ("\n-------- ");
  222.   if (f2n2 == UNUSED)
  223.     {
  224.       /* 1a1 */
  225.       printf ("1 line added at %ld:\n", f1n1);
  226.       copylines (1L, 2);    /* Skip the "> ". */
  227.     }
  228.   else
  229.     {
  230.       /* 1a1,2 */
  231.       printf ("%ld lines added at %ld:\n", f2n2 - f2n1 + 1L, f1n1);
  232.       copylines (f2n2 - f2n1 + 1L, 2);
  233.     }
  234. }
  235.  
  236. /* Copy nlines lines from stdin to stdout; start writing at position skip. */
  237.  
  238. void copylines (nlines, skip)
  239.   long nlines;
  240.   int skip;
  241. {
  242.   int result;
  243.  
  244.   while (nlines-- > 0L)
  245.     {
  246.       result = readline (line, BUFSIZ, stdin);
  247.       fputs (&line[skip], stdout);
  248.       /* If it was a long line, copy the remainder. */
  249.       while (result == -1)
  250.     {
  251.       result = readline (line, BUFSIZ, stdin);
  252.       fputs (line, stdout);
  253.     }
  254.     }
  255. }
  256.  
  257. /* Front end to fgets.
  258.    Exit if EOF; return 1 if end of line read, -1 if partial line read. */
  259.  
  260. int readline (s, length, fp)
  261.   char *s;
  262.   int length;
  263.   FILE *fp;
  264. {
  265.   if (!fgets (s, length, fp))
  266.     exit (0);
  267.   if (s[strlen (s) - 1] == '\n')
  268.     return 1;
  269.   else
  270.     return -1;
  271. }
  272. @//E*O*F ediff.c//
  273. chmod u=rw,g=r,o=r ediff.c
  274. exit 0
  275.