File Coverage

blib/lib/Cyrillic.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 Cyrillic;
2             ######################################################################
3             #
4             # Cyrillic - Source code filter to escape Cyrillic script
5             #
6             # http://search.cpan.org/dist/Char-Cyrillic/
7             #
8             # Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 INABA Hitoshi
9             ######################################################################
10              
11 200     200   119172 use 5.00503; # Galapagos Consensus 1998 for primetools
  200         501  
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   11006 BEGIN { CORE::eval q{ use vars qw($VERSION) } }
  200     200   811  
  200         271  
  200         30363  
27             $VERSION = '1.08';
28             $VERSION = CORE::eval $VERSION;
29              
30             BEGIN {
31 200 50   200   1163 if ($^X =~ / jperl /oxmsi) {
32 0         0 die __FILE__, ": needs perl(not jperl) 5.00503 or later. (\$^X==$^X)\n";
33             }
34 200         227 if (CORE::ord('A') == 193) {
35             die __FILE__, ": is not US-ASCII script (may be EBCDIC or EBCDIK script).\n";
36             }
37 200         3877 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   277496 BEGIN { CORE::require Ecyrillic; }
43              
44             # instead of Symbol.pm
45 0         0 BEGIN {
46 200     200   1212 my $genpkg = "Symbol::";
47 200         7984 my $genseq = 0;
48             sub gensym () {
49 600     600 0 2957 my $name = "GEN" . $genseq++;
50              
51             # here, no strict qw(refs); if strict.pm exists
52              
53 600         1598 my $ref = \*{$genpkg . $name};
  600         5472  
54 600         2310 delete $$genpkg{$name};
55 600         1832 $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   1652 if (CORE::eval { local $@; CORE::require strict }) {
  200         916  
  200         2723  
67 200         320086 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 Cyrillic::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 Cyrillic_script.pl > Escaped_script.pl.e
113             END
114             }
115              
116             print Cyrillic::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   12714 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       2965 if (not -e("$filename.e")) {
196 200         1139 my $fh = gensym();
197              
198 200 50 33 200   1679 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         880  
  200         14269  
  200         12893  
199             }
200             else {
201 0 0       0 Ecyrillic::_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         3665 select((select($fh), $|=1)[0]);
209              
210 200 50       2265 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         12236 CORE::eval q{ flock($fh, LOCK_EX) };
239             }
240              
241 200         11905 CORE::eval q{ truncate($fh, 0) };
242 200 50       1729 seek($fh, 0, 0) or die __FILE__, ": Can't seek file: $filename.e\n";
243              
244 200         4466 my $e_script = Cyrillic::escape_script($filename);
245 200         352 print {$fh} $e_script;
  200         15407  
246              
247 200         3953 my $mode = (stat($filename))[2] & 0777;
248 200         6506 chmod $mode, "$filename.e";
249              
250 200 50       4307 close($fh) or die __FILE__, ": Can't close file: $filename.e\n";
251             }
252              
253 200         707 my $fh = gensym();
254 200 50       1186 Ecyrillic::_open_r($fh, "$filename.e") or die __FILE__, ": Can't read open file: $filename.e\n";
255              
256 200 50       1178 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         10486 CORE::eval q{ flock($fh, LOCK_SH) };
266             }
267              
268 200         689 my @switch = ();
269 200 50       986 if ($^W) {
270 0         0 push @switch, '-w';
271             }
272 200 50       919 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       1214 if ($^O =~ /\A (?: MSWin32 | NetWare | symbian | dos ) \z/oxms) {
311             exit Ecyrillic::_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 Ecyrillic::_systemx(
327             _escapeshellcmd($^X),
328 2200         2308 (map { '-I' . _escapeshellcmd($_) } @INC),
329             @switch,
330             '--',
331 200         760 map { _escapeshellcmd($_) } "$filename.e", @ARGV
  200         599  
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   2608 my($word) = @_;
350 2600         5574 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 Cyrillic script
362             sub Cyrillic::escape_script {
363 200     200 0 3625 my($script) = @_;
364 200         1868 my $e_script = '';
365              
366             # read Cyrillic script
367 200         2459 my $fh = gensym();
368 200 50       718 Ecyrillic::_open_r($fh, $script) or die __FILE__, ": Can't open file: $script\n";
369 200         1014 local $/ = undef; # slurp mode
370 200         3578 $_ = <$fh>;
371 200 50       1811 close($fh) or die __FILE__, ": Can't close file: $script\n";
372              
373 200 50       863 if (/^ use Ecyrillic(?:(?>\s+)(?>[0-9\.]*))?(?>\s*); $/oxms) {
374 0         0 return $_;
375             }
376             else {
377              
378             # #! shebang line
379 200 50       613 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       475 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       7576 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         2722 $e_script .= sprintf("use Ecyrillic '%s.0'; # 'quote' for perl5.6\n", $Cyrillic::VERSION); # require run-time routines version
417              
418             # use Cyrillic version qw(ord reverse getc);
419 200 50       2244 if (s/^ (?>\s*) use (?>\s+) (?: Char | Cyrillic ) (?>\s*) ([^;]*) ; \s* \n? $//oxms) {
420              
421             # require version
422 200         523 my $list = $1;
423 200 50       728 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 $Cyrillic::VERSION) {
426 0         0 my @file = grep -e, map {qq{$_/Cyrillic.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 Cyrillic.pm FILE
435              
436             $file
437             ****************************************************
438              
439             END
440             }
441 0         0 die "Script $0 expects Cyrillic.pm $version, but @{[__FILE__]} is version $Cyrillic::VERSION\n";
  0         0  
442             }
443 0         0 $e_script .= qq{die "Script \$0 expects Ecyrillic.pm $version, but \\\$Ecyrillic::VERSION is \$Ecyrillic::VERSION" if \$Ecyrillic::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 > $Cyrillic::VERSION) {
448 0         0 die "Script $0 required Cyrillic.pm $version, but @{[__FILE__]} is only version $Cyrillic::VERSION\n";
  0         0  
449             }
450             }
451              
452             # demand ord, reverse, and getc
453 200 100       948 if ($list !~ /\A (?>\s*) \z/oxms) {
454 3         3 local $@;
455 3         109 my @list = CORE::eval $list;
456 3         7 for (@list) {
457 3 50       8 $Ecyrillic::function_ord = 'Cyrillic::ord' if /\A ord \z/oxms;
458 3 50       5 $Ecyrillic::function_ord_ = 'Cyrillic::ord_' if /\A ord \z/oxms;
459 3 50       6 $Ecyrillic::function_reverse = 'Cyrillic::reverse' if /\A reverse \z/oxms;
460 3 50       13 $Ecyrillic::function_getc = 'Cyrillic::getc' if /\A getc \z/oxms;
461             }
462             }
463             }
464             }
465              
466 200         653 $e_script .= Cyrillic::escape();
467              
468 200         1733 return $e_script;
469             }
470              
471             1;
472              
473             __END__