--- gforth/engine/io.c 2007/07/14 19:57:16 1.30 +++ gforth/engine/io.c 2009/06/29 20:21:28 1.36 @@ -1,12 +1,12 @@ /* direct key io driver - Copyright (C) 1995,1996,1997,1998,1999,2002,2003,2006 Free Software Foundation, Inc. + Copyright (C) 1995,1996,1997,1998,1999,2002,2003,2006,2007,2008 Free Software Foundation, Inc. This file is part of Gforth. Gforth is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 + as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, @@ -15,8 +15,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. + along with this program; if not, see http://www.gnu.org/licenses/. The following is stolen from the readline library for bash */ @@ -40,6 +39,7 @@ typedef unsigned int uint32_t; #endif +#include #include #include #include @@ -618,20 +618,77 @@ void deprep_terminal () } #endif /* NEW_TTY_DRIVER */ -long key_avail (FILE * stream) +/* an ungetc() variant where we can know that there is a character waiting: + gf_ungetc: like ungetc + gf_regetc: call when reading a char, but does not get the character + gf_ungottenc: true if there is a char waiting + + Implementation: just an array containing all the FILEs with ungotten chars. + Sequential search for membership checking (there should not be many elements) +*/ + +static FILE **ungotten_files = NULL; +static size_t n_ungotten = 0; /* actual number */ +static size_t max_ungotten = 0; /* buffer size */ + +int gf_ungetc(int c, FILE *stream) +/* like ungetc, but works with the others */ +{ + if (n_ungotten>=max_ungotten) { + max_ungotten = max_ungotten*2+1; + ungotten_files = realloc(ungotten_files, max_ungotten*sizeof(FILE *)); + } + ungotten_files[n_ungotten++] = stream; + return ungetc(c, stream); +} + +static long search_ungotten(FILE *stream) +{ + long i; + for (i=0; i=0) + ungotten_files[i] = ungotten_files[--n_ungotten]; +} + +int gf_ungottenc(FILE *stream) +/* true if stream has been ungotten */ +{ + return search_ungotten(stream)>=0; +} + +long key_avail (FILE *stream) { int tty = fileno (stream); fd_set selin; - static struct timespec now = { 0 , 0 }; - int res; + static struct timeval now = { 0 , 0 }; + int chars_avail; 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); - return select(1, &selin, NULL, NULL, &now); + chars_avail = select(1, &selin, NULL, NULL, &now); + if (chars_avail > 0) { + /* getc won't block */ + int c = getc(stream); + if (c==EOF) + return 0; + gf_ungetc(c, stream); + } + return (chars_avail == -1) ? 0 : chars_avail; } /* Get a key from the buffer of characters to be read.