| 1 | /* $Id$ */\r |
| 2 | /***************************************************************\r |
| 3 | ** I/O subsystem for PForth based on 'C'\r |
| 4 | **\r |
| 5 | ** Author: Phil Burk\r |
| 6 | ** Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom\r |
| 7 | **\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 |
| 16 | **\r |
| 17 | ****************************************************************\r |
| 18 | ** 941004 PLB Extracted IO calls from pforth_main.c\r |
| 19 | ***************************************************************/\r |
| 20 | \r |
| 21 | #include "../pf_all.h"\r |
| 22 | \r |
| 23 | #if PF_POSIX_IO\r |
| 24 | /* Configure console so that characters are not buffered.\r |
| 25 | * This allows KEY to work and also HISTORY.ON\r |
| 26 | * Thanks to Ralf Baechle and David Feuer for contributing this.\r |
| 27 | */\r |
| 28 | \r |
| 29 | #include <unistd.h>\r |
| 30 | #ifdef sun\r |
| 31 | #include <sys/int_types.h> /* Needed on Solaris for uint32_t in termio.h */\r |
| 32 | #endif\r |
| 33 | #include <termios.h>\r |
| 34 | #include <sys/poll.h>\r |
| 35 | \r |
| 36 | #define stdin_fd 1\r |
| 37 | \r |
| 38 | static struct termios save_termios;\r |
| 39 | static int stdin_is_tty;\r |
| 40 | \r |
| 41 | /* Default portable terminal I/O. */\r |
| 42 | int sdTerminalOut( char c )\r |
| 43 | {\r |
| 44 | return putchar(c);\r |
| 45 | }\r |
| 46 | /* We don't need to echo because getchar() echos. */\r |
| 47 | int sdTerminalEcho( char c )\r |
| 48 | {\r |
| 49 | TOUCH(c);\r |
| 50 | return 0;\r |
| 51 | }\r |
| 52 | int sdTerminalIn( void )\r |
| 53 | {\r |
| 54 | return getchar();\r |
| 55 | }\r |
| 56 | \r |
| 57 | int sdTerminalFlush( void )\r |
| 58 | {\r |
| 59 | #ifdef PF_NO_FILEIO\r |
| 60 | return -1;\r |
| 61 | #else\r |
| 62 | return fflush(PF_STDOUT);\r |
| 63 | #endif\r |
| 64 | }\r |
| 65 | \r |
| 66 | /****************************************************/\r |
| 67 | int sdQueryTerminal( void )\r |
| 68 | {\r |
| 69 | struct pollfd pfd;\r |
| 70 | sdTerminalFlush();\r |
| 71 | pfd.fd = stdin_fd;\r |
| 72 | pfd.events = stdin_fd;\r |
| 73 | return poll( &pfd, 1, 0 ); \r |
| 74 | }\r |
| 75 | \r |
| 76 | /****************************************************/\r |
| 77 | void sdTerminalInit(void)\r |
| 78 | {\r |
| 79 | struct termios term;\r |
| 80 | \r |
| 81 | stdin_is_tty = isatty(stdin_fd);\r |
| 82 | if (!stdin_is_tty)\r |
| 83 | return;\r |
| 84 | \r |
| 85 | /* Get current terminal attributes and save them so we can restore them. */\r |
| 86 | tcgetattr(stdin_fd, &term);\r |
| 87 | save_termios = term;\r |
| 88 | \r |
| 89 | /* ICANON says to wait upon read until a character is received,\r |
| 90 | * and then to return it immediately (or soon enough....)\r |
| 91 | * ECHOCTL says not to echo backspaces and other control chars as ^H */\r |
| 92 | term.c_lflag &= ~( ECHO | ECHONL | ECHOCTL | ICANON );\r |
| 93 | term.c_cc[VTIME] = 0;\r |
| 94 | term.c_cc[VMIN] = 1;\r |
| 95 | tcsetattr(stdin_fd, TCSANOW, &term);\r |
| 96 | }\r |
| 97 | \r |
| 98 | /****************************************************/\r |
| 99 | void sdTerminalTerm(void)\r |
| 100 | {\r |
| 101 | if (!stdin_is_tty)\r |
| 102 | return;\r |
| 103 | \r |
| 104 | tcsetattr(stdin_fd, TCSANOW, &save_termios);\r |
| 105 | }\r |
| 106 | \r |
| 107 | #undef stdin_fd\r |
| 108 | \r |
| 109 | #endif\r |