Commit | Line | Data |
---|---|---|
73fc293b | 1 | # Common tools for test files files to find the locales which exist on the |
abfe20b2 KW |
2 | # system. Caller should have defined ok() for the unlikely event that setup |
3 | # here fails, and should have verified that this isn't miniperl before calling | |
4 | # the functions. | |
73fc293b KW |
5 | |
6 | # Note that it's okay that some languages have their native names | |
7 | # capitalized here even though that's not "right". They are lowercased | |
8 | # anyway later during the scanning process (and besides, some clueless | |
9 | # vendor might have them capitalized erroneously anyway). | |
10 | ||
bb31ec6a KW |
11 | # Functions whose names begin with underscore are internal helper functions |
12 | # for this file, and are not to be used by outside callers. | |
13 | ||
a274778c | 14 | eval { require POSIX; import POSIX 'locale_h'; }; |
cfe9fa91 | 15 | $has_locale_h = ! $@; |
a274778c | 16 | |
bb31ec6a KW |
17 | sub _trylocale ($$$$) { # For use only by other functions in this file! |
18 | ||
19 | # Adds the locale given by the first parameter to the list given by the | |
20 | # 3rd iff the platform supports the locale in each of the categories given | |
21 | # by the 2nd parameter, which is either a single category or a reference | |
22 | # to a list of categories The 4th parameter is true if to reject locales | |
23 | # that aren't apparently fully compatible with Perl. | |
24 | ||
73fc293b | 25 | my $locale = shift; |
9b0711ee | 26 | my $categories = shift; |
73fc293b | 27 | my $list = shift; |
f079f9b7 KW |
28 | my $only_plays_well = shift; |
29 | ||
162eb2b8 | 30 | return if ! $locale || grep { $locale eq $_ } @$list; |
9b0711ee KW |
31 | |
32 | $categories = [ $categories ] unless ref $categories; | |
33 | ||
f079f9b7 KW |
34 | my $badutf8 = 0; |
35 | my $plays_well = 1; | |
36 | ||
37 | use warnings 'locale'; | |
38 | ||
39 | local $SIG{__WARN__} = sub { | |
40 | $badutf8 = 1 if $_[0] =~ /Malformed UTF-8/; | |
41 | $plays_well = 0 if $_[0] =~ /Locale .* may not work well/i | |
42 | }; | |
43 | ||
9b0711ee KW |
44 | foreach my $category (@$categories) { |
45 | return unless setlocale($category, $locale); | |
f079f9b7 | 46 | return if $only_plays_well && ! $plays_well; |
73fc293b KW |
47 | } |
48 | ||
49 | if ($badutf8) { | |
50 | ok(0, "Verify locale name doesn't contain malformed utf8"); | |
51 | return; | |
52 | } | |
53 | push @$list, $locale; | |
54 | } | |
55 | ||
bb31ec6a | 56 | sub _decode_encodings { # For use only by other functions in this file! |
73fc293b KW |
57 | my @enc; |
58 | ||
59 | foreach (split(/ /, shift)) { | |
60 | if (/^(\d+)$/) { | |
61 | push @enc, "ISO8859-$1"; | |
62 | push @enc, "iso8859$1"; # HP | |
63 | if ($1 eq '1') { | |
64 | push @enc, "roman8"; # HP | |
65 | } | |
66 | push @enc, $_; | |
67 | push @enc, "$_.UTF-8"; | |
d646bffe KW |
68 | push @enc, "$_.65001"; # Windows UTF-8 |
69 | push @enc, "$_.ACP"; # Windows ANSI code page | |
70 | push @enc, "$_.OCP"; # Windows OEM code page | |
cf34c81f | 71 | push @enc, "$_.1252"; # Windows |
73fc293b KW |
72 | } |
73 | } | |
74 | if ($^O eq 'os390') { | |
75 | push @enc, qw(IBM-037 IBM-819 IBM-1047); | |
76 | } | |
77 | push @enc, "UTF-8"; | |
d646bffe | 78 | push @enc, "65001"; # Windows UTF-8 |
73fc293b KW |
79 | |
80 | return @enc; | |
81 | } | |
82 | ||
308482c2 KW |
83 | # LC_ALL can be -1 on some platforms. And, in fact the implementors could |
84 | # legally use any integer to represent any category. But it makes the most | |
85 | # sense for them to have used small integers. Below, we create new locale | |
86 | # numbers for ones missing from this machine. We make them very negative, | |
87 | # hopefully more negative than anything likely to be a valid category on the | |
88 | # platform, but also below is a check to be sure that our guess is valid. | |
89 | my $max_bad_category_number = -1000000; | |
90 | ||
128e4113 KW |
91 | # Initialize this hash so that it looks like e.g., |
92 | # 6 => 'CTYPE', | |
93 | # where 6 is the value of &POSIX::LC_CTYPE | |
94 | my %category_name; | |
128e4113 | 95 | unless ($@) { |
308482c2 | 96 | my $number_for_missing_category = $max_bad_category_number; |
128e4113 KW |
97 | foreach my $name (qw(ALL COLLATE CTYPE MESSAGES MONETARY NUMERIC TIME)) { |
98 | my $number = eval "&POSIX::LC_$name"; | |
2cc6a9db | 99 | |
308482c2 KW |
100 | if ($@) { |
101 | # Use a negative number (smaller than any legitimate category | |
102 | # number) if the platform doesn't support this category, so we | |
103 | # have an entry for all the ones that might be specified in calls | |
104 | # to us. | |
105 | $number = $number_for_missing_category-- if $@; | |
106 | } | |
107 | elsif ( $number !~ / ^ -? \d+ $ /x | |
108 | || $number <= $max_bad_category_number) | |
109 | { | |
110 | # We think this should be an int. And it has to be larger than | |
111 | # any of our synthetic numbers. | |
112 | die "Unexpected locale category number '$number' for LC_$name" | |
113 | } | |
2cc6a9db | 114 | |
128e4113 KW |
115 | $category_name{$number} = "$name"; |
116 | } | |
117 | } | |
118 | ||
119 | sub locales_enabled(;$) { | |
120 | # Returns 0 if no locale handling is available on this platform; otherwise | |
121 | # 1. | |
122 | # | |
123 | # The optional parameter is a reference to a list of individual POSIX | |
124 | # locale categories. If present, this function also returns 0 if any of | |
125 | # them are individually not available on this platform; otherwise 1. | |
126 | # Actually, it is acceptable for the list to be just a simple scalar | |
127 | # denoting a single category. | |
128 | # | |
129 | # If any of the individual categories specified by the optional parameter | |
095b9fe6 KW |
130 | # is all digits (and an optional leading minus), it is taken to be the C |
131 | # enum for the category (e.g., &POSIX::LC_CTYPE). Otherwise it should be | |
132 | # a string name of the category, like 'LC_TIME'. The initial 'LC_' is | |
133 | # optional. It is a fatal error to call this with something that isn't a | |
134 | # known category | |
128e4113 | 135 | |
a274778c | 136 | use Config; |
128e4113 KW |
137 | |
138 | return 0 unless $Config{d_setlocale} | |
139 | # I (khw) cargo-culted the '?' in the pattern on the | |
140 | # next line. | |
a274778c | 141 | && $Config{ccflags} !~ /\bD?NO_LOCALE\b/ |
cfe9fa91 | 142 | && $has_locale_h; |
128e4113 KW |
143 | |
144 | # Done with the global possibilities. Now check if any passed in category | |
145 | # is disabled. | |
146 | my $categories_ref = shift; | |
e6965c14 KW |
147 | if (defined $categories_ref) { |
148 | $categories_ref = [ $categories_ref ] if ! ref $categories_ref; | |
149 | my @local_categories_copy = @$categories_ref; | |
308482c2 KW |
150 | for my $category_name_or_number (@local_categories_copy) { |
151 | my $name; | |
152 | my $number; | |
153 | if ($category_name_or_number =~ / ^ -? \d+ $ /x) { | |
154 | $number = $category_name_or_number; | |
155 | die "Invalid locale category number '$number'" | |
156 | unless grep { $number == $_ } keys %category_name; | |
157 | $name = $category_name{$number}; | |
e6965c14 KW |
158 | } |
159 | else { | |
308482c2 KW |
160 | $name = $category_name_or_number; |
161 | $name =~ s/ ^ LC_ //x; | |
162 | foreach my $trial (keys %category_name) { | |
163 | if ($category_name{$trial} eq $name) { | |
164 | $number = $trial; | |
165 | last; | |
166 | } | |
167 | } | |
168 | die "Invalid locale category name '$name'" | |
169 | unless defined $number; | |
e6965c14 KW |
170 | } |
171 | ||
308482c2 KW |
172 | return 0 if $number <= $max_bad_category_number |
173 | || $Config{ccflags} =~ /\bD?NO_LOCALE_$name\b/; | |
cfe9fa91 KW |
174 | |
175 | eval "defined &POSIX::LC_$name"; | |
176 | return 0 if $@; | |
128e4113 | 177 | } |
128e4113 KW |
178 | } |
179 | ||
180 | return 1; | |
181 | } | |
182 | ||
183 | ||
f079f9b7 KW |
184 | sub find_locales ($;$) { # Returns an array of all the locales we found on the |
185 | # system. If the optional 2nd parameter is | |
186 | # non-zero, the list is restricted to those locales | |
187 | # that play well with Perl. | |
188 | # The first parameter is either a single locale | |
189 | # category or a reference to a list of categories to | |
190 | # find valid locales for it (or in the case of | |
191 | # multiple) for all of them. | |
9b0711ee | 192 | my $categories = shift; |
f079f9b7 | 193 | my $only_plays_well = shift // 0; |
73fc293b | 194 | |
128e4113 | 195 | return unless locales_enabled($categories); |
d369fd5b KW |
196 | |
197 | # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1" | |
198 | # and mingw32 uses said silly CRT | |
199 | # This doesn't seem to be an issue any more, at least on Windows XP, | |
200 | # so re-enable the tests for Windows XP onwards. | |
201 | my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion && | |
202 | join('.', (Win32::GetOSVersion())[1..2]) >= 5.1); | |
128e4113 KW |
203 | return if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') |
204 | && $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i); | |
d369fd5b KW |
205 | |
206 | # UWIN seems to loop after taint tests, just skip for now | |
128e4113 | 207 | return if ($^O =~ /^uwin/); |
d369fd5b | 208 | |
f079f9b7 KW |
209 | _trylocale("C", $categories, \@Locale, $only_plays_well); |
210 | _trylocale("POSIX", $categories, \@Locale, $only_plays_well); | |
73fc293b | 211 | foreach (0..15) { |
f079f9b7 KW |
212 | _trylocale("ISO8859-$_", $categories, \@Locale, $only_plays_well); |
213 | _trylocale("iso8859$_", $categories, \@Locale, $only_plays_well); | |
214 | _trylocale("iso8859-$_", $categories, \@Locale, $only_plays_well); | |
215 | _trylocale("iso_8859_$_", $categories, \@Locale, $only_plays_well); | |
216 | _trylocale("isolatin$_", $categories, \@Locale, $only_plays_well); | |
217 | _trylocale("isolatin-$_", $categories, \@Locale, $only_plays_well); | |
218 | _trylocale("iso_latin_$_", $categories, \@Locale, $only_plays_well); | |
73fc293b KW |
219 | } |
220 | ||
221 | # Sanitize the environment so that we can run the external 'locale' | |
222 | # program without the taint mode getting grumpy. | |
223 | ||
224 | # $ENV{PATH} is special in VMS. | |
8b76dc1d | 225 | delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv}; |
73fc293b KW |
226 | |
227 | # Other subversive stuff. | |
8b76dc1d | 228 | delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)}; |
73fc293b KW |
229 | |
230 | if (-x "/usr/bin/locale" | |
231 | && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) | |
232 | { | |
233 | while (<LOCALES>) { | |
234 | # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which | |
235 | # ain't great when we're running this testPERL_UNICODE= so that utf8 | |
236 | # locales will cause all IO hadles to default to (assume) utf8 | |
237 | next unless utf8::valid($_); | |
238 | chomp; | |
f079f9b7 | 239 | _trylocale($_, $categories, \@Locale, $only_plays_well); |
73fc293b KW |
240 | } |
241 | close(LOCALES); | |
242 | } elsif ($^O eq 'VMS' | |
243 | && defined($ENV{'SYS$I18N_LOCALE'}) | |
244 | && -d 'SYS$I18N_LOCALE') | |
245 | { | |
246 | # The SYS$I18N_LOCALE logical name search list was not present on | |
247 | # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions. | |
248 | opendir(LOCALES, "SYS\$I18N_LOCALE:"); | |
249 | while ($_ = readdir(LOCALES)) { | |
250 | chomp; | |
f079f9b7 | 251 | _trylocale($_, $categories, \@Locale, $only_plays_well); |
73fc293b KW |
252 | } |
253 | close(LOCALES); | |
254 | } elsif (($^O eq 'openbsd' || $^O eq 'bitrig' ) && -e '/usr/share/locale') { | |
255 | ||
7e1e0d99 KW |
256 | # OpenBSD doesn't have a locale executable, so reading |
257 | # /usr/share/locale is much easier and faster than the last resort | |
258 | # method. | |
73fc293b KW |
259 | |
260 | opendir(LOCALES, '/usr/share/locale'); | |
261 | while ($_ = readdir(LOCALES)) { | |
262 | chomp; | |
f079f9b7 | 263 | _trylocale($_, $categories, \@Locale, $only_plays_well); |
73fc293b KW |
264 | } |
265 | close(LOCALES); | |
266 | } else { # Final fallback. Try our list of locales hard-coded here | |
267 | ||
268 | # This is going to be slow. | |
269 | my @Data; | |
270 | ||
73fc293b KW |
271 | # Locales whose name differs if the utf8 bit is on are stored in these two |
272 | # files with appropriate encodings. | |
273 | if ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/) { | |
274 | @Data = do "lib/locale/utf8"; | |
275 | } else { | |
276 | @Data = do "lib/locale/latin1"; | |
277 | } | |
278 | ||
279 | # The rest of the locales are in this file. | |
280 | push @Data, <DATA>; | |
281 | ||
282 | foreach my $line (@Data) { | |
283 | my ($locale_name, $language_codes, $country_codes, $encodings) = | |
284 | split /:/, $line; | |
285 | my @enc = _decode_encodings($encodings); | |
286 | foreach my $loc (split(/ /, $locale_name)) { | |
f079f9b7 | 287 | _trylocale($loc, $categories, \@Locale, $only_plays_well); |
73fc293b | 288 | foreach my $enc (@enc) { |
f079f9b7 KW |
289 | _trylocale("$loc.$enc", $categories, \@Locale, |
290 | $only_plays_well); | |
73fc293b KW |
291 | } |
292 | $loc = lc $loc; | |
293 | foreach my $enc (@enc) { | |
f079f9b7 KW |
294 | _trylocale("$loc.$enc", $categories, \@Locale, |
295 | $only_plays_well); | |
73fc293b KW |
296 | } |
297 | } | |
298 | foreach my $lang (split(/ /, $language_codes)) { | |
f079f9b7 | 299 | _trylocale($lang, $categories, \@Locale, $only_plays_well); |
73fc293b KW |
300 | foreach my $country (split(/ /, $country_codes)) { |
301 | my $lc = "${lang}_${country}"; | |
f079f9b7 | 302 | _trylocale($lc, $categories, \@Locale, $only_plays_well); |
73fc293b | 303 | foreach my $enc (@enc) { |
f079f9b7 KW |
304 | _trylocale("$lc.$enc", $categories, \@Locale, |
305 | $only_plays_well); | |
73fc293b KW |
306 | } |
307 | my $lC = "${lang}_\U${country}"; | |
f079f9b7 | 308 | _trylocale($lC, $categories, \@Locale, $only_plays_well); |
73fc293b | 309 | foreach my $enc (@enc) { |
f079f9b7 KW |
310 | _trylocale("$lC.$enc", $categories, \@Locale, |
311 | $only_plays_well); | |
73fc293b KW |
312 | } |
313 | } | |
314 | } | |
315 | } | |
316 | } | |
317 | ||
318 | @Locale = sort @Locale; | |
319 | ||
320 | return @Locale; | |
73fc293b KW |
321 | } |
322 | ||
ab8b8bcc | 323 | sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input |
9b0711ee | 324 | # is a UTF-8 locale |
92c0a900 KW |
325 | |
326 | # On z/OS, even locales marked as UTF-8 aren't. | |
327 | return 0 if ord "A" != 65; | |
328 | ||
cfe9fa91 | 329 | return 0 unless locales_enabled('LC_CTYPE'); |
21732d5c | 330 | |
ab8b8bcc KW |
331 | my $locale = shift; |
332 | ||
31f05a37 | 333 | use locale; |
f079f9b7 | 334 | no warnings 'locale'; # We may be trying out a weird locale |
31f05a37 KW |
335 | |
336 | my $save_locale = setlocale(&POSIX::LC_CTYPE()); | |
337 | if (! $save_locale) { | |
338 | ok(0, "Verify could save previous locale"); | |
339 | return 0; | |
340 | } | |
341 | ||
342 | if (! setlocale(&POSIX::LC_CTYPE(), $locale)) { | |
343 | ok(0, "Verify could setlocale to $locale"); | |
344 | return 0; | |
345 | } | |
346 | ||
347 | my $ret = 0; | |
348 | ||
349 | # Use an op that gives different results for UTF-8 than any other locale. | |
350 | # If a platform has UTF-8 locales, there should be at least one locale on | |
351 | # most platforms with UTF-8 in its name, so if there is a bug in the op | |
352 | # giving a false negative, we should get a failure for those locales as we | |
353 | # go through testing all the locales on the platform. | |
354 | if (CORE::fc(chr utf8::unicode_to_native(0xdf)) ne "ss") { | |
355 | if ($locale =~ /UTF-?8/i) { | |
356 | ok (0, "Verify $locale with UTF-8 in name is a UTF-8 locale"); | |
357 | } | |
358 | } | |
359 | else { | |
360 | $ret = 1; | |
361 | } | |
362 | ||
363 | die "Couldn't restore locale '$save_locale'" | |
364 | unless setlocale(&POSIX::LC_CTYPE(), $save_locale); | |
365 | ||
366 | return $ret; | |
ab8b8bcc KW |
367 | } |
368 | ||
c1d1e689 | 369 | sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl |
9b0711ee KW |
370 | # thinks is a UTF-8 LC_CTYPE locale. |
371 | # Optional parameter is a reference to a | |
372 | # list of locales to try; if omitted, this | |
373 | # tries all locales it can find on the | |
374 | # platform | |
cfe9fa91 KW |
375 | return unless locales_enabled('LC_CTYPE'); |
376 | ||
ab8b8bcc | 377 | my $locales_ref = shift; |
21732d5c | 378 | |
ab8b8bcc | 379 | if (! defined $locales_ref) { |
21732d5c | 380 | |
f079f9b7 KW |
381 | my @locales = find_locales(&POSIX::LC_CTYPE(), |
382 | 1 # Reject iffy locales. | |
383 | ); | |
ab8b8bcc KW |
384 | $locales_ref = \@locales; |
385 | } | |
386 | ||
387 | foreach my $locale (@$locales_ref) { | |
388 | return $locale if is_locale_utf8($locale); | |
389 | } | |
390 | ||
391 | return; | |
392 | } | |
393 | ||
73fc293b KW |
394 | 1 |
395 | ||
396 | # Format of data is: locale_name, language_codes, country_codes, encodings | |
397 | __DATA__ | |
398 | Afrikaans:af:za:1 15 | |
399 | Arabic:ar:dz eg sa:6 arabic8 | |
400 | Brezhoneg Breton:br:fr:1 15 | |
401 | Bulgarski Bulgarian:bg:bg:5 | |
402 | Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC | |
403 | Hrvatski Croatian:hr:hr:2 | |
404 | Cymraeg Welsh:cy:cy:1 14 15 | |
405 | Czech:cs:cz:2 | |
406 | Dansk Danish:da:dk:1 15 | |
407 | Nederlands Dutch:nl:be nl:1 15 | |
408 | English American British:en:au ca gb ie nz us uk zw:1 15 cp850 | |
409 | Esperanto:eo:eo:3 | |
410 | Eesti Estonian:et:ee:4 6 13 | |
411 | Suomi Finnish:fi:fi:1 15 | |
412 | Flamish::fl:1 15 | |
413 | Deutsch German:de:at be ch de lu:1 15 | |
414 | Euskaraz Basque:eu:es fr:1 15 | |
415 | Galego Galician:gl:es:1 15 | |
416 | Ellada Greek:el:gr:7 g8 | |
417 | Frysk:fy:nl:1 15 | |
418 | Greenlandic:kl:gl:4 6 | |
419 | Hebrew:iw:il:8 hebrew8 | |
420 | Hungarian:hu:hu:2 | |
421 | Indonesian:id:id:1 15 | |
422 | Gaeilge Irish:ga:IE:1 14 15 | |
423 | Italiano Italian:it:ch it:1 15 | |
424 | Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis | |
425 | Korean:ko:kr: | |
426 | Latine Latin:la:va:1 15 | |
427 | Latvian:lv:lv:4 6 13 | |
428 | Lithuanian:lt:lt:4 6 13 | |
429 | Macedonian:mk:mk:1 15 | |
430 | Maltese:mt:mt:3 | |
431 | Moldovan:mo:mo:2 | |
432 | Norsk Norwegian:no no\@nynorsk nb nn:no:1 15 | |
433 | Occitan:oc:es:1 15 | |
434 | Polski Polish:pl:pl:2 | |
435 | Rumanian:ro:ro:2 | |
436 | Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866 | |
437 | Serbski Serbian:sr:yu:5 | |
438 | Slovak:sk:sk:2 | |
439 | Slovene Slovenian:sl:si:2 | |
440 | Sqhip Albanian:sq:sq:1 15 | |
441 | Svenska Swedish:sv:fi se:1 15 | |
442 | Thai:th:th:11 tis620 | |
443 | Turkish:tr:tr:9 turkish8 | |
444 | Yiddish:yi::1 15 |