1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
/* $Id$ */
/***************************************************************
** I/O subsystem for PForth based on 'C'
**
** Author: Phil Burk
** Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom
**
** Permission to use, copy, modify, and/or distribute this
** software for any purpose with or without fee is hereby granted.
**
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
** THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
** CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
** FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
** CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
** OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
**
****************************************************************
** 941004 PLB Extracted IO calls from pforth_main.c
** 090220 PLB Fixed broken sdQueryTerminal on Mac. It always returned true.
***************************************************************/
#include "../pf_all.h"
/* Configure console so that characters are not buffered.
* This allows KEY and ?TERMINAL to work and also HISTORY.ON
*/
#include <unistd.h>
#include <sys/time.h>
#ifdef sun
#include <sys/int_types.h> /* Needed on Solaris for uint32_t in termio.h */
#endif
#include <termios.h>
#include <sys/poll.h>
static struct termios save_termios;
static int stdin_is_tty;
/* poll() is broken in Mac OS X Tiger OS so use select() instead. */
#ifndef PF_USE_SELECT
#define PF_USE_SELECT (1)
#endif
/* Default portable terminal I/O. */
int sdTerminalOut( char c )
{
return putchar(c);
}
int sdTerminalEcho( char c )
{
putchar(c);
return 0;
}
int sdTerminalIn( void )
{
return getchar();
}
int sdTerminalFlush( void )
{
#ifdef PF_NO_FILEIO
return -1;
#else
return fflush(PF_STDOUT);
#endif
}
/****************************************************/
int sdQueryTerminal( void )
{
#if PF_USE_SELECT
int select_retval;
fd_set readfds;
struct timeval tv;
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
/* Set timeout to zero so that we just poll and return. */
tv.tv_sec = 0;
tv.tv_usec = 0;
select_retval = select(STDIN_FILENO+1, &readfds, NULL, NULL, &tv);
if (select_retval < 0)
{
perror("sdTerminalInit: select");
}
return FD_ISSET(STDIN_FILENO,&readfds) ? FTRUE : FFALSE;
#else
int result;
struct pollfd pfd = { 0 };
sdTerminalFlush();
pfd.fd = STDIN_FILENO;
pfd.events = POLLIN;
result = poll( &pfd, 1, 0 );
/* On a Mac it may set revents to POLLNVAL because poll() is broken on Tiger. */
if( pfd.revents & POLLNVAL )
{
PRT(("sdQueryTerminal: poll got POLLNVAL, stdin not open\n"));
return FFALSE;
}
else
{
return (pfd.revents & POLLIN) ? FTRUE : FFALSE;
}
#endif
}
/****************************************************/
void sdTerminalInit(void)
{
struct termios term;
stdin_is_tty = isatty(STDIN_FILENO);
if (stdin_is_tty)
{
/* Get current terminal attributes and save them so we can restore them. */
tcgetattr(STDIN_FILENO, &term);
save_termios = term;
/* ICANON says to wait upon read until a character is received,
* and then to return it immediately (or soon enough....)
* ECHOCTL says not to echo backspaces and other control chars as ^H */
term.c_lflag &= ~( ECHO | ECHONL | ECHOCTL | ICANON );
term.c_cc[VTIME] = 0;
term.c_cc[VMIN] = 1;
if( tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0 )
{
perror("sdTerminalInit: tcsetattr");
}
if (setvbuf(stdout, NULL, _IONBF, (size_t) 0) != 0)
{
perror("sdTerminalInit: setvbuf");
}
}
}
/****************************************************/
void sdTerminalTerm(void)
{
if (stdin_is_tty)
{
tcsetattr(STDIN_FILENO, TCSANOW, &save_termios);
}
}
cell_t sdSleepMillis(cell_t msec)
{
const cell_t kMaxMicros = 500000; /* to be safe, usleep() limit is 1000000 */
cell_t micros;
cell_t napTime;
if (msec < 0) return 0;
micros = msec * 1000;
while (micros > 0)
{
napTime = (micros > kMaxMicros) ? kMaxMicros : micros;
if (usleep(napTime))
{
perror("sdSleepMillis: usleep failed");
return -1;
}
micros -= napTime;
}
return 0;
}
|