diff options
author | Thomas Bellman <bellman@nsc.liu.se> | 2009-08-04 14:47:12 +0200 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-08-08 09:42:39 +1000 |
commit | 79a4339e1f614647d8b3e074752d8bf0e583aef2 (patch) | |
tree | 7c3e8687e13392f2cfdcf8a33cd5f8d86c8fc5ce /lib/puppet | |
parent | 79d705faec6b7de2bd644e77e5e4c88e8974f207 (diff) | |
download | puppet-79a4339e1f614647d8b3e074752d8bf0e583aef2.tar.gz puppet-79a4339e1f614647d8b3e074752d8bf0e583aef2.tar.xz puppet-79a4339e1f614647d8b3e074752d8bf0e583aef2.zip |
Add shellquote() function.
This adds a new function shellquote() which can be used for quoting
arguments in shell commands used in the exec type.
This only supports Unixoid operating systems. Other systems would
likely require some other quoting.
Signed-off-by: Thomas Bellman <bellman@nsc.liu.se>
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/parser/functions/shellquote.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/shellquote.rb b/lib/puppet/parser/functions/shellquote.rb new file mode 100644 index 000000000..6c994520c --- /dev/null +++ b/lib/puppet/parser/functions/shellquote.rb @@ -0,0 +1,41 @@ +module Puppet::Parser::Functions + + Safe = 'a-zA-Z0-9@%_+=:,./-' # Safe unquoted + Dangerous = '!"`$\\' # Unsafe inside double quotes + + newfunction(:shellquote, :type => :rvalue, :doc => "\ + Quote and concatenate arguments for use in Bourne shell. + + Each argument is quoted separately, and then all are concatenated + with spaces. If an argument is an array, the elements of that + array is interpolated within the rest of the arguments; this makes + it possible to have an array of arguments and pass that array to + shellquote() instead of having to specify specify each argument + individually in the call. + ") \ + do |args| + + result = [] + args.flatten.each do |word| + if word.length != 0 and word.count(Safe) == word.length + result << word + elsif word.count(Dangerous) == 0 + result << ('"' + word + '"') + elsif word.count("'") == 0 + result << ("'" + word + "'") + else + r = '"' + word.each_byte() do |c| + if Dangerous.include?(c) + r += "\\" + end + r += c.chr() + end + r += '"' + result << r + end + end + + return result.join(" ") + end +end |