home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1442 / uumerge
Encoding:
Text File  |  1990-12-28  |  3.4 KB  |  139 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. if ($#ARGV < 0 ) {
  40.     print "Usage: uumerge filename [filename...]\n";
  41.     exit 1;
  42. }
  43.  
  44. # if we only have one file, pump it straight into uudecode and die
  45. if ( $#ARGV == 0 ) {
  46.     system("cat $ARGV[0] | uudecode");
  47.     exit 0;
  48. }
  49. $| = 1;
  50. # open a pipe into uudecode
  51. open(DECO,"|uudecode") || die "Can't pipe into uudecode\n";
  52.  
  53. # process the first file - make sure we have a "begin" line
  54.  
  55. open(FIRST,"<$ARGV[0]") || die "Can't open $ARGV[0] for input\n";
  56.  
  57. while ( <FIRST> ) {
  58.     # skip past everything before the "begin" line
  59.     next unless /^begin [0-9]/;
  60.     last;
  61. }
  62. die "First file on command line doesn't contain \"begin\"\n" if eof(FIRST);
  63.     
  64. print DECO $_; # the begin line
  65.  
  66. # the remaining "real" uuencoded lines in this file should begin with "M"
  67. while ( <FIRST> ) {
  68.     if ( /^M/ ) {
  69.         print DECO $_;
  70.     }
  71.     else {
  72.         last;
  73.     }
  74. }
  75.  
  76. # done with the first file
  77. close(FIRST);
  78.  
  79. # do all except the last file
  80. $maxindex = $#ARGV;
  81. $curr = 1;
  82.  
  83. while ( $curr < $maxindex ) {
  84.     open(CURR,"<$ARGV[$curr]") || die "Can't open $ARGV[$curr]\n";
  85.     # skip the header junk
  86.     while ( <CURR> ) {
  87.         next unless /^$/;
  88.         last;
  89.     }
  90.     # at the body of the message - start looking for /^M/
  91.     while ( <CURR> ) {
  92.         next unless /^M/;
  93.         last;
  94.     }
  95.     die "$ARGV[$curr] isn't a uuencoded file\n" if eof(CURR);
  96.     # OK, we're at the start of the good stuff (probably)
  97.     print DECO $_;
  98.     while ( <CURR> ) {
  99.         if (/^M/) {
  100.             print DECO $_;
  101.         }
  102.         else {
  103.             last;
  104.         }
  105.     }
  106.     # done with current file
  107.     close(CURR);
  108.     $curr++;
  109. }
  110.  
  111. # time to do the last file in the set
  112. $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. # OK, we're at the start of the good stuff (probably)
  125. print DECO $_;
  126. while ( <CURR> ) {
  127.     print DECO $_ unless /^end/;
  128.     if ( /^end/ ) {
  129.         print DECO $_;
  130.         last;
  131.     }
  132.     die "Last file on command line doesn't contain \"end\"\n" if eof(CURR);
  133. }
  134. # done with final file
  135. close(CURR);
  136. # close the pipe to uudecode and exit
  137. close(DECO);
  138. exit(0);
  139.