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