- add use strict + use warnings

- kill lots of global vars
- get rid of & in &sub() calls which makes prototypes not work
This commit is contained in:
Ward Wouts 2007-11-04 19:34:54 +00:00
parent 894f0304d8
commit 1226e31323

View file

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