29efe1b9 |
1 | #!/usr/bin/env perl |
2 | |
3 | # Perform multiple moves/copies. |
4 | |
5 | # Usage: multi mv 's/$/.old/' *.c |
6 | # multi cp 's:/tmp/(.*):$1:' * |
7 | # multi - svn mv - 's/foo/bar/' *foo* |
8 | # |
9 | # Options: -n don't actually do anything, just print what it would do |
10 | # -r reverse the arguments to each command |
11 | # -q don't print the commands as they are executed |
12 | |
13 | # Note that all variables in this script begin with a double |
14 | # underscore. This is because the <action> parameter is a piece of |
15 | # user-supplied Perl, which might perfectly legitimately want to |
16 | # define and use its own variables in the course of doing a complex |
17 | # transformation on file names. Thus, I arrange that all _my_ |
18 | # variables stay as far out of its likely namespace as they can. |
19 | |
da0f8522 |
20 | $usage = |
21 | "usage: multi <cmd> <action> <files>\n" . |
22 | " e.g. multi mv 'tr/A-Z/a-z/' *\n" . |
23 | " or: multi - <multiple-word-cmd> - <action> <files>\n" . |
24 | " e.g. multi - svn mv - 'tr/A-Z/a-z/' *\n" . |
25 | " also: multi --version report version number\n" . |
26 | " multi --help display this help text\n" . |
27 | " multi --licence display (MIT) licence text\n"; |
28 | |
29 | $licence = |
30 | "multi is copyright 1999-2004 Simon Tatham.\n" . |
31 | "\n" . |
32 | "Permission is hereby granted, free of charge, to any person\n" . |
33 | "obtaining a copy of this software and associated documentation files\n" . |
34 | "(the \"Software\"), to deal in the Software without restriction,\n" . |
35 | "including without limitation the rights to use, copy, modify, merge,\n" . |
36 | "publish, distribute, sublicense, and/or sell copies of the Software,\n" . |
37 | "and to permit persons to whom the Software is furnished to do so,\n" . |
38 | "subject to the following conditions:\n" . |
39 | "\n" . |
40 | "The above copyright notice and this permission notice shall be\n" . |
41 | "included in all copies or substantial portions of the Software.\n" . |
42 | "\n" . |
43 | "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n" . |
44 | "EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n" . |
45 | "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n" . |
46 | "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n" . |
47 | "BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n" . |
48 | "ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n" . |
49 | "CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n" . |
50 | "SOFTWARE.\n"; |
51 | |
29efe1b9 |
52 | $__quiet = $__donothing = $__reverse = 0; |
53 | |
54 | while ($ARGV[0] =~ /^-(.+)$/) { |
55 | shift @ARGV; |
56 | $__quiet = 1, next if $1 eq "q"; |
57 | $__quiet = 0, $__donothing = 1, next if $1 eq "n"; |
58 | $__reverse = 1, next if $1 eq "r"; |
da0f8522 |
59 | if ($1 eq "-help") { |
60 | print STDERR $usage; |
61 | exit 0; |
62 | } elsif ($1 eq "-version") { |
63 | if ('$Revision$' =~ /Revision:\s+(\d+)/) { |
64 | print "multi revision $1\n"; |
65 | } else { |
66 | print "multi: unknown revision\n"; |
67 | } |
68 | exit 0; |
69 | } elsif ($1 eq "-licence" or $1 eq "-license") { |
70 | print $licence; |
71 | exit 0; |
72 | } |
29efe1b9 |
73 | } |
74 | |
da0f8522 |
75 | die $usage if $#ARGV < 2; |
29efe1b9 |
76 | |
77 | @__cmd = (); |
78 | if ($ARGV[0] eq "-") { |
79 | shift @ARGV; |
80 | while (defined $ARGV[0] and $ARGV[0] ne "-") { |
81 | push @__cmd, shift @ARGV; |
82 | } |
83 | if (!defined $ARGV[0]) { |
84 | die "multi: no terminating - in multiple-word command mode\n"; |
85 | } |
86 | shift @ARGV; # eat trailing - |
87 | } else { |
88 | $__cmd[0] = shift @ARGV; |
89 | } |
90 | $__action = shift @ARGV; |
91 | |
92 | while (@ARGV) { |
93 | $_ = $__origfile = shift @ARGV; |
94 | eval $__action; |
95 | $__newfile = $_; |
96 | ($__origfile, $__newfile) = ($__newfile, $__origfile) if $__reverse; |
97 | &pcmd(@__cmd, $__origfile, $__newfile) if !$__quiet; |
98 | system @__cmd, $__origfile, $__newfile if !$__donothing; |
99 | } |
100 | |
101 | sub pcmd { |
102 | my (@words) = @_; |
103 | printf "%s\n", join " ", map { &fmt($_) } @words; |
104 | } |
105 | |
106 | sub fmt { |
107 | local ($_) = @_; |
108 | if (/[ !"#$&'()*;<>?\\`|~]/) { |
109 | s/'/'\\''/g; |
110 | "'$_'"; |
111 | } else { |
112 | $_; |
113 | } |
114 | } |