1 # Common tools for test files files to find the locales which exist on the
2 # system. Caller should have defined ok() for the unlikely event that setup
3 # here fails, and should have verified that this isn't miniperl before calling
6 # Note that it's okay that some languages have their native names
7 # capitalized here even though that's not "right". They are lowercased
8 # anyway later during the scanning process (and besides, some clueless
9 # vendor might have them capitalized erroneously anyway).
11 # Functions whose names begin with underscore are internal helper functions
12 # for this file, and are not to be used by outside callers.
14 eval { require POSIX; import POSIX 'locale_h'; };
15 my $has_posix_locales = defined &POSIX::LC_CTYPE;
17 sub _trylocale ($$$$) { # For use only by other functions in this file!
19 # Adds the locale given by the first parameter to the list given by the
20 # 3rd iff the platform supports the locale in each of the categories given
21 # by the 2nd parameter, which is either a single category or a reference
22 # to a list of categories The 4th parameter is true if to reject locales
23 # that aren't apparently fully compatible with Perl.
26 my $categories = shift;
28 my $only_plays_well = shift;
30 return if ! $locale || grep { $locale eq $_ } @$list;
32 $categories = [ $categories ] unless ref $categories;
37 use warnings 'locale';
39 local $SIG{__WARN__} = sub {
40 $badutf8 = 1 if $_[0] =~ /Malformed UTF-8/;
41 $plays_well = 0 if $_[0] =~ /Locale .* may not work well/i
44 foreach my $category (@$categories) {
45 return unless setlocale($category, $locale);
46 return if $only_plays_well && ! $plays_well;
50 ok(0, "Verify locale name doesn't contain malformed utf8");
56 sub _decode_encodings { # For use only by other functions in this file!
59 foreach (split(/ /, shift)) {
61 push @enc, "ISO8859-$1";
62 push @enc, "iso8859$1"; # HP
64 push @enc, "roman8"; # HP
67 push @enc, "$_.UTF-8";
68 push @enc, "$_.65001"; # Windows UTF-8
69 push @enc, "$_.ACP"; # Windows ANSI code page
70 push @enc, "$_.OCP"; # Windows OEM code page
71 push @enc, "$_.1252"; # Windows
75 push @enc, qw(IBM-037 IBM-819 IBM-1047);
78 push @enc, "65001"; # Windows UTF-8
83 # LC_ALL can be -1 on some platforms. And, in fact the implementors could
84 # legally use any integer to represent any category. But it makes the most
85 # sense for them to have used small integers. Below, we create new locale
86 # numbers for ones missing from this machine. We make them very negative,
87 # hopefully more negative than anything likely to be a valid category on the
88 # platform, but also below is a check to be sure that our guess is valid.
89 my $max_bad_category_number = -1000000;
91 # Initialize this hash so that it looks like e.g.,
93 # where 6 is the value of &POSIX::LC_CTYPE
95 eval { require POSIX; import POSIX 'locale_h'; };
97 my $number_for_missing_category = $max_bad_category_number;
98 foreach my $name (qw(ALL COLLATE CTYPE MESSAGES MONETARY NUMERIC TIME)) {
99 my $number = eval "&POSIX::LC_$name";
102 # Use a negative number (smaller than any legitimate category
103 # number) if the platform doesn't support this category, so we
104 # have an entry for all the ones that might be specified in calls
106 $number = $number_for_missing_category-- if $@;
108 elsif ( $number !~ / ^ -? \d+ $ /x
109 || $number <= $max_bad_category_number)
111 # We think this should be an int. And it has to be larger than
112 # any of our synthetic numbers.
113 die "Unexpected locale category number '$number' for LC_$name"
116 $category_name{$number} = "$name";
120 sub locales_enabled(;$) {
121 # Returns 0 if no locale handling is available on this platform; otherwise
124 # The optional parameter is a reference to a list of individual POSIX
125 # locale categories. If present, this function also returns 0 if any of
126 # them are individually not available on this platform; otherwise 1.
127 # Actually, it is acceptable for the list to be just a simple scalar
128 # denoting a single category.
130 # If any of the individual categories specified by the optional parameter
131 # is all digits (and an optional leading minus), it is taken to be the C
132 # enum for the category (e.g., &POSIX::LC_CTYPE). Otherwise it should be
133 # a string name of the category, like 'LC_TIME'. The initial 'LC_' is
134 # optional. It is a fatal error to call this with something that isn't a
139 return 0 unless $Config{d_setlocale}
140 # I (khw) cargo-culted the '?' in the pattern on the
142 && $Config{ccflags} !~ /\bD?NO_LOCALE\b/
143 && $has_posix_locales;
145 # Done with the global possibilities. Now check if any passed in category
147 my $categories_ref = shift;
148 if (defined $categories_ref) {
149 $categories_ref = [ $categories_ref ] if ! ref $categories_ref;
150 my @local_categories_copy = @$categories_ref;
151 for my $category_name_or_number (@local_categories_copy) {
154 if ($category_name_or_number =~ / ^ -? \d+ $ /x) {
155 $number = $category_name_or_number;
156 die "Invalid locale category number '$number'"
157 unless grep { $number == $_ } keys %category_name;
158 $name = $category_name{$number};
161 $name = $category_name_or_number;
162 $name =~ s/ ^ LC_ //x;
163 foreach my $trial (keys %category_name) {
164 if ($category_name{$trial} eq $name) {
169 die "Invalid locale category name '$name'"
170 unless defined $number;
173 return 0 if $number <= $max_bad_category_number
174 || $Config{ccflags} =~ /\bD?NO_LOCALE_$name\b/;
182 sub find_locales ($;$) { # Returns an array of all the locales we found on the
183 # system. If the optional 2nd parameter is
184 # non-zero, the list is restricted to those locales
185 # that play well with Perl.
186 # The first parameter is either a single locale
187 # category or a reference to a list of categories to
188 # find valid locales for it (or in the case of
189 # multiple) for all of them.
190 my $categories = shift;
191 my $only_plays_well = shift // 0;
193 return unless locales_enabled($categories);
195 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
196 # and mingw32 uses said silly CRT
197 # This doesn't seem to be an issue any more, at least on Windows XP,
198 # so re-enable the tests for Windows XP onwards.
199 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
200 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
201 return if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare')
202 && $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i);
204 # UWIN seems to loop after taint tests, just skip for now
205 return if ($^O =~ /^uwin/);
207 # Done this way in case this is 'required' in the caller before seeing if
209 return unless $has_posix_locales;
211 _trylocale("C", $categories, \@Locale, $only_plays_well);
212 _trylocale("POSIX", $categories, \@Locale, $only_plays_well);
214 _trylocale("ISO8859-$_", $categories, \@Locale, $only_plays_well);
215 _trylocale("iso8859$_", $categories, \@Locale, $only_plays_well);
216 _trylocale("iso8859-$_", $categories, \@Locale, $only_plays_well);
217 _trylocale("iso_8859_$_", $categories, \@Locale, $only_plays_well);
218 _trylocale("isolatin$_", $categories, \@Locale, $only_plays_well);
219 _trylocale("isolatin-$_", $categories, \@Locale, $only_plays_well);
220 _trylocale("iso_latin_$_", $categories, \@Locale, $only_plays_well);
223 # Sanitize the environment so that we can run the external 'locale'
224 # program without the taint mode getting grumpy.
226 # $ENV{PATH} is special in VMS.
227 delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
229 # Other subversive stuff.
230 delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)};
232 if (-x "/usr/bin/locale"
233 && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|"))
236 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
237 # ain't great when we're running this testPERL_UNICODE= so that utf8
238 # locales will cause all IO hadles to default to (assume) utf8
239 next unless utf8::valid($_);
241 _trylocale($_, $categories, \@Locale, $only_plays_well);
244 } elsif ($^O eq 'VMS'
245 && defined($ENV{'SYS$I18N_LOCALE'})
246 && -d 'SYS$I18N_LOCALE')
248 # The SYS$I18N_LOCALE logical name search list was not present on
249 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
250 opendir(LOCALES, "SYS\$I18N_LOCALE:");
251 while ($_ = readdir(LOCALES)) {
253 _trylocale($_, $categories, \@Locale, $only_plays_well);
256 } elsif (($^O eq 'openbsd' || $^O eq 'bitrig' ) && -e '/usr/share/locale') {
258 # OpenBSD doesn't have a locale executable, so reading
259 # /usr/share/locale is much easier and faster than the last resort
262 opendir(LOCALES, '/usr/share/locale');
263 while ($_ = readdir(LOCALES)) {
265 _trylocale($_, $categories, \@Locale, $only_plays_well);
268 } else { # Final fallback. Try our list of locales hard-coded here
270 # This is going to be slow.
273 # Locales whose name differs if the utf8 bit is on are stored in these two
274 # files with appropriate encodings.
275 if ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/) {
276 @Data = do "lib/locale/utf8";
278 @Data = do "lib/locale/latin1";
281 # The rest of the locales are in this file.
284 foreach my $line (@Data) {
285 my ($locale_name, $language_codes, $country_codes, $encodings) =
287 my @enc = _decode_encodings($encodings);
288 foreach my $loc (split(/ /, $locale_name)) {
289 _trylocale($loc, $categories, \@Locale, $only_plays_well);
290 foreach my $enc (@enc) {
291 _trylocale("$loc.$enc", $categories, \@Locale,
295 foreach my $enc (@enc) {
296 _trylocale("$loc.$enc", $categories, \@Locale,
300 foreach my $lang (split(/ /, $language_codes)) {
301 _trylocale($lang, $categories, \@Locale, $only_plays_well);
302 foreach my $country (split(/ /, $country_codes)) {
303 my $lc = "${lang}_${country}";
304 _trylocale($lc, $categories, \@Locale, $only_plays_well);
305 foreach my $enc (@enc) {
306 _trylocale("$lc.$enc", $categories, \@Locale,
309 my $lC = "${lang}_\U${country}";
310 _trylocale($lC, $categories, \@Locale, $only_plays_well);
311 foreach my $enc (@enc) {
312 _trylocale("$lC.$enc", $categories, \@Locale,
320 @Locale = sort @Locale;
325 sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input
328 # On z/OS, even locales marked as UTF-8 aren't.
329 return 0 if ord "A" != 65;
331 return 0 if ! $has_posix_locales;
332 return 0 if ! locales_enabled('LC_CTYPE');
337 no warnings 'locale'; # We may be trying out a weird locale
339 my $save_locale = setlocale(&POSIX::LC_CTYPE());
340 if (! $save_locale) {
341 ok(0, "Verify could save previous locale");
345 if (! setlocale(&POSIX::LC_CTYPE(), $locale)) {
346 ok(0, "Verify could setlocale to $locale");
352 # Use an op that gives different results for UTF-8 than any other locale.
353 # If a platform has UTF-8 locales, there should be at least one locale on
354 # most platforms with UTF-8 in its name, so if there is a bug in the op
355 # giving a false negative, we should get a failure for those locales as we
356 # go through testing all the locales on the platform.
357 if (CORE::fc(chr utf8::unicode_to_native(0xdf)) ne "ss") {
358 if ($locale =~ /UTF-?8/i) {
359 ok (0, "Verify $locale with UTF-8 in name is a UTF-8 locale");
366 die "Couldn't restore locale '$save_locale'"
367 unless setlocale(&POSIX::LC_CTYPE(), $save_locale);
372 sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl
373 # thinks is a UTF-8 LC_CTYPE locale.
374 # Optional parameter is a reference to a
375 # list of locales to try; if omitted, this
376 # tries all locales it can find on the
378 my $locales_ref = shift;
380 if (! defined $locales_ref) {
381 return if ! $has_posix_locales;
383 my @locales = find_locales(&POSIX::LC_CTYPE(),
384 1 # Reject iffy locales.
386 $locales_ref = \@locales;
389 foreach my $locale (@$locales_ref) {
390 return $locale if is_locale_utf8($locale);
398 # Format of data is: locale_name, language_codes, country_codes, encodings
401 Arabic:ar:dz eg sa:6 arabic8
402 Brezhoneg Breton:br:fr:1 15
403 Bulgarski Bulgarian:bg:bg:5
404 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
405 Hrvatski Croatian:hr:hr:2
406 Cymraeg Welsh:cy:cy:1 14 15
408 Dansk Danish:da:dk:1 15
409 Nederlands Dutch:nl:be nl:1 15
410 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
412 Eesti Estonian:et:ee:4 6 13
413 Suomi Finnish:fi:fi:1 15
415 Deutsch German:de:at be ch de lu:1 15
416 Euskaraz Basque:eu:es fr:1 15
417 Galego Galician:gl:es:1 15
418 Ellada Greek:el:gr:7 g8
420 Greenlandic:kl:gl:4 6
421 Hebrew:iw:il:8 hebrew8
423 Indonesian:id:id:1 15
424 Gaeilge Irish:ga:IE:1 14 15
425 Italiano Italian:it:ch it:1 15
426 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
428 Latine Latin:la:va:1 15
430 Lithuanian:lt:lt:4 6 13
431 Macedonian:mk:mk:1 15
434 Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
436 Polski Polish:pl:pl:2
438 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
439 Serbski Serbian:sr:yu:5
441 Slovene Slovenian:sl:si:2
442 Sqhip Albanian:sq:sq:1 15
443 Svenska Swedish:sv:fi se:1 15
445 Turkish:tr:tr:9 turkish8