#!/usr/bin/perl -w # Thanks to folks on comp.lang.perl.misc for a few tips. use strict; use Convert::UU qw/uudecode/; die "Filename required\n" unless $ARGV[0]; my $file = shift; print "Parsing $file ...\n\n"; open (FILE, "<", $file) or die "Can't open file for reading: $!\n"; my ($data, $tempfile, $tempmode); MAIN: while () { next MAIN unless /^begin / .. /^end\s+/; INNER: { # extract the filename. If we already have a filename, it's a possible short file in the data file, so reset everything. Could all of this be done better somehow? if ( /^begin / ) { if ($tempfile) { print "Warning: possible short file $tempfile found\n\n"; $data = ''; } # if ($tempfile) ($tempmode, $tempfile) = ( /\Abegin (\d+) (.*)$/m); last INNER; } # if ( /^begin/ ) last INNER if /^end\s+/; # Last line of data. next MAIN if /[a-z]/; # Skip non-data lines next MAIN if /^\s+$/; # And again } #end of INNER $data .= $_; if ( /^end\s+/ ) { my $filename = $tempfile; my $mode = $tempmode; print "Found uuencoded file $filename\n"; my $outstr = uudecode($data); if (-e $filename) { print "Error, $filename already exists. Ignoring\n\n"; $data = ''; $tempfile = ''; $filename = ''; next MAIN; } open (OUTFILE, ">", $filename) or die "Can't open $filename for writing: $!\n"; print "Writing to $filename ...\n\n"; print OUTFILE $outstr; close OUTFILE; system ("chmod", $mode, $filename) and warn "Unable to change perms of $filename: $!\n"; $data = ''; $tempfile = ''; $filename = ''; } } close (FILE)