1 # Common tools for test files files to find the locales which exist on the
2 # system. Caller should have verified that this isn't miniperl before calling
5 # Note that it's okay that some languages have their native names
6 # capitalized here even though that's not "right". They are lowercased
7 # anyway later during the scanning process (and besides, some clueless
8 # vendor might have them capitalized erroneously anyway).
10 # Functions whose names begin with underscore are internal helper functions
11 # for this file, and are not to be used by outside callers.
16 eval { require POSIX; import POSIX 'locale_h'; };
17 my $has_locale_h = ! $@;
19 my @known_categories = ( qw(LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY
20 LC_NUMERIC LC_TIME LC_ADDRESS LC_IDENTIFICATION
21 LC_MEASUREMENT LC_PAPER LC_TELEPHONE));
22 my @platform_categories;
24 # LC_ALL can be -1 on some platforms. And, in fact the implementors could
25 # legally use any integer to represent any category. But it makes the most
26 # sense for them to have used small integers. Below, we create new locale
27 # numbers for ones missing from this machine. We make them very negative,
28 # hopefully more negative than anything likely to be a valid category on the
29 # platform, but also below is a check to be sure that our guess is valid.
30 my $max_bad_category_number = -1000000;
32 # Initialize this hash so that it looks like e.g.,
34 # where 6 is the value of &POSIX::LC_CTYPE
38 my $number_for_missing_category = $max_bad_category_number;
39 foreach my $name (@known_categories) {
40 my $number = eval "&POSIX::$name";
42 # Use a negative number (smaller than any legitimate category
43 # number) if the platform doesn't support this category, so we
44 # have an entry for all the ones that might be specified in calls
46 $number = $number_for_missing_category-- if $@;
48 elsif ( $number !~ / ^ -? \d+ $ /x
49 || $number <= $max_bad_category_number)
51 # We think this should be an int. And it has to be larger than
52 # any of our synthetic numbers.
53 die "Unexpected locale category number '$number' for $name"
56 push @platform_categories, $name;
60 $category_name{$number} = "$name";
61 $category_number{$name} = $number;
67 if (defined &main::diag) {
71 local($\, $", $,) = (undef, ' ', '');
72 print STDERR $message, "\n";
78 if (defined &main::fail) {
82 local($\, $", $,) = (undef, ' ', '');
83 print "not ok 0 $message\n";
87 sub _trylocale ($$$$) { # For use only by other functions in this file!
89 # Adds the locale given by the first parameter to the list given by the
90 # 3rd iff the platform supports the locale in each of the category numbers
91 # given by the 2nd parameter, which is either a single category or a
92 # reference to a list of categories. The list MUST be sorted so that
93 # CTYPE is first, COLLATE is last unless ALL is present, in which case
94 # that comes after COLLATE. This is because locale.c detects bad locales
95 # only with CTYPE, and COLLATE on some platforms can core dump if it is a
98 # The 4th parameter is true if to accept locales that aren't apparently
99 # fully compatible with Perl.
102 my $categories = shift;
104 my $allow_incompatible = shift;
106 return if ! $locale || grep { $locale eq $_ } @$list;
108 # This is a toy (pig latin) locale that is not fully implemented on some
110 return if $locale =~ / ^ pig $ /ix;
112 # As of 6.3, this platform's locale handling is basically broken. khw
113 # filed a bug report (no ticket number was returned), and it is supposedly
114 # going to change in a future release, so the statements here below sunset
115 # for any larger version, at which point this may start failing and have
118 # Given a legal individual category, basically whatever you set the locale
119 # to, the return from setlocale() indicates that it has taken effect, even
120 # if it hasn't. However, the return from querying LC_ALL won't reflect
122 if ($Config{osname} =~ /openbsd/i && $locale !~ / ^ (?: C | POSIX ) $/ix) {
123 my ($major, $minor) = $Config{osvers} =~ / ^ ( \d+ ) \. ( \d+ ) /ax;
124 return if ! defined $major || ! defined $minor
125 || $major < 6 || ($major == 6 && $minor <= 3);
128 $categories = [ $categories ] unless ref $categories;
133 use warnings 'locale';
135 local $SIG{__WARN__} = sub {
136 $badutf8 = 1 if grep { /Malformed UTF-8/ } @_;
137 $plays_well = 0 if grep {
138 /Locale .* may not work well(?#
139 )|The Perl program will use the expected meanings/i
143 # Incompatible locales aren't warned about unless using locales.
146 foreach my $category (@$categories) {
147 die "category '$category' must instead be a number"
148 unless $category =~ / ^ -? \d+ $ /x;
150 return unless setlocale($category, $locale);
151 last if $badutf8 || ! $plays_well;
155 _my_fail("Verify locale name doesn't contain malformed utf8");
158 push @$list, $locale if $plays_well || $allow_incompatible;
161 sub _decode_encodings { # For use only by other functions in this file!
164 foreach (split(/ /, shift)) {
166 push @enc, "ISO8859-$1";
167 push @enc, "iso8859$1"; # HP
169 push @enc, "roman8"; # HP
172 push @enc, "$_.UTF-8";
173 push @enc, "$_.65001"; # Windows UTF-8
174 push @enc, "$_.ACP"; # Windows ANSI code page
175 push @enc, "$_.OCP"; # Windows OEM code page
176 push @enc, "$_.1252"; # Windows
179 if ($^O eq 'os390') {
180 push @enc, qw(IBM-037 IBM-819 IBM-1047);
183 push @enc, "65001"; # Windows UTF-8
188 sub valid_locale_categories() {
189 # Returns a list of the locale categories (expressed as strings, like
190 # "LC_ALL) known to this program that are available on this platform.
192 return @platform_categories;
195 sub locales_enabled(;$) {
196 # Returns 0 if no locale handling is available on this platform; otherwise
199 # The optional parameter is a reference to a list of individual POSIX
200 # locale categories. If any of the individual categories specified by the
201 # optional parameter is all digits (and an optional leading minus), it is
202 # taken to be the C enum for the category (e.g., &POSIX::LC_CTYPE).
203 # Otherwise it should be a string name of the category, like 'LC_TIME'.
204 # The initial 'LC_' is optional. It is a fatal error to call this with
205 # something that isn't a known category to this file.
207 # This optional parameter denotes which POSIX locale categories must be
208 # available on the platform. If any aren't available, this function
209 # returns 0; otherwise it returns 1 and changes the list for the caller so
210 # that any category names are converted into their equivalent numbers, and
211 # sorts it to match the expectations of _trylocale.
213 # It is acceptable for the second parameter to be just a simple scalar
214 # denoting a single category (either name or number). No conversion into
215 # a number is done in this case.
217 return 0 unless $Config{d_setlocale}
218 # I (khw) cargo-culted the '?' in the pattern on the
220 && $Config{ccflags} !~ /\bD?NO_LOCALE\b/
223 # Done with the global possibilities. Now check if any passed in category
226 my $categories_ref = shift;
227 my $return_categories_numbers = 0;
228 my @categories_numbers;
230 my $has_LC_COLLATE = 0;
232 if (defined $categories_ref) {
233 my @local_categories_copy;
235 if (ref $categories_ref) {
236 @local_categories_copy = @$$categories_ref;
237 $return_categories_numbers = 1;
239 else { # Single category passed in
240 @local_categories_copy = $categories_ref;
243 for my $category_name_or_number (@local_categories_copy) {
246 if ($category_name_or_number =~ / ^ -? \d+ $ /x) {
247 $number = $category_name_or_number;
248 die "Invalid locale category number '$number'"
249 unless grep { $number == $_ } keys %category_name;
250 $name = $category_name{$number};
253 $name = $category_name_or_number;
254 $name =~ s/ ^ LC_ //x;
255 foreach my $trial (keys %category_name) {
256 if ($category_name{$trial} eq $name) {
261 die "Invalid locale category name '$name'"
262 unless defined $number;
265 return 0 if $number <= $max_bad_category_number
266 || $Config{ccflags} =~ /\bD?NO_LOCALE_$name\b/;
268 eval "defined &POSIX::LC_$name";
271 if ($return_categories_numbers) {
272 if ($name eq 'CTYPE') {
273 unshift @categories_numbers, $number; # Always first
275 elsif ($name eq 'ALL') {
278 elsif ($name eq 'COLLATE') {
282 push @categories_numbers, $number;
288 if ($return_categories_numbers) {
290 # COLLATE comes after all other locales except ALL, which comes last
291 if ($has_LC_COLLATE) {
292 push @categories_numbers, $category_number{'COLLATE'};
295 push @categories_numbers, $category_number{'ALL'};
297 $$categories_ref = \@categories_numbers;
304 sub find_locales ($;$) {
306 # Returns an array of all the locales we found on the system. If the
307 # optional 2nd parameter is non-zero, the list includes all found locales;
308 # otherwise it is restricted to those locales that play well with Perl, as
309 # far as we can easily determine.
311 # The first parameter is either a single locale category or a reference to
312 # a list of categories to find valid locales for it (or in the case of
313 # multiple) for all of them. Each category can be a name (like 'LC_ALL'
314 # or simply 'ALL') or the C enum value for the category.
316 my $categories = shift;
317 my $allow_incompatible = shift // 0;
319 $categories = [ $categories ] unless ref $categories;
320 return unless locales_enabled(\$categories);
322 # Note, the subroutine call above converts the $categories into a form
323 # suitable for _trylocale().
325 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
326 # and mingw32 uses said silly CRT
327 # This doesn't seem to be an issue any more, at least on Windows XP,
328 # so re-enable the tests for Windows XP onwards.
329 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
330 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
331 return if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare')
332 && $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i);
334 # UWIN seems to loop after taint tests, just skip for now
335 return if ($^O =~ /^uwin/);
338 _trylocale("C", $categories, \@Locale, $allow_incompatible);
339 _trylocale("POSIX", $categories, \@Locale, $allow_incompatible);
340 _trylocale("C.UTF-8", $categories, \@Locale, $allow_incompatible);
342 _trylocale("ISO8859-$_", $categories, \@Locale, $allow_incompatible);
343 _trylocale("iso8859$_", $categories, \@Locale, $allow_incompatible);
344 _trylocale("iso8859-$_", $categories, \@Locale, $allow_incompatible);
345 _trylocale("iso_8859_$_", $categories, \@Locale, $allow_incompatible);
346 _trylocale("isolatin$_", $categories, \@Locale, $allow_incompatible);
347 _trylocale("isolatin-$_", $categories, \@Locale, $allow_incompatible);
348 _trylocale("iso_latin_$_", $categories, \@Locale, $allow_incompatible);
351 # Sanitize the environment so that we can run the external 'locale'
352 # program without the taint mode getting grumpy.
354 # $ENV{PATH} is special in VMS.
355 delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
357 # Other subversive stuff.
358 delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)};
360 if (-x "/usr/bin/locale"
361 && open(LOCALES, '-|', "/usr/bin/locale -a 2>/dev/null"))
364 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
365 # ain't great when we're running this testPERL_UNICODE= so that utf8
366 # locales will cause all IO hadles to default to (assume) utf8
367 next unless utf8::valid($_);
369 _trylocale($_, $categories, \@Locale, $allow_incompatible);
372 } elsif ($^O eq 'VMS'
373 && defined($ENV{'SYS$I18N_LOCALE'})
374 && -d 'SYS$I18N_LOCALE')
376 # The SYS$I18N_LOCALE logical name search list was not present on
377 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
378 opendir(LOCALES, "SYS\$I18N_LOCALE:");
379 while ($_ = readdir(LOCALES)) {
381 _trylocale($_, $categories, \@Locale, $allow_incompatible);
384 } elsif (($^O eq 'openbsd' || $^O eq 'bitrig' ) && -e '/usr/share/locale') {
386 # OpenBSD doesn't have a locale executable, so reading
387 # /usr/share/locale is much easier and faster than the last resort
390 opendir(LOCALES, '/usr/share/locale');
391 while ($_ = readdir(LOCALES)) {
393 _trylocale($_, $categories, \@Locale, $allow_incompatible);
396 } else { # Final fallback. Try our list of locales hard-coded here
398 # This is going to be slow.
401 # Locales whose name differs if the utf8 bit is on are stored in these
402 # two files with appropriate encodings.
403 my $data_file = ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/)
404 ? _source_location() . "/lib/locale/utf8"
405 : _source_location() . "/lib/locale/latin1";
407 @Data = do $data_file;
410 _my_diag(__FILE__ . ":" . __LINE__ . ": '$data_file' doesn't exist");
413 # The rest of the locales are in this file.
416 foreach my $line (@Data) {
417 my ($locale_name, $language_codes, $country_codes, $encodings) =
419 _my_diag(__FILE__ . ":" . __LINE__ . ": Unexpected syntax in '$line'")
420 unless defined $locale_name;
421 my @enc = _decode_encodings($encodings);
422 foreach my $loc (split(/ /, $locale_name)) {
423 _trylocale($loc, $categories, \@Locale, $allow_incompatible);
424 foreach my $enc (@enc) {
425 _trylocale("$loc.$enc", $categories, \@Locale,
426 $allow_incompatible);
429 foreach my $enc (@enc) {
430 _trylocale("$loc.$enc", $categories, \@Locale,
431 $allow_incompatible);
434 foreach my $lang (split(/ /, $language_codes)) {
435 _trylocale($lang, $categories, \@Locale, $allow_incompatible);
436 foreach my $country (split(/ /, $country_codes)) {
437 my $lc = "${lang}_${country}";
438 _trylocale($lc, $categories, \@Locale, $allow_incompatible);
439 foreach my $enc (@enc) {
440 _trylocale("$lc.$enc", $categories, \@Locale,
441 $allow_incompatible);
443 my $lC = "${lang}_\U${country}";
444 _trylocale($lC, $categories, \@Locale, $allow_incompatible);
445 foreach my $enc (@enc) {
446 _trylocale("$lC.$enc", $categories, \@Locale,
447 $allow_incompatible);
454 @Locale = sort @Locale;
459 sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input
462 # On z/OS, even locales marked as UTF-8 aren't.
463 return 0 if ord "A" != 65;
465 return 0 unless locales_enabled('LC_CTYPE');
470 no warnings 'locale'; # We may be trying out a weird locale
472 my $save_locale = setlocale(&POSIX::LC_CTYPE());
473 if (! $save_locale) {
474 ok(0, "Verify could save previous locale");
478 if (! setlocale(&POSIX::LC_CTYPE(), $locale)) {
479 ok(0, "Verify could setlocale to $locale");
485 # Use an op that gives different results for UTF-8 than any other locale.
486 # If a platform has UTF-8 locales, there should be at least one locale on
487 # most platforms with UTF-8 in its name, so if there is a bug in the op
488 # giving a false negative, we should get a failure for those locales as we
489 # go through testing all the locales on the platform.
490 if (CORE::fc(chr utf8::unicode_to_native(0xdf)) ne "ss") {
491 if ($locale =~ /UTF-?8/i) {
492 ok (0, "Verify $locale with UTF-8 in name is a UTF-8 locale");
499 die "Couldn't restore locale '$save_locale'"
500 unless setlocale(&POSIX::LC_CTYPE(), $save_locale);
505 sub find_utf8_ctype_locales (;$) { # Return the names of the locales that core
506 # Perl thinks are UTF-8 LC_CTYPE locales.
507 # Optional parameter is a reference to a
508 # list of locales to try; if omitted, this
509 # tries all locales it can find on the
511 return unless locales_enabled('LC_CTYPE');
513 my $locales_ref = shift;
516 if (! defined $locales_ref) {
518 my @locales = find_locales(&POSIX::LC_CTYPE());
519 $locales_ref = \@locales;
522 foreach my $locale (@$locales_ref) {
523 push @return, $locale if is_locale_utf8($locale);
530 sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl
531 # thinks is a UTF-8 LC_CTYPE non-turkic
533 # Optional parameter is a reference to a
534 # list of locales to try; if omitted, this
535 # tries all locales it can find on the
537 my $try_locales_ref = shift;
539 my @utf8_locales = find_utf8_ctype_locales($try_locales_ref);
540 my @turkic_locales = find_utf8_turkic_locales($try_locales_ref);
544 # Create undef elements in the hash for turkic locales
545 @seen_turkic{@turkic_locales} = ();
547 foreach my $locale (@utf8_locales) {
548 return $locale unless exists $seen_turkic{$locale};
554 sub find_utf8_turkic_locales (;$) {
556 # Return the name of all the locales that core Perl thinks are UTF-8
557 # Turkic LC_CTYPE. Optional parameter is a reference to a list of locales
558 # to try; if omitted, this tries all locales it can find on the platform
562 my $save_locale = setlocale(&POSIX::LC_CTYPE());
563 foreach my $locale (find_utf8_ctype_locales(shift)) {
565 setlocale(&POSIX::LC_CTYPE(), $locale);
566 push @return, $locale if uc('i') eq "\x{130}";
568 setlocale(&POSIX::LC_CTYPE(), $save_locale);
573 sub find_utf8_turkic_locale (;$) {
574 my @turkics = find_utf8_turkic_locales(shift);
576 return unless @turkics;
581 # returns full path to the directory containing the current source
582 # file, inspired by mauke's Dir::Self
583 sub _source_location {
586 my $caller_filename = (caller)[1];
588 my $loc = File::Spec->rel2abs(
590 (File::Spec->splitpath($caller_filename))[0, 1], ''
594 return ($loc =~ /^(.*)$/)[0]; # untaint
599 # Format of data is: locale_name, language_codes, country_codes, encodings
602 Arabic:ar:dz eg sa:6 arabic8
603 Brezhoneg Breton:br:fr:1 15
604 Bulgarski Bulgarian:bg:bg:5
605 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
606 Hrvatski Croatian:hr:hr:2
607 Cymraeg Welsh:cy:cy:1 14 15
609 Dansk Danish:da:dk:1 15
610 Nederlands Dutch:nl:be nl:1 15
611 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
613 Eesti Estonian:et:ee:4 6 13
614 Suomi Finnish:fi:fi:1 15
616 Deutsch German:de:at be ch de lu:1 15
617 Euskaraz Basque:eu:es fr:1 15
618 Galego Galician:gl:es:1 15
619 Ellada Greek:el:gr:7 g8
621 Greenlandic:kl:gl:4 6
622 Hebrew:iw:il:8 hebrew8
624 Indonesian:id:id:1 15
625 Gaeilge Irish:ga:IE:1 14 15
626 Italiano Italian:it:ch it:1 15
627 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
629 Latine Latin:la:va:1 15
631 Lithuanian:lt:lt:4 6 13
632 Macedonian:mk:mk:1 15
635 Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
637 Polski Polish:pl:pl:2
639 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
640 Serbski Serbian:sr:yu:5
642 Slovene Slovenian:sl:si:2
643 Sqhip Albanian:sq:sq:1 15
644 Svenska Swedish:sv:fi se:1 15
646 Turkish:tr:tr:9 turkish8