Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | # |
| 4 | #//===----------------------------------------------------------------------===// |
| 5 | #// |
| 6 | #// The LLVM Compiler Infrastructure |
| 7 | #// |
| 8 | #// This file is dual licensed under the MIT and the University of Illinois Open |
| 9 | #// Source Licenses. See LICENSE.txt for details. |
| 10 | #// |
| 11 | #//===----------------------------------------------------------------------===// |
| 12 | # |
| 13 | |
| 14 | use strict; |
| 15 | use warnings; |
| 16 | |
| 17 | use File::Glob ":glob"; |
| 18 | |
| 19 | use FindBin; |
| 20 | use lib "$FindBin::Bin/lib"; |
| 21 | |
| 22 | use tools; |
| 23 | |
| 24 | our $VERSION = "0.02"; |
| 25 | |
| 26 | sub wipe($$$) { |
| 27 | |
| 28 | my ( $input, $output, $wipe ) = @_; |
| 29 | my $bulk = read_file( $input, -binary => 1 ); |
| 30 | $bulk =~ s{($wipe)}{ " " x length( $1 ) }ge; |
| 31 | write_file( $output, \$bulk, -binary => 1 ); |
| 32 | return undef; |
| 33 | |
| 34 | }; # sub wipe |
| 35 | |
| 36 | my @wipe; |
| 37 | my $target = "."; |
| 38 | get_options( |
| 39 | "wipe-literal=s" => |
| 40 | sub { my $arg = $_[ 1 ]; push( @wipe, qr{@{ [ quotemeta( $arg ) ] }} ); }, |
| 41 | "wipe-regexp=s" => |
| 42 | sub { my $arg = $_[ 1 ]; push( @wipe, qr{$arg} ); }, |
| 43 | "target-directory=s" => \$target, |
| 44 | ); |
| 45 | |
| 46 | # Convert strings to regular expression. |
| 47 | my $wipe = qr{@{ [ join( "|", @wipe ) ] }}; |
| 48 | |
| 49 | my %jobs; |
| 50 | |
| 51 | # Collect files to process. |
| 52 | # jobs: output -> input. |
| 53 | foreach my $arg ( @ARGV ) { |
| 54 | my @inputs = ( $^O eq "MSWin32" ? bsd_glob( $arg ) : ( $arg ) ); |
| 55 | foreach my $input ( @inputs ) { |
| 56 | my $file = get_file( $input ); |
| 57 | my $output = cat_file( $target, $file ); |
| 58 | if ( exists( $jobs{ $output } ) ) { |
| 59 | runtime_error( |
| 60 | "\"$jobs{ $output }\" and \"$input\" input files tend to be written " . |
| 61 | "to the same output file \"$output\"" |
| 62 | ); |
| 63 | }; # if |
| 64 | $jobs{ $output } = $input; |
| 65 | }; # foreach |
| 66 | }; # foreach $file |
| 67 | |
| 68 | # Process files. |
| 69 | %jobs = reverse( %jobs ); # jobs: input -> output. |
| 70 | foreach my $input ( sort( keys( %jobs ) ) ) { |
| 71 | my $output = $jobs{ $input }; |
| 72 | info( "\"$input\" -> \"$output\"" ); |
| 73 | wipe( $input, $output, $wipe ); |
| 74 | }; # foreach $input |
| 75 | |
| 76 | exit( 0 ); |
| 77 | |
| 78 | __END__ |
| 79 | |
| 80 | # |
| 81 | # Embedded documentation. |
| 82 | # |
| 83 | |
| 84 | =pod |
| 85 | |
| 86 | =head1 NAME |
| 87 | |
| 88 | B<wipe-string.pl> -- Wipe string in text or binary files. |
| 89 | |
| 90 | =head1 SYNOPSIS |
| 91 | |
| 92 | B<wipe-string.pl> I<OPTION>... I<FILE>... |
| 93 | |
| 94 | =head1 OPTIONS |
| 95 | |
| 96 | =over |
| 97 | |
| 98 | =item B<--doc> |
| 99 | |
| 100 | =item B<--manual> |
| 101 | |
| 102 | Print full help message and exit. |
| 103 | |
| 104 | =item B<--help> |
| 105 | |
| 106 | Print short help message and exit. |
| 107 | |
| 108 | =item B<--target-directory=>I<dir> |
| 109 | |
| 110 | Directory to put result files to. By default result files are written in the current working |
| 111 | directory. |
| 112 | |
| 113 | =item B<--usage> |
| 114 | |
| 115 | Print very short usage message and exit. |
| 116 | |
| 117 | =item B<--version> |
| 118 | |
| 119 | Print version and exit. |
| 120 | |
| 121 | =item B<--wipe-literal=>I<str> |
| 122 | |
| 123 | Specify literal string to wipe. Multiple literals are allowed. |
| 124 | |
| 125 | =item B<--wipe-regexp=>I<str> |
| 126 | |
| 127 | Specify Perl regular expression to wipe. Multiple regular expressions may be specified. |
| 128 | |
| 129 | Be careful. Protect special characters from beign interpreted by shell. |
| 130 | |
| 131 | =back |
| 132 | |
| 133 | =head1 ARGUMENTS |
| 134 | |
| 135 | =over |
| 136 | |
| 137 | =item I<file> |
| 138 | |
| 139 | File name to wipe string in. |
| 140 | |
| 141 | =back |
| 142 | |
| 143 | =head1 DESCRIPTION |
| 144 | |
| 145 | The script wipes strings in files. String may be specified literally or by Perl regular expression. |
| 146 | Strings are wiped by replacing characters with spaces, so size of file remains the same. The script |
| 147 | may be applied to both text and binary files. |
| 148 | |
| 149 | Result files are written by default to current directory, or to directory specified by |
| 150 | B<--target-directory> option, if any. If multiple input files tend to be written to the same output |
| 151 | file (e. g. identically named input files located in different directories), the script generates an |
| 152 | error. |
| 153 | |
| 154 | The script reads entire file, process it, and the writes to disk. Therefore it is (almost) safe to |
| 155 | update files in-place (see examples). |
| 156 | |
| 157 | =head1 EXAMPLES |
| 158 | |
| 159 | Wipe "Copyright" word in all the files with "txt" suffix in current directory, overwrite original |
| 160 | files (update them in-place): |
| 161 | |
| 162 | wipe-string.pl --wipe-literal="Copyright" *.txt |
| 163 | |
| 164 | Wipe "Copyright" and "Copyleft" words in all the files with "txt" suffix in current directory, |
| 165 | write result files to ../wiped directory: |
| 166 | |
| 167 | wipe-string.pl --wipe-literal=Copyright --wipe-literal=Copyleft --target-dir=../wiped *.txt |
| 168 | |
| 169 | Wipe "Copyright" and "Copyleft" words in files from "doc" directory, write result files to current |
| 170 | directory; |
| 171 | |
| 172 | wipe-string.pl --wipe-regexp="Copyright|Copyleft" doc/* |
| 173 | |
| 174 | Wipe "defaultlib" directive in all the library files: |
| 175 | |
| 176 | wipe-string.pl --wipe-regexp="-defaultlib:[A-Za-z0-9_.]+" *.lib |
| 177 | |
| 178 | (Be careful: the script does not analyze structure of library and object files, it just wipes |
Alp Toker | 8f2d3f0 | 2014-02-24 10:40:15 +0000 | [diff] [blame] | 179 | U<strings>, so it wipes all the occurrences of strings matching to specified regular expression.) |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 180 | |
| 181 | =cut |
| 182 | |
| 183 | # end of file # |