blob: f06647c41289cd57ced56d3ce8687a86828b2fed [file] [log] [blame]
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +01001#!/usr/bin/perl
2
3use warnings;
4use strict;
5
6use utf8;
7use open qw(:std utf8);
8
9# apply substitutions from the table in the first arg to files
10# expected usage: via invoke-rename.pl
11
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020012my $do_strings = 0;
13if( $ARGV[0] eq "-s" ) {
14 shift;
15 $do_strings = 1;
16}
17
18die "Usage: $0 [-s] names-file [filenames...]\n" if( @ARGV < 1 or ! -r $ARGV[0] );
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010019
20open my $nfh, '<', shift or die;
21my @names = <$nfh>;
22close $nfh or die;
23
24my %subst;
25for my $name (@names) {
26 chomp $name;
27 my ($old, $new) = split / /, $name;
28 $subst{$old} = $new;
29}
30
31my $string = qr/".*?(?<!\\)"/;
32my $space = qr/\s+/;
33my $idnum = qr/[a-zA-Z0-9_]+/;
34my $symbols = qr/[!#%&'()*+,-.:;<=>?@^_`{|}~\$\/\[\\\]]+|"/;
35
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020036# if we replace inside strings, we don't consider them a token
37my $token = $do_strings ? qr/$space|$idnum|$symbols/
38 : qr/$string|$space|$idnum|$symbols/;
39
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010040my %warnings;
41
42while( my $filename = shift )
43{
44 print STDERR "$filename... ";
Manuel Pégourié-Gonnardf7d945f2015-04-03 15:21:50 +020045 if( -d $filename ) { print STDERR "skip (directory)\n"; next }
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010046
47 open my $rfh, '<', $filename or die;
48 my @lines = <$rfh>;
49 close $rfh or die;
50
51 my @out;
52 for my $line (@lines) {
Manuel Pégourié-Gonnardfb9f2a02015-04-03 18:37:07 +020053 if( $line =~ /#include/ ) {
54 $line =~ s/polarssl/mbedtls/;
55 $line =~ s/POLARSSL/MBEDTLS/;
56 push( @out, $line );
57 next;
58 }
59
Manuel Pégourié-Gonnard88323c72015-04-03 14:38:02 +020060 my @words = ($line =~ /$token/g);
Manuel Pégourié-Gonnard3385cf42015-04-02 17:59:30 +010061 my $checkline = join '', @words;
62 if( $checkline eq $line ) {
63 my @new = map { exists $subst{$_} ? $subst{$_} : $_ } @words;
64 push( @out, join '', @new );
65 } else {
66 $warnings{$filename} = [] unless $warnings{$filename};
67 push @{ $warnings{$filename} }, $line;
68 push( @out, $line );
69 }
70 }
71
72 open my $wfh, '>', $filename or die;
73 print $wfh $_ for @out;
74 close $wfh or die;
75 print STDERR "done\n";
76}
77
78if( %warnings ) {
79 print "\nWarning: lines skipped due to unexpected charaacters:\n";
80 for my $filename (sort keys %warnings) {
81 print "in $filename:\n";
82 print for @{ $warnings{$filename} };
83 }
84}