1998-01-16 07:19:09 -05:00
|
|
|
# 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 = ''
|
1999-08-13 01:45:20 -04:00
|
|
|
while true
|
1999-01-19 23:59:39 -05:00
|
|
|
if line.sub! /^"(([^"\\]|\\.)*)"/, '' then #"
|
1998-01-16 07:19:09 -05:00
|
|
|
snippet = $1
|
|
|
|
snippet.gsub! /\\(.)/, '\1'
|
1999-01-19 23:59:39 -05:00
|
|
|
elsif line =~ /^"/ then #"
|
|
|
|
raise ArgError, "Unmatched double quote: #{line}"
|
|
|
|
elsif line.sub! /^'(([^'\\]|\\.)*)'/, '' then #'
|
1998-01-16 07:19:09 -05:00
|
|
|
snippet = $1
|
|
|
|
snippet.gsub! /\\(.)/, '\1'
|
1999-01-19 23:59:39 -05:00
|
|
|
elsif line =~ /^'/ then #'
|
|
|
|
raise ArgError, "Unmatched single quote: #{line}"
|
1998-01-16 07:19:09 -05:00
|
|
|
elsif line.sub! /^\\(.)/, '' then
|
|
|
|
snippet = $1
|
1999-01-19 23:59:39 -05:00
|
|
|
elsif line.sub! /^([^\s\\'"]+)/, '' then #'
|
1998-01-16 07:19:09 -05:00
|
|
|
snippet = $1
|
|
|
|
else
|
|
|
|
line.sub! /^\s+/, ''
|
|
|
|
break
|
|
|
|
end
|
|
|
|
field += snippet
|
|
|
|
end
|
|
|
|
words += field
|
|
|
|
end
|
|
|
|
words
|
|
|
|
end
|
|
|
|
module_function :shellwords
|
|
|
|
end
|