2008-05-05 Paolo Borelli <pborelli@katamail.com>
[nautilus.git] / check-strings.pl
blob86126c95b3f5c6db816d0edb5e42d013b8dd460c
1 #!/usr/bin/perl -w
2 # -*- Mode: perl; indent-tabs-mode: nil -*-
5 # Nautilus
7 # Copyright (C) 2000, 2001 Eazel, Inc.
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; either version 2 of the
12 # License, or (at your option) any later version.
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this library; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # Author: Darin Adler <darin@bentspoon.com>,
26 # check-strings.pl: Search for .c and .h files where someone forgot
27 # to put _() around a string.
29 # Throughout this file you will find extra \ before quote symbols
30 # that perl does not require. These are to appease emacs perl mode.
32 use diagnostics;
33 use strict;
35 # default to all the files starting from the current directory
36 if (!@ARGV)
38 @ARGV = `find . \\( -name '*.c' -o -name '*.h' \\) -print`;
41 sub found_string;
43 # read in file with functions for which no translation is needed
44 my @no_translation_needed_functions;
45 open FUNCTIONS, "check-strings-functions" or die "can't open functions file";
46 while (<FUNCTIONS>)
48 chomp;
49 s/(([^\\]|\\.)*)\#.*/$1/;
50 s/\s*$//;
51 next if /^$/;
52 push @no_translation_needed_functions, $_;
54 close FUNCTIONS;
55 my $no_translation_needed_function_pattern = "^(" . (join "|", @no_translation_needed_functions) . ")\$";
57 # read in file with patterns for which no translation is needed
58 my @no_translation_needed_patterns;
59 open STRINGS, "check-strings-patterns" or die "can't open patterns file";
60 while (<STRINGS>)
62 chomp;
63 s/(([^\\]|\\.)*)\#.*/$1/;
64 s/\s*$//;
65 next if /^$/;
66 my ($string_pattern, $file_name_pattern) = /(.+?)\s*\|\|\|\s*(.+)/;
67 $string_pattern ||= $_;
68 $file_name_pattern ||= ".";
69 push @no_translation_needed_patterns, [$string_pattern, $file_name_pattern];
71 close STRINGS;
73 FILE: foreach my $file (@ARGV)
75 chomp $file;
76 open FILE, $file or die "can't open $file";
78 my $in_comment = 0;
80 my $string = "";
82 my $last_word;
83 my @stack = ();
84 my $paren_level = 0;
85 my $in_exception_function = 0;
87 LINE: while (<FILE>)
89 if ($in_comment)
91 s/.*?\*\/// or next LINE;
92 $in_comment = 0;
95 # general approach is to just remove things we aren't interested in
97 next LINE if /^\s*#\s*(\d|include)/;
99 while (s/(((.*?)(\/\*|[\'\"\(\)]|\w+))|.+)//)
101 my $skipped = $3;
102 $skipped = $1 unless defined $skipped;
103 my $found = $4;
104 $found = "" unless defined $found;
106 my $function_name = $last_word || "";
108 if ($skipped =~ /\S/ or $found =~ /^[\(\)\w]/)
110 if ($string ne "")
112 found_string ($string, $file, $.) unless $in_exception_function;
113 $string = "";
115 undef $last_word;
118 last unless $found ne "";
120 if ($found eq '"')
122 s/^(([^\\\"]|\\.)*)\"// or (print "$file:$.:unclosed quote\n"), next FILE;
123 $string .= $1;
125 elsif ($found eq "'")
127 s/^([^\\\']|\\.)*\'// or (print "$file:$.:unclosed single quote\n"), next FILE;
129 elsif ($found eq "/*")
131 s/^.*?\*\/// or $in_comment = 1, next LINE;
133 elsif ($found eq "(")
135 if ($function_name or $paren_level == 0)
137 push @stack, [$paren_level, $in_exception_function];
138 $paren_level = 0;
139 $in_exception_function = 1 if $function_name =~ /$no_translation_needed_function_pattern/o;
141 $paren_level++;
143 elsif ($found eq ")")
145 (print "$file:$.:mismatched paren (type 1)\n"), next FILE if $paren_level == 0;
146 $paren_level--;
147 if ($paren_level == 0)
149 (print "$file:$.:mismatched paren (type 2)\n"), next FILE if @stack == 0;
150 ($paren_level, $in_exception_function) = @{pop @stack};
153 else
155 $last_word = $found;
159 close FILE;
162 sub found_string
164 my ($string, $file, $line) = @_;
166 for my $exception (@no_translation_needed_patterns)
168 return if $string =~ /$exception->[0]/ and $file =~ /$exception->[1]/;
171 print "$file:$line:\"$string\" is not marked for translation\n";