1N/A;# shellwords.pl
1N/A;#
1N/A;# Usage:
1N/A;# require 'shellwords.pl';
1N/A;# @words = &shellwords($line);
1N/A;# or
1N/A;# @words = &shellwords(@lines);
1N/A;# or
1N/A;# @words = &shellwords; # defaults to $_ (and clobbers it)
1N/A
1N/Asub shellwords {
1N/A package shellwords;
1N/A local($_) = join('', @_) if @_;
1N/A local(@words,$snippet,$field);
1N/A
1N/A s/^\s+//;
1N/A while ($_ ne '') {
1N/A $field = '';
1N/A for (;;) {
1N/A use re 'taint'; # leave strings tainted
1N/A if (s/^"(([^"\\]|\\.)*)"//) {
1N/A ($snippet = $1) =~ s#\\(.)#$1#g;
1N/A }
1N/A elsif (/^"/) {
1N/A die "Unmatched double quote: $_\n";
1N/A }
1N/A elsif (s/^'(([^'\\]|\\.)*)'//) {
1N/A ($snippet = $1) =~ s#\\(.)#$1#g;
1N/A }
1N/A elsif (/^'/) {
1N/A die "Unmatched single quote: $_\n";
1N/A }
1N/A elsif (s/^\\(.)//) {
1N/A $snippet = $1;
1N/A }
1N/A elsif (s/^([^\s\\'"]+)//) {
1N/A $snippet = $1;
1N/A }
1N/A else {
1N/A s/^\s+//;
1N/A last;
1N/A }
1N/A $field .= $snippet;
1N/A }
1N/A push(@words, $field);
1N/A }
1N/A @words;
1N/A}
1N/A1;