3 binmode STDOUT, ':utf8';
4 binmode STDERR, ':utf8';
10 require Config; import Config;
11 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
24 my $dumper = Dumpvalue->new(
31 my($mess) = join "", @_;
33 print $dumper->stringify($mess,1), "\n";
40 my $have_setlocale = 0;
43 import POSIX ':locale_h';
47 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
48 # and mingw32 uses said silly CRT
49 # This doesn't seem to be an issue any more, at least on Windows XP,
50 # so re-enable the tests for Windows XP onwards.
51 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
52 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
53 $have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') &&
54 $Config{cc} =~ /^(cl|gcc)/i);
56 # UWIN seems to loop after test 98, just skip for now
57 $have_setlocale = 0 if ($^O =~ /^uwin/);
59 my $last_locales = $have_setlocale ? &last_locales : &last_without_setlocale;
60 my $last = $have_setlocale ? &last : &last_without_setlocale;
69 my ($n, $result, $message) = @_;
70 $message = "" unless defined $message;
72 print 'not ' unless ($result);
78 # First we'll do a lot of taint checking for locales.
79 # This is the easiest to test, actually, as any locale,
80 # even the default locale will taint under 'use locale'.
82 sub is_tainted { # hello, camel two.
83 no warnings 'uninitialized' ;
86 not eval { $dummy = join("", @_), kill 0; 1 }
89 sub check_taint ($$) {
90 ok $_[0], is_tainted($_[1]), "verify that is tainted";
93 sub check_taint_not ($$) {
94 ok $_[0], (not is_tainted($_[1])), "verify that isn't tainted";
97 use locale; # engage locale and therefore locale taint.
99 check_taint_not 1, $a;
101 check_taint 2, uc($a);
102 check_taint 3, "\U$a";
103 check_taint 4, ucfirst($a);
104 check_taint 5, "\u$a";
105 check_taint 6, lc($a);
106 check_taint 7, "\L$a";
107 check_taint 8, lcfirst($a);
108 check_taint 9, "\l$a";
110 check_taint_not 10, sprintf('%e', 123.456);
111 check_taint_not 11, sprintf('%f', 123.456);
112 check_taint_not 12, sprintf('%g', 123.456);
113 check_taint_not 13, sprintf('%d', 123.456);
114 check_taint_not 14, sprintf('%x', 123.456);
116 $_ = $a; # untaint $_
118 $_ = uc($a); # taint $_
122 /(\w)/; # taint $&, $`, $', $+, $1.
128 check_taint_not 21, $2;
130 /(.)/; # untaint $&, $`, $', $+, $1.
131 check_taint_not 22, $&;
132 check_taint_not 23, $`;
133 check_taint_not 24, $';
134 check_taint_not 25, $+;
135 check_taint_not 26, $1;
136 check_taint_not 27, $2;
138 /(\W)/; # taint $&, $`, $', $+, $1.
144 check_taint_not 33, $2;
146 /(\s)/; # taint $&, $`, $', $+, $1.
152 check_taint_not 39, $2;
154 /(\S)/; # taint $&, $`, $', $+, $1.
160 check_taint_not 45, $2;
162 $_ = $a; # untaint $_
164 check_taint_not 46, $_;
166 /(b)/; # this must not taint
167 check_taint_not 47, $&;
168 check_taint_not 48, $`;
169 check_taint_not 49, $';
170 check_taint_not 50, $+;
171 check_taint_not 51, $1;
172 check_taint_not 52, $2;
174 $_ = $a; # untaint $_
176 check_taint_not 53, $_;
178 $b = uc($a); # taint $b
179 s/(.+)/$b/; # this must taint only the $_
182 check_taint_not 55, $&;
183 check_taint_not 56, $`;
184 check_taint_not 57, $';
185 check_taint_not 58, $+;
186 check_taint_not 59, $1;
187 check_taint_not 60, $2;
189 $_ = $a; # untaint $_
191 s/(.+)/b/; # this must not taint
192 check_taint_not 61, $_;
193 check_taint_not 62, $&;
194 check_taint_not 63, $`;
195 check_taint_not 64, $';
196 check_taint_not 65, $+;
197 check_taint_not 66, $1;
198 check_taint_not 67, $2;
200 $b = $a; # untaint $b
202 ($b = $a) =~ s/\w/$&/;
203 check_taint 68, $b; # $b should be tainted.
204 check_taint_not 69, $a; # $a should be not.
206 $_ = $a; # untaint $_
208 s/(\w)/\l$1/; # this must taint
215 check_taint_not 76, $2;
217 $_ = $a; # untaint $_
219 s/(\w)/\L$1/; # this must taint
226 check_taint_not 83, $2;
228 $_ = $a; # untaint $_
230 s/(\w)/\u$1/; # this must taint
237 check_taint_not 90, $2;
239 $_ = $a; # untaint $_
241 s/(\w)/\U$1/; # this must taint
248 check_taint_not 97, $2;
250 # After all this tainting $a should be cool.
252 check_taint_not 98, $a;
254 sub last_without_setlocale { 98 }
256 # I think we've seen quite enough of taint.
257 # Let us do some *real* locale work now,
258 # unless setlocale() is missing (i.e. minitest).
260 exit unless $have_setlocale;
264 debug "# Scanning for locales...\n";
266 # Note that it's okay that some languages have their native names
267 # capitalized here even though that's not "right". They are lowercased
268 # anyway later during the scanning process (and besides, some clueless
269 # vendor might have them capitalized erroneously anyway).
273 Arabic:ar:dz eg sa:6 arabic8
274 Brezhoneg Breton:br:fr:1 15
275 Bulgarski Bulgarian:bg:bg:5
276 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
277 Hrvatski Croatian:hr:hr:2
278 Cymraeg Welsh:cy:cy:1 14 15
280 Dansk Danish:dk:da:1 15
281 Nederlands Dutch:nl:be nl:1 15
282 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
284 Eesti Estonian:et:ee:4 6 13
285 Suomi Finnish:fi:fi:1 15
287 Deutsch German:de:at be ch de lu:1 15
288 Euskaraz Basque:eu:es fr:1 15
289 Galego Galician:gl:es:1 15
290 Ellada Greek:el:gr:7 g8
292 Greenlandic:kl:gl:4 6
293 Hebrew:iw:il:8 hebrew8
295 Indonesian:in:id:1 15
296 Gaeilge Irish:ga:IE:1 14 15
297 Italiano Italian:it:ch it:1 15
298 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
300 Latine Latin:la:va:1 15
302 Lithuanian:lt:lt:4 6 13
303 Macedonian:mk:mk:1 15
306 Norsk Norwegian:no no\@nynorsk:no:1 15
308 Polski Polish:pl:pl:2
310 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
311 Serbski Serbian:sr:yu:5
313 Slovene Slovenian:sl:si:2
314 Sqhip Albanian:sq:sq:1 15
315 Svenska Swedish:sv:fi se:1 15
317 Turkish:tr:tr:9 turkish8
321 if ($^O eq 'os390') {
322 # These cause heartburn. Broken locales?
323 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
324 $locales =~ s/Thai:th:th:11 tis620\n//;
327 sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
330 require "lib/locale/utf8";
332 require "lib/locale/latin1";
343 sort grep /\w/, map { chr } 0..255
348 if (setlocale(LC_ALL, $locale)) {
349 push @Locale, $locale;
353 sub decode_encodings {
356 foreach (split(/ /, shift)) {
358 push @enc, "ISO8859-$1";
359 push @enc, "iso8859$1"; # HP
361 push @enc, "roman8"; # HP
365 push @enc, "$_.UTF-8";
368 if ($^O eq 'os390') {
369 push @enc, qw(IBM-037 IBM-819 IBM-1047);
378 trylocale("ISO8859-$_");
379 trylocale("iso8859$_");
380 trylocale("iso8859-$_");
381 trylocale("iso_8859_$_");
382 trylocale("isolatin$_");
383 trylocale("isolatin-$_");
384 trylocale("iso_latin_$_");
387 # Sanitize the environment so that we can run the external 'locale'
388 # program without the taint mode getting grumpy.
390 # $ENV{PATH} is special in VMS.
391 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
393 # Other subversive stuff.
394 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
396 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
398 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
399 # ain't great when we're running this testPERL_UNICODE= so that utf8
400 # locales will cause all IO hadles to default to (assume) utf8
401 next unless utf8::valid($_);
406 } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
407 # The SYS$I18N_LOCALE logical name search list was not present on
408 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
409 opendir(LOCALES, "SYS\$I18N_LOCALE:");
410 while ($_ = readdir(LOCALES)) {
415 } elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
417 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
418 # is much easier and faster than the last resort method.
420 opendir(LOCALES, '/usr/share/locale');
421 while ($_ = readdir(LOCALES)) {
428 # This is going to be slow.
430 foreach my $locale (split(/\n/, $locales)) {
431 my ($locale_name, $language_codes, $country_codes, $encodings) =
433 my @enc = decode_encodings($encodings);
434 foreach my $loc (split(/ /, $locale_name)) {
436 foreach my $enc (@enc) {
437 trylocale("$loc.$enc");
440 foreach my $enc (@enc) {
441 trylocale("$loc.$enc");
444 foreach my $lang (split(/ /, $language_codes)) {
446 foreach my $country (split(/ /, $country_codes)) {
447 my $lc = "${lang}_${country}";
449 foreach my $enc (@enc) {
450 trylocale("$lc.$enc");
452 my $lC = "${lang}_\U${country}";
454 foreach my $enc (@enc) {
455 trylocale("$lC.$enc");
462 setlocale(LC_ALL, "C");
464 if ($^O eq 'darwin') {
465 # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
466 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
467 (my $v) = $Config{osvers} =~ /^(\d+)/;
468 if ($v >= 8 and $v < 10) {
469 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
470 @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
472 debug "# Skipping be_BY locales -- buggy in Darwin\n";
473 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
477 @Locale = sort @Locale;
479 debug "# Locales =\n";
491 my ($Locale, $i, $test) = @_;
493 $Problem{$i}{$Locale} = 1;
494 debug "# failed $i with locale '$Locale'\n";
496 push @{$Okay{$i}}, $Locale;
500 foreach $Locale (@Locale) {
501 debug "# Locale = $Locale\n";
502 @Alnum_ = getalnum_();
503 debug "# w = ", join("",@Alnum_), "\n";
505 unless (setlocale(LC_ALL, $Locale)) {
507 $Problem{$_}{$Locale} = -1;
512 # Sieve the uppercase and the lowercase.
518 if (/[^\d_]/) { # skip digits and the _
527 foreach (keys %UPPER) {
528 $BoThCaSe{$_}++ if exists $lower{$_};
530 foreach (keys %lower) {
531 $BoThCaSe{$_}++ if exists $UPPER{$_};
533 foreach (keys %BoThCaSe) {
538 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
539 debug "# lower = ", join("", sort keys %lower ), "\n";
540 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
542 # Find the alphabets that are not alphabets in the default locale.
548 for (keys %UPPER, keys %lower) {
549 push(@Neoalpha, $_) if (/\W/);
554 @Neoalpha = sort @Neoalpha;
556 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
558 if (@Neoalpha == 0) {
559 # If we have no Neoalphas the remaining tests are no-ops.
560 debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
562 push @{$Okay{$_}}, $Locale;
568 my $word = join('', @Neoalpha);
572 local $SIG{__WARN__} = sub {
573 $badutf8 = $_[0] =~ /Malformed UTF-8/;
575 $Locale =~ /utf-?8/i;
579 debug "# Locale name contains bad UTF-8, skipping test 99 for locale '$Locale'\n";
580 } elsif ($Locale =~ /utf-?8/i) {
581 debug "# unknown whether locale and Unicode have the same \\w, skipping test 99 for locale '$Locale'\n";
582 push @{$Okay{99}}, $Locale;
584 if ($word =~ /^(\w+)$/) {
585 tryneoalpha($Locale, 99, 1);
587 tryneoalpha($Locale, 99, 0);
591 # Cross-check the whole 8-bit character set.
593 for (map { chr } 0..255) {
594 tryneoalpha($Locale, 100,
600 # Test for read-only scalars' locale vs non-locale comparisons.
607 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
612 my ($from, $to, $lesser, $greater,
613 @test, %test, $test, $yes, $no, $sign);
617 $from = int(($_*@Alnum_)/10);
618 $to = $from + int(@Alnum_/10);
619 $to = $#Alnum_ if ($to > $#Alnum_);
620 $lesser = join('', @Alnum_[$from..$to]);
621 # Select a slice one character on.
623 $to = $#Alnum_ if ($to > $#Alnum_);
624 $greater = join('', @Alnum_[$from..$to]);
625 ($yes, $no, $sign) = ($lesser lt $greater
627 : ("not ", " ", -1));
628 # all these tests should FAIL (return 0).
629 # Exact lt or gt cannot be tested because
630 # in some locales, say, eacute and E may test equal.
633 $no.' ($lesser le $greater)', # 1
634 'not ($lesser ne $greater)', # 2
635 ' ($lesser eq $greater)', # 3
636 $yes.' ($lesser ge $greater)', # 4
637 $yes.' ($lesser ge $greater)', # 5
638 $yes.' ($greater le $lesser )', # 7
639 'not ($greater ne $lesser )', # 8
640 ' ($greater eq $lesser )', # 9
641 $no.' ($greater ge $lesser )', # 10
642 'not (($lesser cmp $greater) == -($sign))' # 11
644 @test{@test} = 0 x @test;
647 $test{$ti} = eval $ti;
650 tryneoalpha($Locale, 102, $test == 0);
652 debug "# lesser = '$lesser'\n";
653 debug "# greater = '$greater'\n";
654 debug "# lesser cmp greater = ",
655 $lesser cmp $greater, "\n";
656 debug "# greater cmp lesser = ",
657 $greater cmp $lesser, "\n";
658 debug "# (greater) from = $from, to = $to\n";
660 debugf("# %-40s %-4s", $ti,
661 $test{$ti} ? 'FAIL' : 'ok');
662 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
663 debugf("(%s == %4d)", $1, eval $1);
676 my ($x, $y) = (1.23, 1.23);
679 printf ''; # printf used to reset locale to "C"
682 debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
684 tryneoalpha($Locale, 103, $a eq $b);
687 my $z = sprintf ''; # sprintf used to reset locale to "C"
690 debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
692 tryneoalpha($Locale, 104, $c eq $d);
697 local $SIG{__WARN__} =
703 # The == (among other ops) used to warn for locales
704 # that had something else than "." as the radix character.
706 tryneoalpha($Locale, 105, $c == 1.23);
708 tryneoalpha($Locale, 106, $c == $x);
710 tryneoalpha($Locale, 107, $c == $d);
713 # no locale; # XXX did this ever work correctly?
717 debug "# 108..110: e = $e, Locale = $Locale\n";
719 tryneoalpha($Locale, 108, $e == 1.23);
721 tryneoalpha($Locale, 109, $e == $x);
723 tryneoalpha($Locale, 110, $e == $c);
729 debug "# 111..115: f = $f, g = $g, locale = $Locale\n";
731 tryneoalpha($Locale, 111, $f == 1.23);
733 tryneoalpha($Locale, 112, $f == $x);
735 tryneoalpha($Locale, 113, $f == $c);
737 tryneoalpha($Locale, 114, abs(($f + $g) - 3.57) < 0.01);
739 tryneoalpha($Locale, 115, $w == 0);
742 # Does taking lc separately differ from taking
743 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
744 # The bug was in the caching of the 'o'-magic.
751 return $lc0 cmp $lc1;
755 return lc($_[0]) cmp lc($_[1]);
762 tryneoalpha($Locale, 116,
763 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
764 lcA($x, $z) == 0 && lcB($x, $z) == 0);
767 # Does lc of an UPPER (if different from the UPPER) match
768 # case-insensitively the UPPER, and does the UPPER match
769 # case-insensitively the lc of the UPPER. And vice versa.
773 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
776 foreach my $x (keys %UPPER) {
778 next unless uc $y eq $x;
779 print "# UPPER $x lc $y ",
780 $x =~ /$y/i ? 1 : 0, " ",
781 $y =~ /$x/i ? 1 : 0, "\n" if 0;
783 # If $x and $y contain regular expression characters
784 # AND THEY lowercase (/i) to regular expression characters,
785 # regcomp() will be mightily confused. No, the \Q doesn't
786 # help here (maybe regex engine internal lowercasing
787 # is done after the \Q?) An example of this happening is
788 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
789 # the chr(173) (the "[") is the lowercase of the chr(235).
791 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
792 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
793 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
794 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
795 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
796 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
798 # Similar things can happen even under (bastardised)
799 # non-EBCDIC locales: in many European countries before the
800 # advent of ISO 8859-x nationally customised versions of
801 # ISO 646 were devised, reusing certain punctuation
802 # characters for modified characters needed by the
803 # country/language. For example, the "|" might have
804 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
806 if ($x =~ $re || $y =~ $re) {
807 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
810 # With utf8 both will fail since the locale concept
811 # of upper/lower does not work well in Unicode.
812 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
815 foreach my $x (keys %lower) {
817 next unless lc $y eq $x;
818 print "# lower $x uc $y ",
819 $x =~ /$y/i ? 1 : 0, " ",
820 $y =~ /$x/i ? 1 : 0, "\n" if 0;
821 if ($x =~ $re || $y =~ $re) { # See above.
822 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
825 # With utf8 both will fail since the locale concept
826 # of upper/lower does not work well in Unicode.
827 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
829 tryneoalpha($Locale, 117, @f == 0);
831 print "# failed 117 locale '$Locale' characters @f\n"
836 # Recount the errors.
838 foreach (&last_without_setlocale()+1..$last_locales) {
839 if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
841 print "# The failure of test 102 is not necessarily fatal.\n";
842 print "# It usually indicates a problem in the environment,\n";
843 print "# not in Perl itself.\n";
854 foreach (99..$last_locales) {
856 my @f = sort keys %{ $Problem{$_} };
857 my $f = join(" ", @f);
858 $f =~ s/(.{50,60}) /$1\n#\t/g;
861 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
863 "# on your system may have errors because the locale test $_\n",
864 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
868 # If your users are not using these locales you are safe for the moment,
869 # but please report this failure first to perlbug\@perl.com using the
870 # perlbug script (as described in the INSTALL file) so that the exact
871 # details of the failures can be sorted out first and then your operating
872 # system supplier can be alerted about these anomalies.
879 # Tell which locales were okay and which were not.
884 foreach my $l (@Locale) {
886 foreach my $t (102..$last_locales) {
887 $p++ if $Problem{$t}{$l};
889 push @s, $l if $p == 0;
890 push @F, $l unless $p == 0;
894 my $s = join(" ", @s);
895 $s =~ s/(.{50,60}) /$1\n#\t/g;
898 "# The following locales\n#\n",
900 "# tested okay.\n#\n",
902 warn "# None of your locales were fully okay.\n";
906 my $F = join(" ", @F);
907 $F =~ s/(.{50,60}) /$1\n#\t/g;
910 "# The following locales\n#\n",
912 "# had problems.\n#\n",
914 warn "# None of your locales were broken.\n";
918 my $S = join(" ", @utf8locale);
919 $S =~ s/(.{50,60}) /$1\n#\t/g;
921 warn "#\n# The following locales\n#\n",
923 "# were skipped for the tests ",
924 join(" ", sort {$a<=>$b} keys %utf8skip), "\n",
925 "# because UTF-8 and locales do not work together in Perl.\n#\n";
929 sub last_locales { 117 }