JBoss Native SVN: r958 - trunk/sight/native/os/linux.
by jbossnative-commits@lists.jboss.org
Author: mladen.turk(a)jboss.com
Date: 2007-08-30 05:22:15 -0400 (Thu, 30 Aug 2007)
New Revision: 958
Modified:
trunk/sight/native/os/linux/console.c
Log:
Adding Linux Console implementation
Modified: trunk/sight/native/os/linux/console.c
===================================================================
--- trunk/sight/native/os/linux/console.c 2007-08-29 11:33:18 UTC (rev 957)
+++ trunk/sight/native/os/linux/console.c 2007-08-30 09:22:15 UTC (rev 958)
@@ -34,17 +34,213 @@
#include "sight_types.h"
#include "sight_private.h"
+/* If unistd.h defines _POSIX_VERSION, we conclude that we
+ * are on a POSIX system and have sigaction and termios. */
+#if defined(_POSIX_VERSION)
+
+# define SIGACTION
+# if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
+# define TERMIOS
+# endif
+#endif
+
+/* There are 5 types of terminal interface supported,
+ * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
+ */
+
+#if defined(__sgi) && !defined(TERMIOS)
+# define TERMIOS
+# undef TERMIO
+# undef SGTTY
+#endif
+
+#if defined(linux) && !defined(TERMIO)
+# undef TERMIOS
+# define TERMIO
+# undef SGTTY
+#endif
+
+#ifdef _LIBC
+# undef TERMIOS
+# define TERMIO
+# undef SGTTY
+#endif
+
+#if !defined(TERMIO) && !defined(TERMIOS)
+# undef TERMIOS
+# undef TERMIO
+# define SGTTY
+#endif
+
+#ifdef TERMIOS
+# include <termios.h>
+# define TTY_STRUCT struct termios
+# define TTY_FLAGS c_lflag
+# define TTY_get(tty,data) tcgetattr(tty,data)
+# define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
+#endif
+
+#ifdef TERMIO
+# include <termio.h>
+# define TTY_STRUCT struct termio
+# define TTY_FLAGS c_lflag
+# define TTY_get(tty,data) ioctl(tty,TCGETA,data)
+# define TTY_set(tty,data) ioctl(tty,TCSETA,data)
+#endif
+
+#ifdef SGTTY
+# include <sgtty.h>
+# define TTY_STRUCT struct sgttyb
+# define TTY_FLAGS sg_flags
+# define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
+# define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
+#endif
+
+#if !defined(_LIBC)
+# include <sys/ioctl.h>
+#endif
+
+#ifndef NX509_SIG
+# define NX509_SIG 32
+#endif
+
+
+/* Define globals. They are protected by a lock */
+#ifdef SIGACTION
+static struct sigaction savsig[NX509_SIG];
+#else
+static void (*savsig[NX509_SIG])(int );
+#endif
+
+#define DEV_TTY "/dev/tty"
+
+static TTY_STRUCT tty_orig;
+static TTY_STRUCT tty_new;
+
+static FILE *tty_in;
+static FILE *tty_out;
+static int is_a_tty;
+
+static volatile sig_atomic_t intr_signal;
+
+static void recsig(int i)
+{
+ intr_signal = i;
+}
+
+
+/* Internal functions to handle signals and act on them */
+static void pushsig(void)
+{
+ int i;
+#ifdef SIGACTION
+ struct sigaction sa;
+
+ memset(&sa, 0, sizeof sa);
+ sa.sa_handler = recsig;
+#endif
+
+ for (i = 1; i < NX509_SIG; i++) {
+#ifdef SIGUSR1
+ if (i == SIGUSR1)
+ continue;
+#endif
+#ifdef SIGUSR2
+ if (i == SIGUSR2)
+ continue;
+#endif
+#ifdef SIGKILL
+ if (i == SIGKILL) /* We can't make any action on that. */
+ continue;
+#endif
+#ifdef SIGACTION
+ sigaction(i, &sa, &savsig[i]);
+#else
+ savsig[i] = signal(i, recsig);
+#endif
+ }
+
+#ifdef SIGWINCH
+ signal(SIGWINCH, SIG_DFL);
+#endif
+}
+
+static void popsig(void)
+{
+ int i;
+
+ for (i = 1; i < NX509_SIG; i++) {
+#ifdef SIGUSR1
+ if (i == SIGUSR1)
+ continue;
+#endif
+#ifdef SIGUSR2
+ if (i == SIGUSR2)
+ continue;
+#endif
+#ifdef SIGACTION
+ sigaction(i, &savsig[i], NULL);
+#else
+ signal(i, savsig[i]);
+#endif
+ }
+}
+
+typedef struct {
+ FILE *coni;
+ FILE *cono;
+ TTY_STRUCT ttyo;
+ TTY_STRUCT ttyn;
+ int is_a_tty;
+} sight_console_t;
+
SIGHT_EXPORT_DECLARE(jlong, Console, alloc0)(SIGHT_STDARGS)
{
- UNREFERENCED_STDARGS;
- return 0;
+ sight_console_t *con;
+ UNREFERENCED_O;
+
+ if (!(con = (sight_console_t *)calloc(1, sizeof(sight_console_t)))) {
+ throwAprMemoryException(_E, THROW_FMARK,
+ apr_get_os_error());
+ return 0;
+ }
+ return P2J(con);
}
SIGHT_EXPORT_DECLARE(void, Console, open0)(SIGHT_STDARGS,
jlong instance)
{
+ sight_console_t *con = J2P(instance, sight_console_t *);
UNREFERENCED_STDARGS;
- UNREFERENCED(instance);
+
+ if (!con)
+ return;
+ con->is_a_tty = 1;
+ if ((con->coni = fopen(DEV_TTY,"r")) == NULL)
+ con->coni = stdin;
+ if ((con->cono = fopen(DEV_TTY,"r")) == NULL)
+ con->cono = stdout;
+
+ if (TTY_get(fileno(con->coni), &con->ttyo) == -1) {
+#ifdef ENOTTY
+ if (errno == ENOTTY)
+ con->is_a_tty = 0;
+ else
+#endif
+#ifdef EINVAL
+ /* Ariel Glenn ariel(a)columbia.edu reports that solaris
+ * can return EINVAL instead. This should be ok
+ */
+ if (errno == EINVAL)
+ con->is_a_tty = 0;
+ else
+#endif
+ {
+ /* TODO: Trow an exception ? */
+ return;
+ }
+ }
+
}
SIGHT_EXPORT_DECLARE(jint, Console, attach0)(SIGHT_STDARGS,
@@ -58,16 +254,40 @@
SIGHT_EXPORT_DECLARE(void, Console, close0)(SIGHT_STDARGS,
jlong instance)
{
+ sight_console_t *con = J2P(instance, sight_console_t *);
+
UNREFERENCED_STDARGS;
+
+ if (!con)
+ return;
+ if (con->coni != stdin)
+ fclose(con->coni);
+ if (con->cono != stdout)
+ fclose(con->cono);
+ free(con);
}
SIGHT_EXPORT_DECLARE(void, Console, echo0)(SIGHT_STDARGS,
jlong instance,
jboolean on)
{
+ sight_console_t *con = J2P(instance, sight_console_t *);
UNREFERENCED_STDARGS;
- UNREFERENCED(instance);
- UNREFERENCED(on);
+
+#ifdef TTY_FLAGS
+ memcpy(&(con->ttyn), &(con->ttyo), sizeof(con->ttyo));
+ if (on)
+ con->ttyn.TTY_FLAGS |= ECHO;
+ else
+ con->ttyn.TTY_FLAGS &= ~ECHO;
+#endif
+
+#if defined(TTY_set)
+ if (con->is_a_tty && (TTY_set(fileno(con->coni), &(con->ttyn)) == -1)) {
+ return;
+ }
+#endif
+
}
SIGHT_EXPORT_DECLARE(void, Console, stitle0)(SIGHT_STDARGS,
@@ -93,16 +313,16 @@
SIGHT_EXPORT_DECLARE(jstring, Console, gets0)(SIGHT_STDARGS,
jlong instance)
{
+ sight_console_t *con = J2P(instance, sight_console_t *);
UNREFERENCED_STDARGS;
- UNREFERENCED(instance);
return NULL;
}
SIGHT_EXPORT_DECLARE(jint, Console, getc0)(SIGHT_STDARGS,
jlong instance)
{
+ sight_console_t *con = J2P(instance, sight_console_t *);
UNREFERENCED_STDARGS;
- UNREFERENCED(instance);
return -1;
}
@@ -110,25 +330,34 @@
jlong instance,
jint ch)
{
+ sight_console_t *con = J2P(instance, sight_console_t *);
UNREFERENCED_STDARGS;
- UNREFERENCED(instance);
- UNREFERENCED(ch);
+ if (con->cono) {
+ fputc(ch, con->cono);
+ }
}
SIGHT_EXPORT_DECLARE(void, Console, flush0)(SIGHT_STDARGS,
jlong instance)
{
+ sight_console_t *con = J2P(instance, sight_console_t *);
+ if (con->cono)
+ fflush(con->cono);
UNREFERENCED_STDARGS;
- UNREFERENCED(instance);
}
SIGHT_EXPORT_DECLARE(void, Console, puts0)(SIGHT_STDARGS,
jlong instance,
jstring str)
{
- UNREFERENCED_STDARGS;
- UNREFERENCED(instance);
- UNREFERENCED(str);
+ sight_console_t *con = J2P(instance, sight_console_t *);
+ SIGHT_ALLOC_CSTRING(str);
+
+ UNREFERENCED_O;
+ if (con->cono) {
+ fputs(J2S(str), con->cono);
+ }
+ SIGHT_FREE_CSTRING(str);
}