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 $have_setlocale = 0 if (($^O eq 'MSWin32' || $^O eq 'NetWare') && $Config{cc} =~ /^(cl|gcc)/i);
48 # UWIN seems to loop after test 98, just skip for now
49 $have_setlocale = 0 if ($^O =~ /^uwin/);
51 my $last = $have_setlocale ? &last : &last_without_setlocale;
60 my ($n, $result) = @_;
62 print 'not ' unless ($result);
66 # First we'll do a lot of taint checking for locales.
67 # This is the easiest to test, actually, as any locale,
68 # even the default locale will taint under 'use locale'.
70 sub is_tainted { # hello, camel two.
71 no warnings 'uninitialized' ;
73 not eval { $dummy = join("", @_), kill 0; 1 }
76 sub check_taint ($$) {
77 ok $_[0], is_tainted($_[1]);
80 sub check_taint_not ($$) {
81 ok $_[0], not is_tainted($_[1]);
84 use locale; # engage locale and therefore locale taint.
86 check_taint_not 1, $a;
88 check_taint 2, uc($a);
89 check_taint 3, "\U$a";
90 check_taint 4, ucfirst($a);
91 check_taint 5, "\u$a";
92 check_taint 6, lc($a);
93 check_taint 7, "\L$a";
94 check_taint 8, lcfirst($a);
95 check_taint 9, "\l$a";
97 check_taint_not 10, sprintf('%e', 123.456);
98 check_taint_not 11, sprintf('%f', 123.456);
99 check_taint_not 12, sprintf('%g', 123.456);
100 check_taint_not 13, sprintf('%d', 123.456);
101 check_taint_not 14, sprintf('%x', 123.456);
103 $_ = $a; # untaint $_
105 $_ = uc($a); # taint $_
109 /(\w)/; # taint $&, $`, $', $+, $1.
115 check_taint_not 21, $2;
117 /(.)/; # untaint $&, $`, $', $+, $1.
118 check_taint_not 22, $&;
119 check_taint_not 23, $`;
120 check_taint_not 24, $';
121 check_taint_not 25, $+;
122 check_taint_not 26, $1;
123 check_taint_not 27, $2;
125 /(\W)/; # taint $&, $`, $', $+, $1.
131 check_taint_not 33, $2;
133 /(\s)/; # taint $&, $`, $', $+, $1.
139 check_taint_not 39, $2;
141 /(\S)/; # taint $&, $`, $', $+, $1.
147 check_taint_not 45, $2;
149 $_ = $a; # untaint $_
151 check_taint_not 46, $_;
153 /(b)/; # this must not taint
154 check_taint_not 47, $&;
155 check_taint_not 48, $`;
156 check_taint_not 49, $';
157 check_taint_not 50, $+;
158 check_taint_not 51, $1;
159 check_taint_not 52, $2;
161 $_ = $a; # untaint $_
163 check_taint_not 53, $_;
165 $b = uc($a); # taint $b
166 s/(.+)/$b/; # this must taint only the $_
169 check_taint_not 55, $&;
170 check_taint_not 56, $`;
171 check_taint_not 57, $';
172 check_taint_not 58, $+;
173 check_taint_not 59, $1;
174 check_taint_not 60, $2;
176 $_ = $a; # untaint $_
178 s/(.+)/b/; # this must not taint
179 check_taint_not 61, $_;
180 check_taint_not 62, $&;
181 check_taint_not 63, $`;
182 check_taint_not 64, $';
183 check_taint_not 65, $+;
184 check_taint_not 66, $1;
185 check_taint_not 67, $2;
187 $b = $a; # untaint $b
189 ($b = $a) =~ s/\w/$&/;
190 check_taint 68, $b; # $b should be tainted.
191 check_taint_not 69, $a; # $a should be not.
193 $_ = $a; # untaint $_
195 s/(\w)/\l$1/; # this must taint
202 check_taint_not 76, $2;
204 $_ = $a; # untaint $_
206 s/(\w)/\L$1/; # this must taint
213 check_taint_not 83, $2;
215 $_ = $a; # untaint $_
217 s/(\w)/\u$1/; # this must taint
224 check_taint_not 90, $2;
226 $_ = $a; # untaint $_
228 s/(\w)/\U$1/; # this must taint
235 check_taint_not 97, $2;
237 # After all this tainting $a should be cool.
239 check_taint_not 98, $a;
241 sub last_without_setlocale { 98 }
243 # I think we've seen quite enough of taint.
244 # Let us do some *real* locale work now,
245 # unless setlocale() is missing (i.e. minitest).
247 exit unless $have_setlocale;
251 debug "# Scanning for locales...\n";
253 # Note that it's okay that some languages have their native names
254 # capitalized here even though that's not "right". They are lowercased
255 # anyway later during the scanning process (and besides, some clueless
256 # vendor might have them capitalized errorneously anyway).
260 Arabic:ar:dz eg sa:6 arabic8
261 Brezhoneg Breton:br:fr:1 15
262 Bulgarski Bulgarian:bg:bg:5
263 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
264 Hrvatski Croatian:hr:hr:2
265 Cymraeg Welsh:cy:cy:1 14 15
267 Dansk Danish:dk:da:1 15
268 Nederlands Dutch:nl:be nl:1 15
269 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
271 Eesti Estonian:et:ee:4 6 13
272 Suomi Finnish:fi:fi:1 15
274 Deutsch German:de:at be ch de lu:1 15
275 Euskaraz Basque:eu:es fr:1 15
276 Galego Galician:gl:es:1 15
277 Ellada Greek:el:gr:7 g8
279 Greenlandic:kl:gl:4 6
280 Hebrew:iw:il:8 hebrew8
282 Indonesian:in:id:1 15
283 Gaeilge Irish:ga:IE:1 14 15
284 Italiano Italian:it:ch it:1 15
285 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
287 Latine Latin:la:va:1 15
289 Lithuanian:lt:lt:4 6 13
290 Macedonian:mk:mk:1 15
293 Norsk Norwegian:no no\@nynorsk:no:1 15
295 Polski Polish:pl:pl:2
297 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
298 Serbski Serbian:sr:yu:5
300 Slovene Slovenian:sl:si:2
301 Sqhip Albanian:sq:sq:1 15
302 Svenska Swedish:sv:fi se:1 15
304 Turkish:tr:tr:9 turkish8
308 if ($^O eq 'os390') {
309 # These cause heartburn. Broken locales?
310 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
311 $locales =~ s/Thai:th:th:11 tis620\n//;
314 sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
317 require "lib/locale/utf8";
319 require "lib/locale/latin1";
330 sort grep /\w/, map { chr } 0..255
335 if (setlocale(LC_ALL, $locale)) {
336 push @Locale, $locale;
340 sub decode_encodings {
343 foreach (split(/ /, shift)) {
345 push @enc, "ISO8859-$1";
346 push @enc, "iso8859$1"; # HP
348 push @enc, "roman8"; # HP
352 push @enc, "$_.UTF-8";
355 if ($^O eq 'os390') {
356 push @enc, qw(IBM-037 IBM-819 IBM-1047);
365 trylocale("ISO8859-$_");
366 trylocale("iso8859$_");
367 trylocale("iso8859-$_");
368 trylocale("iso_8859_$_");
369 trylocale("isolatin$_");
370 trylocale("isolatin-$_");
371 trylocale("iso_latin_$_");
374 # Sanitize the environment so that we can run the external 'locale'
375 # program without the taint mode getting grumpy.
377 # $ENV{PATH} is special in VMS.
378 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
380 # Other subversive stuff.
381 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
383 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
385 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
386 # ain't great when we're running this testPERL_UNICODE= so that utf8
387 # locales will cause all IO hadles to default to (assume) utf8
388 next unless utf8::valid($_);
393 } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
394 # The SYS$I18N_LOCALE logical name search list was not present on
395 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
396 opendir(LOCALES, "SYS\$I18N_LOCALE:");
397 while ($_ = readdir(LOCALES)) {
404 # This is going to be slow.
406 foreach my $locale (split(/\n/, $locales)) {
407 my ($locale_name, $language_codes, $country_codes, $encodings) =
409 my @enc = decode_encodings($encodings);
410 foreach my $loc (split(/ /, $locale_name)) {
412 foreach my $enc (@enc) {
413 trylocale("$loc.$enc");
416 foreach my $enc (@enc) {
417 trylocale("$loc.$enc");
420 foreach my $lang (split(/ /, $language_codes)) {
422 foreach my $country (split(/ /, $country_codes)) {
423 my $lc = "${lang}_${country}";
425 foreach my $enc (@enc) {
426 trylocale("$lc.$enc");
428 my $lC = "${lang}_\U${country}";
430 foreach my $enc (@enc) {
431 trylocale("$lC.$enc");
438 setlocale(LC_ALL, "C");
440 if ($^O eq 'darwin') {
441 # Darwin 8/Mac OS X 10.4 has bad Catalan locales: perl bug #35895,
442 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
443 if ($Config{osvers} ge '8' and $Config{osvers} le '8.3.0') {
444 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
445 @Locale = grep ! m/^(eu_ES|be_BY.CP1131$)/, @Locale;
449 @Locale = sort @Locale;
451 debug "# Locales =\n";
463 my ($Locale, $i, $test) = @_;
465 $Problem{$i}{$Locale} = 1;
466 debug "# failed $i with locale '$Locale'\n";
468 push @{$Okay{$i}}, $Locale;
472 foreach $Locale (@Locale) {
473 debug "# Locale = $Locale\n";
474 @Alnum_ = getalnum_();
475 debug "# w = ", join("",@Alnum_), "\n";
477 unless (setlocale(LC_ALL, $Locale)) {
479 $Problem{$_}{$Locale} = -1;
484 # Sieve the uppercase and the lowercase.
490 if (/[^\d_]/) { # skip digits and the _
499 foreach (keys %UPPER) {
500 $BoThCaSe{$_}++ if exists $lower{$_};
502 foreach (keys %lower) {
503 $BoThCaSe{$_}++ if exists $UPPER{$_};
505 foreach (keys %BoThCaSe) {
510 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
511 debug "# lower = ", join("", sort keys %lower ), "\n";
512 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
514 # Find the alphabets that are not alphabets in the default locale.
520 for (keys %UPPER, keys %lower) {
521 push(@Neoalpha, $_) if (/\W/);
526 @Neoalpha = sort @Neoalpha;
528 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
530 if (@Neoalpha == 0) {
531 # If we have no Neoalphas the remaining tests are no-ops.
532 debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
534 push @{$Okay{$_}}, $Locale;
540 my $word = join('', @Neoalpha);
544 local $SIG{__WARN__} = sub {
545 $badutf8 = $_[0] =~ /Malformed UTF-8/;
547 $Locale =~ /utf-?8/i;
551 debug "# Locale name contains bad UTF-8, skipping test 99 for locale '$Locale'\n";
552 } elsif ($Locale =~ /utf-?8/i) {
553 debug "# unknown whether locale and Unicode have the same \\w, skipping test 99 for locale '$Locale'\n";
554 push @{$Okay{99}}, $Locale;
556 if ($word =~ /^(\w+)$/) {
557 tryneoalpha($Locale, 99, 1);
559 tryneoalpha($Locale, 99, 0);
563 # Cross-check the whole 8-bit character set.
565 for (map { chr } 0..255) {
566 tryneoalpha($Locale, 100,
572 # Test for read-only scalars' locale vs non-locale comparisons.
579 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
584 my ($from, $to, $lesser, $greater,
585 @test, %test, $test, $yes, $no, $sign);
589 $from = int(($_*@Alnum_)/10);
590 $to = $from + int(@Alnum_/10);
591 $to = $#Alnum_ if ($to > $#Alnum_);
592 $lesser = join('', @Alnum_[$from..$to]);
593 # Select a slice one character on.
595 $to = $#Alnum_ if ($to > $#Alnum_);
596 $greater = join('', @Alnum_[$from..$to]);
597 ($yes, $no, $sign) = ($lesser lt $greater
599 : ("not ", " ", -1));
600 # all these tests should FAIL (return 0).
601 # Exact lt or gt cannot be tested because
602 # in some locales, say, eacute and E may test equal.
605 $no.' ($lesser le $greater)', # 1
606 'not ($lesser ne $greater)', # 2
607 ' ($lesser eq $greater)', # 3
608 $yes.' ($lesser ge $greater)', # 4
609 $yes.' ($lesser ge $greater)', # 5
610 $yes.' ($greater le $lesser )', # 7
611 'not ($greater ne $lesser )', # 8
612 ' ($greater eq $lesser )', # 9
613 $no.' ($greater ge $lesser )', # 10
614 'not (($lesser cmp $greater) == -($sign))' # 11
616 @test{@test} = 0 x @test;
619 $test{$ti} = eval $ti;
622 tryneoalpha($Locale, 102, $test == 0);
624 debug "# lesser = '$lesser'\n";
625 debug "# greater = '$greater'\n";
626 debug "# lesser cmp greater = ",
627 $lesser cmp $greater, "\n";
628 debug "# greater cmp lesser = ",
629 $greater cmp $lesser, "\n";
630 debug "# (greater) from = $from, to = $to\n";
632 debugf("# %-40s %-4s", $ti,
633 $test{$ti} ? 'FAIL' : 'ok');
634 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
635 debugf("(%s == %4d)", $1, eval $1);
648 my ($x, $y) = (1.23, 1.23);
651 printf ''; # printf used to reset locale to "C"
654 debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
656 tryneoalpha($Locale, 103, $a eq $b);
659 my $z = sprintf ''; # sprintf used to reset locale to "C"
662 debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
664 tryneoalpha($Locale, 104, $c eq $d);
669 local $SIG{__WARN__} =
675 # The == (among other ops) used to warn for locales
676 # that had something else than "." as the radix character.
678 tryneoalpha($Locale, 105, $c == 1.23);
680 tryneoalpha($Locale, 106, $c == $x);
682 tryneoalpha($Locale, 107, $c == $d);
685 # no locale; # XXX did this ever work correctly?
689 debug "# 108..110: e = $e, Locale = $Locale\n";
691 tryneoalpha($Locale, 108, $e == 1.23);
693 tryneoalpha($Locale, 109, $e == $x);
695 tryneoalpha($Locale, 110, $e == $c);
701 debug "# 111..115: f = $f, g = $g, locale = $Locale\n";
703 tryneoalpha($Locale, 111, $f == 1.23);
705 tryneoalpha($Locale, 112, $f == $x);
707 tryneoalpha($Locale, 113, $f == $c);
709 tryneoalpha($Locale, 114, abs(($f + $g) - 3.57) < 0.01);
711 tryneoalpha($Locale, 115, $w == 0);
714 # Does taking lc separately differ from taking
715 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
716 # The bug was in the caching of the 'o'-magic.
723 return $lc0 cmp $lc1;
727 return lc($_[0]) cmp lc($_[1]);
734 tryneoalpha($Locale, 116,
735 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
736 lcA($x, $z) == 0 && lcB($x, $z) == 0);
739 # Does lc of an UPPER (if different from the UPPER) match
740 # case-insensitively the UPPER, and does the UPPER match
741 # case-insensitively the lc of the UPPER. And vice versa.
745 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
748 foreach my $x (keys %UPPER) {
750 next unless uc $y eq $x;
751 print "# UPPER $x lc $y ",
752 $x =~ /$y/i ? 1 : 0, " ",
753 $y =~ /$x/i ? 1 : 0, "\n" if 0;
755 # If $x and $y contain regular expression characters
756 # AND THEY lowercase (/i) to regular expression characters,
757 # regcomp() will be mightily confused. No, the \Q doesn't
758 # help here (maybe regex engine internal lowercasing
759 # is done after the \Q?) An example of this happening is
760 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
761 # the chr(173) (the "[") is the lowercase of the chr(235).
763 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
764 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
765 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
766 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
767 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
768 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
770 # Similar things can happen even under (bastardised)
771 # non-EBCDIC locales: in many European countries before the
772 # advent of ISO 8859-x nationally customised versions of
773 # ISO 646 were devised, reusing certain punctuation
774 # characters for modified characters needed by the
775 # country/language. For example, the "|" might have
776 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
778 if ($x =~ $re || $y =~ $re) {
779 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
782 # With utf8 both will fail since the locale concept
783 # of upper/lower does not work well in Unicode.
784 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
786 foreach my $x (keys %lower) {
788 next unless lc $y eq $x;
789 print "# lower $x uc $y ",
790 $x =~ /$y/i ? 1 : 0, " ",
791 $y =~ /$x/i ? 1 : 0, "\n" if 0;
792 if ($x =~ $re || $y =~ $re) { # See above.
793 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
796 # With utf8 both will fail since the locale concept
797 # of upper/lower does not work well in Unicode.
798 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
800 tryneoalpha($Locale, 117, @f == 0);
802 print "# failed 117 locale '$Locale' characters @f\n"
808 # Recount the errors.
810 foreach (&last_without_setlocale()+1..$last) {
811 if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
813 print "# The failure of test 102 is not necessarily fatal.\n";
814 print "# It usually indicates a problem in the environment,\n";
815 print "# not in Perl itself.\n";
826 foreach (99..$last) {
828 my @f = sort keys %{ $Problem{$_} };
829 my $f = join(" ", @f);
830 $f =~ s/(.{50,60}) /$1\n#\t/g;
833 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
835 "# on your system may have errors because the locale test $_\n",
836 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
840 # If your users are not using these locales you are safe for the moment,
841 # but please report this failure first to perlbug\@perl.com using the
842 # perlbug script (as described in the INSTALL file) so that the exact
843 # details of the failures can be sorted out first and then your operating
844 # system supplier can be alerted about these anomalies.
851 # Tell which locales were okay and which were not.
856 foreach my $l (@Locale) {
858 foreach my $t (102..$last) {
859 $p++ if $Problem{$t}{$l};
861 push @s, $l if $p == 0;
862 push @F, $l unless $p == 0;
866 my $s = join(" ", @s);
867 $s =~ s/(.{50,60}) /$1\n#\t/g;
870 "# The following locales\n#\n",
872 "# tested okay.\n#\n",
874 warn "# None of your locales were fully okay.\n";
878 my $F = join(" ", @F);
879 $F =~ s/(.{50,60}) /$1\n#\t/g;
882 "# The following locales\n#\n",
884 "# had problems.\n#\n",
886 warn "# None of your locales were broken.\n";
890 my $S = join(" ", @utf8locale);
891 $S =~ s/(.{50,60}) /$1\n#\t/g;
893 warn "#\n# The following locales\n#\n",
895 "# were skipped for the tests ",
896 join(" ", sort {$a<=>$b} keys %utf8skip), "\n",
897 "# because UTF-8 and locales do not work together in Perl.\n#\n";