2 /***************************************************************
3 ** I/O subsystem for PForth based on 'C'
6 ** Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom
8 ** Permission to use, copy, modify, and/or distribute this
9 ** software for any purpose with or without fee is hereby granted.
11 ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
12 ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
13 ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
14 ** THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
15 ** CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
16 ** FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
17 ** CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 ** OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 ****************************************************************
21 ** 941004 PLB Extracted IO calls from pforth_main.c
22 ** 090220 PLB Fixed broken sdQueryTerminal on Mac. It always returned true.
23 ***************************************************************/
25 #include "../pf_all.h"
27 /* Configure console so that characters are not buffered.
28 * This allows KEY and ?TERMINAL to work and also HISTORY.ON
34 #include <sys/int_types.h> /* Needed on Solaris for uint32_t in termio.h */
39 static struct termios save_termios;
40 static int stdin_is_tty;
42 /* poll() is broken in Mac OS X Tiger OS so use select() instead. */
44 #define PF_USE_SELECT (1)
47 /* Default portable terminal I/O. */
48 int sdTerminalOut( char c )
53 int sdTerminalEcho( char c )
59 int sdTerminalIn( void )
64 int sdTerminalFlush( void )
69 return fflush(PF_STDOUT);
73 /****************************************************/
74 int sdQueryTerminal( void )
81 FD_SET(STDIN_FILENO, &readfds);
82 /* Set timeout to zero so that we just poll and return. */
85 select_retval = select(STDIN_FILENO+1, &readfds, NULL, NULL, &tv);
86 if (select_retval < 0)
88 perror("sdTerminalInit: select");
90 return FD_ISSET(STDIN_FILENO,&readfds) ? FTRUE : FFALSE;
94 struct pollfd pfd = { 0 };
96 pfd.fd = STDIN_FILENO;
98 result = poll( &pfd, 1, 0 );
99 /* On a Mac it may set revents to POLLNVAL because poll() is broken on Tiger. */
100 if( pfd.revents & POLLNVAL )
102 PRT(("sdQueryTerminal: poll got POLLNVAL, stdin not open\n"));
107 return (pfd.revents & POLLIN) ? FTRUE : FFALSE;
112 /****************************************************/
113 void sdTerminalInit(void)
117 stdin_is_tty = isatty(STDIN_FILENO);
120 /* Get current terminal attributes and save them so we can restore them. */
121 tcgetattr(STDIN_FILENO, &term);
124 /* ICANON says to wait upon read until a character is received,
125 * and then to return it immediately (or soon enough....)
126 * ECHOCTL says not to echo backspaces and other control chars as ^H */
127 term.c_lflag &= ~( ECHO | ECHONL | ECHOCTL | ICANON );
128 term.c_cc[VTIME] = 0;
130 if( tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0 )
132 perror("sdTerminalInit: tcsetattr");
137 /****************************************************/
138 void sdTerminalTerm(void)
142 tcsetattr(STDIN_FILENO, TCSANOW, &save_termios);