25 string Replace(
const string& input,
const string& find,
const string& replace) {
26 string result = input;
28 while ((start_pos = result.find(find, start_pos)) != string::npos) {
29 result.replace(start_pos, find.length(), replace);
30 start_pos += replace.length();
39 return Replace(path,
" ",
"\\ ");
43 SECURITY_ATTRIBUTES security_attributes = {};
44 security_attributes.nLength =
sizeof(SECURITY_ATTRIBUTES);
45 security_attributes.bInheritHandle = TRUE;
49 CreateFileA(
"NUL", GENERIC_READ,
50 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
51 &security_attributes, OPEN_EXISTING, 0, NULL);
52 if (nul == INVALID_HANDLE_VALUE)
53 Fatal(
"couldn't open nul");
55 HANDLE stdout_read, stdout_write;
56 if (!CreatePipe(&stdout_read, &stdout_write, &security_attributes, 0))
57 Win32Fatal(
"CreatePipe");
59 if (!SetHandleInformation(stdout_read, HANDLE_FLAG_INHERIT, 0))
60 Win32Fatal(
"SetHandleInformation");
62 PROCESS_INFORMATION process_info = {};
63 STARTUPINFOA startup_info = {};
64 startup_info.cb =
sizeof(STARTUPINFOA);
65 startup_info.hStdInput = nul;
66 startup_info.hStdError = ::GetStdHandle(STD_ERROR_HANDLE);
67 startup_info.hStdOutput = stdout_write;
68 startup_info.dwFlags |= STARTF_USESTDHANDLES;
70 if (!CreateProcessA(NULL, (
char*)command.c_str(), NULL, NULL,
73 &startup_info, &process_info)) {
74 Win32Fatal(
"CreateProcess");
77 if (!CloseHandle(nul) ||
78 !CloseHandle(stdout_write)) {
79 Win32Fatal(
"CloseHandle");
87 if (!::
ReadFile(stdout_read, buf,
sizeof(buf), &read_len, NULL) &&
88 GetLastError() != ERROR_BROKEN_PIPE) {
89 Win32Fatal(
"ReadFile");
91 output->append(buf, read_len);
95 if (WaitForSingleObject(process_info.hProcess, INFINITE) == WAIT_FAILED)
96 Win32Fatal(
"WaitForSingleObject");
98 if (!GetExitCodeProcess(process_info.hProcess, &exit_code))
99 Win32Fatal(
"GetExitCodeProcess");
101 if (!CloseHandle(stdout_read) ||
102 !CloseHandle(process_info.hProcess) ||
103 !CloseHandle(process_info.hThread)) {
104 Win32Fatal(
"CloseHandle");
string EscapeForDepfile(const string &path)
int Run(const std::string &command, std::string *output)
Start a process and gather its raw output.
void Fatal(const char *msg,...)
Log a fatal message and exit.
int ReadFile(const string &path, string *contents, string *err)