5 unshift @INC, '../lib';
7 require Config; import Config;
8 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
26 my $have_setlocale = 0;
29 import POSIX ':locale_h';
33 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
34 # and mingw32 uses said silly CRT
35 $have_setlocale = 0 if $^O eq 'MSWin32' && $Config{cc} =~ /^(cl|gcc)/i;
37 print "1..", ($have_setlocale ? 116 : 98), "\n";
44 my ($n, $result) = @_;
46 print 'not ' unless ($result);
50 # First we'll do a lot of taint checking for locales.
51 # This is the easiest to test, actually, as any locale,
52 # even the default locale will taint under 'use locale'.
54 sub is_tainted { # hello, camel two.
55 no warnings 'uninitialized' ;
57 not eval { $dummy = join("", @_), kill 0; 1 }
60 sub check_taint ($$) {
61 ok $_[0], is_tainted($_[1]);
64 sub check_taint_not ($$) {
65 ok $_[0], not is_tainted($_[1]);
68 use locale; # engage locale and therefore locale taint.
70 check_taint_not 1, $a;
72 check_taint 2, uc($a);
73 check_taint 3, "\U$a";
74 check_taint 4, ucfirst($a);
75 check_taint 5, "\u$a";
76 check_taint 6, lc($a);
77 check_taint 7, "\L$a";
78 check_taint 8, lcfirst($a);
79 check_taint 9, "\l$a";
81 check_taint_not 10, sprintf('%e', 123.456);
82 check_taint_not 11, sprintf('%f', 123.456);
83 check_taint_not 12, sprintf('%g', 123.456);
84 check_taint_not 13, sprintf('%d', 123.456);
85 check_taint_not 14, sprintf('%x', 123.456);
89 $_ = uc($a); # taint $_
93 /(\w)/; # taint $&, $`, $', $+, $1.
99 check_taint_not 21, $2;
101 /(.)/; # untaint $&, $`, $', $+, $1.
102 check_taint_not 22, $&;
103 check_taint_not 23, $`;
104 check_taint_not 24, $';
105 check_taint_not 25, $+;
106 check_taint_not 26, $1;
107 check_taint_not 27, $2;
109 /(\W)/; # taint $&, $`, $', $+, $1.
115 check_taint_not 33, $2;
117 /(\s)/; # taint $&, $`, $', $+, $1.
123 check_taint_not 39, $2;
125 /(\S)/; # taint $&, $`, $', $+, $1.
131 check_taint_not 45, $2;
133 $_ = $a; # untaint $_
135 check_taint_not 46, $_;
137 /(b)/; # this must not taint
138 check_taint_not 47, $&;
139 check_taint_not 48, $`;
140 check_taint_not 49, $';
141 check_taint_not 50, $+;
142 check_taint_not 51, $1;
143 check_taint_not 52, $2;
145 $_ = $a; # untaint $_
147 check_taint_not 53, $_;
149 $b = uc($a); # taint $b
150 s/(.+)/$b/; # this must taint only the $_
153 check_taint_not 55, $&;
154 check_taint_not 56, $`;
155 check_taint_not 57, $';
156 check_taint_not 58, $+;
157 check_taint_not 59, $1;
158 check_taint_not 60, $2;
160 $_ = $a; # untaint $_
162 s/(.+)/b/; # this must not taint
163 check_taint_not 61, $_;
164 check_taint_not 62, $&;
165 check_taint_not 63, $`;
166 check_taint_not 64, $';
167 check_taint_not 65, $+;
168 check_taint_not 66, $1;
169 check_taint_not 67, $2;
171 $b = $a; # untaint $b
173 ($b = $a) =~ s/\w/$&/;
174 check_taint 68, $b; # $b should be tainted.
175 check_taint_not 69, $a; # $a should be not.
177 $_ = $a; # untaint $_
179 s/(\w)/\l$1/; # this must taint
186 check_taint_not 76, $2;
188 $_ = $a; # untaint $_
190 s/(\w)/\L$1/; # this must taint
197 check_taint_not 83, $2;
199 $_ = $a; # untaint $_
201 s/(\w)/\u$1/; # this must taint
208 check_taint_not 90, $2;
210 $_ = $a; # untaint $_
212 s/(\w)/\U$1/; # this must taint
219 check_taint_not 97, $2;
221 # After all this tainting $a should be cool.
223 check_taint_not 98, $a;
225 # I think we've seen quite enough of taint.
226 # Let us do some *real* locale work now,
227 # unless setlocale() is missing (i.e. minitest).
229 exit unless $have_setlocale;
233 debug "# Scanning for locales...\n";
235 # Note that it's okay that some languages have their native names
236 # capitalized here even though that's not "right". They are lowercased
237 # anyway later during the scanning process (and besides, some clueless
238 # vendor might have them capitalized errorneously anyway).
242 Arabic:ar:dz eg sa:6 arabic8
243 Brezhoneg Breton:br:fr:1 15
244 Bulgarski Bulgarian:bg:bg:5
245 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW GB2312 tw.EUC
246 Hrvatski Croatian:hr:hr:2
247 Cymraeg Welsh:cy:cy:1 14 15
249 Dansk Danish:dk:da:1 15
250 Nederlands Dutch:nl:be nl:1 15
251 English American British:en:au ca gb ie nz us uk:1 15 cp850
253 Eesti Estonian:et:ee:4 6 13
254 Suomi Finnish:fi:fi:1 15
256 Deutsch German:de:at be ch de lu:1 15
257 Euskaraz Basque:eu:es fr:1 15
258 Galego Galician:gl:es:1 15
259 Ellada Greek:el:gr:7 g8
261 Greenlandic:kl:gl:4 6
262 Hebrew:iw:il:8 hebrew8
264 Indonesian:in:id:1 15
265 Gaeilge Irish:ga:IE:1 14 15
266 Italiano Italian:it:ch it:1 15
267 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
269 Latine Latin:la:va:1 15
271 Lithuanian:lt:lt:4 6 13
272 Macedonian:mk:mk:1 15
274 Norsk Norwegian:no:no:1 15
276 Polski Polish:pl:pl:2
278 Russki Russian:ru:ru su ua:5 koi8 koi8r koi8u cp1251
279 Serbski Serbian:sr:yu:5
281 Slovene Slovenian:sl:si:2
282 Sqhip Albanian:sq:sq:1 15
283 Svenska Swedish:sv:fi se:1 15
285 Turkish:tr:tr:9 turkish8
289 if ($^O eq 'os390') {
290 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
291 $locales =~ s/Thai:th:th:11 tis620\n//;
294 sub in_utf8 () { $^H & 0x08 }
297 require "pragma/locale/utf8";
299 require "pragma/locale/latin1";
307 sort grep /\w/, map { chr } 0..255
312 if (setlocale(LC_ALL, $locale)) {
313 push @Locale, $locale;
317 sub decode_encodings {
320 foreach (split(/ /, shift)) {
322 push @enc, "ISO8859-$1";
323 push @enc, "iso8859$1"; # HP
325 push @enc, "roman8"; # HP
331 if ($^O eq 'os390') {
332 push @enc, qw(IBM-037 IBM-819 IBM-1047);
341 trylocale("ISO8859-$_");
342 trylocale("iso8859$_");
343 trylocale("iso8859-$_");
344 trylocale("iso_8859_$_");
345 trylocale("isolatin$_");
346 trylocale("isolatin-$_");
347 trylocale("iso_latin_$_");
350 foreach my $locale (split(/\n/, $locales)) {
351 my ($locale_name, $language_codes, $country_codes, $encodings) =
353 my @enc = decode_encodings($encodings);
354 foreach my $loc (split(/ /, $locale_name)) {
356 foreach my $enc (@enc) {
357 trylocale("$loc.$enc");
360 foreach my $enc (@enc) {
361 trylocale("$loc.$enc");
364 foreach my $lang (split(/ /, $language_codes)) {
366 foreach my $country (split(/ /, $country_codes)) {
367 my $lc = "${lang}_${country}";
369 foreach my $enc (@enc) {
370 trylocale("$lc.$enc");
372 my $lC = "${lang}_\U${country}";
374 foreach my $enc (@enc) {
375 trylocale("$lC.$enc");
381 setlocale(LC_ALL, "C");
383 @Locale = sort @Locale;
385 debug "# Locales = @Locale\n";
394 my ($Locale, $i, $test) = @_;
395 debug "# testing $i with locale '$Locale'\n"
396 unless $Testing{$i}{$Locale}++;
398 $Problem{$i}{$Locale} = 1;
399 debug "# failed $i with locale '$Locale'\n";
401 push @{$Okay{$i}}, $Locale;
405 foreach $Locale (@Locale) {
406 debug "# Locale = $Locale\n";
407 @Alnum_ = getalnum_();
408 debug "# \\w = @Alnum_\n";
410 unless (setlocale(LC_ALL, $Locale)) {
412 $Problem{$_}{$Locale} = -1;
417 # Sieve the uppercase and the lowercase.
423 if (/[^\d_]/) { # skip digits and the _
432 foreach (keys %UPPER) {
433 $BoThCaSe{$_}++ if exists $lower{$_};
435 foreach (keys %lower) {
436 $BoThCaSe{$_}++ if exists $UPPER{$_};
438 foreach (keys %BoThCaSe) {
443 debug "# UPPER = ", join(" ", sort keys %UPPER ), "\n";
444 debug "# lower = ", join(" ", sort keys %lower ), "\n";
445 debug "# BoThCaSe = ", join(" ", sort keys %BoThCaSe), "\n";
447 # Find the alphabets that are not alphabets in the default locale.
453 for (keys %UPPER, keys %lower) {
454 push(@Neoalpha, $_) if (/\W/);
459 @Neoalpha = sort @Neoalpha;
461 debug "# Neoalpha = @Neoalpha\n";
463 if (@Neoalpha == 0) {
464 # If we have no Neoalphas the remaining tests are no-ops.
465 debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
467 push @{$Okay{$_}}, $Locale;
474 my $word = join('', @Neoalpha);
478 tryneoalpha($Locale, 99, $1 eq $word);
481 # Cross-check the whole 8-bit character set.
483 for (map { chr } 0..255) {
484 tryneoalpha($Locale, 100,
490 # Test for read-only scalars' locale vs non-locale comparisons.
497 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
502 my ($from, $to, $lesser, $greater,
503 @test, %test, $test, $yes, $no, $sign);
507 $from = int(($_*@Alnum_)/10);
508 $to = $from + int(@Alnum_/10);
509 $to = $#Alnum_ if ($to > $#Alnum_);
510 $lesser = join('', @Alnum_[$from..$to]);
511 # Select a slice one character on.
513 $to = $#Alnum_ if ($to > $#Alnum_);
514 $greater = join('', @Alnum_[$from..$to]);
515 ($yes, $no, $sign) = ($lesser lt $greater
517 : ("not ", " ", -1));
518 # all these tests should FAIL (return 0).
519 # Exact lt or gt cannot be tested because
520 # in some locales, say, eacute and E may test equal.
523 $no.' ($lesser le $greater)', # 1
524 'not ($lesser ne $greater)', # 2
525 ' ($lesser eq $greater)', # 3
526 $yes.' ($lesser ge $greater)', # 4
527 $yes.' ($lesser ge $greater)', # 5
528 $yes.' ($greater le $lesser )', # 7
529 'not ($greater ne $lesser )', # 8
530 ' ($greater eq $lesser )', # 9
531 $no.' ($greater ge $lesser )', # 10
532 'not (($lesser cmp $greater) == -$sign)' # 12
534 @test{@test} = 0 x @test;
537 $test{$ti} = eval $ti;
540 tryneoalpha($Locale, 102, $test == 0);
542 debug "# lesser = '$lesser'\n";
543 debug "# greater = '$greater'\n";
544 debug "# lesser cmp greater = ",
545 $lesser cmp $greater, "\n";
546 debug "# greater cmp lesser = ",
547 $greater cmp $lesser, "\n";
548 debug "# (greater) from = $from, to = $to\n";
550 debugf("# %-40s %-4s", $ti,
551 $test{$ti} ? 'FAIL' : 'ok');
552 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
553 debugf("(%s == %4d)", $1, eval $1);
566 my ($x, $y) = (1.23, 1.23);
569 printf ''; # printf used to reset locale to "C"
572 debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
574 tryneoalpha($Locale, 103, $a eq $b);
577 my $z = sprintf ''; # sprintf used to reset locale to "C"
580 debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
582 tryneoalpha($Locale, 104, $c eq $d);
587 local $SIG{__WARN__} = sub { $w++ };
589 # the == (among other ops) used to warn for locales
590 # that had something else than "." as the radix character
592 tryneoalpha($Locale, 105, $c == 1.23);
594 tryneoalpha($Locale, 106, $c == $x);
596 tryneoalpha($Locale, 107, $c == $d);
603 debug "# 108..110: e = $e, Locale = $Locale\n";
605 tryneoalpha($Locale, 108, $e == 1.23);
607 tryneoalpha($Locale, 109, $e == $x);
609 tryneoalpha($Locale, 110, $e == $c);
612 tryneoalpha($Locale, 111, $w == 0);
616 debug "# 112..114: f = $f, locale = $Locale\n";
618 tryneoalpha($Locale, 112, $f == 1.23);
620 tryneoalpha($Locale, 113, $f == $x);
622 tryneoalpha($Locale, 114, $f == $c);
625 debug "# testing 115 with locale '$Locale'\n";
632 return $lc0 cmp $lc1;
636 return lc($_[0]) cmp lc($_[1]);
643 tryneoalpha($Locale, 115,
644 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
645 lcA($x, $z) == 0 && lcB($x, $z) == 0);
648 debug "# testing 116 with locale '$Locale'\n";
653 foreach my $x (keys %UPPER) {
655 next unless uc $y eq $x;
656 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
658 foreach my $x (keys %lower) {
660 next unless lc $y eq $x;
661 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
663 tryneoalpha($Locale, 116, @f == 0);
664 print "# testing 116 failed for locale '$Locale' for characters @f\n"
670 # Recount the errors.
673 if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
675 print "# The failure of test 102 is not necessarily fatal.\n";
676 print "# It usually indicates a problem in the enviroment,\n";
677 print "# not in Perl itself.\n";
690 my @f = sort keys %{ $Problem{$_} };
691 my $f = join(" ", @f);
692 $f =~ s/(.{50,60}) /$1\n#\t/g;
695 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
697 "# on your system may have errors because the locale test $_\n",
698 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
702 # If your users are not using these locales you are safe for the moment,
703 # but please report this failure first to perlbug\@perl.com using the
704 # perlbug script (as described in the INSTALL file) so that the exact
705 # details of the failures can be sorted out first and then your operating
706 # system supplier can be alerted about these anomalies.
713 # Tell which locales were okay.
718 foreach my $l (@Locale) {
720 foreach my $t (102..116) {
721 $p++ if $Problem{$t}{$l};
723 push @s, $l if $p == 0;
727 my $s = join(" ", @s);
728 $s =~ s/(.{50,60}) /$1\n#\t/g;
731 "# The following locales\n#\n",
733 "# tested okay.\n#\n",
735 warn "# None of your locales was fully okay.\n";