This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Increase page file quota advice in README.vms
[perl5.git] / t / loc_tools.pl
CommitLineData
a3815e44 1# Common tools for test files to find the locales which exist on the
13022195 2# system. Caller should have verified that this isn't miniperl before calling
abfe20b2 3# the functions.
73fc293b
KW
4
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).
9
bb31ec6a
KW
10# Functions whose names begin with underscore are internal helper functions
11# for this file, and are not to be used by outside callers.
12
99617871 13use Config;
e1f89022 14use strict;
27526f3a 15use warnings;
a3e69330 16use feature 'state';
e1f89022 17
a274778c 18eval { require POSIX; import POSIX 'locale_h'; };
e1f89022 19my $has_locale_h = ! $@;
a274778c 20
83858d2d
KW
21my @known_categories = ( qw(LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY
22 LC_NUMERIC LC_TIME LC_ADDRESS LC_IDENTIFICATION
36dbc955
KW
23 LC_MEASUREMENT LC_PAPER LC_TELEPHONE LC_SYNTAX
24 LC_TOD));
83858d2d
KW
25my @platform_categories;
26
6fa6c80b
KW
27# LC_ALL can be -1 on some platforms. And, in fact the implementors could
28# legally use any integer to represent any category. But it makes the most
29# sense for them to have used small integers. Below, we create new locale
30# numbers for ones missing from this machine. We make them very negative,
31# hopefully more negative than anything likely to be a valid category on the
32# platform, but also below is a check to be sure that our guess is valid.
33my $max_bad_category_number = -1000000;
34
35# Initialize this hash so that it looks like e.g.,
36# 6 => 'CTYPE',
37# where 6 is the value of &POSIX::LC_CTYPE
38my %category_name;
cd1f7080 39my %category_number;
86e85139 40if ($has_locale_h) {
6fa6c80b 41 my $number_for_missing_category = $max_bad_category_number;
83858d2d
KW
42 foreach my $name (@known_categories) {
43 my $number = eval "&POSIX::$name";
6fa6c80b
KW
44 if ($@) {
45 # Use a negative number (smaller than any legitimate category
46 # number) if the platform doesn't support this category, so we
47 # have an entry for all the ones that might be specified in calls
48 # to us.
829290ef 49 $number = $number_for_missing_category--;
6fa6c80b
KW
50 }
51 elsif ( $number !~ / ^ -? \d+ $ /x
52 || $number <= $max_bad_category_number)
53 {
54 # We think this should be an int. And it has to be larger than
55 # any of our synthetic numbers.
83858d2d
KW
56 die "Unexpected locale category number '$number' for $name"
57 }
58 else {
59 push @platform_categories, $name;
6fa6c80b
KW
60 }
61
83858d2d 62 $name =~ s/LC_//;
6fa6c80b 63 $category_name{$number} = "$name";
cd1f7080 64 $category_number{$name} = $number;
6fa6c80b
KW
65 }
66}
67
759b442c
KW
68sub _my_diag($) {
69 my $message = shift;
70 if (defined &main::diag) {
71 diag($message);
72 }
73 else {
74 local($\, $", $,) = (undef, ' ', '');
75 print STDERR $message, "\n";
76 }
77}
78
13022195
KW
79sub _my_fail($) {
80 my $message = shift;
81 if (defined &main::fail) {
82 fail($message);
83 }
84 else {
85 local($\, $", $,) = (undef, ' ', '');
86 print "not ok 0 $message\n";
87 }
88}
89
bb31ec6a
KW
90sub _trylocale ($$$$) { # For use only by other functions in this file!
91
92 # Adds the locale given by the first parameter to the list given by the
cd1f7080
KW
93 # 3rd iff the platform supports the locale in each of the category numbers
94 # given by the 2nd parameter, which is either a single category or a
fcabef25 95 # reference to a list of categories.
cd1f7080
KW
96 #
97 # The 4th parameter is true if to accept locales that aren't apparently
98 # fully compatible with Perl.
bb31ec6a 99
73fc293b 100 my $locale = shift;
9b0711ee 101 my $categories = shift;
73fc293b 102 my $list = shift;
2c6c88ec 103 my $allow_incompatible = shift;
f079f9b7 104
162eb2b8 105 return if ! $locale || grep { $locale eq $_ } @$list;
9b0711ee 106
577d3e04
KW
107 # This is a toy (pig latin) locale that is not fully implemented on some
108 # systems
109 return if $locale =~ / ^ pig $ /ix;
110
8e13243a
KW
111 # Certain platforms have a crippled locale system in which setlocale
112 # returns success for just about any possible locale name, but if anything
113 # actually happens as a result of the call, it is that the underlying
114 # locale is set to a system default, likely C or C.UTF-8. We can't test
115 # such systems fully, but we shouldn't disable the user from using
116 # locales, as it may work out for them (or not).
117 return if defined $Config{d_setlocale_accepts_any_locale_name}
118 && $locale !~ / ^ (?: C | POSIX | C\.UTF-8 ) $/ix;
99617871 119
9b0711ee
KW
120 $categories = [ $categories ] unless ref $categories;
121
f079f9b7
KW
122 my $badutf8 = 0;
123 my $plays_well = 1;
124
125 use warnings 'locale';
126
127 local $SIG{__WARN__} = sub {
973072b2 128 $badutf8 = 1 if grep { /Malformed UTF-8/ } @_;
1649de0c
KW
129 $plays_well = 0 if grep {
130 /Locale .* may not work well(?#
ef5d49f0 131 )|The Perl program will use the expected meanings/i
1649de0c 132 } @_;
f079f9b7
KW
133 };
134
9c5fc553
KW
135 # Incompatible locales aren't warned about unless using locales.
136 use locale;
137
fcabef25
KW
138 # Sort the input so CTYPE is first, COLLATE comes after all but ALL. This
139 # is because locale.c detects bad locales only with CTYPE, and COLLATE on
140 # some platforms can core dump if it is a bad locale.
141 my @sorted;
142 my $has_ctype = 0;
143 my $has_all = 0;
144 my $has_collate = 0;
9b0711ee 145 foreach my $category (@$categories) {
cd1f7080
KW
146 die "category '$category' must instead be a number"
147 unless $category =~ / ^ -? \d+ $ /x;
fcabef25
KW
148 if ($category_name{$category} eq 'CTYPE') {
149 $has_ctype = 1;
150 }
151 elsif ($category_name{$category} eq 'ALL') {
152 $has_all = 1;
153 }
154 elsif ($category_name{$category} eq 'COLLATE') {
155 $has_collate = 1;
156 }
157 else {
158 push @sorted, $category unless grep { $_ == $category } @sorted;
159 }
160 }
161 push @sorted, $category_number{'COLLATE'} if $has_collate;
162 push @sorted, $category_number{'ALL'} if $has_all;
163 unshift @sorted, $category_number{'CTYPE'} if $has_ctype || ! $allow_incompatible;
cd1f7080 164
fcabef25 165 foreach my $category (@sorted) {
9b0711ee 166 return unless setlocale($category, $locale);
caf06cdc 167 last if $badutf8 || ! $plays_well;
73fc293b
KW
168 }
169
170 if ($badutf8) {
13022195 171 _my_fail("Verify locale name doesn't contain malformed utf8");
73fc293b
KW
172 return;
173 }
caf06cdc 174 push @$list, $locale if $plays_well || $allow_incompatible;
73fc293b
KW
175}
176
bb31ec6a 177sub _decode_encodings { # For use only by other functions in this file!
73fc293b
KW
178 my @enc;
179
180 foreach (split(/ /, shift)) {
181 if (/^(\d+)$/) {
182 push @enc, "ISO8859-$1";
183 push @enc, "iso8859$1"; # HP
184 if ($1 eq '1') {
185 push @enc, "roman8"; # HP
186 }
187 push @enc, $_;
188 push @enc, "$_.UTF-8";
d646bffe
KW
189 push @enc, "$_.65001"; # Windows UTF-8
190 push @enc, "$_.ACP"; # Windows ANSI code page
191 push @enc, "$_.OCP"; # Windows OEM code page
cf34c81f 192 push @enc, "$_.1252"; # Windows
73fc293b
KW
193 }
194 }
195 if ($^O eq 'os390') {
196 push @enc, qw(IBM-037 IBM-819 IBM-1047);
197 }
198 push @enc, "UTF-8";
d646bffe 199 push @enc, "65001"; # Windows UTF-8
73fc293b
KW
200
201 return @enc;
202}
203
83858d2d
KW
204sub valid_locale_categories() {
205 # Returns a list of the locale categories (expressed as strings, like
206 # "LC_ALL) known to this program that are available on this platform.
207
208 return @platform_categories;
209}
210
128e4113 211sub locales_enabled(;$) {
23f9d84e
KW
212 # If no parameter is specified, the function returns 1 if there is any
213 # "safe" locale handling available to the caller; otherwise 0. Safeness
214 # is defined here as the caller operating in the main thread of a program,
215 # or if threaded locales are safe on the platform and Configured to be
216 # used. This sub is used for testing purposes, and for those, this
217 # definition of safety is sufficient, and necessary to get some tests to
218 # run on certain configurations on certain platforms. But beware that the
219 # main thread can change the locale of any subthreads unless
220 # ${^SAFE_LOCALES} is non-zero.
128e4113 221 #
23f9d84e
KW
222 # Use the optional parameter to discover if a particular category or
223 # categories are available on the system. 1 is returned if the global
224 # criteria described in the previous paragraph are true, AND if all the
225 # specified categories are available on the platform and Configured to be
226 # used. Otherwise 0 is returned. The parameter is either a single POSIX
227 # locale category or a reference to a list of them. Each category must be
228 # its name as a string, like 'LC_TIME' (the initial 'LC_' is optional), or
229 # the number this platform uses to signify the category (e.g.,
230 # 'locales_enabled(&POSIX::LC_CTYPE)'
128e4113 231 #
23f9d84e
KW
232 # When the function returns 1 and a parameter was specified as a list
233 # reference, the reference will be altered on return to point to an
234 # equivalent list such that the categories are numeric instead of strings
235 # and sorted to meet the input expectations of _trylocale().
cd1f7080 236 #
23f9d84e
KW
237 # It is a fatal error to call this with something that isn't a known
238 # category to this file. If this happens, look first for a typo, and
239 # second if you are using a category unknown to Perl. In the latter case
240 # a bug report should be submitted.
128e4113 241
aa8a2baa
KW
242 # khw cargo-culted the '?' in the pattern on the next line.
243 return 0 if $Config{ccflags} =~ /\bD?NO_LOCALE\b/;
244
d373bd46
KW
245 # If we can't load the POSIX XS module, we can't have locales even if they
246 # normally would be available
247 return 0 if ! defined &DynaLoader::boot_DynaLoader;
248
b0441c5b
KW
249 # Don't test locales where they aren't safe. On systems with unsafe
250 # threads, for the purposes of testing, we consider the main thread safe,
251 # and all other threads unsafe.
252 if (! ${^SAFE_LOCALES}) {
253 require threads;
254 return 0 if threads->tid() != 0;
255 }
39481c55
KW
256
257 # If no setlocale, we need the POSIX 2008 alternatives
aa8a2baa
KW
258 if (! $Config{d_setlocale}) {
259 return 0 if $Config{ccflags} =~ /\bD?NO_POSIX_2008_LOCALE\b/;
260 return 0 unless $Config{d_newlocale};
261 return 0 unless $Config{d_uselocale};
262 return 0 unless $Config{d_duplocale};
263 return 0 unless $Config{d_freelocale};
264 }
128e4113
KW
265
266 # Done with the global possibilities. Now check if any passed in category
267 # is disabled.
cd1f7080 268
23f9d84e 269 my $categories_ref = $_[0];
cd1f7080
KW
270 my $return_categories_numbers = 0;
271 my @categories_numbers;
272 my $has_LC_ALL = 0;
273 my $has_LC_COLLATE = 0;
274
e6965c14 275 if (defined $categories_ref) {
cd1f7080
KW
276 my @local_categories_copy;
277
23f9d84e
KW
278 my $reftype = ref $categories_ref;
279 if ($reftype eq 'ARRAY') {
280 @local_categories_copy = @$categories_ref;
cd1f7080
KW
281 $return_categories_numbers = 1;
282 }
23f9d84e
KW
283 elsif ($reftype ne "") {
284 die "Parameter to locales_enabled() must be an ARRAY;"
285 . " instead you used a $reftype";
286 }
cd1f7080
KW
287 else { # Single category passed in
288 @local_categories_copy = $categories_ref;
289 }
290
308482c2
KW
291 for my $category_name_or_number (@local_categories_copy) {
292 my $name;
293 my $number;
294 if ($category_name_or_number =~ / ^ -? \d+ $ /x) {
295 $number = $category_name_or_number;
296 die "Invalid locale category number '$number'"
297 unless grep { $number == $_ } keys %category_name;
298 $name = $category_name{$number};
e6965c14
KW
299 }
300 else {
308482c2
KW
301 $name = $category_name_or_number;
302 $name =~ s/ ^ LC_ //x;
303 foreach my $trial (keys %category_name) {
304 if ($category_name{$trial} eq $name) {
305 $number = $trial;
306 last;
307 }
308 }
309 die "Invalid locale category name '$name'"
310 unless defined $number;
e6965c14
KW
311 }
312
308482c2
KW
313 return 0 if $number <= $max_bad_category_number
314 || $Config{ccflags} =~ /\bD?NO_LOCALE_$name\b/;
cfe9fa91
KW
315
316 eval "defined &POSIX::LC_$name";
317 return 0 if $@;
cd1f7080
KW
318
319 if ($return_categories_numbers) {
320 if ($name eq 'CTYPE') {
321 unshift @categories_numbers, $number; # Always first
322 }
323 elsif ($name eq 'ALL') {
324 $has_LC_ALL = 1;
325 }
326 elsif ($name eq 'COLLATE') {
327 $has_LC_COLLATE = 1;
328 }
329 else {
330 push @categories_numbers, $number;
331 }
332 }
333 }
334 }
335
336 if ($return_categories_numbers) {
337
338 # COLLATE comes after all other locales except ALL, which comes last
339 if ($has_LC_COLLATE) {
340 push @categories_numbers, $category_number{'COLLATE'};
128e4113 341 }
cd1f7080
KW
342 if ($has_LC_ALL) {
343 push @categories_numbers, $category_number{'ALL'};
344 }
16791992
KW
345
346 @$categories_ref = @categories_numbers;
128e4113
KW
347 }
348
349 return 1;
350}
351
352
cd1f7080
KW
353sub find_locales ($;$) {
354
355 # Returns an array of all the locales we found on the system. If the
356 # optional 2nd parameter is non-zero, the list includes all found locales;
357 # otherwise it is restricted to those locales that play well with Perl, as
358 # far as we can easily determine.
359 #
360 # The first parameter is either a single locale category or a reference to
361 # a list of categories to find valid locales for it (or in the case of
362 # multiple) for all of them. Each category can be a name (like 'LC_ALL'
363 # or simply 'ALL') or the C enum value for the category.
364
f2956899 365 my $input_categories = shift;
2c6c88ec 366 my $allow_incompatible = shift // 0;
73fc293b 367
f2956899
KW
368 my @categories = (ref $input_categories) ? $input_categories->@* : $input_categories;
369 return unless locales_enabled(\@categories);
cd1f7080
KW
370
371 # Note, the subroutine call above converts the $categories into a form
372 # suitable for _trylocale().
d369fd5b
KW
373
374 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
375 # and mingw32 uses said silly CRT
376 # This doesn't seem to be an issue any more, at least on Windows XP,
377 # so re-enable the tests for Windows XP onwards.
378 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
379 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
128e4113
KW
380 return if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare')
381 && $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i);
d369fd5b
KW
382
383 # UWIN seems to loop after taint tests, just skip for now
128e4113 384 return if ($^O =~ /^uwin/);
d369fd5b 385
e1f89022 386 my @Locale;
f2956899
KW
387 _trylocale("C", \@categories, \@Locale, $allow_incompatible);
388 _trylocale("POSIX", \@categories, \@Locale, $allow_incompatible);
201f75a9 389
960e29a3 390 if ($Config{d_has_C_UTF8} && $Config{d_has_C_UTF8} eq 'true') {
f2956899 391 _trylocale("C.UTF-8", \@categories, \@Locale, $allow_incompatible);
201f75a9
KW
392 }
393
8e13243a
KW
394 # There's no point in looking at anything more if we know that setlocale
395 # will return success on any garbage or non-garbage name.
396 return sort @Locale if defined $Config{d_setlocale_accepts_any_locale_name};
397
82b1f618 398 foreach (1..16) {
f2956899
KW
399 _trylocale("ISO8859-$_", \@categories, \@Locale, $allow_incompatible);
400 _trylocale("iso8859$_", \@categories, \@Locale, $allow_incompatible);
401 _trylocale("iso8859-$_", \@categories, \@Locale, $allow_incompatible);
402 _trylocale("iso_8859_$_", \@categories, \@Locale, $allow_incompatible);
403 _trylocale("isolatin$_", \@categories, \@Locale, $allow_incompatible);
404 _trylocale("isolatin-$_", \@categories, \@Locale, $allow_incompatible);
405 _trylocale("iso_latin_$_", \@categories, \@Locale, $allow_incompatible);
73fc293b
KW
406 }
407
408 # Sanitize the environment so that we can run the external 'locale'
409 # program without the taint mode getting grumpy.
410
411 # $ENV{PATH} is special in VMS.
8b76dc1d 412 delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
73fc293b
KW
413
414 # Other subversive stuff.
8b76dc1d 415 delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)};
73fc293b
KW
416
417 if (-x "/usr/bin/locale"
1ae6ead9 418 && open(LOCALES, '-|', "/usr/bin/locale -a 2>/dev/null"))
73fc293b
KW
419 {
420 while (<LOCALES>) {
421 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
422 # ain't great when we're running this testPERL_UNICODE= so that utf8
423 # locales will cause all IO hadles to default to (assume) utf8
424 next unless utf8::valid($_);
425 chomp;
f2956899 426 _trylocale($_, \@categories, \@Locale, $allow_incompatible);
73fc293b
KW
427 }
428 close(LOCALES);
429 } elsif ($^O eq 'VMS'
430 && defined($ENV{'SYS$I18N_LOCALE'})
431 && -d 'SYS$I18N_LOCALE')
432 {
433 # The SYS$I18N_LOCALE logical name search list was not present on
434 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
435 opendir(LOCALES, "SYS\$I18N_LOCALE:");
436 while ($_ = readdir(LOCALES)) {
437 chomp;
f2956899 438 _trylocale($_, \@categories, \@Locale, $allow_incompatible);
73fc293b
KW
439 }
440 close(LOCALES);
441 } elsif (($^O eq 'openbsd' || $^O eq 'bitrig' ) && -e '/usr/share/locale') {
442
7e1e0d99
KW
443 # OpenBSD doesn't have a locale executable, so reading
444 # /usr/share/locale is much easier and faster than the last resort
445 # method.
73fc293b
KW
446
447 opendir(LOCALES, '/usr/share/locale');
448 while ($_ = readdir(LOCALES)) {
449 chomp;
f2956899 450 _trylocale($_, \@categories, \@Locale, $allow_incompatible);
73fc293b
KW
451 }
452 close(LOCALES);
453 } else { # Final fallback. Try our list of locales hard-coded here
454
455 # This is going to be slow.
456 my @Data;
457
589c4ae0
KW
458 # Locales whose name differs if the utf8 bit is on are stored in these
459 # two files with appropriate encodings.
460 my $data_file = ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/)
9969000e
TK
461 ? _source_location() . "/lib/locale/utf8"
462 : _source_location() . "/lib/locale/latin1";
589c4ae0
KW
463 if (-e $data_file) {
464 @Data = do $data_file;
465 }
466 else {
467 _my_diag(__FILE__ . ":" . __LINE__ . ": '$data_file' doesn't exist");
73fc293b
KW
468 }
469
470 # The rest of the locales are in this file.
a3e69330
TC
471 state @my_data = <DATA>; close DATA if fileno DATA;
472 push @Data, @my_data;
73fc293b
KW
473
474 foreach my $line (@Data) {
f670c2bb 475 chomp $line;
73fc293b
KW
476 my ($locale_name, $language_codes, $country_codes, $encodings) =
477 split /:/, $line;
759b442c
KW
478 _my_diag(__FILE__ . ":" . __LINE__ . ": Unexpected syntax in '$line'")
479 unless defined $locale_name;
73fc293b
KW
480 my @enc = _decode_encodings($encodings);
481 foreach my $loc (split(/ /, $locale_name)) {
f2956899 482 _trylocale($loc, \@categories, \@Locale, $allow_incompatible);
73fc293b 483 foreach my $enc (@enc) {
f2956899 484 _trylocale("$loc.$enc", \@categories, \@Locale,
2c6c88ec 485 $allow_incompatible);
73fc293b
KW
486 }
487 $loc = lc $loc;
488 foreach my $enc (@enc) {
f2956899 489 _trylocale("$loc.$enc", \@categories, \@Locale,
2c6c88ec 490 $allow_incompatible);
73fc293b
KW
491 }
492 }
493 foreach my $lang (split(/ /, $language_codes)) {
f2956899 494 _trylocale($lang, \@categories, \@Locale, $allow_incompatible);
73fc293b
KW
495 foreach my $country (split(/ /, $country_codes)) {
496 my $lc = "${lang}_${country}";
f2956899 497 _trylocale($lc, \@categories, \@Locale, $allow_incompatible);
73fc293b 498 foreach my $enc (@enc) {
f2956899 499 _trylocale("$lc.$enc", \@categories, \@Locale,
2c6c88ec 500 $allow_incompatible);
73fc293b
KW
501 }
502 my $lC = "${lang}_\U${country}";
f2956899 503 _trylocale($lC, \@categories, \@Locale, $allow_incompatible);
73fc293b 504 foreach my $enc (@enc) {
f2956899 505 _trylocale("$lC.$enc", \@categories, \@Locale,
2c6c88ec 506 $allow_incompatible);
73fc293b
KW
507 }
508 }
509 }
510 }
511 }
512
513 @Locale = sort @Locale;
514
515 return @Locale;
73fc293b
KW
516}
517
ab8b8bcc 518sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input
9b0711ee 519 # is a UTF-8 locale
92c0a900
KW
520
521 # On z/OS, even locales marked as UTF-8 aren't.
522 return 0 if ord "A" != 65;
523
cfe9fa91 524 return 0 unless locales_enabled('LC_CTYPE');
21732d5c 525
ab8b8bcc
KW
526 my $locale = shift;
527
31f05a37 528 use locale;
f079f9b7 529 no warnings 'locale'; # We may be trying out a weird locale
31f05a37
KW
530
531 my $save_locale = setlocale(&POSIX::LC_CTYPE());
532 if (! $save_locale) {
533 ok(0, "Verify could save previous locale");
534 return 0;
535 }
536
537 if (! setlocale(&POSIX::LC_CTYPE(), $locale)) {
538 ok(0, "Verify could setlocale to $locale");
539 return 0;
540 }
541
542 my $ret = 0;
543
544 # Use an op that gives different results for UTF-8 than any other locale.
545 # If a platform has UTF-8 locales, there should be at least one locale on
546 # most platforms with UTF-8 in its name, so if there is a bug in the op
547 # giving a false negative, we should get a failure for those locales as we
548 # go through testing all the locales on the platform.
549 if (CORE::fc(chr utf8::unicode_to_native(0xdf)) ne "ss") {
550 if ($locale =~ /UTF-?8/i) {
551 ok (0, "Verify $locale with UTF-8 in name is a UTF-8 locale");
552 }
553 }
554 else {
555 $ret = 1;
556 }
557
558 die "Couldn't restore locale '$save_locale'"
559 unless setlocale(&POSIX::LC_CTYPE(), $save_locale);
560
561 return $ret;
ab8b8bcc
KW
562}
563
e0bfe19f
KW
564sub find_utf8_ctype_locales (;$) { # Return the names of the locales that core
565 # Perl thinks are UTF-8 LC_CTYPE locales.
9b0711ee
KW
566 # Optional parameter is a reference to a
567 # list of locales to try; if omitted, this
568 # tries all locales it can find on the
569 # platform
cfe9fa91
KW
570 return unless locales_enabled('LC_CTYPE');
571
ab8b8bcc 572 my $locales_ref = shift;
e0bfe19f 573 my @return;
21732d5c 574
ab8b8bcc 575 if (! defined $locales_ref) {
21732d5c 576
2c6c88ec 577 my @locales = find_locales(&POSIX::LC_CTYPE());
ab8b8bcc
KW
578 $locales_ref = \@locales;
579 }
580
581 foreach my $locale (@$locales_ref) {
e0bfe19f 582 push @return, $locale if is_locale_utf8($locale);
ab8b8bcc
KW
583 }
584
e0bfe19f
KW
585 return @return;
586}
587
588
589sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl
b5954e7e 590 # thinks is a UTF-8 LC_CTYPE non-turkic
e0bfe19f
KW
591 # locale.
592 # Optional parameter is a reference to a
593 # list of locales to try; if omitted, this
594 # tries all locales it can find on the
595 # platform
596 my $try_locales_ref = shift;
597
598 my @utf8_locales = find_utf8_ctype_locales($try_locales_ref);
b5954e7e 599 my @turkic_locales = find_utf8_turkic_locales($try_locales_ref);
e0bfe19f 600
b5954e7e
KW
601 my %seen_turkic;
602
603 # Create undef elements in the hash for turkic locales
604 @seen_turkic{@turkic_locales} = ();
605
606 foreach my $locale (@utf8_locales) {
607 return $locale unless exists $seen_turkic{$locale};
608 }
e0bfe19f 609
ab8b8bcc
KW
610 return;
611}
612
b5954e7e
KW
613sub find_utf8_turkic_locales (;$) {
614
615 # Return the name of all the locales that core Perl thinks are UTF-8
616 # Turkic LC_CTYPE. Optional parameter is a reference to a list of locales
617 # to try; if omitted, this tries all locales it can find on the platform
618
619 my @return;
620
e41845ea
KW
621 return unless locales_enabled('LC_CTYPE');
622
b5954e7e
KW
623 my $save_locale = setlocale(&POSIX::LC_CTYPE());
624 foreach my $locale (find_utf8_ctype_locales(shift)) {
625 use locale;
626 setlocale(&POSIX::LC_CTYPE(), $locale);
627 push @return, $locale if uc('i') eq "\x{130}";
628 }
629 setlocale(&POSIX::LC_CTYPE(), $save_locale);
630
631 return @return;
632}
633
634sub find_utf8_turkic_locale (;$) {
635 my @turkics = find_utf8_turkic_locales(shift);
636
637 return unless @turkics;
638 return $turkics[0]
639}
640
641
9969000e
TK
642# returns full path to the directory containing the current source
643# file, inspired by mauke's Dir::Self
644sub _source_location {
645 require File::Spec;
646
647 my $caller_filename = (caller)[1];
648
b3c872eb 649 my $loc = File::Spec->rel2abs(
9969000e 650 File::Spec->catpath(
b3c872eb 651 (File::Spec->splitpath($caller_filename))[0, 1], ''
9969000e
TK
652 )
653 );
b3c872eb
TK
654
655 return ($loc =~ /^(.*)$/)[0]; # untaint
9969000e
TK
656}
657
73fc293b
KW
6581
659
660# Format of data is: locale_name, language_codes, country_codes, encodings
661__DATA__
662Afrikaans:af:za:1 15
663Arabic:ar:dz eg sa:6 arabic8
664Brezhoneg Breton:br:fr:1 15
665Bulgarski Bulgarian:bg:bg:5
666Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
667Hrvatski Croatian:hr:hr:2
668Cymraeg Welsh:cy:cy:1 14 15
669Czech:cs:cz:2
670Dansk Danish:da:dk:1 15
671Nederlands Dutch:nl:be nl:1 15
672English American British:en:au ca gb ie nz us uk zw:1 15 cp850
673Esperanto:eo:eo:3
674Eesti Estonian:et:ee:4 6 13
675Suomi Finnish:fi:fi:1 15
676Flamish::fl:1 15
677Deutsch German:de:at be ch de lu:1 15
678Euskaraz Basque:eu:es fr:1 15
679Galego Galician:gl:es:1 15
680Ellada Greek:el:gr:7 g8
681Frysk:fy:nl:1 15
682Greenlandic:kl:gl:4 6
683Hebrew:iw:il:8 hebrew8
684Hungarian:hu:hu:2
685Indonesian:id:id:1 15
686Gaeilge Irish:ga:IE:1 14 15
687Italiano Italian:it:ch it:1 15
688Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
689Korean:ko:kr:
690Latine Latin:la:va:1 15
691Latvian:lv:lv:4 6 13
692Lithuanian:lt:lt:4 6 13
693Macedonian:mk:mk:1 15
694Maltese:mt:mt:3
695Moldovan:mo:mo:2
696Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
697Occitan:oc:es:1 15
698Polski Polish:pl:pl:2
699Rumanian:ro:ro:2
700Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
701Serbski Serbian:sr:yu:5
702Slovak:sk:sk:2
703Slovene Slovenian:sl:si:2
704Sqhip Albanian:sq:sq:1 15
705Svenska Swedish:sv:fi se:1 15
706Thai:th:th:11 tis620
707Turkish:tr:tr:9 turkish8
708Yiddish:yi::1 15