| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
not $force and $text ne "" and not $text =~ /[ \t\n\x0b"]/
not $force and $text ne ""
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"]/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | or return @argv; | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 51 | m/\G\s*/gc; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 51 | ▶ | ARGS: until ( pos == $length ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 71 | my $quote_mode; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 71 | my $arg = ''; | |||||||||||||||||||||||||||||||||||||||||
| 115 | 71 | ▶ | CHARS: until ( pos == $length ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 313 | ▶ | if ( m/\G((?:\\\\)+)(?=\\?(")?)/gc ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 10 | ▶ | if (defined $2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$quote_mode and /\G"/cg
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||