aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-05 04:58:48 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-11-05 04:58:48 +0000
commit5ee9cb1500e5f34c4b11bbb703ea82f613b649c8 (patch)
tree44fd2efaf75c6abc329d97c7219e17ba631ea7ff /lib
parent049fc52c9708c98bf05305d1377f4385975ba0d7 (diff)
downloadruby-5ee9cb1500e5f34c4b11bbb703ea82f613b649c8.tar.gz
Fix the handling of the backslash in double quotes
* lib/shellwords.rb (Shellwords#shellsplit): Fix the handling of the backslash in double quotes to conform to the standard. [ruby-core:63807] [Bug #10055] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56573 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/shellwords.rb14
1 files changed, 11 insertions, 3 deletions
diff --git a/lib/shellwords.rb b/lib/shellwords.rb
index 0030f0784f..eb5fa2d226 100644
--- a/lib/shellwords.rb
+++ b/lib/shellwords.rb
@@ -6,7 +6,8 @@
# 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]).
+# but modified to conform to the Shell & Utilities volume of the IEEE
+# Std 1003.1-2008, 2016 Edition [1].
#
# === Usage
#
@@ -55,7 +56,7 @@
#
# === Resources
#
-# 1: {IEEE Std 1003.1-2004}[http://pubs.opengroup.org/onlinepubs/009695399/toc.htm]
+# 1: {IEEE Std 1003.1-2008, 2016 Edition, the Shell & Utilities volume}[http://pubs.opengroup.org/onlinepubs/9699919799/utilities/contents.html]
module Shellwords
# Splits a string into an array of tokens in the same way the UNIX
@@ -81,7 +82,14 @@ module Shellwords
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'))
+ # 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'))
if sep
words << field
field = String.new