2 /***************************************************************
3 ** I/O subsystem for PForth based on 'C'
6 ** Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom
8 ** The pForth software code is dedicated to the public domain,
9 ** and any third party may reproduce, distribute and modify
10 ** the pForth software code or any derivative works thereof
11 ** without any compensation or license. The pForth software
12 ** code is provided on an "as is" basis without any warranty
13 ** of any kind, including, without limitation, the implied
14 ** warranties of merchantability and fitness for a particular
15 ** purpose and their equivalents under the laws of any jurisdiction.
17 ****************************************************************
18 ** 941004 PLB Extracted IO calls from pforth_main.c
19 ** 090220 PLB Fixed broken sdQueryTerminal on Mac. It always returned true.
20 ***************************************************************/
22 #include "../pf_all.h"
24 /* Configure console so that characters are not buffered.
25 * This allows KEY and ?TERMINAL to work and also HISTORY.ON
31 #include <sys/int_types.h> /* Needed on Solaris for uint32_t in termio.h */
36 static struct termios save_termios;
37 static int stdin_is_tty;
39 /* poll() is broken in Mac OS X Tiger OS so use select() instead. */
41 #define PF_USE_SELECT (1)
44 /* Default portable terminal I/O. */
45 int sdTerminalOut( char c )
50 int sdTerminalEcho( char c )
56 int sdTerminalIn( void )
61 int sdTerminalFlush( void )
66 return fflush(PF_STDOUT);
70 /****************************************************/
71 int sdQueryTerminal( void )
78 FD_SET(STDIN_FILENO, &readfds);
79 /* Set timeout to zero so that we just poll and return. */
82 select_retval = select(STDIN_FILENO+1, &readfds, NULL, NULL, &tv);
83 if (select_retval < 0)
85 perror("sdTerminalInit: select");
87 return FD_ISSET(STDIN_FILENO,&readfds) ? FTRUE : FFALSE;
91 struct pollfd pfd = { 0 };
93 pfd.fd = STDIN_FILENO;
95 result = poll( &pfd, 1, 0 );
96 /* On a Mac it may set revents to POLLNVAL because poll() is broken on Tiger. */
97 if( pfd.revents & POLLNVAL )
99 PRT(("sdQueryTerminal: poll got POLLNVAL, stdin not open\n"));
104 return (pfd.revents & POLLIN) ? FTRUE : FFALSE;
109 /****************************************************/
110 void sdTerminalInit(void)
114 stdin_is_tty = isatty(STDIN_FILENO);
117 /* Get current terminal attributes and save them so we can restore them. */
118 tcgetattr(STDIN_FILENO, &term);
121 /* ICANON says to wait upon read until a character is received,
122 * and then to return it immediately (or soon enough....)
123 * ECHOCTL says not to echo backspaces and other control chars as ^H */
124 term.c_lflag &= ~( ECHO | ECHONL | ECHOCTL | ICANON );
125 term.c_cc[VTIME] = 0;
127 if( tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0 )
129 perror("sdTerminalInit: tcsetattr");
134 /****************************************************/
135 void sdTerminalTerm(void)
139 tcsetattr(STDIN_FILENO, TCSANOW, &save_termios);