5 void PipeStruct::Open(const char *szProcFile) {
7 HANDLE hStdinRead, hStdinWrite, hStdoutRead, hStdoutWrite;
8 SECURITY_ATTRIBUTES sa;
10 PROCESS_INFORMATION pi;
12 if (szProcFile == NULL) {
13 hInput = GetStdHandle(STD_INPUT_HANDLE);
14 hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
15 bConsole = GetConsoleMode(hInput, &dwMode);
19 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
20 sa.bInheritHandle = TRUE;
21 sa.lpSecurityDescriptor = NULL;
22 CreatePipe(&hStdinRead, &hStdinWrite, &sa, 0);
23 CreatePipe(&hStdoutRead, &hStdoutWrite, &sa, 0);
24 si.cb = sizeof(STARTUPINFO);
25 si.lpReserved = si.lpDesktop = si.lpTitle = NULL;
26 si.dwFlags = STARTF_USESTDHANDLES;
28 si.lpReserved2 = NULL;
29 si.hStdInput = hStdinRead;
30 si.hStdOutput = hStdoutWrite;
31 si.hStdError = hStdoutWrite;
32 if(CreateProcess(NULL, (LPSTR) szProcFile, NULL, NULL, TRUE, DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP, NULL, NULL, &si, &pi)){
35 CloseHandle(pi.hThread);
36 CloseHandle(hStdinRead);
37 CloseHandle(hStdoutWrite);
39 hOutput = hStdinWrite;
44 SetConsoleMode(hInput, dwMode & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
45 FlushConsoleInputBuffer(hInput);
52 void PipeStruct::Close(void) const {
56 void PipeStruct::Kill(void) const {
61 if(GetExitCodeProcess(hProcess,&lpexit)){
62 if(lpexit==STILL_ACTIVE)
63 //must be java,hammer it down!
64 TerminateProcess(hProcess,lpexit);
66 CloseHandle(hProcess);
69 bool PipeStruct::EOF_(void){ // EOF is defined
70 return state&PIPE_EOF;
73 bool PipeStruct::Active(void){
74 return state&PIPE_ACTIVE;
77 void PipeStruct::ReadInput(void) {
79 if(ReadFile(hInput, szBuffer + nReadEnd, LINE_INPUT_MAX_CHAR - nReadEnd, &dwBytes, NULL)){
82 nBytesLeft -= dwBytes;
85 state|=PIPE_EOF; // if we are here there should be data!
89 bool PipeStruct::CheckInput(void) {
90 DWORD dwEvents, dwBytes;
91 if (bConsole) { // a tty, or an un-redirected handle
92 GetNumberOfConsoleInputEvents(hInput, &dwEvents);
98 } else { // a handle redirected to a pipe or a file
102 if (PeekNamedPipe(hInput, NULL, 0, NULL, &dwBytes, NULL)) {
103 nBytesLeft = dwBytes;
104 return nBytesLeft > 0; // a pipe
106 return TRUE; // a file, always TRUE
112 void PipeStruct::LineOutput(const char *szLineStr) const {
115 char szWriteBuffer[LINE_INPUT_MAX_CHAR];
116 nStrLen = strlen(szLineStr);
117 memcpy(szWriteBuffer, szLineStr, nStrLen);
118 szWriteBuffer[nStrLen] = '\r';
119 szWriteBuffer[nStrLen + 1] = '\n';
120 WriteFile(hOutput, szWriteBuffer, nStrLen + 2, &dwBytes, NULL);
125 bool PipeStruct::GetBuffer(char *szLineStr) {
128 lpFeedEnd = (char *) memchr(szBuffer, '\n', nReadEnd);
129 if (lpFeedEnd == NULL) {
132 nFeedEnd = lpFeedEnd - szBuffer;
133 memcpy(szLineStr, szBuffer, nFeedEnd);
134 if (szLineStr[nFeedEnd - 1] == '\r') {
135 szLineStr[nFeedEnd - 1] = '\0';
137 szLineStr[nFeedEnd] = '\0';
140 nReadEnd -= nFeedEnd;
141 memcpy(szBuffer, szBuffer + nFeedEnd, nReadEnd);
146 bool PipeStruct::LineInput(char *szLineStr) {
147 if (GetBuffer(szLineStr)) {
152 if (GetBuffer(szLineStr)) {
155 if (nReadEnd == LINE_INPUT_MAX_CHAR) {
156 memcpy(szLineStr, szBuffer, LINE_INPUT_MAX_CHAR - 1);
157 szLineStr[LINE_INPUT_MAX_CHAR - 1] = '\0';
158 szBuffer[0] = szBuffer[LINE_INPUT_MAX_CHAR - 1];