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