File Coverage

blib/lib/Windows1258.pm
Criterion Covered Total %
statement 79 134 58.9
branch 29 74 39.1
condition 0 3 0.0
subroutine 11 13 84.6
pod 0 2 0.0
total 119 226 52.6


line stmt bran cond sub pod time code
1             package Windows1258;
2 206     206   99771 use strict;
  206         1294  
  206         7749  
3 206 50   206   3538 BEGIN { $INC{'warnings.pm'} = '' if $] < 5.006 } use warnings;
  206     206   915  
  206         351  
  206         4763  
4             ######################################################################
5             #
6             # Windows1258 - Source code filter to escape Windows-1258 script
7             #
8             # http://search.cpan.org/dist/Char-Windows1258/
9             #
10             # Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2018, 2019 INABA Hitoshi
11             ######################################################################
12              
13 206     206   3799 use 5.00503; # Galapagos Consensus 1998 for primetools
  206         606  
14             # use 5.008001; # Lancaster Consensus 2013 for toolchains
15              
16             # 12.3. Delaying use Until Runtime
17             # in Chapter 12. Packages, Libraries, and Modules
18             # of ISBN 0-596-00313-7 Perl Cookbook, 2nd Edition.
19             # (and so on)
20              
21             # Version numbers should be boring
22             # http://www.dagolden.com/index.php/369/version-numbers-should-be-boring/
23             # For the impatient, the disinterested or those who just want to follow
24             # a recipe, my advice for all modules is this:
25             # our $VERSION = "0.001"; # or "0.001_001" for a dev release
26             # $VERSION = CORE::eval $VERSION; # No!! because '1.10' makes '1.1'
27              
28 206     206   1067 use vars qw($VERSION);
  206         374  
  206         29748  
29             $VERSION = '1.22';
30             $VERSION = $VERSION;
31              
32             BEGIN {
33 206 50   206   1364 if ($^X =~ / jperl /oxmsi) {
34 0         0 die __FILE__, ": needs perl(not jperl) 5.00503 or later. (\$^X==$^X)\n";
35             }
36 206         373 if (CORE::ord('A') == 193) {
37             die __FILE__, ": is not US-ASCII script (may be EBCDIC or EBCDIK script).\n";
38             }
39 206         13626 if (CORE::ord('A') != 0x41) {
40             die __FILE__, ": is not US-ASCII script (must be US-ASCII script).\n";
41             }
42             }
43              
44             BEGIN {
45 206     206   1880 (my $dirname = __FILE__) =~ s{^(.+)/[^/]*$}{$1};
46 206         634 unshift @INC, $dirname;
47 206         239589 CORE::require Ewindows1258;
48             }
49              
50             # instead of Symbol.pm
51 618         2027 BEGIN {
52             sub gensym () {
53 0 50   618 0 0 if ($] < 5.006) {
54 0         0 return \do { local *_ };
  618         1389  
55             }
56             else {
57 206         11210 return undef;
58             }
59             }
60             }
61              
62             # P.714 29.2.39. flock
63             # in Chapter 29: Functions
64             # of ISBN 0-596-00027-8 Programming Perl Third Edition.
65              
66             # P.863 flock
67             # in Chapter 27: Functions
68             # of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
69              
70             # P.228 Inlining Constant Functions
71             # in Chapter 6: Subroutines
72             # of ISBN 0-596-00027-8 Programming Perl Third Edition.
73              
74             # P.331 Inlining Constant Functions
75             # in Chapter 7: Subroutines
76             # of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
77              
78             sub LOCK_SH() {1}
79             sub LOCK_EX() {2}
80             sub LOCK_UN() {8}
81             sub LOCK_NB() {4}
82              
83       0     sub unimport {}
84             sub Windows1258::escape_script;
85              
86             # 6.18. Matching Multiple-Byte Characters
87             # in Chapter 6. Pattern Matching
88             # of ISBN 978-1-56592-243-3 Perl Perl Cookbook.
89             # (and so on)
90              
91             # regexp of character
92             my $qq_char = qr/(?> \\c[\x40-\x5F] | \\? (?:[\x00-\xFF] | [\x00-\xFF]) )/oxms;
93             my $q_char = qr/(?> [\x00-\xFF] | [\x00-\xFF] )/oxms;
94              
95             # when this script is main program
96             if ($0 eq __FILE__) {
97              
98             # show usage
99             unless (@ARGV) {
100             die <
101             $0: usage
102              
103             perl $0 Windows-1258_script.pl > Escaped_script.pl.e
104             END
105             }
106              
107             print Windows1258::escape_script($ARGV[0]);
108             exit 0;
109             }
110              
111             my($package,$filename,$line,$subroutine,$hasargs,$wantarray,$evaltext,$is_require,$hints,$bitmask) = caller 0;
112              
113             # called any package not main
114             if ($package ne 'main') {
115             die <
116             @{[__FILE__]}: escape by manually command '$^X @{[__FILE__]} "$filename" > "@{[__PACKAGE__]}::$filename"'
117             and rewrite "use $package;" to "use @{[__PACKAGE__]}::$package;" of script "$0".
118             END
119             }
120              
121             # P.302 Module Privacy and the Exporter
122             # in Chapter 11: Modules
123             # of ISBN 0-596-00027-8 Programming Perl Third Edition.
124             #
125             # A module can do anything it jolly well pleases when it's used, since use just
126             # calls the ordinary import method for the module, and you can define that
127             # method to do anything you like.
128              
129             # P.406 Module Privacy and the Exporter
130             # in Chapter 11: Modules
131             # of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
132             #
133             # A module can do anything it jolly well pleases when it's used, since use just
134             # calls the ordinary import method for the module, and you can define that
135             # method to do anything you like.
136              
137             sub import {
138              
139 0 50   206   0 if (-e("$filename.e")) {
140 0 0       0 if (exists $ENV{'CHAR_DEBUG'}) {
    0          
141 0         0 unlink "$filename.e";
142             }
143             elsif (-z("$filename.e")) {
144 0         0 unlink "$filename.e";
145             }
146             else {
147              
148             #----------------------------------------------------
149             # older >
150             # newer >>>>>
151             #----------------------------------------------------
152             # Filter >
153             # Source >>>>>
154             # Escape >>> needs re-escape (Source was changed)
155             #
156             # Filter >>>
157             # Source >>>>>
158             # Escape > needs re-escape (Source was changed)
159             #
160             # Filter >>>>>
161             # Source >>>
162             # Escape > needs re-escape (Source was changed)
163             #
164             # Filter >>>>>
165             # Source >
166             # Escape >>> needs re-escape (Filter was changed)
167             #
168             # Filter >
169             # Source >>>
170             # Escape >>>>> executable without re-escape
171             #
172             # Filter >>>
173             # Source >
174             # Escape >>>>> executable without re-escape
175             #----------------------------------------------------
176              
177 0         0 my $mtime_filter = (stat(__FILE__ ))[9];
178 0         0 my $mtime_source = (stat($filename ))[9];
179 0         0 my $mtime_escape = (stat("$filename.e"))[9];
180 0 0 0     0 if (($mtime_escape < $mtime_source) or ($mtime_escape < $mtime_filter)) {
181 206         2087 unlink "$filename.e";
182             }
183             }
184             }
185              
186 206 50       756 if (not -e("$filename.e")) {
187 206         5405 my $fh = gensym();
188 206 50       1778 Ewindows1258::_open_a($fh, "$filename.e") or die __FILE__, ": Can't write open file: $filename.e\n";
189              
190             # 7.19. Flushing Output
191             # in Chapter 7. File Access
192             # of ISBN 0-596-00313-7 Perl Cookbook, 2nd Edition.
193              
194 206         970 select((select($fh), $|=1)[0]);
195              
196 0 50       0 if (0) {
197             }
198 0         0 elsif (exists $ENV{'CHAR_NONBLOCK'}) {
199              
200             # P.419 File Locking
201             # in Chapter 16: Interprocess Communication
202             # of ISBN 0-596-00027-8 Programming Perl Third Edition.
203              
204             # P.524 File Locking
205             # in Chapter 15: Interprocess Communication
206             # of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
207              
208             # P.571 Handling Race Conditions
209             # in Chapter 23: Security
210             # of ISBN 0-596-00027-8 Programming Perl Third Edition.
211              
212             # P.663 Handling Race Conditions
213             # in Chapter 20: Security
214             # of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
215              
216             # (and so on)
217              
218 0         0 CORE::eval q{ flock($fh, LOCK_EX | LOCK_NB) };
219 0 0       0 if ($@) {
220 206         13022 die __FILE__, ": Can't immediately write-lock the file: $filename.e\n";
221             }
222             }
223             else {
224 206         8754 CORE::eval q{ flock($fh, LOCK_EX) };
225             }
226              
227 206         2431 CORE::eval q{ truncate($fh, 0) };
228 206 50       673 seek($fh, 0, 0) or die __FILE__, ": Can't seek file: $filename.e\n";
229              
230 206         470 my $e_script = Windows1258::escape_script($filename);
231 206         13395 print {$fh} $e_script;
  206         4866  
232              
233 206         4886 my $mode = (stat($filename))[2] & 0777;
234 206         36528 chmod $mode, "$filename.e";
235              
236 206 50       974 close($fh) or die "Can't close file: $filename.e: $!";
237             }
238              
239 206         1291 my $fh = gensym();
240 206 50       1158 Ewindows1258::_open_r($fh, "$filename.e") or die __FILE__, ": Can't read open file: $filename.e\n";
241              
242 0 50       0 if (0) {
243             }
244 0         0 elsif (exists $ENV{'CHAR_NONBLOCK'}) {
245 0         0 CORE::eval q{ flock($fh, LOCK_SH | LOCK_NB) };
246 0 0       0 if ($@) {
247 206         14317 die __FILE__, ": Can't immediately read-lock the file: $filename.e\n";
248             }
249             }
250             else {
251 206         944 CORE::eval q{ flock($fh, LOCK_SH) };
252             }
253              
254 206         966 my @switch = ();
255 0 50       0 if ($^W) {
256 206         817 push @switch, '-w';
257             }
258 0 50       0 if (defined $^I) {
259 0         0 push @switch, '-i' . $^I;
260 206         405 undef $^I;
261             }
262              
263             # P.707 29.2.33. exec
264             # in Chapter 29: Functions
265             # of ISBN 0-596-00027-8 Programming Perl Third Edition.
266             #
267             # If there is more than one argument in LIST, or if LIST is an array with more
268             # than one value, the system shell will never be used. This also bypasses any
269             # shell processing of the command. The presence or absence of metacharacters in
270             # the arguments doesn't affect this list-triggered behavior, which makes it the
271             # preferred from in security-conscious programs that do not with to expose
272             # themselves to potential shell escapes.
273             # Environment variable PERL5SHELL(Microsoft ports only) will never be used, too.
274              
275             # P.855 exec
276             # in Chapter 27: Functions
277             # of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
278             #
279             # If there is more than one argument in LIST, or if LIST is an array with more
280             # than one value, the system shell will never be used. This also bypasses any
281             # shell processing of the command. The presence or absence of metacharacters in
282             # the arguments doesn't affect this list-triggered behavior, which makes it the
283             # preferred from in security-conscious programs that do not wish to expose
284             # themselves to injection attacks via shell escapes.
285             # Environment variable PERL5SHELL(Microsoft ports only) will never be used, too.
286              
287             # P.489 #! and Quoting on Non-Unix Systems
288             # in Chapter 19: The Command-Line Interface
289             # of ISBN 0-596-00027-8 Programming Perl Third Edition.
290              
291             # P.578 #! and Quoting on Non-Unix Systems
292             # in Chapter 17: The Command-Line Interface
293             # of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
294              
295 206         1452 my $system = 0;
296              
297             # DOS-like system
298 0 50       0 if ($^O =~ /\A (?: MSWin32 | NetWare | symbian | dos ) \z/oxms) {
299             $system = Ewindows1258::_systemx(
300             _escapeshellcmd_MSWin32($^X),
301              
302             # -I switch can not treat space included path
303             # (map { '-I' . _escapeshellcmd_MSWin32($_) } @INC),
304 0         0 (map { '-I' . $_ } @INC),
305              
306             @switch,
307             '--',
308 0         0 map { _escapeshellcmd_MSWin32($_) } "$filename.e", @ARGV
  206         741  
309             );
310             }
311              
312             # UNIX-like system
313             else {
314             $system = Ewindows1258::_systemx(
315             _escapeshellcmd($^X),
316 206         677 (map { '-I' . _escapeshellcmd($_) } @INC),
317             @switch,
318             '--',
319 2472         3284 map { _escapeshellcmd($_) } "$filename.e", @ARGV
  206         340549  
320             );
321             }
322              
323             # exit with actual exit value
324 0         0 exit($system >> 8);
325             }
326              
327             # escape shell command line on DOS-like system
328             sub _escapeshellcmd_MSWin32 {
329 0     0   0 my($word) = @_;
330 0 0       0 if ($word =~ / [ ] /oxms) {
331 0         0 return qq{"$word"};
332             }
333             else {
334 2884         4008 return $word;
335             }
336             }
337              
338             # escape shell command line on UNIX-like system
339             sub _escapeshellcmd {
340 2884     2884   7049 my($word) = @_;
341 206         467 return $word;
342             }
343              
344             # P.619 Source Filters
345             # in Chapter 24: Common Practices
346             # of ISBN 0-596-00027-8 Programming Perl Third Edition.
347              
348             # P.718 Source Filters
349             # in Chapter 21: Common Practices
350             # of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
351              
352             # escape Windows-1258 script
353             sub Windows1258::escape_script {
354 206     206 0 345 my($script) = @_;
355 206         413 my $e_script = '';
356              
357             # read Windows-1258 script
358 206         710 my $fh = gensym();
359 206 50       1074 Ewindows1258::_open_r($fh, $script) or die __FILE__, ": Can't open file: $script\n";
360 206         5123 local $/ = undef; # slurp mode
361 206         2263 $_ = <$fh>;
362 206 50       1020 close($fh) or die "Can't close file: $script: $!";
363              
364 0 50       0 if (/^ use Ewindows1258(?:(?>\s+)(?>[0-9\.]*))?(?>\s*); $/oxms) {
365 206         642 return $_;
366             }
367             else {
368              
369             # #! shebang line
370 0 50       0 if (s/\A(#!.+?\n)//oms) {
371 0         0 my $head = $1;
372 0         0 $head =~ s/\bjperl\b/perl/gi;
373 206         485 $e_script .= $head;
374             }
375              
376             # DOS-like system header
377 0 50       0 if (s/\A(\@rem(?>\s*)=(?>\s*)'.*?'(?>\s*);\s*\n)//oms) {
378 0         0 my $head = $1;
379 0         0 $head =~ s/\bjperl\b/perl/gi;
380 206         6987 $e_script .= $head;
381             }
382              
383             # P.618 Generating Perl in Other Languages
384             # in Chapter 24: Common Practices
385             # of ISBN 0-596-00027-8 Programming Perl Third Edition.
386              
387             # P.717 Generating Perl in Other Languages
388             # in Chapter 21: Common Practices
389             # of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
390              
391 0 50       0 if (s/(.*^#(?>\s*)line(?>\s+)(?>[0-9]+)(?:(?>\s+)"(?:$q_char)+?")?\s*\n)//oms) {
392 0         0 my $head = $1;
393 0         0 $head =~ s/\bjperl\b/perl/gi;
394 206         1055 $e_script .= $head;
395             }
396              
397             # P.210 5.10.3.3. Match-time code evaluation
398             # in Chapter 5: Pattern Matching
399             # of ISBN 0-596-00027-8 Programming Perl Third Edition.
400              
401             # P.255 Match-time code evaluation
402             # in Chapter 5: Pattern Matching
403             # of ISBN 978-0-596-00492-7 Programming Perl 4th Edition.
404              
405             # '...' quote to avoid "Octal number in vector unsupported" on perl 5.6
406              
407 206         2132 $e_script .= sprintf("use Ewindows1258 '%s.0'; # 'quote' for perl5.6\n", $Windows1258::VERSION); # require run-time routines version
408              
409             # use Windows1258 version qw(ord reverse getc);
410 206 50       597 if (s/^ (?>\s*) use (?>\s+) (?: Char | Windows1258 ) (?>\s*) ([^;]*) ; \s* \n? $//oxms) {
411              
412             # require version
413 206         976 my $list = $1;
414 0 50       0 if ($list =~ s/\A ((?>[0-9]+)\.(?>[0-9]+)) \.0 (?>\s*) //oxms) {
    50          
415 0         0 my $version = $1;
416 0 0       0 if ($version ne $Windows1258::VERSION) {
417 0         0 my @file = grep -e, map {qq{$_/Windows1258.pm}} @INC;
  0         0  
418 0         0 my %file = map { $_ => 1 } @file;
  0         0  
419 0 0       0 if (scalar(keys %file) >= 2) {
420 0         0 my $file = join "\n", sort keys %file;
421 0         0 warn <
422             ****************************************************
423             C A U T I O N
424              
425             CONFLICT Windows1258.pm FILE
426              
427             $file
428             ****************************************************
429              
430             END
431             }
432 0         0 die "Script $0 expects Windows1258.pm $version, but @{[__FILE__]} is version $Windows1258::VERSION\n";
  0         0  
433             }
434 0         0 $e_script .= qq{die "Script \$0 expects Ewindows1258.pm $version, but \\\$Ewindows1258::VERSION is \$Ewindows1258::VERSION" if \$Ewindows1258::VERSION ne '$version';\n};
435             }
436             elsif ($list =~ s/\A ((?>[0-9]+)(?>\.[0-9]*)) (?>\s*) //oxms) {
437 0         0 my $version = $1;
438 0 0       0 if ($version > $Windows1258::VERSION) {
439 0         0 die "Script $0 required Windows1258.pm $version, but @{[__FILE__]} is only version $Windows1258::VERSION\n";
  206         844  
440             }
441             }
442              
443             # demand ord, reverse, and getc
444 3 100       3 if ($list !~ /\A (?>\s*) \z/oxms) {
445 3         118 local $@;
446 3         9 my @list = CORE::eval $list;
447 3         9 for (@list) {
448 3 50       6 $Ewindows1258::function_ord = 'Windows1258::ord' if /\A ord \z/oxms;
449 3 50       5 $Ewindows1258::function_ord_ = 'Windows1258::ord_' if /\A ord \z/oxms;
450 3 50       11 $Ewindows1258::function_reverse = 'Windows1258::reverse' if /\A reverse \z/oxms;
451 3 50       4 $Ewindows1258::function_getc = 'Windows1258::getc' if /\A getc \z/oxms;
452              
453             # avoid: used only once: possible typo at ...
454 3         4 $Ewindows1258::function_ord = $Ewindows1258::function_ord;
455 3         4 $Ewindows1258::function_ord_ = $Ewindows1258::function_ord_;
456 3         6 $Ewindows1258::function_reverse = $Ewindows1258::function_reverse;
457 206         730 $Ewindows1258::function_getc = $Ewindows1258::function_getc;
458             }
459             }
460             }
461             }
462              
463 206         1659 $e_script .= Windows1258::escape();
464              
465             return $e_script;
466             }
467              
468             1;
469              
470             __END__