Skip to content

Commit 9c22b7a

Browse files
author
Daniel Kroening
committed
run now does I/O redirection on Windows
1 parent d33bd22 commit 9c22b7a

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

src/util/run.cpp

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,34 @@ int run(
8787
const std::string &std_error)
8888
{
8989
#ifdef _WIN32
90-
// we don't support stdin/stdout/stderr redirection on Windows
91-
PRECONDITION(std_input.empty());
92-
PRECONDITION(std_output.empty());
93-
PRECONDITION(std_error.empty());
90+
// we use the cmd.exe shell to do stdin/stdout/stderr redirection on Windows
91+
if(!std_input.empty() || !std_output.empty() || !std_error.empty())
92+
{
93+
std::vector<std::string> new_argv = argv;
94+
new_argv.insert(new_argv.begin(), "cmd.exe");
95+
new_argv.insert(new_argv.begin() + 1, "/c");
96+
97+
if(!std_input.empty())
98+
{
99+
new_argv.push_back("<");
100+
new_argv.push_back(std_input);
101+
}
102+
103+
if(!std_output.empty())
104+
{
105+
new_argv.push_back(">");
106+
new_argv.push_back(std_output);
107+
}
108+
109+
if(!std_error.empty())
110+
{
111+
new_argv.push_back("2>");
112+
new_argv.push_back(std_error);
113+
}
114+
115+
// this is recursive
116+
return run(new_argv[0], new_argv, "", "", "");
117+
}
94118

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

108132
_argv[argv.size()]=NULL;
109133

110-
// warning: the arguments may still need escaping
134+
// warning: the arguments may still need escaping,
135+
// as windows will concatenate the argv strings back together,
136+
// separating them with spaces
111137

112138
std::wstring wide_what=widen(what);
113-
114139
int status=_wspawnvp(_P_WAIT, wide_what.c_str(), _argv.data());
115-
116140
return status;
117141

118142
#else

0 commit comments

Comments
 (0)