Commit | Line | Data |
9ef589d8 |
1 | #; shellwords.pl |
2 | #; |
3 | #; Usage: |
4 | #; require 'shellwords.pl'; |
5 | #; @words = &shellwords($line); |
6 | #; or |
7 | #; @words = &shellwords(@lines); |
8 | #; or |
9 | #; @words = &shellwords; # defaults to $_ (and clobbers it) |
10 | |
11 | sub shellwords { |
12 | package shellwords; |
13 | local($_) = join('', @_) if @_; |
14 | local(@words,$snippet,$field); |
15 | |
16 | s/^\s+//; |
17 | while ($_ ne '') { |
18 | $field = ''; |
19 | for (;;) { |
20 | if (s/^"(([^"\\]+|\\[\\"])*)"//) { |
21 | ($snippet = $1) =~ s#\\(.)#$1#g; |
22 | } |
23 | elsif (s/^'(([^'\\]+|\\[\\'])*)'//) { |
24 | ($snippet = $1) =~ s#\\(.)#$1#g; |
25 | } |
26 | elsif (s/^\\(.)//) { |
27 | $snippet = $1; |
28 | } |
29 | elsif (s/^([^\s\\'"]+)//) { |
30 | $snippet = $1; |
31 | } |
32 | else { |
33 | s/^\s+//; |
34 | last; |
35 | } |
36 | $field .= $snippet; |
37 | } |
38 | push(@words, $field); |
39 | } |
40 | @words; |
41 | } |
42 | 1; |