7 require Config; import Config;
8 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
21 my $dumper = Dumpvalue->new(
28 my($mess) = join "", @_;
30 print $dumper->stringify($mess,1), "\n";
37 my $have_setlocale = 0;
40 import POSIX ':locale_h';
44 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
45 # and mingw32 uses said silly CRT
46 # This doesn't seem to be an issue any more, at least on Windows XP,
47 # so re-enable the tests for Windows XP onwards.
48 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
49 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
50 $have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') &&
51 $Config{cc} =~ /^(cl|gcc)/i);
53 # UWIN seems to loop after test 98, just skip for now
54 $have_setlocale = 0 if ($^O =~ /^uwin/);
56 my $last = $have_setlocale ? &last : &last_without_setlocale;
65 my ($n, $result) = @_;
67 print 'not ' unless ($result);
71 # First we'll do a lot of taint checking for locales.
72 # This is the easiest to test, actually, as any locale,
73 # even the default locale will taint under 'use locale'.
75 sub is_tainted { # hello, camel two.
76 no warnings 'uninitialized' ;
78 not eval { $dummy = join("", @_), kill 0; 1 }
81 sub check_taint ($$) {
82 ok $_[0], is_tainted($_[1]);
85 sub check_taint_not ($$) {
86 ok $_[0], not is_tainted($_[1]);
89 use locale; # engage locale and therefore locale taint.
91 check_taint_not 1, $a;
93 check_taint 2, uc($a);
94 check_taint 3, "\U$a";
95 check_taint 4, ucfirst($a);
96 check_taint 5, "\u$a";
97 check_taint 6, lc($a);
98 check_taint 7, "\L$a";
99 check_taint 8, lcfirst($a);
100 check_taint 9, "\l$a";
102 check_taint_not 10, sprintf('%e', 123.456);
103 check_taint_not 11, sprintf('%f', 123.456);
104 check_taint_not 12, sprintf('%g', 123.456);
105 check_taint_not 13, sprintf('%d', 123.456);
106 check_taint_not 14, sprintf('%x', 123.456);
108 $_ = $a; # untaint $_
110 $_ = uc($a); # taint $_
114 /(\w)/; # taint $&, $`, $', $+, $1.
120 check_taint_not 21, $2;
122 /(.)/; # untaint $&, $`, $', $+, $1.
123 check_taint_not 22, $&;
124 check_taint_not 23, $`;
125 check_taint_not 24, $';
126 check_taint_not 25, $+;
127 check_taint_not 26, $1;
128 check_taint_not 27, $2;
130 /(\W)/; # taint $&, $`, $', $+, $1.
136 check_taint_not 33, $2;
138 /(\s)/; # taint $&, $`, $', $+, $1.
144 check_taint_not 39, $2;
146 /(\S)/; # taint $&, $`, $', $+, $1.
152 check_taint_not 45, $2;
154 $_ = $a; # untaint $_
156 check_taint_not 46, $_;
158 /(b)/; # this must not taint
159 check_taint_not 47, $&;
160 check_taint_not 48, $`;
161 check_taint_not 49, $';
162 check_taint_not 50, $+;
163 check_taint_not 51, $1;
164 check_taint_not 52, $2;
166 $_ = $a; # untaint $_
168 check_taint_not 53, $_;
170 $b = uc($a); # taint $b
171 s/(.+)/$b/; # this must taint only the $_
174 check_taint_not 55, $&;
175 check_taint_not 56, $`;
176 check_taint_not 57, $';
177 check_taint_not 58, $+;
178 check_taint_not 59, $1;
179 check_taint_not 60, $2;
181 $_ = $a; # untaint $_
183 s/(.+)/b/; # this must not taint
184 check_taint_not 61, $_;
185 check_taint_not 62, $&;
186 check_taint_not 63, $`;
187 check_taint_not 64, $';
188 check_taint_not 65, $+;
189 check_taint_not 66, $1;
190 check_taint_not 67, $2;
192 $b = $a; # untaint $b
194 ($b = $a) =~ s/\w/$&/;
195 check_taint 68, $b; # $b should be tainted.
196 check_taint_not 69, $a; # $a should be not.
198 $_ = $a; # untaint $_
200 s/(\w)/\l$1/; # this must taint
207 check_taint_not 76, $2;
209 $_ = $a; # untaint $_
211 s/(\w)/\L$1/; # this must taint
218 check_taint_not 83, $2;
220 $_ = $a; # untaint $_
222 s/(\w)/\u$1/; # this must taint
229 check_taint_not 90, $2;
231 $_ = $a; # untaint $_
233 s/(\w)/\U$1/; # this must taint
240 check_taint_not 97, $2;
242 # After all this tainting $a should be cool.
244 check_taint_not 98, $a;
246 sub last_without_setlocale { 98 }
248 # I think we've seen quite enough of taint.
249 # Let us do some *real* locale work now,
250 # unless setlocale() is missing (i.e. minitest).
252 exit unless $have_setlocale;
256 debug "# Scanning for locales...\n";
258 # Note that it's okay that some languages have their native names
259 # capitalized here even though that's not "right". They are lowercased
260 # anyway later during the scanning process (and besides, some clueless
261 # vendor might have them capitalized errorneously anyway).
265 Arabic:ar:dz eg sa:6 arabic8
266 Brezhoneg Breton:br:fr:1 15
267 Bulgarski Bulgarian:bg:bg:5
268 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
269 Hrvatski Croatian:hr:hr:2
270 Cymraeg Welsh:cy:cy:1 14 15
272 Dansk Danish:dk:da:1 15
273 Nederlands Dutch:nl:be nl:1 15
274 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
276 Eesti Estonian:et:ee:4 6 13
277 Suomi Finnish:fi:fi:1 15
279 Deutsch German:de:at be ch de lu:1 15
280 Euskaraz Basque:eu:es fr:1 15
281 Galego Galician:gl:es:1 15
282 Ellada Greek:el:gr:7 g8
284 Greenlandic:kl:gl:4 6
285 Hebrew:iw:il:8 hebrew8
287 Indonesian:in:id:1 15
288 Gaeilge Irish:ga:IE:1 14 15
289 Italiano Italian:it:ch it:1 15
290 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
292 Latine Latin:la:va:1 15
294 Lithuanian:lt:lt:4 6 13
295 Macedonian:mk:mk:1 15
298 Norsk Norwegian:no no\@nynorsk:no:1 15
300 Polski Polish:pl:pl:2
302 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
303 Serbski Serbian:sr:yu:5
305 Slovene Slovenian:sl:si:2
306 Sqhip Albanian:sq:sq:1 15
307 Svenska Swedish:sv:fi se:1 15
309 Turkish:tr:tr:9 turkish8
313 if ($^O eq 'os390') {
314 # These cause heartburn. Broken locales?
315 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
316 $locales =~ s/Thai:th:th:11 tis620\n//;
319 sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
322 require "lib/locale/utf8";
324 require "lib/locale/latin1";
335 sort grep /\w/, map { chr } 0..255
340 if (setlocale(LC_ALL, $locale)) {
341 push @Locale, $locale;
345 sub decode_encodings {
348 foreach (split(/ /, shift)) {
350 push @enc, "ISO8859-$1";
351 push @enc, "iso8859$1"; # HP
353 push @enc, "roman8"; # HP
357 push @enc, "$_.UTF-8";
360 if ($^O eq 'os390') {
361 push @enc, qw(IBM-037 IBM-819 IBM-1047);
370 trylocale("ISO8859-$_");
371 trylocale("iso8859$_");
372 trylocale("iso8859-$_");
373 trylocale("iso_8859_$_");
374 trylocale("isolatin$_");
375 trylocale("isolatin-$_");
376 trylocale("iso_latin_$_");
379 # Sanitize the environment so that we can run the external 'locale'
380 # program without the taint mode getting grumpy.
382 # $ENV{PATH} is special in VMS.
383 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
385 # Other subversive stuff.
386 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
388 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
390 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
391 # ain't great when we're running this testPERL_UNICODE= so that utf8
392 # locales will cause all IO hadles to default to (assume) utf8
393 next unless utf8::valid($_);
398 } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
399 # The SYS$I18N_LOCALE logical name search list was not present on
400 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
401 opendir(LOCALES, "SYS\$I18N_LOCALE:");
402 while ($_ = readdir(LOCALES)) {
407 } elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
409 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
410 # is much easier and faster than the last resort method.
412 opendir(LOCALES, '/usr/share/locale');
413 while ($_ = readdir(LOCALES)) {
420 # This is going to be slow.
422 foreach my $locale (split(/\n/, $locales)) {
423 my ($locale_name, $language_codes, $country_codes, $encodings) =
425 my @enc = decode_encodings($encodings);
426 foreach my $loc (split(/ /, $locale_name)) {
428 foreach my $enc (@enc) {
429 trylocale("$loc.$enc");
432 foreach my $enc (@enc) {
433 trylocale("$loc.$enc");
436 foreach my $lang (split(/ /, $language_codes)) {
438 foreach my $country (split(/ /, $country_codes)) {
439 my $lc = "${lang}_${country}";
441 foreach my $enc (@enc) {
442 trylocale("$lc.$enc");
444 my $lC = "${lang}_\U${country}";
446 foreach my $enc (@enc) {
447 trylocale("$lC.$enc");
454 setlocale(LC_ALL, "C");
456 if ($^O eq 'darwin') {
457 # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
458 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
459 (my $v) = $Config{osvers} =~ /^(\d+)/;
460 if ($v >= 8 and $v < 10) {
461 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
462 @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
464 debug "# Skipping be_BY locales -- buggy in Darwin\n";
465 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
469 @Locale = sort @Locale;
471 debug "# Locales =\n";
483 my ($Locale, $i, $test) = @_;
485 $Problem{$i}{$Locale} = 1;
486 debug "# failed $i with locale '$Locale'\n";
488 push @{$Okay{$i}}, $Locale;
492 foreach $Locale (@Locale) {
493 debug "# Locale = $Locale\n";
494 @Alnum_ = getalnum_();
495 debug "# w = ", join("",@Alnum_), "\n";
497 unless (setlocale(LC_ALL, $Locale)) {
499 $Problem{$_}{$Locale} = -1;
504 # Sieve the uppercase and the lowercase.
510 if (/[^\d_]/) { # skip digits and the _
519 foreach (keys %UPPER) {
520 $BoThCaSe{$_}++ if exists $lower{$_};
522 foreach (keys %lower) {
523 $BoThCaSe{$_}++ if exists $UPPER{$_};
525 foreach (keys %BoThCaSe) {
530 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
531 debug "# lower = ", join("", sort keys %lower ), "\n";
532 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
534 # Find the alphabets that are not alphabets in the default locale.
540 for (keys %UPPER, keys %lower) {
541 push(@Neoalpha, $_) if (/\W/);
546 @Neoalpha = sort @Neoalpha;
548 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
550 if (@Neoalpha == 0) {
551 # If we have no Neoalphas the remaining tests are no-ops.
552 debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
554 push @{$Okay{$_}}, $Locale;
560 my $word = join('', @Neoalpha);
564 local $SIG{__WARN__} = sub {
565 $badutf8 = $_[0] =~ /Malformed UTF-8/;
567 $Locale =~ /utf-?8/i;
571 debug "# Locale name contains bad UTF-8, skipping test 99 for locale '$Locale'\n";
572 } elsif ($Locale =~ /utf-?8/i) {
573 debug "# unknown whether locale and Unicode have the same \\w, skipping test 99 for locale '$Locale'\n";
574 push @{$Okay{99}}, $Locale;
576 if ($word =~ /^(\w+)$/) {
577 tryneoalpha($Locale, 99, 1);
579 tryneoalpha($Locale, 99, 0);
583 # Cross-check the whole 8-bit character set.
585 for (map { chr } 0..255) {
586 tryneoalpha($Locale, 100,
592 # Test for read-only scalars' locale vs non-locale comparisons.
599 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
604 my ($from, $to, $lesser, $greater,
605 @test, %test, $test, $yes, $no, $sign);
609 $from = int(($_*@Alnum_)/10);
610 $to = $from + int(@Alnum_/10);
611 $to = $#Alnum_ if ($to > $#Alnum_);
612 $lesser = join('', @Alnum_[$from..$to]);
613 # Select a slice one character on.
615 $to = $#Alnum_ if ($to > $#Alnum_);
616 $greater = join('', @Alnum_[$from..$to]);
617 ($yes, $no, $sign) = ($lesser lt $greater
619 : ("not ", " ", -1));
620 # all these tests should FAIL (return 0).
621 # Exact lt or gt cannot be tested because
622 # in some locales, say, eacute and E may test equal.
625 $no.' ($lesser le $greater)', # 1
626 'not ($lesser ne $greater)', # 2
627 ' ($lesser eq $greater)', # 3
628 $yes.' ($lesser ge $greater)', # 4
629 $yes.' ($lesser ge $greater)', # 5
630 $yes.' ($greater le $lesser )', # 7
631 'not ($greater ne $lesser )', # 8
632 ' ($greater eq $lesser )', # 9
633 $no.' ($greater ge $lesser )', # 10
634 'not (($lesser cmp $greater) == -($sign))' # 11
636 @test{@test} = 0 x @test;
639 $test{$ti} = eval $ti;
642 tryneoalpha($Locale, 102, $test == 0);
644 debug "# lesser = '$lesser'\n";
645 debug "# greater = '$greater'\n";
646 debug "# lesser cmp greater = ",
647 $lesser cmp $greater, "\n";
648 debug "# greater cmp lesser = ",
649 $greater cmp $lesser, "\n";
650 debug "# (greater) from = $from, to = $to\n";
652 debugf("# %-40s %-4s", $ti,
653 $test{$ti} ? 'FAIL' : 'ok');
654 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
655 debugf("(%s == %4d)", $1, eval $1);
668 my ($x, $y) = (1.23, 1.23);
671 printf ''; # printf used to reset locale to "C"
674 debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
676 tryneoalpha($Locale, 103, $a eq $b);
679 my $z = sprintf ''; # sprintf used to reset locale to "C"
682 debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
684 tryneoalpha($Locale, 104, $c eq $d);
689 local $SIG{__WARN__} =
695 # The == (among other ops) used to warn for locales
696 # that had something else than "." as the radix character.
698 tryneoalpha($Locale, 105, $c == 1.23);
700 tryneoalpha($Locale, 106, $c == $x);
702 tryneoalpha($Locale, 107, $c == $d);
705 # no locale; # XXX did this ever work correctly?
709 debug "# 108..110: e = $e, Locale = $Locale\n";
711 tryneoalpha($Locale, 108, $e == 1.23);
713 tryneoalpha($Locale, 109, $e == $x);
715 tryneoalpha($Locale, 110, $e == $c);
721 debug "# 111..115: f = $f, g = $g, locale = $Locale\n";
723 tryneoalpha($Locale, 111, $f == 1.23);
725 tryneoalpha($Locale, 112, $f == $x);
727 tryneoalpha($Locale, 113, $f == $c);
729 tryneoalpha($Locale, 114, abs(($f + $g) - 3.57) < 0.01);
731 tryneoalpha($Locale, 115, $w == 0);
734 # Does taking lc separately differ from taking
735 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
736 # The bug was in the caching of the 'o'-magic.
743 return $lc0 cmp $lc1;
747 return lc($_[0]) cmp lc($_[1]);
754 tryneoalpha($Locale, 116,
755 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
756 lcA($x, $z) == 0 && lcB($x, $z) == 0);
759 # Does lc of an UPPER (if different from the UPPER) match
760 # case-insensitively the UPPER, and does the UPPER match
761 # case-insensitively the lc of the UPPER. And vice versa.
765 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
768 foreach my $x (keys %UPPER) {
770 next unless uc $y eq $x;
771 print "# UPPER $x lc $y ",
772 $x =~ /$y/i ? 1 : 0, " ",
773 $y =~ /$x/i ? 1 : 0, "\n" if 0;
775 # If $x and $y contain regular expression characters
776 # AND THEY lowercase (/i) to regular expression characters,
777 # regcomp() will be mightily confused. No, the \Q doesn't
778 # help here (maybe regex engine internal lowercasing
779 # is done after the \Q?) An example of this happening is
780 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
781 # the chr(173) (the "[") is the lowercase of the chr(235).
783 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
784 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
785 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
786 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
787 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
788 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
790 # Similar things can happen even under (bastardised)
791 # non-EBCDIC locales: in many European countries before the
792 # advent of ISO 8859-x nationally customised versions of
793 # ISO 646 were devised, reusing certain punctuation
794 # characters for modified characters needed by the
795 # country/language. For example, the "|" might have
796 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
798 if ($x =~ $re || $y =~ $re) {
799 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
802 # With utf8 both will fail since the locale concept
803 # of upper/lower does not work well in Unicode.
804 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
806 foreach my $x (keys %lower) {
808 next unless lc $y eq $x;
809 print "# lower $x uc $y ",
810 $x =~ /$y/i ? 1 : 0, " ",
811 $y =~ /$x/i ? 1 : 0, "\n" if 0;
812 if ($x =~ $re || $y =~ $re) { # See above.
813 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
816 # With utf8 both will fail since the locale concept
817 # of upper/lower does not work well in Unicode.
818 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
820 tryneoalpha($Locale, 117, @f == 0);
822 print "# failed 117 locale '$Locale' characters @f\n"
828 # Recount the errors.
830 foreach (&last_without_setlocale()+1..$last) {
831 if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
833 print "# The failure of test 102 is not necessarily fatal.\n";
834 print "# It usually indicates a problem in the environment,\n";
835 print "# not in Perl itself.\n";
846 foreach (99..$last) {
848 my @f = sort keys %{ $Problem{$_} };
849 my $f = join(" ", @f);
850 $f =~ s/(.{50,60}) /$1\n#\t/g;
853 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
855 "# on your system may have errors because the locale test $_\n",
856 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
860 # If your users are not using these locales you are safe for the moment,
861 # but please report this failure first to perlbug\@perl.com using the
862 # perlbug script (as described in the INSTALL file) so that the exact
863 # details of the failures can be sorted out first and then your operating
864 # system supplier can be alerted about these anomalies.
871 # Tell which locales were okay and which were not.
876 foreach my $l (@Locale) {
878 foreach my $t (102..$last) {
879 $p++ if $Problem{$t}{$l};
881 push @s, $l if $p == 0;
882 push @F, $l unless $p == 0;
886 my $s = join(" ", @s);
887 $s =~ s/(.{50,60}) /$1\n#\t/g;
890 "# The following locales\n#\n",
892 "# tested okay.\n#\n",
894 warn "# None of your locales were fully okay.\n";
898 my $F = join(" ", @F);
899 $F =~ s/(.{50,60}) /$1\n#\t/g;
902 "# The following locales\n#\n",
904 "# had problems.\n#\n",
906 warn "# None of your locales were broken.\n";
910 my $S = join(" ", @utf8locale);
911 $S =~ s/(.{50,60}) /$1\n#\t/g;
913 warn "#\n# The following locales\n#\n",
915 "# were skipped for the tests ",
916 join(" ", sort {$a<=>$b} keys %utf8skip), "\n",
917 "# because UTF-8 and locales do not work together in Perl.\n#\n";