This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/loc_tools.pl: Complement sense of optional parameter
[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
2c6c88ec 22 # to a list of categories The 4th parameter is true if to accept locales
bb31ec6a
KW
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;
2c6c88ec 28 my $allow_incompatible = shift;
f079f9b7 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);
2c6c88ec 49 return if ! $plays_well && ! $allow_incompatible;
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
2c6c88ec
KW
189 # non-zero, the list includes all found locales;
190 # otherwise it is restricted to those locales
191 # that play well with Perl, as far as we can
192 # easily determine.
f079f9b7
KW
193 # The first parameter is either a single locale
194 # category or a reference to a list of categories to
195 # find valid locales for it (or in the case of
196 # multiple) for all of them.
9b0711ee 197 my $categories = shift;
2c6c88ec 198 my $allow_incompatible = shift // 0;
73fc293b 199
128e4113 200 return unless locales_enabled($categories);
d369fd5b
KW
201
202 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
203 # and mingw32 uses said silly CRT
204 # This doesn't seem to be an issue any more, at least on Windows XP,
205 # so re-enable the tests for Windows XP onwards.
206 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
207 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
128e4113
KW
208 return if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare')
209 && $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i);
d369fd5b
KW
210
211 # UWIN seems to loop after taint tests, just skip for now
128e4113 212 return if ($^O =~ /^uwin/);
d369fd5b 213
2c6c88ec
KW
214 _trylocale("C", $categories, \@Locale, $allow_incompatible);
215 _trylocale("POSIX", $categories, \@Locale, $allow_incompatible);
73fc293b 216 foreach (0..15) {
2c6c88ec
KW
217 _trylocale("ISO8859-$_", $categories, \@Locale, $allow_incompatible);
218 _trylocale("iso8859$_", $categories, \@Locale, $allow_incompatible);
219 _trylocale("iso8859-$_", $categories, \@Locale, $allow_incompatible);
220 _trylocale("iso_8859_$_", $categories, \@Locale, $allow_incompatible);
221 _trylocale("isolatin$_", $categories, \@Locale, $allow_incompatible);
222 _trylocale("isolatin-$_", $categories, \@Locale, $allow_incompatible);
223 _trylocale("iso_latin_$_", $categories, \@Locale, $allow_incompatible);
73fc293b
KW
224 }
225
226 # Sanitize the environment so that we can run the external 'locale'
227 # program without the taint mode getting grumpy.
228
229 # $ENV{PATH} is special in VMS.
8b76dc1d 230 delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
73fc293b
KW
231
232 # Other subversive stuff.
8b76dc1d 233 delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)};
73fc293b
KW
234
235 if (-x "/usr/bin/locale"
236 && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|"))
237 {
238 while (<LOCALES>) {
239 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
240 # ain't great when we're running this testPERL_UNICODE= so that utf8
241 # locales will cause all IO hadles to default to (assume) utf8
242 next unless utf8::valid($_);
243 chomp;
2c6c88ec 244 _trylocale($_, $categories, \@Locale, $allow_incompatible);
73fc293b
KW
245 }
246 close(LOCALES);
247 } elsif ($^O eq 'VMS'
248 && defined($ENV{'SYS$I18N_LOCALE'})
249 && -d 'SYS$I18N_LOCALE')
250 {
251 # The SYS$I18N_LOCALE logical name search list was not present on
252 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
253 opendir(LOCALES, "SYS\$I18N_LOCALE:");
254 while ($_ = readdir(LOCALES)) {
255 chomp;
2c6c88ec 256 _trylocale($_, $categories, \@Locale, $allow_incompatible);
73fc293b
KW
257 }
258 close(LOCALES);
259 } elsif (($^O eq 'openbsd' || $^O eq 'bitrig' ) && -e '/usr/share/locale') {
260
7e1e0d99
KW
261 # OpenBSD doesn't have a locale executable, so reading
262 # /usr/share/locale is much easier and faster than the last resort
263 # method.
73fc293b
KW
264
265 opendir(LOCALES, '/usr/share/locale');
266 while ($_ = readdir(LOCALES)) {
267 chomp;
2c6c88ec 268 _trylocale($_, $categories, \@Locale, $allow_incompatible);
73fc293b
KW
269 }
270 close(LOCALES);
271 } else { # Final fallback. Try our list of locales hard-coded here
272
273 # This is going to be slow.
274 my @Data;
275
73fc293b
KW
276 # Locales whose name differs if the utf8 bit is on are stored in these two
277 # files with appropriate encodings.
278 if ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/) {
279 @Data = do "lib/locale/utf8";
280 } else {
281 @Data = do "lib/locale/latin1";
282 }
283
284 # The rest of the locales are in this file.
285 push @Data, <DATA>;
286
287 foreach my $line (@Data) {
288 my ($locale_name, $language_codes, $country_codes, $encodings) =
289 split /:/, $line;
290 my @enc = _decode_encodings($encodings);
291 foreach my $loc (split(/ /, $locale_name)) {
2c6c88ec 292 _trylocale($loc, $categories, \@Locale, $allow_incompatible);
73fc293b 293 foreach my $enc (@enc) {
f079f9b7 294 _trylocale("$loc.$enc", $categories, \@Locale,
2c6c88ec 295 $allow_incompatible);
73fc293b
KW
296 }
297 $loc = lc $loc;
298 foreach my $enc (@enc) {
f079f9b7 299 _trylocale("$loc.$enc", $categories, \@Locale,
2c6c88ec 300 $allow_incompatible);
73fc293b
KW
301 }
302 }
303 foreach my $lang (split(/ /, $language_codes)) {
2c6c88ec 304 _trylocale($lang, $categories, \@Locale, $allow_incompatible);
73fc293b
KW
305 foreach my $country (split(/ /, $country_codes)) {
306 my $lc = "${lang}_${country}";
2c6c88ec 307 _trylocale($lc, $categories, \@Locale, $allow_incompatible);
73fc293b 308 foreach my $enc (@enc) {
f079f9b7 309 _trylocale("$lc.$enc", $categories, \@Locale,
2c6c88ec 310 $allow_incompatible);
73fc293b
KW
311 }
312 my $lC = "${lang}_\U${country}";
2c6c88ec 313 _trylocale($lC, $categories, \@Locale, $allow_incompatible);
73fc293b 314 foreach my $enc (@enc) {
f079f9b7 315 _trylocale("$lC.$enc", $categories, \@Locale,
2c6c88ec 316 $allow_incompatible);
73fc293b
KW
317 }
318 }
319 }
320 }
321 }
322
323 @Locale = sort @Locale;
324
325 return @Locale;
73fc293b
KW
326}
327
ab8b8bcc 328sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input
9b0711ee 329 # is a UTF-8 locale
92c0a900
KW
330
331 # On z/OS, even locales marked as UTF-8 aren't.
332 return 0 if ord "A" != 65;
333
cfe9fa91 334 return 0 unless locales_enabled('LC_CTYPE');
21732d5c 335
ab8b8bcc
KW
336 my $locale = shift;
337
31f05a37 338 use locale;
f079f9b7 339 no warnings 'locale'; # We may be trying out a weird locale
31f05a37
KW
340
341 my $save_locale = setlocale(&POSIX::LC_CTYPE());
342 if (! $save_locale) {
343 ok(0, "Verify could save previous locale");
344 return 0;
345 }
346
347 if (! setlocale(&POSIX::LC_CTYPE(), $locale)) {
348 ok(0, "Verify could setlocale to $locale");
349 return 0;
350 }
351
352 my $ret = 0;
353
354 # Use an op that gives different results for UTF-8 than any other locale.
355 # If a platform has UTF-8 locales, there should be at least one locale on
356 # most platforms with UTF-8 in its name, so if there is a bug in the op
357 # giving a false negative, we should get a failure for those locales as we
358 # go through testing all the locales on the platform.
359 if (CORE::fc(chr utf8::unicode_to_native(0xdf)) ne "ss") {
360 if ($locale =~ /UTF-?8/i) {
361 ok (0, "Verify $locale with UTF-8 in name is a UTF-8 locale");
362 }
363 }
364 else {
365 $ret = 1;
366 }
367
368 die "Couldn't restore locale '$save_locale'"
369 unless setlocale(&POSIX::LC_CTYPE(), $save_locale);
370
371 return $ret;
ab8b8bcc
KW
372}
373
c1d1e689 374sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl
9b0711ee
KW
375 # thinks is a UTF-8 LC_CTYPE locale.
376 # Optional parameter is a reference to a
377 # list of locales to try; if omitted, this
378 # tries all locales it can find on the
379 # platform
cfe9fa91
KW
380 return unless locales_enabled('LC_CTYPE');
381
ab8b8bcc 382 my $locales_ref = shift;
21732d5c 383
ab8b8bcc 384 if (! defined $locales_ref) {
21732d5c 385
2c6c88ec 386 my @locales = find_locales(&POSIX::LC_CTYPE());
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