Repository: mesos Updated Branches: refs/heads/master 394141075 -> 5495ce0ec
Windows: Stout: Added `stringify_args` utility. This is used to simplify calling subprocesses and shell commands on Windows. Review: https://reviews.apache.org/r/47943/ Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/52397028 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/52397028 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/52397028 Branch: refs/heads/master Commit: 5239702850c2288371151d35ad102b8d35e7c450 Parents: 3941410 Author: Alex Clemmer <[email protected]> Authored: Mon May 30 20:40:41 2016 -0700 Committer: Joris Van Remoortere <[email protected]> Committed: Mon May 30 20:45:34 2016 -0700 ---------------------------------------------------------------------- .../stout/include/stout/os/windows/shell.hpp | 23 +++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/52397028/3rdparty/stout/include/stout/os/windows/shell.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/os/windows/shell.hpp b/3rdparty/stout/include/stout/os/windows/shell.hpp index f84eed4..66f2306 100644 --- a/3rdparty/stout/include/stout/os/windows/shell.hpp +++ b/3rdparty/stout/include/stout/os/windows/shell.hpp @@ -29,7 +29,6 @@ namespace Shell { // `name` is the command name, `arg0` is the first argument received // by the callee, usually the command name and `arg1` is the second // command argument received by the callee. - constexpr const char* name = "cmd.exe"; constexpr const char* arg0 = "cmd"; constexpr const char* arg1 = "/c"; @@ -94,6 +93,7 @@ Try<std::string> shell(const std::string& fmt, const T&... t) return stdoutstr.str(); } + // Executes a command by calling "cmd /c <command>", and returns // after the command has been completed. Returns 0 if succeeds, and // return -1 on error @@ -103,12 +103,33 @@ inline int system(const std::string& command) _P_WAIT, Shell::name, Shell::arg0, Shell::arg1, command.c_str(), NULL); } + template<typename... T> inline int execlp(const char* file, T... t) { exit(::_spawnlp(_P_WAIT, file, t...)); } + +// Concatenates multiple command-line arguments and escapes the values. +// If `arg` is not specified (or takes the value `0`), the function will +// scan `argv` until a `NULL` is encountered. +inline std::string stringify_args(char** argv, unsigned long argc = 0) +{ + std::string arg_line = ""; + unsigned long index = 0; + while ((argc == 0 || index < argc) && argv[index] != NULL) { + // TODO(dpravat): (MESOS-5522) Format these args for all cases. + // Specifically, we need to: + // (1) Add double quotes around arguments that contain special + // characters, like spaces and tabs. + // (2) Escape any existing double quotes and backslashes. + arg_line = strings::join(" ", arg_line, argv[index++]); + } + + return arg_line; +} + } // namespace os { #endif // __STOUT_OS_WINDOWS_SHELL_HPP__
