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 = $have_setlocale ? &last : &last_without_setlocale;
68 my ($n, $result, $message) = @_;
69 $message = "" unless defined $message;
71 print 'not ' unless ($result);
77 # First we'll do a lot of taint checking for locales.
78 # This is the easiest to test, actually, as any locale,
79 # even the default locale will taint under 'use locale'.
81 sub is_tainted { # hello, camel two.
82 no warnings 'uninitialized' ;
85 not eval { $dummy = join("", @_), kill 0; 1 }
88 sub check_taint ($$) {
89 ok $_[0], is_tainted($_[1]), "verify that is tainted";
92 sub check_taint_not ($$) {
93 ok $_[0], (not is_tainted($_[1])), "verify that isn't tainted";
96 use locale; # engage locale and therefore locale taint.
98 check_taint_not 1, $a;
100 check_taint 2, uc($a);
101 check_taint 3, "\U$a";
102 check_taint 4, ucfirst($a);
103 check_taint 5, "\u$a";
104 check_taint 6, lc($a);
105 check_taint 7, "\L$a";
106 check_taint 8, lcfirst($a);
107 check_taint 9, "\l$a";
109 check_taint_not 10, sprintf('%e', 123.456);
110 check_taint_not 11, sprintf('%f', 123.456);
111 check_taint_not 12, sprintf('%g', 123.456);
112 check_taint_not 13, sprintf('%d', 123.456);
113 check_taint_not 14, sprintf('%x', 123.456);
115 $_ = $a; # untaint $_
117 $_ = uc($a); # taint $_
121 /(\w)/; # taint $&, $`, $', $+, $1.
127 check_taint_not 21, $2;
129 /(.)/; # untaint $&, $`, $', $+, $1.
130 check_taint_not 22, $&;
131 check_taint_not 23, $`;
132 check_taint_not 24, $';
133 check_taint_not 25, $+;
134 check_taint_not 26, $1;
135 check_taint_not 27, $2;
137 /(\W)/; # taint $&, $`, $', $+, $1.
143 check_taint_not 33, $2;
145 /(\s)/; # taint $&, $`, $', $+, $1.
151 check_taint_not 39, $2;
153 /(\S)/; # taint $&, $`, $', $+, $1.
159 check_taint_not 45, $2;
161 $_ = $a; # untaint $_
163 check_taint_not 46, $_;
165 /(b)/; # this must not taint
166 check_taint_not 47, $&;
167 check_taint_not 48, $`;
168 check_taint_not 49, $';
169 check_taint_not 50, $+;
170 check_taint_not 51, $1;
171 check_taint_not 52, $2;
173 $_ = $a; # untaint $_
175 check_taint_not 53, $_;
177 $b = uc($a); # taint $b
178 s/(.+)/$b/; # this must taint only the $_
181 check_taint_not 55, $&;
182 check_taint_not 56, $`;
183 check_taint_not 57, $';
184 check_taint_not 58, $+;
185 check_taint_not 59, $1;
186 check_taint_not 60, $2;
188 $_ = $a; # untaint $_
190 s/(.+)/b/; # this must not taint
191 check_taint_not 61, $_;
192 check_taint_not 62, $&;
193 check_taint_not 63, $`;
194 check_taint_not 64, $';
195 check_taint_not 65, $+;
196 check_taint_not 66, $1;
197 check_taint_not 67, $2;
199 $b = $a; # untaint $b
201 ($b = $a) =~ s/\w/$&/;
202 check_taint 68, $b; # $b should be tainted.
203 check_taint_not 69, $a; # $a should be not.
205 $_ = $a; # untaint $_
207 s/(\w)/\l$1/; # this must taint
214 check_taint_not 76, $2;
216 $_ = $a; # untaint $_
218 s/(\w)/\L$1/; # this must taint
225 check_taint_not 83, $2;
227 $_ = $a; # untaint $_
229 s/(\w)/\u$1/; # this must taint
236 check_taint_not 90, $2;
238 $_ = $a; # untaint $_
240 s/(\w)/\U$1/; # this must taint
247 check_taint_not 97, $2;
249 # After all this tainting $a should be cool.
251 check_taint_not 98, $a;
253 sub last_without_setlocale { 98 }
255 # I think we've seen quite enough of taint.
256 # Let us do some *real* locale work now,
257 # unless setlocale() is missing (i.e. minitest).
259 exit unless $have_setlocale;
263 debug "# Scanning for locales...\n";
265 # Note that it's okay that some languages have their native names
266 # capitalized here even though that's not "right". They are lowercased
267 # anyway later during the scanning process (and besides, some clueless
268 # vendor might have them capitalized erroneously anyway).
272 Arabic:ar:dz eg sa:6 arabic8
273 Brezhoneg Breton:br:fr:1 15
274 Bulgarski Bulgarian:bg:bg:5
275 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
276 Hrvatski Croatian:hr:hr:2
277 Cymraeg Welsh:cy:cy:1 14 15
279 Dansk Danish:dk:da:1 15
280 Nederlands Dutch:nl:be nl:1 15
281 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
283 Eesti Estonian:et:ee:4 6 13
284 Suomi Finnish:fi:fi:1 15
286 Deutsch German:de:at be ch de lu:1 15
287 Euskaraz Basque:eu:es fr:1 15
288 Galego Galician:gl:es:1 15
289 Ellada Greek:el:gr:7 g8
291 Greenlandic:kl:gl:4 6
292 Hebrew:iw:il:8 hebrew8
294 Indonesian:in:id:1 15
295 Gaeilge Irish:ga:IE:1 14 15
296 Italiano Italian:it:ch it:1 15
297 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
299 Latine Latin:la:va:1 15
301 Lithuanian:lt:lt:4 6 13
302 Macedonian:mk:mk:1 15
305 Norsk Norwegian:no no\@nynorsk:no:1 15
307 Polski Polish:pl:pl:2
309 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
310 Serbski Serbian:sr:yu:5
312 Slovene Slovenian:sl:si:2
313 Sqhip Albanian:sq:sq:1 15
314 Svenska Swedish:sv:fi se:1 15
316 Turkish:tr:tr:9 turkish8
320 if ($^O eq 'os390') {
321 # These cause heartburn. Broken locales?
322 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
323 $locales =~ s/Thai:th:th:11 tis620\n//;
326 sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
329 require "lib/locale/utf8";
331 require "lib/locale/latin1";
342 sort grep /\w/, map { chr } 0..255
347 if (setlocale(LC_ALL, $locale)) {
348 push @Locale, $locale;
352 sub decode_encodings {
355 foreach (split(/ /, shift)) {
357 push @enc, "ISO8859-$1";
358 push @enc, "iso8859$1"; # HP
360 push @enc, "roman8"; # HP
364 push @enc, "$_.UTF-8";
367 if ($^O eq 'os390') {
368 push @enc, qw(IBM-037 IBM-819 IBM-1047);
377 trylocale("ISO8859-$_");
378 trylocale("iso8859$_");
379 trylocale("iso8859-$_");
380 trylocale("iso_8859_$_");
381 trylocale("isolatin$_");
382 trylocale("isolatin-$_");
383 trylocale("iso_latin_$_");
386 # Sanitize the environment so that we can run the external 'locale'
387 # program without the taint mode getting grumpy.
389 # $ENV{PATH} is special in VMS.
390 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
392 # Other subversive stuff.
393 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
395 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
397 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
398 # ain't great when we're running this testPERL_UNICODE= so that utf8
399 # locales will cause all IO hadles to default to (assume) utf8
400 next unless utf8::valid($_);
405 } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
406 # The SYS$I18N_LOCALE logical name search list was not present on
407 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
408 opendir(LOCALES, "SYS\$I18N_LOCALE:");
409 while ($_ = readdir(LOCALES)) {
414 } elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
416 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
417 # is much easier and faster than the last resort method.
419 opendir(LOCALES, '/usr/share/locale');
420 while ($_ = readdir(LOCALES)) {
427 # This is going to be slow.
429 foreach my $locale (split(/\n/, $locales)) {
430 my ($locale_name, $language_codes, $country_codes, $encodings) =
432 my @enc = decode_encodings($encodings);
433 foreach my $loc (split(/ /, $locale_name)) {
435 foreach my $enc (@enc) {
436 trylocale("$loc.$enc");
439 foreach my $enc (@enc) {
440 trylocale("$loc.$enc");
443 foreach my $lang (split(/ /, $language_codes)) {
445 foreach my $country (split(/ /, $country_codes)) {
446 my $lc = "${lang}_${country}";
448 foreach my $enc (@enc) {
449 trylocale("$lc.$enc");
451 my $lC = "${lang}_\U${country}";
453 foreach my $enc (@enc) {
454 trylocale("$lC.$enc");
461 setlocale(LC_ALL, "C");
463 if ($^O eq 'darwin') {
464 # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
465 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
466 (my $v) = $Config{osvers} =~ /^(\d+)/;
467 if ($v >= 8 and $v < 10) {
468 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
469 @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
471 debug "# Skipping be_BY locales -- buggy in Darwin\n";
472 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
476 @Locale = sort @Locale;
478 debug "# Locales =\n";
490 my ($Locale, $i, $test) = @_;
492 $Problem{$i}{$Locale} = 1;
493 debug "# failed $i with locale '$Locale'\n";
495 push @{$Okay{$i}}, $Locale;
499 foreach $Locale (@Locale) {
500 debug "# Locale = $Locale\n";
501 @Alnum_ = getalnum_();
502 debug "# w = ", join("",@Alnum_), "\n";
504 unless (setlocale(LC_ALL, $Locale)) {
506 $Problem{$_}{$Locale} = -1;
511 # Sieve the uppercase and the lowercase.
517 if (/[^\d_]/) { # skip digits and the _
526 foreach (keys %UPPER) {
527 $BoThCaSe{$_}++ if exists $lower{$_};
529 foreach (keys %lower) {
530 $BoThCaSe{$_}++ if exists $UPPER{$_};
532 foreach (keys %BoThCaSe) {
537 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
538 debug "# lower = ", join("", sort keys %lower ), "\n";
539 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
541 # Find the alphabets that are not alphabets in the default locale.
547 for (keys %UPPER, keys %lower) {
548 push(@Neoalpha, $_) if (/\W/);
553 @Neoalpha = sort @Neoalpha;
555 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
557 if (@Neoalpha == 0) {
558 # If we have no Neoalphas the remaining tests are no-ops.
559 debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
561 push @{$Okay{$_}}, $Locale;
567 my $word = join('', @Neoalpha);
571 local $SIG{__WARN__} = sub {
572 $badutf8 = $_[0] =~ /Malformed UTF-8/;
574 $Locale =~ /utf-?8/i;
578 debug "# Locale name contains bad UTF-8, skipping test 99 for locale '$Locale'\n";
579 } elsif ($Locale =~ /utf-?8/i) {
580 debug "# unknown whether locale and Unicode have the same \\w, skipping test 99 for locale '$Locale'\n";
581 push @{$Okay{99}}, $Locale;
583 if ($word =~ /^(\w+)$/) {
584 tryneoalpha($Locale, 99, 1);
586 tryneoalpha($Locale, 99, 0);
590 # Cross-check the whole 8-bit character set.
592 for (map { chr } 0..255) {
593 tryneoalpha($Locale, 100,
599 # Test for read-only scalars' locale vs non-locale comparisons.
606 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
611 my ($from, $to, $lesser, $greater,
612 @test, %test, $test, $yes, $no, $sign);
616 $from = int(($_*@Alnum_)/10);
617 $to = $from + int(@Alnum_/10);
618 $to = $#Alnum_ if ($to > $#Alnum_);
619 $lesser = join('', @Alnum_[$from..$to]);
620 # Select a slice one character on.
622 $to = $#Alnum_ if ($to > $#Alnum_);
623 $greater = join('', @Alnum_[$from..$to]);
624 ($yes, $no, $sign) = ($lesser lt $greater
626 : ("not ", " ", -1));
627 # all these tests should FAIL (return 0).
628 # Exact lt or gt cannot be tested because
629 # in some locales, say, eacute and E may test equal.
632 $no.' ($lesser le $greater)', # 1
633 'not ($lesser ne $greater)', # 2
634 ' ($lesser eq $greater)', # 3
635 $yes.' ($lesser ge $greater)', # 4
636 $yes.' ($lesser ge $greater)', # 5
637 $yes.' ($greater le $lesser )', # 7
638 'not ($greater ne $lesser )', # 8
639 ' ($greater eq $lesser )', # 9
640 $no.' ($greater ge $lesser )', # 10
641 'not (($lesser cmp $greater) == -($sign))' # 11
643 @test{@test} = 0 x @test;
646 $test{$ti} = eval $ti;
649 tryneoalpha($Locale, 102, $test == 0);
651 debug "# lesser = '$lesser'\n";
652 debug "# greater = '$greater'\n";
653 debug "# lesser cmp greater = ",
654 $lesser cmp $greater, "\n";
655 debug "# greater cmp lesser = ",
656 $greater cmp $lesser, "\n";
657 debug "# (greater) from = $from, to = $to\n";
659 debugf("# %-40s %-4s", $ti,
660 $test{$ti} ? 'FAIL' : 'ok');
661 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
662 debugf("(%s == %4d)", $1, eval $1);
675 my ($x, $y) = (1.23, 1.23);
678 printf ''; # printf used to reset locale to "C"
681 debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
683 tryneoalpha($Locale, 103, $a eq $b);
686 my $z = sprintf ''; # sprintf used to reset locale to "C"
689 debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
691 tryneoalpha($Locale, 104, $c eq $d);
696 local $SIG{__WARN__} =
702 # The == (among other ops) used to warn for locales
703 # that had something else than "." as the radix character.
705 tryneoalpha($Locale, 105, $c == 1.23);
707 tryneoalpha($Locale, 106, $c == $x);
709 tryneoalpha($Locale, 107, $c == $d);
712 # no locale; # XXX did this ever work correctly?
716 debug "# 108..110: e = $e, Locale = $Locale\n";
718 tryneoalpha($Locale, 108, $e == 1.23);
720 tryneoalpha($Locale, 109, $e == $x);
722 tryneoalpha($Locale, 110, $e == $c);
728 debug "# 111..115: f = $f, g = $g, locale = $Locale\n";
730 tryneoalpha($Locale, 111, $f == 1.23);
732 tryneoalpha($Locale, 112, $f == $x);
734 tryneoalpha($Locale, 113, $f == $c);
736 tryneoalpha($Locale, 114, abs(($f + $g) - 3.57) < 0.01);
738 tryneoalpha($Locale, 115, $w == 0);
741 # Does taking lc separately differ from taking
742 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
743 # The bug was in the caching of the 'o'-magic.
750 return $lc0 cmp $lc1;
754 return lc($_[0]) cmp lc($_[1]);
761 tryneoalpha($Locale, 116,
762 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
763 lcA($x, $z) == 0 && lcB($x, $z) == 0);
766 # Does lc of an UPPER (if different from the UPPER) match
767 # case-insensitively the UPPER, and does the UPPER match
768 # case-insensitively the lc of the UPPER. And vice versa.
772 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
775 foreach my $x (keys %UPPER) {
777 next unless uc $y eq $x;
778 print "# UPPER $x lc $y ",
779 $x =~ /$y/i ? 1 : 0, " ",
780 $y =~ /$x/i ? 1 : 0, "\n" if 0;
782 # If $x and $y contain regular expression characters
783 # AND THEY lowercase (/i) to regular expression characters,
784 # regcomp() will be mightily confused. No, the \Q doesn't
785 # help here (maybe regex engine internal lowercasing
786 # is done after the \Q?) An example of this happening is
787 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
788 # the chr(173) (the "[") is the lowercase of the chr(235).
790 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
791 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
792 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
793 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
794 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
795 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
797 # Similar things can happen even under (bastardised)
798 # non-EBCDIC locales: in many European countries before the
799 # advent of ISO 8859-x nationally customised versions of
800 # ISO 646 were devised, reusing certain punctuation
801 # characters for modified characters needed by the
802 # country/language. For example, the "|" might have
803 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
805 if ($x =~ $re || $y =~ $re) {
806 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
809 # With utf8 both will fail since the locale concept
810 # of upper/lower does not work well in Unicode.
811 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
814 foreach my $x (keys %lower) {
816 next unless lc $y eq $x;
817 print "# lower $x uc $y ",
818 $x =~ /$y/i ? 1 : 0, " ",
819 $y =~ /$x/i ? 1 : 0, "\n" if 0;
820 if ($x =~ $re || $y =~ $re) { # See above.
821 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
824 # With utf8 both will fail since the locale concept
825 # of upper/lower does not work well in Unicode.
826 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
828 tryneoalpha($Locale, 117, @f == 0);
830 print "# failed 117 locale '$Locale' characters @f\n"
835 # Recount the errors.
837 foreach (&last_without_setlocale()+1..$last) {
838 if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
840 print "# The failure of test 102 is not necessarily fatal.\n";
841 print "# It usually indicates a problem in the environment,\n";
842 print "# not in Perl itself.\n";
853 foreach (99..$last) {
855 my @f = sort keys %{ $Problem{$_} };
856 my $f = join(" ", @f);
857 $f =~ s/(.{50,60}) /$1\n#\t/g;
860 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
862 "# on your system may have errors because the locale test $_\n",
863 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
867 # If your users are not using these locales you are safe for the moment,
868 # but please report this failure first to perlbug\@perl.com using the
869 # perlbug script (as described in the INSTALL file) so that the exact
870 # details of the failures can be sorted out first and then your operating
871 # system supplier can be alerted about these anomalies.
878 # Tell which locales were okay and which were not.
883 foreach my $l (@Locale) {
885 foreach my $t (102..$last) {
886 $p++ if $Problem{$t}{$l};
888 push @s, $l if $p == 0;
889 push @F, $l unless $p == 0;
893 my $s = join(" ", @s);
894 $s =~ s/(.{50,60}) /$1\n#\t/g;
897 "# The following locales\n#\n",
899 "# tested okay.\n#\n",
901 warn "# None of your locales were fully okay.\n";
905 my $F = join(" ", @F);
906 $F =~ s/(.{50,60}) /$1\n#\t/g;
909 "# The following locales\n#\n",
911 "# had problems.\n#\n",
913 warn "# None of your locales were broken.\n";
917 my $S = join(" ", @utf8locale);
918 $S =~ s/(.{50,60}) /$1\n#\t/g;
920 warn "#\n# The following locales\n#\n",
922 "# were skipped for the tests ",
923 join(" ", sort {$a<=>$b} keys %utf8skip), "\n",
924 "# because UTF-8 and locales do not work together in Perl.\n#\n";