diff --git a/mvwrap/mvwrap b/mvwrap/mvwrap index c685abc..2973562 100755 --- a/mvwrap/mvwrap +++ b/mvwrap/mvwrap @@ -3,7 +3,7 @@ # $Id$ # -# Copyright (c) 2001 - 2005 Ward Wouts +# Copyright (c) 2001 - 2007 Ward Wouts # # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -19,7 +19,7 @@ # # -# Script to let an editor loose on a directory listing +# Script to use an editor on a directory listing # Much easier than doing 6000 moves by hand # @@ -27,45 +27,22 @@ use IO::File; use File::Copy; use POSIX qw(tmpnam); use Getopt::Long; - -&lock; - -$SIG{__DIE__} = 'cleanup'; - -&cmdline; -unless (defined @source) { @source = &read_cur_dir; } - -if (@pattern) { - @target = @source; - &pattern_edit; - if ( &run_checks ) { - die("Aborting\n"); - } -} elsif ($opt_l) { - @target=(&readlist($opt_l)); - if (&run_checks) { die "Aborting\n"; } -} else { - $temp_file = &open_temp; - &edit($temp_file); - &read_temp($temp_file); - while ( &run_checks ) { - if ( &askyn("Do you want to continue editing") ) { - &edit($temp_file); - &read_temp($temp_file); - } else { - die("Aborting\n"); - } - } -} - -# if it's unsafe to move files directly, move the unsafe ones to a -# temporary file first -if (@unsafe = &check_safety(\@source, \@target)) { - &safety_belt(\@unsafe, \@source); -} - -&move_files(\@source, \@target); -&cleanup; +use strict; +use warnings; +use vars qw( + @source + @target + @pattern + @unsafe + $opt_e + $opt_l + $opt_f + $opt_n + $opt_v + $opt_h + $paranoia + $temp_file +); ######################################################## # sub routines from here on... @@ -89,6 +66,7 @@ sub read_cur_dir { # edit($filename); sub edit($) { my $filename = shift; + my @editor; @editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", "$filename"); if ($opt_e) { @editor = ($opt_e, "$filename") } system(@editor) == 0 @@ -98,6 +76,7 @@ sub edit($) { # make tempfiles and install handler to remove them sub open_temp { my $target; + my $target_name; do { $target_name = tmpnam() } until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL); END { if ($opt_e) { unlink($target_name) or die("Couldn't unlink $target_name: $!");} } @@ -185,14 +164,14 @@ sub run_checks { } } - if ( &check_unique(@target) ) { - &which_doubles(@source, @target); + if ( check_unique(@target) ) { + which_doubles(@source, @target); print "ERROR: You're trying to move multiple files to the same name.\n"; return 1; } if ($paranoia) { # extra checks for a specified list of files - if ( ¶noia(\@source, \@target) ) { return 1; } + if ( paranoia(\@source, \@target) ) { return 1; } } return 0; } @@ -209,6 +188,7 @@ sub check_unique(@) { # show what the non-unique moves are sub which_doubles { my %selector = (); + my $i; foreach $i (0 .. $#target) { if ( exists $selector{$target[$i]} ) { push @{$selector{$target[$i]}}, $i; @@ -276,7 +256,7 @@ sub safety_belt($$) { my($filenr, $filename, $rand); foreach $filenr (@$unsafe) { $filename = $source->[$filenr]; - $rand = &rand_file; + $rand = rand_file; if ($opt_v||$opt_n) { print "mv \"$filename\" \"$rand\"\n"; } @@ -293,6 +273,7 @@ sub safety_belt($$) { # paranoia(\@unsafe, \@source) sub paranoia($$) { my ($source, $target) = @_; + my ($ctarget, $csource); my $safe; foreach $ctarget (@$target) { $safe = 0; @@ -311,7 +292,7 @@ sub paranoia($$) { } # place lockfile in dir, or exit if one exists. -sub lock { +sub mvwlock { if ( -e ".mvwrap") { die "Another mvwrap process is active in this direcory\n" } @@ -323,7 +304,7 @@ sub lock { } # clean up lockfile -sub unlock { +sub mvwunlock { if ( -e ".mvwrap" ) { unlink(".mvwrap") or die "Couldn't remove lock file: $!\n"; } @@ -331,7 +312,7 @@ sub unlock { # clean up sub cleanup { - &unlock; + mvwunlock; if ( defined($temp_file) ) { unlink $temp_file or die "Couldn't remove temp file: $!\n"; } @@ -339,6 +320,7 @@ sub cleanup { sub askyn($) { my $question = shift; + my $line; while (1) { print "$question [y/n]? "; $line = <>; @@ -352,10 +334,10 @@ sub askyn($) { # parse commandline sub cmdline { - &GetOptions("e=s", "h", "p=s", \@pattern, "f=s", "v", "n", "l=s"); + GetOptions("e=s", "h", "p=s", \@pattern, "f=s", "v", "n", "l=s"); - &help if $opt_h; - if ($opt_f) { &read_pattern; } + help() if $opt_h; + if ($opt_f) { read_pattern(); } if (scalar(@ARGV)>0) { foreach (@ARGV) { push @source, "$_"; @@ -387,6 +369,49 @@ At the moment it takes the following options: -n test; doesn't move, implies -v EOT -&unlock; -exit; + mvwunlock; + exit; } + +######################################################## +# main loop +# + +mvwlock(); + +$SIG{__DIE__} = 'cleanup'; + +cmdline; +unless (defined @source) { @source = read_cur_dir; } + +if (@pattern) { + @target = @source; + pattern_edit; + if ( run_checks ) { + die("Aborting\n"); + } +} elsif ($opt_l) { + @target=(readlist($opt_l)); + if (run_checks) { die "Aborting\n"; } +} else { + $temp_file = open_temp; + edit($temp_file); + read_temp($temp_file); + while ( run_checks() ) { + if ( askyn("Do you want to continue editing") ) { + edit($temp_file); + read_temp($temp_file); + } else { + die("Aborting\n"); + } + } +} + +# if it's unsafe to move files directly, move the unsafe ones to a +# temporary file first +if (@unsafe = check_safety(\@source, \@target)) { + safety_belt(\@unsafe, \@source); +} + +move_files(\@source, \@target); +cleanup;