1998-01-16 07:19:09 -05:00
|
|
|
# shellwords.rb
|
|
|
|
# original is shellwords.pl
|
|
|
|
#
|
|
|
|
# Usage:
|
2000-02-08 03:54:01 -05:00
|
|
|
# require 'shellwords'
|
1998-01-16 07:19:09 -05:00
|
|
|
# words = Shellwords.shellwords(line)
|
|
|
|
#
|
|
|
|
# or
|
|
|
|
#
|
2000-02-08 03:54:01 -05:00
|
|
|
# require 'shellwords'
|
1998-01-16 07:19:09 -05:00
|
|
|
# include Shellwords
|
|
|
|
# words = shellwords(line)
|
|
|
|
|
|
|
|
module Shellwords
|
|
|
|
def shellwords(line)
|
2000-02-08 03:54:01 -05:00
|
|
|
unless line.kind_of?(String)
|
|
|
|
raise ArgumentError, "Argument must be String class object."
|
|
|
|
end
|
|
|
|
line.sub!(/^\s+/, '')
|
1998-01-16 07:19:09 -05:00
|
|
|
words = []
|
|
|
|
while line != ''
|
|
|
|
field = ''
|
1999-08-13 01:45:20 -04:00
|
|
|
while true
|
2000-02-08 03:54:01 -05:00
|
|
|
if line.sub!(/^"(([^"\\]|\\.)*)"/, '') then #"
|
1998-01-16 07:19:09 -05:00
|
|
|
snippet = $1
|
2000-02-08 03:54:01 -05:00
|
|
|
snippet.gsub!(/\\(.)/, '\1')
|
1999-01-19 23:59:39 -05:00
|
|
|
elsif line =~ /^"/ then #"
|
2000-02-08 03:54:01 -05:00
|
|
|
raise ArgumentError, "Unmatched double quote: #{line}"
|
|
|
|
elsif line.sub!(/^'(([^'\\]|\\.)*)'/, '') then #'
|
1998-01-16 07:19:09 -05:00
|
|
|
snippet = $1
|
2000-02-08 03:54:01 -05:00
|
|
|
snippet.gsub!(/\\(.)/, '\1')
|
1999-01-19 23:59:39 -05:00
|
|
|
elsif line =~ /^'/ then #'
|
2000-02-08 03:54:01 -05:00
|
|
|
raise ArgumentError, "Unmatched single quote: #{line}"
|
|
|
|
elsif line.sub!(/^\\(.)/, '') then
|
1998-01-16 07:19:09 -05:00
|
|
|
snippet = $1
|
2000-02-08 03:54:01 -05:00
|
|
|
elsif line.sub!(/^([^\s\\'"]+)/, '') then #'
|
1998-01-16 07:19:09 -05:00
|
|
|
snippet = $1
|
|
|
|
else
|
2000-02-08 03:54:01 -05:00
|
|
|
line.sub!(/^\s+/, '')
|
1998-01-16 07:19:09 -05:00
|
|
|
break
|
|
|
|
end
|
2000-02-08 03:54:01 -05:00
|
|
|
field.concat(snippet)
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
2000-02-08 03:54:01 -05:00
|
|
|
words.push(field)
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
words
|
|
|
|
end
|
|
|
|
module_function :shellwords
|
|
|
|
end
|