+# LC_ALL can be -1 on some platforms. And, in fact the implementors could
+# legally use any integer to represent any category. But it makes the most
+# sense for them to have used small integers. Below, we create new locale
+# numbers for ones missing from this machine. We make them very negative,
+# hopefully more negative than anything likely to be a valid category on the
+# platform, but also below is a check to be sure that our guess is valid.
+my $max_bad_category_number = -1000000;
+
+# Initialize this hash so that it looks like e.g.,
+# 6 => 'CTYPE',
+# where 6 is the value of &POSIX::LC_CTYPE
+my %category_name;
+unless ($@) {
+ my $number_for_missing_category = $max_bad_category_number;
+ foreach my $name (qw(ALL COLLATE CTYPE MESSAGES MONETARY NUMERIC TIME)) {
+ my $number = eval "&POSIX::LC_$name";
+
+ if ($@) {
+ # Use a negative number (smaller than any legitimate category
+ # number) if the platform doesn't support this category, so we
+ # have an entry for all the ones that might be specified in calls
+ # to us.
+ $number = $number_for_missing_category-- if $@;
+ }
+ elsif ( $number !~ / ^ -? \d+ $ /x
+ || $number <= $max_bad_category_number)
+ {
+ # We think this should be an int. And it has to be larger than
+ # any of our synthetic numbers.
+ die "Unexpected locale category number '$number' for LC_$name"
+ }
+
+ $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 (and an optional leading minus), 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_locale_h;
+
+ # 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_name_or_number (@local_categories_copy) {
+ my $name;
+ my $number;
+ if ($category_name_or_number =~ / ^ -? \d+ $ /x) {
+ $number = $category_name_or_number;
+ die "Invalid locale category number '$number'"
+ unless grep { $number == $_ } keys %category_name;
+ $name = $category_name{$number};
+ }
+ else {
+ $name = $category_name_or_number;
+ $name =~ s/ ^ LC_ //x;
+ foreach my $trial (keys %category_name) {
+ if ($category_name{$trial} eq $name) {
+ $number = $trial;
+ last;
+ }
+ }
+ die "Invalid locale category name '$name'"
+ unless defined $number;
+ }
+
+ return 0 if $number <= $max_bad_category_number
+ || $Config{ccflags} =~ /\bD?NO_LOCALE_$name\b/;
+
+ eval "defined &POSIX::LC_$name";
+ return 0 if $@;
+ }
+ }
+
+ 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
+ # that play well with Perl.
+ # The first parameter is either a single locale
+ # category or a reference to a list of categories to
+ # find valid locales for it (or in the case of
+ # multiple) for all of them.