version 1.14, 2002/03/13 17:09:09
|
version 1.44, 2012/07/22 22:24:50
|
Line 1
|
Line 1
|
/* direct key io driver |
/* direct key io driver |
|
|
Copyright (C) 1995,1996,1997,1998,1999 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. |
This file is part of Gforth. |
|
|
Gforth is free software; you can redistribute it and/or |
Gforth is free software; you can redistribute it and/or |
modify it under the terms of the GNU General Public License |
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. |
of the License, or (at your option) any later version. |
|
|
This program is distributed in the hope that it will be useful, |
This program is distributed in the hope that it will be useful, |
Line 15
|
Line 15
|
GNU General Public License for more details. |
GNU General Public License for more details. |
|
|
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
along with this program; if not, write to the Free Software |
along with this program; if not, see http://www.gnu.org/licenses/. |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. |
|
|
|
The following is stolen from the readline library for bash |
The following is stolen from the readline library for bash |
*/ |
*/ |
Line 26
|
Line 25
|
*/ |
*/ |
|
|
#include "config.h" |
#include "config.h" |
|
#include "forth.h" |
#include <sys/time.h> |
#include <sys/time.h> |
#include <sys/types.h> |
#include <sys/types.h> |
#include <unistd.h> |
#include <unistd.h> |
Line 39
|
Line 39
|
typedef unsigned int uint32_t; |
typedef unsigned int uint32_t; |
#endif |
#endif |
|
|
|
#include <stdlib.h> |
#include <stdio.h> |
#include <stdio.h> |
#include <signal.h> |
#include <signal.h> |
#include <string.h> |
#include <string.h> |
Line 51 typedef unsigned int uint32_t;
|
Line 52 typedef unsigned int uint32_t;
|
#include <sys/filio.h> |
#include <sys/filio.h> |
#endif |
#endif |
#include <setjmp.h> |
#include <setjmp.h> |
#include "forth.h" |
|
#include "io.h" |
#include "io.h" |
|
|
#ifndef MSDOS |
#ifndef MSDOS |
|
#include <poll.h> |
#if defined (__GNUC__) |
#if defined (__GNUC__) |
# define alloca __builtin_alloca |
# define alloca __builtin_alloca |
#else |
#else |
Line 613 void deprep_terminal ()
|
Line 614 void deprep_terminal ()
|
} |
} |
#endif /* NEW_TTY_DRIVER */ |
#endif /* NEW_TTY_DRIVER */ |
|
|
/* If a character is available to be read, then read it |
/* an ungetc() variant where we can know that there is a character waiting: |
and stuff it into IBUFFER. Otherwise, just return. */ |
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); |
|
} |
|
|
long pending = -1L; |
static long search_ungotten(FILE *stream) |
|
{ |
|
long i; |
|
for (i=0; i<n_ungotten; i++) |
|
if (ungotten_files[i] == stream) |
|
return i; |
|
return -1; |
|
} |
|
|
|
void gf_regetc(FILE *stream) |
|
/* remove STREAM from ungotten_files */ |
|
{ |
|
long i = search_ungotten(stream); |
|
if (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) |
long key_avail (FILE *stream) |
{ |
{ |
int tty = fileno (stream); |
int tty = fileno (stream); |
int chars_avail = pending; |
struct pollfd fds = { tty, POLLIN, 0 }; |
int result; |
int chars_avail; |
|
|
if(!terminal_prepped) prep_terminal(); |
if (gf_ungottenc(stream)) |
|
return 1; |
|
setvbuf(stream, NULL, _IONBF, 0); |
|
if(!terminal_prepped && stream == stdin) |
|
prep_terminal(); |
|
|
#if defined(FIONREAD) && !defined(_WIN32) |
#if defined(FIONREAD) && !defined(_WIN32) |
if(isatty (tty)) { |
if(isatty (tty)) { |
result = ioctl (tty, FIONREAD, &chars_avail); |
int result = ioctl (tty, FIONREAD, &chars_avail); |
} |
} else |
#else |
#endif |
{ |
{ |
fd_set selin; |
chars_avail = poll(&fds, 1, 0); |
static int now[2] = { 0 , 0 }; |
|
int res; |
|
|
|
FD_ZERO(&selin); |
|
FD_SET(tty, &selin); |
|
chars_avail=select(1, &selin, NULL, NULL, now); |
|
} |
} |
#endif |
#ifndef __ANDROID__ |
|
if (chars_avail > 0) { |
if(chars_avail == -1L) { |
/* getc won't block */ |
unsigned char inchar; |
int c = getc(stream); |
|
if (c==EOF) |
fcntl(tty, F_SETFL, O_NDELAY); |
return 0; |
result = read(tty, &inchar, sizeof(char)); |
gf_ungetc(c, stream); |
if(result == sizeof(char)) { |
|
chars_avail = 1; |
|
pending = (long)inchar; |
|
} else { |
|
chars_avail = 0; |
|
} |
|
fcntl(tty, F_SETFL, 0); |
|
} |
} |
|
#endif |
return chars_avail; |
return (chars_avail == -1) ? 0 : chars_avail; |
} |
} |
|
|
/* Get a key from the buffer of characters to be read. |
/* Get a key from the buffer of characters to be read. |
Line 671 Cell getkey(FILE * stream)
|
Line 705 Cell getkey(FILE * stream)
|
Cell result; |
Cell result; |
unsigned char c; |
unsigned char c; |
|
|
if(!terminal_prepped) |
if (!gf_ungottenc(stream)) |
|
setvbuf(stream, NULL, _IONBF, 0); |
|
if(!terminal_prepped && stream == stdin) |
prep_terminal(); |
prep_terminal(); |
|
|
while (pending < 0) |
result = fread(&c, sizeof(c), 1, stream); |
{ |
if (result>0) |
result = read (fileno (stream), &c, sizeof (char)); |
gf_regetc(stream); |
|
return result==0 ? (errno == EINTR ? 12 : 4) : c; |
if (result == sizeof (char)) |
} |
return /* (c == 0x7F ? 0x08 :*/ c /*)*/; |
|
|
|
/* If zero characters are returned, then the file that we are |
|
reading from is empty! Return EOF in that case. */ |
|
if (result == 0) |
|
return (EOF); |
|
|
|
/* If the error that we received was SIGINT, then try again, |
|
this is simply an interrupted system call to read (). |
|
Otherwise, some error ocurred, also signifying EOF. */ |
|
if (errno != EINTR) |
|
return (EOF); |
|
} |
|
|
|
/* otherwise there is a character pending; |
#ifdef STANDALONE |
return it and delete pending char */ |
void emit_char(char x) |
result = (Cell) pending; |
{ |
pending = -1L; |
putc(x, stdout); |
|
} |
|
|
return result; |
void type_chars(char *addr, unsigned int l) |
|
{ |
|
fwrite(addr, l, 1, stdout); |
} |
} |
|
#endif |
|
|
#ifdef TEST |
#ifdef TEST |
|
|