about summary refs log tree commit homepage
path: root/lib/dtas/process.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/dtas/process.rb')
-rw-r--r--lib/dtas/process.rb39
1 files changed, 31 insertions, 8 deletions
diff --git a/lib/dtas/process.rb b/lib/dtas/process.rb
index 8c5e8e9..f5f9a9e 100644
--- a/lib/dtas/process.rb
+++ b/lib/dtas/process.rb
@@ -1,6 +1,7 @@
 # Copyright (C) 2013-2015 all contributors <dtas-all@nongnu.org>
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
 require 'io/wait'
+require 'shellwords'
 require_relative '../dtas'
 require_relative 'xs'
 
@@ -23,6 +24,7 @@ module DTAS::Process # :nodoc:
 
   # expand common shell constructs based on environment variables
   # this is order-dependent, but Ruby 1.9+ hashes are already order-dependent
+  # This recurses
   def env_expand(env, opts)
     env = env.dup
     if false == opts.delete(:expand)
@@ -31,19 +33,40 @@ module DTAS::Process # :nodoc:
       end
     else
       env.each do |key, val|
-        case val
-        when Numeric # stringify numeric values to simplify users' lives
-          env[key] = val.to_s
-        when /[\`\$]/ # perform variable/command expansion
-          tmp = env.dup
-          tmp.delete(key)
-          val = qx(tmp, "echo #{val}", expand: false)
-          env[key] = val.chomp
+        case val = env_expand_i(env, key, val)
+        when Array
+          val.flatten!
+          env[key] = Shellwords.join(val)
         end
       end
     end
   end
 
+  def env_expand_i(env, key, val)
+    case val
+    when Numeric # stringify numeric values to simplify users' lives
+      env[key] = val.to_s
+    when /[\`\$]/ # perform variable/command expansion
+      tmp = env.dup
+      tmp.delete(key)
+      tmp.each do |k,v|
+        # best effort, this can get wonky
+        tmp[k] = Shellwords.join(v.flatten) if Array === v
+      end
+      val = qx(tmp, "echo #{val}", expand: false)
+      env[key] = val.chomp
+    when Array
+      env[key] = env_expand_ary(env, key, val)
+    else
+      val
+    end
+  end
+
+  # warning, recursion:
+  def env_expand_ary(env, key, val)
+    val.map { |v| env_expand_i(env.dup, key, v) }
+  end
+
   # for long-running processes (sox/play/ecasound filters)
   def dtas_spawn(env, cmd, opts)
     opts = { close_others: true, pgroup: true }.merge!(opts)