| 1 | package ExtUtils::MakeMaker::Locale; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | our $VERSION = "7.13_01"; | ||
| 5 | $VERSION = eval $VERSION; | ||
| 6 | |||
| 7 | use base 'Exporter'; | ||
| 8 | our @EXPORT_OK = qw( | ||
| 9 | decode_argv env | ||
| 10 | $ENCODING_LOCALE $ENCODING_LOCALE_FS | ||
| 11 | $ENCODING_CONSOLE_IN $ENCODING_CONSOLE_OUT | ||
| 12 | ); | ||
| 13 | |||
| 14 | use Encode (); | ||
| 15 | use Encode::Alias (); | ||
| 16 | |||
| 17 | our $ENCODING_LOCALE; | ||
| 18 | our $ENCODING_LOCALE_FS; | ||
| 19 | our $ENCODING_CONSOLE_IN; | ||
| 20 | our $ENCODING_CONSOLE_OUT; | ||
| 21 | |||
| 22 | sub DEBUG () { 0 } | ||
| 23 | |||
| 24 | sub _init { | ||
| 25 | if ($^O eq "MSWin32") { | ||
| 26 | unless ($ENCODING_LOCALE) { | ||
| 27 | # Try to obtain what the Windows ANSI code page is | ||
| 28 | eval { | ||
| 29 | unless (defined &GetConsoleCP) { | ||
| 30 | require Win32; | ||
| 31 | # no point falling back to Win32::GetConsoleCP from this | ||
| 32 | # as added same time, 0.45 | ||
| 33 | eval { Win32::GetConsoleCP() }; | ||
| 34 | # manually "import" it since Win32->import refuses | ||
| 35 | *GetConsoleCP = sub { &Win32::GetConsoleCP } unless $@; | ||
| 36 | } | ||
| 37 | unless (defined &GetConsoleCP) { | ||
| 38 | require Win32::API; | ||
| 39 | Win32::API->Import('kernel32', 'int GetConsoleCP()'); | ||
| 40 | } | ||
| 41 | if (defined &GetConsoleCP) { | ||
| 42 | my $cp = GetConsoleCP(); | ||
| 43 | $ENCODING_LOCALE = "cp$cp" if $cp; | ||
| 44 | } | ||
| 45 | }; | ||
| 46 | } | ||
| 47 | |||
| 48 | unless ($ENCODING_CONSOLE_IN) { | ||
| 49 | # only test one since set together | ||
| 50 | unless (defined &GetInputCP) { | ||
| 51 | eval { | ||
| 52 | require Win32; | ||
| 53 | eval { Win32::GetConsoleCP() }; | ||
| 54 | # manually "import" it since Win32->import refuses | ||
| 55 | *GetInputCP = sub { &Win32::GetConsoleCP } unless $@; | ||
| 56 | *GetOutputCP = sub { &Win32::GetConsoleOutputCP } unless $@; | ||
| 57 | }; | ||
| 58 | unless (defined &GetInputCP) { | ||
| 59 | eval { | ||
| 60 | # try Win32::Console module for codepage to use | ||
| 61 | require Win32::Console; | ||
| 62 | eval { Win32::Console::InputCP() }; | ||
| 63 | *GetInputCP = sub { &Win32::Console::InputCP } | ||
| 64 | unless $@; | ||
| 65 | *GetOutputCP = sub { &Win32::Console::OutputCP } | ||
| 66 | unless $@; | ||
| 67 | }; | ||
| 68 | } | ||
| 69 | unless (defined &GetInputCP) { | ||
| 70 | # final fallback | ||
| 71 | *GetInputCP = *GetOutputCP = sub { | ||
| 72 | # another fallback that could work is: | ||
| 73 | # reg query HKLM\System\CurrentControlSet\Control\Nls\CodePage /v ACP | ||
| 74 | ((qx(chcp) || '') =~ /^Active code page: (\d+)/) | ||
| 75 | ? $1 : (); | ||
| 76 | }; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | my $cp = GetInputCP(); | ||
| 80 | $ENCODING_CONSOLE_IN = "cp$cp" if $cp; | ||
| 81 | $cp = GetOutputCP(); | ||
| 82 | $ENCODING_CONSOLE_OUT = "cp$cp" if $cp; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | unless ($ENCODING_LOCALE) { | ||
| 87 | eval { | ||
| 88 | require I18N::Langinfo; | ||
| 89 | $ENCODING_LOCALE = I18N::Langinfo::langinfo(I18N::Langinfo::CODESET()); | ||
| 90 | |||
| 91 | # Workaround of Encode < v2.25. The "646" encoding alias was | ||
| 92 | # introduced in Encode-2.25, but we don't want to require that version | ||
| 93 | # quite yet. Should avoid the CPAN testers failure reported from | ||
| 94 | # openbsd-4.7/perl-5.10.0 combo. | ||
| 95 | $ENCODING_LOCALE = "ascii" if $ENCODING_LOCALE eq "646"; | ||
| 96 | |||
| 97 | # https://rt.cpan.org/Ticket/Display.html?id=66373 | ||
| 98 | $ENCODING_LOCALE = "hp-roman8" if $^O eq "hpux" && $ENCODING_LOCALE eq "roman8"; | ||
| 99 | }; | ||
| 100 | $ENCODING_LOCALE ||= $ENCODING_CONSOLE_IN; | ||
| 101 | } | ||
| 102 | |||
| 103 | if ($^O eq "darwin") { | ||
| 104 | $ENCODING_LOCALE_FS ||= "UTF-8"; | ||
| 105 | } | ||
| 106 | |||
| 107 | # final fallback | ||
| 108 | $ENCODING_LOCALE ||= $^O eq "MSWin32" ? "cp1252" : "UTF-8"; | ||
| 109 | $ENCODING_LOCALE_FS ||= $ENCODING_LOCALE; | ||
| 110 | $ENCODING_CONSOLE_IN ||= $ENCODING_LOCALE; | ||
| 111 | $ENCODING_CONSOLE_OUT ||= $ENCODING_CONSOLE_IN; | ||
| 112 | |||
| 113 | unless (Encode::find_encoding($ENCODING_LOCALE)) { | ||
| 114 | my $foundit; | ||
| 115 | if (lc($ENCODING_LOCALE) eq "gb18030") { | ||
| 116 | eval { | ||
| 117 | require Encode::HanExtra; | ||
| 118 | }; | ||
| 119 | if ($@) { | ||
| 120 | die "Need Encode::HanExtra to be installed to support locale codeset ($ENCODING_LOCALE), stopped"; | ||
| 121 | } | ||
| 122 | $foundit++ if Encode::find_encoding($ENCODING_LOCALE); | ||
| 123 | } | ||
| 124 | die "The locale codeset ($ENCODING_LOCALE) isn't one that perl can decode, stopped" | ||
| 125 | unless $foundit; | ||
| 126 | |||
| 127 | } | ||
| 128 | |||
| 129 | # use Data::Dump; ddx $ENCODING_LOCALE, $ENCODING_LOCALE_FS, $ENCODING_CONSOLE_IN, $ENCODING_CONSOLE_OUT; | ||
| 130 | } | ||
| 131 | |||
| 132 | _init(); | ||
| 133 | Encode::Alias::define_alias(sub { | ||
| 134 | no strict 'refs'; | ||
| 135 | no warnings 'once'; | ||
| 136 | return ${"ENCODING_" . uc(shift)}; | ||
| 137 | }, "locale"); | ||
| 138 | |||
| 139 | sub _flush_aliases { | ||
| 140 | no strict 'refs'; | ||
| 141 | for my $a (sort keys %Encode::Alias::Alias) { | ||
| 142 | if (defined ${"ENCODING_" . uc($a)}) { | ||
| 143 | delete $Encode::Alias::Alias{$a}; | ||
| 144 | warn "Flushed alias cache for $a" if DEBUG; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | sub reinit { | ||
| 150 | $ENCODING_LOCALE = shift; | ||
| 151 | $ENCODING_LOCALE_FS = shift; | ||
| 152 | $ENCODING_CONSOLE_IN = $ENCODING_LOCALE; | ||
| 153 | $ENCODING_CONSOLE_OUT = $ENCODING_LOCALE; | ||
| 154 | _init(); | ||
| 155 | _flush_aliases(); | ||
| 156 | } | ||
| 157 | |||
| 158 | sub decode_argv { | ||
| 159 | die if defined wantarray; | ||
| 160 | for (@ARGV) { | ||
| 161 | $_ = Encode::decode(locale => $_, @_); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | sub env { | ||
| 166 | my $k = Encode::encode(locale => shift); | ||
| 167 | my $old = $ENV{$k}; | ||
| 168 | if (@_) { | ||
| 169 | my $v = shift; | ||
| 170 | if (defined $v) { | ||
| 171 | $ENV{$k} = Encode::encode(locale => $v); | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | delete $ENV{$k}; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | return Encode::decode(locale => $old) if defined wantarray; | ||
| 178 | } | ||
| 179 | |||
| 180 | 1; | ||
| 181 | |||
| 182 | __END__ |