Skip to content

run now does I/O redirection on Windows #2849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/util/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,34 @@ int run(
const std::string &std_error)
{
#ifdef _WIN32
// we don't support stdin/stdout/stderr redirection on Windows
PRECONDITION(std_input.empty());
PRECONDITION(std_output.empty());
PRECONDITION(std_error.empty());
// we use the cmd.exe shell to do stdin/stdout/stderr redirection on Windows
if(!std_input.empty() || !std_output.empty() || !std_error.empty())
{
std::vector<std::string> new_argv = argv;
new_argv.insert(new_argv.begin(), "cmd.exe");
new_argv.insert(new_argv.begin() + 1, "/c");

if(!std_input.empty())
{
new_argv.push_back("<");
new_argv.push_back(std_input);
}

if(!std_output.empty())
{
new_argv.push_back(">");
new_argv.push_back(std_output);
}

if(!std_error.empty())
{
new_argv.push_back("2>");
new_argv.push_back(std_error);
}

// this is recursive
return run(new_argv[0], new_argv, "", "", "");
}

// unicode version of the arguments
std::vector<std::wstring> wargv;
Expand All @@ -107,12 +131,12 @@ int run(

_argv[argv.size()]=NULL;

// warning: the arguments may still need escaping
// warning: the arguments may still need escaping,
// as windows will concatenate the argv strings back together,
// separating them with spaces

std::wstring wide_what=widen(what);

int status=_wspawnvp(_P_WAIT, wide_what.c_str(), _argv.data());

return status;

#else
Expand Down