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