b456788dcc3c702dd8d6880971baf5219766d778
/***************************************************************
** I/O subsystem for PForth based on 'C'
** Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom
** The pForth software code is dedicated to the public domain,
** and any third party may reproduce, distribute and modify
** the pForth software code or any derivative works thereof
** without any compensation or license. The pForth software
** code is provided on an "as is" basis without any warranty
** of any kind, including, without limitation, the implied
** warranties of merchantability and fitness for a particular
** purpose and their equivalents under the laws of any jurisdiction.
****************************************************************
** 941004 PLB Extracted IO calls from pforth_main.c
** 090220 PLB Fixed broken sdQueryTerminal on Mac. It always returned true.
***************************************************************/
/* Configure console so that characters are not buffered.
* This allows KEY and ?TERMINAL to work and also HISTORY.ON
#include <sys/int_types.h> /* Needed on Solaris for uint32_t in termio.h */
static struct termios save_termios
;
/* poll() is broken in Mac OS X Tiger OS so use select() instead. */
#define PF_USE_SELECT (1)
/* Default portable terminal I/O. */
int sdTerminalOut( char c
)
int sdTerminalEcho( char c
)
int sdTerminalFlush( void )
return fflush(PF_STDOUT
);
/****************************************************/
int sdQueryTerminal( void )
FD_SET(STDIN_FILENO
, &readfds
);
/* Set timeout to zero so that we just poll and return. */
select_retval
= select(STDIN_FILENO
+1, &readfds
, NULL
, NULL
, &tv
);
perror("sdTerminalInit: select");
return FD_ISSET(STDIN_FILENO
,&readfds
) ? FTRUE
: FFALSE
;
struct pollfd pfd
= { 0 };
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 (pfd
.revents
& POLLIN
) ? FTRUE
: FFALSE
;
/****************************************************/
void sdTerminalInit(void)
stdin_is_tty
= isatty(STDIN_FILENO
);
/* Get current terminal attributes and save them so we can restore them. */
tcgetattr(STDIN_FILENO
, &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
);
if( tcsetattr(STDIN_FILENO
, TCSANOW
, &term
) < 0 )
perror("sdTerminalInit: tcsetattr");
/****************************************************/
void sdTerminalTerm(void)
tcsetattr(STDIN_FILENO
, TCSANOW
, &save_termios
);