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 my $last = $have_setlocale ? 116 : 98;
46 my ($n, $result) = @_;
48 print 'not ' unless ($result);
52 # First we'll do a lot of taint checking for locales.
53 # This is the easiest to test, actually, as any locale,
54 # even the default locale will taint under 'use locale'.
56 sub is_tainted { # hello, camel two.
57 no warnings 'uninitialized' ;
59 not eval { $dummy = join("", @_), kill 0; 1 }
62 sub check_taint ($$) {
63 ok $_[0], is_tainted($_[1]);
66 sub check_taint_not ($$) {
67 ok $_[0], not is_tainted($_[1]);
70 use locale; # engage locale and therefore locale taint.
72 check_taint_not 1, $a;
74 check_taint 2, uc($a);
75 check_taint 3, "\U$a";
76 check_taint 4, ucfirst($a);
77 check_taint 5, "\u$a";
78 check_taint 6, lc($a);
79 check_taint 7, "\L$a";
80 check_taint 8, lcfirst($a);
81 check_taint 9, "\l$a";
83 check_taint_not 10, sprintf('%e', 123.456);
84 check_taint_not 11, sprintf('%f', 123.456);
85 check_taint_not 12, sprintf('%g', 123.456);
86 check_taint_not 13, sprintf('%d', 123.456);
87 check_taint_not 14, sprintf('%x', 123.456);
91 $_ = uc($a); # taint $_
95 /(\w)/; # taint $&, $`, $', $+, $1.
101 check_taint_not 21, $2;
103 /(.)/; # untaint $&, $`, $', $+, $1.
104 check_taint_not 22, $&;
105 check_taint_not 23, $`;
106 check_taint_not 24, $';
107 check_taint_not 25, $+;
108 check_taint_not 26, $1;
109 check_taint_not 27, $2;
111 /(\W)/; # taint $&, $`, $', $+, $1.
117 check_taint_not 33, $2;
119 /(\s)/; # taint $&, $`, $', $+, $1.
125 check_taint_not 39, $2;
127 /(\S)/; # taint $&, $`, $', $+, $1.
133 check_taint_not 45, $2;
135 $_ = $a; # untaint $_
137 check_taint_not 46, $_;
139 /(b)/; # this must not taint
140 check_taint_not 47, $&;
141 check_taint_not 48, $`;
142 check_taint_not 49, $';
143 check_taint_not 50, $+;
144 check_taint_not 51, $1;
145 check_taint_not 52, $2;
147 $_ = $a; # untaint $_
149 check_taint_not 53, $_;
151 $b = uc($a); # taint $b
152 s/(.+)/$b/; # this must taint only the $_
155 check_taint_not 55, $&;
156 check_taint_not 56, $`;
157 check_taint_not 57, $';
158 check_taint_not 58, $+;
159 check_taint_not 59, $1;
160 check_taint_not 60, $2;
162 $_ = $a; # untaint $_
164 s/(.+)/b/; # this must not taint
165 check_taint_not 61, $_;
166 check_taint_not 62, $&;
167 check_taint_not 63, $`;
168 check_taint_not 64, $';
169 check_taint_not 65, $+;
170 check_taint_not 66, $1;
171 check_taint_not 67, $2;
173 $b = $a; # untaint $b
175 ($b = $a) =~ s/\w/$&/;
176 check_taint 68, $b; # $b should be tainted.
177 check_taint_not 69, $a; # $a should be not.
179 $_ = $a; # untaint $_
181 s/(\w)/\l$1/; # this must taint
188 check_taint_not 76, $2;
190 $_ = $a; # untaint $_
192 s/(\w)/\L$1/; # this must taint
199 check_taint_not 83, $2;
201 $_ = $a; # untaint $_
203 s/(\w)/\u$1/; # this must taint
210 check_taint_not 90, $2;
212 $_ = $a; # untaint $_
214 s/(\w)/\U$1/; # this must taint
221 check_taint_not 97, $2;
223 # After all this tainting $a should be cool.
225 check_taint_not 98, $a;
227 # I think we've seen quite enough of taint.
228 # Let us do some *real* locale work now,
229 # unless setlocale() is missing (i.e. minitest).
231 exit unless $have_setlocale;
235 debug "# Scanning for locales...\n";
237 # Note that it's okay that some languages have their native names
238 # capitalized here even though that's not "right". They are lowercased
239 # anyway later during the scanning process (and besides, some clueless
240 # vendor might have them capitalized errorneously anyway).
244 Arabic:ar:dz eg sa:6 arabic8
245 Brezhoneg Breton:br:fr:1 15
246 Bulgarski Bulgarian:bg:bg:5
247 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
248 Hrvatski Croatian:hr:hr:2
249 Cymraeg Welsh:cy:cy:1 14 15
251 Dansk Danish:dk:da:1 15
252 Nederlands Dutch:nl:be nl:1 15
253 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
255 Eesti Estonian:et:ee:4 6 13
256 Suomi Finnish:fi:fi:1 15
258 Deutsch German:de:at be ch de lu:1 15
259 Euskaraz Basque:eu:es fr:1 15
260 Galego Galician:gl:es:1 15
261 Ellada Greek:el:gr:7 g8
263 Greenlandic:kl:gl:4 6
264 Hebrew:iw:il:8 hebrew8
266 Indonesian:in:id:1 15
267 Gaeilge Irish:ga:IE:1 14 15
268 Italiano Italian:it:ch it:1 15
269 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
271 Latine Latin:la:va:1 15
273 Lithuanian:lt:lt:4 6 13
274 Macedonian:mk:mk:1 15
277 Norsk Norwegian:no no\@nynorsk:no:1 15
279 Polski Polish:pl:pl:2
281 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251
282 Serbski Serbian:sr:yu:5
284 Slovene Slovenian:sl:si:2
285 Sqhip Albanian:sq:sq:1 15
286 Svenska Swedish:sv:fi se:1 15
288 Turkish:tr:tr:9 turkish8
292 if ($^O eq 'os390') {
293 # These cause heartburn. Broken locales?
294 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
295 $locales =~ s/Thai:th:th:11 tis620\n//;
298 sub in_utf8 () { $^H & 0x08 }
301 require "pragma/locale/utf8";
303 require "pragma/locale/latin1";
311 sort grep /\w/, map { chr } 0..255
316 if (setlocale(LC_ALL, $locale)) {
317 push @Locale, $locale;
321 sub decode_encodings {
324 foreach (split(/ /, shift)) {
326 push @enc, "ISO8859-$1";
327 push @enc, "iso8859$1"; # HP
329 push @enc, "roman8"; # HP
333 push @enc, "$_.UTF-8";
336 if ($^O eq 'os390') {
337 push @enc, qw(IBM-037 IBM-819 IBM-1047);
346 trylocale("ISO8859-$_");
347 trylocale("iso8859$_");
348 trylocale("iso8859-$_");
349 trylocale("iso_8859_$_");
350 trylocale("isolatin$_");
351 trylocale("isolatin-$_");
352 trylocale("iso_latin_$_");
355 # Sanitize the environment so that we can run the external 'locale'
356 # program without the taint mode getting grumpy.
357 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)};
359 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a|")) {
367 # This is going to be slow.
369 foreach my $locale (split(/\n/, $locales)) {
370 my ($locale_name, $language_codes, $country_codes, $encodings) =
372 my @enc = decode_encodings($encodings);
373 foreach my $loc (split(/ /, $locale_name)) {
375 foreach my $enc (@enc) {
376 trylocale("$loc.$enc");
379 foreach my $enc (@enc) {
380 trylocale("$loc.$enc");
383 foreach my $lang (split(/ /, $language_codes)) {
385 foreach my $country (split(/ /, $country_codes)) {
386 my $lc = "${lang}_${country}";
388 foreach my $enc (@enc) {
389 trylocale("$lc.$enc");
391 my $lC = "${lang}_\U${country}";
393 foreach my $enc (@enc) {
394 trylocale("$lC.$enc");
401 setlocale(LC_ALL, "C");
403 @Locale = sort @Locale;
405 debug "# Locales = @Locale\n";
414 my ($Locale, $i, $test) = @_;
415 debug "# testing $i with locale '$Locale'\n"
416 unless $Testing{$i}{$Locale}++;
418 $Problem{$i}{$Locale} = 1;
419 debug "# failed $i with locale '$Locale'\n";
421 push @{$Okay{$i}}, $Locale;
425 foreach $Locale (@Locale) {
426 debug "# Locale = $Locale\n";
427 @Alnum_ = getalnum_();
428 debug "# \\w = @Alnum_\n";
430 unless (setlocale(LC_ALL, $Locale)) {
432 $Problem{$_}{$Locale} = -1;
437 # Sieve the uppercase and the lowercase.
443 if (/[^\d_]/) { # skip digits and the _
452 foreach (keys %UPPER) {
453 $BoThCaSe{$_}++ if exists $lower{$_};
455 foreach (keys %lower) {
456 $BoThCaSe{$_}++ if exists $UPPER{$_};
458 foreach (keys %BoThCaSe) {
463 debug "# UPPER = ", join(" ", sort keys %UPPER ), "\n";
464 debug "# lower = ", join(" ", sort keys %lower ), "\n";
465 debug "# BoThCaSe = ", join(" ", sort keys %BoThCaSe), "\n";
467 # Find the alphabets that are not alphabets in the default locale.
473 for (keys %UPPER, keys %lower) {
474 push(@Neoalpha, $_) if (/\W/);
479 @Neoalpha = sort @Neoalpha;
481 debug "# Neoalpha = @Neoalpha\n";
483 if (@Neoalpha == 0) {
484 # If we have no Neoalphas the remaining tests are no-ops.
485 debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
487 push @{$Okay{$_}}, $Locale;
494 my $word = join('', @Neoalpha);
498 tryneoalpha($Locale, 99, $1 eq $word);
501 # Cross-check the whole 8-bit character set.
503 for (map { chr } 0..255) {
504 tryneoalpha($Locale, 100,
510 # Test for read-only scalars' locale vs non-locale comparisons.
517 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
522 my ($from, $to, $lesser, $greater,
523 @test, %test, $test, $yes, $no, $sign);
527 $from = int(($_*@Alnum_)/10);
528 $to = $from + int(@Alnum_/10);
529 $to = $#Alnum_ if ($to > $#Alnum_);
530 $lesser = join('', @Alnum_[$from..$to]);
531 # Select a slice one character on.
533 $to = $#Alnum_ if ($to > $#Alnum_);
534 $greater = join('', @Alnum_[$from..$to]);
535 ($yes, $no, $sign) = ($lesser lt $greater
537 : ("not ", " ", -1));
538 # all these tests should FAIL (return 0).
539 # Exact lt or gt cannot be tested because
540 # in some locales, say, eacute and E may test equal.
543 $no.' ($lesser le $greater)', # 1
544 'not ($lesser ne $greater)', # 2
545 ' ($lesser eq $greater)', # 3
546 $yes.' ($lesser ge $greater)', # 4
547 $yes.' ($lesser ge $greater)', # 5
548 $yes.' ($greater le $lesser )', # 7
549 'not ($greater ne $lesser )', # 8
550 ' ($greater eq $lesser )', # 9
551 $no.' ($greater ge $lesser )', # 10
552 'not (($lesser cmp $greater) == -$sign)' # 12
554 @test{@test} = 0 x @test;
557 $test{$ti} = eval $ti;
560 tryneoalpha($Locale, 102, $test == 0);
562 debug "# lesser = '$lesser'\n";
563 debug "# greater = '$greater'\n";
564 debug "# lesser cmp greater = ",
565 $lesser cmp $greater, "\n";
566 debug "# greater cmp lesser = ",
567 $greater cmp $lesser, "\n";
568 debug "# (greater) from = $from, to = $to\n";
570 debugf("# %-40s %-4s", $ti,
571 $test{$ti} ? 'FAIL' : 'ok');
572 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
573 debugf("(%s == %4d)", $1, eval $1);
586 my ($x, $y) = (1.23, 1.23);
589 printf ''; # printf used to reset locale to "C"
592 debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
594 tryneoalpha($Locale, 103, $a eq $b);
597 my $z = sprintf ''; # sprintf used to reset locale to "C"
600 debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
602 tryneoalpha($Locale, 104, $c eq $d);
607 local $SIG{__WARN__} = sub { $w++ };
609 # the == (among other ops) used to warn for locales
610 # that had something else than "." as the radix character
612 tryneoalpha($Locale, 105, $c == 1.23);
614 tryneoalpha($Locale, 106, $c == $x);
616 tryneoalpha($Locale, 107, $c == $d);
623 debug "# 108..110: e = $e, Locale = $Locale\n";
625 tryneoalpha($Locale, 108, $e == 1.23);
627 tryneoalpha($Locale, 109, $e == $x);
629 tryneoalpha($Locale, 110, $e == $c);
632 tryneoalpha($Locale, 111, $w == 0);
636 debug "# 112..114: f = $f, locale = $Locale\n";
638 tryneoalpha($Locale, 112, $f == 1.23);
640 tryneoalpha($Locale, 113, $f == $x);
642 tryneoalpha($Locale, 114, $f == $c);
645 debug "# testing 115 with locale '$Locale'\n";
646 # Does taking lc separately differ from taking
647 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
648 # The bug was in the caching of the 'o'-magic.
655 return $lc0 cmp $lc1;
659 return lc($_[0]) cmp lc($_[1]);
666 tryneoalpha($Locale, 115,
667 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
668 lcA($x, $z) == 0 && lcB($x, $z) == 0);
671 debug "# testing 116 with locale '$Locale'\n";
672 # Does lc of an UPPER (if different from the UPPER) match
673 # case-insensitively the UPPER, and does the UPPER match
674 # case-insensitively the lc of the UPPER. And vice versa.
679 foreach my $x (keys %UPPER) {
681 next unless uc $y eq $x;
682 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
684 foreach my $x (keys %lower) {
686 next unless lc $y eq $x;
687 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
689 tryneoalpha($Locale, 116, @f == 0);
691 print "# failed 116 locale '$Locale' characters @f\n"
697 # Recount the errors.
699 foreach (99..$last) {
700 if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
702 print "# The failure of test 102 is not necessarily fatal.\n";
703 print "# It usually indicates a problem in the enviroment,\n";
704 print "# not in Perl itself.\n";
715 foreach (99..$last) {
717 my @f = sort keys %{ $Problem{$_} };
718 my $f = join(" ", @f);
719 $f =~ s/(.{50,60}) /$1\n#\t/g;
722 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
724 "# on your system may have errors because the locale test $_\n",
725 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
729 # If your users are not using these locales you are safe for the moment,
730 # but please report this failure first to perlbug\@perl.com using the
731 # perlbug script (as described in the INSTALL file) so that the exact
732 # details of the failures can be sorted out first and then your operating
733 # system supplier can be alerted about these anomalies.
740 # Tell which locales were okay and which were not.
745 foreach my $l (@Locale) {
747 foreach my $t (102..$last) {
748 $p++ if $Problem{$t}{$l};
750 push @s, $l if $p == 0;
751 push @F, $l unless $p == 0;
755 my $s = join(" ", @s);
756 $s =~ s/(.{50,60}) /$1\n#\t/g;
759 "# The following locales\n#\n",
761 "# tested okay.\n#\n",
763 warn "# None of your locales were fully okay.\n";
767 my $F = join(" ", @F);
768 $F =~ s/(.{50,60}) /$1\n#\t/g;
771 "# The following locales\n#\n",
773 "# had problems.\n#\n",
775 warn "# None of your locales were broken.\n";