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.
15 eval { require POSIX; import POSIX 'locale_h'; };
16 my $has_locale_h = ! $@;
18 my @known_categories = ( qw(LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY
19 LC_NUMERIC LC_TIME LC_ADDRESS LC_IDENTIFICATION
20 LC_MEASUREMENT LC_PAPER LC_TELEPHONE));
21 my @platform_categories;
23 # LC_ALL can be -1 on some platforms. And, in fact the implementors could
24 # legally use any integer to represent any category. But it makes the most
25 # sense for them to have used small integers. Below, we create new locale
26 # numbers for ones missing from this machine. We make them very negative,
27 # hopefully more negative than anything likely to be a valid category on the
28 # platform, but also below is a check to be sure that our guess is valid.
29 my $max_bad_category_number = -1000000;
31 # Initialize this hash so that it looks like e.g.,
33 # where 6 is the value of &POSIX::LC_CTYPE
37 my $number_for_missing_category = $max_bad_category_number;
38 foreach my $name (@known_categories) {
39 my $number = eval "&POSIX::$name";
41 # Use a negative number (smaller than any legitimate category
42 # number) if the platform doesn't support this category, so we
43 # have an entry for all the ones that might be specified in calls
45 $number = $number_for_missing_category-- if $@;
47 elsif ( $number !~ / ^ -? \d+ $ /x
48 || $number <= $max_bad_category_number)
50 # We think this should be an int. And it has to be larger than
51 # any of our synthetic numbers.
52 die "Unexpected locale category number '$number' for $name"
55 push @platform_categories, $name;
59 $category_name{$number} = "$name";
60 $category_number{$name} = $number;
66 if (defined &main::diag) {
70 local($\, $", $,) = (undef, ' ', '');
71 print STDERR $message, "\n";
77 if (defined &main::fail) {
81 local($\, $", $,) = (undef, ' ', '');
82 print "not ok 0 $message\n";
86 sub _trylocale ($$$$) { # For use only by other functions in this file!
88 # Adds the locale given by the first parameter to the list given by the
89 # 3rd iff the platform supports the locale in each of the category numbers
90 # given by the 2nd parameter, which is either a single category or a
91 # reference to a list of categories. The list MUST be sorted so that
92 # CTYPE is first, COLLATE is last unless ALL is present, in which case
93 # that comes after COLLATE. This is because locale.c detects bad locales
94 # only with CTYPE, and COLLATE on some platforms can core dump if it is a
97 # The 4th parameter is true if to accept locales that aren't apparently
98 # fully compatible with Perl.
101 my $categories = shift;
103 my $allow_incompatible = shift;
105 return if ! $locale || grep { $locale eq $_ } @$list;
107 # This is a toy (pig latin) locale that is not fully implemented on some
109 return if $locale =~ / ^ pig $ /ix;
111 $categories = [ $categories ] unless ref $categories;
116 use warnings 'locale';
118 local $SIG{__WARN__} = sub {
119 $badutf8 = 1 if grep { /Malformed UTF-8/ } @_;
120 $plays_well = 0 if grep {
121 /Locale .* may not work well(?#
122 )|The Perl program will use the expected meanings/i
126 # Incompatible locales aren't warned about unless using locales.
129 foreach my $category (@$categories) {
130 die "category '$category' must instead be a number"
131 unless $category =~ / ^ -? \d+ $ /x;
133 return unless setlocale($category, $locale);
134 last if $badutf8 || ! $plays_well;
138 _my_fail("Verify locale name doesn't contain malformed utf8");
141 push @$list, $locale if $plays_well || $allow_incompatible;
144 sub _decode_encodings { # For use only by other functions in this file!
147 foreach (split(/ /, shift)) {
149 push @enc, "ISO8859-$1";
150 push @enc, "iso8859$1"; # HP
152 push @enc, "roman8"; # HP
155 push @enc, "$_.UTF-8";
156 push @enc, "$_.65001"; # Windows UTF-8
157 push @enc, "$_.ACP"; # Windows ANSI code page
158 push @enc, "$_.OCP"; # Windows OEM code page
159 push @enc, "$_.1252"; # Windows
162 if ($^O eq 'os390') {
163 push @enc, qw(IBM-037 IBM-819 IBM-1047);
166 push @enc, "65001"; # Windows UTF-8
171 sub valid_locale_categories() {
172 # Returns a list of the locale categories (expressed as strings, like
173 # "LC_ALL) known to this program that are available on this platform.
175 return @platform_categories;
178 sub locales_enabled(;$) {
179 # Returns 0 if no locale handling is available on this platform; otherwise
182 # The optional parameter is a reference to a list of individual POSIX
183 # locale categories. If any of the individual categories specified by the
184 # optional parameter is all digits (and an optional leading minus), it is
185 # taken to be the C enum for the category (e.g., &POSIX::LC_CTYPE).
186 # Otherwise it should be a string name of the category, like 'LC_TIME'.
187 # The initial 'LC_' is optional. It is a fatal error to call this with
188 # something that isn't a known category to this file.
190 # This optional parameter denotes which POSIX locale categories must be
191 # available on the platform. If any aren't available, this function
192 # returns 0; otherwise it returns 1 and changes the list for the caller so
193 # that any category names are converted into their equivalent numbers, and
194 # sorts it to match the expectations of _trylocale.
196 # It is acceptable for the second parameter to be just a simple scalar
197 # denoting a single category (either name or number). No conversion into
198 # a number is done in this case.
202 return 0 unless $Config{d_setlocale}
203 # I (khw) cargo-culted the '?' in the pattern on the
205 && $Config{ccflags} !~ /\bD?NO_LOCALE\b/
208 # Done with the global possibilities. Now check if any passed in category
211 my $categories_ref = shift;
212 my $return_categories_numbers = 0;
213 my @categories_numbers;
215 my $has_LC_COLLATE = 0;
217 if (defined $categories_ref) {
218 my @local_categories_copy;
220 if (ref $categories_ref) {
221 @local_categories_copy = @$$categories_ref;
222 $return_categories_numbers = 1;
224 else { # Single category passed in
225 @local_categories_copy = $categories_ref;
228 for my $category_name_or_number (@local_categories_copy) {
231 if ($category_name_or_number =~ / ^ -? \d+ $ /x) {
232 $number = $category_name_or_number;
233 die "Invalid locale category number '$number'"
234 unless grep { $number == $_ } keys %category_name;
235 $name = $category_name{$number};
238 $name = $category_name_or_number;
239 $name =~ s/ ^ LC_ //x;
240 foreach my $trial (keys %category_name) {
241 if ($category_name{$trial} eq $name) {
246 die "Invalid locale category name '$name'"
247 unless defined $number;
250 return 0 if $number <= $max_bad_category_number
251 || $Config{ccflags} =~ /\bD?NO_LOCALE_$name\b/;
253 eval "defined &POSIX::LC_$name";
256 if ($return_categories_numbers) {
257 if ($name eq 'CTYPE') {
258 unshift @categories_numbers, $number; # Always first
260 elsif ($name eq 'ALL') {
263 elsif ($name eq 'COLLATE') {
267 push @categories_numbers, $number;
273 if ($return_categories_numbers) {
275 # COLLATE comes after all other locales except ALL, which comes last
276 if ($has_LC_COLLATE) {
277 push @categories_numbers, $category_number{'COLLATE'};
280 push @categories_numbers, $category_number{'ALL'};
282 $$categories_ref = \@categories_numbers;
289 sub find_locales ($;$) {
291 # Returns an array of all the locales we found on the system. If the
292 # optional 2nd parameter is non-zero, the list includes all found locales;
293 # otherwise it is restricted to those locales that play well with Perl, as
294 # far as we can easily determine.
296 # The first parameter is either a single locale category or a reference to
297 # a list of categories to find valid locales for it (or in the case of
298 # multiple) for all of them. Each category can be a name (like 'LC_ALL'
299 # or simply 'ALL') or the C enum value for the category.
301 my $categories = shift;
302 my $allow_incompatible = shift // 0;
304 $categories = [ $categories ] unless ref $categories;
305 return unless locales_enabled(\$categories);
307 # Note, the subroutine call above converts the $categories into a form
308 # suitable for _trylocale().
310 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
311 # and mingw32 uses said silly CRT
312 # This doesn't seem to be an issue any more, at least on Windows XP,
313 # so re-enable the tests for Windows XP onwards.
314 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
315 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
316 return if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare')
317 && $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i);
319 # UWIN seems to loop after taint tests, just skip for now
320 return if ($^O =~ /^uwin/);
323 _trylocale("C", $categories, \@Locale, $allow_incompatible);
324 _trylocale("POSIX", $categories, \@Locale, $allow_incompatible);
326 _trylocale("ISO8859-$_", $categories, \@Locale, $allow_incompatible);
327 _trylocale("iso8859$_", $categories, \@Locale, $allow_incompatible);
328 _trylocale("iso8859-$_", $categories, \@Locale, $allow_incompatible);
329 _trylocale("iso_8859_$_", $categories, \@Locale, $allow_incompatible);
330 _trylocale("isolatin$_", $categories, \@Locale, $allow_incompatible);
331 _trylocale("isolatin-$_", $categories, \@Locale, $allow_incompatible);
332 _trylocale("iso_latin_$_", $categories, \@Locale, $allow_incompatible);
335 # Sanitize the environment so that we can run the external 'locale'
336 # program without the taint mode getting grumpy.
338 # $ENV{PATH} is special in VMS.
339 delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
341 # Other subversive stuff.
342 delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)};
344 if (-x "/usr/bin/locale"
345 && open(LOCALES, '-|', "/usr/bin/locale -a 2>/dev/null"))
348 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
349 # ain't great when we're running this testPERL_UNICODE= so that utf8
350 # locales will cause all IO hadles to default to (assume) utf8
351 next unless utf8::valid($_);
353 _trylocale($_, $categories, \@Locale, $allow_incompatible);
356 } elsif ($^O eq 'VMS'
357 && defined($ENV{'SYS$I18N_LOCALE'})
358 && -d 'SYS$I18N_LOCALE')
360 # The SYS$I18N_LOCALE logical name search list was not present on
361 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
362 opendir(LOCALES, "SYS\$I18N_LOCALE:");
363 while ($_ = readdir(LOCALES)) {
365 _trylocale($_, $categories, \@Locale, $allow_incompatible);
368 } elsif (($^O eq 'openbsd' || $^O eq 'bitrig' ) && -e '/usr/share/locale') {
370 # OpenBSD doesn't have a locale executable, so reading
371 # /usr/share/locale is much easier and faster than the last resort
374 opendir(LOCALES, '/usr/share/locale');
375 while ($_ = readdir(LOCALES)) {
377 _trylocale($_, $categories, \@Locale, $allow_incompatible);
380 } else { # Final fallback. Try our list of locales hard-coded here
382 # This is going to be slow.
385 # Locales whose name differs if the utf8 bit is on are stored in these
386 # two files with appropriate encodings.
387 my $data_file = ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/)
388 ? _source_location() . "/lib/locale/utf8"
389 : _source_location() . "/lib/locale/latin1";
391 @Data = do $data_file;
394 _my_diag(__FILE__ . ":" . __LINE__ . ": '$data_file' doesn't exist");
397 # The rest of the locales are in this file.
400 foreach my $line (@Data) {
401 my ($locale_name, $language_codes, $country_codes, $encodings) =
403 _my_diag(__FILE__ . ":" . __LINE__ . ": Unexpected syntax in '$line'")
404 unless defined $locale_name;
405 my @enc = _decode_encodings($encodings);
406 foreach my $loc (split(/ /, $locale_name)) {
407 _trylocale($loc, $categories, \@Locale, $allow_incompatible);
408 foreach my $enc (@enc) {
409 _trylocale("$loc.$enc", $categories, \@Locale,
410 $allow_incompatible);
413 foreach my $enc (@enc) {
414 _trylocale("$loc.$enc", $categories, \@Locale,
415 $allow_incompatible);
418 foreach my $lang (split(/ /, $language_codes)) {
419 _trylocale($lang, $categories, \@Locale, $allow_incompatible);
420 foreach my $country (split(/ /, $country_codes)) {
421 my $lc = "${lang}_${country}";
422 _trylocale($lc, $categories, \@Locale, $allow_incompatible);
423 foreach my $enc (@enc) {
424 _trylocale("$lc.$enc", $categories, \@Locale,
425 $allow_incompatible);
427 my $lC = "${lang}_\U${country}";
428 _trylocale($lC, $categories, \@Locale, $allow_incompatible);
429 foreach my $enc (@enc) {
430 _trylocale("$lC.$enc", $categories, \@Locale,
431 $allow_incompatible);
438 @Locale = sort @Locale;
443 sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input
446 # On z/OS, even locales marked as UTF-8 aren't.
447 return 0 if ord "A" != 65;
449 return 0 unless locales_enabled('LC_CTYPE');
454 no warnings 'locale'; # We may be trying out a weird locale
456 my $save_locale = setlocale(&POSIX::LC_CTYPE());
457 if (! $save_locale) {
458 ok(0, "Verify could save previous locale");
462 if (! setlocale(&POSIX::LC_CTYPE(), $locale)) {
463 ok(0, "Verify could setlocale to $locale");
469 # Use an op that gives different results for UTF-8 than any other locale.
470 # If a platform has UTF-8 locales, there should be at least one locale on
471 # most platforms with UTF-8 in its name, so if there is a bug in the op
472 # giving a false negative, we should get a failure for those locales as we
473 # go through testing all the locales on the platform.
474 if (CORE::fc(chr utf8::unicode_to_native(0xdf)) ne "ss") {
475 if ($locale =~ /UTF-?8/i) {
476 ok (0, "Verify $locale with UTF-8 in name is a UTF-8 locale");
483 die "Couldn't restore locale '$save_locale'"
484 unless setlocale(&POSIX::LC_CTYPE(), $save_locale);
489 sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl
490 # thinks is a UTF-8 LC_CTYPE locale.
491 # Optional parameter is a reference to a
492 # list of locales to try; if omitted, this
493 # tries all locales it can find on the
495 return unless locales_enabled('LC_CTYPE');
497 my $locales_ref = shift;
499 if (! defined $locales_ref) {
501 my @locales = find_locales(&POSIX::LC_CTYPE());
502 $locales_ref = \@locales;
505 foreach my $locale (@$locales_ref) {
506 return $locale if is_locale_utf8($locale);
512 # returns full path to the directory containing the current source
513 # file, inspired by mauke's Dir::Self
514 sub _source_location {
517 my $caller_filename = (caller)[1];
519 my $loc = File::Spec->rel2abs(
521 (File::Spec->splitpath($caller_filename))[0, 1], ''
525 return ($loc =~ /^(.*)$/)[0]; # untaint
530 # Format of data is: locale_name, language_codes, country_codes, encodings
533 Arabic:ar:dz eg sa:6 arabic8
534 Brezhoneg Breton:br:fr:1 15
535 Bulgarski Bulgarian:bg:bg:5
536 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
537 Hrvatski Croatian:hr:hr:2
538 Cymraeg Welsh:cy:cy:1 14 15
540 Dansk Danish:da:dk:1 15
541 Nederlands Dutch:nl:be nl:1 15
542 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
544 Eesti Estonian:et:ee:4 6 13
545 Suomi Finnish:fi:fi:1 15
547 Deutsch German:de:at be ch de lu:1 15
548 Euskaraz Basque:eu:es fr:1 15
549 Galego Galician:gl:es:1 15
550 Ellada Greek:el:gr:7 g8
552 Greenlandic:kl:gl:4 6
553 Hebrew:iw:il:8 hebrew8
555 Indonesian:id:id:1 15
556 Gaeilge Irish:ga:IE:1 14 15
557 Italiano Italian:it:ch it:1 15
558 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
560 Latine Latin:la:va:1 15
562 Lithuanian:lt:lt:4 6 13
563 Macedonian:mk:mk:1 15
566 Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
568 Polski Polish:pl:pl:2
570 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
571 Serbski Serbian:sr:yu:5
573 Slovene Slovenian:sl:si:2
574 Sqhip Albanian:sq:sq:1 15
575 Svenska Swedish:sv:fi se:1 15
577 Turkish:tr:tr:9 turkish8