--- gforth/engine/io.c 2009/06/29 20:21:28 1.36 +++ gforth/engine/io.c 2012/04/03 22:57:00 1.42 @@ -1,6 +1,6 @@ /* direct key io driver - Copyright (C) 1995,1996,1997,1998,1999,2002,2003,2006,2007,2008 Free Software Foundation, Inc. + Copyright (C) 1995,1996,1997,1998,1999,2002,2003,2006,2007,2008,2009,2010 Free Software Foundation, Inc. This file is part of Gforth. @@ -447,8 +447,6 @@ void deprep_terminal () #define VTIME VEOL #endif -#include - void prep_terminal () { int tty = fileno (stdin); @@ -474,9 +472,6 @@ void prep_terminal () return; /* added by MdG */ } /* added by MdG */ - setlocale(LC_ALL, ""); - setlocale(LC_NUMERIC, "C"); - /* Try to keep this function from being INTerrupted. We can do it on POSIX and systems with BSD-like signal handling. */ #if defined (HAVE_POSIX_SIGNALS) @@ -672,15 +667,22 @@ long key_avail (FILE *stream) static struct timeval now = { 0 , 0 }; int chars_avail; + if (gf_ungottenc(stream)) + return 1; setvbuf(stream, NULL, _IONBF, 0); if(!terminal_prepped && stream == stdin) prep_terminal(); - if (gf_ungottenc(stream)) - return 1; - FD_ZERO(&selin); - FD_SET(tty, &selin); - chars_avail = select(1, &selin, NULL, NULL, &now); +#if defined(FIONREAD) && !defined(_WIN32) + if(isatty (tty)) { + int result = ioctl (tty, FIONREAD, &chars_avail); + } else +#endif + { + FD_ZERO(&selin); + FD_SET(tty, &selin); + chars_avail = select(1, &selin, NULL, NULL, &now); + } if (chars_avail > 0) { /* getc won't block */ int c = getc(stream); @@ -703,11 +705,14 @@ Cell getkey(FILE * stream) Cell result; unsigned char c; - setvbuf(stream, NULL, _IONBF, 0); + if (!gf_ungottenc(stream)) + setvbuf(stream, NULL, _IONBF, 0); if(!terminal_prepped && stream == stdin) prep_terminal(); result = fread(&c, sizeof(c), 1, stream); + if (result>0) + gf_regetc(stream); return result==0 ? (errno == EINTR ? 12 : 4) : c; }