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