#!/usr/bin/perl -w # (c) Copyright 2012. Francois Gouget. use strict; my $name0=$0; $name0 =~ s+^.*/++; my $opt_file; my $usage; while (@ARGV > 0) { my $arg=shift @ARGV; if ($arg eq "-?" or $arg eq "-h" or $arg eq "--help") { $usage=0; } elsif ($arg =~ /^-/) { print STDERR "$name0:error: unknown '$arg' option\n"; $usage=2; last; } elsif (!defined $opt_file) { $opt_file=$arg; } else { print STDERR "$name0:error: unknown '$arg' option\n"; $usage=2; last; } } if (!defined $usage) { if (!defined $opt_file) { print STDERR "$name0:error: you must specify the path to the PO file to fix\n"; $usage=2; } } if (defined $usage) { if ($usage) { print STDERR "$name0:error: try '$name0 --help' for more information\n"; exit $usage; } print "Usage: $name0 PO_FILE [--help]\n"; print "\n"; print "Adds a full stop at the end of the winerror.mc messages,\n"; print "\n"; print "Options:\n"; print " PO_FILE The source PO file\n"; print " --help, -h Shows this help message\n"; exit 0; } my $fh; if (!open($fh, "<:encoding(utf-8)", $opt_file)) { print STDERR "$name0:error: unable to open '$opt_file' for reading: $!\n"; exit 1; } my $fstop_re='(?:\.|\x{3002})'; my $fstop=($opt_file =~ /(?:ja|zh)/ ? "\x{3002}" : '.'); my $change=0; my $state=""; my @lines; sub fix_last_line() { if (@lines and $lines[-1] =~ /\"\"\n$/) { # Nothing to do } elsif ($change and $state eq "msgid") { $lines[-1] =~ s/(?:\\n)?\"\n$/.\\n\"\n/ if ($lines[-1] !~ /$fstop_re(?:\\n)?\"\n$/); } elsif ($change and $state eq "msgstr") { $lines[-1] =~ s/(?:\\n)?\"\n$/$fstop\\n\"\n/ if ($lines[-1] !~ /$fstop_re(?:\\n)?\"\n$/); } } while (my $line=<$fh>) { if ($line =~ /^#:.* winerror\.mc:/) { fix_last_line(); # This message comes from winerror.mc $change=1; $state=""; } elsif ($line =~ /^\s*$/) { fix_last_line(); $change=0 if ($state eq "msgstr"); $state=""; } elsif ($line =~ /^msgid /) { fix_last_line(); $state="msgid"; } elsif ($line =~ /^msgstr /) { fix_last_line(); $state="msgstr"; } elsif ($line =~ /^\"/) { # Just continuing the current msgid / msgstr # Nothing to do } else { fix_last_line(); $change=0 if ($state ne ""); $state=""; } push @lines, $line; } if ($change and $lines[-1] =~ /^(?:msg|\")/) { # This is an msgstr -> end-of-file boundary so fix that msgstr's last line $lines[-1] =~ s/\\n\"\n$/$fstop\\n\"\n/ if ($lines[-1] !~ /$fstop_re\\n\"\n$/); } close($fh); if (!open($fh, ">:encoding(utf-8)", $opt_file)) { print STDERR "$name0:error: unable to open '$opt_file' for writing: $!\n"; exit 1; } print $fh @lines; close($fh);