From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 208.118.235.0/24 X-Spam-Status: No, score=-4.8 required=3.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI, URIBL_BLOCKED shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: dtas-all@80x24.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id B242A1F515 for ; Mon, 18 May 2015 00:21:28 +0000 (UTC) Received: from localhost ([::1]:38767 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yu8oB-0006Av-TV for dtas-all@80x24.org; Sun, 17 May 2015 20:21:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49244) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yu8o6-0006Al-Lv for dtas-all@nongnu.org; Sun, 17 May 2015 20:21:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yu8o2-0001uZ-OB for dtas-all@nongnu.org; Sun, 17 May 2015 20:21:22 -0400 Received: from dcvr.yhbt.net ([64.71.152.64]:43779) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yu8o2-0001uE-JB for dtas-all@nongnu.org; Sun, 17 May 2015 20:21:18 -0400 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 6D4A41F515; Mon, 18 May 2015 00:21:17 +0000 (UTC) From: Eric Wong To: Subject: [PATCH] process: implement array expansion to preserve spaces Date: Mon, 18 May 2015 00:21:15 +0000 Message-Id: <1431908475-15700-1-git-send-email-e@80x24.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 64.71.152.64 Cc: Eric Wong X-BeenThere: dtas-all@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dtas-all-bounces+dtas-all=80x24.org@nongnu.org Sender: dtas-all-bounces+dtas-all=80x24.org@nongnu.org This can make it easier to specify mcompand parameters in socks, as those require separate levels of parameter parsing and require quoting in shell. --- lib/dtas/process.rb | 39 +++++++++++++++++++++++++++++++-------- test/test_env.rb | 16 ++++++++++++++++ 2 files changed, 47 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 # 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) diff --git a/test/test_env.rb b/test/test_env.rb index bd7961d..6b36f32 100644 --- a/test/test_env.rb +++ b/test/test_env.rb @@ -52,4 +52,20 @@ class TestEnv < Testcase res = env_expand({"PATH"=>"$PATH"}, expand: true) assert_equal ENV["PATH"], res["PATH"] end + + def test_ary + ENV['HELLO'] = 'HIHI' + ENV['PAATH'] = '/usr/local/bin:/usr/bin:/bin' + env = { 'BLAH' => [ '$HELLO/WORLD', '$PAATH', '$(echo hello world)' ] } + res = env_expand(env, expand: true) + exp = [ "HIHI/WORLD", ENV['PAATH'], 'hello world' ] + assert_equal exp, Shellwords.split(res['BLAH']) + env = { + 'BLAH' => [ '$(echo hello world)' ], + 'MOAR' => [ '$BLAH', 'OMG HALP SPACES' ] + } + res = env_expand(env, expand: true) + exp = ["hello\\ world", "OMG HALP SPACES"] + assert_equal exp, Shellwords.split(res['MOAR']) + end end -- EW