14 #define ErrorBufferSize 4096
15 #define dwMaxHandles 32
19 static char ErrorBuffer[ErrorBufferSize];
23 static bool pipex_eof_input(pipex_t *pipex);
24 static void pipex_set_eof_input(pipex_t *pipex);
25 static void pipex_set_active(pipex_t *pipex);
26 static int pipex_read_data(pipex_t *pipex);
27 static void pipex_read_input(pipex_t *pipex);
33 static char * win32_error(){
35 FORMAT_MESSAGE_FROM_SYSTEM,
47 static DWORD WINAPI ThreadProc(LPVOID lpParam){
48 pipex_t *p=(pipex_t *) lpParam;
49 while(!pipex_eof_input(p)){
50 if(p->nReadEnd<LINE_INPUT_MAX_CHAR-1){
53 // wait until there is room in buffer
62 void pipex_open(pipex_t *pipex,
64 const char *szWorkingDir,
65 const char *szProcFile) {
66 DWORD dwMode, dwThreadId;
67 HANDLE hStdinRead, hStdinWrite, hStdoutRead, hStdoutWrite, hThread;
68 SECURITY_ATTRIBUTES sa;
70 PROCESS_INFORMATION pi;
75 pipex->command=szProcFile;
76 pipex->quit_pending=FALSE;
78 if (szProcFile == NULL) {
79 pipex->hInput = GetStdHandle(STD_INPUT_HANDLE);
80 pipex->hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
81 pipex->bConsole = GetConsoleMode(pipex->hInput, &dwMode);
84 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
85 sa.bInheritHandle = TRUE;
86 sa.lpSecurityDescriptor = NULL;
87 CreatePipe(&hStdinRead, &hStdinWrite, &sa, 0);
88 CreatePipe(&hStdoutRead, &hStdoutWrite, &sa, 0);
89 si.cb = sizeof(STARTUPINFO);
90 si.lpReserved = si.lpDesktop = si.lpTitle = NULL;
91 si.dwFlags = STARTF_USESTDHANDLES;
93 si.lpReserved2 = NULL;
94 si.hStdInput = hStdinRead;
95 si.hStdOutput = hStdoutWrite;
96 si.hStdError = hStdoutWrite;
97 if((szCurrentDir = (char*)_getcwd( NULL, 0 )) == NULL )
98 my_fatal("pipex_open(): no current directory: %s\n",
100 if(_chdir(szWorkingDir)){
101 my_fatal("pipex_open(): %s: %s\n",
105 if(CreateProcess(NULL,
110 DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP,
115 pipex->hProcess=pi.hProcess;
116 CloseHandle(pi.hThread);
117 CloseHandle(hStdinRead);
118 CloseHandle(hStdoutWrite);
119 pipex->hInput = hStdoutRead;
120 pipex->hOutput = hStdinWrite;
121 pipex->bConsole = FALSE;
124 my_fatal("pipex_open(): %s: %s",szProcFile,win32_error());
126 _chdir(szCurrentDir);
128 if (pipex->bConsole) {
129 SetConsoleMode(pipex->hInput,
130 dwMode & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
131 FlushConsoleInputBuffer(pipex->hInput);
133 fdInput=_open_osfhandle((long) pipex->hInput,_O_RDONLY);
135 my_fatal("pipex_open(): %s",strerror(errno));
137 pipex->fpInput=fdopen(fdInput,"r");
138 if(pipex->fpInput==NULL){
139 my_fatal("pipex_open(): %s",strerror(errno));
142 pipex->lpFeedEnd = NULL;
143 InitializeCriticalSection(&(pipex->CriticalSection));
144 pipex->hEvent=CreateEvent(NULL, // default security
146 FALSE, // not signaled
149 if(!(pipex->hEvent)){
150 my_fatal("pipex_open(): %s",win32_error());
152 hThread=CreateThread(NULL, // default security
153 0, // default stacksize
154 ThreadProc, // worker function
155 pipex, // tell worker about ourselves
156 0, // run immediately
157 &dwThreadId // dropped, but needed for the call to work in Win9x
160 my_fatal("pipex_open(): %s",win32_error());
162 pipex->dwWriteIndex=0;
163 pipex_set_active(pipex);
167 // pipex_wait_event(pipex)
169 void pipex_wait_event(pipex_t *pipex[]){
170 HANDLE hHandles[dwMaxHandles];
171 DWORD dwHandleCount=0;
173 while((p=pipex[dwHandleCount])!=NULL){
174 ASSERT((p->hEvent)!=0);
175 if(dwHandleCount>=dwMaxHandles){
176 my_fatal("pipex_wait_event(): Too many objects to wait for");
178 hHandles[dwHandleCount++]=p->hEvent;
180 WaitForMultipleObjects(dwHandleCount, // count
182 FALSE, // return if one object is signaled
183 INFINITE // no timeout
189 void pipex_send_eof(pipex_t *pipex) {
190 my_log("Adapter->%s: EOF\n",pipex->name);
191 CloseHandle(pipex->hOutput);
196 void pipex_exit(pipex_t *pipex) {
198 CloseHandle(pipex->hInput);
199 CloseHandle(pipex->hOutput);
200 if(!pipex->quit_pending){
201 // suppress further errors
202 pipex->quit_pending=TRUE;
203 my_fatal("pipex_exit(): %s: child exited unexpectedly.\n",pipex->command);
205 if(GetExitCodeProcess(pipex->hProcess,&lpexit)){
206 if(lpexit==STILL_ACTIVE)
207 //must be java,hammer it down!
208 TerminateProcess(pipex->hProcess,lpexit);
210 CloseHandle(pipex->hProcess);
215 static bool pipex_eof_input(pipex_t *pipex){
217 EnterCriticalSection(&(pipex->CriticalSection));
218 ret=(pipex->state)&PIPEX_EOF;
219 LeaveCriticalSection(&(pipex->CriticalSection));
223 // pipex_set_eof_input()
225 static void pipex_set_eof_input(pipex_t *pipex){
226 EnterCriticalSection(&(pipex->CriticalSection));
227 (pipex->state)|=PIPEX_EOF;
228 LeaveCriticalSection(&(pipex->CriticalSection));
234 * This function returns TRUE if and only if the pipes have succesfully
235 * been created and the client has been started.
239 bool pipex_active(pipex_t *pipex){
241 EnterCriticalSection(&(pipex->CriticalSection));
242 ret=(pipex->state)&PIPEX_ACTIVE;
243 LeaveCriticalSection(&(pipex->CriticalSection));
247 // pipex_set_active()
249 static void pipex_set_active(pipex_t *pipex){
250 EnterCriticalSection(&(pipex->CriticalSection));
251 (pipex->state)|=PIPEX_ACTIVE;
252 LeaveCriticalSection(&(pipex->CriticalSection));
257 static int pipex_read_data(pipex_t *pipex){
260 // No protection. Access to nReadEnd is atomic.
261 // It is not a problem that nReadEnd becomes smaller after the call.
262 // This just means we have read less than we could have.
263 ret=fgets(pipex->lpReadBuffer,
264 LINE_INPUT_MAX_CHAR-(pipex->nReadEnd),
267 pipex_set_eof_input(pipex);
268 (pipex->lpReadBuffer)[0]='\0';
271 dwBytes=strlen(pipex->lpReadBuffer);
272 (pipex->lpReadBuffer)[dwBytes]='\0';
276 // pipex_read_input()
278 static void pipex_read_input(pipex_t *pipex) {
280 BOOL bSetEvent=FALSE;
281 // ReadData is outside the critical section otherwise everything
282 // would block during the blocking read
283 ret=pipex_read_data(pipex);
284 EnterCriticalSection(&(pipex->CriticalSection));
285 if(!pipex_eof_input(pipex)){
286 if(ret+(pipex->nReadEnd)>=LINE_INPUT_MAX_CHAR){
287 my_fatal("pipex_read_input(): Internal error: buffer overflow\n");
289 memcpy((pipex->lpBuffer)+(pipex->nReadEnd),(pipex->lpReadBuffer),ret+1);
290 (pipex->nReadEnd) += ret;
291 if(!(pipex->lpFeedEnd)){
293 (char *) memchr(pipex->lpBuffer,'\n',pipex->nReadEnd);
295 if(pipex->lpFeedEnd){
297 }else if((pipex->nReadEnd)>=LINE_INPUT_MAX_CHAR-1){
298 my_fatal("pipex_read_input(): LINE_INPUT_MAX_CHAR is equal to %d which is too small to contain a full line of engine output or GUI input.\n",LINE_INPUT_MAX_CHAR);
301 LeaveCriticalSection(&(pipex->CriticalSection));
302 if(pipex_eof_input(pipex) || bSetEvent){
303 SetEvent(pipex->hEvent);
310 * This function returns TRUE if and only if the input buffer does not
311 * contain a full line of data and EOF was encountered by
312 * the working thread.
316 bool pipex_eof(pipex_t *pipex){
318 EnterCriticalSection(&(pipex->CriticalSection));
319 if((pipex->lpFeedEnd) != NULL){
321 }else if(pipex_eof_input(pipex)){
326 LeaveCriticalSection(&(pipex->CriticalSection));
333 * This function returns FALSE if and only if the asynchronously filled
334 * input buffer does not contain a full line of data.
335 * In other words it operates in non-blocking mode.
339 bool pipex_readln_nb(pipex_t *pipex, char *szLineStr) {
344 EnterCriticalSection(&(pipex->CriticalSection));
345 if ((pipex->lpFeedEnd) == NULL) {
348 nFeedEnd = pipex->lpFeedEnd - pipex->lpBuffer;
349 memcpy(szLineStr, pipex->lpBuffer, nFeedEnd+1);
350 szLineStr[nFeedEnd] = '\0';
352 // temp hack: stolen from util.c
353 // remove CRs and LFs
356 while ((c=szLineStr[src++]) != '\0') {
357 if (c != '\r' && c != '\n') szLineStr[dst++] = c;
359 szLineStr[dst] = '\0';
360 ASSERT(strchr(szLineStr,'\n')==NULL)
361 ASSERT(strchr(szLineStr,'\r')==NULL)
364 pipex->nReadEnd -= nFeedEnd;
365 memcpy(pipex->lpBuffer, pipex->lpBuffer + nFeedEnd, pipex->nReadEnd+1);
367 (char *) memchr(pipex->lpBuffer, '\n', pipex->nReadEnd);
370 LeaveCriticalSection(&(pipex->CriticalSection));
372 my_log("%s->Adapter: %s\n",pipex->name,szLineStr);
380 * This function returns FALSE if and only if EOF has been encountered by
381 * the working thread and no full line of data is present in the input buffer.
383 * If there is a full line of data present in the input buffer it returns
386 * If none of these conditions is satisfied it blocks.
388 * As the name say this function is strictly for line input.
389 * An incomplete line of data (i.e. not ending with \n) is lost when EOF
394 bool pipex_readln(pipex_t *pipex, char *szLineStr) {
395 while(!pipex_eof(pipex)){
396 if (pipex_readln_nb(pipex,szLineStr)) {
399 WaitForSingleObject(pipex->hEvent,INFINITE);
402 my_log("%s->Adapter: EOF\n",pipex->name);
407 // GetWin32Priority()
409 static DWORD GetWin32Priority(int nice)
412 REALTIME_PRIORITY_CLASS 0x00000100
413 HIGH_PRIORITY_CLASS 0x00000080
414 ABOVE_NORMAL_PRIORITY_CLASS 0x00008000
415 NORMAL_PRIORITY_CLASS 0x00000020
416 BELOW_NORMAL_PRIORITY_CLASS 0x00004000
417 IDLE_PRIORITY_CLASS 0x00000040
419 if (nice < -15) return 0x00000080;
420 if (nice < 0) return 0x00008000;
421 if (nice == 0) return 0x00000020;
422 if (nice < 15) return 0x00004000;
426 // pipex_set_priority()
428 void pipex_set_priority(pipex_t *pipex, int value){
430 if(!SetPriorityClass(pipex->hProcess,
431 GetWin32Priority(value))){
432 my_log("POLYGLOT Unable to change priority\n");
437 // pipex_set_affinit()
439 typedef void (WINAPI *SPAM)(HANDLE, int);
440 void pipex_set_affinity(pipex_t *pipex, int value){
443 if(pipex->hProcess) return;
444 if(value==-1) return;
446 pSPAM = (SPAM) GetProcAddress(
447 GetModuleHandle(TEXT("kernel32.dll")),
448 "SetProcessAffinityMask");
450 // [HGM] avoid crash on Win95 by first checking if API call exists
451 pSPAM(pipex->hProcess,value);
453 my_log("POLYGLOT API call \"SetProcessAffinityMask\" not available\n");
459 void pipex_write(pipex_t *pipex, const char *szLineStr) {
461 size=sizeof(pipex->szWriteBuffer)-pipex->dwWriteIndex;
462 written=snprintf(pipex->szWriteBuffer + pipex->dwWriteIndex,
466 // snprintf returns how many bytes should have been written
467 // (not including the trailing zero)
468 // old versions of glibc and msvcrt return -1 in
469 // case of truncated output.
470 if(written>=size || written<0){
471 my_fatal("engine_send(): write_buffer overflow\n");
473 pipex->dwWriteIndex+=written;
480 void pipex_writeln(pipex_t *pipex, const char *szLineStr) {
482 DWORD dwLengthWriteBuffer;
483 pipex_write(pipex, szLineStr);
484 my_log("Adapter->%s: %s\n",pipex->name,pipex->szWriteBuffer);
486 dwLengthWriteBuffer = strlen(pipex->szWriteBuffer);
487 if(dwLengthWriteBuffer>=sizeof(pipex->szWriteBuffer)-3){
488 my_fatal("pipex_writeln(): write buffer overflow\n");
490 pipex->szWriteBuffer[dwLengthWriteBuffer] = '\r';
491 pipex->szWriteBuffer[dwLengthWriteBuffer + 1] = '\n';
492 // for easy debugging
493 pipex->szWriteBuffer[dwLengthWriteBuffer + 2] = '\0';
494 WriteFile(pipex->hOutput, pipex->szWriteBuffer,
495 dwLengthWriteBuffer + 2,
498 printf("%s\n",pipex->szWriteBuffer);
501 pipex->dwWriteIndex = 0;