| 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 | sub _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 | |
| 22 | my $locale = shift; |
| 23 | my $categories = shift; |
| 24 | my $list = shift; |
| 25 | my $only_plays_well = shift; |
| 26 | |
| 27 | return if ! $locale || grep { $locale eq $_ } @$list; |
| 28 | |
| 29 | $categories = [ $categories ] unless ref $categories; |
| 30 | |
| 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 | |
| 41 | foreach my $category (@$categories) { |
| 42 | return unless setlocale($category, $locale); |
| 43 | return if $only_plays_well && ! $plays_well; |
| 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 | |
| 53 | sub _decode_encodings { # For use only by other functions in this file! |
| 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"; |
| 65 | push @enc, "$_.65001"; # Windows UTF-8 |
| 66 | push @enc, "$_.ACP"; # Windows ANSI code page |
| 67 | push @enc, "$_.OCP"; # Windows OEM code page |
| 68 | push @enc, "$_.1252"; # Windows |
| 69 | } |
| 70 | } |
| 71 | if ($^O eq 'os390') { |
| 72 | push @enc, qw(IBM-037 IBM-819 IBM-1047); |
| 73 | } |
| 74 | push @enc, "UTF-8"; |
| 75 | push @enc, "65001"; # Windows UTF-8 |
| 76 | |
| 77 | return @enc; |
| 78 | } |
| 79 | |
| 80 | # Initialize this hash so that it looks like e.g., |
| 81 | # 6 => 'CTYPE', |
| 82 | # where 6 is the value of &POSIX::LC_CTYPE |
| 83 | my %category_name; |
| 84 | eval { require POSIX; import POSIX 'locale_h'; }; |
| 85 | unless ($@) { |
| 86 | my $number_for_missing_category = 0; |
| 87 | foreach my $name (qw(ALL COLLATE CTYPE MESSAGES MONETARY NUMERIC TIME)) { |
| 88 | my $number = eval "&POSIX::LC_$name"; |
| 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 | |
| 95 | $category_name{$number} = "$name"; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | sub 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; |
| 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/; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | sub 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. |
| 156 | my $categories = shift; |
| 157 | my $only_plays_well = shift // 0; |
| 158 | |
| 159 | return unless locales_enabled($categories); |
| 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); |
| 167 | return if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') |
| 168 | && $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i); |
| 169 | |
| 170 | # UWIN seems to loop after taint tests, just skip for now |
| 171 | return if ($^O =~ /^uwin/); |
| 172 | |
| 173 | # Done this way in case this is 'required' in the caller before seeing if |
| 174 | # this is miniperl. |
| 175 | eval { require POSIX; import POSIX 'locale_h'; }; |
| 176 | unless (defined &POSIX::LC_CTYPE) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | _trylocale("C", $categories, \@Locale, $only_plays_well); |
| 181 | _trylocale("POSIX", $categories, \@Locale, $only_plays_well); |
| 182 | foreach (0..15) { |
| 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); |
| 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. |
| 196 | delete local $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv}; |
| 197 | |
| 198 | # Other subversive stuff. |
| 199 | delete local @ENV{qw(IFS CDPATH ENV BASH_ENV)}; |
| 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; |
| 210 | _trylocale($_, $categories, \@Locale, $only_plays_well); |
| 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; |
| 222 | _trylocale($_, $categories, \@Locale, $only_plays_well); |
| 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; |
| 233 | _trylocale($_, $categories, \@Locale, $only_plays_well); |
| 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 | |
| 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)) { |
| 257 | _trylocale($loc, $categories, \@Locale, $only_plays_well); |
| 258 | foreach my $enc (@enc) { |
| 259 | _trylocale("$loc.$enc", $categories, \@Locale, |
| 260 | $only_plays_well); |
| 261 | } |
| 262 | $loc = lc $loc; |
| 263 | foreach my $enc (@enc) { |
| 264 | _trylocale("$loc.$enc", $categories, \@Locale, |
| 265 | $only_plays_well); |
| 266 | } |
| 267 | } |
| 268 | foreach my $lang (split(/ /, $language_codes)) { |
| 269 | _trylocale($lang, $categories, \@Locale, $only_plays_well); |
| 270 | foreach my $country (split(/ /, $country_codes)) { |
| 271 | my $lc = "${lang}_${country}"; |
| 272 | _trylocale($lc, $categories, \@Locale, $only_plays_well); |
| 273 | foreach my $enc (@enc) { |
| 274 | _trylocale("$lc.$enc", $categories, \@Locale, |
| 275 | $only_plays_well); |
| 276 | } |
| 277 | my $lC = "${lang}_\U${country}"; |
| 278 | _trylocale($lC, $categories, \@Locale, $only_plays_well); |
| 279 | foreach my $enc (@enc) { |
| 280 | _trylocale("$lC.$enc", $categories, \@Locale, |
| 281 | $only_plays_well); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | @Locale = sort @Locale; |
| 289 | |
| 290 | return @Locale; |
| 291 | } |
| 292 | |
| 293 | sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input |
| 294 | # is a UTF-8 locale |
| 295 | |
| 296 | # On z/OS, even locales marked as UTF-8 aren't. |
| 297 | return 0 if ord "A" != 65; |
| 298 | |
| 299 | eval { require POSIX; import POSIX 'locale_h'; }; |
| 300 | return 0 if ! defined &POSIX::LC_CTYPE; |
| 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 | eval { require POSIX; import POSIX 'locale_h'; }; |
| 351 | return if ! defined &POSIX::LC_CTYPE; |
| 352 | |
| 353 | my @locales = find_locales(&POSIX::LC_CTYPE(), |
| 354 | 1 # Reject iffy locales. |
| 355 | ); |
| 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 | |
| 366 | 1 |
| 367 | |
| 368 | # Format of data is: locale_name, language_codes, country_codes, encodings |
| 369 | __DATA__ |
| 370 | Afrikaans:af:za:1 15 |
| 371 | Arabic:ar:dz eg sa:6 arabic8 |
| 372 | Brezhoneg Breton:br:fr:1 15 |
| 373 | Bulgarski Bulgarian:bg:bg:5 |
| 374 | Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC |
| 375 | Hrvatski Croatian:hr:hr:2 |
| 376 | Cymraeg Welsh:cy:cy:1 14 15 |
| 377 | Czech:cs:cz:2 |
| 378 | Dansk Danish:da:dk:1 15 |
| 379 | Nederlands Dutch:nl:be nl:1 15 |
| 380 | English American British:en:au ca gb ie nz us uk zw:1 15 cp850 |
| 381 | Esperanto:eo:eo:3 |
| 382 | Eesti Estonian:et:ee:4 6 13 |
| 383 | Suomi Finnish:fi:fi:1 15 |
| 384 | Flamish::fl:1 15 |
| 385 | Deutsch German:de:at be ch de lu:1 15 |
| 386 | Euskaraz Basque:eu:es fr:1 15 |
| 387 | Galego Galician:gl:es:1 15 |
| 388 | Ellada Greek:el:gr:7 g8 |
| 389 | Frysk:fy:nl:1 15 |
| 390 | Greenlandic:kl:gl:4 6 |
| 391 | Hebrew:iw:il:8 hebrew8 |
| 392 | Hungarian:hu:hu:2 |
| 393 | Indonesian:id:id:1 15 |
| 394 | Gaeilge Irish:ga:IE:1 14 15 |
| 395 | Italiano Italian:it:ch it:1 15 |
| 396 | Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis |
| 397 | Korean:ko:kr: |
| 398 | Latine Latin:la:va:1 15 |
| 399 | Latvian:lv:lv:4 6 13 |
| 400 | Lithuanian:lt:lt:4 6 13 |
| 401 | Macedonian:mk:mk:1 15 |
| 402 | Maltese:mt:mt:3 |
| 403 | Moldovan:mo:mo:2 |
| 404 | Norsk Norwegian:no no\@nynorsk nb nn:no:1 15 |
| 405 | Occitan:oc:es:1 15 |
| 406 | Polski Polish:pl:pl:2 |
| 407 | Rumanian:ro:ro:2 |
| 408 | Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866 |
| 409 | Serbski Serbian:sr:yu:5 |
| 410 | Slovak:sk:sk:2 |
| 411 | Slovene Slovenian:sl:si:2 |
| 412 | Sqhip Albanian:sq:sq:1 15 |
| 413 | Svenska Swedish:sv:fi se:1 15 |
| 414 | Thai:th:th:11 tis620 |
| 415 | Turkish:tr:tr:9 turkish8 |
| 416 | Yiddish:yi::1 15 |