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