| 1 | package Win32::ParseWords; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | $Win32::ParseWords::VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | require Exporter; | |||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | our %EXPORT_TAGS = (); | |||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | our @EXPORT = qw(shellwords quotewords nested_quotewords parse_line); | |||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | our @EXPORT_OK = qw(old_shellwords set_perl_squote); | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | my $PERL_SINGLE_QUOTE = 0; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | sub set_perl_squote { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set_perl_squote: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | $PERL_SINGLE_QUOTE = $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | ▶ | sub shellwords { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
shellwords: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | my (@lines) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | my @allwords; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | foreach my $line (@lines) { | |||||||||||||||||||||||||||||||||||||||||
| 25 | 6 | $line =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 6 | my @words = parse_line('\s+', 0, $line); | |||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | ▶ | pop @words if (@words and !defined $words[-1]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@words and not defined $words[-1]
MC/DC100%2/2
@words and not defined $words[-1]
@words
not defined $words[-1]
Truth table100%3/3
A and not(B)
A= @wordsB= not defined $words[-1]
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 6 | ▶ | return() unless (@words || !length($line)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@words or not length $line
MC/DC50%1/2
@words or not length $line
@words
not length $line
Truth table66%2/3
A or not(B)
A= @wordsB= not length $line
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 4 | push(@allwords, @words); | |||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 2 | return(@allwords); | |||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 4 | ▶ | sub quotewords { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
quotewords: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | my($delim, $keep, @lines) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 4 | my($line, @words, @allwords); | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 4 | foreach $line (@lines) { | |||||||||||||||||||||||||||||||||||||||||
| 39 | 4 | @words = parse_line($delim, $keep, $line); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 4 | ▶ | return() unless (@words || !length($line)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@words or not length $line
MC/DC50%1/2
@words or not length $line
@words
not length $line
Truth table66%2/3
A or not(B)
A= @wordsB= not length $line
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | push(@allwords, @words); | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | return(@allwords); | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | sub nested_quotewords { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
nested_quotewords: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | my($delim, $keep, @lines) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | my($i, @allwords); | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 2 | ▶ | for ($i = 0; $i < @lines; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | @{$allwords[$i]} = parse_line($delim, $keep, $lines[$i]); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | ▶ | return() unless (@{$allwords[$i]} || !length($lines[$i])); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@{$allwords[$i];} or not length $lines[$i]
MC/DC50%1/2
@{$allwords[$i];} or not length $lines[$i]
@{$allwords[$i];}
not length $lines[$i]
Truth table66%2/3
A or not(B)
A= @{$allwords[$i];}B= not length $lines[$i]
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | return(@allwords); | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 25 | ▶ | sub parse_line { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_line: called CC=16 SCAR=27.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 25 | my($delimiter, $keep, $line) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 25 | my($word, @pieces); | |||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | no warnings 'uninitialized'; # we will be testing undef strings | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 25 | ▶ | while (length($line)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | # This pattern is optimised to be stack conservative on older perls. | ||||||||||||||||||||||||||||||||||||||||||
| 65 | # Do not refactor without being careful and testing it on very long strings. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # See Perl bug #42980 for an example of a stack busting input. | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 105 | ▶ | $line =~ s/^ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | (?: | ||||||||||||||||||||||||||||||||||||||||||
| 69 | # double quoted string | ||||||||||||||||||||||||||||||||||||||||||
| 70 | (") # $quote | ||||||||||||||||||||||||||||||||||||||||||
| 71 | ((?>[^\^"]*(?:\^.[^\^"]*)*))" # $quoted | ||||||||||||||||||||||||||||||||||||||||||
| 72 | | # --OR-- | ||||||||||||||||||||||||||||||||||||||||||
| 73 | # singe quoted string | ||||||||||||||||||||||||||||||||||||||||||
| 74 | (') # $quote | ||||||||||||||||||||||||||||||||||||||||||
| 75 | ((?>[^\^']*(?:\^.[^\^']*)*))' # $quoted | ||||||||||||||||||||||||||||||||||||||||||
| 76 | | # --OR-- | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # unquoted string | ||||||||||||||||||||||||||||||||||||||||||
| 78 | ( # $unquoted | ||||||||||||||||||||||||||||||||||||||||||
| 79 | (?:\^.|[^\^"'])*? | ||||||||||||||||||||||||||||||||||||||||||
| 80 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # followed by | ||||||||||||||||||||||||||||||||||||||||||
| 82 | ( # $delim | ||||||||||||||||||||||||||||||||||||||||||
| 83 | \Z(?!\n) # EOL | ||||||||||||||||||||||||||||||||||||||||||
| 84 | | # --OR-- | ||||||||||||||||||||||||||||||||||||||||||
| 85 | (?-x:$delimiter) # delimiter | ||||||||||||||||||||||||||||||||||||||||||
| 86 | | # --OR-- | ||||||||||||||||||||||||||||||||||||||||||
| 87 | (?!^)(?=["']) # a quote | ||||||||||||||||||||||||||||||||||||||||||
| 88 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 89 | )//xs or return; # extended layout | ||||||||||||||||||||||||||||||||||||||||||
| 90 | 100 | ▶ | my ($quote, $quoted, $unquoted, $delim) = (($1 ? ($1,$2) : ($3,$4)), $5, $6); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 100 | ▶ | return() unless( defined($quote) || length($unquoted) || length($delim)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $quote or length $unquoted or length $delim
defined $quote or length $unquoted
MC/DC0%0/3
defined $quote or length $unquoted or length $delim
defined $quote
length $unquoted
length $delim
Truth table75%3/4
A or B or C
A= defined $quoteB= length $unquotedC= length $delim
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 100 | ▶ | if ($keep) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 27 | $quoted = "$quote$quoted$quote"; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 73 | $unquoted =~ s/\^(.)/$1/sg; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 73 | ▶ | if (defined $quote) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 17 | ▶ | $quoted =~ s/\^(.)/$1/sg if ($quote eq '"'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 17 | ▶ | $quoted =~ s/\^([\^'])/$1/g if ( $PERL_SINGLE_QUOTE && $quote eq "'"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$PERL_SINGLE_QUOTE and $quote eq "'"
MC/DC100%2/2
$PERL_SINGLE_QUOTE and $quote eq "'"
$PERL_SINGLE_QUOTE
$quote eq "'"
Truth table100%3/3
A and B
A= $PERL_SINGLE_QUOTEB= $quote eq "'"
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 100 | $word .= substr($line, 0, 0); # leave results tainted | |||||||||||||||||||||||||||||||||||||||||
| 105 | 100 | ▶ | $word .= defined $quote ? $quoted : $unquoted; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | 100 | ▶ | if (length($delim)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 55 | push(@pieces, $word); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 55 | ▶ | push(@pieces, $delim) if ($keep eq 'delimiters'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 55 | undef $word; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 100 | ▶ | if (!length($line)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | 20 | push(@pieces, $word); | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 20 | return(@pieces); | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub old_shellwords { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
old_shellwords: not called CC=9 SCAR=42.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | # Usage: | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # use ParseWords; | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # @words = old_shellwords($line); | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # or | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # @words = old_shellwords(@lines); | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # or | ||||||||||||||||||||||||||||||||||||||||||
| 126 | # @words = old_shellwords(); # defaults to $_ (and clobbers it) | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | ▶ | no warnings 'uninitialized'; # we will be testing undef strings | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | local *_ = \join('', @_) if @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | my (@words, $snippet); | |||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | while ($_ ne '') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | my $field = substr($_, 0, 0); # leave results tainted | |||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | for (;;) { | |||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | if (s/\A"(([^"\^]|\^.)*)"//s) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ($snippet = $1) =~ s#\^(.)#$1#sg; | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | elsif (/\A"/) { | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | Carp::carp("Unmatched double quote: $_"); | |||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | return(); | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | elsif (s/\A'(([^'\^]|\^.)*)'//s) { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ($snippet = $1) =~ s#\^(.)#$1#sg; | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | elsif (/\A'/) { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | Carp::carp("Unmatched single quote: $_"); | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | return(); | |||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | elsif (s/\A\^(.?)//s) { | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | $snippet = $1; | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | elsif (s/\A([^\s\^'"]+)//) { | ||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | $snippet = $1; | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | s/\A\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | $field .= $snippet; | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | push(@words, $field); | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | return @words; | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||||||||||||||||||||||||