# anyway later during the scanning process (and besides, some clueless
# vendor might have them capitalized erroneously anyway).
-sub _trylocale ($$$$) { # Adds the locale given by the first parameter to the
- # list given by the 3rd iff the platform supports the
- # locale in each of the categories given by the 2nd
- # parameter, which is either a single category or a
- # reference to a list of categories
- # The 4th parameter is true if to reject locales that
- # aren't apparently fully compatible with Perl.
+# Functions whose names begin with underscore are internal helper functions
+# for this file, and are not to be used by outside callers.
+
+eval { require POSIX; import POSIX 'locale_h'; };
+my $has_posix_locales = defined &POSIX::LC_CTYPE;
+
+sub _trylocale ($$$$) { # For use only by other functions in this file!
+
+ # Adds the locale given by the first parameter to the list given by the
+ # 3rd iff the platform supports the locale in each of the categories given
+ # by the 2nd parameter, which is either a single category or a reference
+ # to a list of categories The 4th parameter is true if to reject locales
+ # that aren't apparently fully compatible with Perl.
+
my $locale = shift;
my $categories = shift;
my $list = shift;
push @$list, $locale;
}
-sub _decode_encodings {
+sub _decode_encodings { # For use only by other functions in this file!
my @enc;
foreach (split(/ /, shift)) {
return @enc;
}
+# Initialize this hash so that it looks like e.g.,
+# 6 => 'CTYPE',
+# where 6 is the value of &POSIX::LC_CTYPE
+my %category_name;
+eval { require POSIX; import POSIX 'locale_h'; };
+unless ($@) {
+ my $number_for_missing_category = 0;
+ foreach my $name (qw(ALL COLLATE CTYPE MESSAGES MONETARY NUMERIC TIME)) {
+ my $number = eval "&POSIX::LC_$name";
+
+ # Use a negative number if the platform doesn't support this category,
+ # so we have an entry for all ones that might be specified in calls to
+ # us.
+ $number = --$number_for_missing_category if $@;
+
+ $category_name{$number} = "$name";
+ }
+}
+
+sub locales_enabled(;$) {
+ # Returns 0 if no locale handling is available on this platform; otherwise
+ # 1.
+ #
+ # The optional parameter is a reference to a list of individual POSIX
+ # locale categories. If present, this function also returns 0 if any of
+ # them are individually not available on this platform; otherwise 1.
+ # Actually, it is acceptable for the list to be just a simple scalar
+ # denoting a single category.
+ #
+ # If any of the individual categories specified by the optional parameter
+ # is all digits, it is taken to be the C enum for the category (e.g.,
+ # &POSIX::LC_CTYPE). Otherwise it should be a string name of the
+ # category, like 'LC_TIME'. The initial 'LC_' is optional. It is a fatal
+ # error to call this with something that isn't a known category
+
+ use Config;
+
+ return 0 unless $Config{d_setlocale}
+ # I (khw) cargo-culted the '?' in the pattern on the
+ # next line.
+ && $Config{ccflags} !~ /\bD?NO_LOCALE\b/
+ && $has_posix_locales;
+
+ # Done with the global possibilities. Now check if any passed in category
+ # is disabled.
+ my $categories_ref = shift;
+ if (defined $categories_ref) {
+ $categories_ref = [ $categories_ref ] if ! ref $categories_ref;
+ my @local_categories_copy = @$categories_ref;
+ for my $category (@local_categories_copy) {
+ if ($category =~ / ^ -? \d+ $ /x) {
+ die "Invalid locale category number '$category'"
+ unless grep { $category == $_ } keys %category_name;
+ $category = $category_name{$category};
+ }
+ else {
+ $category =~ s/ ^ LC_ //x;
+ die "Invalid locale category name '$category'"
+ unless grep { $category eq $_ } values %category_name;
+ }
+
+ return 0 if $Config{ccflags} =~ /\bD?NO_LOCALE_$category\b/;
+ }
+ }
+
+ return 1;
+}
+
+
sub find_locales ($;$) { # Returns an array of all the locales we found on the
# system. If the optional 2nd parameter is
# non-zero, the list is restricted to those locales
my $categories = shift;
my $only_plays_well = shift // 0;
- use Config;;
- my $have_setlocale = $Config{d_setlocale};
+ return unless locales_enabled($categories);
# Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
# and mingw32 uses said silly CRT
# so re-enable the tests for Windows XP onwards.
my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
- $have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') &&
- $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i);
+ return if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare')
+ && $Config{cc} =~ /^(cl|gcc|g\+\+|ici)/i);
# UWIN seems to loop after taint tests, just skip for now
- $have_setlocale = 0 if ($^O =~ /^uwin/);
-
- return unless $have_setlocale;
+ return if ($^O =~ /^uwin/);
# Done this way in case this is 'required' in the caller before seeing if
# this is miniperl.
- eval { require POSIX; import POSIX 'locale_h'; };
- unless (defined &POSIX::LC_CTYPE) {
- return;
- }
+ return unless $has_posix_locales;
_trylocale("C", $categories, \@Locale, $only_plays_well);
_trylocale("POSIX", $categories, \@Locale, $only_plays_well);
# On z/OS, even locales marked as UTF-8 aren't.
return 0 if ord "A" != 65;
- eval { require POSIX; import POSIX 'locale_h'; };
- return 0 if ! defined &POSIX::LC_CTYPE;
+ return 0 if ! $has_posix_locales;
+ return 0 if ! locales_enabled('LC_CTYPE');
my $locale = shift;
my $locales_ref = shift;
if (! defined $locales_ref) {
- eval { require POSIX; import POSIX 'locale_h'; };
- return if ! defined &POSIX::LC_CTYPE;
+ return if ! $has_posix_locales;
my @locales = find_locales(&POSIX::LC_CTYPE(),
1 # Reject iffy locales.