GIF87a;
## # == Manipulates strings like the UNIX Bourne shell # # This module manipulates strings according to the word parsing rules # of the UNIX Bourne shell. # # The shellwords() function was originally a port of shellwords.pl, # but modified to conform to POSIX / SUSv3 (IEEE Std 1003.1-2001 [1]). # # === Usage # # You can use shellwords to parse a string into a Bourne shell friendly Array. # # 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: ... # # In this case, you might want to use Shellwords.escape, or it's alias # String#shellescape. # # This method will escape the String for you to safely use with a Bourne shell. # # argv = Shellwords.escape("special's.txt") # argv #=> "special\\s.txt" # system("cat " + argv) # # 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 # separated by a space. In this example we'll use the literal shortcut for # Array.new. # # === Authors # * Wakou Aoyama # * Akinori MUSHA <knu@iDaemons.org> # # === Contact # * Akinori MUSHA <knu@iDaemons.org> (current maintainer) # # === Resources # # 1: {IEEE Std 1003.1-2004}[http://pubs.opengroup.org/onlinepubs/009695399/toc.htm] module Shellwords # 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"] # # String#shellsplit is a shortcut for this function. # # argv = 'here are "two words"'.shellsplit # argv #=> ["here", "are", "two words"] def shellsplit(line) words = [] field = '' 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 field << (word || sq || (dq || esc).gsub(/\\(.)/, '\\1')) if sep words << field field = '' end end words end 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 # command line. +str+ can be a non-string object that responds to # +to_s+. # # Note that a resulted string should be used unquoted and is not # intended for use in double quotes nor in single quotes. # # argv = Shellwords.escape("It's better to give than to receive") # argv #=> "It\\'s\\ better\\ to\\ give\\ than\\ to\\ receive" # # String#shellescape is a shorthand for this function. # # argv = "It's better to give than to receive".shellescape # argv #=> "It\\'s\\ better\\ to\\ give\\ than\\ to\\ receive" # # # 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) # # ... # } # } # # It is the caller's responsibility to encode the string in the right # encoding for the shell environment where this string is used. # # Multibyte characters are treated as multibyte characters, not bytes. # # Returns an empty quoted String if +str+ has a length of zero. def shellescape(str) str = str.to_s # An empty argument will be skipped, so return empty quotes. return "''" if str.empty? str = str.dup # Treat multibyte characters as is. It is caller's responsibility # to encode the string in the right encoding for the shell # environment. str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/, "\\\\\\1") # A LF cannot be escaped with a backslash because a backslash + LF # combo is regarded as line continuation and simply ignored. str.gsub!(/\n/, "'\n'") return str end module_function :shellescape class << self alias escape shellescape end # Builds a command line string from an argument list, +array+. # # All elements are joined into a single string with fields separated by a # space, where each element is escaped for Bourne shell and stringified using # +to_s+. # # ary = ["There's", "a", "time", "and", "place", "for", "everything"] # argv = Shellwords.join(ary) # argv #=> "There\\'s a time and place for everything" # # Array#shelljoin is a shortcut for this function. # # ary = ["Don't", "rock", "the", "boat"] # argv = ary.shelljoin # argv #=> "Don\\'t rock the boat" # # You can also mix non-string objects in the elements as allowed in Array#join. # # output = `#{['ps', '-p', $$].shelljoin}` # 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 # Bourne shell does. # # See Shellwords.shellsplit for details. def shellsplit Shellwords.split(self) end # call-seq: # str.shellescape => string # # Escapes +str+ so that it can be safely used in a Bourne shell # command line. # # See Shellwords.shellescape for details. 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 # all elements escaped for Bourne shell and separated by a space. # # See Shellwords.shelljoin for details. def shelljoin Shellwords.join(self) end end
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
bigdecimal | Folder | 0755 |
|
|
cgi | Folder | 0755 |
|
|
date | Folder | 0755 |
|
|
digest | Folder | 0755 |
|
|
dl | Folder | 0755 |
|
|
drb | Folder | 0755 |
|
|
fiddle | Folder | 0755 |
|
|
io | Folder | 0755 |
|
|
irb | Folder | 0755 |
|
|
json | Folder | 0755 |
|
|
matrix | Folder | 0755 |
|
|
net | Folder | 0755 |
|
|
openssl | Folder | 0755 |
|
|
optparse | Folder | 0755 |
|
|
psych | Folder | 0755 |
|
|
racc | Folder | 0755 |
|
|
rbconfig | Folder | 0755 |
|
|
rexml | Folder | 0755 |
|
|
rinda | Folder | 0755 |
|
|
ripper | Folder | 0755 |
|
|
rss | Folder | 0755 |
|
|
shell | Folder | 0755 |
|
|
syslog | Folder | 0755 |
|
|
test | Folder | 0755 |
|
|
uri | Folder | 0755 |
|
|
vendor_ruby | Folder | 0755 |
|
|
webrick | Folder | 0755 |
|
|
xmlrpc | Folder | 0755 |
|
|
yaml | Folder | 0755 |
|
|
English.rb | File | 6.44 KB | 0644 |
|
abbrev.rb | File | 3.31 KB | 0644 |
|
base64.rb | File | 2.63 KB | 0644 |
|
benchmark.rb | File | 17.94 KB | 0644 |
|
cgi.rb | File | 9.39 KB | 0644 |
|
cmath.rb | File | 7.22 KB | 0644 |
|
complex.rb | File | 380 B | 0644 |
|
csv.rb | File | 81.32 KB | 0644 |
|
date.rb | File | 946 B | 0644 |
|
debug.rb | File | 28.9 KB | 0644 |
|
delegate.rb | File | 9.78 KB | 0644 |
|
digest.rb | File | 2.24 KB | 0644 |
|
dl.rb | File | 280 B | 0644 |
|
drb.rb | File | 19 B | 0644 |
|
e2mmap.rb | File | 3.8 KB | 0644 |
|
erb.rb | File | 26.08 KB | 0644 |
|
expect.rb | File | 2.14 KB | 0644 |
|
fiddle.rb | File | 1.25 KB | 0644 |
|
fileutils.rb | File | 46.35 KB | 0644 |
|
find.rb | File | 2.08 KB | 0644 |
|
forwardable.rb | File | 7.56 KB | 0644 |
|
getoptlong.rb | File | 15.38 KB | 0644 |
|
gserver.rb | File | 8.86 KB | 0644 |
|
ipaddr.rb | File | 26.17 KB | 0644 |
|
irb.rb | File | 20.03 KB | 0644 |
|
json.rb | File | 1.74 KB | 0644 |
|
kconv.rb | File | 5.74 KB | 0644 |
|
logger.rb | File | 20.96 KB | 0644 |
|
mathn.rb | File | 6.52 KB | 0644 |
|
matrix.rb | File | 45.02 KB | 0644 |
|
mkmf.rb | File | 78.13 KB | 0644 |
|
monitor.rb | File | 6.93 KB | 0644 |
|
mutex_m.rb | File | 2 KB | 0644 |
|
observer.rb | File | 5.71 KB | 0644 |
|
open-uri.rb | File | 23.66 KB | 0644 |
|
open3.rb | File | 21.17 KB | 0644 |
|
openssl.rb | File | 528 B | 0644 |
|
optparse.rb | File | 51.27 KB | 0644 |
|
ostruct.rb | File | 7.64 KB | 0644 |
|
pathname.rb | File | 15.3 KB | 0644 |
|
pp.rb | File | 13.14 KB | 0644 |
|
prettyprint.rb | File | 9.63 KB | 0644 |
|
prime.rb | File | 13.98 KB | 0644 |
|
profile.rb | File | 205 B | 0644 |
|
profiler.rb | File | 4.29 KB | 0644 |
|
pstore.rb | File | 14.85 KB | 0644 |
|
psych.rb | File | 11.45 KB | 0644 |
|
rational.rb | File | 308 B | 0644 |
|
resolv-replace.rb | File | 1.73 KB | 0644 |
|
resolv.rb | File | 61.46 KB | 0644 |
|
ripper.rb | File | 2.53 KB | 0644 |
|
rss.rb | File | 2.84 KB | 0644 |
|
scanf.rb | File | 23.52 KB | 0644 |
|
securerandom.rb | File | 8.56 KB | 0644 |
|
set.rb | File | 17.32 KB | 0644 |
|
shell.rb | File | 10.3 KB | 0644 |
|
shellwords.rb | File | 5.94 KB | 0644 |
|
singleton.rb | File | 4.02 KB | 0644 |
|
socket.rb | File | 25.76 KB | 0644 |
|
sync.rb | File | 7.26 KB | 0644 |
|
tempfile.rb | File | 10.15 KB | 0644 |
|
thread.rb | File | 6.94 KB | 0644 |
|
thwait.rb | File | 3.38 KB | 0644 |
|
time.rb | File | 21.09 KB | 0644 |
|
timeout.rb | File | 3.16 KB | 0644 |
|
tmpdir.rb | File | 4.29 KB | 0644 |
|
tracer.rb | File | 6.54 KB | 0644 |
|
tsort.rb | File | 6.79 KB | 0644 |
|
un.rb | File | 8.34 KB | 0644 |
|
uri.rb | File | 3.07 KB | 0644 |
|
weakref.rb | File | 3.23 KB | 0644 |
|
webrick.rb | File | 6.7 KB | 0644 |
|
xmlrpc.rb | File | 8.49 KB | 0644 |
|
yaml.rb | File | 2.3 KB | 0644 |
|