diff options
Diffstat (limited to 'lib/shellwords.pl')
-rw-r--r-- | lib/shellwords.pl | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/shellwords.pl b/lib/shellwords.pl new file mode 100644 index 0000000000..168991fa3f --- /dev/null +++ b/lib/shellwords.pl @@ -0,0 +1,42 @@ +#; shellwords.pl +#; +#; Usage: +#; require 'shellwords.pl'; +#; @words = &shellwords($line); +#; or +#; @words = &shellwords(@lines); +#; or +#; @words = &shellwords; # defaults to $_ (and clobbers it) + +sub shellwords { + package shellwords; + local($_) = join('', @_) if @_; + local(@words,$snippet,$field); + + s/^\s+//; + while ($_ ne '') { + $field = ''; + for (;;) { + if (s/^"(([^"\\]+|\\[\\"])*)"//) { + ($snippet = $1) =~ s#\\(.)#$1#g; + } + elsif (s/^'(([^'\\]+|\\[\\'])*)'//) { + ($snippet = $1) =~ s#\\(.)#$1#g; + } + elsif (s/^\\(.)//) { + $snippet = $1; + } + elsif (s/^([^\s\\'"]+)//) { + $snippet = $1; + } + else { + s/^\s+//; + last; + } + $field .= $snippet; + } + push(@words, $field); + } + @words; +} +1; |