| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::RegexFileUtils; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 6 |  |  | 6 |  | 150370 | use strict; | 
|  | 6 |  |  |  |  | 8 |  | 
|  | 6 |  |  |  |  | 136 |  | 
| 4 | 6 |  |  | 6 |  | 19 | use warnings; | 
|  | 6 |  |  |  |  | 5 |  | 
|  | 6 |  |  |  |  | 277 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | # ABSTRACT: use regexes with file utils like rm, cp, mv, ln | 
| 7 |  |  |  |  |  |  | our $VERSION = '0.07'; # VERSION | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | BEGIN { | 
| 11 | 6 | 50 |  | 6 |  | 4608 | if($^O eq 'MSWin32') | 
| 12 |  |  |  |  |  |  | { | 
| 13 | 0 |  |  |  |  | 0 | require App::RegexFileUtils::MSWin32; | 
| 14 |  |  |  |  |  |  | } | 
| 15 |  |  |  |  |  |  | } | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | sub main { | 
| 18 | 5 |  |  | 5 | 1 | 11327 | my $class = shift; | 
| 19 | 5 |  |  |  |  | 9 | my $mode = shift; | 
| 20 | 5 |  |  |  |  | 7 | my $appname = $mode; | 
| 21 | 5 |  |  |  |  | 11 | $mode =~ s!^.*/!!; | 
| 22 | 5 |  |  |  |  | 11 | my @args = @_; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 5 |  |  |  |  | 8 | my @options = (); | 
| 25 | 5 |  |  |  |  | 7 | my $verbose = 0; | 
| 26 | 5 |  |  |  |  | 6 | my $do_hidden = 0; | 
| 27 | 5 |  |  |  |  | 6 | my $re; | 
| 28 |  |  |  |  |  |  | my $sub; | 
| 29 | 0 |  |  |  |  | 0 | my $modifiers; | 
| 30 | 5 |  |  |  |  | 9 | my $modifiers_match = ''; | 
| 31 | 5 |  |  |  |  | 7 | my $tr; | 
| 32 | 5 |  |  |  |  | 6 | my $static_dest = 0; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 5 |  | 66 |  |  | 49 | while(defined($args[0]) && $args[0] =~ /^-/) { | 
| 35 | 1 |  |  |  |  | 2 | my $arg = shift @args; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 1 | 50 |  |  |  | 6 | 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 |  |  |  |  | 6 | push @options, $arg; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 5 |  |  |  |  | 10 | my $dest = pop @args; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 5 | 50 |  |  |  | 16 | 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 |  |  |  |  | 8 | my $orig_mode = $mode; | 
| 64 | 5 |  |  |  |  | 9 | $mode =~ s/^re//; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 5 |  |  |  |  | 71 | 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 |  |  |  | 16 | unshift @options, '-s' if $mode eq 'symlink'; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 5 |  |  |  |  | 10 | $mode = $modes{$mode}; | 
| 84 | 5 | 50 |  |  |  | 14 | unless(defined $mode) { | 
| 85 | 0 |  |  |  |  | 0 | print STDERR "unknown mode $orig_mode\n"; | 
| 86 | 0 |  |  |  |  | 0 | exit; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 5 |  |  |  |  | 7 | my $no_dest = 0; | 
| 90 | 5 | 100 | 100 |  |  | 36 | if($mode eq 'touch' || $mode eq 'rm') { | 
| 91 | 2 |  |  |  |  | 4 | $no_dest = 1; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 5 | 100 |  |  |  | 53 | if($dest =~ m!^(s|)/(.*)/(.*)/([ig]*)$!) { | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 95 | 2 |  |  |  |  | 5 | $re = $2; | 
| 96 | 2 |  |  |  |  | 4 | $sub = $3; | 
| 97 | 2 |  |  |  |  | 19 | $modifiers = $4; | 
| 98 | 2 | 100 |  |  |  | 10 | $modifiers_match = 'i' if $modifiers =~ /i/; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 2 | 50 |  |  |  | 6 | 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 |  |  |  |  | 4 | $modifiers_match = $3; | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | elsif(-d $dest) { | 
| 123 | 1 |  |  |  |  | 9 | my $src = pop @args; | 
| 124 | 1 | 50 |  |  |  | 5 | if($src =~ m!^(m|)/(.*)/([i]*)$!) { | 
| 125 | 1 |  |  |  |  | 2 | $static_dest = 1; | 
| 126 | 1 |  |  |  |  | 2 | $re = $2; | 
| 127 | 1 |  |  |  |  | 2 | $modifiers = $3; | 
| 128 | 1 |  |  |  |  | 2 | $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 |  |  |  |  | 9 | my @files = @args; | 
| 139 |  |  |  |  |  |  |  | 
| 140 | 5 | 50 |  |  |  | 15 | if(@files ==0) { | 
| 141 | 5 | 50 |  |  |  | 131 | opendir(DIR, '.') || die "unable to opendir `.' $!"; | 
| 142 | 5 |  |  |  |  | 166 | @files = readdir(DIR); | 
| 143 | 5 |  |  |  |  | 57 | closedir DIR; | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 5 |  |  |  |  | 14 | for(@files) { | 
| 147 | 35 | 100 | 66 |  |  | 328 | next if /^\./ && !$do_hidden; | 
| 148 | 25 | 100 | 66 |  |  | 1871 | next unless eval "/$re/$modifiers_match" || defined $tr; | 
| 149 | 14 |  |  |  |  | 37 | my $old = $_; | 
| 150 | 14 |  |  |  |  | 17 | my $new = $old; | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 14 |  |  |  |  | 29 | my @cmd = ($mode, @options, $old); | 
| 153 |  |  |  |  |  |  |  | 
| 154 | 14 | 50 |  |  |  | 43 | if(defined $tr) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 100 |  |  |  |  |  | 
| 155 | 0 |  |  |  |  | 0 | eval "\$new =~ tr/$tr/$sub/"; | 
| 156 |  |  |  |  |  |  | } elsif(defined $sub) { | 
| 157 | 7 |  |  |  |  | 366 | eval "\$new =~ s/$re/$sub/$modifiers"; | 
| 158 |  |  |  |  |  |  | } elsif($static_dest) { | 
| 159 | 2 |  |  |  |  | 2 | $new = $dest; | 
| 160 |  |  |  |  |  |  | } else { | 
| 161 | 5 | 50 |  |  |  | 9 | if($no_dest) { | 
| 162 | 5 |  |  |  |  | 9 | $new = ''; | 
| 163 |  |  |  |  |  |  | } else { | 
| 164 | 0 |  |  |  |  | 0 | $new = '.'; | 
| 165 |  |  |  |  |  |  | } | 
| 166 |  |  |  |  |  |  | } | 
| 167 |  |  |  |  |  |  |  | 
| 168 | 14 | 100 |  |  |  | 43 | push @cmd, $new unless $no_dest; | 
| 169 | 14 | 50 |  |  |  | 24 | print "% @cmd\n" if $verbose; | 
| 170 |  |  |  |  |  |  |  | 
| 171 | 14 | 50 |  |  |  | 71 | __PACKAGE__->fix_path(\@cmd) if $^O eq 'MSWin32'; | 
| 172 |  |  |  |  |  |  |  | 
| 173 | 14 |  |  |  |  | 20920 | system @cmd; | 
| 174 |  |  |  |  |  |  |  | 
| 175 | 14 | 50 |  |  |  | 3968 | 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 |  |  |  |  | 42 | print "child exited with value ", $? >> 8, "\n"; | 
| 182 |  |  |  |  |  |  | } | 
| 183 |  |  |  |  |  |  | } | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | 1; | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | __END__ |