void *SC_TerminalClient::readlineFunc( void *arg ) { readlineInit(); SC_TerminalClient *client = static_cast<SC_TerminalClient*>(arg); HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE); HANDLE hnds[] = { client->mQuitInputEvent, hStdIn }; bool shouldRun = true; while (shouldRun) { DWORD result = WaitForMultipleObjects( 2, hnds, false, INFINITE ); if( result == WAIT_FAILED ) { postfl("readline: wait error.\n"); client->onQuit(1); break; } int hIndex = result - WAIT_OBJECT_0; if( hIndex == 0 ) { postfl("readline: quit requested.\n"); break; } if( hIndex == 1 ) { rl_callback_read_char(); } } postfl("readline: stopped.\n"); return NULL; }
void *SC_TerminalClient::readlineFunc( void *arg ) { readlineInit(); SC_TerminalClient *client = static_cast<SC_TerminalClient*>(arg); fd_set fds; FD_ZERO(&fds); while(true) { FD_SET(STDIN_FD, &fds); FD_SET(client->mInputCtlPipe[0], &fds); if( select(FD_SETSIZE, &fds, NULL, NULL, NULL) < 0 ) { if( errno == EINTR ) continue; postfl("readline: select() error:\n%s\n", strerror(errno)); client->onQuit(1); break; } if( FD_ISSET(client->mInputCtlPipe[0], &fds) ) { postfl("readline: quit requested\n"); break; } if( FD_ISSET(STDIN_FD, &fds) ) { rl_callback_read_char(); } } postfl("readline: stopped.\n"); return NULL; }
void SC_TerminalClient::readlineCmdLine( char *cmdLine ) { SC_TerminalClient *client = static_cast<SC_TerminalClient*>(instance()); if( cmdLine == NULL ) { postfl("\nExiting sclang (ctrl-D)\n"); client->onQuit(0); return; } if(*cmdLine!=0){ // If line wasn't empty, store it so that uparrow retrieves it add_history(cmdLine); int len = (int)strlen(cmdLine); client->lockInput(); client->mInputBuf.append(cmdLine, len); client->mInputBuf.append(kInterpretPrintCmdLine); client->sendSignal(sig_input); // Wait for input to be processed, // so that its output is displayed before readline prompt. if (client->mInputShouldBeRunning) pthread_cond_wait( &client->mInputCond, &client->mInputMutex ); client->unlockInput(); } }
void *SC_TerminalClient::pipeFunc( void *arg ) { SC_TerminalClient *client = static_cast<SC_TerminalClient*>(arg); SC_StringBuffer stack; HANDLE hStdIn = GetStdHandle(STD_INPUT_HANDLE); char buf[256]; while(1) { DWORD n; BOOL ok = ReadFile( hStdIn, &buf, 256, &n, NULL ); if(ok) { client->pushCmdLine( stack, buf, n ); } else { postfl("pipe-in: ERROR (ReadFile): %i\n", GetLastError()); client->onQuit(1); break; } } return NULL; }
void SC_TerminalClient::readlineCmdLine( char *cmdLine ) { SC_TerminalClient *client = static_cast<SC_TerminalClient*>(instance()); if( cmdLine == NULL ) { postfl("\nExiting sclang (ctrl-D)\n"); client->onQuit(0); return; } if( *cmdLine != 0 ) { // If line wasn't empty, store it so that uparrow retrieves it add_history(cmdLine); int len = strlen(cmdLine); client->mInputBuf.append(cmdLine, len); client->mInputBuf.append(kInterpretPrintCmdLine); client->sendSignal(sig_input); client->mReadlineSem.wait(); } }
void *SC_TerminalClient::pipeFunc( void *arg ) { SC_TerminalClient *client = static_cast<SC_TerminalClient*>(arg); ssize_t bytes; const size_t toRead = 256; char buf[toRead]; SC_StringBuffer stack; fd_set fds; FD_ZERO(&fds); bool shouldRead = true; while(shouldRead) { FD_SET(STDIN_FD, &fds); FD_SET(client->mInputCtlPipe[0], &fds); if( select(FD_SETSIZE, &fds, NULL, NULL, NULL) < 0 ) { if( errno == EINTR ) continue; postfl("pipe-in: select() error: %s\n", strerror(errno)); client->onQuit(1); break; } if( FD_ISSET(client->mInputCtlPipe[0], &fds) ) { postfl("pipe-in: quit requested\n"); break; } if( FD_ISSET(STDIN_FD, &fds) ) { while(true) { bytes = read( STDIN_FD, buf, toRead ); if( bytes > 0 ) { client->pushCmdLine( stack, buf, bytes ); } else if( bytes == 0 ) { postfl("pipe-in: EOF. Will quit.\n"); client->onQuit(0); shouldRead = false; break; } else { if( errno == EAGAIN ) { break; // no more to read this time; } else if( errno != EINTR ){ postfl("pipe-in: read() error: %s\n", strerror(errno)); client->onQuit(1); shouldRead = false; break; } } } } } postfl("pipe-in: stopped.\n"); return NULL; }