This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/loc_tools.pl: No setlocale when no locales
[perl5.git] / t / loc_tools.pl
index ed24c37..c76e293 100644 (file)
@@ -1,6 +1,5 @@
 # Common tools for test files files to find the locales which exist on the
-# system.  Caller should have defined ok() for the unlikely event that setup
-# here fails, and should have verified that this isn't miniperl before calling
+# system.  Caller should have verified that this isn't miniperl before calling
 # the functions.
 
 # Note that it's okay that some languages have their native names
 # Functions whose names begin with underscore are internal helper functions
 # for this file, and are not to be used by outside callers.
 
+use Config;
 use strict;
 
 eval { require POSIX; import POSIX 'locale_h'; };
 my $has_locale_h = ! $@;
 
+my @known_categories = ( qw(LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY
+                            LC_NUMERIC LC_TIME LC_ADDRESS LC_IDENTIFICATION
+                            LC_MEASUREMENT LC_PAPER LC_TELEPHONE));
+my @platform_categories;
+
 # 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
@@ -29,11 +34,10 @@ my $max_bad_category_number = -1000000;
 # where 6 is the value of &POSIX::LC_CTYPE
 my %category_name;
 my %category_number;
-unless ($@) {
+if ($has_locale_h) {
     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";
-
+    foreach my $name (@known_categories) {
+        my $number = eval "&POSIX::$name";
         if ($@) {
             # Use a negative number (smaller than any legitimate category
             # number) if the platform doesn't support this category, so we
@@ -46,14 +50,40 @@ unless ($@) {
         {
             # 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"
+            die "Unexpected locale category number '$number' for $name"
+        }
+        else {
+            push @platform_categories, $name;
         }
 
+        $name =~ s/LC_//;
         $category_name{$number} = "$name";
         $category_number{$name} = $number;
     }
 }
 
+sub _my_diag($) {
+    my $message = shift;
+    if (defined &main::diag) {
+        diag($message);
+    }
+    else {
+        local($\, $", $,) = (undef, ' ', '');
+        print STDERR $message, "\n";
+    }
+}
+
+sub _my_fail($) {
+    my $message = shift;
+    if (defined &main::fail) {
+        fail($message);
+    }
+    else {
+        local($\, $", $,) = (undef, ' ', '');
+        print "not ok 0 $message\n";
+    }
+}
+
 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
@@ -75,6 +105,19 @@ sub _trylocale ($$$$) { # For use only by other functions in this file!
 
     return if ! $locale || grep { $locale eq $_ } @$list;
 
+    # This is a toy (pig latin) locale that is not fully implemented on some
+    # systems
+    return if $locale =~ / ^ pig $ /ix;
+
+    # Certain platforms have a crippled locale system in which setlocale
+    # returns success for just about any possible locale name, but if anything
+    # actually happens as a result of the call, it is that the underlying
+    # locale is set to a system default, likely C or C.UTF-8.  We can't test
+    # such systems fully, but we shouldn't disable the user from using
+    # locales, as it may work out for them (or not).
+    return if    defined $Config{d_setlocale_accepts_any_locale_name}
+              && $locale !~ / ^ (?: C | POSIX | C\.UTF-8 ) $/ix;
+
     $categories = [ $categories ] unless ref $categories;
 
     my $badutf8 = 0;
@@ -84,7 +127,10 @@ sub _trylocale ($$$$) { # For use only by other functions in this file!
 
     local $SIG{__WARN__} = sub {
         $badutf8 = 1 if grep { /Malformed UTF-8/ } @_;
-        $plays_well = 0 if grep { /Locale .* may not work well/i } @_;
+        $plays_well = 0 if grep {
+                    /Locale .* may not work well(?#
+                   )|The Perl program will use the expected meanings/i
+            } @_;
     };
 
     # Incompatible locales aren't warned about unless using locales.
@@ -99,7 +145,7 @@ sub _trylocale ($$$$) { # For use only by other functions in this file!
     }
 
     if ($badutf8) {
-        ok(0, "Verify locale name doesn't contain malformed utf8");
+        _my_fail("Verify locale name doesn't contain malformed utf8");
         return;
     }
     push @$list, $locale if $plays_well || $allow_incompatible;
@@ -132,6 +178,13 @@ sub _decode_encodings { # For use only by other functions in this file!
     return @enc;
 }
 
+sub valid_locale_categories() {
+    # Returns a list of the locale categories (expressed as strings, like
+    # "LC_ALL) known to this program that are available on this platform.
+
+    return @platform_categories;
+}
+
 sub locales_enabled(;$) {
     # Returns 0 if no locale handling is available on this platform; otherwise
     # 1.
@@ -142,7 +195,7 @@ sub locales_enabled(;$) {
     # 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 to the platform.
+    # something that isn't a known category to this file.
     #
     # This optional parameter denotes which POSIX locale categories must be
     # available on the platform.  If any aren't available, this function
@@ -154,13 +207,20 @@ sub locales_enabled(;$) {
     # denoting a single category (either name or number).  No conversion into
     # a number is done in this case.
 
-    use Config;
+    # khw cargo-culted the '?' in the pattern on the next line.
+    return 0 if $Config{ccflags} =~ /\bD?NO_LOCALE\b/;
+
+    # If we can't load the POSIX XS module, we can't have locales even if they
+    # normally would be available
+    return 0 if ! defined &DynaLoader::boot_DynaLoader;
 
-    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;
+    if (! $Config{d_setlocale}) {
+        return 0 if $Config{ccflags} =~ /\bD?NO_POSIX_2008_LOCALE\b/;
+        return 0 unless $Config{d_newlocale};
+        return 0 unless $Config{d_uselocale};
+        return 0 unless $Config{d_duplocale};
+        return 0 unless $Config{d_freelocale};
+    }
 
     # Done with the global possibilities.  Now check if any passed in category
     # is disabled.
@@ -279,7 +339,16 @@ sub find_locales ($;$) {
     my @Locale;
     _trylocale("C", $categories, \@Locale, $allow_incompatible);
     _trylocale("POSIX", $categories, \@Locale, $allow_incompatible);
-    foreach (0..15) {
+
+    if ($Config{d_has_C_UTF8} eq 'true') {
+        _trylocale("C.UTF-8", $categories, \@Locale, $allow_incompatible);
+    }
+
+    # There's no point in looking at anything more if we know that setlocale
+    # will return success on any garbage or non-garbage name.
+    return sort @Locale if defined $Config{d_setlocale_accepts_any_locale_name};
+
+    foreach (1..16) {
         _trylocale("ISO8859-$_", $categories, \@Locale, $allow_incompatible);
         _trylocale("iso8859$_", $categories, \@Locale, $allow_incompatible);
         _trylocale("iso8859-$_", $categories, \@Locale, $allow_incompatible);
@@ -339,12 +408,16 @@ sub find_locales ($;$) {
         # This is going to be slow.
         my @Data;
 
-        # Locales whose name differs if the utf8 bit is on are stored in these two
-        # files with appropriate encodings.
-        if ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/) {
-            @Data = do "./lib/locale/utf8";
-        } else {
-            @Data = do "./lib/locale/latin1";
+        # Locales whose name differs if the utf8 bit is on are stored in these
+        # two files with appropriate encodings.
+        my $data_file = ($^H & 0x08 || (${^OPEN} || "") =~ /:utf8/)
+                        ? _source_location() . "/lib/locale/utf8"
+                        : _source_location() . "/lib/locale/latin1";
+        if (-e $data_file) {
+            @Data = do $data_file;
+        }
+        else {
+            _my_diag(__FILE__ . ":" . __LINE__ . ": '$data_file' doesn't exist");
         }
 
         # The rest of the locales are in this file.
@@ -353,6 +426,8 @@ sub find_locales ($;$) {
         foreach my $line (@Data) {
             my ($locale_name, $language_codes, $country_codes, $encodings) =
                 split /:/, $line;
+            _my_diag(__FILE__ . ":" . __LINE__ . ": Unexpected syntax in '$line'")
+                                                     unless defined $locale_name;
             my @enc = _decode_encodings($encodings);
             foreach my $loc (split(/ /, $locale_name)) {
                 _trylocale($loc, $categories, \@Locale, $allow_incompatible);
@@ -437,8 +512,8 @@ sub is_locale_utf8 ($) { # Return a boolean as to if core Perl thinks the input
     return $ret;
 }
 
-sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl
-                                  # thinks is a UTF-8 LC_CTYPE locale.
+sub find_utf8_ctype_locales (;$) { # Return the names of the locales that core
+                                  # Perl thinks are UTF-8 LC_CTYPE locales.
                                   # Optional parameter is a reference to a
                                   # list of locales to try; if omitted, this
                                   # tries all locales it can find on the
@@ -446,6 +521,7 @@ sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl
     return unless locales_enabled('LC_CTYPE');
 
     my $locales_ref = shift;
+    my @return;
 
     if (! defined $locales_ref) {
 
@@ -454,12 +530,82 @@ sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl
     }
 
     foreach my $locale (@$locales_ref) {
-        return $locale if is_locale_utf8($locale);
+        push @return, $locale if is_locale_utf8($locale);
+    }
+
+    return @return;
+}
+
+
+sub find_utf8_ctype_locale (;$) { # Return the name of a locale that core Perl
+                                  # thinks is a UTF-8 LC_CTYPE non-turkic
+                                  # locale.
+                                  # Optional parameter is a reference to a
+                                  # list of locales to try; if omitted, this
+                                  # tries all locales it can find on the
+                                  # platform
+    my $try_locales_ref = shift;
+
+    my @utf8_locales = find_utf8_ctype_locales($try_locales_ref);
+    my @turkic_locales = find_utf8_turkic_locales($try_locales_ref);
+
+    my %seen_turkic;
+
+    # Create undef elements in the hash for turkic locales
+    @seen_turkic{@turkic_locales} = ();
+
+    foreach my $locale (@utf8_locales) {
+        return $locale unless exists $seen_turkic{$locale};
     }
 
     return;
 }
 
+sub find_utf8_turkic_locales (;$) {
+
+    # Return the name of all the locales that core Perl thinks are UTF-8
+    # Turkic LC_CTYPE.  Optional parameter is a reference to a list of locales
+    # to try; if omitted, this tries all locales it can find on the platform
+
+    my @return;
+
+    return unless locales_enabled('LC_CTYPE');
+
+    my $save_locale = setlocale(&POSIX::LC_CTYPE());
+    foreach my $locale (find_utf8_ctype_locales(shift)) {
+        use locale;
+        setlocale(&POSIX::LC_CTYPE(), $locale);
+        push @return, $locale if uc('i') eq "\x{130}";
+    }
+    setlocale(&POSIX::LC_CTYPE(), $save_locale);
+
+    return @return;
+}
+
+sub find_utf8_turkic_locale (;$) {
+    my @turkics = find_utf8_turkic_locales(shift);
+
+    return unless @turkics;
+    return $turkics[0]
+}
+
+
+# returns full path to the directory containing the current source
+# file, inspired by mauke's Dir::Self
+sub _source_location {
+    require File::Spec;
+
+    my $caller_filename = (caller)[1];
+
+    my $loc = File::Spec->rel2abs(
+        File::Spec->catpath(
+            (File::Spec->splitpath($caller_filename))[0, 1], ''
+        )
+    );
+
+    return ($loc =~ /^(.*)$/)[0]; # untaint
+}
+
 1
 
 # Format of data is: locale_name, language_codes, country_codes, encodings