From 053bdc1b09b2bec18065270dd62bfd0ae1afd38b Mon Sep 17 00:00:00 2001 From: Ward Wouts Date: Tue, 31 Jul 2001 22:16:52 +0000 Subject: [PATCH] added options: -h for help -e "editor" for anternative editor -s "streameditor" for stream editors also added checking for empty lines --- mvwrap/mvwrap | 93 ++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 80 insertions(+), 13 deletions(-) diff --git a/mvwrap/mvwrap b/mvwrap/mvwrap index 2858ea1..68eb07f 100755 --- a/mvwrap/mvwrap +++ b/mvwrap/mvwrap @@ -12,6 +12,9 @@ use IO::File; use File::Copy; use POSIX qw(tmpnam); +use Getopt::Std; + +&cmdline; #Read current dir opendir(DIRHANDLE, ".") or die "couldn't open .: $!"; @@ -21,26 +24,25 @@ while ( defined ($filename = readdir(DIRHANDLE)) ) { closedir(DIRHANDLE); @source = sort @dir; +&lock; + # make tempfiles and install handler to remove them do { $target_name = tmpnam() } until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL); -END { unlink($target_name) or die "Couldn't unlink $target_name: $!" } +END { unlink($target_name) or die "Couldn't unlink $target_name: $!"; + unlink(".mv_wrap") or die "Couldn't unlink .mv_wrap: $!"; } foreach (@source) { print $target $_; } close ($target); -if ( -e ".mv_wrap") { - die "Another mv_wrap process is active in this direcory\n" -} -else { - open LOCK, ">.mv_wrap"; - print LOCK $$; - close LOCK; -} -&edit($target_name); +if ($option{s}) { + &stream_edit($target_name); +} else { + &edit($target_name); +} open $target, $target_name or die "Couldn't open tempfile: $target_name: $!"; @target = <$target>; @@ -50,6 +52,11 @@ unless ( scalar(@source) == scalar(@target) ) { die "Aborting. Source and target list don't have the same number of lines.\n"; } +# ook nog checken op lege regels hier.... +foreach $line (@target) { + if ( $line =~ m/^$/ ) { die "Aborting. You can't move to empy names.\n"; } +} + if ( &check_unique(@target) ) { die "Aborting. You're trying to move multiple files to the same name.\n"; } @@ -63,16 +70,38 @@ if (@unsafe = &check_safety(\@source, \@target)) { # final move &move_files(\@source, \@target); -unlink ".mv_wrap" or die "Couldn't remove lock"; - # call EDITOR or vi with filename # edit($filename); sub edit { - @editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", shift); + my $filename = shift; + @editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", "$filename"); + if ($option{e}) { @editor = ($option{e}, "$filename") } system(@editor) == 0 or die "System @editor failed: $?"; } +sub stream_edit { + my $filename = shift; + my ($pid, @output); + @editor = (split(" ", $option{s}), "$filename"); +# foreach (@editor) { print "$_\n"; } + die "cannot fork: $!" unless defined ($pid = open(KID, "-|")); + if ($pid) { #parent + while () { +# print ; + push @output, $_; + } + close(KID); + } else { #child + exec(@editor) or die "can't exec program: $!\n"; + } + open OUT, ">$filename" or die "Can't open $filename for writeing: $!\n"; + foreach $line (@output) { + print OUT "$line"; + } + close OUT; +} + # moves files from the names in one array to the names in another array # must be called like: # move_files(\@source, \@target) @@ -142,3 +171,41 @@ sub safety_belt { $source->[$filenr] = "$rand\n"; } } + +sub lock { + if ( -e ".mv_wrap") { + die "Another mv_wrap process is active in this direcory\n" + } + else { + open LOCK, ">.mv_wrap"; + print LOCK $$; + close LOCK; + } +} + +sub cmdline { + %option = (); + getopts("e:hs:", \%option); + + &help if $option{h}; +} + +sub help { + print << "EOT"; + +This is a little script to make renaming large quantities of files easy. +It basically lets you edit the names in a directory with an editor +of your choice. By default this will be "vi", but it honors the EDITOR +environment variable. Which in turn can be overridden with the -e option. + +At the moment it takes the following options: +-h this help message +-e "editor" invoke with this editor +-s "streameditor" use a stream editor + this makes things like: + mv_wrap.pl -s "sed -e s/1/5/" + possible + +EOT +exit; +}