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