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