Commit | Line | Data |
---|---|---|
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 | 13 | use Config; |
e1f89022 | 14 | use strict; |
27526f3a | 15 | use warnings; |
a3e69330 | 16 | use feature 'state'; |
e1f89022 | 17 | |
e8954fbe KW |
18 | my %known_bad_locales = ( # XXX eventually will need version info if and |
19 | # when these get fixed. | |
9a266c09 KW |
20 | solaris => [ 'vi_VN.UTF-8', ], # Use of U+A8 segfaults: GH #20578 |
21 | ); | |
e8954fbe | 22 | |
a274778c | 23 | eval { require POSIX; import POSIX 'locale_h'; }; |
e1f89022 | 24 | my $has_locale_h = ! $@; |
a274778c | 25 | |
83858d2d KW |
26 | my @known_categories = ( qw(LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY |
27 | LC_NUMERIC LC_TIME LC_ADDRESS LC_IDENTIFICATION | |
36dbc955 | 28 | LC_MEASUREMENT LC_PAPER LC_TELEPHONE LC_SYNTAX |
6125a5e7 | 29 | LC_TOD LC_NAME)); |
83858d2d KW |
30 | my @platform_categories; |
31 | ||
225d64bc | 32 | my $has_excluded_category = $Config{ccflags} =~ /\bD?NO_LOCALE_/; |
0f81abe0 | 33 | sub category_excluded($) { |
8aed2978 KW |
34 | my $cat_name = shift =~ s/^LC_//r; |
35 | ||
36 | # Recognize Configure option to exclude a category | |
225d64bc KW |
37 | return $has_excluded_category |
38 | && $Config{ccflags} =~ /\bD?NO_LOCALE_$cat_name\b/; | |
8aed2978 KW |
39 | } |
40 | ||
6fa6c80b KW |
41 | # LC_ALL can be -1 on some platforms. And, in fact the implementors could |
42 | # legally use any integer to represent any category. But it makes the most | |
43 | # sense for them to have used small integers. Below, we create new locale | |
44 | # numbers for ones missing from this machine. We make them very negative, | |
45 | # hopefully more negative than anything likely to be a valid category on the | |
46 | # platform, but also below is a check to be sure that our guess is valid. | |
47 | my $max_bad_category_number = -1000000; | |
48 | ||
49 | # Initialize this hash so that it looks like e.g., | |
50 | # 6 => 'CTYPE', | |
51 | # where 6 is the value of &POSIX::LC_CTYPE | |
52 | my %category_name; | |
cd1f7080 | 53 | my %category_number; |
86e85139 | 54 | if ($has_locale_h) { |
6fa6c80b | 55 | my $number_for_missing_category = $max_bad_category_number; |
83858d2d KW |
56 | foreach my $name (@known_categories) { |
57 | my $number = eval "&POSIX::$name"; | |
6fa6c80b KW |
58 | if ($@) { |
59 | # Use a negative number (smaller than any legitimate category | |
60 | # number) if the platform doesn't support this category, so we | |
61 | # have an entry for all the ones that might be specified in calls | |
62 | # to us. | |
829290ef | 63 | $number = $number_for_missing_category--; |
6fa6c80b KW |
64 | } |
65 | elsif ( $number !~ / ^ -? \d+ $ /x | |
66 | || $number <= $max_bad_category_number) | |
67 | { | |
68 | # We think this should be an int. And it has to be larger than | |
69 | # any of our synthetic numbers. | |
83858d2d KW |
70 | die "Unexpected locale category number '$number' for $name" |
71 | } | |
72 | else { | |
73 | push @platform_categories, $name; | |
6fa6c80b KW |
74 | } |
75 | ||
83858d2d | 76 | $name =~ s/LC_//; |
6fa6c80b | 77 | $category_name{$number} = "$name"; |
cd1f7080 | 78 | $category_number{$name} = $number; |
6fa6c80b KW |
79 | } |
80 | } | |
81 | ||
759b442c KW |
82 | sub _my_diag($) { |
83 | my $message = shift; | |
84 | if (defined &main::diag) { | |
85 | diag($message); | |
86 | } | |
87 | else { | |
88 | local($\, $", $,) = (undef, ' ', ''); | |
89 | print STDERR $message, "\n"; | |
90 | } | |
91 | } | |
92 | ||
06b6e87b KW |
93 | # Larger than any real test |
94 | my $my_count = 1_000_000; | |
95 | ||
13022195 KW |
96 | sub _my_fail($) { |
97 | my $message = shift; | |
98 | if (defined &main::fail) { | |
99 | fail($message); | |
100 | } | |
101 | else { | |
102 | local($\, $", $,) = (undef, ' ', ''); | |
06b6e87b | 103 | print "not ok " . $my_count++ . $message . "\n"; |
13022195 KW |
104 | } |
105 | } | |
106 | ||
1ab9fcf7 KW |
107 | sub platform_locale_categories() { |
108 | return @platform_categories; | |
109 | } | |
110 | ||
11154ad9 KW |
111 | sub valid_locale_categories() { |
112 | # Returns a list of the locale categories (expressed as strings, like | |
a44bf644 | 113 | # "LC_ALL") known to this program that are available on this platform. |
11154ad9 KW |
114 | |
115 | return grep { ! category_excluded($_) } @platform_categories; | |
116 | } | |
117 | ||
118 | sub is_category_valid($) { | |
119 | my $name = shift; | |
120 | $name = 'LC_' . $name =~ s/^LC_//r; | |
121 | return grep { $name eq $_ } valid_locale_categories(); | |
122 | } | |
123 | ||
d87c0f3a KW |
124 | # It turns out that strings generated under the control of a given locale |
125 | # category are often affected as well by LC_CTYPE. If the two categories | |
126 | # don't match, one can get mojibake or even core dumps. (khw thinks it more | |
127 | # likely that it's the code set, not the locale that's critical here; but | |
128 | # didn't run experiments to verify this.) Hence, in the code below, CTYPE and | |
129 | # the tested categories are all set to the same locale. If CTYPE isn't | |
130 | # available on the platform, LC_ALL is instead used. One might think to just | |
131 | # use LC_ALL all the time, but on Windows | |
132 | # setlocale(LC_ALL, "some_borked_locale") | |
133 | # can return success, whereas setting LC_CTYPE to it fails. | |
134 | my $master_category; | |
135 | $master_category = $category_number{'CTYPE'} | |
136 | if is_category_valid('LC_CTYPE') && defined $category_number{'CTYPE'}; | |
137 | $master_category = $category_number{'ALL'} | |
138 | if ! defined $master_category | |
139 | && is_category_valid('LC_ALL') && defined $category_number{'ALL'}; | |
140 | ||
e8cff0e8 KW |
141 | my @platform_locales; # cache of locales found on this platform |
142 | my $gathering_platform_locales = 0; # Should we gather locales, or use the | |
143 | # cache? | |
144 | my %seen; # Used to avoid duplicates | |
145 | ||
bb31ec6a KW |
146 | sub _trylocale ($$$$) { # For use only by other functions in this file! |
147 | ||
148 | # Adds the locale given by the first parameter to the list given by the | |
cd1f7080 KW |
149 | # 3rd iff the platform supports the locale in each of the category numbers |
150 | # given by the 2nd parameter, which is either a single category or a | |
fcabef25 | 151 | # reference to a list of categories. |
cd1f7080 KW |
152 | # |
153 | # The 4th parameter is true if to accept locales that aren't apparently | |
154 | # fully compatible with Perl. | |
bb31ec6a | 155 | |
73fc293b | 156 | my $locale = shift; |
9b0711ee | 157 | my $categories = shift; |
73fc293b | 158 | my $list = shift; |
2c6c88ec | 159 | my $allow_incompatible = shift; |
f079f9b7 | 160 | |
55aaa55d KW |
161 | my $normalized_locale = lc ($locale =~ s/\W//gr); |
162 | return if ! $locale || grep { $normalized_locale eq lc ($_ =~ s/\W//gr) } @$list; | |
9b0711ee | 163 | |
577d3e04 KW |
164 | # This is a toy (pig latin) locale that is not fully implemented on some |
165 | # systems | |
166 | return if $locale =~ / ^ pig $ /ix; | |
167 | ||
8e13243a KW |
168 | # Certain platforms have a crippled locale system in which setlocale |
169 | # returns success for just about any possible locale name, but if anything | |
170 | # actually happens as a result of the call, it is that the underlying | |
171 | # locale is set to a system default, likely C or C.UTF-8. We can't test | |
172 | # such systems fully, but we shouldn't disable the user from using | |
173 | # locales, as it may work out for them (or not). | |
174 | return if defined $Config{d_setlocale_accepts_any_locale_name} | |
4d564167 | 175 | && $locale !~ / ^ (?: C | POSIX | C\.UTF-?8 ) $/ix; |
99617871 | 176 | |
e8954fbe KW |
177 | if (exists $known_bad_locales{$^O}) { |
178 | my @bad_locales = $known_bad_locales{$^O}->@*; | |
179 | return if grep { $locale eq $_ } @bad_locales; | |
180 | } | |
181 | ||
9b0711ee | 182 | |
f079f9b7 KW |
183 | my $badutf8 = 0; |
184 | my $plays_well = 1; | |
74ecb5a1 | 185 | my $unsupported = 0; |
f079f9b7 KW |
186 | |
187 | use warnings 'locale'; | |
188 | ||
189 | local $SIG{__WARN__} = sub { | |
973072b2 | 190 | $badutf8 = 1 if grep { /Malformed UTF-8/ } @_; |
74ecb5a1 | 191 | $unsupported = 1 if grep { /Locale .* is unsupported/i } @_; |
1649de0c | 192 | $plays_well = 0 if grep { |
fd785c18 | 193 | /The following characters .* may not have the same meaning as the Perl program expects(?# |
ef5d49f0 | 194 | )|The Perl program will use the expected meanings/i |
1649de0c | 195 | } @_; |
f079f9b7 KW |
196 | }; |
197 | ||
80e09067 | 198 | my $result; |
c8820453 KW |
199 | my @category_list; |
200 | if (defined $categories) { | |
201 | $categories = [ $categories ] unless ref $categories; | |
202 | push @category_list, $categories->@*; | |
203 | } | |
204 | ||
205 | # Make the master category first thing on the list; adding it if necessary | |
206 | if (defined $master_category) { | |
207 | @category_list = grep { $_ != $master_category } @category_list; | |
208 | unshift @category_list, $master_category; | |
209 | } | |
d87c0f3a | 210 | |
c8820453 | 211 | foreach my $category (@category_list) { |
d87c0f3a KW |
212 | my $save_locale = setlocale($category); |
213 | if (! $save_locale) { | |
214 | _my_fail("Verify could save previous locale"); | |
215 | return; | |
fcabef25 | 216 | } |
cd1f7080 | 217 | |
d87c0f3a KW |
218 | # Incompatible locales aren't warned about unless using locales. |
219 | use locale; | |
220 | ||
5077456c KW |
221 | my $cur_result = setlocale($category, $locale); |
222 | return unless defined $cur_result; | |
73fc293b | 223 | |
63eccc74 KW |
224 | no locale; |
225 | ||
e8cff0e8 KW |
226 | if ( $gathering_platform_locales |
227 | && $category eq $master_category | |
228 | && ! $seen{$locale}) | |
229 | { | |
230 | push @platform_locales, $locale; | |
231 | $seen{$locale}++; | |
232 | } | |
233 | ||
eb1c747c KW |
234 | # We definitely don't want the locale set to something that is |
235 | # unsupported | |
2740baa9 KW |
236 | if (! setlocale($category, $save_locale)) { |
237 | my $error_text = "\$!=$!"; | |
238 | $error_text .= "; \$^E=$^E" if $^E != $!; | |
239 | die "Couldn't restore locale '$save_locale', category $category;" | |
240 | . $error_text; | |
241 | } | |
63eccc74 KW |
242 | if ($badutf8) { |
243 | _my_fail("Verify locale name doesn't contain malformed utf8"); | |
244 | return; | |
245 | } | |
246 | ||
74ecb5a1 KW |
247 | return if $unsupported; |
248 | ||
7a7a0c20 KW |
249 | # Commas in locale names are bad in Windows, and there is a bug in |
250 | # some versions where setlocale() turns a legal input locale name into | |
251 | # an illegal return value, which it can't later parse. | |
5077456c | 252 | return if $cur_result =~ /,/; |
7a7a0c20 | 253 | |
63eccc74 | 254 | return unless $plays_well || $allow_incompatible; |
80e09067 KW |
255 | |
256 | if (! defined $result) { # First time | |
225d64bc KW |
257 | |
258 | # If the name returned as $cur_result by the setlocale() above is the | |
259 | # same as we requested, there are no complications: use that. | |
260 | if ($locale eq $cur_result) { | |
80e09067 | 261 | $result = $cur_result; |
225d64bc KW |
262 | } |
263 | else { | |
264 | ||
265 | # But if it's different, we check if it's part of a disparate | |
266 | # LC_ALL. If so, use the input locale; if not it means the | |
267 | # input was a synonym, and we use what it maps to. | |
268 | # | |
269 | # First, if the platform uses positional notation | |
270 | if ($Config{PERL_LC_ALL_SEPARATOR}) { | |
271 | $result = (index($cur_result, $Config{PERL_LC_ALL_SEPARATOR}) | |
272 | >= 0) | |
273 | ? $locale | |
274 | : $cur_result; | |
275 | } | |
276 | else { # Must be using name=value notation | |
277 | $result = ($cur_result =~ / = .* ; /x) | |
278 | ? $locale | |
279 | : $cur_result; | |
280 | } | |
281 | } | |
80e09067 | 282 | } |
225d64bc | 283 | elsif (! $has_excluded_category && $result ne $cur_result) { |
80e09067 KW |
284 | |
285 | # Some platforms will translate POSIX into C | |
286 | if (! ( ($result eq "C" && $cur_result eq "POSIX") | |
287 | || ($result eq "POSIX" && $cur_result eq "C"))) | |
288 | { | |
289 | # But otherwise if the new result for this category doesn't | |
290 | # match what we already have for a previous category for this | |
291 | # same input locale, it's problematic, so discard this whole | |
292 | # locale. | |
293 | return; | |
294 | } | |
295 | } | |
73fc293b | 296 | } |
d87c0f3a | 297 | |
80e09067 | 298 | push @$list, $result; |
73fc293b KW |
299 | } |
300 | ||
bb31ec6a | 301 | sub _decode_encodings { # For use only by other functions in this file! |
73fc293b KW |
302 | my @enc; |
303 | ||
304 | foreach (split(/ /, shift)) { | |
305 | if (/^(\d+)$/) { | |
306 | push @enc, "ISO8859-$1"; | |
d49bacc8 | 307 | push @enc, "ISO-8859-$1"; |
73fc293b KW |
308 | push @enc, "iso8859$1"; # HP |
309 | if ($1 eq '1') { | |
310 | push @enc, "roman8"; # HP | |
311 | } | |
312 | push @enc, $_; | |
313 | push @enc, "$_.UTF-8"; | |
d646bffe KW |
314 | push @enc, "$_.65001"; # Windows UTF-8 |
315 | push @enc, "$_.ACP"; # Windows ANSI code page | |
316 | push @enc, "$_.OCP"; # Windows OEM code page | |
cf34c81f | 317 | push @enc, "$_.1252"; # Windows |
73fc293b KW |
318 | } |
319 | } | |
320 | if ($^O eq 'os390') { | |
321 | push @enc, qw(IBM-037 IBM-819 IBM-1047); | |
322 | } | |
323 | push @enc, "UTF-8"; | |
d646bffe | 324 | push @enc, "65001"; # Windows UTF-8 |
73fc293b KW |
325 | |
326 | return @enc; | |
327 | } | |
328 | ||
128e4113 | 329 | sub locales_enabled(;$) { |
23f9d84e KW |
330 | # If no parameter is specified, the function returns 1 if there is any |
331 | # "safe" locale handling available to the caller; otherwise 0. Safeness | |
332 | # is defined here as the caller operating in the main thread of a program, | |
333 | # or if threaded locales are safe on the platform and Configured to be | |
334 | # used. This sub is used for testing purposes, and for those, this | |
335 | # definition of safety is sufficient, and necessary to get some tests to | |
336 | # run on certain configurations on certain platforms. But beware that the | |
337 | # main thread can change the locale of any subthreads unless | |
338 | # ${^SAFE_LOCALES} is non-zero. | |
128e4113 | 339 | # |
23f9d84e KW |
340 | # Use the optional parameter to discover if a particular category or |
341 | # categories are available on the system. 1 is returned if the global | |
342 | # criteria described in the previous paragraph are true, AND if all the | |
343 | # specified categories are available on the platform and Configured to be | |
344 | # used. Otherwise 0 is returned. The parameter is either a single POSIX | |
345 | # locale category or a reference to a list of them. Each category must be | |
346 | # its name as a string, like 'LC_TIME' (the initial 'LC_' is optional), or | |
347 | # the number this platform uses to signify the category (e.g., | |
348 | # 'locales_enabled(&POSIX::LC_CTYPE)' | |
128e4113 | 349 | # |
23f9d84e KW |
350 | # When the function returns 1 and a parameter was specified as a list |
351 | # reference, the reference will be altered on return to point to an | |
352 | # equivalent list such that the categories are numeric instead of strings | |
353 | # and sorted to meet the input expectations of _trylocale(). | |
cd1f7080 | 354 | # |
23f9d84e KW |
355 | # It is a fatal error to call this with something that isn't a known |
356 | # category to this file. If this happens, look first for a typo, and | |
357 | # second if you are using a category unknown to Perl. In the latter case | |
358 | # a bug report should be submitted. | |
128e4113 | 359 | |
aa8a2baa KW |
360 | # khw cargo-culted the '?' in the pattern on the next line. |
361 | return 0 if $Config{ccflags} =~ /\bD?NO_LOCALE\b/; | |
362 | ||
d373bd46 KW |
363 | # If we can't load the POSIX XS module, we can't have locales even if they |
364 | # normally would be available | |
365 | return 0 if ! defined &DynaLoader::boot_DynaLoader; | |
366 | ||
b0441c5b KW |
367 | # Don't test locales where they aren't safe. On systems with unsafe |
368 | # threads, for the purposes of testing, we consider the main thread safe, | |
369 | # and all other threads unsafe. | |
370 | if (! ${^SAFE_LOCALES}) { | |
01175909 | 371 | return 0 if $^O eq 'os390'; # Threaded locales don't work well here |
b0441c5b KW |
372 | require threads; |
373 | return 0 if threads->tid() != 0; | |
374 | } | |
39481c55 KW |
375 | |
376 | # If no setlocale, we need the POSIX 2008 alternatives | |
aa8a2baa KW |
377 | if (! $Config{d_setlocale}) { |
378 | return 0 if $Config{ccflags} =~ /\bD?NO_POSIX_2008_LOCALE\b/; | |
379 | return 0 unless $Config{d_newlocale}; | |
380 | return 0 unless $Config{d_uselocale}; | |
381 | return 0 unless $Config{d_duplocale}; | |
382 | return 0 unless $Config{d_freelocale}; | |
383 | } | |
128e4113 KW |
384 | |
385 | # Done with the global possibilities. Now check if any passed in category | |
386 | # is disabled. | |
cd1f7080 | 387 | |
23f9d84e | 388 | my $categories_ref = $_[0]; |
cd1f7080 KW |
389 | my $return_categories_numbers = 0; |
390 | my @categories_numbers; | |
391 | my $has_LC_ALL = 0; | |
392 | my $has_LC_COLLATE = 0; | |
393 | ||
e6965c14 | 394 | if (defined $categories_ref) { |
cd1f7080 KW |
395 | my @local_categories_copy; |
396 | ||
23f9d84e KW |
397 | my $reftype = ref $categories_ref; |
398 | if ($reftype eq 'ARRAY') { | |
399 | @local_categories_copy = @$categories_ref; | |
cd1f7080 KW |
400 | $return_categories_numbers = 1; |
401 | } | |
23f9d84e KW |
402 | elsif ($reftype ne "") { |
403 | die "Parameter to locales_enabled() must be an ARRAY;" | |
404 | . " instead you used a $reftype"; | |
405 | } | |
cd1f7080 KW |
406 | else { # Single category passed in |
407 | @local_categories_copy = $categories_ref; | |
408 | } | |
409 | ||
308482c2 KW |
410 | for my $category_name_or_number (@local_categories_copy) { |
411 | my $name; | |
412 | my $number; | |
413 | if ($category_name_or_number =~ / ^ -? \d+ $ /x) { | |
414 | $number = $category_name_or_number; | |
415 | die "Invalid locale category number '$number'" | |
416 | unless grep { $number == $_ } keys %category_name; | |
417 | $name = $category_name{$number}; | |
e6965c14 KW |
418 | } |
419 | else { | |
308482c2 KW |
420 | $name = $category_name_or_number; |
421 | $name =~ s/ ^ LC_ //x; | |
422 | foreach my $trial (keys %category_name) { | |
423 | if ($category_name{$trial} eq $name) { | |
424 | $number = $trial; | |
425 | last; | |
426 | } | |
427 | } | |
428 | die "Invalid locale category name '$name'" | |
429 | unless defined $number; | |
e6965c14 KW |
430 | } |
431 | ||
0f81abe0 KW |
432 | return 0 if $number <= $max_bad_category_number |
433 | || category_excluded($name); | |
8aed2978 | 434 | |
cfe9fa91 KW |
435 | |
436 | eval "defined &POSIX::LC_$name"; | |
437 | return 0 if $@; | |
cd1f7080 KW |
438 | |
439 | if ($return_categories_numbers) { | |
440 | if ($name eq 'CTYPE') { | |
441 | unshift @categories_numbers, $number; # Always first | |
442 | } | |
443 | elsif ($name eq 'ALL') { | |
444 | $has_LC_ALL = 1; | |
445 | } | |
446 | elsif ($name eq 'COLLATE') { | |
447 | $has_LC_COLLATE = 1; | |
448 | } | |
449 | else { | |
450 | push @categories_numbers, $number; | |
451 | } | |
452 | } | |
453 | } | |
454 | } | |
455 | ||
456 | if ($return_categories_numbers) { | |
457 | ||
458 | # COLLATE comes after all other locales except ALL, which comes last | |
459 | if ($has_LC_COLLATE) { | |
460 | push @categories_numbers, $category_number{'COLLATE'}; | |
128e4113 | 461 | } |
cd1f7080 KW |
462 | if ($has_LC_ALL) { |
463 | push @categories_numbers, $category_number{'ALL'}; | |
464 | } | |
16791992 KW |
465 | |
466 | @$categories_ref = @categories_numbers; | |
128e4113 KW |
467 | } |
468 | ||
469 | return 1; | |
470 | } | |
471 | ||
472 | ||
cd1f7080 KW |
473 | sub find_locales ($;$) { |
474 | ||
475 | # Returns an array of all the locales we found on the system. If the | |
476 | # optional 2nd parameter is non-zero, the list includes all found locales; | |
477 | # otherwise it is restricted to those locales that play well with Perl, as | |
478 | # far as we can easily determine. | |
479 | # | |
480 | # The first parameter is either a single locale category or a reference to | |
481 | # a list of categories to find valid locales for it (or in the case of | |
482 | # multiple) for all of them. Each category can be a name (like 'LC_ALL' | |
483 | # or simply 'ALL') or the C enum value for the category. | |
484 | ||
f2956899 | 485 | my $input_categories = shift; |
2c6c88ec | 486 | my $allow_incompatible = shift // 0; |
73fc293b | 487 | |
7b25dc6d KW |
488 | die ("Usage: find_locales( category | [ categories ] )") |
489 | unless defined $input_categories; | |
89458b6a KW |
490 | my @categories = (ref $input_categories) |
491 | ? $input_categories->@* | |
492 | : $input_categories; | |
5bb402b5 KW |
493 | |
494 | # If we can't use at least one of these categories, investigate further | |
495 | if (! locales_enabled(\@categories)) { | |
496 | ||
497 | # Not usable at all if system doesn't have locales | |
498 | return unless locales_enabled(); | |
499 | ||
500 | # Nor if any of the required categories isn't on the system | |
501 | my @on_platform = platform_locale_categories(); | |
502 | for my $category (@categories) { | |
503 | return unless grep { $category eq $_ } @on_platform; | |
504 | } | |
505 | ||
506 | # Otherwise the category is on the system, but not generally usable. | |
507 | # But the two always-present locales should be usable | |
508 | return ( "C", "POSIX" ); | |
509 | } | |
510 | ||
cd1f7080 KW |
511 | |
512 | # Note, the subroutine call above converts the $categories into a form | |
513 | # suitable for _trylocale(). | |
d369fd5b KW |
514 | |
515 | # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1" | |
516 | # and mingw32 uses said silly CRT | |
517 | # This doesn't seem to be an issue any more, at least on Windows XP, | |
518 | # so re-enable the tests for Windows XP onwards. | |
519 | my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion && | |
520 | join('.', (Win32::GetOSVersion())[1..2]) >= 5.1); | |
2eb109a4 | 521 | return if (($^O eq 'MSWin32' && !$winxp) |
128e4113 | 522 | && $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i); |
d369fd5b | 523 | |
e1f89022 | 524 | my @Locale; |
201f75a9 | 525 | |
e8cff0e8 KW |
526 | if (@platform_locales) { |
527 | $gathering_platform_locales = 0; | |
528 | foreach my $locale (@platform_locales) { | |
529 | _trylocale($locale, \@categories, \@Locale, $allow_incompatible); | |
530 | } | |
201f75a9 | 531 | } |
e8cff0e8 KW |
532 | else { |
533 | $gathering_platform_locales = 1; | |
534 | ||
535 | _trylocale("C", \@categories, \@Locale, $allow_incompatible); | |
536 | _trylocale("POSIX", \@categories, \@Locale, $allow_incompatible); | |
201f75a9 | 537 | |
3245d1b9 KW |
538 | if ($Config{d_has_C_UTF8} && $Config{d_has_C_UTF8} eq 'true') { |
539 | _trylocale("C.UTF-8", \@categories, \@Locale, $allow_incompatible); | |
540 | } | |
541 | ||
0ede794e KW |
542 | # There's no point in looking at anything more if we know that |
543 | # setlocale will return success on any garbage or non-garbage name. | |
544 | return sort @Locale | |
545 | if defined $Config{d_setlocale_accepts_any_locale_name}; | |
546 | ||
547 | foreach (1..16) { | |
548 | _trylocale("ISO8859-$_", \@categories, \@Locale, | |
549 | $allow_incompatible); | |
550 | _trylocale("iso8859$_", \@categories, \@Locale, | |
551 | $allow_incompatible); | |
552 | _trylocale("iso8859-$_", \@categories, \@Locale, | |
553 | $allow_incompatible); | |
554 | _trylocale("iso_8859_$_", \@categories, \@Locale, | |
555 | $allow_incompatible); | |
556 | _trylocale("isolatin$_", \@categories, \@Locale, | |
557 | $allow_incompatible); | |
558 | _trylocale("isolatin-$_", \@categories, \@Locale, | |
559 | $allow_incompatible); | |
560 | _trylocale("iso_latin_$_", \@categories, \@Locale, | |
561 | $allow_incompatible); | |
73fc293b | 562 | } |
73fc293b | 563 | |
0ede794e KW |
564 | # Sanitize the environment so that we can run the external 'locale' |
565 | # program without the taint mode getting grumpy. | |
73fc293b | 566 | |
0ede794e KW |
567 | # $ENV{PATH} is special in VMS. |
568 | delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv}; | |
73fc293b | 569 | |
0ede794e KW |
570 | # Other subversive stuff. |
571 | delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)}; | |
572 | ||
573 | if (-x "/usr/bin/locale" | |
574 | && open(LOCALES, '-|', "/usr/bin/locale -a 2>/dev/null")) | |
575 | { | |
576 | while (<LOCALES>) { | |
577 | ||
578 | # It seems that /usr/bin/locale steadfastly outputs 8 bit | |
579 | # data, which ain't great when we're running this | |
580 | # testPERL_UNICODE= so that utf8 locales will cause all IO | |
581 | # hadles to default to (assume) utf8 | |
582 | next unless utf8::valid($_); | |
583 | chomp; | |
584 | _trylocale($_, \@categories, \@Locale, $allow_incompatible); | |
73fc293b | 585 | } |
0ede794e KW |
586 | |
587 | close(LOCALES); | |
588 | } elsif ($^O eq 'VMS' | |
589 | && defined($ENV{'SYS$I18N_LOCALE'}) | |
590 | && -d 'SYS$I18N_LOCALE') | |
591 | { | |
592 | # The SYS$I18N_LOCALE logical name search list was not present on | |
593 | # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later | |
594 | # versions. | |
595 | opendir(LOCALES, "SYS\$I18N_LOCALE:"); | |
596 | while ($_ = readdir(LOCALES)) { | |
597 | chomp; | |
598 | _trylocale($_, \@categories, \@Locale, $allow_incompatible); | |
599 | } | |
600 | close(LOCALES); | |
601 | } elsif ( ($^O eq 'openbsd' || $^O eq 'bitrig' ) | |
602 | && -e '/usr/share/locale') | |
603 | { | |
604 | ||
605 | # OpenBSD doesn't have a locale executable, so reading | |
606 | # /usr/share/locale is much easier and faster than the last resort | |
607 | # method. | |
608 | ||
609 | opendir(LOCALES, '/usr/share/locale'); | |
610 | while ($_ = readdir(LOCALES)) { | |
611 | chomp; | |
612 | _trylocale($_, \@categories, \@Locale, $allow_incompatible); | |
613 | } | |
614 | close(LOCALES); | |
615 | } else { # Final fallback. Try our list of locales hard-coded here | |
616 | ||
617 | # This is going to be slow. | |
618 | my @Data; | |
619 | ||
620 | # Locales whose name differs if the utf8 bit is on are stored in | |
621 | # these two files with appropriate encodings. | |
622 | my $data_file = ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/) | |
623 | ? _source_location() . "/lib/locale/utf8" | |
624 | : _source_location() . "/lib/locale/latin1"; | |
625 | if (-e $data_file) { | |
626 | @Data = do $data_file; | |
627 | } | |
628 | else { | |
629 | _my_diag(__FILE__ . ":" . __LINE__ . | |
630 | ": '$data_file' doesn't exist"); | |
631 | } | |
632 | ||
633 | # The rest of the locales are in this file. | |
634 | state @my_data = <DATA>; close DATA if fileno DATA; | |
635 | push @Data, @my_data; | |
636 | ||
637 | foreach my $line (@Data) { | |
638 | chomp $line; | |
639 | my ($locale_name, $language_codes, $country_codes, $encodings) = | |
640 | split /:/, $line; | |
641 | _my_diag(__FILE__ . ":" . __LINE__ | |
642 | . ": Unexpected syntax in '$line'") | |
643 | unless defined $locale_name; | |
644 | my @enc = _decode_encodings($encodings); | |
645 | foreach my $loc (split(/ /, $locale_name)) { | |
646 | _trylocale($loc, \@categories, \@Locale, | |
647 | $allow_incompatible); | |
73fc293b | 648 | foreach my $enc (@enc) { |
0ede794e KW |
649 | _trylocale("$loc.$enc", \@categories, \@Locale, |
650 | $allow_incompatible); | |
73fc293b | 651 | } |
0ede794e | 652 | $loc = lc $loc; |
73fc293b | 653 | foreach my $enc (@enc) { |
0ede794e KW |
654 | _trylocale("$loc.$enc", \@categories, \@Locale, |
655 | $allow_incompatible); | |
656 | } | |
657 | } | |
658 | foreach my $lang (split(/ /, $language_codes)) { | |
659 | _trylocale($lang, \@categories, \@Locale, | |
660 | $allow_incompatible); | |
661 | foreach my $country (split(/ /, $country_codes)) { | |
662 | my $lc = "${lang}_${country}"; | |
663 | _trylocale($lc, \@categories, \@Locale, | |
664 | $allow_incompatible); | |
665 | foreach my $enc (@enc) { | |
666 | _trylocale("$lc.$enc", \@categories, \@Locale, | |
667 | $allow_incompatible); | |
668 | } | |
669 | my $lC = "${lang}_\U${country}"; | |
670 | _trylocale($lC, \@categories, \@Locale, | |
671 | $allow_incompatible); | |
672 | foreach my $enc (@enc) { | |
673 | _trylocale("$lC.$enc", \@categories, \@Locale, | |
674 | $allow_incompatible); | |
675 | } | |
73fc293b KW |
676 | } |
677 | } | |
678 | } | |
679 | } | |
680 | } | |
681 | ||
682 | @Locale = sort @Locale; | |
683 | ||
684 | return @Locale; | |
73fc293b KW |
685 | } |
686 | ||
ab8b8bcc | 687 | sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input |
9b0711ee | 688 | # is a UTF-8 locale |
92c0a900 KW |
689 | |
690 | # On z/OS, even locales marked as UTF-8 aren't. | |
691 | return 0 if ord "A" != 65; | |
692 | ||
cfe9fa91 | 693 | return 0 unless locales_enabled('LC_CTYPE'); |
21732d5c | 694 | |
ab8b8bcc KW |
695 | my $locale = shift; |
696 | ||
f079f9b7 | 697 | no warnings 'locale'; # We may be trying out a weird locale |
018fd087 | 698 | use locale; |
31f05a37 KW |
699 | |
700 | my $save_locale = setlocale(&POSIX::LC_CTYPE()); | |
701 | if (! $save_locale) { | |
06b6e87b | 702 | _my_fail("Verify could save previous locale"); |
31f05a37 KW |
703 | return 0; |
704 | } | |
705 | ||
706 | if (! setlocale(&POSIX::LC_CTYPE(), $locale)) { | |
06b6e87b | 707 | _my_fail("Verify could setlocale to $locale"); |
31f05a37 KW |
708 | return 0; |
709 | } | |
710 | ||
711 | my $ret = 0; | |
712 | ||
713 | # Use an op that gives different results for UTF-8 than any other locale. | |
714 | # If a platform has UTF-8 locales, there should be at least one locale on | |
715 | # most platforms with UTF-8 in its name, so if there is a bug in the op | |
716 | # giving a false negative, we should get a failure for those locales as we | |
717 | # go through testing all the locales on the platform. | |
718 | if (CORE::fc(chr utf8::unicode_to_native(0xdf)) ne "ss") { | |
719 | if ($locale =~ /UTF-?8/i) { | |
06b6e87b | 720 | _my_fail("Verify $locale with UTF-8 in name is a UTF-8 locale"); |
31f05a37 KW |
721 | } |
722 | } | |
723 | else { | |
724 | $ret = 1; | |
725 | } | |
726 | ||
727 | die "Couldn't restore locale '$save_locale'" | |
89458b6a | 728 | unless setlocale(&POSIX::LC_CTYPE(), $save_locale); |
31f05a37 KW |
729 | |
730 | return $ret; | |
ab8b8bcc KW |
731 | } |
732 | ||
8ae4238d | 733 | sub classify_locales_wrt_utf8ness($) { |
cfe9fa91 | 734 | |
8ae4238d KW |
735 | # Takes the input list of locales, and returns two lists split apart from |
736 | # it: the UTF-8 ones, and the non-UTF-8 ones. | |
21732d5c | 737 | |
8ae4238d KW |
738 | my $locales_ref = shift; |
739 | my (@utf8, @non_utf8); | |
21732d5c | 740 | |
8ae4238d KW |
741 | if (! locales_enabled('LC_CTYPE')) { # No CTYPE implies all are non-UTF-8 |
742 | @non_utf8 = $locales_ref->@*; | |
743 | return ( \@utf8, \@non_utf8 ); | |
ab8b8bcc KW |
744 | } |
745 | ||
746 | foreach my $locale (@$locales_ref) { | |
8ae4238d KW |
747 | my $which = (is_locale_utf8($locale)) ? \@utf8 : \@non_utf8; |
748 | push $which->@*, $locale; | |
ab8b8bcc KW |
749 | } |
750 | ||
8ae4238d | 751 | return ( \@utf8, \@non_utf8 ); |
e0bfe19f KW |
752 | } |
753 | ||
8ae4238d KW |
754 | sub find_utf8_ctype_locales (;$) { |
755 | ||
756 | # Return the names of the locales that core Perl thinks are UTF-8 LC_CTYPE | |
757 | # locales. Optional parameter is a reference to a list of locales to try; | |
758 | # if omitted, this tries all locales it can find on the platform | |
759 | ||
760 | return unless locales_enabled('LC_CTYPE'); | |
761 | ||
762 | my $locales_ref = shift; | |
763 | if (! defined $locales_ref) { | |
764 | ||
765 | my @locales = find_locales(&POSIX::LC_CTYPE()); | |
766 | $locales_ref = \@locales; | |
767 | } | |
768 | ||
769 | my ($utf8_ref, undef) = classify_locales_wrt_utf8ness($locales_ref); | |
770 | return unless $utf8_ref; | |
771 | return $utf8_ref->@*; | |
772 | } | |
e0bfe19f KW |
773 | |
774 | sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl | |
b5954e7e | 775 | # thinks is a UTF-8 LC_CTYPE non-turkic |
e0bfe19f KW |
776 | # locale. |
777 | # Optional parameter is a reference to a | |
778 | # list of locales to try; if omitted, this | |
779 | # tries all locales it can find on the | |
780 | # platform | |
781 | my $try_locales_ref = shift; | |
782 | ||
783 | my @utf8_locales = find_utf8_ctype_locales($try_locales_ref); | |
b5954e7e | 784 | my @turkic_locales = find_utf8_turkic_locales($try_locales_ref); |
e0bfe19f | 785 | |
b5954e7e KW |
786 | my %seen_turkic; |
787 | ||
788 | # Create undef elements in the hash for turkic locales | |
789 | @seen_turkic{@turkic_locales} = (); | |
790 | ||
791 | foreach my $locale (@utf8_locales) { | |
792 | return $locale unless exists $seen_turkic{$locale}; | |
793 | } | |
e0bfe19f | 794 | |
ab8b8bcc KW |
795 | return; |
796 | } | |
797 | ||
b5954e7e KW |
798 | sub find_utf8_turkic_locales (;$) { |
799 | ||
800 | # Return the name of all the locales that core Perl thinks are UTF-8 | |
801 | # Turkic LC_CTYPE. Optional parameter is a reference to a list of locales | |
802 | # to try; if omitted, this tries all locales it can find on the platform | |
803 | ||
804 | my @return; | |
805 | ||
e41845ea KW |
806 | return unless locales_enabled('LC_CTYPE'); |
807 | ||
b5954e7e KW |
808 | my $save_locale = setlocale(&POSIX::LC_CTYPE()); |
809 | foreach my $locale (find_utf8_ctype_locales(shift)) { | |
810 | use locale; | |
811 | setlocale(&POSIX::LC_CTYPE(), $locale); | |
812 | push @return, $locale if uc('i') eq "\x{130}"; | |
813 | } | |
eb1c747c KW |
814 | |
815 | die "Couldn't restore locale '$save_locale'" | |
816 | unless setlocale(&POSIX::LC_CTYPE(), $save_locale); | |
b5954e7e KW |
817 | |
818 | return @return; | |
819 | } | |
820 | ||
821 | sub find_utf8_turkic_locale (;$) { | |
822 | my @turkics = find_utf8_turkic_locales(shift); | |
823 | ||
824 | return unless @turkics; | |
825 | return $turkics[0] | |
826 | } | |
827 | ||
828 | ||
9969000e TK |
829 | # returns full path to the directory containing the current source |
830 | # file, inspired by mauke's Dir::Self | |
831 | sub _source_location { | |
832 | require File::Spec; | |
833 | ||
834 | my $caller_filename = (caller)[1]; | |
835 | ||
b3c872eb | 836 | my $loc = File::Spec->rel2abs( |
9969000e | 837 | File::Spec->catpath( |
b3c872eb | 838 | (File::Spec->splitpath($caller_filename))[0, 1], '' |
9969000e TK |
839 | ) |
840 | ); | |
b3c872eb TK |
841 | |
842 | return ($loc =~ /^(.*)$/)[0]; # untaint | |
9969000e TK |
843 | } |
844 | ||
73fc293b KW |
845 | 1 |
846 | ||
847 | # Format of data is: locale_name, language_codes, country_codes, encodings | |
848 | __DATA__ | |
849 | Afrikaans:af:za:1 15 | |
850 | Arabic:ar:dz eg sa:6 arabic8 | |
851 | Brezhoneg Breton:br:fr:1 15 | |
852 | Bulgarski Bulgarian:bg:bg:5 | |
853 | Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC | |
854 | Hrvatski Croatian:hr:hr:2 | |
855 | Cymraeg Welsh:cy:cy:1 14 15 | |
856 | Czech:cs:cz:2 | |
857 | Dansk Danish:da:dk:1 15 | |
858 | Nederlands Dutch:nl:be nl:1 15 | |
859 | English American British:en:au ca gb ie nz us uk zw:1 15 cp850 | |
860 | Esperanto:eo:eo:3 | |
861 | Eesti Estonian:et:ee:4 6 13 | |
862 | Suomi Finnish:fi:fi:1 15 | |
863 | Flamish::fl:1 15 | |
864 | Deutsch German:de:at be ch de lu:1 15 | |
865 | Euskaraz Basque:eu:es fr:1 15 | |
866 | Galego Galician:gl:es:1 15 | |
867 | Ellada Greek:el:gr:7 g8 | |
868 | Frysk:fy:nl:1 15 | |
869 | Greenlandic:kl:gl:4 6 | |
870 | Hebrew:iw:il:8 hebrew8 | |
871 | Hungarian:hu:hu:2 | |
872 | Indonesian:id:id:1 15 | |
873 | Gaeilge Irish:ga:IE:1 14 15 | |
874 | Italiano Italian:it:ch it:1 15 | |
875 | Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis | |
876 | Korean:ko:kr: | |
877 | Latine Latin:la:va:1 15 | |
878 | Latvian:lv:lv:4 6 13 | |
879 | Lithuanian:lt:lt:4 6 13 | |
880 | Macedonian:mk:mk:1 15 | |
881 | Maltese:mt:mt:3 | |
882 | Moldovan:mo:mo:2 | |
883 | Norsk Norwegian:no no\@nynorsk nb nn:no:1 15 | |
884 | Occitan:oc:es:1 15 | |
885 | Polski Polish:pl:pl:2 | |
886 | Rumanian:ro:ro:2 | |
887 | Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866 | |
888 | Serbski Serbian:sr:yu:5 | |
889 | Slovak:sk:sk:2 | |
890 | Slovene Slovenian:sl:si:2 | |
891 | Sqhip Albanian:sq:sq:1 15 | |
892 | Svenska Swedish:sv:fi se:1 15 | |
893 | Thai:th:th:11 tis620 | |
894 | Turkish:tr:tr:9 turkish8 | |
895 | Yiddish:yi::1 15 |