1 | #!@PERL@ |
---|
2 | # |
---|
3 | # $Id$ |
---|
4 | # |
---|
5 | |
---|
6 | eval "exec @PERL@ -S $0 $*" |
---|
7 | if $running_under_some_shell; |
---|
8 | |
---|
9 | require 'getopts.pl'; |
---|
10 | &Getopts("p:vh"); # help, pattern file, verbose, |
---|
11 | |
---|
12 | if ($opt_h || ! $opt_p) { |
---|
13 | print STDERR <<NO_MORE_HELP; |
---|
14 | word-replace |
---|
15 | |
---|
16 | Replace *words* with patterns. Pattern file specifies which patterns |
---|
17 | to replace on each line. All patterns are wrapped with perl \\b regexp |
---|
18 | specifiers. |
---|
19 | |
---|
20 | Usage: $0 [-v] -p pattern-file files to replace |
---|
21 | |
---|
22 | -v -- possibly more verbose |
---|
23 | -p file -- pattern file |
---|
24 | -h -- help |
---|
25 | |
---|
26 | anything else == this help message |
---|
27 | |
---|
28 | Pattern file looks like this: |
---|
29 | |
---|
30 | # Example: |
---|
31 | # ignores all lines with beginning with # or not exactly 2 fields |
---|
32 | _Dorky_Name rtems_dorky_name # comments, and blank lines are cool |
---|
33 | _Dorky_Name2 rtems_dorky_name2 # comments, and blank lines are cool |
---|
34 | NO_MORE_HELP |
---|
35 | exit 0; |
---|
36 | } |
---|
37 | |
---|
38 | $verbose = $opt_v; |
---|
39 | $pattern_file = $opt_p; |
---|
40 | |
---|
41 | # make standard outputs unbuffered (so the '.'s come out ok) |
---|
42 | $oldfh = select(STDERR); $| = 1; select($oldfh); |
---|
43 | $oldfh = select(STDOUT); $| = 1; select($oldfh); |
---|
44 | |
---|
45 | # pull in the patterns |
---|
46 | open(PATTERNS, "<$pattern_file") || |
---|
47 | die "could not open $pattern_file: $!, crapped out at"; |
---|
48 | |
---|
49 | foreach (<PATTERNS>) |
---|
50 | { |
---|
51 | chop; |
---|
52 | s/#.*//; |
---|
53 | next if /^$/; |
---|
54 | ($orig, $new, $junk, @rest) = split; |
---|
55 | next if ( ! $orig || ! $new || $junk); # <2 or >2 patterns |
---|
56 | die "pattern appears 2x: '$orig' in '$pattern_file'--" if defined($patterns{$orig}); |
---|
57 | $patterns{$orig} = $new; |
---|
58 | } |
---|
59 | close PATTERNS; |
---|
60 | |
---|
61 | # walk thru each line in each file |
---|
62 | foreach $file (@ARGV) |
---|
63 | { |
---|
64 | print "$file\t"; |
---|
65 | |
---|
66 | open (INFILE, "<$file") || |
---|
67 | die "could not open input file $file: $!"; |
---|
68 | |
---|
69 | $outfile = $file . ".fixed";; |
---|
70 | open (OUTFILE, ">$outfile") || |
---|
71 | die "could not open output file $outfile: $!"; |
---|
72 | |
---|
73 | while (<INFILE>) |
---|
74 | { |
---|
75 | study; # maybe make s/// faster |
---|
76 | foreach $key (keys %patterns) |
---|
77 | { |
---|
78 | if ( s/\b$key\b/$patterns{$key}/ge ) |
---|
79 | { |
---|
80 | print "."; |
---|
81 | } |
---|
82 | } |
---|
83 | print OUTFILE $_; |
---|
84 | } |
---|
85 | print "\n"; |
---|
86 | close INFILE; |
---|
87 | close OUTFILE; |
---|
88 | } |
---|
89 | |
---|