Skip to content

Commit

Permalink
2.0 (20080307)
Browse files Browse the repository at this point in the history
    - Put quotes around src glob to handle spaces
    - Limit explicit =#= patterns to a single-digit
    - Improved debug/verbose/testing messages
  • Loading branch information
raforg committed Mar 7, 2008
1 parent ac0b602 commit 49d69a9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ bindir := $(prefix)/bin
mandir := $(shell [ -d $(prefix)/share/man ] && echo $(prefix)/share/man || echo $(prefix)/man)

name := mved
version := 1.1
date := 20060124
version := 2.0
date := 20080307

install:
@set -e; \
Expand Down
40 changes: 25 additions & 15 deletions mved
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use strict;

# mved - carefully rename multiple files
#
# Copyright (C) 1997-2006 raf <[email protected]>
# Copyright (C) 1997-2008 raf <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -21,7 +21,7 @@ use strict;
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# or visit http://www.gnu.org/copyleft/gpl.html
#
# 20060124 raf <[email protected]>
# 20080307 raf <[email protected]>

=head1 NAME

Expand Down Expand Up @@ -54,9 +54,9 @@ globbing constructs are used, they must be quoted to prevent the shell from
performing filename expansion. This is not necessary when the only construct
used is C<=>. There are two styles of C<dst> argument. One allows C<=> to
represent the text matching the (positionally) corresponding glob construct
in the C<src> argument. The other allows C<=#=> (where C<#> is an integer)
to represent the text matching the C<#-th> glob construct in the C<src>
argument. The two styles cannot be mixed.
in the C<src> argument. The other allows C<=#=> (where C<#> is an integer
from 1 to 9) to represent the text matching the C<#-th> glob construct in
the C<src> argument. The two styles cannot be mixed.

I<mved> creates new links to the existing files. If the C<-v> verbose option
is supplied, the corresponding I<mv> commands are printed. If any of them
Expand Down Expand Up @@ -215,7 +215,7 @@ I<link(2)>, I<unlink(2)>, I<mv(1)>, I<rm(1)>

=head1 AUTHOR

20060124 raf <[email protected]>
20080307 raf <[email protected]>

=head1 URL

Expand Down Expand Up @@ -282,12 +282,12 @@ my $verbose = exists $opt{v} || exists $opt{d};
my $debug = exists $opt{d};
my $backwards = exists $opt{b} ? $opt{b} : 4;

print "$name: glob $src_glob $dst_glob\n" if $debug;
print "$name: src glob <$src_glob> dst glob <$dst_glob>\n" if $debug;

# Construct a glob and get the list of matching files

$src_glob =~ s/=/*/g;
my @src = glob $src_glob ;
my @src = glob '"' . $src_glob . '"';
die "$name: No such file: $src_glob\n" unless @src;

# Translate src into a regular expression search
Expand Down Expand Up @@ -321,19 +321,19 @@ $src_re =~ s/$/\$/;
# Translate dst into a regular expression replacement

my $dst_re = $dst_glob;
my $explicit_target = qr/$unsloshed=(\d+)=/;
my $implicit_target = qr/$unsloshed=(?!\d+=)/;
my $explicit_target = qr/$unsloshed=(\d)=/;
my $implicit_target = qr/$unsloshed=(?!\d=)/;

if ($dst_re =~ /$explicit_target/)
{
$dst_re =~ s/$explicit_target/\$$1/g;
$dst_re =~ s/$explicit_target/\$\{$1\}/g;
die "Cannot mix implicit (=) and explicit (=1=) targets\n" if $dst_re =~ /$implicit_target/;
}
else
{
for (my $i = 1; $dst_re =~ /$implicit_target/; ++$i)
{
$dst_re =~ s/$implicit_target/\$$i/;
$dst_re =~ s/$implicit_target/\$\{$i\}/;
}
}

Expand Down Expand Up @@ -396,8 +396,8 @@ if ($testing)
for (my $i = 0; $i < @src; ++$i)
{
my $r = (-d $dst[$i]) ? 'r' : '';
print "rm -${r}f ", $dst[$i], ' (if forced)', "\n" if -e $dst[$i];
print 'mv ', $src[$i], ' ', $dst[$i], "\n";
print "rm -${r}f ", qu($dst[$i]), ' (if forced)', "\n" if -e $dst[$i];
print 'mv ', qu($src[$i]), ' ', qu($dst[$i]), "\n";
}

exit;
Expand Down Expand Up @@ -427,7 +427,7 @@ if ($force)
my $i;
for ($i = 0; $i < @src; ++$i)
{
print "mv $src[$i] $dst[$i]\n" if $verbose;
print "mv ", qu($src[$i]), ' ', qu($dst[$i]), "\n" if $verbose;

if (-d $src[$i])
{
Expand Down Expand Up @@ -462,4 +462,14 @@ if ($i != @src)

unlink @src;

# Quote the argument if it contains spaces or double quotes

sub qu
{
my $s = shift;
return $s unless $s =~ /[ "]/;
$s =~ s/"/\\"/;
return "\"$s\"";
}

# vi:set ts=4 sw=4:

0 comments on commit 49d69a9

Please sign in to comment.