This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Capitalise magic descriptions consistently
[perl5.git] / t / loc_tools.pl
1 # Common tools for test files files to find the locales which exist on the
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.
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
11 sub _trylocale {    # Adds the locale given by the first parameter to the list
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
16     my $locale = shift;
17     my $categories = shift;
18     my $list = shift;
19     return if ! $locale || grep { $locale eq $_ } @$list;
20
21     $categories = [ $categories ] unless ref $categories;
22
23     foreach my $category (@$categories) {
24         return unless setlocale($category, $locale);
25     }
26
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
41 sub _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";
53             push @enc, "$_.65001"; # Windows UTF-8
54             push @enc, "$_.ACP"; # Windows ANSI code page
55             push @enc, "$_.OCP"; # Windows OEM code page
56         }
57     }
58     if ($^O eq 'os390') {
59         push @enc, qw(IBM-037 IBM-819 IBM-1047);
60     }
61     push @enc, "UTF-8";
62     push @enc, "65001"; # Windows UTF-8
63
64     return @enc;
65 }
66
67 sub 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
70                         # find valid locales for it (or in the case of
71                         # multiple) for all of them
72     my $categories = shift;
73
74     use Config;;
75     my $have_setlocale = $Config{d_setlocale};
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
91     # Done this way in case this is 'required' in the caller before seeing if
92     # this is miniperl.
93     eval { require POSIX; import POSIX 'locale_h'; };
94     unless (defined &POSIX::LC_CTYPE) {
95       return;
96     }
97
98     _trylocale("C", $categories, \@Locale);
99     _trylocale("POSIX", $categories, \@Locale);
100     foreach (0..15) {
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);
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.
114     delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
115
116     # Other subversive stuff.
117     delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)};
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;
128             _trylocale($_, $categories, \@Locale);
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;
140             _trylocale($_, $categories, \@Locale);
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;
151             _trylocale($_, $categories, \@Locale);
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)) {
176                 _trylocale($loc, $categories, \@Locale);
177                 foreach my $enc (@enc) {
178                     _trylocale("$loc.$enc", $categories, \@Locale);
179                 }
180                 $loc = lc $loc;
181                 foreach my $enc (@enc) {
182                     _trylocale("$loc.$enc", $categories, \@Locale);
183                 }
184             }
185             foreach my $lang (split(/ /, $language_codes)) {
186                 _trylocale($lang, $categories, \@Locale);
187                 foreach my $country (split(/ /, $country_codes)) {
188                     my $lc = "${lang}_${country}";
189                     _trylocale($lc, $categories, \@Locale);
190                     foreach my $enc (@enc) {
191                         _trylocale("$lc.$enc", $categories, \@Locale);
192                     }
193                     my $lC = "${lang}_\U${country}";
194                     _trylocale($lC, $categories, \@Locale);
195                     foreach my $enc (@enc) {
196                         _trylocale("$lC.$enc", $categories, \@Locale);
197                     }
198                 }
199             }
200         }
201     }
202
203     @Locale = sort @Locale;
204
205     return @Locale;
206
207
208 }
209
210 sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input
211                          # is a UTF-8 locale
212     my $locale = shift;
213
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;
247 }
248
249 sub 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
255     my $locales_ref = shift;
256     return if !defined &POSIX::LC_CTYPE;
257     if (! defined $locales_ref) {
258         my @locales = find_locales(&POSIX::LC_CTYPE());
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
269 1
270
271 # Format of data is: locale_name, language_codes, country_codes, encodings
272 __DATA__
273 Afrikaans:af:za:1 15
274 Arabic:ar:dz eg sa:6 arabic8
275 Brezhoneg Breton:br:fr:1 15
276 Bulgarski Bulgarian:bg:bg:5
277 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
278 Hrvatski Croatian:hr:hr:2
279 Cymraeg Welsh:cy:cy:1 14 15
280 Czech:cs:cz:2
281 Dansk Danish:da:dk:1 15
282 Nederlands Dutch:nl:be nl:1 15
283 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
284 Esperanto:eo:eo:3
285 Eesti Estonian:et:ee:4 6 13
286 Suomi Finnish:fi:fi:1 15
287 Flamish::fl:1 15
288 Deutsch German:de:at be ch de lu:1 15
289 Euskaraz Basque:eu:es fr:1 15
290 Galego Galician:gl:es:1 15
291 Ellada Greek:el:gr:7 g8
292 Frysk:fy:nl:1 15
293 Greenlandic:kl:gl:4 6
294 Hebrew:iw:il:8 hebrew8
295 Hungarian:hu:hu:2
296 Indonesian:id:id:1 15
297 Gaeilge Irish:ga:IE:1 14 15
298 Italiano Italian:it:ch it:1 15
299 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
300 Korean:ko:kr:
301 Latine Latin:la:va:1 15
302 Latvian:lv:lv:4 6 13
303 Lithuanian:lt:lt:4 6 13
304 Macedonian:mk:mk:1 15
305 Maltese:mt:mt:3
306 Moldovan:mo:mo:2
307 Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
308 Occitan:oc:es:1 15
309 Polski Polish:pl:pl:2
310 Rumanian:ro:ro:2
311 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
312 Serbski Serbian:sr:yu:5
313 Slovak:sk:sk:2
314 Slovene Slovenian:sl:si:2
315 Sqhip Albanian:sq:sq:1 15
316 Svenska Swedish:sv:fi se:1 15
317 Thai:th:th:11 tis620
318 Turkish:tr:tr:9 turkish8
319 Yiddish:yi::1 15