From 2ae3144aa7cf3eea49eb5f02af645634d4290394 Mon Sep 17 00:00:00 2001 From: kris Date: Thu, 1 Feb 2007 14:07:02 +0000 Subject: [PATCH] Merging my input buffer flushing patch for nmap_tty.cc. This uses tcflush() for UNIX and FlushConsoleInputBuffer() for Win32. The more keys that have been queued up, the more of a difference these functions seem to make. That's not to say that they're slow when there's just one :) I've tested it on Windows XP and my Linux box, and it works great on both. If it actually causes some problems, it won't be hard to reverse. --- nmap_tty.cc | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/nmap_tty.cc b/nmap_tty.cc index 83cc4cef2..f5d71c269 100644 --- a/nmap_tty.cc +++ b/nmap_tty.cc @@ -129,6 +129,13 @@ void tty_init() { return; } static int tty_getchar() { return _kbhit() ? _getch() : -1; } static void tty_done() { return; } +static void tty_flush(void) +{ + static HANDLE stdinput = GetStdHandle(STD_INPUT_HANDLE); + + FlushConsoleInputBuffer(stdinput); +} + #else #if !defined(O_NONBLOCK) && defined(O_NDELAY) #define O_NONBLOCK O_NDELAY @@ -183,6 +190,15 @@ static void tty_done() tty_fd = 0; } +static void tty_flush(void) +{ + /* we don't need to test for tty_fd==0 here because + * this isn't called unless we succeeded + */ + + tcflush(tty_fd, TCIFLUSH); +} + /* * Initializes the terminal for unbuffered non-blocking input. Also * registers tty_done() via atexit(). You need to call this before @@ -228,8 +244,7 @@ bool keyWasPressed() return false; if ((c = tty_getchar()) >= 0) { - // Eat any extra keys (so they can't queue up and print forever) - while (tty_getchar() >= 0); + tty_flush(); /* flush input queue */ // printf("You pressed key '%c'!\n", c); if (c == 'v') {