This file is indexed.

/usr/share/perl5/Locale/gettext_pp.pm is in libintl-perl 1.24-1build1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  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
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
#! /bin/false

# vim: set autoindent shiftwidth=4 tabstop=4:

# Pure Perl implementation of Uniforum message translation.
# Copyright (C) 2002-2015 Guido Flohr <guido.flohr@cantanea.com>,
# all rights reserved.

# This program 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 3 of the License, or
# (at your option) any later version.

# This program 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 program.  If not, see <http://www.gnu.org/licenses/>.

package Locale::gettext_pp;

use strict;

require 5.004;

use vars qw ($__gettext_pp_default_dir 
    		 $__gettext_pp_textdomain
    		 $__gettext_pp_domain_bindings
    		 $__gettext_pp_domain_codeset_bindings
    		 $__gettext_pp_domains
    		 $__gettext_pp_recoders
    		 $__gettext_pp_unavailable_dirs
    		 $__gettext_pp_domain_cache
    		 $__gettext_pp_alias_cache
    		 $__gettext_pp_context_glue);

use locale;
use File::Spec;

BEGIN {
    $__gettext_pp_textdomain = 'messages';
    $__gettext_pp_domain_bindings = {};
    $__gettext_pp_domain_codeset_bindings = {};
    $__gettext_pp_domains = {};
    $__gettext_pp_recoders = {};
    $__gettext_pp_unavailable_dirs = {};
    $__gettext_pp_domain_cache = {};
    $__gettext_pp_alias_cache = {};
    # The separator between msgctxt and msgid in a .mo file.  */
    $__gettext_pp_context_glue = "\004";
    
    $__gettext_pp_default_dir = '';
    
    for my $dir (qw (/usr/share/locale /usr/local/share/locale)) {
    	if (-d $dir) {
    		$__gettext_pp_default_dir = $dir;
    		last;
    	}
    }
}

BEGIN {
    	require POSIX;
    	require Exporter;
    	use IO::Handle;
    	require Locale::Recode;

    	local $@;
    	my ($has_messages, $five_ok);
    	
    	$has_messages = eval '&POSIX::LC_MESSAGES';

    	unless (defined $has_messages && length $has_messages) {
    			$five_ok = ! grep {my $x = eval "&POSIX::$_" || 0; $x eq '5';}
    							qw (LC_CTYPE
    							   LC_NUMERIC
    							   LC_TIME
    							   LC_COLLATE
    							   LC_MONETARY
    							   LC_ALL);
    		if ($five_ok) {
    			$five_ok = POSIX::setlocale (5, '');
    		}
    	}
    	
    	if (defined $has_messages && length $has_messages) {
eval <<'EOF';
sub LC_MESSAGES()
{
    local $!; # Do not clobber errno!
    
    return &POSIX::LC_MESSAGES;
}
EOF
    	} elsif ($five_ok) {
eval <<'EOF';
sub LC_MESSAGES()
{
    local $!; # Do not clobber errno!

    # Hack: POSIX.pm deems LC_MESSAGES an invalid macro until
    # Perl 5.8.0.  However, on LC_MESSAGES should be 5 ...
    return 5;
}
EOF
    	} else {
eval <<'EOF';
sub LC_MESSAGES()
{
    local $!; # Do not clobber errno!

    # This fallback value is widely used,
    # when LC_MESSAGES is not available.
    return 1729;
}
EOF
    	}
}

use vars qw (%EXPORT_TAGS @EXPORT_OK @ISA $VERSION);

%EXPORT_TAGS = (locale_h => [ qw (
    							  gettext
    							  dgettext
    							  dcgettext
    							  ngettext
    							  dngettext
    							  dcngettext
    							  pgettext
    							  dpgettext
    							  dcpgettext
    							  npgettext
    							  dnpgettext
    							  dcnpgettext
    							  textdomain
    							  bindtextdomain
    							  bind_textdomain_codeset
    							  )
    						  ],
    			libintl_h => [ qw (LC_CTYPE
    							   LC_NUMERIC
    							   LC_TIME
    							   LC_COLLATE
    							   LC_MONETARY
    							   LC_MESSAGES
    							   LC_ALL)
    						   ],
    			);

@EXPORT_OK = qw (gettext
    			 dgettext
    			 dcgettext
    			 ngettext
    			 dngettext
    			 dcngettext
    			 pgettext
    			 dpgettext
    			 dcpgettext
    			 npgettext
    			 dnpgettext
    			 dcnpgettext
    			 textdomain
    			 bindtextdomain
    			 bind_textdomain_codeset
                 nl_putenv
                 setlocale
    			 LC_CTYPE
    			 LC_NUMERIC
    			 LC_TIME
    			 LC_COLLATE
    			 LC_MONETARY
    			 LC_MESSAGES
    			 LC_ALL);
@ISA = qw (Exporter);

my $has_nl_langinfo;

sub __load_catalog;
sub __load_domain;
sub __locale_category;
sub __untaint_plural_header;
sub __compile_plural_function;

sub LC_NUMERIC()
{
    &POSIX::LC_NUMERIC;
}

sub LC_CTYPE()
{
    &POSIX::LC_CTYPE;
}

sub LC_TIME()
{
    &POSIX::LC_TIME;
}

sub LC_COLLATE()
{
    &POSIX::LC_COLLATE;
}

sub LC_MONETARY()
{
    &POSIX::LC_MONETARY;
}

sub LC_ALL()
{
    &POSIX::LC_ALL;
}

sub textdomain(;$)
{
    my $new_domain = shift;
    
    $__gettext_pp_textdomain = $new_domain if defined $new_domain && 
    	length $new_domain;
    
    return $__gettext_pp_textdomain;
}

sub bindtextdomain($;$)
{
    my ($domain, $directory) = @_;

    my $retval;	
    if (defined $domain && length $domain) {
    	if (defined $directory && length $directory) {
    		$retval = $__gettext_pp_domain_bindings->{$domain} 
    			= $directory;
    	} elsif (exists $__gettext_pp_domain_bindings->{$domain}) {
    		$retval = $__gettext_pp_domain_bindings->{$domain};
    	} else {
    		$retval = $__gettext_pp_default_dir;
    	}
    	$retval = '/usr/share/locale' unless defined $retval && 
    		length $retval;
    	return $retval;
    } else {
    	return;
    }
}

sub bind_textdomain_codeset($;$)
{
    my ($domain, $codeset) = @_;
    
    if (defined $domain && length $domain) {
    	if (defined $codeset && length $codeset) {
    		return $__gettext_pp_domain_codeset_bindings->{$domain} = $codeset;
    	} elsif (exists $__gettext_pp_domain_codeset_bindings->{$domain}) {
    		return $__gettext_pp_domain_codeset_bindings->{$domain};
    	}
    }
    
    return;
}

sub gettext($)
{
    my ($msgid) = @_;

    return dcnpgettext ('', undef, $msgid, undef, undef, undef);
}

sub dgettext($$)
{
    my ($domainname, $msgid) = @_;

    return dcnpgettext ($domainname, undef, $msgid, undef, undef, undef);
}

sub dcgettext($$$)
{
    my ($domainname, $msgid, $category) = @_;

    return dcnpgettext ($domainname, undef, $msgid, undef, undef, undef);
}

sub ngettext($$$)
{
    my ($msgid, $msgid_plural, $n) = @_;

    return dcnpgettext ('', undef, $msgid, $msgid_plural, $n, undef);
}

sub dngettext($$$$)
{
    my ($domainname, $msgid, $msgid_plural, $n) = @_;

    return dcnpgettext ($domainname, undef, $msgid, $msgid_plural, $n, undef);
}

sub dcngettext($$$$$)
{
    my ($domainname, $msgid, $msgid_plural, $n, $category) = @_;

    return dcnpgettext ($domainname, undef, $msgid, $msgid_plural, $n, , $category);
}


sub pgettext($$)
{
    my ($msgctxt, $msgid) = @_;

    return dcnpgettext ('', $msgctxt, $msgid, undef, undef, undef);
}

sub dpgettext($$$)
{
    my ($domainname, $msgctxt, $msgid) = @_;

    return dcnpgettext ($domainname, $msgctxt, $msgid, undef, undef, undef);
}

sub dcpgettext($$$$)
{
    my ($domainname, $msgctxt, $msgid, $category) = @_;

    return dcnpgettext ($domainname, $msgctxt, $msgid, undef, undef, undef);
}

sub npgettext($$$$)
{
    my ($msgctxt, $msgid, $msgid_plural, $n) = @_;

    return dcnpgettext ('', $msgctxt, $msgid, $msgid_plural, $n, undef);
}

sub dnpgettext($$$$$)
{
    my ($domainname, $msgctxt, $msgid, $msgid_plural, $n) = @_;

    return dcnpgettext ($domainname, $msgctxt, $msgid, $msgid_plural, $n, undef);
}

# This is the actual implementation of dncpgettext.  It is also used by the
# corresponding function in Locale::gettext_dumb.
sub _dcnpgettext_impl {
    my ($domainname, $msgctxt, $msgid, $msgid_plural, $n, $category,
        $locale) = @_;

    return unless defined $msgid;

    my $plural = defined $msgid_plural;
    my $msg_ctxt_id = defined $msgctxt ? join($__gettext_pp_context_glue, ($msgctxt, $msgid)) : $msgid;
    
    local $!; # Do not clobber errno!
    
    # This is also done in __load_domain but we need a proper value.
    $domainname = $__gettext_pp_textdomain
    	unless defined $domainname && length $domainname;
    
    # Category is always LC_MESSAGES (other categories are ignored).
    my $category_name = 'LC_MESSAGES';
    $category = LC_MESSAGES;

    my $domains = __load_domain ($domainname, $category, $category_name,
                                 $locale);
    
    my @trans = ();
    my $domain;
    my $found;
    foreach my $this_domain (@$domains) {
    	if ($this_domain && defined $this_domain->{messages}->{$msg_ctxt_id}) {
    		@trans = @{$this_domain->{messages}->{$msg_ctxt_id}};
    		shift @trans;
    		$domain = $this_domain;
    		$found = 1;
    		last;
    	}
    }
    @trans = ($msgid, $msgid_plural) unless @trans;
    
    my $trans = $trans[0];
    if ($plural) {
    	if ($domain) {
    		my $nplurals = 0;
    		($nplurals, $plural) = &{$domain->{plural_func}} ($n);
    		$plural = 0 unless defined $plural;
    		$nplurals = 0 unless defined $nplurals;
    		$plural = 0 if $nplurals <= $plural;
    	} else {
    		$plural = $n != 1 || 0;
    	}
    	
    	$trans = $trans[$plural] if defined $trans[$plural];
    }
    
    if ($found && defined $domain->{po_header}->{charset}) {
    	my $input_codeset = $domain->{po_header}->{charset};
    	# Convert into output charset.
    	my $output_codeset = $__gettext_pp_domain_codeset_bindings->{$domainname};

    	$output_codeset = $ENV{OUTPUT_CHARSET} unless defined $output_codeset;
    	$output_codeset = __get_codeset ($category, $category_name,
    									 $domain->{locale_id})
    		unless defined $output_codeset;
    	
    	unless (defined $output_codeset) {
    		# Still no point.
    		my $lc_ctype = __locale_category (POSIX::LC_CTYPE(), 
    									   'LC_CTYPE');
    		$output_codeset = $1
    			if $lc_ctype =~ /^[a-z]{2}(?:_[A-Z]{2})?\.([^@]+)/;
    	}

    	# No point. :-(
    	$output_codeset = $domain->{po_header}->{charset}
    		unless defined $output_codeset;
    
    	if (exists $__gettext_pp_domain_cache->{$output_codeset}) {
    		$output_codeset = $__gettext_pp_domain_cache->{$output_codeset};
    	} else {
    		$output_codeset = 'utf-8' if lc $output_codeset eq 'utf8';
    		$output_codeset = 
    			$__gettext_pp_domain_cache->{$output_codeset} =
    			Locale::Recode->resolveAlias ($output_codeset);
    	}
    	
    	if (defined $output_codeset &&
    		$output_codeset ne $domain->{po_header}->{charset}) {
    		# We have to convert.
    		my $recoder;
    		
    		if (exists 
    			$__gettext_pp_recoders->{$input_codeset}->{$output_codeset}) {
    			$recoder = $__gettext_pp_recoders->{$input_codeset}->{$output_codeset};
    		} else {
    			$recoder = 
    				$__gettext_pp_recoders->{$input_codeset}->{$output_codeset} =
    				Locale::Recode->new (from => $input_codeset,
    									 to => $output_codeset,
    									 );
    		}
    		
    		$recoder->recode ($trans);
    	}
    }
    
    return $trans;
}

sub dcnpgettext ($$$$$$) {
    return &_dcnpgettext_impl;
}

sub nl_putenv ($)
{
    my ($envspec) = @_;
    return unless defined $envspec;
    return unless length $envspec;
    return if substr ($envspec, 0, 1) eq '=';
    
    my ($var, $value) = split /=/, $envspec, 2;

    # In Perl we *could* set empty environment variables even under
    # MS-DOS, but for compatibility reasons, we implement the
    # brain-damaged behavior of the Microsoft putenv().
    if ($^O eq 'MSWin32') {
        $value = '' unless defined $value;
        if (length $value) {
            $ENV{$var} = $value;
        } else {
            delete $ENV{$var};
        }
    } else {
        if (defined $value) {
            $ENV{$var} = $value;
        } else {
            delete $ENV{$var};
        }
    }

    return 1;
}

sub setlocale($;$) {
	require POSIX;
	&POSIX::setlocale;
}

sub __load_domain
{
    my ($domainname, $category, $category_name, $locale) = @_;

        # If no locale was selected for the requested locale category,
        # l10n is disabled completely.  This matches the behavior of GNU
        # gettext.
        if ($category != LC_MESSAGES) {
            # Not supported.
            return [];
        }
        
        if (!defined $locale && $category != 1729) {
                $locale = POSIX::setlocale ($category);
                if (!defined $locale || 'C' eq $locale || 'POSIX' eq $locale) {
                        return [];
                }
        }
    
    $domainname = $__gettext_pp_textdomain
    	unless defined $domainname && length $domainname;

    my $dir = bindtextdomain ($domainname, '');
    $dir = $__gettext_pp_default_dir unless defined $dir && length $dir;
    return [] unless defined $dir && length $dir;

    my @locales;
    my $cache_key;

    if (defined $ENV{LANGUAGE} && length $ENV{LANGUAGE}) {
    	@locales = split /:/, $ENV{LANGUAGE};
    	$cache_key = $ENV{LANGUAGE};
    } elsif (!defined $locale) {
            # The system does not have LC_MESSAGES.  Guess the value.
    	@locales = $cache_key = __locale_category ($category, 
    	                                           $category_name);
    } else {
            @locales = $cache_key = $locale;
    }

    # Have we looked that one up already?
    my $domains = $__gettext_pp_domain_cache->{$dir}->{$cache_key}->{$category_name}->{$domainname};
    return $domains if defined $domains;
    return [] unless @locales;
    
    my @dirs = ($dir);
    my @tries = (@locales);
    my %locale_lookup = map { $_ => $_ } @tries;

    foreach my $locale (@locales) {
    	if ($locale =~ /^([a-z][a-z])
    		(?:(_[A-Z][A-Z])?
    		 (\.[-_A-Za-z0-9]+)?
    		 )?
    		(\@[-_A-Za-z0-9]+)?$/x) {
    		
    		if (defined $3) {
    			defined $2 ?
    				push @tries, $1 . $2 . $3 : push @tries, $1 . $3;
    		}
    		if (defined $2) {
    			push @tries, $1 . $2;
    			$locale_lookup{$1 . $2} = $locale;
    		}
    		if (defined $1) {
    			push @tries, $1 if defined $1;
    			$locale_lookup{$1} = $locale;
    		}
    	}
    }
    	push @dirs, $__gettext_pp_default_dir
    	if $__gettext_pp_default_dir && $dir ne $__gettext_pp_default_dir;
    
    my %seen = ();
    foreach my $basedir (@dirs) {
    	foreach my $try (@tries) {
    		my $fulldir = "$basedir/$try/$category_name";
    		
    		next if $seen{$fulldir}++;

    		# If the cache for unavailable directories is removed,
    		# the three lines below should be replaced by:
    		# 'next unless -d $fulldir;'
    		next if $__gettext_pp_unavailable_dirs->{$fulldir};
    		++$__gettext_pp_unavailable_dirs->{$fulldir} and next
    				unless -d $fulldir;
                        my $filename = File::Spec->catfile ($fulldir, 
                                                            "$domainname.mo");
    		my $domain = __load_catalog ($filename, $try);
    		next unless $domain;
    			
    		$domain->{locale_id} = $locale_lookup{$try};
    		push @$domains, $domain;
    	}
    }
    $__gettext_pp_domain_cache->{$dir}
                              ->{$cache_key}
                              ->{$category_name}
                              ->{$domainname} = $domains;

    $domains = [] unless defined $domains;
    
    return $domains;
}

sub __load_catalog
{
    my ($filename, $locale) = @_;
    
    # Alternatively we could check the filename for evil characters ...
    # (Important for CGIs).
    return unless -f $filename && -r $filename;
    
    local $/;
    local *HANDLE;
    
    open HANDLE, "<$filename"
    	or return;
    binmode HANDLE;
    my $raw = <HANDLE>;
    close HANDLE;
    
    # Corrupted?
    return if ! defined $raw || length $raw < 28;
    
    my $filesize = length $raw;
    
    # Read the magic number in order to determine the byte order.
    my $domain = {};
    my $unpack = 'N';
    $domain->{potter} = unpack $unpack, substr $raw, 0, 4;
    
    if ($domain->{potter} == 0xde120495) {
    	$unpack = 'V';
    } elsif ($domain->{potter} != 0x950412de) {
    	return;
    }
    my $domain_unpack = $unpack x 6;
    
    my ($revision, $num_strings, $msgids_off, $msgstrs_off,
    	$hash_size, $hash_off) = 
    		unpack (($unpack x 6), substr $raw, 4, 24);
    
    return unless $revision == 0; # Invalid revision number.
    
    $domain->{revision} = $revision;
    $domain->{num_strings} = $num_strings;
    $domain->{msgids_off} = $msgids_off;
    $domain->{msgstrs_off} = $msgstrs_off;
    $domain->{hash_size} = $hash_size;
    $domain->{hash_off} = $hash_off;
    
    return if $msgids_off + 4 * $num_strings > $filesize;
    return if $msgstrs_off + 4 * $num_strings > $filesize;
    
    my @orig_tab = unpack (($unpack x (2 * $num_strings)), 
    					   substr $raw, $msgids_off, 8 * $num_strings);
    my @trans_tab = unpack (($unpack x (2 * $num_strings)), 
    						substr $raw, $msgstrs_off, 8 * $num_strings);
    
    my $messages = {};
    
    for (my $count = 0; $count < 2 * $num_strings; $count += 2) {
    	my $orig_length = $orig_tab[$count];
    	my $orig_offset = $orig_tab[$count + 1];
    	my $trans_length = $trans_tab[$count];
    	my $trans_offset = $trans_tab[$count + 1];
    	
    	return if $orig_offset + $orig_length > $filesize;
    	return if $trans_offset + $trans_length > $filesize;
    	
    	my @origs = split /\000/, substr $raw, $orig_offset, $orig_length;
    	my @trans = split /\000/, substr $raw, $trans_offset, $trans_length;
    	
    	# The singular is the key, the plural plus all translations is the
    	# value.
    	my $msgid = $origs[0];
    	$msgid = '' unless defined $msgid && length $msgid;
    	my $msgstr = [ $origs[1], @trans ];
    	$messages->{$msgid} = $msgstr;
    }
    
    $domain->{messages} = $messages;
    
    # Try to find po header information.
    my $po_header = {};
    my $null_entry = $messages->{''}->[1];
    if ($null_entry) {
    	my @lines = split /\n/, $null_entry;
    	foreach my $line (@lines) {
    		my ($key, $value) = split /:/, $line, 2;
    		$key =~ s/-/_/g;
    		$po_header->{lc $key} = $value;
    	}
    }
    $domain->{po_header} = $po_header;
    
    if (exists $domain->{po_header}->{content_type}) {
    	my $content_type = $domain->{po_header}->{content_type};
    	if ($content_type =~ s/.*=//) {
    		$domain->{po_header}->{charset} = $content_type;
    	}
    }
    
    my $code = $domain->{po_header}->{plural_forms} || '';
    
    # Whitespace, locale-independent.
    my $s = '[ \t\r\n\013\014]';

    # Untaint the plural header.
    # Keep line breaks as is (Perl 5_005 compatibility).
    $code = $domain->{po_header}->{plural_forms} 
        = __untaint_plural_header $code;

    $domain->{plural_func} = __compile_plural_function $code;

    unless (defined $domain->{po_header}->{charset} 
            && length $domain->{po_header}->{charset} 
            && $locale =~ /^(?:[a-z][a-z])
                            (?:(?:_[A-Z][A-Z])?
                             (\.[-_A-Za-z0-9]+)?
                            )?
                            (?:\@[-_A-Za-z0-9]+)?$/x) {
        $domain->{po_header}->{charset} = $1;
    }
                                
    if (defined $domain->{po_header}->{charset}) {
        $domain->{po_header}->{charset} = 
            Locale::Recode->resolveAlias ($domain->{po_header}->{charset});
    }
    
    return $domain;
}

sub __locale_category
{
    my ($category, $category_name) = @_;
    
    local $@;
    my $value = eval {POSIX::setlocale ($category)};
    
    # We support only XPG syntax, i. e.
    # language[_territory[.codeset]][@modifier].
    undef $value unless (defined $value && 
    					 length $value &&
    					 $value =~ /^[a-z][a-z]
    					 (?:_[A-Z][A-Z]
    					  (?:\.[-_A-Za-z0-9]+)?
    					  )?
    					 (?:\@[-_A-Za-z0-9]+)?$/x);

    unless ($value) {
    	$value = $ENV{LC_ALL};
    	$value = $ENV{$category_name} unless defined $value && length $value;
    	$value = $ENV{LANG} unless defined $value && length $value;
    	return 'C' unless defined $value && length $value;
    }
    
    return $value if $value ne 'C' && $value ne 'POSIX';
}

sub __get_codeset
{
    my ($category, $category_name, $locale_id) = @_;

    local $@;
    unless (defined $has_nl_langinfo) {
    	eval {
    		require I18N::Langinfo;
    	};
    	$has_nl_langinfo = !$@;
    }

    if ($has_nl_langinfo) {
    	# Try to set the locale via the specified id.
    	my $saved_locale = eval { POSIX::setlocale (LC_ALL) };
    	my $had_lc_all = exists $ENV{LC_ALL};
    	my $saved_lc_all = $ENV{LC_ALL} if $had_lc_all;

    	# Now try to set the locale via the environment.  There is no
    	# point in calling the langinfo routines if this fails.
    	$ENV{LC_ALL} = $locale_id;
    	my $codeset;
    	my $lc_all = eval { POSIX::setlocale (LC_ALL, $locale_id); };
    	$codeset = I18N::Langinfo::langinfo (I18N::Langinfo::CODESET())
    		if defined $lc_all;

        # Restore environment.
    	if ($saved_locale) {
    		eval { POSIX::setlocale (LC_ALL, $saved_locale); }
    	}
    	if ($had_lc_all) {
            $ENV{LC_ALL} = $saved_lc_all if $had_lc_all;
    	} else {
    	    delete $ENV{LC_ALL};
    	}
    	return $codeset;
    }

    return;
}
    
sub __untaint_plural_header {
    my ($code) = @_;

    # Whitespace, locale-independent.
    my $s = '[ \t\r\n\013\014]';

    if ($code =~ m{^($s*
    				 nplurals$s*=$s*[0-9]+
    				 $s*;$s*
    				 plural$s*=$s*(?:$s|[-\?\|\&=!<>+*/\%:;a-zA-Z0-9_\(\)])+
    				 )}xms) {
    	return $1;
    }

    return '';    
}

sub __compile_plural_function {
    my ($code) = @_;

    # The leading and trailing space is necessary to be able to match
    # against word boundaries.
    my $plural_func;
    
    if (length $code) {
    	my $code = ' ' . $code . ' ';
    	$code =~ 
    		s/(?<=[^_a-zA-Z0-9])[_a-z][_A-Za-z0-9]*(?=[^_a-zA-Z0-9])/\$$&/gs;
    	
    	$code = "sub { my \$n = shift || 0; 
    			   my (\$plural, \$nplurals); 
    			   $code; 
    			   return (\$nplurals, \$plural ? \$plural : 0); }";
    	
    	# Now try to evaluate the code.	 There is no need to run the code in
    	# a Safe compartment.  The above substitutions should have destroyed
    	# all evil code.  Corrections are welcome!
        #warn $code;
    	$plural_func = eval $code;
        #warn $@ if $@;
    	undef $plural_func if $@;
    }
        
    # Default is Germanic plural (which is incorrect for French).
    $plural_func = eval "sub { (2, 1 != shift || 0) }" unless $plural_func;

    return $plural_func;    
}

1;

__END__

=head1 NAME

Locale::gettext_pp - Pure Perl Implementation of Uniforum Message Translation

=head1 SYNOPSIS

 use Locale::gettext_pp (:locale_h :libintl_h);

 gettext $msgid;
 dgettext $domainname, $msgid;
 dcgettext $domainname, $msgid, LC_MESSAGES;
 ngettext $msgid, $msgid_plural, $count;
 dngettext $domainname, $msgid, $msgid_plural, $count;
 dcngettext $domainname, $msgid, $msgid_plural, $count, LC_MESSAGES;
 pgettext $msgctxt, $msgid;
 dpgettext $domainname, $msgctxt, $msgid;
 dcpgettext $domainname, $msgctxt, $msgid, LC_MESSAGES;
 npgettext $msgctxt, $msgid, $msgid_plural, $count;
 dnpgettext $domainname, $msgctxt, $msgid, $msgid_plural, $count;
 dcnpgettext $domainname, $msgctxt, $msgid, $msgid_plural, $count, LC_MESSAGES;
 textdomain $domainname;
 bindtextdomain $domainname, $directory;
 bind_textdomain_codeset $domainname, $encoding;
 my $category = LC_CTYPE;
 my $category = LC_NUMERIC;
 my $category = LC_TIME;
 my $category = LC_COLLATE;
 my $category = LC_MONETARY;
 my $category = LC_MESSAGES;
 my $category = LC_ALL;

=head1 DESCRIPTION

The module B<Locale::gettext_pp> is the low-level interface to 
message translation according to the Uniforum approach that is
for example used in GNU gettext and Sun's Solaris.

Normally you should not use this module directly, but the high
level interface Locale::TextDomain(3) that provides a much simpler
interface.    This description is therefore deliberately kept
brief.    Please refer to the GNU gettext documentation available at
L<http://www.gnu.org/manual/gettext/> for in-depth and background 
information on the topic.

=head1 FUNCTIONS

The module exports by default nothing.    Every function has to be
imported explicitely or via an export tag (L<"EXPORT TAGS">).

=over 4

=item B<gettext MSGID>

See L<Locale::Messages/FUNCTIONS>.

=item B<dgettext TEXTDOMAIN, MSGID>

See L<Locale::Messages/FUNCTIONS>.

=item B<dcgettext TEXTDOMAIN, MSGID, CATEGORY>

See L<Locale::Messages/FUNCTIONS>.

=item B<ngettext MSGID, MSGID_PLURAL, COUNT>

See L<Locale::Messages/FUNCTIONS>.

=item B<dngettext TEXTDOMAIN, MSGID, MSGID_PLURAL, COUNT>

See L<Locale::Messages/FUNCTIONS>.

=item B<dcngettext TEXTDOMAIN, MSGID, MSGID_PLURAL, COUNT, CATEGORY>

See L<Locale::Messages/FUNCTIONS>.

=item B<pgettext MSGCTXT, MSGID>

See L<Locale::Messages/FUNCTIONS>.

=item B<dpgettext TEXTDOMAIN, MSGCTXT, MSGID>

See L<Locale::Messages/FUNCTIONS>.

=item B<dcpgettext TEXTDOMAIN, MSGCTXT, MSGID, CATEGORY>

See L<Locale::Messages/FUNCTIONS>.

=item B<npgettext MSGCTXT, MSGID, MSGID_PLURAL, COUNT>

See L<Locale::Messages/FUNCTIONS>.

=item B<dnpgettext TEXTDOMAIN, MSGCTXT, MSGID, MSGID_PLURAL, COUNT>

See L<Locale::Messages/FUNCTIONS>.

=item B<dcnpgettext TEXTDOMAIN, MSGCTXT, MSGID, MSGID_PLURAL, COUNT, CATEGORY>

See L<Locale::Messages/FUNCTIONS>.

=item B<textdomain TEXTDOMAIN>

See L<Locale::Messages/FUNCTIONS>.

=item B<bindtextdomain TEXTDOMAIN, DIRECTORY>

See L<Locale::Messages/FUNCTIONS>.

=item B<bind_textdomain_codeset TEXTDOMAIN, ENCODING>

=item B<nl_putenv ENVSPEC>

See L<Locale::Messages/FUNCTIONS>.

=item B<setlocale>

See L<Locale::Messages/FUNCTIONS>.

=back

=head1 CONSTANTS

You can (maybe) get the same constants from POSIX(3); see there for
a detailed description

=over 4

=item B<LC_CTYPE>

=item B<LC_NUMERIC>

=item B<LC_TIME>

=item B<LC_COLLATE>

=item B<LC_MONETARY>

=item B<LC_MESSAGES>

=item B<LC_ALL>

See L<Locale::Messages/CONSTANTS> for more information.

=back

=head1 EXPORT TAGS

This module does not export anything unless explicitely requested.
You can import groups of functions via two tags:

=over 4

=item B<use Locale::gettext_pp (':locale_h')>

Imports the functions that are normally defined in the C include
file F<locale.h>:

=over 8

=item B<gettext()>

=item B<dgettext()>

=item B<dcgettext()>

=item B<ngettext()>

=item B<dngettext()>

=item B<dcngettext()>

=item B<pgettext()>

Introduced with libintl-perl 1.17.

=item B<dpgettext()>

Introduced with libintl-perl 1.17.

=item B<dcpgettext()>

Introduced with libintl-perl 1.17.

=item B<npgettext()>

Introduced with libintl-perl 1.17.

=item B<dnpgettext()>

Introduced with libintl-perl 1.17.

=item B<dcnpgettext()>

Introduced with libintl-perl 1.17.

=item B<textdomain()>

=item B<bindtextdomain()>

=item B<bind_textdomain_codeset()>

=back

=item B<use Locale::gettext_pp (':libintl_h')>

Imports the locale category constants:

=over 8

=item B<LC_CTYPE>

=item B<LC_NUMERIC>

=item B<LC_TIME>

=item B<LC_COLLATE>

=item B<LC_MONETARY>

=item B<LC_MESSAGES>

=item B<LC_ALL>

=back

=back

=head1 AUTHOR

Copyright (C) 2002-2015, Guido Flohr E<lt>guido.flohr@cantanea.comE<gt>, all
rights reserved.  See the source code for details.

=head1 SEE ALSO

Locale::TextDomain(3pm), Locale::Messages(3pm), Encode(3pm),
perllocale(3pm), POSIX(3pm), perl(1), gettext(1), gettext(3)

=cut

Local Variables:
mode: perl
perl-indent-level: 4
perl-continued-statement-offset: 4
perl-continued-brace-offset: 0
perl-brace-offset: -4
perl-brace-imaginary-offset: 0
perl-label-offset: -4
tab-width: 4
End:

=cut