aboutsummaryrefslogtreecommitdiffstats
path: root/lib/shellwords.rb
blob: 055a6a5be2f9d53e9b1ae4c9306f361f8ed4d0ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#
# shellwords.rb: Split text into an array of tokens a la UNIX shell
#

#
# This module is originally a port of shellwords.pl, but modified to
# conform to POSIX / SUSv3 (IEEE Std 1003.1-2001).
#
# Examples:
#
#   require 'shellwords'
#   words = Shellwords.shellwords(line)
#
# or
#
#   require 'shellwords'
#   include Shellwords
#   words = shellwords(line)
#
module Shellwords

  #
  # Split text into an array of tokens in the same way the UNIX Bourne
  # shell does.
  #
  # See the +Shellwords+ module documentation for an example.
  #
  def shellwords(line)
    line = String.new(line) rescue
      raise(ArgumentError, "Argument must be a string")
    words = []
    field = ''
    last = 0
    sep = nil
    line.scan(/\G\s*(?:([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?))(\s+|\z)?/m) do
      last = $~.end(0)
      sep = $~.begin(5)
      field << ($1 || $2 || ($3 || $4).gsub(/\\(?=.)/, ''))
      if sep
        words << field
        field = ''
      end
    end
    raise ArgumentError, "Unmatched double quote: #{line}" if line[last]
    words
  end

  module_function :shellwords
end