~ubuntu-branches/ubuntu/raring/ruby1.9.1/raring-security

« back to all changes in this revision

Viewing changes to lib/shellwords.rb

  • Committer: Package Import Robot
  • Author(s): Antonio Terceiro, Lucas Nussbaum, Daigo Moriwaki, James Healy, Antonio Terceiro
  • Date: 2012-06-02 07:42:28 UTC
  • mfrom: (21.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20120602074228-09t2jgg1ozrsnnfo
Tags: 1.9.3.194-1
[ Lucas Nussbaum ]
* Add hurd-path-max.diff. Fixes FTBFS on Hurd. (Closes: #648055)

[ Daigo Moriwaki ]
* Removed debian/patches/debian/patches/sparc-continuations.diff,
  which the upstream has applied.
* debian/rules:
  - Bumped up tcltk_ver to 8.5.
  - Used chrpath for tcltklib.so to fix a lintian error,
    binary-or-shlib-defines-rpath.
* debian/control:
  - Suggests ruby-switch. (Closes: #654312)
  - Build-Depends: chrpath.
* debian/libruby1.9.1.symbols: Added a new symbol for
  rb_str_modify_expand@Base.
* debian/run-test-suites.bash:
  - Corrected options for test-all.
  - Enabled timeout to allow hang tests to be aborted.

[ James Healy ]
* New upstream release: 1.9.3p194 (Closes: #669582)
  + This release includes a fix for CVE-2011-0188 (Closes: #628451)
  + This release also does not segfault when running the test suite under
    amd64 (Closes: #674347)
* Enable hardened build flags (Closes: #667964)
* debian/control:
  - depend on specific version on coreutils
  - update policy version (no changes)

[ Antonio Terceiro ]
* debian/ruby1.9.1.postinst:
  + bump alternatives priority for `ruby` to 51 so that Ruby 1.9 has a
    higher priority than Ruby 1.8 (50).
  + bump alternatives priority for `gem` to 181 so that the Rubygems
    provided by Ruby 1.9 has priority over the one provided by the rubygems
    package.
* debian/control: added myself to Uploaders:
* debian/libruby1.9.1.symbols: update with new symbols added in 1.9.3p194
  upstream release.
* debian/manpages/*: fix references to command names with s/1.9/1.9.1/
* debian/rules: skip running DRB tests, since they seem to make the build
  hang. This should close #647296, but let's way and see. Also, with this do
  not need to timeout the test suite anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#   - Akinori MUSHA <knu@iDaemons.org> (current maintainer)
18
18
#
19
19
module Shellwords
20
 
  #
21
20
  # Splits a string into an array of tokens in the same way the UNIX
22
21
  # Bourne shell does.
23
22
  #
24
23
  #   argv = Shellwords.split('here are "two words"')
25
24
  #   argv #=> ["here", "are", "two words"]
26
25
  #
27
 
  # +String#shellsplit+ is a shorthand for this function.
 
26
  # String#shellsplit is a shorthand for this function.
28
27
  #
29
28
  #   argv = 'here are "two words"'.shellsplit
30
29
  #   argv #=> ["here", "are", "two words"]
31
 
  #
32
30
  def shellsplit(line)
33
31
    words = []
34
32
    field = ''
35
33
    line.scan(/\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m) do
36
34
      |word, sq, dq, esc, garbage, sep|
37
35
      raise ArgumentError, "Unmatched double quote: #{line.inspect}" if garbage
38
 
      field << (word || sq || (dq || esc).gsub(/\\(?=.)/, ''))
 
36
      field << (word || sq || (dq || esc).gsub(/\\(.)/, '\\1'))
39
37
      if sep
40
38
        words << field
41
39
        field = ''
52
50
    alias split shellsplit
53
51
  end
54
52
 
55
 
  #
56
53
  # Escapes a string so that it can be safely used in a Bourne shell
57
54
  # command line.
58
55
  #
63
60
  #     # ...
64
61
  #   }
65
62
  #
66
 
  # +String#shellescape+ is a shorthand for this function.
 
63
  # String#shellescape is a shorthand for this function.
67
64
  #
68
65
  #   open("| grep #{pattern.shellescape} file") { |pipe|
69
66
  #     # ...
70
67
  #   }
71
68
  #
 
69
  # It is caller's responsibility to encode the string in the right
 
70
  # encoding for the shell environment where this string is used.
 
71
  # Multibyte characters are treated as multibyte characters, not
 
72
  # bytes.
72
73
  def shellescape(str)
73
74
    # An empty argument will be skipped, so return empty quotes.
74
75
    return "''" if str.empty?
75
76
 
76
77
    str = str.dup
77
78
 
78
 
    # Process as a single byte sequence because not all shell
79
 
    # implementations are multibyte aware.
80
 
    str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1")
 
79
    # Treat multibyte characters as is.  It is caller's responsibility
 
80
    # to encode the string in the right encoding for the shell
 
81
    # environment.
 
82
    str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/, "\\\\\\1")
81
83
 
82
84
    # A LF cannot be escaped with a backslash because a backslash + LF
83
85
    # combo is regarded as line continuation and simply ignored.
92
94
    alias escape shellescape
93
95
  end
94
96
 
95
 
  #
96
97
  # Builds a command line string from an argument list +array+ joining
97
98
  # all elements escaped for Bourne shell and separated by a space.
98
99
  #
100
101
  #     # ...
101
102
  #   }
102
103
  #
103
 
  # +Array#shelljoin+ is a shorthand for this function.
 
104
  # Array#shelljoin is a shorthand for this function.
104
105
  #
105
106
  #   open('|' + ['grep', pattern, *files].shelljoin) { |pipe|
106
107
  #     # ...
118
119
end
119
120
 
120
121
class String
121
 
  #
122
122
  # call-seq:
123
123
  #   str.shellsplit => array
124
124
  #
125
125
  # Splits +str+ into an array of tokens in the same way the UNIX
126
 
  # Bourne shell does.  See +Shellwords::shellsplit+ for details.
127
 
  #
 
126
  # Bourne shell does.  See Shellwords::shellsplit for details.
128
127
  def shellsplit
129
128
    Shellwords.split(self)
130
129
  end
131
130
 
132
 
  #
133
131
  # call-seq:
134
132
  #   str.shellescape => string
135
133
  #
136
134
  # Escapes +str+ so that it can be safely used in a Bourne shell
137
 
  # command line.  See +Shellwords::shellescape+ for details.
138
 
  #
 
135
  # command line.  See Shellwords::shellescape for details.
139
136
  def shellescape
140
137
    Shellwords.escape(self)
141
138
  end
142
139
end
143
140
 
144
141
class Array
145
 
  #
146
142
  # call-seq:
147
143
  #   array.shelljoin => string
148
144
  #
149
145
  # Builds a command line string from an argument list +array+ joining
150
146
  # all elements escaped for Bourne shell and separated by a space.
151
 
  # See +Shellwords::shelljoin+ for details.
152
 
  #
 
147
  # See Shellwords::shelljoin for details.
153
148
  def shelljoin
154
149
    Shellwords.join(self)
155
150
  end