shellwords 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -1
  3. data/lib/shellwords.rb +47 -19
  4. metadata +4 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0753cd7454585ac9e3396f0ce90063eb24f892e450133448e4c7003ed0bd3d5a
4
- data.tar.gz: 252b5aad6e67bc8c776ef848e567808a9dddcf66292849735b1d379e01c2de12
3
+ metadata.gz: d8acce9d3d49704b5ee680d427e238dd9c8cf736515dfd2ae113e6f1505230ee
4
+ data.tar.gz: 4e5403da79a04b28031106a07b496a3f583beebff8f34be18ec4984ffc3c5b2c
5
5
  SHA512:
6
- metadata.gz: e548901809637b5f5b18a7f53d3b2cf748572151f668b288deaa13d59805d64a2c9737ebe1796980ef858a32e2d9065369c3d392fa3dfbcb1df00bdffc53a34c
7
- data.tar.gz: 673b55ffce8fe25968f9f0a1f19c58b9e7b0835eb37ffec8a60766b4838851e266d22edec0dd4489aad163de1157e70c2fffcfd4834f142cfcd353e6aa691f0b
6
+ metadata.gz: b8ec0e6f107495ee21caf2cdc3f9726999266d91b1226404e2028a0d1d9805af928938d34fdf9261a70ffa632bb48611de9e4b8286168cbf87ed85a3216cab89
7
+ data.tar.gz: ba4c005e4dc6fa74beeec4952bf017586a6a58e4799eb9b0a1b8ed586b85aacd7921b0346cccddde80658ea2aabd8629017d8980c29a81a332088989dbab9bb2
data/README.md CHANGED
@@ -38,7 +38,15 @@ argv #=> ["three", "blind", "mice"]
38
38
 
39
39
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
40
 
41
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+ To install this gem onto your local machine, run `bundle exec rake install`.
42
+
43
+ To release a new version, update `Shellwords::VERSION` in `lib/shellwords.rb`.
44
+ Push the release commit to `master` and wait for CI to pass. Then create and
45
+ push a matching `vX.Y.Z` tag on that commit. The [publish workflow] builds the
46
+ gem, publishes it to [RubyGems.org], and creates a GitHub release.
47
+
48
+ [publish workflow]: https://github.com/ruby/shellwords/actions/workflows/push_gem.yml
49
+ [RubyGems.org]: https://rubygems.org/gems/shellwords
42
50
 
43
51
  ## Contributing
44
52
 
data/lib/shellwords.rb CHANGED
@@ -40,11 +40,10 @@
40
40
  # # runs "cat -- special\\'s.txt"
41
41
  #
42
42
  # Note the '--'. Without it, cat(1) will treat the following argument
43
- # as a command line option if it starts with '-'. It is guaranteed
44
- # that Shellwords.escape converts a string to a form that a Bourne
45
- # shell will parse back to the original string, but it is the
46
- # programmer's responsibility to make sure that passing an arbitrary
47
- # argument to a command does no harm.
43
+ # as a command line option if it starts with '-'. When the string's encoding
44
+ # is compatible with how the shell parses command lines, Shellwords.escape
45
+ # preserves the original argument.
46
+ # The caller must still ensure that passing the argument to the command is safe.
48
47
  #
49
48
  # Shellwords also comes with a core extension for Array, Array#shelljoin.
50
49
  #
@@ -65,7 +64,7 @@
65
64
 
66
65
  module Shellwords
67
66
  # The version number string.
68
- VERSION = "0.2.1"
67
+ VERSION = "0.3.0"
69
68
 
70
69
  # Splits a string into an array of tokens in the same way the UNIX
71
70
  # Bourne shell does.
@@ -73,6 +72,9 @@ module Shellwords
73
72
  # argv = Shellwords.split('here are "two words"')
74
73
  # argv #=> ["here", "are", "two words"]
75
74
  #
75
+ # +line+ must not contain NUL characters because of nature of
76
+ # +exec+ system call.
77
+ #
76
78
  # Note, however, that this is not a command line parser. Shell
77
79
  # metacharacters except for the single and double quotes and
78
80
  # backslash are not treated as such.
@@ -86,10 +88,15 @@ module Shellwords
86
88
  # argv #=> ["here", "are", "two words"]
87
89
  def shellsplit(line)
88
90
  words = []
89
- field = String.new
90
- line.scan(/\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m) do
91
+ field = nil
92
+ line.scan(/\G\s*(?>([^\0\s\\\'\"]+)|'([^\0\']*)'|"((?:[^\0\"\\]|\\[^\0])*)"|(\\[^\0]?)|(\S))(\s|\z)?/m) do
91
93
  |word, sq, dq, esc, garbage, sep|
92
- raise ArgumentError, "Unmatched quote: #{line.inspect}" if garbage
94
+ if garbage
95
+ b = $~.begin(0)
96
+ line = $~[0]
97
+ line = "..." + line if b > 0
98
+ raise ArgumentError, "#{garbage == "\0" ? 'Nul character' : 'Unmatched quote'} at #{b}: #{line}"
99
+ end
93
100
  # 2.2.3 Double-Quotes:
94
101
  #
95
102
  # The <backslash> shall retain its special meaning as an
@@ -97,10 +104,15 @@ module Shellwords
97
104
  # characters when considered special:
98
105
  #
99
106
  # $ ` " \ <newline>
100
- field << (word || sq || (dq && dq.gsub(/\\([$`"\\\n])/, '\\1')) || esc.gsub(/\\(.)/, '\\1'))
101
- if sep
107
+ #
108
+ # An unquoted line continuation alone does not start a word.
109
+ unless esc == "\\\n"
110
+ field ||= String.new
111
+ field << (word || sq || (dq && dq.gsub(/\\(?:([$`"\\])|\n)/, '\\1')) || esc.gsub(/\\(.)/, '\\1'))
112
+ end
113
+ if sep && field
102
114
  words << field
103
- field = String.new
115
+ field = nil
104
116
  end
105
117
  end
106
118
  words
@@ -115,8 +127,11 @@ module Shellwords
115
127
  end
116
128
 
117
129
  # Escapes a string so that it can be safely used in a Bourne shell
118
- # command line. +str+ can be a non-string object that responds to
119
- # +to_s+.
130
+ # command line (see {Encoding considerations}[rdoc-ref:Shellwords.shellescape@Encoding+considerations]).
131
+ # +str+ can be a non-string object that responds to +to_s+.
132
+ #
133
+ # +str+ must not contain NUL characters because of nature of +exec+
134
+ # system call.
120
135
  #
121
136
  # Note that a resulted string should be used unquoted and is not
122
137
  # intended for use in double quotes nor in single quotes.
@@ -138,18 +153,29 @@ module Shellwords
138
153
  # }
139
154
  # }
140
155
  #
141
- # It is the caller's responsibility to encode the string in the right
142
- # encoding for the shell environment where this string is used.
156
+ # Returns an empty quoted String if +str+ has a length of zero.
143
157
  #
144
- # Multibyte characters are treated as multibyte characters, not as bytes.
158
+ # === Encoding considerations
145
159
  #
146
- # Returns an empty quoted String if +str+ has a length of zero.
160
+ # The caller must choose an encoding suited to the shell environment.
161
+ # Shellwords escapes the string character by character. Some shells parse
162
+ # command lines byte by byte, even in multibyte locales. With encodings
163
+ # such as GBK, Big5, and Shift_JIS, these shells may interpret shell
164
+ # metacharacters in the escaped result even when the string's encoding
165
+ # matches the shell's locale.
166
+ #
167
+ # For byte-oriented shells, use <tt>Shellwords.escape(str.b)</tt> to escape
168
+ # the string byte by byte, or bypass the shell by passing arguments directly
169
+ # to the command.
147
170
  def shellescape(str)
148
171
  str = str.to_s
149
172
 
150
173
  # An empty argument will be skipped, so return empty quotes.
151
174
  return "''".dup if str.empty?
152
175
 
176
+ # Shellwords cannot contain NUL characters.
177
+ raise ArgumentError, "NUL character" if str.index("\0")
178
+
153
179
  str = str.dup
154
180
 
155
181
  # Treat multibyte characters as is. It is the caller's responsibility
@@ -175,6 +201,7 @@ module Shellwords
175
201
  # All elements are joined into a single string with fields separated by a
176
202
  # space, where each element is escaped for the Bourne shell and stringified
177
203
  # using +to_s+.
204
+ # See {Encoding considerations}[rdoc-ref:Shellwords.shellescape@Encoding+considerations].
178
205
  #
179
206
  # ary = ["There's", "a", "time", "and", "place", "for", "everything"]
180
207
  # argv = Shellwords.join(ary)
@@ -197,6 +224,7 @@ module Shellwords
197
224
  module_function :shelljoin
198
225
 
199
226
  class << self
227
+ # See {Encoding considerations}[rdoc-ref:Shellwords.shellescape@Encoding+considerations].
200
228
  alias join shelljoin
201
229
  end
202
230
  end
@@ -217,7 +245,7 @@ class String
217
245
  # str.shellescape => string
218
246
  #
219
247
  # Escapes +str+ so that it can be safely used in a Bourne shell
220
- # command line.
248
+ # command line (see {Encoding considerations}[rdoc-ref:Shellwords.shellescape@Encoding+considerations]).
221
249
  #
222
250
  # See Shellwords.shellescape for details.
223
251
  def shellescape
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shellwords
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akinori MUSHA
8
- autorequire:
9
- bindir: exe
8
+ bindir: bin
10
9
  cert_chain: []
11
- date: 2024-11-06 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Manipulates strings with word parsing rules of UNIX Bourne shell.
14
13
  email:
@@ -34,7 +33,6 @@ licenses:
34
33
  metadata:
35
34
  homepage_uri: https://github.com/ruby/shellwords
36
35
  source_code_uri: https://github.com/ruby/shellwords
37
- post_install_message:
38
36
  rdoc_options: []
39
37
  require_paths:
40
38
  - lib
@@ -49,8 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
47
  - !ruby/object:Gem::Version
50
48
  version: '0'
51
49
  requirements: []
52
- rubygems_version: 3.5.11
53
- signing_key:
50
+ rubygems_version: 4.0.20
54
51
  specification_version: 4
55
52
  summary: Manipulates strings with word parsing rules of UNIX Bourne shell.
56
53
  test_files: []