home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / intltool-debian / intltool-update < prev   
Encoding:
Text File  |  2006-11-08  |  27.9 KB  |  1,101 lines

  1. #!/usr/bin/perl -w
  2. # -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 4  -*-
  3.  
  4. #
  5. #  The Intltool Message Updater
  6. #
  7. #  Copyright (C) 2000-2003 Free Software Foundation.
  8. #
  9. #  Intltool is free software; you can redistribute it and/or
  10. #  modify it under the terms of the GNU General Public License 
  11. #  version 2 published by the Free Software Foundation.
  12. #
  13. #  Intltool is distributed in the hope that it will be useful,
  14. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. #  General Public License for more details.
  17. #
  18. #  You should have received a copy of the GNU General Public License
  19. #  along with this program; if not, write to the Free Software
  20. #  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. #
  22. #  As a special exception to the GNU General Public License, if you
  23. #  distribute this file as part of a program that contains a
  24. #  configuration script generated by Autoconf, you may include it under
  25. #  the same distribution terms that you use for the rest of that program.
  26. #
  27. #  Authors: Kenneth Christiansen <kenneth@gnu.org>
  28. #           Maciej Stachowiak
  29. #           Darin Adler <darin@bentspoon.com>
  30.  
  31. ## Release information
  32. my $PROGRAM = "intltool-update";
  33. my $VERSION = "0.35.0";
  34. my $PACKAGE = "intltool";
  35.  
  36. ## Loaded modules
  37. use strict;
  38. use Getopt::Long;
  39. use Cwd;
  40. use File::Copy;
  41. use File::Find;
  42.  
  43. ## Scalars used by the option stuff
  44. my $HELP_ARG        = 0;
  45. my $VERSION_ARG    = 0;
  46. my $DIST_ARG       = 0;
  47. my $POT_ARG       = 0;
  48. my $HEADERS_ARG    = 0;
  49. my $MAINTAIN_ARG   = 0;
  50. my $REPORT_ARG     = 0;
  51. my $VERBOSE       = 0;
  52. my $GETTEXT_PACKAGE = "";
  53. my $OUTPUT_FILE    = "";
  54.  
  55. my @languages;
  56. my %varhash = ();
  57. my %po_files_by_lang = ();
  58.  
  59. # Regular expressions to categorize file types.
  60. # FIXME: Please check if the following is correct
  61.  
  62. my $xml_support =
  63. "xml(?:\\.in)*|".    # http://www.w3.org/XML/ (Note: .in is not required)
  64. "ui|".            # Bonobo specific - User Interface desc. files
  65. "lang|".        # ?
  66. "glade2?(?:\\.in)*|".    # Glade specific - User Interface desc. files (Note: .in is not required)
  67. "scm(?:\\.in)*|".    # ? (Note: .in is not required)
  68. "oaf(?:\\.in)+|".    # DEPRECATED: Replaces by Bonobo .server files 
  69. "etspec|".        # ?
  70. "server(?:\\.in)+|".    # Bonobo specific
  71. "sheet(?:\\.in)+|".    # ?
  72. "schemas(?:\\.in)+|".    # GConf specific
  73. "pong(?:\\.in)+|".    # DEPRECATED: PONG is not used [by GNOME] any longer.
  74. "kbd(?:\\.in)+";    # GOK specific. 
  75.  
  76. my $ini_support =
  77. "icon(?:\\.in)+|".    # http://www.freedesktop.org/Standards/icon-theme-spec
  78. "desktop(?:\\.in)+|".    # http://www.freedesktop.org/Standards/menu-spec
  79. "caves(?:\\.in)+|".    # GNOME Games specific
  80. "directory(?:\\.in)+|".    # http://www.freedesktop.org/Standards/menu-spec
  81. "soundlist(?:\\.in)+|".    # GNOME specific
  82. "keys(?:\\.in)+|".    # GNOME Mime database specific
  83. "theme(?:\\.in)+|".    # http://www.freedesktop.org/Standards/icon-theme-spec
  84. "service(?:\\.in)+";    # DBus specific
  85.  
  86. my $buildin_gettext_support = 
  87. "c|y|cs|cc|cpp|c\\+\\+|h|hh|gob|py";
  88.  
  89. ## Always flush buffer when printing
  90. $| = 1;
  91.  
  92. ## Sometimes the source tree will be rooted somewhere else.
  93. my $SRCDIR = ".";
  94. my $POTFILES_in;
  95.  
  96. $SRCDIR = $ENV{"srcdir"} if $ENV{"srcdir"};
  97. $POTFILES_in = "<$SRCDIR/POTFILES.in";
  98.  
  99. my $devnull = ($^O eq 'MSWin32' ? 'NUL:' : '/dev/null');
  100.  
  101. ## Handle options
  102. GetOptions 
  103. (
  104.  "help"            => \$HELP_ARG,
  105.  "version"            => \$VERSION_ARG,
  106.  "dist|d"           => \$DIST_ARG,
  107.  "pot|p"           => \$POT_ARG,
  108.  "headers|s"           => \$HEADERS_ARG,
  109.  "maintain|m"           => \$MAINTAIN_ARG,
  110.  "report|r"           => \$REPORT_ARG,
  111.  "verbose|x"           => \$VERBOSE,
  112.  "gettext-package|g=s" => \$GETTEXT_PACKAGE,
  113.  "output-file|o=s"     => \$OUTPUT_FILE,
  114.  ) or &Console_WriteError_InvalidOption;
  115.  
  116. &Console_Write_IntltoolHelp if $HELP_ARG;
  117. &Console_Write_IntltoolVersion if $VERSION_ARG;
  118.  
  119. my $arg_count = ($DIST_ARG > 0)
  120.     + ($POT_ARG > 0)
  121.     + ($HEADERS_ARG > 0)
  122.     + ($MAINTAIN_ARG > 0)
  123.     + ($REPORT_ARG > 0);
  124.  
  125. &Console_Write_IntltoolHelp if $arg_count > 1;
  126.  
  127. # --version and --help don't require a module name
  128. my $MODULE = $GETTEXT_PACKAGE || &FindPackageName || "unknown";
  129.  
  130. if ($POT_ARG)
  131. {
  132.     &GenerateHeaders;
  133.     &GeneratePOTemplate;
  134. }
  135. elsif ($HEADERS_ARG)
  136. {
  137.     &GenerateHeaders;
  138. }
  139. elsif ($MAINTAIN_ARG)
  140. {
  141.     &FindLeftoutFiles;
  142. }
  143. elsif ($REPORT_ARG)
  144. {
  145.     &GenerateHeaders;
  146.     &GeneratePOTemplate;
  147.     &Console_Write_CoverageReport;
  148. }
  149. elsif ((defined $ARGV[0]) && $ARGV[0] =~ /^[a-z]/)
  150. {
  151.     my $lang = $ARGV[0];
  152.  
  153.     ## Report error if the language file supplied
  154.     ## to the command line is non-existent
  155.     &Console_WriteError_NotExisting("$SRCDIR/$lang.po")
  156.         if ! -s "$SRCDIR/$lang.po";
  157.  
  158.     if (!$DIST_ARG)
  159.     {
  160.     print "Working, please wait..." if $VERBOSE;
  161.     &GenerateHeaders;
  162.     &GeneratePOTemplate;
  163.     }
  164.     &POFile_Update ($lang, $OUTPUT_FILE);
  165.     &Console_Write_TranslationStatus ($lang, $OUTPUT_FILE) if $VERBOSE;
  166. else 
  167. {
  168.     &Console_Write_IntltoolHelp;
  169. }
  170.  
  171. exit;
  172.  
  173. #########
  174.  
  175. sub Console_Write_IntltoolVersion
  176. {
  177.     print <<_EOF_;
  178. ${PROGRAM} (${PACKAGE}) $VERSION
  179. Written by Kenneth Christiansen, Maciej Stachowiak, and Darin Adler.
  180.  
  181. Copyright (C) 2000-2003 Free Software Foundation, Inc.
  182. This is free software; see the source for copying conditions.  There is NO
  183. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  184. _EOF_
  185.     exit;
  186. }
  187.  
  188. sub Console_Write_IntltoolHelp
  189. {
  190.     print <<_EOF_;
  191. Usage: ${PROGRAM} [OPTION]... LANGCODE
  192. Updates PO template files and merge them with the translations.
  193.  
  194. Mode of operation (only one is allowed):
  195.   -p, --pot                   generate the PO template only
  196.   -s, --headers               generate the header files in POTFILES.in
  197.   -m, --maintain              search for left out files from POTFILES.in
  198.   -r, --report                display a status report for the module
  199.   -d, --dist                  merge LANGCODE.po with existing PO template
  200.  
  201. Extra options:
  202.   -g, --gettext-package=NAME  override PO template name, useful with --pot
  203.   -o, --output-file=FILE      write merged translation to FILE
  204.   -x, --verbose               display lots of feedback
  205.       --help                  display this help and exit
  206.       --version               output version information and exit
  207.  
  208. Examples of use:
  209. ${PROGRAM} --pot    just create a new PO template
  210. ${PROGRAM} xy       create new PO template and merge xy.po with it
  211.  
  212. Report bugs to http://bugzilla.gnome.org/ (product name "$PACKAGE")
  213. or send email to <xml-i18n-tools\@gnome.org>.
  214. _EOF_
  215.     exit;
  216. }
  217.  
  218. sub echo_n
  219. {
  220.     my $str = shift;
  221.     my $ret = `echo "$str"`;
  222.  
  223.     $ret =~ s/\n$//; # do we need the "s" flag?
  224.  
  225.     return $ret;
  226. }
  227.  
  228. sub POFile_DetermineType ($) 
  229. {
  230.    my $type = $_;
  231.    my $gettext_type;
  232.  
  233.    my $xml_regex     = "(?:" . $xml_support . ")";
  234.    my $ini_regex     = "(?:" . $ini_support . ")";
  235.    my $buildin_regex = "(?:" . $buildin_gettext_support . ")";
  236.  
  237.    if ($type =~ /\[type: gettext\/([^\]].*)]/) 
  238.    {
  239.     $gettext_type=$1;
  240.    }
  241.    elsif ($type =~ /schemas(\.in)+$/) 
  242.    {
  243.     $gettext_type="schemas";
  244.    }
  245.    elsif ($type =~ /glade2?(\.in)*$/) 
  246.    {
  247.        $gettext_type="glade";
  248.    }
  249.    elsif ($type =~ /scm(\.in)*$/) 
  250.    {
  251.        $gettext_type="scheme";
  252.    }
  253.    elsif ($type =~ /keys(\.in)+$/) 
  254.    {
  255.        $gettext_type="keys";
  256.    }
  257.  
  258.    # bucket types
  259.  
  260.    elsif ($type =~ /$xml_regex$/) 
  261.    {
  262.        $gettext_type="xml";
  263.    }
  264.    elsif ($type =~ /$ini_regex$/) 
  265.    { 
  266.        $gettext_type="ini";
  267.    }
  268.    elsif ($type =~ /$buildin_regex$/) 
  269.    {
  270.        $gettext_type="buildin";
  271.    }
  272.    else
  273.    { 
  274.        $gettext_type="unknown"; 
  275.    }
  276.  
  277.    return "gettext\/$gettext_type";
  278. }
  279.  
  280. sub TextFile_DetermineEncoding ($) 
  281. {
  282.     my $gettext_code="ASCII"; # All files are ASCII by default
  283.     return $gettext_code;
  284. }
  285.  
  286. sub isNotValidMissing
  287. {
  288.     my ($file) = @_;
  289.  
  290.     return if $file =~ /^\{arch\}\/.*$/;
  291.     return if $file =~ /^$varhash{"PACKAGE"}-$varhash{"VERSION"}\/.*$/;
  292. }
  293.  
  294. sub FindLeftoutFiles
  295. {
  296.     my (@buf_i18n_plain,
  297.     @buf_i18n_xml,
  298.     @buf_i18n_xml_unmarked,
  299.     @buf_i18n_ini,
  300.     @buf_potfiles,
  301.     @buf_potfiles_ignore,
  302.     @buf_allfiles,
  303.     @buf_allfiles_sorted,
  304.     @buf_potfiles_sorted
  305.     );
  306.  
  307.     ## Search and find all translatable files
  308.     find sub { 
  309.     push @buf_i18n_plain,        "$File::Find::name" if /\.($buildin_gettext_support)$/;
  310.     push @buf_i18n_xml,          "$File::Find::name" if /\.($xml_support)$/;
  311.     push @buf_i18n_ini,          "$File::Find::name" if /\.($ini_support)$/;
  312.     push @buf_i18n_xml_unmarked, "$File::Find::name" if /\.(schemas(\.in)+)$/;
  313.     }, "..";
  314.  
  315.  
  316.     open POTFILES, $POTFILES_in or die "$PROGRAM:  there's no POTFILES.in!\n";
  317.     @buf_potfiles = grep !/^(#|\s*$)/, <POTFILES>;
  318.     close POTFILES;
  319.  
  320.     foreach (@buf_potfiles) {
  321.     s/^\[.*]\s*//;
  322.     }
  323.  
  324.     print "Searching for missing translatable files...\n" if $VERBOSE;
  325.  
  326.     ## Check if we should ignore some found files, when
  327.     ## comparing with POTFILES.in
  328.     foreach my $ignore ("POTFILES.skip", "POTFILES.ignore")
  329.     {
  330.     (-s $ignore) or next;
  331.  
  332.     if ("$ignore" eq "POTFILES.ignore")
  333.     {
  334.         print "The usage of POTFILES.ignore is deprecated. Please consider moving the\n".
  335.           "content of this file to POTFILES.skip.\n";
  336.     }
  337.  
  338.     print "Found $ignore: Ignoring files...\n" if $VERBOSE;
  339.     open FILE, "<$ignore" or die "ERROR: Failed to open $ignore!\n";
  340.         
  341.     while (<FILE>)
  342.     {
  343.         push @buf_potfiles_ignore, $_ unless /^(#|\s*$)/;
  344.     }
  345.     close FILE;
  346.  
  347.     @buf_potfiles = (@buf_potfiles_ignore, @buf_potfiles);
  348.     }
  349.  
  350.     foreach my $file (@buf_i18n_plain)
  351.     {
  352.     my $in_comment = 0;
  353.     my $in_macro = 0;
  354.  
  355.     open FILE, "<$file";
  356.     while (<FILE>)
  357.     {
  358.         # Handle continued multi-line comment.
  359.         if ($in_comment)
  360.         {
  361.         next unless s-.*\*/--;
  362.         $in_comment = 0;
  363.         }
  364.  
  365.         # Handle continued macro.
  366.         if ($in_macro)
  367.         {
  368.         $in_macro = 0 unless /\\$/;
  369.         next;
  370.         }
  371.  
  372.         # Handle start of macro (or any preprocessor directive).
  373.         if (/^\s*\#/)
  374.         {
  375.         $in_macro = 1 if /^([^\\]|\\.)*\\$/;
  376.         next;
  377.         }
  378.  
  379.         # Handle comments and quoted text.
  380.         while (m-(/\*|//|\'|\")-) # \' and \" keep emacs perl mode happy
  381.         {
  382.         my $match = $1;
  383.         if ($match eq "/*")
  384.         {
  385.             if (!s-/\*.*?\*/--)
  386.             {
  387.             s-/\*.*--;
  388.             $in_comment = 1;
  389.             }
  390.         }
  391.         elsif ($match eq "//")
  392.         {
  393.             s-//.*--;
  394.         }
  395.         else # ' or "
  396.         {
  397.             if (!s-$match([^\\]|\\.)*?$match-QUOTEDTEXT-)
  398.             {
  399.             warn "mismatched quotes at line $. in $file\n";
  400.             s-$match.*--;
  401.             }
  402.         }
  403.         }        
  404.  
  405.         if (/\.GetString ?\(QUOTEDTEXT/)
  406.         {
  407.                 if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  408.                     ## Remove the first 3 chars and add newline
  409.                     push @buf_allfiles, unpack("x3 A*", $file) . "\n";
  410.                 }
  411.         last;
  412.         }
  413.  
  414.         if (/_\(QUOTEDTEXT/)
  415.         {
  416.                 if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  417.                     ## Remove the first 3 chars and add newline
  418.                     push @buf_allfiles, unpack("x3 A*", $file) . "\n";
  419.                 }
  420.         last;
  421.         }
  422.     }
  423.     close FILE;
  424.     }
  425.  
  426.     foreach my $file (@buf_i18n_xml) 
  427.     {
  428.     open FILE, "<$file";
  429.     
  430.     while (<FILE>) 
  431.     {
  432.         # FIXME: share the pattern matching code with intltool-extract
  433.         if (/\s_[-A-Za-z0-9._:]+\s*=\s*\"([^"]+)\"/ || /<_[^>]+>/ || /translatable=\"yes\"/)
  434.         {
  435.                 if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  436.                     push @buf_allfiles, unpack("x3 A*", $file) . "\n";
  437.                 }
  438.         last;
  439.         }
  440.     }
  441.     close FILE;
  442.     }
  443.  
  444.     foreach my $file (@buf_i18n_ini)
  445.     {
  446.     open FILE, "<$file";
  447.     while (<FILE>) 
  448.     {
  449.         if (/_(.*)=/)
  450.         {
  451.                 if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  452.                     push @buf_allfiles, unpack("x3 A*", $file) . "\n";
  453.                 }
  454.         last;
  455.         }
  456.     }
  457.     close FILE;
  458.     }
  459.  
  460.     foreach my $file (@buf_i18n_xml_unmarked)
  461.     {
  462.         if (defined isNotValidMissing (unpack("x3 A*", $file))) {
  463.             push @buf_allfiles, unpack("x3 A*", $file) . "\n";
  464.         }
  465.     }
  466.  
  467.  
  468.     @buf_allfiles_sorted = sort (@buf_allfiles);
  469.     @buf_potfiles_sorted = sort (@buf_potfiles);
  470.  
  471.     my %in2;
  472.     foreach (@buf_potfiles_sorted) 
  473.     {
  474.     $in2{$_} = 1;
  475.     }
  476.  
  477.     my @result;
  478.  
  479.     foreach (@buf_allfiles_sorted)
  480.     {
  481.     if (!exists($in2{$_}))
  482.     {
  483.         push @result, $_
  484.     }
  485.     }
  486.  
  487.     my @buf_potfiles_notexist;
  488.  
  489.     foreach (@buf_potfiles_sorted)
  490.     {
  491.     chomp (my $dummy = $_);
  492.     if ("$dummy" ne "" and ! -f "../$dummy")
  493.     {
  494.         push @buf_potfiles_notexist, $_;
  495.     }
  496.     }
  497.  
  498.     ## Save file with information about the files missing
  499.     ## if any, and give information about this procedure.
  500.     if (@result + @buf_potfiles_notexist > 0)
  501.     {
  502.     if (@result) 
  503.     {
  504.         print "\n" if $VERBOSE;
  505.         unlink "missing";
  506.         open OUT, ">missing";
  507.         print OUT @result;
  508.         close OUT;
  509.         warn "\e[1mThe following files contain translations and are currently not in use. Please\e[0m\n".
  510.              "\e[1mconsider adding these to the POTFILES.in file, located in the po/ directory.\e[0m\n\n";
  511.         print STDERR @result, "\n";
  512.         warn "If some of these files are left out on purpose then please add them to\n".
  513.          "POTFILES.skip instead of POTFILES.in. A file \e[1m'missing'\e[0m containing this list\n".
  514.          "of left out files has been written in the current directory.\n";
  515.     }
  516.     if (@buf_potfiles_notexist)
  517.     {
  518.         unlink "notexist";
  519.         open OUT, ">notexist";
  520.         print OUT @buf_potfiles_notexist;
  521.         close OUT;
  522.         warn "\n" if ($VERBOSE or @result);
  523.         warn "\e[1mThe following files do not exist anymore:\e[0m\n\n";
  524.         warn @buf_potfiles_notexist, "\n";
  525.         warn "Please remove them from POTFILES.in or POTFILES.skip. A file \e[1m'notexist'\e[0m\n".
  526.          "containing this list of absent files has been written in the current directory.\n";
  527.     }
  528.     }
  529.  
  530.     ## If there is nothing to complain about, notify the user
  531.     else {
  532.     print "\nAll files containing translations are present in POTFILES.in.\n" if $VERBOSE;
  533.     }
  534. }
  535.  
  536. sub Console_WriteError_InvalidOption
  537. {
  538.     ## Handle invalid arguments
  539.     print STDERR "Try `${PROGRAM} --help' for more information.\n";
  540.     exit 1;
  541. }
  542.  
  543. sub GenerateHeaders
  544. {
  545.     my $EXTRACT = "/usr/bin/intltool-extract";
  546.     chomp $EXTRACT;
  547.  
  548.     $EXTRACT = $ENV{"INTLTOOL_EXTRACT"} if $ENV{"INTLTOOL_EXTRACT"};
  549.  
  550.     ## Generate the .h header files, so we can allow glade and
  551.     ## xml translation support
  552.     if (! -x "$EXTRACT")
  553.     {
  554.     print STDERR "\n *** The intltool-extract script wasn't found!"
  555.          ."\n *** Without it, intltool-update can not generate files.\n";
  556.     exit;
  557.     }
  558.     else
  559.     {
  560.     open (FILE, $POTFILES_in) or die "$PROGRAM: POTFILES.in not found.\n";
  561.     
  562.     while (<FILE>) 
  563.     {
  564.        chomp;
  565.        next if /^\[\s*encoding/;
  566.  
  567.        ## Find xml files in POTFILES.in and generate the
  568.        ## files with help from the extract script
  569.  
  570.        my $gettext_type= &POFile_DetermineType ($1);
  571.  
  572.        if (/\.($xml_support|$ini_support)$/ || /^\[/)
  573.        {
  574.            s/^\[[^\[].*]\s*//;
  575.  
  576.            my $filename = "../$_";
  577.  
  578.            if ($VERBOSE)
  579.            {
  580.            system ($EXTRACT, "--update", "--srcdir=$SRCDIR",
  581.                "--type=$gettext_type", $filename);
  582.            } 
  583.            else 
  584.            {
  585.             system ($EXTRACT, "--update", "--type=$gettext_type", 
  586.                "--srcdir=$SRCDIR", "--quiet", $filename);
  587.            }
  588.        }
  589.        }
  590.        close FILE;
  591.    }
  592. }
  593.  
  594. #
  595. # Generate .pot file from POTFILES.in
  596. #
  597. sub GeneratePOTemplate
  598. {
  599.     my $XGETTEXT = $ENV{"XGETTEXT"} || "/usr/bin/xgettext";
  600.     my $XGETTEXT_ARGS = $ENV{"XGETTEXT_ARGS"} || '';
  601.     chomp $XGETTEXT;
  602.  
  603.     if (! -x $XGETTEXT)
  604.     {
  605.     print STDERR " *** xgettext is not found on this system!\n".
  606.              " *** Without it, intltool-update can not extract strings.\n";
  607.     exit;
  608.     }
  609.  
  610.     print "Building $MODULE.pot...\n" if $VERBOSE;
  611.  
  612.     open INFILE, $POTFILES_in;
  613.     unlink "POTFILES.in.temp";
  614.     open OUTFILE, ">POTFILES.in.temp" or die("Cannot open POTFILES.in.temp for writing");
  615.  
  616.     my $gettext_support_nonascii = 0;
  617.  
  618.     # checks for GNU gettext >= 0.12
  619.     my $dummy = `$XGETTEXT --version --from-code=UTF-8 >$devnull 2>$devnull`;
  620.     if ($? == 0)
  621.     {
  622.     $gettext_support_nonascii = 1;
  623.     }
  624.     else
  625.     {
  626.     # urge everybody to upgrade gettext
  627.     print STDERR "WARNING: This version of gettext does not support extracting non-ASCII\n".
  628.              "         strings. That means you should install a version of gettext\n".
  629.              "         that supports non-ASCII strings (such as GNU gettext >= 0.12),\n".
  630.              "         or have to let non-ASCII strings untranslated. (If there is any)\n";
  631.     }
  632.  
  633.     my $encoding = "ASCII";
  634.     my $forced_gettext_code;
  635.     my @temp_headers;
  636.     my $encoding_problem_is_reported = 0;
  637.  
  638.     while (<INFILE>) 
  639.     {
  640.     next if (/^#/ or /^\s*$/);
  641.  
  642.     chomp;
  643.  
  644.     my $gettext_code;
  645.  
  646.     if (/^\[\s*encoding:\s*(.*)\s*\]/)
  647.     {
  648.         $forced_gettext_code=$1;
  649.     }
  650.     elsif (/\.($xml_support|$ini_support)$/ || /^\[/)
  651.     {
  652.         s/^\[.*]\s*//;
  653.             print OUTFILE "../$_.h\n";
  654.         push @temp_headers, "../$_.h";
  655.         $gettext_code = &TextFile_DetermineEncoding ("../$_.h") if ($gettext_support_nonascii and not defined $forced_gettext_code);
  656.     } 
  657.     else 
  658.     {
  659.         if ($SRCDIR eq ".") {
  660.             print OUTFILE "../$_\n";
  661.         } else {
  662.             print OUTFILE "$SRCDIR/../$_\n";
  663.         }
  664.         $gettext_code = &TextFile_DetermineEncoding ("../$_") if ($gettext_support_nonascii and not defined $forced_gettext_code);
  665.     }
  666.  
  667.     next if (! $gettext_support_nonascii);
  668.  
  669.     if (defined $forced_gettext_code)
  670.     {
  671.         $encoding=$forced_gettext_code;
  672.     }
  673.     elsif (defined $gettext_code and "$encoding" ne "$gettext_code")
  674.     {
  675.         if ($encoding eq "ASCII")
  676.         {
  677.         $encoding=$gettext_code;
  678.         }
  679.         elsif ($gettext_code ne "ASCII")
  680.         {
  681.         # Only report once because the message is quite long
  682.         if (! $encoding_problem_is_reported)
  683.         {
  684.             print STDERR "WARNING: You should use the same file encoding for all your project files,\n".
  685.                  "         but $PROGRAM thinks that most of the source files are in\n".
  686.                  "         $encoding encoding, while \"$_\" is (likely) in\n".
  687.                         "         $gettext_code encoding. If you are sure that all translatable strings\n".
  688.                  "         are in same encoding (say UTF-8), please \e[1m*prepend*\e[0m the following\n".
  689.                  "         line to POTFILES.in:\n\n".
  690.                  "                 [encoding: UTF-8]\n\n".
  691.                  "         and make sure that configure.in/ac checks for $PACKAGE >= 0.27 .\n".
  692.                  "(such warning message will only be reported once.)\n";
  693.             $encoding_problem_is_reported = 1;
  694.         }
  695.         }
  696.     }
  697.     }
  698.  
  699.     close OUTFILE;
  700.     close INFILE;
  701.  
  702.     unlink "$MODULE.pot-update";
  703.     my @xgettext_argument=("$XGETTEXT",
  704.                "--add-comments",
  705.                "--directory\=\.",
  706.                "--output\=$MODULE\.pot-update",
  707.                "--files-from\=\.\/POTFILES\.in\.temp");
  708.     my $XGETTEXT_KEYWORDS = &FindPOTKeywords;
  709.     push @xgettext_argument, $XGETTEXT_KEYWORDS;
  710.     my $MSGID_BUGS_ADDRESS = &FindMakevarsBugAddress;
  711.     push @xgettext_argument, "--msgid-bugs-address\=$MSGID_BUGS_ADDRESS" if $MSGID_BUGS_ADDRESS;
  712.     push @xgettext_argument, "--from-code\=$encoding" if ($gettext_support_nonascii);
  713.     push @xgettext_argument, $XGETTEXT_ARGS if $XGETTEXT_ARGS;
  714.     my $xgettext_command = join ' ', @xgettext_argument;
  715.  
  716.     # intercept xgettext error message
  717.     print "Running $xgettext_command\n" if $VERBOSE;
  718.     my $xgettext_error_msg = `$xgettext_command 2>\&1`;
  719.     my $command_failed = $?;
  720.  
  721.     unlink "POTFILES.in.temp";
  722.  
  723.     print "Removing generated header (.h) files..." if $VERBOSE;
  724.     unlink foreach (@temp_headers);
  725.     print "done.\n" if $VERBOSE;
  726.  
  727.     if (! $command_failed)
  728.     {
  729.     if (! -e "$MODULE.pot-update")
  730.     {
  731.         print STDERR "None of the files in POTFILES.in contain strings marked for translation.\n";
  732.     }
  733.     if (-e "$MODULE.pot")
  734.     {
  735.         RemovePOHeader("$MODULE.pot", "$MODULE.1po");
  736.         RemovePOHeader("$MODULE.pot-update", "$MODULE.2po");
  737.         system("cmp", "-s", "$MODULE.1po", "$MODULE.2po");
  738.         rename("$MODULE.pot-update", "$MODULE.pot") if $?;
  739.         unlink "$MODULE.1po", "$MODULE.2po", "$MODULE.pot-update";
  740.     }
  741.     else
  742.     {
  743.         rename("$MODULE.pot-update", "$MODULE.pot");
  744.     }
  745.     print "Wrote $MODULE.pot\n" if $VERBOSE;
  746.     }
  747.     else
  748.     {
  749.     if ($xgettext_error_msg =~ /--from-code/)
  750.     {
  751.         # replace non-ASCII error message with a more useful one.
  752.         print STDERR "ERROR: xgettext failed to generate PO template file because there is non-ASCII\n".
  753.              "       string marked for translation. Please make sure that all strings marked\n".
  754.              "       for translation are in uniform encoding (say UTF-8), then \e[1m*prepend*\e[0m the\n".
  755.              "       following line to POTFILES.in and rerun $PROGRAM:\n\n".
  756.              "           [encoding: UTF-8]\n\n";
  757.     }
  758.     else
  759.     {
  760.         print STDERR "$xgettext_error_msg";
  761.         if (-e "$MODULE.pot-update")
  762.         {
  763.         # is this possible?
  764.         print STDERR "ERROR: xgettext failed but still managed to generate PO template file.\n".
  765.                  "       Please consult error message above if there is any.\n";
  766.         }
  767.         else
  768.         {
  769.         print STDERR "ERROR: xgettext failed to generate PO template file. Please consult\n".
  770.                  "       error message above if there is any.\n";
  771.         }
  772.     }
  773.     exit (1);
  774.     }
  775. }
  776.  
  777. sub RemovePOHeader
  778. {
  779.     my ($infile, $outfile) = @_;
  780.     my $first = 1;
  781.  
  782.     open (IN, "< $infile") || die "file $infile does not exist";
  783.     open (OUT, "> $outfile") || die "Unable to write into $outfile: $!";
  784.     local $/ = "\n\n";
  785.     while (<IN>) {
  786.         next if m/^msgid ""\nmsgstr/m;
  787.         print OUT;
  788.     }
  789.     close IN;
  790.     close OUT;
  791. }
  792.  
  793. sub POFile_Update
  794. {
  795.     -f "$MODULE.pot" or die "$PROGRAM: $MODULE.pot does not exist.\n";
  796.  
  797.     my $MSGMERGE = $ENV{"MSGMERGE"} || "/usr/bin/msgmerge";
  798.     my ($lang, $outfile) = @_;
  799.  
  800.     print "Merging $SRCDIR/$lang.po with $MODULE.pot..." if $VERBOSE;
  801.  
  802.     my $infile = "$SRCDIR/$lang.po";
  803.     $outfile = "$SRCDIR/$lang.po" if ($outfile eq "");
  804.  
  805.     # I think msgmerge won't overwrite old file if merge is not successful
  806.     system ("$MSGMERGE", "-q", "-o", $outfile, $infile, "$MODULE.pot");
  807. }
  808.  
  809. sub Console_WriteError_NotExisting
  810. {
  811.     my ($file) = @_;
  812.  
  813.     ## Report error if supplied language file is non-existing
  814.     print STDERR "$PROGRAM: $file does not exist!\n";
  815.     print STDERR "Try '$PROGRAM --help' for more information.\n";
  816.     exit;
  817. }
  818.  
  819. sub GatherPOFiles
  820. {
  821.     my @po_files = glob ("./*.po");
  822.  
  823.     @languages = map (&POFile_GetLanguage, @po_files);
  824.  
  825.     foreach my $lang (@languages) 
  826.     {
  827.     $po_files_by_lang{$lang} = shift (@po_files);
  828.     }
  829. }
  830.  
  831. sub POFile_GetLanguage ($)
  832. {
  833.     s/^(.*\/)?(.+)\.po$/$2/;
  834.     return $_;
  835. }
  836.  
  837. sub Console_Write_TranslationStatus
  838. {
  839.     my ($lang, $output_file) = @_;
  840.     my $MSGFMT = $ENV{"MSGFMT"} || "/usr/bin/msgfmt";
  841.  
  842.     $output_file = "$SRCDIR/$lang.po" if ($output_file eq "");
  843.  
  844.     system ("$MSGFMT", "-o", "$devnull", "--verbose", $output_file);
  845. }
  846.  
  847. sub Console_Write_CoverageReport
  848. {
  849.     my $MSGFMT = $ENV{"MSGFMT"} || "/usr/bin/msgfmt";
  850.  
  851.     &GatherPOFiles;
  852.  
  853.     foreach my $lang (@languages) 
  854.     {
  855.     print "$lang: ";
  856.     &POFile_Update ($lang, "");
  857.     }
  858.  
  859.     print "\n\n * Current translation support in $MODULE \n\n";
  860.  
  861.     foreach my $lang (@languages)
  862.     {
  863.     print "$lang: ";
  864.     system ("$MSGFMT", "-o", "$devnull", "--verbose", "$SRCDIR/$lang.po");
  865.     }
  866. }
  867.  
  868. sub SubstituteVariable
  869. {
  870.     my ($str) = @_;
  871.     
  872.     # always need to rewind file whenever it has been accessed
  873.     seek (CONF, 0, 0);
  874.  
  875.     # cache each variable. varhash is global to we can add
  876.     # variables elsewhere.
  877.     while (<CONF>)
  878.     {
  879.     if (/^(\w+)=(.*)$/)
  880.     {
  881.         ($varhash{$1} = $2) =~  s/^["'](.*)["']$/$1/;
  882.     }
  883.     }
  884.     
  885.     if ($str =~ /^(.*)\${?([A-Z_]+)}?(.*)$/)
  886.     {
  887.     my $rest = $3;
  888.     my $untouched = $1;
  889.     my $sub = "";
  890.         # Ignore recursive definitions of variables
  891.         $sub = $varhash{$2} if defined $varhash{$2} and $varhash{$2} !~ /\${?$2}?/;
  892.  
  893.     return SubstituteVariable ("$untouched$sub$rest");
  894.     }
  895.     
  896.     # We're using Perl backticks ` and "echo -n" here in order to 
  897.     # expand any shell escapes (such as backticks themselves) in every variable
  898.     return echo_n ($str);
  899. }
  900.  
  901. sub CONF_Handle_Open
  902. {
  903.     my $base_dirname = getcwd();
  904.     $base_dirname =~ s@.*/@@;
  905.  
  906.     my ($conf_in, $src_dir);
  907.  
  908.     if ($base_dirname =~ /^po(-.+)?$/) 
  909.     {
  910.     if (-f "Makevars") 
  911.     {
  912.         my $makefile_source;
  913.  
  914.         local (*IN);
  915.         open (IN, "<Makevars") || die "can't open Makevars: $!";
  916.  
  917.         while (<IN>) 
  918.         {
  919.         if (/^top_builddir[ \t]*=/) 
  920.         {
  921.             $src_dir = $_;
  922.             $src_dir =~ s/^top_builddir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
  923.  
  924.             chomp $src_dir;
  925.                     if (-f "$src_dir" . "/configure.ac") {
  926.                         $conf_in = "$src_dir" . "/configure.ac" . "\n";
  927.                     } else {
  928.                         $conf_in = "$src_dir" . "/configure.in" . "\n";
  929.                     }
  930.             last;
  931.         }
  932.         }
  933.         close IN;
  934.  
  935.         $conf_in || die "Cannot find top_builddir in Makevars.";
  936.     }
  937.     elsif (-f "../configure.ac") 
  938.     {
  939.         $conf_in = "../configure.ac";
  940.     } 
  941.     elsif (-f "../configure.in") 
  942.     {
  943.         $conf_in = "../configure.in";
  944.     } 
  945.     else 
  946.     {
  947.         my $makefile_source;
  948.  
  949.         local (*IN);
  950.         open (IN, "<Makefile") || return;
  951.  
  952.         while (<IN>) 
  953.         {
  954.         if (/^top_srcdir[ \t]*=/) 
  955.         {
  956.             $src_dir = $_;            
  957.             $src_dir =~ s/^top_srcdir[ \t]*=[ \t]*([^ \t\n\r]*)/$1/;
  958.  
  959.             chomp $src_dir;
  960.             $conf_in = "$src_dir" . "/configure.in" . "\n";
  961.  
  962.             last;
  963.         }
  964.         }
  965.         close IN;
  966.  
  967.         $conf_in || die "Cannot find top_srcdir in Makefile.";
  968.     }
  969.  
  970.     open (CONF, "<$conf_in");
  971.     }
  972.     else
  973.     {
  974.     print STDERR "$PROGRAM: Unable to proceed.\n" .
  975.              "Make sure to run this script inside the po directory.\n";
  976.     exit;
  977.     }
  978. }
  979.  
  980. sub FindPackageName
  981. {
  982.     my $version;
  983.     my $domain = &FindMakevarsDomain;
  984.     my $name = $domain || "untitled";
  985.  
  986.     &CONF_Handle_Open;
  987.  
  988.     my $conf_source; {
  989.     local (*IN);
  990.     open (IN, "<&CONF") || return $name;
  991.     seek (IN, 0, 0);
  992.     local $/; # slurp mode
  993.     $conf_source = <IN>;
  994.     close IN;
  995.     }
  996.  
  997.     # priority for getting package name:
  998.     # 1. GETTEXT_PACKAGE
  999.     # 2. first argument of AC_INIT (with >= 2 arguments)
  1000.     # 3. first argument of AM_INIT_AUTOMAKE (with >= 2 argument)
  1001.  
  1002.     # /^AM_INIT_AUTOMAKE\([\s\[]*([^,\)\s\]]+)/m 
  1003.     # the \s makes this not work, why?
  1004.     if ($conf_source =~ /^AM_INIT_AUTOMAKE\(([^,\)]+),([^,\)]+)/m)
  1005.     {
  1006.     ($name, $version) = ($1, $2);
  1007.     $name    =~ s/[\[\]\s]//g;
  1008.     $version =~ s/[\[\]\s]//g;
  1009.     $varhash{"PACKAGE_NAME"} = $name if (not $name =~ /\${?AC_PACKAGE_NAME}?/);
  1010.     $varhash{"PACKAGE"} = $name if (not $name =~ /\${?PACKAGE}?/);
  1011.     $varhash{"PACKAGE_VERSION"} = $version if (not $name =~ /\${?AC_PACKAGE_VERSION}?/);
  1012.     $varhash{"VERSION"} = $version if (not $name =~ /\${?VERSION}?/);
  1013.     }
  1014.     
  1015.     if ($conf_source =~ /^AC_INIT\(([^,\)]+),([^,\)]+)/m) 
  1016.     {
  1017.     ($name, $version) = ($1, $2);
  1018.     $name    =~ s/[\[\]\s]//g;
  1019.     $version =~ s/[\[\]\s]//g;
  1020.     $varhash{"PACKAGE_NAME"} = $name if (not $name =~ /\${?AC_PACKAGE_NAME}?/);
  1021.     $varhash{"PACKAGE"} = $name if (not $name =~ /\${?PACKAGE}?/);
  1022.     $varhash{"PACKAGE_VERSION"} = $version if (not $name =~ /\${?AC_PACKAGE_VERSION}?/);
  1023.     $varhash{"VERSION"} = $version if (not $name =~ /\${?VERSION}?/);
  1024.     }
  1025.  
  1026.     # \s makes this not work, why?
  1027.     $name = $1 if $conf_source =~ /^GETTEXT_PACKAGE=\[?([^\n\]]+)/m;
  1028.     
  1029.     # m4 macros AC_PACKAGE_NAME, AC_PACKAGE_VERSION etc. have same value
  1030.     # as corresponding $PACKAGE_NAME, $PACKAGE_VERSION etc. shell variables.
  1031.     $name =~ s/\bAC_PACKAGE_/\$PACKAGE_/g;
  1032.  
  1033.     $name = $domain if $domain;
  1034.  
  1035.     $name = SubstituteVariable ($name);
  1036.     $name =~ s/^["'](.*)["']$/$1/;
  1037.  
  1038.     return $name if $name;
  1039. }
  1040.  
  1041.  
  1042. sub FindPOTKeywords
  1043. {
  1044.  
  1045.     my $keywords = "--keyword\=\_ --keyword\=N\_ --keyword\=U\_ --keyword\=Q\_";
  1046.     my $varname = "XGETTEXT_OPTIONS";
  1047.     my $make_source; {
  1048.     local (*IN);
  1049.     open (IN, "<Makevars") || (open(IN, "<Makefile.in.in") && ($varname = "XGETTEXT_KEYWORDS")) || return $keywords;
  1050.     seek (IN, 0, 0);
  1051.     local $/; # slurp mode
  1052.     $make_source = <IN>;
  1053.     close IN;
  1054.     }
  1055.  
  1056.     $keywords = $1 if $make_source =~ /^$varname[ ]*=\[?([^\n\]]+)/m;
  1057.     
  1058.     return $keywords;
  1059. }
  1060.  
  1061. sub FindMakevarsDomain
  1062. {
  1063.  
  1064.     my $domain = "";
  1065.     my $makevars_source; { 
  1066.     local (*IN);
  1067.     open (IN, "<Makevars") || return $domain;
  1068.     seek (IN, 0, 0);
  1069.     local $/; # slurp mode
  1070.     $makevars_source = <IN>;
  1071.     close IN;
  1072.     }
  1073.  
  1074.     $domain = $1 if $makevars_source =~ /^DOMAIN[ ]*=\[?([^\n\]\$]+)/m;
  1075.     $domain =~ s/^\s+//;
  1076.     $domain =~ s/\s+$//;
  1077.     
  1078.     return $domain;
  1079. }
  1080.  
  1081. sub FindMakevarsBugAddress
  1082. {
  1083.  
  1084.     my $address = "";
  1085.     my $makevars_source; { 
  1086.     local (*IN);
  1087.     open (IN, "<Makevars") || return undef;
  1088.     seek (IN, 0, 0);
  1089.     local $/; # slurp mode
  1090.     $makevars_source = <IN>;
  1091.     close IN;
  1092.     }
  1093.  
  1094.     $address = $1 if $makevars_source =~ /^MSGID_BUGS_ADDRESS[ ]*=\[?([^\n\]\$]+)/m;
  1095.     $address =~ s/^\s+//;
  1096.     $address =~ s/\s+$//;
  1097.     
  1098.     return $address;
  1099. }
  1100.