7 require Config; import Config;
8 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
20 my $dumper = Dumpvalue->new(
27 my($mess) = join "", @_;
29 print $dumper->stringify($mess,1), "\n";
36 my $have_setlocale = 0;
39 import POSIX ':locale_h';
43 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
44 # and mingw32 uses said silly CRT
45 $have_setlocale = 0 if $^O eq 'MSWin32' && $Config{cc} =~ /^(cl|gcc)/i;
47 my $last = $have_setlocale ? 116 : 98;
56 my ($n, $result) = @_;
58 print 'not ' unless ($result);
62 # First we'll do a lot of taint checking for locales.
63 # This is the easiest to test, actually, as any locale,
64 # even the default locale will taint under 'use locale'.
66 sub is_tainted { # hello, camel two.
67 no warnings 'uninitialized' ;
69 not eval { $dummy = join("", @_), kill 0; 1 }
72 sub check_taint ($$) {
73 ok $_[0], is_tainted($_[1]);
76 sub check_taint_not ($$) {
77 ok $_[0], not is_tainted($_[1]);
80 use locale; # engage locale and therefore locale taint.
82 check_taint_not 1, $a;
84 check_taint 2, uc($a);
85 check_taint 3, "\U$a";
86 check_taint 4, ucfirst($a);
87 check_taint 5, "\u$a";
88 check_taint 6, lc($a);
89 check_taint 7, "\L$a";
90 check_taint 8, lcfirst($a);
91 check_taint 9, "\l$a";
93 check_taint_not 10, sprintf('%e', 123.456);
94 check_taint_not 11, sprintf('%f', 123.456);
95 check_taint_not 12, sprintf('%g', 123.456);
96 check_taint_not 13, sprintf('%d', 123.456);
97 check_taint_not 14, sprintf('%x', 123.456);
101 $_ = uc($a); # taint $_
105 /(\w)/; # taint $&, $`, $', $+, $1.
111 check_taint_not 21, $2;
113 /(.)/; # untaint $&, $`, $', $+, $1.
114 check_taint_not 22, $&;
115 check_taint_not 23, $`;
116 check_taint_not 24, $';
117 check_taint_not 25, $+;
118 check_taint_not 26, $1;
119 check_taint_not 27, $2;
121 /(\W)/; # taint $&, $`, $', $+, $1.
127 check_taint_not 33, $2;
129 /(\s)/; # taint $&, $`, $', $+, $1.
135 check_taint_not 39, $2;
137 /(\S)/; # taint $&, $`, $', $+, $1.
143 check_taint_not 45, $2;
145 $_ = $a; # untaint $_
147 check_taint_not 46, $_;
149 /(b)/; # this must not taint
150 check_taint_not 47, $&;
151 check_taint_not 48, $`;
152 check_taint_not 49, $';
153 check_taint_not 50, $+;
154 check_taint_not 51, $1;
155 check_taint_not 52, $2;
157 $_ = $a; # untaint $_
159 check_taint_not 53, $_;
161 $b = uc($a); # taint $b
162 s/(.+)/$b/; # this must taint only the $_
165 check_taint_not 55, $&;
166 check_taint_not 56, $`;
167 check_taint_not 57, $';
168 check_taint_not 58, $+;
169 check_taint_not 59, $1;
170 check_taint_not 60, $2;
172 $_ = $a; # untaint $_
174 s/(.+)/b/; # this must not taint
175 check_taint_not 61, $_;
176 check_taint_not 62, $&;
177 check_taint_not 63, $`;
178 check_taint_not 64, $';
179 check_taint_not 65, $+;
180 check_taint_not 66, $1;
181 check_taint_not 67, $2;
183 $b = $a; # untaint $b
185 ($b = $a) =~ s/\w/$&/;
186 check_taint 68, $b; # $b should be tainted.
187 check_taint_not 69, $a; # $a should be not.
189 $_ = $a; # untaint $_
191 s/(\w)/\l$1/; # this must taint
198 check_taint_not 76, $2;
200 $_ = $a; # untaint $_
202 s/(\w)/\L$1/; # this must taint
209 check_taint_not 83, $2;
211 $_ = $a; # untaint $_
213 s/(\w)/\u$1/; # this must taint
220 check_taint_not 90, $2;
222 $_ = $a; # untaint $_
224 s/(\w)/\U$1/; # this must taint
231 check_taint_not 97, $2;
233 # After all this tainting $a should be cool.
235 check_taint_not 98, $a;
237 # I think we've seen quite enough of taint.
238 # Let us do some *real* locale work now,
239 # unless setlocale() is missing (i.e. minitest).
241 exit unless $have_setlocale;
245 debug "# Scanning for locales...\n";
247 # Note that it's okay that some languages have their native names
248 # capitalized here even though that's not "right". They are lowercased
249 # anyway later during the scanning process (and besides, some clueless
250 # vendor might have them capitalized errorneously anyway).
254 Arabic:ar:dz eg sa:6 arabic8
255 Brezhoneg Breton:br:fr:1 15
256 Bulgarski Bulgarian:bg:bg:5
257 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
258 Hrvatski Croatian:hr:hr:2
259 Cymraeg Welsh:cy:cy:1 14 15
261 Dansk Danish:dk:da:1 15
262 Nederlands Dutch:nl:be nl:1 15
263 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
265 Eesti Estonian:et:ee:4 6 13
266 Suomi Finnish:fi:fi:1 15
268 Deutsch German:de:at be ch de lu:1 15
269 Euskaraz Basque:eu:es fr:1 15
270 Galego Galician:gl:es:1 15
271 Ellada Greek:el:gr:7 g8
273 Greenlandic:kl:gl:4 6
274 Hebrew:iw:il:8 hebrew8
276 Indonesian:in:id:1 15
277 Gaeilge Irish:ga:IE:1 14 15
278 Italiano Italian:it:ch it:1 15
279 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
281 Latine Latin:la:va:1 15
283 Lithuanian:lt:lt:4 6 13
284 Macedonian:mk:mk:1 15
287 Norsk Norwegian:no no\@nynorsk:no:1 15
289 Polski Polish:pl:pl:2
291 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
292 Serbski Serbian:sr:yu:5
294 Slovene Slovenian:sl:si:2
295 Sqhip Albanian:sq:sq:1 15
296 Svenska Swedish:sv:fi se:1 15
298 Turkish:tr:tr:9 turkish8
302 if ($^O eq 'os390') {
303 # These cause heartburn. Broken locales?
304 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
305 $locales =~ s/Thai:th:th:11 tis620\n//;
308 sub in_utf8 () { $^H & 0x08 }
311 require "pragma/locale/utf8";
313 require "pragma/locale/latin1";
321 sort grep /\w/, map { chr } 0..255
326 if (setlocale(LC_ALL, $locale)) {
327 push @Locale, $locale;
331 sub decode_encodings {
334 foreach (split(/ /, shift)) {
336 push @enc, "ISO8859-$1";
337 push @enc, "iso8859$1"; # HP
339 push @enc, "roman8"; # HP
343 push @enc, "$_.UTF-8";
346 if ($^O eq 'os390') {
347 push @enc, qw(IBM-037 IBM-819 IBM-1047);
356 trylocale("ISO8859-$_");
357 trylocale("iso8859$_");
358 trylocale("iso8859-$_");
359 trylocale("iso_8859_$_");
360 trylocale("isolatin$_");
361 trylocale("isolatin-$_");
362 trylocale("iso_latin_$_");
365 # Sanitize the environment so that we can run the external 'locale'
366 # program without the taint mode getting grumpy.
368 # $ENV{PATH} is special in VMS.
369 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
371 # Other subversive stuff.
372 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
374 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
380 } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
381 # The SYS$I18N_LOCALE logical name search list was not present on
382 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
383 opendir(LOCALES, "SYS\$I18N_LOCALE:");
384 while ($_ = readdir(LOCALES)) {
391 # This is going to be slow.
393 foreach my $locale (split(/\n/, $locales)) {
394 my ($locale_name, $language_codes, $country_codes, $encodings) =
396 my @enc = decode_encodings($encodings);
397 foreach my $loc (split(/ /, $locale_name)) {
399 foreach my $enc (@enc) {
400 trylocale("$loc.$enc");
403 foreach my $enc (@enc) {
404 trylocale("$loc.$enc");
407 foreach my $lang (split(/ /, $language_codes)) {
409 foreach my $country (split(/ /, $country_codes)) {
410 my $lc = "${lang}_${country}";
412 foreach my $enc (@enc) {
413 trylocale("$lc.$enc");
415 my $lC = "${lang}_\U${country}";
417 foreach my $enc (@enc) {
418 trylocale("$lC.$enc");
425 setlocale(LC_ALL, "C");
427 sub utf8locale { $_[0] =~ /utf-?8/i }
429 @Locale = sort @Locale;
431 debug "# Locales = @Locale\n";
440 my ($Locale, $i, $test) = @_;
442 $Problem{$i}{$Locale} = 1;
443 debug "# failed $i with locale '$Locale'\n";
445 push @{$Okay{$i}}, $Locale;
449 foreach $Locale (@Locale) {
450 debug "# Locale = $Locale\n";
451 @Alnum_ = getalnum_();
452 debug "# w = ", join("",@Alnum_), "\n";
454 unless (setlocale(LC_ALL, $Locale)) {
456 $Problem{$_}{$Locale} = -1;
461 # Sieve the uppercase and the lowercase.
467 if (/[^\d_]/) { # skip digits and the _
476 foreach (keys %UPPER) {
477 $BoThCaSe{$_}++ if exists $lower{$_};
479 foreach (keys %lower) {
480 $BoThCaSe{$_}++ if exists $UPPER{$_};
482 foreach (keys %BoThCaSe) {
487 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
488 debug "# lower = ", join("", sort keys %lower ), "\n";
489 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
491 # Find the alphabets that are not alphabets in the default locale.
497 for (keys %UPPER, keys %lower) {
498 push(@Neoalpha, $_) if (/\W/);
503 @Neoalpha = sort @Neoalpha;
505 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
507 if (@Neoalpha == 0) {
508 # If we have no Neoalphas the remaining tests are no-ops.
509 debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
511 push @{$Okay{$_}}, $Locale;
517 if (utf8locale($Locale)) {
518 # Until the polymorphic regexen arrive.
519 debug "# skipping UTF-8 locale '$Locale'\n";
521 my $word = join('', @Neoalpha);
525 tryneoalpha($Locale, 99, $1 eq $word);
528 # Cross-check the whole 8-bit character set.
530 for (map { chr } 0..255) {
531 tryneoalpha($Locale, 100,
537 # Test for read-only scalars' locale vs non-locale comparisons.
544 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
549 my ($from, $to, $lesser, $greater,
550 @test, %test, $test, $yes, $no, $sign);
554 $from = int(($_*@Alnum_)/10);
555 $to = $from + int(@Alnum_/10);
556 $to = $#Alnum_ if ($to > $#Alnum_);
557 $lesser = join('', @Alnum_[$from..$to]);
558 # Select a slice one character on.
560 $to = $#Alnum_ if ($to > $#Alnum_);
561 $greater = join('', @Alnum_[$from..$to]);
562 ($yes, $no, $sign) = ($lesser lt $greater
564 : ("not ", " ", -1));
565 # all these tests should FAIL (return 0).
566 # Exact lt or gt cannot be tested because
567 # in some locales, say, eacute and E may test equal.
570 $no.' ($lesser le $greater)', # 1
571 'not ($lesser ne $greater)', # 2
572 ' ($lesser eq $greater)', # 3
573 $yes.' ($lesser ge $greater)', # 4
574 $yes.' ($lesser ge $greater)', # 5
575 $yes.' ($greater le $lesser )', # 7
576 'not ($greater ne $lesser )', # 8
577 ' ($greater eq $lesser )', # 9
578 $no.' ($greater ge $lesser )', # 10
579 'not (($lesser cmp $greater) == -$sign)' # 12
581 @test{@test} = 0 x @test;
584 $test{$ti} = eval $ti;
587 tryneoalpha($Locale, 102, $test == 0);
589 debug "# lesser = '$lesser'\n";
590 debug "# greater = '$greater'\n";
591 debug "# lesser cmp greater = ",
592 $lesser cmp $greater, "\n";
593 debug "# greater cmp lesser = ",
594 $greater cmp $lesser, "\n";
595 debug "# (greater) from = $from, to = $to\n";
597 debugf("# %-40s %-4s", $ti,
598 $test{$ti} ? 'FAIL' : 'ok');
599 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
600 debugf("(%s == %4d)", $1, eval $1);
613 my ($x, $y) = (1.23, 1.23);
616 printf ''; # printf used to reset locale to "C"
619 debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
621 tryneoalpha($Locale, 103, $a eq $b);
624 my $z = sprintf ''; # sprintf used to reset locale to "C"
627 debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
629 tryneoalpha($Locale, 104, $c eq $d);
634 local $SIG{__WARN__} = sub { $w++ };
636 # the == (among other ops) used to warn for locales
637 # that had something else than "." as the radix character
639 tryneoalpha($Locale, 105, $c == 1.23);
641 tryneoalpha($Locale, 106, $c == $x);
643 tryneoalpha($Locale, 107, $c == $d);
650 debug "# 108..110: e = $e, Locale = $Locale\n";
652 tryneoalpha($Locale, 108, $e == 1.23);
654 tryneoalpha($Locale, 109, $e == $x);
656 tryneoalpha($Locale, 110, $e == $c);
659 tryneoalpha($Locale, 111, $w == 0);
663 debug "# 112..114: f = $f, locale = $Locale\n";
665 tryneoalpha($Locale, 112, $f == 1.23);
667 tryneoalpha($Locale, 113, $f == $x);
669 tryneoalpha($Locale, 114, $f == $c);
672 # Does taking lc separately differ from taking
673 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
674 # The bug was in the caching of the 'o'-magic.
681 return $lc0 cmp $lc1;
685 return lc($_[0]) cmp lc($_[1]);
692 tryneoalpha($Locale, 115,
693 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
694 lcA($x, $z) == 0 && lcB($x, $z) == 0);
697 # Does lc of an UPPER (if different from the UPPER) match
698 # case-insensitively the UPPER, and does the UPPER match
699 # case-insensitively the lc of the UPPER. And vice versa.
700 if (utf8locale($Locale)) {
701 # Until the polymorphic regexen arrive.
702 debug "# skipping UTF-8 locale '$Locale'\n";
707 foreach my $x (keys %UPPER) {
709 next unless uc $y eq $x;
710 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
712 foreach my $x (keys %lower) {
714 next unless lc $y eq $x;
715 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
717 tryneoalpha($Locale, 116, @f == 0);
719 print "# failed 116 locale '$Locale' characters @f\n"
725 # Recount the errors.
727 foreach (99..$last) {
728 if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
730 print "# The failure of test 102 is not necessarily fatal.\n";
731 print "# It usually indicates a problem in the enviroment,\n";
732 print "# not in Perl itself.\n";
743 foreach (99..$last) {
745 my @f = sort keys %{ $Problem{$_} };
746 my $f = join(" ", @f);
747 $f =~ s/(.{50,60}) /$1\n#\t/g;
750 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
752 "# on your system may have errors because the locale test $_\n",
753 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
757 # If your users are not using these locales you are safe for the moment,
758 # but please report this failure first to perlbug\@perl.com using the
759 # perlbug script (as described in the INSTALL file) so that the exact
760 # details of the failures can be sorted out first and then your operating
761 # system supplier can be alerted about these anomalies.
768 # Tell which locales were okay and which were not.
773 foreach my $l (@Locale) {
775 foreach my $t (102..$last) {
776 $p++ if $Problem{$t}{$l};
778 push @s, $l if $p == 0;
779 push @F, $l unless $p == 0;
783 my $s = join(" ", @s);
784 $s =~ s/(.{50,60}) /$1\n#\t/g;
787 "# The following locales\n#\n",
789 "# tested okay.\n#\n",
791 warn "# None of your locales were fully okay.\n";
795 my $F = join(" ", @F);
796 $F =~ s/(.{50,60}) /$1\n#\t/g;
799 "# The following locales\n#\n",
801 "# had problems.\n#\n",
803 warn "# None of your locales were broken.\n";