7 DWORD WINAPI ThreadProc(LPVOID lpParam){
8 PipeStruct *p=(PipeStruct *) lpParam;
17 void PipeStruct::Open(const char *szProcFile) {
19 HANDLE hStdinRead, hStdinWrite, hStdoutRead, hStdoutWrite;
20 SECURITY_ATTRIBUTES sa;
22 PROCESS_INFORMATION pi;
24 if (szProcFile == NULL) {
25 hInput = GetStdHandle(STD_INPUT_HANDLE);
26 hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
27 bConsole = GetConsoleMode(hInput, &dwMode);
30 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
31 sa.bInheritHandle = TRUE;
32 sa.lpSecurityDescriptor = NULL;
33 CreatePipe(&hStdinRead, &hStdinWrite, &sa, 0);
34 CreatePipe(&hStdoutRead, &hStdoutWrite, &sa, 0);
35 si.cb = sizeof(STARTUPINFO);
36 si.lpReserved = si.lpDesktop = si.lpTitle = NULL;
37 si.dwFlags = STARTF_USESTDHANDLES;
39 si.lpReserved2 = NULL;
40 si.hStdInput = hStdinRead;
41 si.hStdOutput = hStdoutWrite;
42 si.hStdError = hStdoutWrite;
43 if(CreateProcess(NULL,
48 DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP,
54 CloseHandle(pi.hThread);
55 CloseHandle(hStdinRead);
56 CloseHandle(hStdoutWrite);
58 hOutput = hStdinWrite;
62 my_fatal("PipeStruct::Open(): %s",my_error());
66 SetConsoleMode(hInput,
67 dwMode & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
68 FlushConsoleInputBuffer(hInput);
71 InitializeCriticalSection(&CriticalSection);
72 hEvent=CreateEvent(NULL, // default security
74 FALSE, // not signaled
77 hThread=CreateThread(NULL, // default security
78 0, // default stacksize
79 ThreadProc, // worker function
80 this, // tell worker about ourselves
89 void PipeStruct::Close(void) const {
93 void PipeStruct::Kill(void) const {
98 if(GetExitCodeProcess(hProcess,&lpexit)){
99 if(lpexit==STILL_ACTIVE)
100 //must be java,hammer it down!
101 TerminateProcess(hProcess,lpexit);
103 CloseHandle(hProcess);
106 bool PipeStruct::EOF_(void){ // EOF is defined
108 EnterCriticalSection(&CriticalSection);
110 LeaveCriticalSection(&CriticalSection);
114 void PipeStruct::set_EOF_(void){
115 EnterCriticalSection(&CriticalSection);
117 LeaveCriticalSection(&CriticalSection);
120 bool PipeStruct::Active(void){
122 EnterCriticalSection(&CriticalSection);
123 ret=state&PIPE_ACTIVE;
124 LeaveCriticalSection(&CriticalSection);
128 void PipeStruct::set_Active(void){
129 EnterCriticalSection(&CriticalSection);
131 LeaveCriticalSection(&CriticalSection);
136 int PipeStruct::ReadLine(void){
143 fgets(lpReadBuffer,LINE_INPUT_MAX_CHAR,stdin);
144 start=strlen(lpReadBuffer);
147 lpReadBuffer[0]='\0';
152 // Unfortunately we need to use polling here.
153 // Otherwise Windows returns single bytes if
154 // the engine runs at low priority.
155 // This kills performance.
157 ret=PeekNamedPipe(hInput,
158 NULL, // don't read anything yet
160 NULL, // no we don't read anything!
161 &dwBytes,// now we're talking
162 NULL); // nono we don't read anything
165 lpReadBuffer[0]='\0';
177 LINE_INPUT_MAX_CHAR-start,
182 lpReadBuffer[0]='\0';
187 if (memchr(lpReadBuffer+start1, '\n', dwBytes)){
188 lpReadBuffer[start]='\0';
196 void PipeStruct::ReadInput(void) {
200 EnterCriticalSection(&CriticalSection);
202 if(ret+nReadEnd>=LINE_INPUT_MAX_CHAR){
203 my_fatal("PipeStruct::ReadInput(): buffer overflow\n");
205 memcpy(lpBuffer+nReadEnd,lpReadBuffer,ret);
208 LeaveCriticalSection(&CriticalSection);
212 void PipeStruct::LineOutput(const char *szLineStr) const {
215 char szWriteBuffer[LINE_INPUT_MAX_CHAR];
217 nStrLen = strlen(szLineStr);
218 memcpy(szWriteBuffer, szLineStr, nStrLen);
219 szWriteBuffer[nStrLen] = '\r';
220 szWriteBuffer[nStrLen + 1] = '\n';
221 WriteFile(hOutput, szWriteBuffer, nStrLen + 2, &dwBytes, NULL);
223 printf("%s\n",szLineStr);
228 bool PipeStruct::GetBuffer(char *szLineStr) {
232 EnterCriticalSection(&CriticalSection);
233 lpFeedEnd = (char *) memchr(lpBuffer, '\n', nReadEnd);
234 if (lpFeedEnd == NULL) {
237 nFeedEnd = lpFeedEnd - lpBuffer;
238 memcpy(szLineStr, lpBuffer, nFeedEnd);
239 if (szLineStr[nFeedEnd - 1] == '\r') {
240 szLineStr[nFeedEnd - 1] = '\0';
242 szLineStr[nFeedEnd] = '\0';
245 nReadEnd -= nFeedEnd;
246 memcpy(lpBuffer, lpBuffer + nFeedEnd, nReadEnd);
249 LeaveCriticalSection(&CriticalSection);
253 void PipeStruct::LineInput(char *szLineStr) {
255 if (GetBuffer(szLineStr)) {
258 WaitForSingleObject(hEvent,INFINITE);