blob: deab95ffad0433d5b0749509dfed29ef6e3d9277 [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001#!/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
14use strict;
15use warnings;
16
17use File::Glob ":glob";
18
19use FindBin;
20use lib "$FindBin::Bin/lib";
21
22use tools;
23
24our $VERSION = "0.02";
25
26sub 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
36my @wipe;
37my $target = ".";
38get_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.
47my $wipe = qr{@{ [ join( "|", @wipe ) ] }};
48
49my %jobs;
50
51# Collect files to process.
52# jobs: output -> input.
53foreach 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.
70foreach my $input ( sort( keys( %jobs ) ) ) {
71 my $output = $jobs{ $input };
72 info( "\"$input\" -> \"$output\"" );
73 wipe( $input, $output, $wipe );
74}; # foreach $input
75
76exit( 0 );
77
78__END__
79
80#
81# Embedded documentation.
82#
83
84=pod
85
86=head1 NAME
87
88B<wipe-string.pl> -- Wipe string in text or binary files.
89
90=head1 SYNOPSIS
91
92B<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
102Print full help message and exit.
103
104=item B<--help>
105
106Print short help message and exit.
107
108=item B<--target-directory=>I<dir>
109
110Directory to put result files to. By default result files are written in the current working
111directory.
112
113=item B<--usage>
114
115Print very short usage message and exit.
116
117=item B<--version>
118
119Print version and exit.
120
121=item B<--wipe-literal=>I<str>
122
123Specify literal string to wipe. Multiple literals are allowed.
124
125=item B<--wipe-regexp=>I<str>
126
127Specify Perl regular expression to wipe. Multiple regular expressions may be specified.
128
129Be 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
139File name to wipe string in.
140
141=back
142
143=head1 DESCRIPTION
144
145The script wipes strings in files. String may be specified literally or by Perl regular expression.
146Strings are wiped by replacing characters with spaces, so size of file remains the same. The script
147may be applied to both text and binary files.
148
149Result files are written by default to current directory, or to directory specified by
150B<--target-directory> option, if any. If multiple input files tend to be written to the same output
151file (e. g. identically named input files located in different directories), the script generates an
152error.
153
154The script reads entire file, process it, and the writes to disk. Therefore it is (almost) safe to
155update files in-place (see examples).
156
157=head1 EXAMPLES
158
159Wipe "Copyright" word in all the files with "txt" suffix in current directory, overwrite original
160files (update them in-place):
161
162 wipe-string.pl --wipe-literal="Copyright" *.txt
163
164Wipe "Copyright" and "Copyleft" words in all the files with "txt" suffix in current directory,
165write result files to ../wiped directory:
166
167 wipe-string.pl --wipe-literal=Copyright --wipe-literal=Copyleft --target-dir=../wiped *.txt
168
169Wipe "Copyright" and "Copyleft" words in files from "doc" directory, write result files to current
170directory;
171
172 wipe-string.pl --wipe-regexp="Copyright|Copyleft" doc/*
173
174Wipe "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 Toker8f2d3f02014-02-24 10:40:15 +0000179U<strings>, so it wipes all the occurrences of strings matching to specified regular expression.)
Jim Cownie5e8470a2013-09-27 10:38:44 +0000180
181=cut
182
183# end of file #