check-strings.pl 4.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#!/usr/bin/perl -w
# -*- Mode: perl; indent-tabs-mode: nil -*-

#
#  Nautilus
#
#  Copyright (C) 2000, 2001 Eazel, Inc.
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License as
#  published by the Free Software Foundation; either version 2 of the
#  License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this library; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
23
#  Author: Darin Adler <darin@bentspoon.com>,
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#

# check-strings.pl: Search for .c and .h files where someone forgot
# to put _() around a string.

# Throughout this file you will find extra \ before quote symbols
# that perl does not require. These are to appease emacs perl mode.

use diagnostics;
use strict;

# default to all the files starting from the current directory
if (!@ARGV)
  {
    @ARGV = `find . \\( -name '*.c' -o -name '*.h' \\) -print`;
  }

sub found_string;

# read in file with functions for which no translation is needed
my @no_translation_needed_functions;
45
open FUNCTIONS, "check-strings-functions" or die "can't open functions file";
46 47 48 49 50 51 52 53 54
while (<FUNCTIONS>)
  {
    chomp;
    s/(([^\\]|\\.)*)\#.*/$1/;
    s/\s*$//;
    next if /^$/;
    push @no_translation_needed_functions, $_;
  }
close FUNCTIONS;
55
my $no_translation_needed_function_pattern = "^(" . (join "|", @no_translation_needed_functions) . ")\$";
56 57 58

# read in file with patterns for which no translation is needed
my @no_translation_needed_patterns;
59
open STRINGS, "check-strings-patterns" or die "can't open patterns file";
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
while (<STRINGS>)
  {
    chomp;
    s/(([^\\]|\\.)*)\#.*/$1/;
    s/\s*$//;
    next if /^$/;
    my ($string_pattern, $file_name_pattern) = /(.+?)\s*\|\|\|\s*(.+)/;
    $string_pattern ||= $_;
    $file_name_pattern ||= ".";
    push @no_translation_needed_patterns, [$string_pattern, $file_name_pattern];
  }
close STRINGS;

FILE: foreach my $file (@ARGV)
  {
    chomp $file;
    open FILE, $file or die "can't open $file";

    my $in_comment = 0;

    my $string = "";

    my $last_word;
    my @stack = ();
    my $paren_level = 0;
    my $in_exception_function = 0;

    LINE: while (<FILE>)
      {
	if ($in_comment)
	  {
	    s/.*?\*\/// or next LINE;
	    $in_comment = 0;
	  }

	# general approach is to just remove things we aren't interested in

	next LINE if /^\s*#\s*(\d|include)/;

	while (s/(((.*?)(\/\*|[\'\"\(\)]|\w+))|.+)//)
	  {
            my $skipped = $3;
            $skipped = $1 unless defined $skipped;
            my $found = $4;
            $found = "" unless defined $found;

            my $function_name = $last_word || "";

	    if ($skipped =~ /\S/ or $found =~ /^[\(\)\w]/)
	      {
		if ($string ne "")
		  {
		    found_string ($string, $file, $.) unless $in_exception_function;
		    $string = "";
		  }
                undef $last_word;
	      }

	    last unless $found ne "";

	    if ($found eq '"')
	      {
		s/^(([^\\\"]|\\.)*)\"// or (print "$file:$.:unclosed quote\n"), next FILE;
		$string .= $1;
	      }
	    elsif ($found eq "'")
	      {
                s/^([^\\\']|\\.)*\'// or (print "$file:$.:unclosed single quote\n"), next FILE;
              }
	    elsif ($found eq "/*")
	      {
		s/^.*?\*\/// or $in_comment = 1, next LINE;
	      }
	    elsif ($found eq "(")
              {
                if ($function_name or $paren_level == 0)
                  {
                    push @stack, [$paren_level, $in_exception_function];
                    $paren_level = 0;
                    $in_exception_function = 1 if $function_name =~ /$no_translation_needed_function_pattern/o;
                  }
                $paren_level++;
              }
	    elsif ($found eq ")")
	      {
145
                (print "$file:$.:mismatched paren (type 1)\n"), next FILE if $paren_level == 0;
146 147 148
                $paren_level--;
                if ($paren_level == 0)
                  {
149
                    (print "$file:$.:mismatched paren (type 2)\n"), next FILE if @stack == 0;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
                    ($paren_level, $in_exception_function) = @{pop @stack};
                  }
	      }
            else
              {
                $last_word = $found;
              }
	  }
      }
    close FILE;
  }

sub found_string
  {
    my ($string, $file, $line) = @_;

    for my $exception (@no_translation_needed_patterns)
      {
        return if $string =~ /$exception->[0]/ and $file =~ /$exception->[1]/;
      }

    print "$file:$line:\"$string\" is not marked for translation\n";
  }