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
30 #include <sys/int_types.h> /* Needed on Solaris for uint32_t in termio.h */
\r
32 #include <termios.h>
\r
33 #include <sys/poll.h>
\r
35 static struct termios save_termios;
\r
36 static int stdin_is_tty;
\r
38 /* poll() is broken in Mac OS X Tiger OS so use select() instead. */
\r
39 #define PF_USE_SELECT (1)
\r
41 /* Default portable terminal I/O. */
\r
42 int sdTerminalOut( char c )
\r
47 int sdTerminalEcho( char c )
\r
53 int sdTerminalIn( void )
\r
58 int sdTerminalFlush( void )
\r
63 return fflush(PF_STDOUT);
\r
67 /****************************************************/
\r
68 int sdQueryTerminal( void )
\r
74 FD_SET(STDIN_FILENO, &readfds);
\r
75 /* Set timeout to zero so that we just poll and return. */
\r
78 int select_retval = select(STDIN_FILENO+1, &readfds, NULL, NULL, &tv);
\r
79 if (select_retval < 0)
\r
81 perror("sdTerminalInit: select");
\r
83 return FD_ISSET(STDIN_FILENO,&readfds) ? FTRUE : FFALSE;
\r
86 struct pollfd pfd = { 0 };
\r
88 pfd.fd = STDIN_FILENO;
\r
89 pfd.events = POLLIN;
\r
90 int result = poll( &pfd, 1, 0 );
\r
91 /* On a Mac it may set revents to POLLNVAL because poll() is broken on Tiger. */
\r
92 if( pfd.revents & POLLNVAL )
\r
94 PRT(("sdQueryTerminal: poll got POLLNVAL, stdin not open\n"));
\r
99 return (pfd.revents & POLLIN) ? FTRUE : FFALSE;
\r
104 /****************************************************/
\r
105 void sdTerminalInit(void)
\r
107 struct termios term;
\r
109 stdin_is_tty = isatty(STDIN_FILENO);
\r
112 /* Get current terminal attributes and save them so we can restore them. */
\r
113 tcgetattr(STDIN_FILENO, &term);
\r
114 save_termios = term;
\r
116 /* ICANON says to wait upon read until a character is received,
\r
117 * and then to return it immediately (or soon enough....)
\r
118 * ECHOCTL says not to echo backspaces and other control chars as ^H */
\r
119 term.c_lflag &= ~( ECHO | ECHONL | ECHOCTL | ICANON );
\r
120 term.c_cc[VTIME] = 0;
\r
121 term.c_cc[VMIN] = 1;
\r
122 if( tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0 )
\r
124 perror("sdTerminalInit: tcsetattr");
\r
129 /****************************************************/
\r
130 void sdTerminalTerm(void)
\r
134 tcsetattr(STDIN_FILENO, TCSANOW, &save_termios);
\r