blib/lib/Win32/ShellQuote.pm

filter: Statementstmt 90.4% 66 / 73 Branchbran 81.2% 26 / 32 Conditioncond 66.6% 8 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 87.5% 14 / 16 Podpod 80.0% 8 / 10 totaltotal 84.0% 126 / 150 CC 21 SCAR 18.7
CC
21
Cov
85%
unquote_native
13.0
quote_system
6.0
quote_system_cmd
6.0
CRAP 6.5
↑ Summary
1 package Win32::ShellQuote;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.003001';
8 3 $VERSION = eval $VERSION;
9  
10 3 our @EXPORT_OK = qw(
11 quote_native
12 quote_cmd
13 quote_system_list
14 quote_system_string
15 quote_system
16 quote_system_cmd
17 quote_literal
18 cmd_escape
19 unquote_native
20 cmd_unescape
21 );
22 3 our %EXPORT_TAGS = (all => [@EXPORT_OK]);
23  
24 141 ▶sub quote_native {
Subroutine quote_native: called CC=1 SCAR=0.0
Pod documented
25 141 return join q{ }, quote_system_list(@_);
26 }
27  
28 47 ▶sub quote_cmd {
Subroutine quote_cmd: called CC=1 SCAR=0.0
Pod documented
29 47 return cmd_escape(quote_native(@_));
30 }
31  
32 141 ▶sub quote_system_list {
Subroutine quote_system_list: called CC=1 SCAR=0.0
Pod documented
33 # have to force quoting, or perl might try to use cmd anyway
34 141 return map { quote_literal($_, 1) } @_;
35 }
36  
37 47 ▶sub quote_system_string {
Subroutine quote_system_string: called CC=2 SCAR=6.9
Pod documented
38 47 my $args = quote_native(@_);
39  
40 47 ▶ if (_has_shell_metachars($args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_has_shell_metachars($args)) 11 36 47
41 11 $args = cmd_escape($args);
42 }
43 47 return $args;
44 }
45  
46 0 ▶sub quote_system {
Subroutine quote_system: not called CC=2 SCAR=17.9
Pod documented
47 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 0 0
48 0 return quote_system_list(@_);
49 }
50 else {
51 0 return quote_system_string(@_);
52 }
53 }
54  
55 0 ▶sub quote_system_cmd {
Subroutine quote_system_cmd: not called CC=2 SCAR=17.9
Pod documented
56 # force cmd, even when running through system
57 0 my $args = quote_native(@_);
58  
59 0 ▶ if (! _has_shell_metachars($args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_has_shell_metachars($args)) 0 0 0
60 # IT BURNS LOOK AWAY
61 0 return '%PATH:~0,0%' . cmd_escape($args);
62 }
63 0 return cmd_escape($args);
64 }
65  
66  
67 58 ▶sub cmd_escape {
Subroutine cmd_escape: called CC=2 SCAR=6.9
Pod documented
68 58 my $string = shift;
69 58 ▶ if ($string =~ /[\r\n\0]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /[\r\n\0]/) 6 52 58
70 6 croak "can't quote newlines to pass through cmd.exe";
71 }
72 52 $string =~ s/([()%!^"<>&|])/^$1/g;
73 52 return $string;
74 }
75  
76 144 ▶sub quote_literal {
Subroutine quote_literal: called CC=4 SCAR=15.5
Pod documented
77 144 my ($text, $force) = @_;
78  
79 # basic argument quoting. uses backslashes and quotes to escape
80 # everything.
81 144 ▶ if (!$force && $text ne '' && $text !~ /[ \t\n\x0b"]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $force and $text ne '' and not $text =~ /[ \t\n\x0b"]/) { } 0 144 144
Condition33%2/6
not $force and $text ne "" and not $text =~ /[ \t\n\x0b"]/
!ll&&!rl&&r
14400
not $force and $text ne ""
!ll&&!rl&&r
14400
MC/DC0%0/3
not $force and $text ne "" and not $text =~ /[ \t\n\x0b"]/
not $force $text ne "" not $text =~ /[ \t\n\x0b"]/
Truth table25%1/4
not(A) and B and not(C)
A= not $forceB= $text ne ""C= not $text =~ /[ \t\n\x0b"]/
ABCresult
0XX0
10X0
1100
1111
82 # no quoting needed
83 }
84 else {
85 144 $text =~ s{(\\*)(?="|\z)}{$1$1}g;
86 144 $text =~ s{"}{\\"}g;
87 144 $text = qq{"$text"};
88 }
89  
90 144 return $text;
91 }
92  
93 # derived from rules in code in win32.c
94 47 ▶sub _has_shell_metachars {
Subroutine _has_shell_metachars: called CC=2 SCAR=6.9
95 47 my $string = shift;
96  
97 47 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ /%/ 4 43 47
98 if $string =~ /%/;
99 43 $string =~ s/(['"]).*?(\1|\z)//sg;
100 43 return $string =~ /[<>|]/;
101 }
102  
103 53 ▶sub unquote_native {
Subroutine unquote_native: called CC=13 SCAR=25.7
Pod undocumented
104 53 local ($_) = @_;
105 53 my @argv;
106  
107 53 ▶ my $length = length
Branch: 1 branch
BranchTrueFalseTotal
my $length = length $_ or return @argv 51 2 53
108 or return @argv;
109  
110 51 m/\G\s*/gc;
111  
112 51 ▶ ARGS: until ( pos == $length ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (pos $_ == $length) 71 51 122
113 71 my $quote_mode;
114 71 my $arg = '';
115 71 ▶ CHARS: until ( pos == $length ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (pos $_ == $length) 313 48 361
116 313 ▶ if ( m/\G((?:\\\\)+)(?=\\?(")?)/gc ) {
Branch: 5 branches
BranchTrueFalseTotal
if (/\G((?:\\\\)+)(?=\\?(")?)/cg) { } 10 303 313
elsif (/\G\\"/cg) { } 16 287 303
elsif (/\G"/cg) { } 131 156 287
elsif (not $quote_mode and /\G\s+/cg) { } 23 133 156
elsif (/\G(.)/cgs) { } 133 0 133
117 10 ▶ if (defined $2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) { } 7 3 10
118 7 $arg .= '\\' x (length($1) / 2);
119 }
120 else {
121 3 $arg .= $1;
122 }
123 }
124 elsif ( m/\G\\"/gc ) {
125 16 $arg .= '"';
126 }
127 elsif ( m/\G"/gc ) {
128 131 ▶ if ( $quote_mode && m/\G"/gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quote_mode and /\G"/cg) 14 117 131
Condition100%3/3
$quote_mode and /\G"/cg
!ll&&!rl&&r
734414
MC/DC100%2/2
$quote_mode and /\G"/cg
$quote_mode /\G"/cg
Truth table100%3/3
A and B
A= $quote_modeB= /\G"/cg
ABresult
0X0
100
111
129 14 $arg .= '"';
130 }
131 131 $quote_mode = !$quote_mode;
132 }
133 ▶ elsif ( !$quote_mode && m/\G\s+/gc ) {
Condition100%3/3
not $quote_mode and /\G\s+/cg
!ll&&!rl&&r
726123
MC/DC100%2/2
not $quote_mode and /\G\s+/cg
not $quote_mode /\G\s+/cg
Truth table100%3/3
not(A) and B
A= not $quote_modeB= /\G\s+/cg
ABresult
0X0
100
111
134 23 last;
135 }
136 elsif ( m/\G(.)/sgc ) {
137 133 $arg .= $1;
138 }
139 }
140 71 push @argv, $arg;
141 }
142  
143 51 return @argv;
144 }
145  
146 13 ▶sub cmd_unescape {
Subroutine cmd_unescape: called CC=1 SCAR=0.0
Pod undocumented
147 13 my ($string) = @_;
148  
149 3 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
150 13 $string =~ s/\^(.?)|([^^"]+)|("[^"]*(?:"|\z))/$1$2$3/gs;
151  
152 13 return $string;
153 }
154  
155 3 1;
156  
157 __END__
↑ Summary