blob: 9154cd84c16203720904bf3cec1bb961a70de9bc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# shellwords.rb
# original is shellwords.pl
#
# Usage:
# require 'shellwords.rb'
# words = Shellwords.shellwords(line)
#
# or
#
# include Shellwords
# words = shellwords(line)
module Shellwords
def shellwords(line)
return '' unless line
line.sub! /^\s+/, ''
words = []
while line != ''
field = ''
while TRUE
if line.sub! /^"(([^"\\]|\\.)*)"/, '' then #"
snippet = $1
snippet.gsub! /\\(.)/, '\1'
elsif line =~ /^"/ then #"
raise ArgError, "Unmatched double quote: #{line}"
elsif line.sub! /^'(([^'\\]|\\.)*)'/, '' then #'
snippet = $1
snippet.gsub! /\\(.)/, '\1'
elsif line =~ /^'/ then #'
raise ArgError, "Unmatched single quote: #{line}"
elsif line.sub! /^\\(.)/, '' then
snippet = $1
elsif line.sub! /^([^\s\\'"]+)/, '' then #'
snippet = $1
else
line.sub! /^\s+/, ''
break
end
field += snippet
end
words += field
end
words
end
module_function :shellwords
end
|