Diff for /gforth/engine/io.c between versions 1.10 and 1.35

version 1.10, 1999/02/28 08:37:45 version 1.35, 2009/06/13 10:22:24
Line 1 Line 1
 /* direct key io driver  /* direct key io driver
   
   Copyright (C) 1995,1996,1997,1998 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.    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., 675 Mass Ave, Cambridge, MA 02139, 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 51  typedef unsigned int uint32_t; Line 51  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
Line 447  void deprep_terminal () Line 446  void deprep_terminal ()
 #define VTIME VEOL  #define VTIME VEOL
 #endif  #endif
   
   #include <locale.h>
   
 void prep_terminal ()  void prep_terminal ()
 {  {
   int tty = fileno (stdin);    int tty = fileno (stdin);
Line 472  void prep_terminal () Line 473  void prep_terminal ()
     return;      /* added by MdG */      return;      /* added by MdG */
   }      /* 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    /* Try to keep this function from being INTerrupted.  We can do it
      on POSIX and systems with BSD-like signal handling. */       on POSIX and systems with BSD-like signal handling. */
 #if defined (HAVE_POSIX_SIGNALS)  #if defined (HAVE_POSIX_SIGNALS)
Line 613  void deprep_terminal () Line 617  void deprep_terminal ()
 }  }
 #endif  /* NEW_TTY_DRIVER */  #endif  /* NEW_TTY_DRIVER */
   
 /* If a character is available to be read, then read it  
    and stuff it into IBUFFER.  Otherwise, just return. */  
   
 long pending = -1L;  
   
 long key_avail (FILE * stream)  long key_avail (FILE * stream)
 {  {
   int tty = fileno (stream);    int tty = fileno (stream);
   int chars_avail = pending;    fd_set selin;
   int result;    static struct timeval now = { 0 , 0 };
     int chars_avail;
   if(!terminal_prepped)  prep_terminal();  
     setvbuf(stream, NULL, _IONBF, 0);
 #ifndef _WIN32    if(!terminal_prepped && stream == stdin)
   if(isatty (tty)) {      prep_terminal();
     result = ioctl (tty, FIONREAD, &chars_avail);  
   }    FD_ZERO(&selin);
 #else    FD_SET(tty, &selin);
   {    chars_avail = select(1, &selin, NULL, NULL, &now);
     fd_set selin;    return (chars_avail == -1) ? 0 : chars_avail;
     static int now[2] = { 0 , 0 };  
     int res;  
       
     FD_ZERO(&selin);  
     FD_SET(tty, &selin);  
     chars_avail=select(1, &selin, NULL, NULL, now);  
   }  
 #endif  
     
   if(chars_avail == -1L) {  
     unsigned char inchar;  
       
     fcntl(tty, F_SETFL, O_NDELAY);  
     result = read(tty, &inchar, sizeof(char));  
     if(result == sizeof(char)) {  
       chars_avail = 1;  
       pending = (long)inchar;  
     } else {  
       chars_avail = 0;  
     }  
     fcntl(tty, F_SETFL, 0);  
   }  
   
   return 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 666  long key_avail (FILE * stream) Line 641  long key_avail (FILE * stream)
 /* When compiling and running in the `Posix' environment, Ultrix does  /* When compiling and running in the `Posix' environment, Ultrix does
    not restart system calls, so this needs to do it. */     not restart system calls, so this needs to do it. */
   
 unsigned getkey(FILE * stream)  Cell getkey(FILE * stream)
 {  {
   int result;    Cell result;
   unsigned char c;    unsigned char c;
   
   if(!terminal_prepped)  prep_terminal();    setvbuf(stream, NULL, _IONBF, 0);
     if(!terminal_prepped && stream == stdin)
   while (pending < 0)      prep_terminal();
     {  
       result = read (fileno (stream), &c, sizeof (char));  
   
       if (result == sizeof (char))  
         return /* (c == 0x7F ? 0x08 :*/ c /*)*/;  
   
       /* If zero characters are returned, then the file that we are    result = fread(&c, sizeof(c), 1, stream);
          reading from is empty!  Return EOF in that case. */    return result==0 ? (errno == EINTR ? 12 : 4) : c;
       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);  
     }  
   
   result = (int) pending;  #ifdef STANDALONE
   pending = -1L;  void emit_char(char x)
   {
     putc(x, stdout);
   }
   
   return result;  void type_chars(char *addr, unsigned int l)
   {
     fwrite(addr, l, 1, stdout);
 }  }
   #endif
   
 #ifdef TEST  #ifdef TEST
   

Removed from v.1.10  
changed lines
  Added in v.1.35


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>