File Coverage

blib/lib/Windows1258.pm
Criterion Covered Total %
statement 77 132 58.3
branch 28 74 37.8
condition 1 6 16.6
subroutine 12 14 85.7
pod 0 2 0.0
total 118 228 51.7


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