home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / PERL30X.ZIP / UUMERGE1.PL < prev    next >
Perl Script  |  1991-01-14  |  5KB  |  168 lines

  1. #! /usr/local/bin/perl
  2. #
  3. # Combine split uuencoded files into a single data stream with
  4. # e-mail garbage removed and pipe into uudecode. The uuencoded
  5. # files must be in the correct order on the command line - in
  6. # particular the first file must contain the "begin" line and
  7. # the last file must contain the "end" line.
  8. #
  9. # WARNING: this code relies on uuencode putting out all lines
  10. # of the form "M[61 ASCII characters]\n" for every line of the
  11. # file except the last few before the "end" line. If you come
  12. # across a uuencoded file that doesn't do this, you'll need to
  13. # modify the code to handle it.
  14. #
  15. # DISCLAIMER: You use this code at your own risk. Also, don't
  16. # take this is as a sterling example of Perl programming. Corrections
  17. # and improvements welcome. You may do whatever you like with this
  18. # code as long as you leave in some reminder of who the original
  19. # culprit^H^H^H^H^H^H^Hauthor was.
  20. #
  21. # Usage: uumerge filename [filename...]
  22. # Requires Perl 3.0 - my copy is at patchlevel 18
  23. #
  24. # Dave Mack csu@alembic.ACS.COM
  25. #
  26. # TODO: modify to allow more than one collection of files on
  27. #       command line.
  28. #
  29. # KNOWN BUGS: 
  30. #
  31. # If some bozo puts a line beginning with "M" in the body of one
  32. # of the intermediate/last chunks, uumerge will assume that uuencoded
  33. # part starts there.
  34. #
  35. # If the last chunk only contains the last two or three lines of
  36. # the uuencoded file (the ones that don't start with "M"), uumerge
  37. # will die.
  38. #
  39. # CHANGES
  40. #
  41. # PATCH 1:
  42. # It appears that some versions of uudecode are too stupid to skip
  43. # past the lines preceding the "begin" line, so feeding a one-part
  44. # uuencoded file to uumerge will bomb.
  45. #
  46. if ($#ARGV < 0 ) {
  47.         print "Usage: uumerge filename [filename...]\n";
  48.         exit 1;
  49. }
  50.  
  51. $| = 1;
  52. # open a pipe into uudecode
  53. open(DECO,"|uudecode") || die "Can't pipe into uudecode\n";
  54.  
  55. # if we only have one file, pump it straight into uudecode and die
  56. if ( $#ARGV == 0 ) {
  57.         open(FIRST,"<$ARGV[0]") || die "Can't open $ARGV[0] for input\n";
  58.  
  59.         while ( <FIRST> ) {
  60.                 # skip past everything before the "begin" line
  61.                 next unless /^begin [0-9]/;
  62.                 last;
  63.         }
  64.         die "$ARGV[0] doesn't contain \"begin\"\n" if eof(FIRST);
  65.         
  66.         print DECO $_; # the begin line
  67.  
  68.         while ( <FIRST> ) {
  69.                 print DECO $_ unless /^end/;
  70.                 if ( /^end/ ) {
  71.                         print DECO $_;
  72.                         last;
  73.                 }
  74.                 die "$ARGV[0] doesn't contain \"end\"\n" if eof(FIRST);
  75.         }
  76.  
  77.         # done with file
  78.         close(FIRST);
  79.         exit 0;
  80. }
  81.  
  82. # process the first file - make sure we have a "begin" line
  83.  
  84. open(FIRST,"<$ARGV[0]") || die "Can't open $ARGV[0] for input\n";
  85.  
  86. while ( <FIRST> ) {
  87.         # skip past everything before the "begin" line
  88.         next unless /^begin [0-9]/;
  89.         last;
  90. }
  91. die "First file on command line doesn't contain \"begin\"\n" if eof(FIRST);
  92.         
  93. print DECO $_; # the begin line
  94.  
  95. # the remaining "real" uuencoded lines in this file should begin with "M"
  96. while ( <FIRST> ) {
  97.         if ( /^M/ ) {
  98.                 print DECO $_;
  99.         }
  100.         else {
  101.                 last;
  102.         }
  103. }
  104.  
  105. # done with the first file
  106. close(FIRST);
  107.  
  108. # do all except the last file
  109. $maxindex = $#ARGV;
  110. $curr = 1;
  111.  
  112. while ( $curr < $maxindex ) {
  113.         open(CURR,"<$ARGV[$curr]") || die "Can't open $ARGV[$curr]\n";
  114.         # skip the header junk
  115.         while ( <CURR> ) {
  116.                 next unless /^$/;
  117.                 last;
  118.         }
  119.         # at the body of the message - start looking for /^M/
  120.         while ( <CURR> ) {
  121.                 next unless /^M/;
  122.                 last;
  123.         }
  124.         die "$ARGV[$curr] isn't a uuencoded file\n" if eof(CURR);
  125.         # OK, we're at the start of the good stuff (probably)
  126.         print DECO $_;
  127.         while ( <CURR> ) {
  128.                 if (/^M/) {
  129.                         print DECO $_;
  130.                 }
  131.                 else {
  132.                         last;
  133.                 }
  134.         }
  135.         # done with current file
  136.         close(CURR);
  137.         $curr++;
  138. }
  139.  
  140. # time to do the last file in the set
  141. $curr = $maxindex;
  142. open(CURR,"<$ARGV[$curr]") || die "Can't open $ARGV[$curr]\n";
  143. # skip the header junk
  144. while ( <CURR> ) {
  145.         next unless /^$/;
  146.         last;
  147. }
  148. # at the body of the message - start looking for /^M/
  149. while ( <CURR> ) {
  150.         next unless /^M/;
  151.         last;
  152. }
  153. # OK, we're at the start of the good stuff (probably)
  154. print DECO $_;
  155. while ( <CURR> ) {
  156.         print DECO $_ unless /^end/;
  157.         if ( /^end/ ) {
  158.                 print DECO $_;
  159.                 last;
  160.         }
  161.         die "Last file on command line doesn't contain \"end\"\n" if eof(CURR);
  162. }
  163. # done with final file
  164. close(CURR);
  165. # close the pipe to uudecode and exit
  166. close(DECO);
  167. exit(0);
  168.