2015-12-12 21:43:56 -05:00
|
|
|
# frozen-string-literal: true
|
2011-10-03 19:23:24 -04:00
|
|
|
##
|
2012-09-12 19:22:26 -04:00
|
|
|
# == Manipulates strings like the UNIX Bourne shell
|
2003-01-26 05:11:30 -05:00
|
|
|
#
|
2007-09-11 04:27:48 -04:00
|
|
|
# This module manipulates strings according to the word parsing rules
|
|
|
|
# of the UNIX Bourne shell.
|
2003-01-26 05:11:30 -05:00
|
|
|
#
|
2007-09-11 04:27:48 -04:00
|
|
|
# The shellwords() function was originally a port of shellwords.pl,
|
2012-09-12 19:22:26 -04:00
|
|
|
# but modified to conform to POSIX / SUSv3 (IEEE Std 1003.1-2001 [1]).
|
2003-01-26 05:11:30 -05:00
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# === Usage
|
2003-01-26 05:11:30 -05:00
|
|
|
#
|
2014-09-05 06:00:46 -04:00
|
|
|
# You can use Shellwords to parse a string into a Bourne shell friendly Array.
|
2008-03-21 08:15:06 -04:00
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# require 'shellwords'
|
|
|
|
#
|
|
|
|
# argv = Shellwords.split('three blind "mice"')
|
|
|
|
# argv #=> ["three", "blind", "mice"]
|
|
|
|
#
|
|
|
|
# Once you've required Shellwords, you can use the #split alias
|
|
|
|
# String#shellsplit.
|
|
|
|
#
|
|
|
|
# argv = "see how they run".shellsplit
|
|
|
|
# argv #=> ["see", "how", "they", "run"]
|
|
|
|
#
|
|
|
|
# Be careful you don't leave a quote unmatched.
|
|
|
|
#
|
|
|
|
# argv = "they all ran after the farmer's wife".shellsplit
|
|
|
|
# #=> ArgumentError: Unmatched double quote: ...
|
|
|
|
#
|
2014-09-05 06:00:46 -04:00
|
|
|
# In this case, you might want to use Shellwords.escape, or its alias
|
2012-09-12 19:22:26 -04:00
|
|
|
# String#shellescape.
|
|
|
|
#
|
|
|
|
# This method will escape the String for you to safely use with a Bourne shell.
|
|
|
|
#
|
|
|
|
# argv = Shellwords.escape("special's.txt")
|
2013-12-14 13:26:59 -05:00
|
|
|
# argv #=> "special\\'s.txt"
|
2011-10-03 19:23:24 -04:00
|
|
|
# system("cat " + argv)
|
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# Shellwords also comes with a core extension for Array, Array#shelljoin.
|
|
|
|
#
|
|
|
|
# argv = %w{ls -lta lib}
|
|
|
|
# system(argv.shelljoin)
|
|
|
|
#
|
|
|
|
# You can use this method to create an escaped string out of an array of tokens
|
2014-09-05 06:00:46 -04:00
|
|
|
# separated by a space. In this example we used the literal shortcut for
|
2012-09-12 19:22:26 -04:00
|
|
|
# Array.new.
|
|
|
|
#
|
|
|
|
# === Authors
|
2011-10-03 19:23:24 -04:00
|
|
|
# * Wakou Aoyama
|
|
|
|
# * Akinori MUSHA <knu@iDaemons.org>
|
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# === Contact
|
2011-10-03 19:23:24 -04:00
|
|
|
# * Akinori MUSHA <knu@iDaemons.org> (current maintainer)
|
2012-09-12 19:22:26 -04:00
|
|
|
#
|
|
|
|
# === Resources
|
|
|
|
#
|
|
|
|
# 1: {IEEE Std 1003.1-2004}[http://pubs.opengroup.org/onlinepubs/009695399/toc.htm]
|
2011-10-03 19:23:24 -04:00
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
module Shellwords
|
2007-09-11 04:27:48 -04:00
|
|
|
# Splits a string into an array of tokens in the same way the UNIX
|
|
|
|
# Bourne shell does.
|
|
|
|
#
|
|
|
|
# argv = Shellwords.split('here are "two words"')
|
|
|
|
# argv #=> ["here", "are", "two words"]
|
2003-01-26 05:11:30 -05:00
|
|
|
#
|
2015-12-13 00:19:03 -05:00
|
|
|
# Note, however, that this is not a command line parser. Shell
|
|
|
|
# metacharacters except for the single and double quotes and
|
|
|
|
# backslash are not treated as such.
|
|
|
|
#
|
|
|
|
# argv = Shellwords.split('ruby my_prog.rb | less')
|
|
|
|
# argv #=> ["ruby", "my_prog.rb", "|", "less"]
|
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# String#shellsplit is a shortcut for this function.
|
2003-01-26 05:11:30 -05:00
|
|
|
#
|
2007-09-11 04:27:48 -04:00
|
|
|
# argv = 'here are "two words"'.shellsplit
|
|
|
|
# argv #=> ["here", "are", "two words"]
|
|
|
|
def shellsplit(line)
|
1998-01-16 07:19:09 -05:00
|
|
|
words = []
|
2015-12-12 21:43:56 -05:00
|
|
|
field = String.new
|
2005-11-10 07:05:57 -05:00
|
|
|
line.scan(/\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m) do
|
|
|
|
|word, sq, dq, esc, garbage, sep|
|
|
|
|
raise ArgumentError, "Unmatched double quote: #{line.inspect}" if garbage
|
2012-01-12 13:42:03 -05:00
|
|
|
field << (word || sq || (dq || esc).gsub(/\\(.)/, '\\1'))
|
2005-11-08 18:41:40 -05:00
|
|
|
if sep
|
|
|
|
words << field
|
2015-12-12 21:43:56 -05:00
|
|
|
field = String.new
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
words
|
|
|
|
end
|
2003-01-26 05:11:30 -05:00
|
|
|
|
2007-09-11 04:27:48 -04:00
|
|
|
alias shellwords shellsplit
|
|
|
|
|
|
|
|
module_function :shellsplit, :shellwords
|
|
|
|
|
|
|
|
class << self
|
|
|
|
alias split shellsplit
|
|
|
|
end
|
|
|
|
|
|
|
|
# Escapes a string so that it can be safely used in a Bourne shell
|
2012-01-12 13:49:30 -05:00
|
|
|
# command line. +str+ can be a non-string object that responds to
|
|
|
|
# +to_s+.
|
2007-09-11 04:27:48 -04:00
|
|
|
#
|
|
|
|
# Note that a resulted string should be used unquoted and is not
|
|
|
|
# intended for use in double quotes nor in single quotes.
|
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# argv = Shellwords.escape("It's better to give than to receive")
|
|
|
|
# argv #=> "It\\'s\\ better\\ to\\ give\\ than\\ to\\ receive"
|
2007-09-11 04:27:48 -04:00
|
|
|
#
|
2012-01-12 13:49:27 -05:00
|
|
|
# String#shellescape is a shorthand for this function.
|
2007-09-11 04:27:48 -04:00
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# argv = "It's better to give than to receive".shellescape
|
|
|
|
# argv #=> "It\\'s\\ better\\ to\\ give\\ than\\ to\\ receive"
|
2007-09-11 04:27:48 -04:00
|
|
|
#
|
2012-11-09 01:28:00 -05:00
|
|
|
# # Search files in lib for method definitions
|
|
|
|
# pattern = "^[ \t]*def "
|
|
|
|
# open("| grep -Ern #{pattern.shellescape} lib") { |grep|
|
|
|
|
# grep.each_line { |line|
|
|
|
|
# file, lineno, matched_line = line.split(':', 3)
|
|
|
|
# # ...
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# It is the caller's responsibility to encode the string in the right
|
2011-12-31 22:26:20 -05:00
|
|
|
# encoding for the shell environment where this string is used.
|
2012-09-12 19:22:26 -04:00
|
|
|
#
|
2014-09-05 06:00:46 -04:00
|
|
|
# Multibyte characters are treated as multibyte characters, not as bytes.
|
2012-09-12 19:22:26 -04:00
|
|
|
#
|
|
|
|
# Returns an empty quoted String if +str+ has a length of zero.
|
2007-09-11 04:27:48 -04:00
|
|
|
def shellescape(str)
|
2012-01-12 13:49:30 -05:00
|
|
|
str = str.to_s
|
|
|
|
|
2007-09-11 04:27:48 -04:00
|
|
|
# An empty argument will be skipped, so return empty quotes.
|
2015-12-12 22:17:28 -05:00
|
|
|
return "''".dup if str.empty?
|
2007-09-11 04:27:48 -04:00
|
|
|
|
|
|
|
str = str.dup
|
|
|
|
|
2014-09-05 06:00:46 -04:00
|
|
|
# Treat multibyte characters as is. It is the caller's responsibility
|
2011-12-31 22:26:20 -05:00
|
|
|
# to encode the string in the right encoding for the shell
|
|
|
|
# environment.
|
|
|
|
str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/, "\\\\\\1")
|
2007-09-11 04:27:48 -04:00
|
|
|
|
|
|
|
# A LF cannot be escaped with a backslash because a backslash + LF
|
2014-09-05 06:00:46 -04:00
|
|
|
# combo is regarded as a line continuation and simply ignored.
|
2007-09-11 04:27:48 -04:00
|
|
|
str.gsub!(/\n/, "'\n'")
|
|
|
|
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
|
|
|
module_function :shellescape
|
|
|
|
|
|
|
|
class << self
|
2008-01-14 02:14:14 -05:00
|
|
|
alias escape shellescape
|
2007-09-11 04:27:48 -04:00
|
|
|
end
|
|
|
|
|
2012-09-12 19:22:26 -04:00
|
|
|
# Builds a command line string from an argument list, +array+.
|
2007-09-11 04:27:48 -04:00
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# All elements are joined into a single string with fields separated by a
|
2014-09-05 06:00:46 -04:00
|
|
|
# space, where each element is escaped for the Bourne shell and stringified
|
|
|
|
# using +to_s+.
|
2007-09-11 04:27:48 -04:00
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# ary = ["There's", "a", "time", "and", "place", "for", "everything"]
|
|
|
|
# argv = Shellwords.join(ary)
|
|
|
|
# argv #=> "There\\'s a time and place for everything"
|
2007-09-11 04:27:48 -04:00
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# Array#shelljoin is a shortcut for this function.
|
2007-09-11 04:27:48 -04:00
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# ary = ["Don't", "rock", "the", "boat"]
|
|
|
|
# argv = ary.shelljoin
|
|
|
|
# argv #=> "Don\\'t rock the boat"
|
2012-01-12 13:49:30 -05:00
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# You can also mix non-string objects in the elements as allowed in Array#join.
|
|
|
|
#
|
2012-11-09 01:25:02 -05:00
|
|
|
# output = `#{['ps', '-p', $$].shelljoin}`
|
2012-01-12 13:49:30 -05:00
|
|
|
#
|
2007-09-11 04:27:48 -04:00
|
|
|
def shelljoin(array)
|
|
|
|
array.map { |arg| shellescape(arg) }.join(' ')
|
|
|
|
end
|
|
|
|
|
|
|
|
module_function :shelljoin
|
|
|
|
|
|
|
|
class << self
|
|
|
|
alias join shelljoin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class String
|
|
|
|
# call-seq:
|
|
|
|
# str.shellsplit => array
|
|
|
|
#
|
|
|
|
# Splits +str+ into an array of tokens in the same way the UNIX
|
2012-09-12 19:22:26 -04:00
|
|
|
# Bourne shell does.
|
|
|
|
#
|
|
|
|
# See Shellwords.shellsplit for details.
|
2007-09-11 04:27:48 -04:00
|
|
|
def shellsplit
|
|
|
|
Shellwords.split(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
|
|
|
# str.shellescape => string
|
|
|
|
#
|
|
|
|
# Escapes +str+ so that it can be safely used in a Bourne shell
|
2012-09-12 19:22:26 -04:00
|
|
|
# command line.
|
|
|
|
#
|
|
|
|
# See Shellwords.shellescape for details.
|
2007-09-11 04:27:48 -04:00
|
|
|
def shellescape
|
|
|
|
Shellwords.escape(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Array
|
|
|
|
# call-seq:
|
|
|
|
# array.shelljoin => string
|
|
|
|
#
|
|
|
|
# Builds a command line string from an argument list +array+ joining
|
2014-09-05 06:00:46 -04:00
|
|
|
# all elements escaped for the Bourne shell and separated by a space.
|
2012-09-12 19:22:26 -04:00
|
|
|
#
|
|
|
|
# See Shellwords.shelljoin for details.
|
2007-09-11 04:27:48 -04:00
|
|
|
def shelljoin
|
|
|
|
Shellwords.join(self)
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|