summaryrefslogtreecommitdiff
path: root/lib/shellwords.rb
diff options
context:
space:
mode:
authorNobuyoshi Nakada <[email protected]>2024-07-03 19:43:07 +0900
committergit <[email protected]>2024-12-04 07:48:59 +0000
commit3b27818062f5bd4723a3b76f7fb7e963f4dd1f49 ()
tree48ac51ff316d442b2dfc32bbb80663dfeff0a34b /lib/shellwords.rb
parentedd3977b4024c27b92a8e132b2762226298a475a (diff)
[ruby/shellwords] NUL char cannot be in shell words
https://.com/ruby/shellwords/commit/2c7ae1b76c
-rw-r--r--lib/shellwords.rb19
1 files changed, 17 insertions, 2 deletions
@@ -73,6 +73,9 @@ module Shellwords
# argv = Shellwords.split('here are "two words"')
# argv #=> ["here", "are", "two words"]
#
# 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.
@@ -87,9 +90,14 @@ module Shellwords
def shellsplit(line)
words = []
field = String.new
- line.scan(/\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m) do
|word, sq, dq, esc, garbage, sep|
- raise ArgumentError, "Unmatched quote: #{line.inspect}" if garbage
# 2.2.3 Double-Quotes:
#
# The <backslash> shall retain its special meaning as an
@@ -118,6 +126,9 @@ module Shellwords
# 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.
#
@@ -150,6 +161,9 @@ module Shellwords
# An empty argument will be skipped, so return empty quotes.
return "''".dup if str.empty?
str = str.dup
# Treat multibyte characters as is. It is the caller's responsibility
@@ -175,6 +189,7 @@ module Shellwords
# All elements are joined into a single string with fields separated by a
# space, where each element is escaped for the Bourne shell and stringified
# using +to_s+.
#
# ary = ["There's", "a", "time", "and", "place", "for", "everything"]
# argv = Shellwords.join(ary)