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,
|
2016-11-05 00:58:48 -04:00
|
|
|
# but modified to conform to the Shell & Utilities volume of the IEEE
|
|
|
|
# Std 1003.1-2008, 2016 Edition [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"]
|
|
|
|
#
|
2020-06-16 10:15:51 -04:00
|
|
|
# They treat quotes as special characters, so an unmatched quote will
|
|
|
|
# cause an ArgumentError.
|
2012-09-12 19:22:26 -04:00
|
|
|
#
|
|
|
|
# argv = "they all ran after the farmer's wife".shellsplit
|
2020-06-16 10:23:13 -04:00
|
|
|
# #=> ArgumentError: Unmatched quote: ...
|
2012-09-12 19:22:26 -04:00
|
|
|
#
|
2020-06-16 10:15:51 -04: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-12 19:22:26 -04:00
|
|
|
#
|
2020-06-16 10:15:51 -04:00
|
|
|
# filename = "special's.txt"
|
2012-09-12 19:22:26 -04:00
|
|
|
#
|
2020-06-16 10:15:51 -04: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-03 19:23:24 -04:00
|
|
|
#
|
2012-09-12 19:22:26 -04:00
|
|
|
# Shellwords also comes with a core extension for Array, Array#shelljoin.
|
|
|
|
#
|
2020-06-16 10:15:51 -04:00
|
|
|
# dir = "Funny GIFs"
|
|
|
|
# argv = %W[ls -lta -- #{dir}]
|
|
|
|
# system(argv.shelljoin + " | less")
|
|
|
|
# # runs "ls -lta -- Funny\\ GIFs | less"
|
2012-09-12 19:22:26 -04:00
|
|
|
#
|
2020-06-16 10:15:51 -04:00
|
|
|
# You can use this method to build a complete command line out of an
|
|
|
|
# array of arguments.
|
2012-09-12 19:22:26 -04:00
|
|
|
#
|
|
|
|
# === 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
|
|
|
|
#
|
2016-11-05 00:58:48 -04:00
|
|
|
# 1: {IEEE Std 1003.1-2008, 2016 Edition, the Shell & Utilities volume}[http://pubs.opengroup.org/onlinepubs/9699919799/utilities/contents.html]
|
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|
|
2020-06-16 10:23:13 -04:00
|
|
|
raise ArgumentError, "Unmatched quote: #{line.inspect}" if garbage
|
2016-11-05 00:58:48 -04: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-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 "
|
2020-06-16 10:15:51 -04:00
|
|
|
# open("| grep -Ern -e #{pattern.shellescape} lib") { |grep|
|
2012-11-09 01:28:00 -05:00
|
|
|
# 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.
|
2019-10-16 23:51:29 -04:00
|
|
|
str.gsub!(/[^A-Za-z0-9_\-.,:+\/@\n]/, "\\\\\\&")
|
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
|