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::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;
}