2015-12-13 05:43:56 +03:00
|
|
|
# frozen-string-literal: true
|
2011-10-04 03:23:24 +04:00
|
|
|
##
|
2012-09-13 03:22:26 +04:00
|
|
|
# == Manipulates strings like the UNIX Bourne shell
|
2003-01-26 13:11:30 +03:00
|
|
|
#
|
2007-09-11 12:27:48 +04:00
|
|
|
# This module manipulates strings according to the word parsing rules
|
|
|
|
# of the UNIX Bourne shell.
|
2003-01-26 13:11:30 +03:00
|
|
|
#
|
2007-09-11 12:27:48 +04:00
|
|
|
# The shellwords() function was originally a port of shellwords.pl,
|
2016-11-05 07:58:48 +03:00
|
|
|
# but modified to conform to the Shell & Utilities volume of the IEEE
|
|
|
|
# Std 1003.1-2008, 2016 Edition [1].
|
2003-01-26 13:11:30 +03:00
|
|
|
#
|
2012-09-13 03:22:26 +04:00
|
|
|
# === Usage
|
2003-01-26 13:11:30 +03:00
|
|
|
#
|
2014-09-05 14:00:46 +04:00
|
|
|
# You can use Shellwords to parse a string into a Bourne shell friendly Array.
|
2008-03-21 15:15:06 +03:00
|
|
|
#
|
2012-09-13 03: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"]
|
|
|
|
#
|
2020-06-16 17:15:51 +03:00
|
|
|
# They treat quotes as special characters, so an unmatched quote will
|
|
|
|
# cause an ArgumentError.
|
2012-09-13 03:22:26 +04:00
|
|
|
#
|
|
|
|
# argv = "they all ran after the farmer's wife".shellsplit
|
2020-06-16 17:23:13 +03:00
|
|
|
# #=> ArgumentError: Unmatched quote: ...
|
2012-09-13 03:22:26 +04:00
|
|
|
#
|
2020-06-16 17:15:51 +03:00
|
|
|
# Shellwords also provides methods that do the opposite.
|
|
|
|
# Shellwords.escape, or its alias, String#shellescape, escapes
|
|
|
|
# shell metacharacters in a string for use in a command line.
|
2012-09-13 03:22:26 +04:00
|
|
|
#
|
2020-06-16 17:15:51 +03:00
|
|
|
# filename = "special's.txt"
|
2012-09-13 03:22:26 +04:00
|
|
|
#
|
2020-06-16 17:15:51 +03:00
|
|
|
# system("cat -- #{filename.shellescape}")
|
|
|
|
# # runs "cat -- special\\'s.txt"
|
|
|
|
#
|
|
|
|
# Note the '--'. Without it, cat(1) will treat the following argument
|
|
|
|
# as a command line option if it starts with '-'. It is guaranteed
|
|
|
|
# that Shellwords.escape converts a string to a form that a Bourne
|
|
|
|
# shell will parse back to the original string, but it is the
|
|
|
|
# programmer's responsibility to make sure that passing an arbitrary
|
|
|
|
# argument to a command does no harm.
|
2011-10-04 03:23:24 +04:00
|
|
|
#
|
2012-09-13 03:22:26 +04:00
|
|
|
# Shellwords also comes with a core extension for Array, Array#shelljoin.
|
|
|
|
#
|
2020-06-16 17:15:51 +03:00
|
|
|
# dir = "Funny GIFs"
|
|
|
|
# argv = %W[ls -lta -- #{dir}]
|
|
|
|
# system(argv.shelljoin + " | less")
|
|
|
|
# # runs "ls -lta -- Funny\\ GIFs | less"
|
2012-09-13 03:22:26 +04:00
|
|
|
#
|
2020-06-16 17:15:51 +03:00
|
|
|
# You can use this method to build a complete command line out of an
|
|
|
|
# array of arguments.
|
2012-09-13 03:22:26 +04:00
|
|
|
#
|
|
|
|
# === Authors
|
2011-10-04 03:23:24 +04:00
|
|
|
# * Wakou Aoyama
|
|
|
|
# * Akinori MUSHA <knu@iDaemons.org>
|
|
|
|
#
|
2012-09-13 03:22:26 +04:00
|
|
|
# === Contact
|
2011-10-04 03:23:24 +04:00
|
|
|
# * Akinori MUSHA <knu@iDaemons.org> (current maintainer)
|
2012-09-13 03:22:26 +04:00
|
|
|
#
|
|
|
|
# === Resources
|
|
|
|
#
|
2016-11-05 07:58:48 +03:00
|
|
|
# 1: {IEEE Std 1003.1-2008, 2016 Edition, the Shell & Utilities volume}[http://pubs.opengroup.org/onlinepubs/9699919799/utilities/contents.html]
|
2011-10-04 03:23:24 +04:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
module Shellwords
|
2007-09-11 12: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 13:11:30 +03:00
|
|
|
#
|
2015-12-13 08:19:03 +03: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-13 03:22:26 +04:00
|
|
|
# String#shellsplit is a shortcut for this function.
|
2003-01-26 13:11:30 +03:00
|
|
|
#
|
2007-09-11 12:27:48 +04:00
|
|
|
# argv = 'here are "two words"'.shellsplit
|
|
|
|
# argv #=> ["here", "are", "two words"]
|
|
|
|
def shellsplit(line)
|
1998-01-16 15:19:09 +03:00
|
|
|
words = []
|
2015-12-13 05:43:56 +03:00
|
|
|
field = String.new
|
2005-11-10 15:05:57 +03:00
|
|
|
line.scan(/\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m) do
|
|
|
|
|word, sq, dq, esc, garbage, sep|
|
2020-06-16 17:23:13 +03:00
|
|
|
raise ArgumentError, "Unmatched quote: #{line.inspect}" if garbage
|
2016-11-05 07:58:48 +03:00
|
|
|
# 2.2.3 Double-Quotes:
|
|
|
|
#
|
|
|
|
# The <backslash> shall retain its special meaning as an
|
|
|
|
# escape character only when followed by one of the following
|
|
|
|
# characters when considered special:
|
|
|
|
#
|
|
|
|
# $ ` " \ <newline>
|
|
|
|
field << (word || sq || (dq && dq.gsub(/\\([$`"\\\n])/, '\\1')) || esc.gsub(/\\(.)/, '\\1'))
|
2005-11-09 02:41:40 +03:00
|
|
|
if sep
|
|
|
|
words << field
|
2015-12-13 05:43:56 +03:00
|
|
|
field = String.new
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
words
|
|
|
|
end
|
2003-01-26 13:11:30 +03:00
|
|
|
|
2007-09-11 12: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 22:49:30 +04:00
|
|
|
# command line. +str+ can be a non-string object that responds to
|
|
|
|
# +to_s+.
|
2007-09-11 12: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-13 03: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 12:27:48 +04:00
|
|
|
#
|
2012-01-12 22:49:27 +04:00
|
|
|
# String#shellescape is a shorthand for this function.
|
2007-09-11 12:27:48 +04:00
|
|
|
#
|
2012-09-13 03: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 12:27:48 +04:00
|
|
|
#
|
2012-11-09 10:28:00 +04:00
|
|
|
# # Search files in lib for method definitions
|
|
|
|
# pattern = "^[ \t]*def "
|
2020-06-16 17:15:51 +03:00
|
|
|
# open("| grep -Ern -e #{pattern.shellescape} lib") { |grep|
|
2012-11-09 10:28:00 +04:00
|
|
|
# grep.each_line { |line|
|
|
|
|
# file, lineno, matched_line = line.split(':', 3)
|
|
|
|
# # ...
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
#
|
2012-09-13 03:22:26 +04:00
|
|
|
# It is the caller's responsibility to encode the string in the right
|
2012-01-01 07:26:20 +04:00
|
|
|
# encoding for the shell environment where this string is used.
|
2012-09-13 03:22:26 +04:00
|
|
|
#
|
2014-09-05 14:00:46 +04:00
|
|
|
# Multibyte characters are treated as multibyte characters, not as bytes.
|
2012-09-13 03:22:26 +04:00
|
|
|
#
|
|
|
|
# Returns an empty quoted String if +str+ has a length of zero.
|
2007-09-11 12:27:48 +04:00
|
|
|
def shellescape(str)
|
2012-01-12 22:49:30 +04:00
|
|
|
str = str.to_s
|
|
|
|
|
2007-09-11 12:27:48 +04:00
|
|
|
# An empty argument will be skipped, so return empty quotes.
|
2015-12-13 06:17:28 +03:00
|
|
|
return "''".dup if str.empty?
|
2007-09-11 12:27:48 +04:00
|
|
|
|
|
|
|
str = str.dup
|
|
|
|
|
2014-09-05 14:00:46 +04:00
|
|
|
# Treat multibyte characters as is. It is the caller's responsibility
|
2012-01-01 07:26:20 +04:00
|
|
|
# to encode the string in the right encoding for the shell
|
|
|
|
# environment.
|
2019-10-17 06:51:29 +03:00
|
|
|
str.gsub!(/[^A-Za-z0-9_\-.,:+\/@\n]/, "\\\\\\&")
|
2007-09-11 12:27:48 +04:00
|
|
|
|
|
|
|
# A LF cannot be escaped with a backslash because a backslash + LF
|
2014-09-05 14:00:46 +04:00
|
|
|
# combo is regarded as a line continuation and simply ignored.
|
2007-09-11 12:27:48 +04:00
|
|
|
str.gsub!(/\n/, "'\n'")
|
|
|
|
|
|
|
|
return str
|
|
|
|
end
|
|
|
|
|
|
|
|
module_function :shellescape
|
|
|
|
|
|
|
|
class << self
|
2008-01-14 10:14:14 +03:00
|
|
|
alias escape shellescape
|
2007-09-11 12:27:48 +04:00
|
|
|
end
|
|
|
|
|
2012-09-13 03:22:26 +04:00
|
|
|
# Builds a command line string from an argument list, +array+.
|
2007-09-11 12:27:48 +04:00
|
|
|
#
|
2012-09-13 03:22:26 +04:00
|
|
|
# All elements are joined into a single string with fields separated by a
|
2014-09-05 14:00:46 +04:00
|
|
|
# space, where each element is escaped for the Bourne shell and stringified
|
|
|
|
# using +to_s+.
|
2007-09-11 12:27:48 +04:00
|
|
|
#
|
2012-09-13 03: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 12:27:48 +04:00
|
|
|
#
|
2012-09-13 03:22:26 +04:00
|
|
|
# Array#shelljoin is a shortcut for this function.
|
2007-09-11 12:27:48 +04:00
|
|
|
#
|
2012-09-13 03:22:26 +04:00
|
|
|
# ary = ["Don't", "rock", "the", "boat"]
|
|
|
|
# argv = ary.shelljoin
|
|
|
|
# argv #=> "Don\\'t rock the boat"
|
2012-01-12 22:49:30 +04:00
|
|
|
#
|
2012-09-13 03:22:26 +04:00
|
|
|
# You can also mix non-string objects in the elements as allowed in Array#join.
|
|
|
|
#
|
2012-11-09 10:25:02 +04:00
|
|
|
# output = `#{['ps', '-p', $$].shelljoin}`
|
2012-01-12 22:49:30 +04:00
|
|
|
#
|
2007-09-11 12: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-13 03:22:26 +04:00
|
|
|
# Bourne shell does.
|
|
|
|
#
|
|
|
|
# See Shellwords.shellsplit for details.
|
2007-09-11 12: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-13 03:22:26 +04:00
|
|
|
# command line.
|
|
|
|
#
|
|
|
|
# See Shellwords.shellescape for details.
|
2007-09-11 12: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 14:00:46 +04:00
|
|
|
# all elements escaped for the Bourne shell and separated by a space.
|
2012-09-13 03:22:26 +04:00
|
|
|
#
|
|
|
|
# See Shellwords.shelljoin for details.
|
2007-09-11 12:27:48 +04:00
|
|
|
def shelljoin
|
|
|
|
Shellwords.join(self)
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|