1 # Common tools for test 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 LC_SYNTAX
23 my @platform_categories;
25 # LC_ALL can be -1 on some platforms. And, in fact the implementors could
26 # legally use any integer to represent any category. But it makes the most
27 # sense for them to have used small integers. Below, we create new locale
28 # numbers for ones missing from this machine. We make them very negative,
29 # hopefully more negative than anything likely to be a valid category on the
30 # platform, but also below is a check to be sure that our guess is valid.
31 my $max_bad_category_number = -1000000;
33 # Initialize this hash so that it looks like e.g.,
35 # where 6 is the value of &POSIX::LC_CTYPE
39 my $number_for_missing_category = $max_bad_category_number;
40 foreach my $name (@known_categories) {
41 my $number = eval "&POSIX::$name";
43 # Use a negative number (smaller than any legitimate category
44 # number) if the platform doesn't support this category, so we
45 # have an entry for all the ones that might be specified in calls
47 $number = $number_for_missing_category-- if $@;
49 elsif ( $number !~ / ^ -? \d+ $ /x
50 || $number <= $max_bad_category_number)
52 # We think this should be an int. And it has to be larger than
53 # any of our synthetic numbers.
54 die "Unexpected locale category number '$number' for $name"
57 push @platform_categories, $name;
61 $category_name{$number} = "$name";
62 $category_number{$name} = $number;
68 if (defined &main::diag) {
72 local($\, $", $,) = (undef, ' ', '');
73 print STDERR $message, "\n";
79 if (defined &main::fail) {
83 local($\, $", $,) = (undef, ' ', '');
84 print "not ok 0 $message\n";
88 sub _trylocale ($$$$) { # For use only by other functions in this file!
90 # Adds the locale given by the first parameter to the list given by the
91 # 3rd iff the platform supports the locale in each of the category numbers
92 # given by the 2nd parameter, which is either a single category or a
93 # reference to a list of categories. The list MUST be sorted so that
94 # CTYPE is first, COLLATE is last unless ALL is present, in which case
95 # that comes after COLLATE. This is because locale.c detects bad locales
96 # only with CTYPE, and COLLATE on some platforms can core dump if it is a
99 # The 4th parameter is true if to accept locales that aren't apparently
100 # fully compatible with Perl.
103 my $categories = shift;
105 my $allow_incompatible = shift;
107 return if ! $locale || grep { $locale eq $_ } @$list;
109 # This is a toy (pig latin) locale that is not fully implemented on some
111 return if $locale =~ / ^ pig $ /ix;
113 # Certain platforms have a crippled locale system in which setlocale
114 # returns success for just about any possible locale name, but if anything
115 # actually happens as a result of the call, it is that the underlying
116 # locale is set to a system default, likely C or C.UTF-8. We can't test
117 # such systems fully, but we shouldn't disable the user from using
118 # locales, as it may work out for them (or not).
119 return if defined $Config{d_setlocale_accepts_any_locale_name}
120 && $locale !~ / ^ (?: C | POSIX | C\.UTF-8 ) $/ix;
122 $categories = [ $categories ] unless ref $categories;
127 use warnings 'locale';
129 local $SIG{__WARN__} = sub {
130 $badutf8 = 1 if grep { /Malformed UTF-8/ } @_;
131 $plays_well = 0 if grep {
132 /Locale .* may not work well(?#
133 )|The Perl program will use the expected meanings/i
137 # Incompatible locales aren't warned about unless using locales.
140 foreach my $category (@$categories) {
141 die "category '$category' must instead be a number"
142 unless $category =~ / ^ -? \d+ $ /x;
144 return unless setlocale($category, $locale);
145 last if $badutf8 || ! $plays_well;
149 _my_fail("Verify locale name doesn't contain malformed utf8");
152 push @$list, $locale if $plays_well || $allow_incompatible;
155 sub _decode_encodings { # For use only by other functions in this file!
158 foreach (split(/ /, shift)) {
160 push @enc, "ISO8859-$1";
161 push @enc, "iso8859$1"; # HP
163 push @enc, "roman8"; # HP
166 push @enc, "$_.UTF-8";
167 push @enc, "$_.65001"; # Windows UTF-8
168 push @enc, "$_.ACP"; # Windows ANSI code page
169 push @enc, "$_.OCP"; # Windows OEM code page
170 push @enc, "$_.1252"; # Windows
173 if ($^O eq 'os390') {
174 push @enc, qw(IBM-037 IBM-819 IBM-1047);
177 push @enc, "65001"; # Windows UTF-8
182 sub valid_locale_categories() {
183 # Returns a list of the locale categories (expressed as strings, like
184 # "LC_ALL) known to this program that are available on this platform.
186 return @platform_categories;
189 sub locales_enabled(;$) {
190 # Returns 0 if no locale handling is available on this platform; otherwise
193 # The optional parameter is a reference to a list of individual POSIX
194 # locale categories. If any of the individual categories specified by the
195 # optional parameter is all digits (and an optional leading minus), it is
196 # taken to be the C enum for the category (e.g., &POSIX::LC_CTYPE).
197 # Otherwise it should be a string name of the category, like 'LC_TIME'.
198 # The initial 'LC_' is optional. It is a fatal error to call this with
199 # something that isn't a known category to this file.
201 # This optional parameter denotes which POSIX locale categories must be
202 # available on the platform. If any aren't available, this function
203 # returns 0; otherwise it returns 1 and changes the list for the caller so
204 # that any category names are converted into their equivalent numbers, and
205 # sorts it to match the expectations of _trylocale.
207 # It is acceptable for the second parameter to be just a simple scalar
208 # denoting a single category (either name or number). No conversion into
209 # a number is done in this case.
211 # khw cargo-culted the '?' in the pattern on the next line.
212 return 0 if $Config{ccflags} =~ /\bD?NO_LOCALE\b/;
214 # If we can't load the POSIX XS module, we can't have locales even if they
215 # normally would be available
216 return 0 if ! defined &DynaLoader::boot_DynaLoader;
218 if (! $Config{d_setlocale}) {
219 return 0 if $Config{ccflags} =~ /\bD?NO_POSIX_2008_LOCALE\b/;
220 return 0 unless $Config{d_newlocale};
221 return 0 unless $Config{d_uselocale};
222 return 0 unless $Config{d_duplocale};
223 return 0 unless $Config{d_freelocale};
226 # Done with the global possibilities. Now check if any passed in category
229 my $categories_ref = shift;
230 my $return_categories_numbers = 0;
231 my @categories_numbers;
233 my $has_LC_COLLATE = 0;
235 if (defined $categories_ref) {
236 my @local_categories_copy;
238 if (ref $categories_ref) {
239 @local_categories_copy = @$$categories_ref;
240 $return_categories_numbers = 1;
242 else { # Single category passed in
243 @local_categories_copy = $categories_ref;
246 for my $category_name_or_number (@local_categories_copy) {
249 if ($category_name_or_number =~ / ^ -? \d+ $ /x) {
250 $number = $category_name_or_number;
251 die "Invalid locale category number '$number'"
252 unless grep { $number == $_ } keys %category_name;
253 $name = $category_name{$number};
256 $name = $category_name_or_number;
257 $name =~ s/ ^ LC_ //x;
258 foreach my $trial (keys %category_name) {
259 if ($category_name{$trial} eq $name) {
264 die "Invalid locale category name '$name'"
265 unless defined $number;
268 return 0 if $number <= $max_bad_category_number
269 || $Config{ccflags} =~ /\bD?NO_LOCALE_$name\b/;
271 eval "defined &POSIX::LC_$name";
274 if ($return_categories_numbers) {
275 if ($name eq 'CTYPE') {
276 unshift @categories_numbers, $number; # Always first
278 elsif ($name eq 'ALL') {
281 elsif ($name eq 'COLLATE') {
285 push @categories_numbers, $number;
291 if ($return_categories_numbers) {
293 # COLLATE comes after all other locales except ALL, which comes last
294 if ($has_LC_COLLATE) {
295 push @categories_numbers, $category_number{'COLLATE'};
298 push @categories_numbers, $category_number{'ALL'};
300 $$categories_ref = \@categories_numbers;
307 sub find_locales ($;$) {
309 # Returns an array of all the locales we found on the system. If the
310 # optional 2nd parameter is non-zero, the list includes all found locales;
311 # otherwise it is restricted to those locales that play well with Perl, as
312 # far as we can easily determine.
314 # The first parameter is either a single locale category or a reference to
315 # a list of categories to find valid locales for it (or in the case of
316 # multiple) for all of them. Each category can be a name (like 'LC_ALL'
317 # or simply 'ALL') or the C enum value for the category.
319 my $categories = shift;
320 my $allow_incompatible = shift // 0;
322 $categories = [ $categories ] unless ref $categories;
323 return unless locales_enabled(\$categories);
325 # Note, the subroutine call above converts the $categories into a form
326 # suitable for _trylocale().
328 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
329 # and mingw32 uses said silly CRT
330 # This doesn't seem to be an issue any more, at least on Windows XP,
331 # so re-enable the tests for Windows XP onwards.
332 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
333 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
334 return if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare')
335 && $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i);
337 # UWIN seems to loop after taint tests, just skip for now
338 return if ($^O =~ /^uwin/);
341 _trylocale("C", $categories, \@Locale, $allow_incompatible);
342 _trylocale("POSIX", $categories, \@Locale, $allow_incompatible);
344 if ($Config{d_has_C_UTF8} && $Config{d_has_C_UTF8} eq 'true') {
345 _trylocale("C.UTF-8", $categories, \@Locale, $allow_incompatible);
348 # There's no point in looking at anything more if we know that setlocale
349 # will return success on any garbage or non-garbage name.
350 return sort @Locale if defined $Config{d_setlocale_accepts_any_locale_name};
353 _trylocale("ISO8859-$_", $categories, \@Locale, $allow_incompatible);
354 _trylocale("iso8859$_", $categories, \@Locale, $allow_incompatible);
355 _trylocale("iso8859-$_", $categories, \@Locale, $allow_incompatible);
356 _trylocale("iso_8859_$_", $categories, \@Locale, $allow_incompatible);
357 _trylocale("isolatin$_", $categories, \@Locale, $allow_incompatible);
358 _trylocale("isolatin-$_", $categories, \@Locale, $allow_incompatible);
359 _trylocale("iso_latin_$_", $categories, \@Locale, $allow_incompatible);
362 # Sanitize the environment so that we can run the external 'locale'
363 # program without the taint mode getting grumpy.
365 # $ENV{PATH} is special in VMS.
366 delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
368 # Other subversive stuff.
369 delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)};
371 if (-x "/usr/bin/locale"
372 && open(LOCALES, '-|', "/usr/bin/locale -a 2>/dev/null"))
375 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
376 # ain't great when we're running this testPERL_UNICODE= so that utf8
377 # locales will cause all IO hadles to default to (assume) utf8
378 next unless utf8::valid($_);
380 _trylocale($_, $categories, \@Locale, $allow_incompatible);
383 } elsif ($^O eq 'VMS'
384 && defined($ENV{'SYS$I18N_LOCALE'})
385 && -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)) {
392 _trylocale($_, $categories, \@Locale, $allow_incompatible);
395 } elsif (($^O eq 'openbsd' || $^O eq 'bitrig' ) && -e '/usr/share/locale') {
397 # OpenBSD doesn't have a locale executable, so reading
398 # /usr/share/locale is much easier and faster than the last resort
401 opendir(LOCALES, '/usr/share/locale');
402 while ($_ = readdir(LOCALES)) {
404 _trylocale($_, $categories, \@Locale, $allow_incompatible);
407 } else { # Final fallback. Try our list of locales hard-coded here
409 # This is going to be slow.
412 # Locales whose name differs if the utf8 bit is on are stored in these
413 # two files with appropriate encodings.
414 my $data_file = ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/)
415 ? _source_location() . "/lib/locale/utf8"
416 : _source_location() . "/lib/locale/latin1";
418 @Data = do $data_file;
421 _my_diag(__FILE__ . ":" . __LINE__ . ": '$data_file' doesn't exist");
424 # The rest of the locales are in this file.
425 push @Data, <DATA>; close DATA;
427 foreach my $line (@Data) {
429 my ($locale_name, $language_codes, $country_codes, $encodings) =
431 _my_diag(__FILE__ . ":" . __LINE__ . ": Unexpected syntax in '$line'")
432 unless defined $locale_name;
433 my @enc = _decode_encodings($encodings);
434 foreach my $loc (split(/ /, $locale_name)) {
435 _trylocale($loc, $categories, \@Locale, $allow_incompatible);
436 foreach my $enc (@enc) {
437 _trylocale("$loc.$enc", $categories, \@Locale,
438 $allow_incompatible);
441 foreach my $enc (@enc) {
442 _trylocale("$loc.$enc", $categories, \@Locale,
443 $allow_incompatible);
446 foreach my $lang (split(/ /, $language_codes)) {
447 _trylocale($lang, $categories, \@Locale, $allow_incompatible);
448 foreach my $country (split(/ /, $country_codes)) {
449 my $lc = "${lang}_${country}";
450 _trylocale($lc, $categories, \@Locale, $allow_incompatible);
451 foreach my $enc (@enc) {
452 _trylocale("$lc.$enc", $categories, \@Locale,
453 $allow_incompatible);
455 my $lC = "${lang}_\U${country}";
456 _trylocale($lC, $categories, \@Locale, $allow_incompatible);
457 foreach my $enc (@enc) {
458 _trylocale("$lC.$enc", $categories, \@Locale,
459 $allow_incompatible);
466 @Locale = sort @Locale;
471 sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input
474 # On z/OS, even locales marked as UTF-8 aren't.
475 return 0 if ord "A" != 65;
477 return 0 unless locales_enabled('LC_CTYPE');
482 no warnings 'locale'; # We may be trying out a weird locale
484 my $save_locale = setlocale(&POSIX::LC_CTYPE());
485 if (! $save_locale) {
486 ok(0, "Verify could save previous locale");
490 if (! setlocale(&POSIX::LC_CTYPE(), $locale)) {
491 ok(0, "Verify could setlocale to $locale");
497 # Use an op that gives different results for UTF-8 than any other locale.
498 # If a platform has UTF-8 locales, there should be at least one locale on
499 # most platforms with UTF-8 in its name, so if there is a bug in the op
500 # giving a false negative, we should get a failure for those locales as we
501 # go through testing all the locales on the platform.
502 if (CORE::fc(chr utf8::unicode_to_native(0xdf)) ne "ss") {
503 if ($locale =~ /UTF-?8/i) {
504 ok (0, "Verify $locale with UTF-8 in name is a UTF-8 locale");
511 die "Couldn't restore locale '$save_locale'"
512 unless setlocale(&POSIX::LC_CTYPE(), $save_locale);
517 sub find_utf8_ctype_locales (;$) { # Return the names of the locales that core
518 # Perl thinks are UTF-8 LC_CTYPE locales.
519 # Optional parameter is a reference to a
520 # list of locales to try; if omitted, this
521 # tries all locales it can find on the
523 return unless locales_enabled('LC_CTYPE');
525 my $locales_ref = shift;
528 if (! defined $locales_ref) {
530 my @locales = find_locales(&POSIX::LC_CTYPE());
531 $locales_ref = \@locales;
534 foreach my $locale (@$locales_ref) {
535 push @return, $locale if is_locale_utf8($locale);
542 sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl
543 # thinks is a UTF-8 LC_CTYPE non-turkic
545 # Optional parameter is a reference to a
546 # list of locales to try; if omitted, this
547 # tries all locales it can find on the
549 my $try_locales_ref = shift;
551 my @utf8_locales = find_utf8_ctype_locales($try_locales_ref);
552 my @turkic_locales = find_utf8_turkic_locales($try_locales_ref);
556 # Create undef elements in the hash for turkic locales
557 @seen_turkic{@turkic_locales} = ();
559 foreach my $locale (@utf8_locales) {
560 return $locale unless exists $seen_turkic{$locale};
566 sub find_utf8_turkic_locales (;$) {
568 # Return the name of all the locales that core Perl thinks are UTF-8
569 # Turkic LC_CTYPE. Optional parameter is a reference to a list of locales
570 # to try; if omitted, this tries all locales it can find on the platform
574 return unless locales_enabled('LC_CTYPE');
576 my $save_locale = setlocale(&POSIX::LC_CTYPE());
577 foreach my $locale (find_utf8_ctype_locales(shift)) {
579 setlocale(&POSIX::LC_CTYPE(), $locale);
580 push @return, $locale if uc('i') eq "\x{130}";
582 setlocale(&POSIX::LC_CTYPE(), $save_locale);
587 sub find_utf8_turkic_locale (;$) {
588 my @turkics = find_utf8_turkic_locales(shift);
590 return unless @turkics;
595 # returns full path to the directory containing the current source
596 # file, inspired by mauke's Dir::Self
597 sub _source_location {
600 my $caller_filename = (caller)[1];
602 my $loc = File::Spec->rel2abs(
604 (File::Spec->splitpath($caller_filename))[0, 1], ''
608 return ($loc =~ /^(.*)$/)[0]; # untaint
613 # Format of data is: locale_name, language_codes, country_codes, encodings
616 Arabic:ar:dz eg sa:6 arabic8
617 Brezhoneg Breton:br:fr:1 15
618 Bulgarski Bulgarian:bg:bg:5
619 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
620 Hrvatski Croatian:hr:hr:2
621 Cymraeg Welsh:cy:cy:1 14 15
623 Dansk Danish:da:dk:1 15
624 Nederlands Dutch:nl:be nl:1 15
625 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
627 Eesti Estonian:et:ee:4 6 13
628 Suomi Finnish:fi:fi:1 15
630 Deutsch German:de:at be ch de lu:1 15
631 Euskaraz Basque:eu:es fr:1 15
632 Galego Galician:gl:es:1 15
633 Ellada Greek:el:gr:7 g8
635 Greenlandic:kl:gl:4 6
636 Hebrew:iw:il:8 hebrew8
638 Indonesian:id:id:1 15
639 Gaeilge Irish:ga:IE:1 14 15
640 Italiano Italian:it:ch it:1 15
641 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
643 Latine Latin:la:va:1 15
645 Lithuanian:lt:lt:4 6 13
646 Macedonian:mk:mk:1 15
649 Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
651 Polski Polish:pl:pl:2
653 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
654 Serbski Serbian:sr:yu:5
656 Slovene Slovenian:sl:si:2
657 Sqhip Albanian:sq:sq:1 15
658 Svenska Swedish:sv:fi se:1 15
660 Turkish:tr:tr:9 turkish8