[alt.sources] uumerge.pl

urlichs@smurf.sub.org (Matthias Urlichs) (07/09/90)

In alt.sources, article <1990Jul7.182135.19069@alembic.acs.com>,
  csu@alembic.acs.com (Dave Mack) writes:
< #
< # TODO: modify to allow more than one collection of files on
< #	command line.
< #

Well, here's my version; after merging and uudecoding N files per hand I
decided that enough was enough. (You might guess what newsgroup I'm talking
about... :-)

It also isn't perturbed if the occasional line in there does start with M.

Warning: My programming style is atrocious, and my Perl style doubly so.
Especially, the $x? variables really should be an array, the number of M lines
required should be an option, and the M itself also.


#!/usr/local/bin/perl
$files = 0;
$isopen = 0;
$file = "";
$lines = 0;

$ARGV[$[] = "-" unless ($#ARGV >= $[);

sub nextline {
   local($next) = (0);
   if ($file eq "") { $next = 1; }
   else {
      $_ = <FILE>;
      if ($_ eq "") { $next = 1;  }
   }
   if ($next) {
      $file = $ARGV[$[];
      if ($#ARGV < $[) {
	 $_ = "";
      } elsif ($#ARGV >= $[) {
	 shift @ARGV;
	 print STDERR "Reading $file\n";
	 if ($file eq "-") {
	    open (FILE,"<&STDIN");
	 } else {
	    open (FILE,$file);
	 }
	 $_ = &nextline;
      } else {
	 $_ = "";
      }
   }
   $_;
}

loop: while(($_ = &nextline) ne "") {
    scan: while (1) {
       if (/^begin\s\d\d\d\s(.*)\s*$/) {
	  print STDERR "Writing $1\n";
	  open (OUT,"|uudecode");
	  print OUT $_;
	  $files++;
	  $isopen = 1;
	  last scan;
       }
    } continue {
       &nextline;
       last loop if ($_ eq "");
    }
    
    $isuu = 3;
    lines: while (($_ = &nextline) ne "") {
       last if /^end/;
       if (/^M/ && $isuu > 2) {
          print OUT $x3, $x2, $x1, $_;
          $x3 = ""; $x2 = ""; $x1 = "";
	  $lines++;
          next lines;
       } else {
          $x3 = $x2; $x2 = $x1; $x1 = $_;
	  if (/^M/) {
	    $isuu++;
	  } else {
	    $isuu = 0;
	  }
       }
    }  

    print STDERR "$lines lines.\n";
    if (/^end/ && $isopen) {
        print OUT $x3, $x2, $x1, $_;
        close(OUT);
	$isopen = 0;
    } elsif ($isopen) {
	close(OUT);
	die "End of file not found";
    } else {
	die "No files found";
    }
    $lines = 0;
}

print STDERR "$files files found.\n";
exit 0;
-- 
Matthias Urlichs -- urlichs@smurf.sub.org -- urlichs@smurf.ira.uka.de
Humboldtstrasse 7 - 7500 Karlsruhe 1 - FRG -- +49+721+621127(Voice)/621227(PEP)

rommel@lan.informatik.tu-muenchen.dbp.de (Kai-Uwe Rommel) (07/09/90)

And this is my version for awk. It does not require all parts to be in
separate files, the parts may be concatenated to one single file.

Kai Uwe Rommel
Munich
rommel@lan.informatik.tu-muenchen.dbp.de

------------------------

#!/bin/awk -f

BEGIN {
  body = 0;
}

/^[ ]*$/ || /[a-z]/ {
  body = 0;
}

/^begin/ {
  body = 1;
}

/^M/ && !/[a-z]/ && length($0)>60 {
  body = 1;
}

/^end/ {
  print $0 | "uudecode -";
  body = 0;
}

{
  if ( body )
    print $0 | "uudecode -";
}

-----------------

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (07/10/90)

In article <3317@tuminfo1.lan.informatik.tu-muenchen.dbp.de> rommel@lan.informatik.tu-muenchen.dbp.de (Kai-Uwe Rommel) writes:
: And this is my version for awk. It does not require all parts to be in
: separate files, the parts may be concatenated to one single file.

[Pretty little awk script deleted.]

A few little problems with the awk script:  1) I don't have nawk, and 2) it
won't handle a "-- " right after an "M" line correctly.

Also, our uudecode doesn't know what to do with file "-".

Here's my little contest entry.  It'll also handle multiple uudecodes from
a single file, as well as single uudecodes over multiple files.

#!/usr/bin/perl

while (<>) {
    if ($seq = /^begin/ .. /^end/) {
	$in = open(UU, "| uudecode"), next if $seq == 1;
	$in = 1 if /^M/ && length($_) / 10 == 6;
	$in = 0 if /^(--)?\s*$/ || /[a-z]/;
	$in = 1 if /^end/;
    }
}
continue {
    print UU if $seq && $in;
}


Or perhaps something more like this:

#!/usr/bin/perl
while (<>) {
    $inrange = /^begin/ .. /^end/;
    $valid = 1				if /^M/ && length($_) / 10 == 6;
    $valid = 0				if /^(--)?\s*$/ || /[a-z]/;
    $valid = open(UU, "| uudecode")	if $inrange == 1;
    $valid = 1				if /^end/;
    print UU if $inrange && $valid;
}

"There's more than one way to do it."

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov

jik@athena.mit.edu (Jonathan I. Kamens) (07/14/90)

  In the referenced article, Larry Wall presents to perl versions of the
uumerge script for taking uuencoded files which are split across
separate files and merging and uudecoding them.

  The problem is, Larry's script doesn't work on a uuencoded file I just
got over the net, while the uumerge script posted by Dave Mack
(csu@alembic.ACS.COM) does.

  In particular, Larry's script doesn't take deal with all possible
endings for an archive, i.e. what the one or two lines before the "end"
look like.

  I'll leave it to him to fix it.  I can provide a uuencoded file that
breaks with his script, upon request :-).

Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8495			      Home: 617-782-0710