home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-alpha / enscript-1.4.0-bin.lha / bin / diffpp next >
Encoding:
Text File  |  1996-10-14  |  2.9 KB  |  143 lines

  1. #!/bin/perl
  2. # -*- perl -*-
  3. #
  4. # Pretty-print diff outputs with GNU enscript.
  5. # Copyright (c) 1996 Markku Rossi
  6. #
  7. # Author: Markku Rossi <mtr@iki.fi>
  8. #
  9.  
  10. #
  11. # This file is part of GNU enscript.
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation; either version 2, or (at your option)
  15. # any later version.
  16. #
  17. # This program is distributed in the hope that it will be useful,
  18. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. # GNU General Public License for more details.
  21. #
  22. # You should have received a copy of the GNU General Public License
  23. # along with this program; see the file COPYING.  If not, write to
  24. # the Free Software Foundation, 59 Temple Place - Suite 330,
  25. # Boston, MA 02111-1307, USA.
  26. #
  27.  
  28. #
  29. # Original idea by Trent Fisher <trent@informix.com>
  30. # Thanks to Tero Kivinen <kivinen@iki.fi> for the first prototype. 
  31. #
  32.  
  33. $file = shift(@ARGV);
  34. $gray = ".95";
  35.  
  36. $program = $0;
  37. $program =~ s/.*\///g;
  38.  
  39. sub usage {
  40.     warn "Usage: $program ORIGINAL_FILE < DIFF\n\n";
  41.     warn "Program reads a diff file from its standard input and annotates
  42. ORIGINAL_FILE to show the changes made to the file.  The easiest way to use 
  43. this program is to use it as an input filter for GNU enscript:
  44.  
  45.   \$ enscript -G2re --filter='rcsdiff %s | diffpp %s' *.c *.h
  46.   \$ enscript -G2re --filter='diff %s~ %s | diffpp %s' *.c *.h
  47.  
  48. ";
  49. }
  50.  
  51. if (!defined($file) || defined($ARGV[0])) {
  52.     &usage;
  53.     exit 1;
  54. }
  55.  
  56. if ($file eq "--help") {
  57.     &usage;
  58.     exit 0;
  59. }
  60.  
  61. if ($file eq "--version") {
  62.     warn "diffpp 1.0\n";
  63.     exit 0;
  64. }
  65.  
  66. open(FP, $file) || die "$program: couldn't open file `$file' for input: $!\n";
  67.  
  68. while (<>) {
  69.     if (/((\d+),)?(\d+)d(\d+)/) {
  70.     # Lines deleted.
  71.     if (defined($1)) {
  72.         $from = $2;
  73.     } else {
  74.         $from = $3;
  75.     }
  76.     &skip_to_line($4 - 1);
  77.     printf("\000ps{gsave -5 4 rmoveto %d {(-) show} repeat grestore}",
  78.            $3 - $from + 1);
  79.     } elsif (/(\d+)a(\d+)(,(\d+))?/) {
  80.     # Lines added.
  81.     if (defined($3)) {
  82.         $to = $4;
  83.     } else {
  84.         $to = $2;
  85.     }
  86.     &skip_to_line($2 - 1);
  87.     printf("\000shade{$gray}");
  88.     &mark_to_line($to, "+");
  89.     printf("\000shade{1.0}");
  90.     } elsif (/(\d+)c(\d+)(,(\d+))?/) {
  91.     # Lines changed.
  92.     if (defined($3)) {
  93.         $to = $4;
  94.     } else {
  95.         $to = $2;
  96.     }
  97.     &skip_to_line($2 - 1);
  98.     printf("\000shade{$gray}");
  99.     &mark_to_line($to, "!");
  100.     printf("\000shade{1.0}");
  101.     } else {
  102.     next;
  103.     }
  104. }
  105.  
  106. # Dump the tail.
  107. while (<FP>) {
  108.     print;
  109. }
  110. close(FP);
  111.  
  112. sub skip_to_line {
  113.     ($line) = @_;
  114.  
  115.     if ($line < 0) {
  116.     return;
  117.     }
  118.  
  119.     while (<FP>) {
  120.     print;
  121.     if ($. >= $line) {
  122.         last;
  123.     }
  124.     }
  125. }
  126.  
  127. sub mark_to_line {
  128.     ($line, $marker) = @_;
  129.  
  130.     if ($line < 0) {
  131.     return;
  132.     }
  133.  
  134.     while (<FP>) {
  135.     print "\000ps{gsave -5 0 rmoveto ($marker) show grestore}";
  136.     print;
  137.     if ($. >= $line) {
  138.         last;
  139.     }
  140.     }
  141. }
  142.