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