line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::RegexFileUtils; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
274355
|
use strict; |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
195
|
|
4
|
6
|
|
|
6
|
|
35
|
use warnings; |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
377
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: use regexes with file utils like rm, cp, mv, ln |
7
|
|
|
|
|
|
|
our $VERSION = '0.06'; # VERSION |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
BEGIN { |
11
|
6
|
50
|
|
6
|
|
7760
|
if($^O eq 'MSWin32') |
12
|
|
|
|
|
|
|
{ |
13
|
0
|
|
|
|
|
0
|
require App::RegexFileUtils::MSWin32; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub main { |
18
|
5
|
|
|
5
|
1
|
17681
|
my $class = shift; |
19
|
5
|
|
|
|
|
14
|
my $mode = shift; |
20
|
5
|
|
|
|
|
11
|
my $appname = $mode; |
21
|
5
|
|
|
|
|
14
|
$mode =~ s!^.*/!!; |
22
|
5
|
|
|
|
|
14
|
my @args = @_; |
23
|
|
|
|
|
|
|
|
24
|
5
|
|
|
|
|
13
|
my @options = (); |
25
|
5
|
|
|
|
|
12
|
my $verbose = 0; |
26
|
5
|
|
|
|
|
8
|
my $do_hidden = 0; |
27
|
5
|
|
|
|
|
10
|
my $re; |
28
|
|
|
|
|
|
|
my $sub; |
29
|
0
|
|
|
|
|
0
|
my $modifiers; |
30
|
5
|
|
|
|
|
11
|
my $modifiers_match = ''; |
31
|
5
|
|
|
|
|
8
|
my $tr; |
32
|
5
|
|
|
|
|
11
|
my $static_dest = 0; |
33
|
|
|
|
|
|
|
|
34
|
5
|
|
66
|
|
|
60
|
while(defined($args[0]) && $args[0] =~ /^-/) { |
35
|
1
|
|
|
|
|
3
|
my $arg = shift @args; |
36
|
|
|
|
|
|
|
|
37
|
1
|
50
|
|
|
|
7
|
if($arg =~ /^--recmd$/) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
38
|
0
|
|
|
|
|
0
|
$mode = shift @args; |
39
|
0
|
|
|
|
|
0
|
$mode =~ s!^.*/!!; |
40
|
|
|
|
|
|
|
} elsif($arg =~ /^--reverbose$/) { |
41
|
0
|
|
|
|
|
0
|
$verbose = 1; |
42
|
|
|
|
|
|
|
} elsif($arg =~ /^--reall$/) { |
43
|
0
|
|
|
|
|
0
|
$do_hidden = 1; |
44
|
|
|
|
|
|
|
} else { |
45
|
1
|
|
|
|
|
7
|
push @options, $arg; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
5
|
|
|
|
|
14
|
my $dest = pop @args; |
50
|
|
|
|
|
|
|
|
51
|
5
|
50
|
|
|
|
19
|
unless(defined $dest) { |
52
|
0
|
|
|
|
|
0
|
print STDERR "usage: $appname [options] [source files] /pattern/[substitution/]\n"; |
53
|
0
|
|
|
|
|
0
|
print STDERR " $appname [options] /pattern/ /path/to/destination\n"; |
54
|
0
|
|
|
|
|
0
|
print STDERR "\n"; |
55
|
0
|
|
|
|
|
0
|
print STDERR "--recmd [command] change the behavior of the tool\n"; |
56
|
0
|
|
|
|
|
0
|
print STDERR "--verbose print commands before they are executed\n"; |
57
|
0
|
|
|
|
|
0
|
print STDERR "--reall include hidden (so called `dot') files\n"; |
58
|
0
|
|
|
|
|
0
|
print STDERR "\n"; |
59
|
0
|
|
|
|
|
0
|
print STDERR "all other arguments are passed to the system tool\n"; |
60
|
0
|
|
|
|
|
0
|
exit; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
5
|
|
|
|
|
10
|
my $orig_mode = $mode; |
64
|
5
|
|
|
|
|
11
|
$mode =~ s/^re//; |
65
|
|
|
|
|
|
|
|
66
|
5
|
|
|
|
|
64
|
my %modes = ( |
67
|
|
|
|
|
|
|
'mv' => 'mv', |
68
|
|
|
|
|
|
|
'move' => 'mv', |
69
|
|
|
|
|
|
|
'rename' => 'mv', |
70
|
|
|
|
|
|
|
'cp' => 'cp', |
71
|
|
|
|
|
|
|
'copy' => 'cp', |
72
|
|
|
|
|
|
|
'ln' => 'ln', |
73
|
|
|
|
|
|
|
'link' => 'ln', |
74
|
|
|
|
|
|
|
'symlink' => 'ln', |
75
|
|
|
|
|
|
|
'rm' => 'rm', |
76
|
|
|
|
|
|
|
'remove' => 'rm', |
77
|
|
|
|
|
|
|
'unlink' => 'rm', |
78
|
|
|
|
|
|
|
'touch' => 'touch', |
79
|
|
|
|
|
|
|
); |
80
|
|
|
|
|
|
|
|
81
|
5
|
50
|
|
|
|
19
|
unshift @options, '-s' if $mode eq 'symlink'; |
82
|
|
|
|
|
|
|
|
83
|
5
|
|
|
|
|
10
|
$mode = $modes{$mode}; |
84
|
5
|
50
|
|
|
|
26
|
unless(defined $mode) { |
85
|
0
|
|
|
|
|
0
|
print STDERR "unknown mode $orig_mode\n"; |
86
|
0
|
|
|
|
|
0
|
exit; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
5
|
|
|
|
|
9
|
my $no_dest = 0; |
90
|
5
|
100
|
100
|
|
|
41
|
if($mode eq 'touch' || $mode eq 'rm') { |
91
|
2
|
|
|
|
|
3
|
$no_dest = 1; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
5
|
100
|
|
|
|
82
|
if($dest =~ m!^(s|)/(.*)/(.*)/([ig]*)$!) { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
95
|
2
|
|
|
|
|
7
|
$re = $2; |
96
|
2
|
|
|
|
|
5
|
$sub = $3; |
97
|
2
|
|
|
|
|
7
|
$modifiers = $4; |
98
|
2
|
100
|
|
|
|
24
|
$modifiers_match = 'i' if $modifiers =~ /i/; |
99
|
|
|
|
|
|
|
|
100
|
2
|
50
|
|
|
|
8
|
if($no_dest) { |
101
|
0
|
|
|
|
|
0
|
print STDERR "substitution `$mode' doesn't make sense\n"; |
102
|
0
|
|
|
|
|
0
|
exit; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
elsif($dest =~ m!tr/(.*)/(.*)/$!) { |
108
|
0
|
|
|
|
|
0
|
$tr = $1; |
109
|
0
|
|
|
|
|
0
|
$sub = $2; |
110
|
|
|
|
|
|
|
|
111
|
0
|
0
|
|
|
|
0
|
if($no_dest) { |
112
|
0
|
|
|
|
|
0
|
print STDERR "translation `$mode' doesn't make sense\n"; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
elsif($dest =~ m!^(m|)/(.*)/([i]*)$!) { |
117
|
2
|
|
|
|
|
6
|
$re = $2; |
118
|
2
|
|
|
|
|
4
|
$modifiers = $3; |
119
|
2
|
|
|
|
|
5
|
$modifiers_match = $3; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
elsif(-d $dest) { |
123
|
1
|
|
|
|
|
2
|
my $src = pop @args; |
124
|
1
|
50
|
|
|
|
7
|
if($src =~ m!^(m|)/(.*)/([i]*)$!) { |
125
|
1
|
|
|
|
|
2
|
$static_dest = 1; |
126
|
1
|
|
|
|
|
4
|
$re = $2; |
127
|
1
|
|
|
|
|
3
|
$modifiers = $3; |
128
|
1
|
|
|
|
|
3
|
$modifiers_match = $3; |
129
|
|
|
|
|
|
|
} else { |
130
|
0
|
|
|
|
|
0
|
die "source is not a regex"; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
else { |
135
|
0
|
|
|
|
|
0
|
die "destination is not a directory or a regex"; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
5
|
|
|
|
|
11
|
my @files = @args; |
139
|
|
|
|
|
|
|
|
140
|
5
|
50
|
|
|
|
17
|
if(@files ==0) { |
141
|
5
|
50
|
|
|
|
182
|
opendir(DIR, '.') || die "unable to opendir `.' $!"; |
142
|
5
|
|
|
|
|
194
|
@files = readdir(DIR); |
143
|
5
|
|
|
|
|
70
|
closedir DIR; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
5
|
|
|
|
|
14
|
for(@files) { |
147
|
35
|
100
|
66
|
|
|
916
|
next if /^\./ && !$do_hidden; |
148
|
25
|
100
|
66
|
|
|
3434
|
next unless eval "/$re/$modifiers_match" || defined $tr; |
149
|
14
|
|
|
|
|
51
|
my $old = $_; |
150
|
14
|
|
|
|
|
38
|
my $new = $old; |
151
|
|
|
|
|
|
|
|
152
|
14
|
|
|
|
|
61
|
my @cmd = ($mode, @options, $old); |
153
|
|
|
|
|
|
|
|
154
|
14
|
50
|
|
|
|
68
|
if(defined $tr) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
155
|
0
|
|
|
|
|
0
|
eval "\$new =~ tr/$tr/$sub/"; |
156
|
|
|
|
|
|
|
} elsif(defined $sub) { |
157
|
7
|
|
|
|
|
559
|
eval "\$new =~ s/$re/$sub/$modifiers"; |
158
|
|
|
|
|
|
|
} elsif($static_dest) { |
159
|
2
|
|
|
|
|
4
|
$new = $dest; |
160
|
|
|
|
|
|
|
} else { |
161
|
5
|
50
|
|
|
|
14
|
if($no_dest) { |
162
|
5
|
|
|
|
|
11
|
$new = ''; |
163
|
|
|
|
|
|
|
} else { |
164
|
0
|
|
|
|
|
0
|
$new = '.'; |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
14
|
100
|
|
|
|
54
|
push @cmd, $new unless $no_dest; |
169
|
14
|
50
|
|
|
|
40
|
print "% @cmd\n" if $verbose; |
170
|
|
|
|
|
|
|
|
171
|
14
|
50
|
|
|
|
316
|
__PACKAGE__->fix_path(\@cmd) if $^O eq 'MSWin32'; |
172
|
|
|
|
|
|
|
|
173
|
14
|
|
|
|
|
78762
|
system @cmd; |
174
|
|
|
|
|
|
|
|
175
|
14
|
50
|
|
|
|
12593
|
if ($? == -1) { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
176
|
0
|
|
|
|
|
0
|
print STDERR "failed to execute: $!\n"; |
177
|
0
|
|
|
|
|
0
|
exit 2; |
178
|
|
|
|
|
|
|
} elsif ($? & 127) { |
179
|
0
|
|
|
|
|
0
|
print STDERR "child died with signal ", $? & 127, "\n"; |
180
|
|
|
|
|
|
|
} elsif($? >> 8) { |
181
|
1
|
|
|
|
|
125
|
print "child exited with value ", $? >> 8, "\n"; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
1; |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
__END__ |