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