-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPipes.d
211 lines (174 loc) · 5.96 KB
/
Pipes.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/+
+ Copyright Andrej Mitrovic 2011.
+ Copyright Tomasz Stachowiak 2009 - 2011.
+ Distributed under the Boost Software License, Version 1.0.
+ (See accompanying file LICENSE_1_0.txt or copy at
+ http://www.boost.org/LICENSE_1_0.txt)
+/
module xfbuild.Pipes;
import xfbuild.Exception;
version (Windows)
{
import core.memory;
import core.runtime;
import core.thread;
import core.stdc.string;
import std.concurrency;
import std.parallelism;
import std.conv;
import std.exception;
import std.file;
import std.math;
import std.range;
import std.string;
import std.utf;
import std.process;
import win32.windef;
import win32.winuser;
import win32.wingdi;
import win32.winbase;
import std.algorithm;
import std.array;
import std.stdio;
import std.conv;
import std.typetuple;
import std.typecons;
import std.traits;
enum BUFSIZE = 4096;
wstring fromUTF16z(const wchar* s)
{
if (s is null) return null;
wchar* ptr;
for (ptr = cast(wchar*)s; *ptr; ++ptr) {}
return to!wstring(s[0..ptr-s]);
}
auto toUTF16z(S)(S s)
{
return toUTFz!(const(wchar)*)(s);
}
struct ProcessInfo
{
string procName;
HANDLE childStdinRead;
HANDLE childStdinWrite;
HANDLE childStdoutRead;
HANDLE childStdoutWrite;
}
ProcessInfo createProcessPipes()
{
ProcessInfo pi;
createProcessPipes(pi);
return pi;
}
void createProcessPipes(ref ProcessInfo procInfo)
{
SECURITY_ATTRIBUTES saAttr;
// Set the bInheritHandle flag so pipe handles are inherited.
saAttr.nLength = SECURITY_ATTRIBUTES.sizeof;
saAttr.bInheritHandle = true;
with (procInfo)
{
// Create a pipe for the child process's STDOUT.
if (!CreatePipe(/* out */ &childStdoutRead, /* out */ &childStdoutWrite, &saAttr, 0) )
ErrorExit(("StdoutRd CreatePipe"));
// Ensure the read handle to the pipe for STDOUT is not inherited (sets to 0)
if (!SetHandleInformation(childStdoutRead, HANDLE_FLAG_INHERIT, 0) )
ErrorExit(("Stdout SetHandleInformation"));
// Create a pipe for the child process's STDIN.
if (!CreatePipe(&childStdinRead, &childStdinWrite, &saAttr, 0))
ErrorExit(("Stdin CreatePipe"));
// Ensure the write handle to the pipe for STDIN is not inherited. (sets to 0)
if (!SetHandleInformation(childStdinWrite, HANDLE_FLAG_INHERIT, 0) )
ErrorExit(("Stdin SetHandleInformation"));
}
}
int runProcess(string procName, ProcessInfo procInfo)
{
// Create a child process that uses the previously created pipes for STDIN and STDOUT.
auto szCmdline = toUTFz!(wchar*)(procName);
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
BOOL bSuccess = false;
// Set up members of the STARTUPINFO structure.
// This structure specifies the STDIN and STDOUT handles for redirection.
siStartInfo.cb = STARTUPINFO.sizeof;
siStartInfo.hStdError = procInfo.childStdoutWrite; // we should replace this
siStartInfo.hStdOutput = procInfo.childStdoutWrite;
siStartInfo.hStdInput = procInfo.childStdinRead;
siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
if (CreateProcess(NULL,
szCmdline, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
true, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo) == 0) // receives PROCESS_INFORMATION
{
ErrorExit("CreateProcess");
}
else
{
CloseHandle(piProcInfo.hThread);
WaitForSingleObject(piProcInfo.hProcess, INFINITE);
DWORD exitCode = 0;
if (GetExitCodeProcess(piProcInfo.hProcess, &exitCode))
{
// successfully retrieved exit code
return exitCode;
}
CloseHandle(piProcInfo.hProcess);
}
return -1;
}
string readProcessPipeString(ProcessInfo procInfo)
{
// Read output from the child process's pipe for STDOUT
// and write to the parent process's pipe for STDOUT.
// Stop when there is no more data.
DWORD dwRead, dwWritten;
CHAR[BUFSIZE] chBuf;
BOOL bSuccess = false;
HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
string buffer;
// Close the write end of the pipe before reading from the
// read end of the pipe, to control child process execution.
// The pipe is assumed to have enough buffer space to hold the
// data the child process has already written to it.
if (!CloseHandle(procInfo.childStdoutWrite))
ErrorExit(("StdOutWr CloseHandle"));
while (1)
{
bSuccess = ReadFile(procInfo.childStdoutRead, chBuf.ptr, BUFSIZE, &dwRead, NULL);
if (!bSuccess || dwRead == 0)
break;
buffer ~= chBuf[0..dwRead];
}
return buffer;
}
void ErrorExit(string lpszFunction)
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
cast(LPTSTR)&lpMsgBuf,
0, NULL);
lpDisplayBuf = cast(LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen(cast(LPCTSTR)lpMsgBuf) + lstrlen(cast(LPCTSTR)lpszFunction) + 40) * (TCHAR.sizeof));
auto errorMsg = format("%s failed with error %s: %s",
lpszFunction,
dw,
fromUTF16z(cast(wchar*)lpMsgBuf)
);
throw new ProcessExecutionException(errorMsg, __FILE__, __LINE__);
}
} // version(Windows)