Finally a big silliness and headache cleared ! I remember now that scanf() waits until I press "enter" so fgets() should wait similarly for a stop signal.
I went through some past posts and now managed to put together the
shortest unsafe input polling on planet earth; just one line "pollInput".
- Code: Select all
// courtesy of Uri thru Dieter
void input_init(void){
DWORD dw;
//PeekNamedPipe(0,....) fails as stdin ( handle 0 ) has been redirected ?
input_handle = GetStdHandle(STD_INPUT_HANDLE);
is_pipe = !GetConsoleMode(input_handle, &dw);
}
void pollInput(HANDLE input_handle, searchInfo_t * searchInfo){
int i;
unsigned long l;
char line[256];
char command[256];
if (PeekNamedPipe(input_handle, line, 255, &l, NULL, NULL) && l > 0){
while ( (i = sscanf(line,"%s\n",command)) == 1){
if (!strcmp(command, "new")){
longjmp(searchInfo->env, 1);
}
if (!strcmp(command, "?")){//move now
longjmp(searchInfo->env, 1);
}
if (!strcmp(command, "result")){
longjmp(searchInfo->env, 1);
}
if (!strcmp(command, "quit")){
longjmp(searchInfo->env, 1);
}
strcpy(command, strchr(line, '\n') + 1);
strcpy(line, command);
}
}
}
if (!(++node & pollFrequency) ){
#ifndef _DEBUG
pollInput(input_handle, searchInfo);
#endif
}
Best Regards,
Rasjid