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