From 567470b136c2b170b327e26f15a0e59de7dec250 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Fri, 5 Aug 2011 09:36:39 +0200 Subject: [PATCH] Child processes: support windowsVerbatimArguments option --- lib/child_process_uv.js | 51 ++++++++++++++++++++++++++++++----------- src/process_wrap.cc | 6 +++++ 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/lib/child_process_uv.js b/lib/child_process_uv.js index 6f3a40aa2a5..8a9216cf63e 100644 --- a/lib/child_process_uv.js +++ b/lib/child_process_uv.js @@ -47,16 +47,43 @@ function createSocket(pipe, readable) { return s; } +function mergeOptions(target, overrides) { + if (overrides) { + var keys = Object.keys(overrides); + for (var i = 0, len = keys.length; i < len; i++) { + var k = keys[i]; + if (overrides[k] !== undefined) { + target[k] = overrides[k]; + } + } + } + return target; +} + exports.exec = function(command /*, options, callback */) { - var rest = Array.prototype.slice.call(arguments, 1); - var args; - if (process.platform == 'win32') { - args = ['cmd.exe', ['/c', command]].concat(rest); + var file, args, options, callback; + + if (typeof arguments[1] === 'function') { + options = undefined; + callback = arguments[1]; } else { - args = ['/bin/sh', ['-c', command]].concat(rest); + options = arguments[1]; + callback = arguments[2]; } - return exports.execFile.apply(this, args); + + if (process.platform === 'win32') { + file = 'cmd.exe'; + args = ['/s', '/c', '"' + command + '"']; + // Make a shallow copy before patching so we don't clobber the user's + // options object. + options = mergeOptions({}, options); + options.windowsVerbatimArguments = true; + } else { + file = '/bin/sh'; + args = ['-c', command]; + } + return exports.execFile(file, args, options, callback); }; @@ -87,17 +114,12 @@ exports.execFile = function(file /* args, options, callback */) { } // Merge optionArg into options - if (optionArg) { - var keys = Object.keys(options); - for (var i = 0, len = keys.length; i < len; i++) { - var k = keys[i]; - if (optionArg[k] !== undefined) options[k] = optionArg[k]; - } - } + mergeOptions(options, optionArg); var child = spawn(file, args, { cwd: options.cwd, - env: options.env + env: options.env, + windowsVerbatimArguments: !!options.windowsVerbatimArguments }); var stdout = ''; @@ -189,6 +211,7 @@ var spawn = exports.spawn = function(file, args, options) { file: file, args: args, cwd: options ? options.cwd : null, + windowsVerbatimArguments: !!(options && options.windowsVerbatimArguments), envPairs: envPairs }); diff --git a/src/process_wrap.cc b/src/process_wrap.cc index 7ec9ffcc460..3626025c1f7 100644 --- a/src/process_wrap.cc +++ b/src/process_wrap.cc @@ -146,6 +146,12 @@ class ProcessWrap : public HandleWrap { options.stderr_stream = stderr_wrap->UVHandle(); } + // options.windows_verbatim_arguments +#if defined(_WIN32) && !defined(__CYGWIN__) + options.windows_verbatim_arguments = js_options-> + Get(String::NewSymbol("windowsVerbatimArguments"))->IsTrue(); +#endif + int r = uv_spawn(&wrap->process_, options); wrap->SetHandle((uv_handle_t*)&wrap->process_);