2 # -*- Mode: perl; indent-tabs-mode: nil -*-
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.
35 # default to all the files starting from the current directory
38 @ARGV = `find . \\( -name '*.c' -o -name '*.h' \\) -print`;
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";
49 s/(([^\\]|\\.)*)\#.*/$1/;
52 push @no_translation_needed_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";
63 s/(([^\\]|\\.)*)\#.*/$1/;
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];
73 FILE
: foreach my $file (@ARGV)
76 open FILE
, $file or die "can't open $file";
85 my $in_exception_function = 0;
91 s/.*?\*\/// or next LINE
;
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+))|.+)//)
102 $skipped = $1 unless defined $skipped;
104 $found = "" unless defined $found;
106 my $function_name = $last_word || "";
108 if ($skipped =~ /\S/ or $found =~ /^[\(\)\w]/)
112 found_string
($string, $file, $.) unless $in_exception_function;
118 last unless $found ne "";
122 s/^(([^\\\"]|\\.)*)\"// or (print "$file:$.:unclosed quote\n"), next FILE
;
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];
139 $in_exception_function = 1 if $function_name =~ /$no_translation_needed_function_pattern/o;
143 elsif ($found eq ")")
145 (print "$file:$.:mismatched paren (type 1)\n"), next FILE
if $paren_level == 0;
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};
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";