Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby27/share/ruby
File: shellwords.rb
# frozen-string-literal: true
[0] Fix | Delete
##
[1] Fix | Delete
# == Manipulates strings like the UNIX Bourne shell
[2] Fix | Delete
#
[3] Fix | Delete
# This module manipulates strings according to the word parsing rules
[4] Fix | Delete
# of the UNIX Bourne shell.
[5] Fix | Delete
#
[6] Fix | Delete
# The shellwords() function was originally a port of shellwords.pl,
[7] Fix | Delete
# but modified to conform to the Shell & Utilities volume of the IEEE
[8] Fix | Delete
# Std 1003.1-2008, 2016 Edition [1].
[9] Fix | Delete
#
[10] Fix | Delete
# === Usage
[11] Fix | Delete
#
[12] Fix | Delete
# You can use Shellwords to parse a string into a Bourne shell friendly Array.
[13] Fix | Delete
#
[14] Fix | Delete
# require 'shellwords'
[15] Fix | Delete
#
[16] Fix | Delete
# argv = Shellwords.split('three blind "mice"')
[17] Fix | Delete
# argv #=> ["three", "blind", "mice"]
[18] Fix | Delete
#
[19] Fix | Delete
# Once you've required Shellwords, you can use the #split alias
[20] Fix | Delete
# String#shellsplit.
[21] Fix | Delete
#
[22] Fix | Delete
# argv = "see how they run".shellsplit
[23] Fix | Delete
# argv #=> ["see", "how", "they", "run"]
[24] Fix | Delete
#
[25] Fix | Delete
# Be careful you don't leave a quote unmatched.
[26] Fix | Delete
#
[27] Fix | Delete
# argv = "they all ran after the farmer's wife".shellsplit
[28] Fix | Delete
# #=> ArgumentError: Unmatched double quote: ...
[29] Fix | Delete
#
[30] Fix | Delete
# In this case, you might want to use Shellwords.escape, or its alias
[31] Fix | Delete
# String#shellescape.
[32] Fix | Delete
#
[33] Fix | Delete
# This method will escape the String for you to safely use with a Bourne shell.
[34] Fix | Delete
#
[35] Fix | Delete
# argv = Shellwords.escape("special's.txt")
[36] Fix | Delete
# argv #=> "special\\'s.txt"
[37] Fix | Delete
# system("cat " + argv)
[38] Fix | Delete
#
[39] Fix | Delete
# Shellwords also comes with a core extension for Array, Array#shelljoin.
[40] Fix | Delete
#
[41] Fix | Delete
# argv = %w{ls -lta lib}
[42] Fix | Delete
# system(argv.shelljoin)
[43] Fix | Delete
#
[44] Fix | Delete
# You can use this method to create an escaped string out of an array of tokens
[45] Fix | Delete
# separated by a space. In this example we used the literal shortcut for
[46] Fix | Delete
# Array.new.
[47] Fix | Delete
#
[48] Fix | Delete
# === Authors
[49] Fix | Delete
# * Wakou Aoyama
[50] Fix | Delete
# * Akinori MUSHA <knu@iDaemons.org>
[51] Fix | Delete
#
[52] Fix | Delete
# === Contact
[53] Fix | Delete
# * Akinori MUSHA <knu@iDaemons.org> (current maintainer)
[54] Fix | Delete
#
[55] Fix | Delete
# === Resources
[56] Fix | Delete
#
[57] Fix | Delete
# 1: {IEEE Std 1003.1-2008, 2016 Edition, the Shell & Utilities volume}[http://pubs.opengroup.org/onlinepubs/9699919799/utilities/contents.html]
[58] Fix | Delete
[59] Fix | Delete
module Shellwords
[60] Fix | Delete
# Splits a string into an array of tokens in the same way the UNIX
[61] Fix | Delete
# Bourne shell does.
[62] Fix | Delete
#
[63] Fix | Delete
# argv = Shellwords.split('here are "two words"')
[64] Fix | Delete
# argv #=> ["here", "are", "two words"]
[65] Fix | Delete
#
[66] Fix | Delete
# Note, however, that this is not a command line parser. Shell
[67] Fix | Delete
# metacharacters except for the single and double quotes and
[68] Fix | Delete
# backslash are not treated as such.
[69] Fix | Delete
#
[70] Fix | Delete
# argv = Shellwords.split('ruby my_prog.rb | less')
[71] Fix | Delete
# argv #=> ["ruby", "my_prog.rb", "|", "less"]
[72] Fix | Delete
#
[73] Fix | Delete
# String#shellsplit is a shortcut for this function.
[74] Fix | Delete
#
[75] Fix | Delete
# argv = 'here are "two words"'.shellsplit
[76] Fix | Delete
# argv #=> ["here", "are", "two words"]
[77] Fix | Delete
def shellsplit(line)
[78] Fix | Delete
words = []
[79] Fix | Delete
field = String.new
[80] Fix | Delete
line.scan(/\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m) do
[81] Fix | Delete
|word, sq, dq, esc, garbage, sep|
[82] Fix | Delete
raise ArgumentError, "Unmatched double quote: #{line.inspect}" if garbage
[83] Fix | Delete
# 2.2.3 Double-Quotes:
[84] Fix | Delete
#
[85] Fix | Delete
# The <backslash> shall retain its special meaning as an
[86] Fix | Delete
# escape character only when followed by one of the following
[87] Fix | Delete
# characters when considered special:
[88] Fix | Delete
#
[89] Fix | Delete
# $ ` " \ <newline>
[90] Fix | Delete
field << (word || sq || (dq && dq.gsub(/\\([$`"\\\n])/, '\\1')) || esc.gsub(/\\(.)/, '\\1'))
[91] Fix | Delete
if sep
[92] Fix | Delete
words << field
[93] Fix | Delete
field = String.new
[94] Fix | Delete
end
[95] Fix | Delete
end
[96] Fix | Delete
words
[97] Fix | Delete
end
[98] Fix | Delete
[99] Fix | Delete
alias shellwords shellsplit
[100] Fix | Delete
[101] Fix | Delete
module_function :shellsplit, :shellwords
[102] Fix | Delete
[103] Fix | Delete
class << self
[104] Fix | Delete
alias split shellsplit
[105] Fix | Delete
end
[106] Fix | Delete
[107] Fix | Delete
# Escapes a string so that it can be safely used in a Bourne shell
[108] Fix | Delete
# command line. +str+ can be a non-string object that responds to
[109] Fix | Delete
# +to_s+.
[110] Fix | Delete
#
[111] Fix | Delete
# Note that a resulted string should be used unquoted and is not
[112] Fix | Delete
# intended for use in double quotes nor in single quotes.
[113] Fix | Delete
#
[114] Fix | Delete
# argv = Shellwords.escape("It's better to give than to receive")
[115] Fix | Delete
# argv #=> "It\\'s\\ better\\ to\\ give\\ than\\ to\\ receive"
[116] Fix | Delete
#
[117] Fix | Delete
# String#shellescape is a shorthand for this function.
[118] Fix | Delete
#
[119] Fix | Delete
# argv = "It's better to give than to receive".shellescape
[120] Fix | Delete
# argv #=> "It\\'s\\ better\\ to\\ give\\ than\\ to\\ receive"
[121] Fix | Delete
#
[122] Fix | Delete
# # Search files in lib for method definitions
[123] Fix | Delete
# pattern = "^[ \t]*def "
[124] Fix | Delete
# open("| grep -Ern #{pattern.shellescape} lib") { |grep|
[125] Fix | Delete
# grep.each_line { |line|
[126] Fix | Delete
# file, lineno, matched_line = line.split(':', 3)
[127] Fix | Delete
# # ...
[128] Fix | Delete
# }
[129] Fix | Delete
# }
[130] Fix | Delete
#
[131] Fix | Delete
# It is the caller's responsibility to encode the string in the right
[132] Fix | Delete
# encoding for the shell environment where this string is used.
[133] Fix | Delete
#
[134] Fix | Delete
# Multibyte characters are treated as multibyte characters, not as bytes.
[135] Fix | Delete
#
[136] Fix | Delete
# Returns an empty quoted String if +str+ has a length of zero.
[137] Fix | Delete
def shellescape(str)
[138] Fix | Delete
str = str.to_s
[139] Fix | Delete
[140] Fix | Delete
# An empty argument will be skipped, so return empty quotes.
[141] Fix | Delete
return "''".dup if str.empty?
[142] Fix | Delete
[143] Fix | Delete
str = str.dup
[144] Fix | Delete
[145] Fix | Delete
# Treat multibyte characters as is. It is the caller's responsibility
[146] Fix | Delete
# to encode the string in the right encoding for the shell
[147] Fix | Delete
# environment.
[148] Fix | Delete
str.gsub!(/[^A-Za-z0-9_\-.,:+\/@\n]/, "\\\\\\&")
[149] Fix | Delete
[150] Fix | Delete
# A LF cannot be escaped with a backslash because a backslash + LF
[151] Fix | Delete
# combo is regarded as a line continuation and simply ignored.
[152] Fix | Delete
str.gsub!(/\n/, "'\n'")
[153] Fix | Delete
[154] Fix | Delete
return str
[155] Fix | Delete
end
[156] Fix | Delete
[157] Fix | Delete
module_function :shellescape
[158] Fix | Delete
[159] Fix | Delete
class << self
[160] Fix | Delete
alias escape shellescape
[161] Fix | Delete
end
[162] Fix | Delete
[163] Fix | Delete
# Builds a command line string from an argument list, +array+.
[164] Fix | Delete
#
[165] Fix | Delete
# All elements are joined into a single string with fields separated by a
[166] Fix | Delete
# space, where each element is escaped for the Bourne shell and stringified
[167] Fix | Delete
# using +to_s+.
[168] Fix | Delete
#
[169] Fix | Delete
# ary = ["There's", "a", "time", "and", "place", "for", "everything"]
[170] Fix | Delete
# argv = Shellwords.join(ary)
[171] Fix | Delete
# argv #=> "There\\'s a time and place for everything"
[172] Fix | Delete
#
[173] Fix | Delete
# Array#shelljoin is a shortcut for this function.
[174] Fix | Delete
#
[175] Fix | Delete
# ary = ["Don't", "rock", "the", "boat"]
[176] Fix | Delete
# argv = ary.shelljoin
[177] Fix | Delete
# argv #=> "Don\\'t rock the boat"
[178] Fix | Delete
#
[179] Fix | Delete
# You can also mix non-string objects in the elements as allowed in Array#join.
[180] Fix | Delete
#
[181] Fix | Delete
# output = `#{['ps', '-p', $$].shelljoin}`
[182] Fix | Delete
#
[183] Fix | Delete
def shelljoin(array)
[184] Fix | Delete
array.map { |arg| shellescape(arg) }.join(' ')
[185] Fix | Delete
end
[186] Fix | Delete
[187] Fix | Delete
module_function :shelljoin
[188] Fix | Delete
[189] Fix | Delete
class << self
[190] Fix | Delete
alias join shelljoin
[191] Fix | Delete
end
[192] Fix | Delete
end
[193] Fix | Delete
[194] Fix | Delete
class String
[195] Fix | Delete
# call-seq:
[196] Fix | Delete
# str.shellsplit => array
[197] Fix | Delete
#
[198] Fix | Delete
# Splits +str+ into an array of tokens in the same way the UNIX
[199] Fix | Delete
# Bourne shell does.
[200] Fix | Delete
#
[201] Fix | Delete
# See Shellwords.shellsplit for details.
[202] Fix | Delete
def shellsplit
[203] Fix | Delete
Shellwords.split(self)
[204] Fix | Delete
end
[205] Fix | Delete
[206] Fix | Delete
# call-seq:
[207] Fix | Delete
# str.shellescape => string
[208] Fix | Delete
#
[209] Fix | Delete
# Escapes +str+ so that it can be safely used in a Bourne shell
[210] Fix | Delete
# command line.
[211] Fix | Delete
#
[212] Fix | Delete
# See Shellwords.shellescape for details.
[213] Fix | Delete
def shellescape
[214] Fix | Delete
Shellwords.escape(self)
[215] Fix | Delete
end
[216] Fix | Delete
end
[217] Fix | Delete
[218] Fix | Delete
class Array
[219] Fix | Delete
# call-seq:
[220] Fix | Delete
# array.shelljoin => string
[221] Fix | Delete
#
[222] Fix | Delete
# Builds a command line string from an argument list +array+ joining
[223] Fix | Delete
# all elements escaped for the Bourne shell and separated by a space.
[224] Fix | Delete
#
[225] Fix | Delete
# See Shellwords.shelljoin for details.
[226] Fix | Delete
def shelljoin
[227] Fix | Delete
Shellwords.join(self)
[228] Fix | Delete
end
[229] Fix | Delete
end
[230] Fix | Delete
[231] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function