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