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