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 my $last = $have_setlocale ? &last : &last_without_setlocale;
57 my ($n, $result) = @_;
59 print 'not ' unless ($result);
63 # First we'll do a lot of taint checking for locales.
64 # This is the easiest to test, actually, as any locale,
65 # even the default locale will taint under 'use locale'.
67 sub is_tainted { # hello, camel two.
68 no warnings 'uninitialized' ;
70 not eval { $dummy = join("", @_), kill 0; 1 }
73 sub check_taint ($$) {
74 ok $_[0], is_tainted($_[1]);
77 sub check_taint_not ($$) {
78 ok $_[0], not is_tainted($_[1]);
81 use locale; # engage locale and therefore locale taint.
83 check_taint_not 1, $a;
85 check_taint 2, uc($a);
86 check_taint 3, "\U$a";
87 check_taint 4, ucfirst($a);
88 check_taint 5, "\u$a";
89 check_taint 6, lc($a);
90 check_taint 7, "\L$a";
91 check_taint 8, lcfirst($a);
92 check_taint 9, "\l$a";
94 check_taint_not 10, sprintf('%e', 123.456);
95 check_taint_not 11, sprintf('%f', 123.456);
96 check_taint_not 12, sprintf('%g', 123.456);
97 check_taint_not 13, sprintf('%d', 123.456);
98 check_taint_not 14, sprintf('%x', 123.456);
100 $_ = $a; # untaint $_
102 $_ = uc($a); # taint $_
106 /(\w)/; # taint $&, $`, $', $+, $1.
112 check_taint_not 21, $2;
114 /(.)/; # untaint $&, $`, $', $+, $1.
115 check_taint_not 22, $&;
116 check_taint_not 23, $`;
117 check_taint_not 24, $';
118 check_taint_not 25, $+;
119 check_taint_not 26, $1;
120 check_taint_not 27, $2;
122 /(\W)/; # taint $&, $`, $', $+, $1.
128 check_taint_not 33, $2;
130 /(\s)/; # taint $&, $`, $', $+, $1.
136 check_taint_not 39, $2;
138 /(\S)/; # taint $&, $`, $', $+, $1.
144 check_taint_not 45, $2;
146 $_ = $a; # untaint $_
148 check_taint_not 46, $_;
150 /(b)/; # this must not taint
151 check_taint_not 47, $&;
152 check_taint_not 48, $`;
153 check_taint_not 49, $';
154 check_taint_not 50, $+;
155 check_taint_not 51, $1;
156 check_taint_not 52, $2;
158 $_ = $a; # untaint $_
160 check_taint_not 53, $_;
162 $b = uc($a); # taint $b
163 s/(.+)/$b/; # this must taint only the $_
166 check_taint_not 55, $&;
167 check_taint_not 56, $`;
168 check_taint_not 57, $';
169 check_taint_not 58, $+;
170 check_taint_not 59, $1;
171 check_taint_not 60, $2;
173 $_ = $a; # untaint $_
175 s/(.+)/b/; # this must not taint
176 check_taint_not 61, $_;
177 check_taint_not 62, $&;
178 check_taint_not 63, $`;
179 check_taint_not 64, $';
180 check_taint_not 65, $+;
181 check_taint_not 66, $1;
182 check_taint_not 67, $2;
184 $b = $a; # untaint $b
186 ($b = $a) =~ s/\w/$&/;
187 check_taint 68, $b; # $b should be tainted.
188 check_taint_not 69, $a; # $a should be not.
190 $_ = $a; # untaint $_
192 s/(\w)/\l$1/; # this must taint
199 check_taint_not 76, $2;
201 $_ = $a; # untaint $_
203 s/(\w)/\L$1/; # this must taint
210 check_taint_not 83, $2;
212 $_ = $a; # untaint $_
214 s/(\w)/\u$1/; # this must taint
221 check_taint_not 90, $2;
223 $_ = $a; # untaint $_
225 s/(\w)/\U$1/; # this must taint
232 check_taint_not 97, $2;
234 # After all this tainting $a should be cool.
236 check_taint_not 98, $a;
238 sub last_without_setlocale { 98 }
240 # I think we've seen quite enough of taint.
241 # Let us do some *real* locale work now,
242 # unless setlocale() is missing (i.e. minitest).
244 exit unless $have_setlocale;
248 debug "# Scanning for locales...\n";
250 # Note that it's okay that some languages have their native names
251 # capitalized here even though that's not "right". They are lowercased
252 # anyway later during the scanning process (and besides, some clueless
253 # vendor might have them capitalized errorneously anyway).
257 Arabic:ar:dz eg sa:6 arabic8
258 Brezhoneg Breton:br:fr:1 15
259 Bulgarski Bulgarian:bg:bg:5
260 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
261 Hrvatski Croatian:hr:hr:2
262 Cymraeg Welsh:cy:cy:1 14 15
264 Dansk Danish:dk:da:1 15
265 Nederlands Dutch:nl:be nl:1 15
266 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
268 Eesti Estonian:et:ee:4 6 13
269 Suomi Finnish:fi:fi:1 15
271 Deutsch German:de:at be ch de lu:1 15
272 Euskaraz Basque:eu:es fr:1 15
273 Galego Galician:gl:es:1 15
274 Ellada Greek:el:gr:7 g8
276 Greenlandic:kl:gl:4 6
277 Hebrew:iw:il:8 hebrew8
279 Indonesian:in:id:1 15
280 Gaeilge Irish:ga:IE:1 14 15
281 Italiano Italian:it:ch it:1 15
282 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
284 Latine Latin:la:va:1 15
286 Lithuanian:lt:lt:4 6 13
287 Macedonian:mk:mk:1 15
290 Norsk Norwegian:no no\@nynorsk:no:1 15
292 Polski Polish:pl:pl:2
294 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
295 Serbski Serbian:sr:yu:5
297 Slovene Slovenian:sl:si:2
298 Sqhip Albanian:sq:sq:1 15
299 Svenska Swedish:sv:fi se:1 15
301 Turkish:tr:tr:9 turkish8
305 if ($^O eq 'os390') {
306 # These cause heartburn. Broken locales?
307 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
308 $locales =~ s/Thai:th:th:11 tis620\n//;
311 sub in_utf8 () { $^H & 0x08 }
314 require "lib/locale/utf8";
316 require "lib/locale/latin1";
327 sort grep /\w/, map { chr } 0..255
332 if (setlocale(LC_ALL, $locale)) {
333 push @Locale, $locale;
337 sub decode_encodings {
340 foreach (split(/ /, shift)) {
342 push @enc, "ISO8859-$1";
343 push @enc, "iso8859$1"; # HP
345 push @enc, "roman8"; # HP
349 push @enc, "$_.UTF-8";
352 if ($^O eq 'os390') {
353 push @enc, qw(IBM-037 IBM-819 IBM-1047);
362 trylocale("ISO8859-$_");
363 trylocale("iso8859$_");
364 trylocale("iso8859-$_");
365 trylocale("iso_8859_$_");
366 trylocale("isolatin$_");
367 trylocale("isolatin-$_");
368 trylocale("iso_latin_$_");
371 # Sanitize the environment so that we can run the external 'locale'
372 # program without the taint mode getting grumpy.
374 # $ENV{PATH} is special in VMS.
375 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
377 # Other subversive stuff.
378 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
380 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
386 } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
387 # The SYS$I18N_LOCALE logical name search list was not present on
388 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
389 opendir(LOCALES, "SYS\$I18N_LOCALE:");
390 while ($_ = readdir(LOCALES)) {
397 # This is going to be slow.
399 foreach my $locale (split(/\n/, $locales)) {
400 my ($locale_name, $language_codes, $country_codes, $encodings) =
402 my @enc = decode_encodings($encodings);
403 foreach my $loc (split(/ /, $locale_name)) {
405 foreach my $enc (@enc) {
406 trylocale("$loc.$enc");
409 foreach my $enc (@enc) {
410 trylocale("$loc.$enc");
413 foreach my $lang (split(/ /, $language_codes)) {
415 foreach my $country (split(/ /, $country_codes)) {
416 my $lc = "${lang}_${country}";
418 foreach my $enc (@enc) {
419 trylocale("$lc.$enc");
421 my $lC = "${lang}_\U${country}";
423 foreach my $enc (@enc) {
424 trylocale("$lC.$enc");
431 setlocale(LC_ALL, "C");
433 sub utf8locale { $_[0] =~ /utf-?8/i }
435 @Locale = sort @Locale;
437 debug "# Locales = @Locale\n";
446 my ($Locale, $i, $test) = @_;
448 $Problem{$i}{$Locale} = 1;
449 debug "# failed $i with locale '$Locale'\n";
451 push @{$Okay{$i}}, $Locale;
455 foreach $Locale (@Locale) {
456 debug "# Locale = $Locale\n";
457 @Alnum_ = getalnum_();
458 debug "# w = ", join("",@Alnum_), "\n";
460 unless (setlocale(LC_ALL, $Locale)) {
462 $Problem{$_}{$Locale} = -1;
467 # Sieve the uppercase and the lowercase.
473 if (/[^\d_]/) { # skip digits and the _
482 foreach (keys %UPPER) {
483 $BoThCaSe{$_}++ if exists $lower{$_};
485 foreach (keys %lower) {
486 $BoThCaSe{$_}++ if exists $UPPER{$_};
488 foreach (keys %BoThCaSe) {
493 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
494 debug "# lower = ", join("", sort keys %lower ), "\n";
495 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
497 # Find the alphabets that are not alphabets in the default locale.
503 for (keys %UPPER, keys %lower) {
504 push(@Neoalpha, $_) if (/\W/);
509 @Neoalpha = sort @Neoalpha;
511 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
513 if (@Neoalpha == 0) {
514 # If we have no Neoalphas the remaining tests are no-ops.
515 debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
517 push @{$Okay{$_}}, $Locale;
523 if (utf8locale($Locale)) {
524 # utf8 and locales do not mix.
525 debug "# skipping UTF-8 locale '$Locale'\n";
526 push @utf8locale, $Locale;
527 @utf8skip{99..102} = ();
529 my $word = join('', @Neoalpha);
533 tryneoalpha($Locale, 99, $1 eq $word);
535 # Cross-check the whole 8-bit character set.
537 for (map { chr } 0..255) {
538 tryneoalpha($Locale, 100,
544 # Test for read-only scalars' locale vs non-locale comparisons.
551 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
556 my ($from, $to, $lesser, $greater,
557 @test, %test, $test, $yes, $no, $sign);
561 $from = int(($_*@Alnum_)/10);
562 $to = $from + int(@Alnum_/10);
563 $to = $#Alnum_ if ($to > $#Alnum_);
564 $lesser = join('', @Alnum_[$from..$to]);
565 # Select a slice one character on.
567 $to = $#Alnum_ if ($to > $#Alnum_);
568 $greater = join('', @Alnum_[$from..$to]);
569 ($yes, $no, $sign) = ($lesser lt $greater
571 : ("not ", " ", -1));
572 # all these tests should FAIL (return 0).
573 # Exact lt or gt cannot be tested because
574 # in some locales, say, eacute and E may test equal.
577 $no.' ($lesser le $greater)', # 1
578 'not ($lesser ne $greater)', # 2
579 ' ($lesser eq $greater)', # 3
580 $yes.' ($lesser ge $greater)', # 4
581 $yes.' ($lesser ge $greater)', # 5
582 $yes.' ($greater le $lesser )', # 7
583 'not ($greater ne $lesser )', # 8
584 ' ($greater eq $lesser )', # 9
585 $no.' ($greater ge $lesser )', # 10
586 'not (($lesser cmp $greater) == -($sign))' # 11
588 @test{@test} = 0 x @test;
591 $test{$ti} = eval $ti;
594 tryneoalpha($Locale, 102, $test == 0);
596 debug "# lesser = '$lesser'\n";
597 debug "# greater = '$greater'\n";
598 debug "# lesser cmp greater = ",
599 $lesser cmp $greater, "\n";
600 debug "# greater cmp lesser = ",
601 $greater cmp $lesser, "\n";
602 debug "# (greater) from = $from, to = $to\n";
604 debugf("# %-40s %-4s", $ti,
605 $test{$ti} ? 'FAIL' : 'ok');
606 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
607 debugf("(%s == %4d)", $1, eval $1);
620 my ($x, $y) = (1.23, 1.23);
623 printf ''; # printf used to reset locale to "C"
626 debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
628 tryneoalpha($Locale, 103, $a eq $b);
631 my $z = sprintf ''; # sprintf used to reset locale to "C"
634 debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
636 tryneoalpha($Locale, 104, $c eq $d);
641 local $SIG{__WARN__} =
647 # The == (among other ops) used to warn for locales
648 # that had something else than "." as the radix character.
650 tryneoalpha($Locale, 105, $c == 1.23);
652 tryneoalpha($Locale, 106, $c == $x);
654 tryneoalpha($Locale, 107, $c == $d);
657 # no locale; # XXX did this ever work correctly?
661 debug "# 108..110: e = $e, Locale = $Locale\n";
663 tryneoalpha($Locale, 108, $e == 1.23);
665 tryneoalpha($Locale, 109, $e == $x);
667 tryneoalpha($Locale, 110, $e == $c);
673 debug "# 111..115: f = $f, g = $g, locale = $Locale\n";
675 tryneoalpha($Locale, 111, $f == 1.23);
677 tryneoalpha($Locale, 112, $f == $x);
679 tryneoalpha($Locale, 113, $f == $c);
681 tryneoalpha($Locale, 114, abs(($f + $g) - 3.57) < 0.01);
683 tryneoalpha($Locale, 115, $w == 0);
686 # Does taking lc separately differ from taking
687 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
688 # The bug was in the caching of the 'o'-magic.
695 return $lc0 cmp $lc1;
699 return lc($_[0]) cmp lc($_[1]);
706 tryneoalpha($Locale, 116,
707 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
708 lcA($x, $z) == 0 && lcB($x, $z) == 0);
711 # Does lc of an UPPER (if different from the UPPER) match
712 # case-insensitively the UPPER, and does the UPPER match
713 # case-insensitively the lc of the UPPER. And vice versa.
715 if (utf8locale($Locale)) {
716 # utf8 and locales do not mix.
717 debug "# skipping UTF-8 locale '$Locale'\n";
718 push @utf8locale, $Locale;
725 foreach my $x (keys %UPPER) {
727 next unless uc $y eq $x;
728 print "# UPPER $x lc $y ",
729 $x =~ /$y/i ? 1 : 0, " ",
730 $y =~ /$x/i ? 1 : 0, "\n" if 0;
731 # With utf8 both will fail since the locale concept
732 # of upper/lower does not work well in Unicode.
733 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
735 foreach my $x (keys %lower) {
737 next unless lc $y eq $x;
738 print "# lower $x uc $y ",
739 $x =~ /$y/i ? 1 : 0, " ",
740 $y =~ /$x/i ? 1 : 0, "\n" if 0;
741 # With utf8 both will fail since the locale concept
742 # of upper/lower does not work well in Unicode.
743 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
745 tryneoalpha($Locale, 117, @f == 0);
747 print "# failed 117 locale '$Locale' characters @f\n"
753 # Recount the errors.
755 foreach (&last_without_setlocale()+1..$last) {
756 if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
758 print "# The failure of test 102 is not necessarily fatal.\n";
759 print "# It usually indicates a problem in the enviroment,\n";
760 print "# not in Perl itself.\n";
771 foreach (99..$last) {
773 my @f = sort keys %{ $Problem{$_} };
774 my $f = join(" ", @f);
775 $f =~ s/(.{50,60}) /$1\n#\t/g;
778 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
780 "# on your system may have errors because the locale test $_\n",
781 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
785 # If your users are not using these locales you are safe for the moment,
786 # but please report this failure first to perlbug\@perl.com using the
787 # perlbug script (as described in the INSTALL file) so that the exact
788 # details of the failures can be sorted out first and then your operating
789 # system supplier can be alerted about these anomalies.
796 # Tell which locales were okay and which were not.
801 foreach my $l (@Locale) {
803 foreach my $t (102..$last) {
804 $p++ if $Problem{$t}{$l};
806 push @s, $l if $p == 0;
807 push @F, $l unless $p == 0;
811 my $s = join(" ", @s);
812 $s =~ s/(.{50,60}) /$1\n#\t/g;
815 "# The following locales\n#\n",
817 "# tested okay.\n#\n",
819 warn "# None of your locales were fully okay.\n";
823 my $F = join(" ", @F);
824 $F =~ s/(.{50,60}) /$1\n#\t/g;
827 "# The following locales\n#\n",
829 "# had problems.\n#\n",
831 warn "# None of your locales were broken.\n";
835 my $S = join(" ", @utf8locale);
836 $S =~ s/(.{50,60}) /$1\n#\t/g;
838 warn "#\n# The following locales\n#\n",
840 "# were skipped for the tests ",
841 join(" ", sort {$a<=>$b} keys %utf8skip), "\n",
842 "# because UTF-8 and locales do not work together in Perl.\n#\n";