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
|
2001-06-03 15:25:07 -04:00
|
|
|
line = line.sub(/\A\s+/, '')
|
1998-01-16 07:19:09 -05:00
|
|
|
words = []
|
|
|
|
while line != ''
|
|
|
|
field = ''
|
1999-08-13 01:45:20 -04:00
|
|
|
while true
|
2000-08-01 05:25:37 -04:00
|
|
|
if line.sub!(/\A"(([^"\\]|\\.)*)"/, '') then #"
|
1998-01-16 07:19:09 -05:00
|
|
|
snippet = $1
|
2000-02-08 03:54:01 -05:00
|
|
|
snippet.gsub!(/\\(.)/, '\1')
|
2000-08-01 05:25:37 -04:00
|
|
|
elsif line =~ /\A"/ then #"
|
2000-02-08 03:54:01 -05:00
|
|
|
raise ArgumentError, "Unmatched double quote: #{line}"
|
2003-01-19 09:13:01 -05:00
|
|
|
elsif line.sub!(/\A'([^']*)'/, '') then #'
|
1998-01-16 07:19:09 -05:00
|
|
|
snippet = $1
|
2000-08-01 05:25:37 -04:00
|
|
|
elsif line =~ /\A'/ then #'
|
2000-02-08 03:54:01 -05:00
|
|
|
raise ArgumentError, "Unmatched single quote: #{line}"
|
2000-08-01 05:25:37 -04:00
|
|
|
elsif line.sub!(/\A\\(.)/, '') then
|
1998-01-16 07:19:09 -05:00
|
|
|
snippet = $1
|
2000-08-01 05:25:37 -04:00
|
|
|
elsif line.sub!(/\A([^\s\\'"]+)/, '') then #'
|
1998-01-16 07:19:09 -05:00
|
|
|
snippet = $1
|
|
|
|
else
|
2000-08-01 05:25:37 -04:00
|
|
|
line.sub!(/\A\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
|