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