relicense to 0BSD
[pforth] / csrc / posix / pf_io_posix.c
index b456788..bd0c50e 100644 (file)
-/* $Id$ */\r
-/***************************************************************\r
-** I/O subsystem for PForth based on 'C'\r
-**\r
-** Author: Phil Burk\r
-** Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom\r
-**\r
-** The pForth software code is dedicated to the public domain,\r
-** and any third party may reproduce, distribute and modify\r
-** the pForth software code or any derivative works thereof\r
-** without any compensation or license.  The pForth software\r
-** code is provided on an "as is" basis without any warranty\r
-** of any kind, including, without limitation, the implied\r
-** warranties of merchantability and fitness for a particular\r
-** purpose and their equivalents under the laws of any jurisdiction.\r
-**\r
-****************************************************************\r
-** 941004 PLB Extracted IO calls from pforth_main.c\r
-** 090220 PLB Fixed broken sdQueryTerminal on Mac. It always returned true.\r
-***************************************************************/\r
-\r
-#include "../pf_all.h"\r
-\r
-/* Configure console so that characters are not buffered.\r
- * This allows KEY and ?TERMINAL to work and also HISTORY.ON\r
- */\r
-\r
-#include <unistd.h>\r
-#include <sys/time.h>\r
-#ifdef sun\r
-#include <sys/int_types.h> /* Needed on Solaris for uint32_t in termio.h */\r
-#endif\r
-#include <termios.h>\r
-#include <sys/poll.h>\r
-\r
-static struct termios save_termios;\r
-static int stdin_is_tty;\r
-\r
-/* poll() is broken in Mac OS X Tiger OS so use select() instead. */\r
-#ifndef PF_USE_SELECT\r
-#define PF_USE_SELECT  (1)\r
-#endif\r
-\r
-/* Default portable terminal I/O. */\r
-int  sdTerminalOut( char c )\r
-{\r
-       return putchar(c);\r
-}\r
-\r
-int  sdTerminalEcho( char c )\r
-{\r
-       putchar(c);\r
-       return 0;\r
-}\r
-\r
-int  sdTerminalIn( void )\r
-{\r
-       return getchar();\r
-}\r
-\r
-int  sdTerminalFlush( void )\r
-{\r
-#ifdef PF_NO_FILEIO\r
-       return -1;\r
-#else\r
-       return fflush(PF_STDOUT);\r
-#endif\r
-}\r
-\r
-/****************************************************/\r
-int sdQueryTerminal( void )\r
-{\r
-#if PF_USE_SELECT\r
-       int select_retval;\r
-       fd_set readfds;\r
-       struct timeval tv;\r
-       FD_ZERO(&readfds);\r
-       FD_SET(STDIN_FILENO, &readfds);\r
-       /* Set timeout to zero so that we just poll and return. */\r
-       tv.tv_sec = 0;\r
-       tv.tv_usec = 0;\r
-       select_retval = select(STDIN_FILENO+1, &readfds, NULL, NULL, &tv);\r
-       if (select_retval < 0)\r
-       {\r
-               perror("sdTerminalInit: select");\r
-       }\r
-       return FD_ISSET(STDIN_FILENO,&readfds) ? FTRUE : FFALSE;\r
-\r
-#else\r
-       int result;\r
-       struct pollfd  pfd = { 0 };\r
-       sdTerminalFlush();\r
-       pfd.fd = STDIN_FILENO;\r
-       pfd.events = POLLIN;\r
-       result = poll( &pfd, 1, 0 );\r
-    /* On a Mac it may set revents to POLLNVAL because poll() is broken on Tiger. */\r
-       if( pfd.revents & POLLNVAL )\r
-       {\r
-               PRT(("sdQueryTerminal: poll got POLLNVAL, stdin not open\n"));\r
-               return FFALSE;\r
-       }\r
-       else\r
-       {\r
-               return (pfd.revents & POLLIN) ? FTRUE : FFALSE;\r
-       }\r
-#endif\r
-}\r
-\r
-/****************************************************/\r
-void sdTerminalInit(void)\r
-{\r
-       struct termios term;\r
-\r
-       stdin_is_tty = isatty(STDIN_FILENO);\r
-       if (stdin_is_tty)\r
-       {               \r
-/* Get current terminal attributes and save them so we can restore them. */\r
-               tcgetattr(STDIN_FILENO, &term);\r
-               save_termios = term;\r
-       \r
-/* ICANON says to wait upon read until a character is received,\r
- * and then to return it immediately (or soon enough....)\r
- * ECHOCTL says not to echo backspaces and other control chars as ^H */\r
-               term.c_lflag &= ~( ECHO | ECHONL | ECHOCTL | ICANON );\r
-               term.c_cc[VTIME] = 0;\r
-               term.c_cc[VMIN] = 1;\r
-               if( tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0 )\r
-               {\r
-                       perror("sdTerminalInit: tcsetattr");\r
-               }\r
-       }\r
-}\r
-\r
-/****************************************************/\r
-void sdTerminalTerm(void)\r
-{\r
-       if (stdin_is_tty)\r
-       {\r
-               tcsetattr(STDIN_FILENO, TCSANOW, &save_termios);\r
-       }\r
-}\r
+/* $Id$ */
+/***************************************************************
+** I/O subsystem for PForth based on 'C'
+**
+** Author: Phil Burk
+** Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom
+**
+** Permission to use, copy, modify, and/or distribute this
+** software for any purpose with or without fee is hereby granted.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
+** THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
+** CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
+** FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+** CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+** OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+**
+****************************************************************
+** 941004 PLB Extracted IO calls from pforth_main.c
+** 090220 PLB Fixed broken sdQueryTerminal on Mac. It always returned true.
+***************************************************************/
+
+#include "../pf_all.h"
+
+/* Configure console so that characters are not buffered.
+ * This allows KEY and ?TERMINAL to work and also HISTORY.ON
+ */
+
+#include <unistd.h>
+#include <sys/time.h>
+#ifdef sun
+#include <sys/int_types.h> /* Needed on Solaris for uint32_t in termio.h */
+#endif
+#include <termios.h>
+#include <sys/poll.h>
+
+static struct termios save_termios;
+static int stdin_is_tty;
+
+/* poll() is broken in Mac OS X Tiger OS so use select() instead. */
+#ifndef PF_USE_SELECT
+#define PF_USE_SELECT  (1)
+#endif
+
+/* Default portable terminal I/O. */
+int  sdTerminalOut( char c )
+{
+    return putchar(c);
+}
+
+int  sdTerminalEcho( char c )
+{
+    putchar(c);
+    return 0;
+}
+
+int  sdTerminalIn( void )
+{
+    return getchar();
+}
+
+int  sdTerminalFlush( void )
+{
+#ifdef PF_NO_FILEIO
+    return -1;
+#else
+    return fflush(PF_STDOUT);
+#endif
+}
+
+/****************************************************/
+int sdQueryTerminal( void )
+{
+#if PF_USE_SELECT
+    int select_retval;
+    fd_set readfds;
+    struct timeval tv;
+    FD_ZERO(&readfds);
+    FD_SET(STDIN_FILENO, &readfds);
+    /* Set timeout to zero so that we just poll and return. */
+    tv.tv_sec = 0;
+    tv.tv_usec = 0;
+    select_retval = select(STDIN_FILENO+1, &readfds, NULL, NULL, &tv);
+    if (select_retval < 0)
+    {
+        perror("sdTerminalInit: select");
+    }
+    return FD_ISSET(STDIN_FILENO,&readfds) ? FTRUE : FFALSE;
+
+#else
+    int result;
+    struct pollfd  pfd = { 0 };
+    sdTerminalFlush();
+    pfd.fd = STDIN_FILENO;
+    pfd.events = POLLIN;
+    result = poll( &pfd, 1, 0 );
+    /* On a Mac it may set revents to POLLNVAL because poll() is broken on Tiger. */
+    if( pfd.revents & POLLNVAL )
+    {
+        PRT(("sdQueryTerminal: poll got POLLNVAL, stdin not open\n"));
+        return FFALSE;
+    }
+    else
+    {
+        return (pfd.revents & POLLIN) ? FTRUE : FFALSE;
+    }
+#endif
+}
+
+/****************************************************/
+void sdTerminalInit(void)
+{
+    struct termios term;
+
+    stdin_is_tty = isatty(STDIN_FILENO);
+    if (stdin_is_tty)
+    {
+/* Get current terminal attributes and save them so we can restore them. */
+        tcgetattr(STDIN_FILENO, &term);
+        save_termios = term;
+
+/* ICANON says to wait upon read until a character is received,
+ * and then to return it immediately (or soon enough....)
+ * ECHOCTL says not to echo backspaces and other control chars as ^H */
+        term.c_lflag &= ~( ECHO | ECHONL | ECHOCTL | ICANON );
+        term.c_cc[VTIME] = 0;
+        term.c_cc[VMIN] = 1;
+        if( tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0 )
+        {
+            perror("sdTerminalInit: tcsetattr");
+        }
+    }
+}
+
+/****************************************************/
+void sdTerminalTerm(void)
+{
+    if (stdin_is_tty)
+    {
+        tcsetattr(STDIN_FILENO, TCSANOW, &save_termios);
+    }
+}