R_API int r_cons_readchar() { char buf[2]; buf[0] = -1; #if __WINDOWS__ && !__CYGWIN__ //&& !MINGW32 #if 1 // if something goes wrong set this to 0. skuater..... return readchar_win(0); #endif BOOL ret; DWORD out; DWORD mode; HANDLE h = GetStdHandle (STD_INPUT_HANDLE); GetConsoleMode (h, &mode); SetConsoleMode (h, 0); // RAW ret = ReadConsole (h, buf, 1, &out, NULL); FlushConsoleInputBuffer (h); if (!ret) { return -1; } SetConsoleMode (h, mode); #else r_cons_set_raw (1); if (read (0, buf, 1) == -1) { return -1; } r_cons_set_raw (0); #endif return r_cons_controlz (buf[0]); }
R_API int r_cons_readchar() { void *bed; char buf[2]; buf[0] = -1; if (readbuffer_length > 0) { int ch = *readbuffer; readbuffer_length--; memmove (readbuffer, readbuffer + 1, readbuffer_length); return ch; } #if __WINDOWS__ && !__CYGWIN__ //&& !MINGW32 #if 1 // if something goes wrong set this to 0. skuater..... return readchar_win(0); #endif BOOL ret; DWORD out; DWORD mode; HANDLE h = GetStdHandle (STD_INPUT_HANDLE); GetConsoleMode (h, &mode); SetConsoleMode (h, 0); // RAW bed = r_cons_sleep_begin (); ret = ReadConsole (h, buf, 1, &out, NULL); r_cons_sleep_end (bed); FlushConsoleInputBuffer (h); if (!ret) { return -1; } SetConsoleMode (h, mode); #else r_cons_set_raw (1); bed = r_cons_sleep_begin (); // Blocks until either stdin has something to read or a signal happens. // This serves to check if the terminal window was resized. It avoids the race // condition that could happen if we did not use pselect or select in case SIGWINCH // was handled immediately before the blocking call (select or read). The race is // prevented from happening by having SIGWINCH blocked process-wide except for in // pselect (that is what pselect is for). fd_set readfds; sigset_t sigmask; FD_ZERO (&readfds); FD_SET (STDIN_FILENO, &readfds); r_signal_sigmask (0, NULL, &sigmask); sigdelset (&sigmask, SIGWINCH); pselect (STDIN_FILENO + 1, &readfds, NULL, NULL, NULL, &sigmask); if (sigwinchFlag != 0) { resizeWin (); } ssize_t ret = read (STDIN_FILENO, buf, 1); r_cons_sleep_end (bed); if (ret != 1) { return -1; } if (bufactive) { r_cons_set_raw (0); } #endif return r_cons_controlz (buf[0]); }
R_API int r_cons_readchar_timeout(ut32 usec) { #if __UNIX__ struct timeval tv; fd_set fdset, errset; FD_ZERO (&fdset); FD_ZERO (&errset); FD_SET (0, &fdset); tv.tv_sec = 0; // usec / 1000; tv.tv_usec = 1000 * usec; r_cons_set_raw (1); if (select (1, &fdset, NULL, &errset, &tv) == 1) { return r_cons_readchar (); } r_cons_set_raw (0); // timeout return -1; #else return readchar_win (usec); #endif }