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