Browse Source

Autologout with timeout. Patch by Camilo Arboleda.

git-svn-id: https://klish.googlecode.com/svn/trunk@488 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
2677326939
9 changed files with 62 additions and 2 deletions
  1. 1 0
      clish.xsd
  2. 1 0
      clish/shell.h
  3. 7 0
      clish/shell/shell_tinyrl.c
  4. 7 0
      clish/shell/shell_tinyxml.cpp
  5. 6 0
      tinyrl/tinyrl.c
  6. 2 0
      tinyrl/tinyrl.h
  7. 1 0
      tinyrl/vt100.h
  8. 1 0
      tinyrl/vt100/private.h
  9. 36 2
      tinyrl/vt100/vt100.c

+ 1 - 0
clish.xsd

@@ -183,6 +183,7 @@
         <xs:attribute name="view" type="xs:string" use="required"/>
         <xs:attribute name="viewid" type="xs:string" use="optional"/>
         <xs:attribute name="default_shebang" type="xs:string" use="optional"/>
+        <xs:attribute name="timeout" type="xs:string" use="optional"/>
     </xs:complexType>
     <!--
 *******************************************************

+ 1 - 0
clish/shell.h

@@ -338,6 +338,7 @@ void clish_shell__set_interactive(clish_shell_t * instance, bool_t interactive);
 bool_t clish_shell__get_interactive(const clish_shell_t * instance);
 bool_t clish_shell__get_utf8(const clish_shell_t * instance);
 void clish_shell__set_utf8(clish_shell_t * instance, bool_t utf8);
+void clish_shell__set_timeout(clish_shell_t *instance, int timeout);
 char *clish_shell__get_line(clish_context_t *context);
 char *clish_shell__get_params(clish_context_t *context);
 

+ 7 - 0
clish/shell/shell_tinyrl.c

@@ -516,6 +516,13 @@ void clish_shell__set_utf8(clish_shell_t * this, bool_t utf8)
 	tinyrl__set_utf8(this->tinyrl, utf8);
 }
 
+/*-------------------------------------------------------- */
+void clish_shell__set_timeout(clish_shell_t *this, int timeout)
+{
+	assert(this);
+	tinyrl__set_timeout(this->tinyrl, timeout);
+}
+
 /*--------------------------------------------------------- */
 tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * this)
 {

+ 7 - 0
clish/shell/shell_tinyxml.cpp

@@ -10,9 +10,11 @@ extern "C" {
 #include "lub/ctype.h"
 }
 /*lint +libh(tinyxml/tinyxml.h) Add this to the library file list */
+#include <stdlib.h>
 #include "tinyxml/tinyxml.h"
 #include <string.h>
 #include <assert.h>
+
 typedef void (PROCESS_FN) (clish_shell_t * instance,
 	TiXmlElement * element, void *parent);
 
@@ -318,6 +320,7 @@ process_startup(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	const char *view = element->Attribute("view");
 	const char *viewid = element->Attribute("viewid");
 	const char *default_shebang = element->Attribute("default_shebang");
+	const char *timeout = element->Attribute("timeout");
 
 	assert(!shell->startup);
 	assert(view);
@@ -338,6 +341,9 @@ process_startup(clish_shell_t * shell, TiXmlElement * element, void *parent)
 	if (default_shebang)
 		clish_shell__set_default_shebang(shell, default_shebang);
 
+	if (timeout)
+		clish_shell__set_timeout(shell, atoi(timeout));
+
 	// remember this command
 	shell->startup = cmd;
 
@@ -658,6 +664,7 @@ process_config(clish_shell_t * shell, TiXmlElement * element, void *parent)
 
 	if (depth)
 		clish_config__set_depth(config, depth);
+
 }
 
 ///////////////////////////////////////

+ 6 - 0
tinyrl/tinyrl.c

@@ -1297,6 +1297,12 @@ void tinyrl__set_utf8(tinyrl_t * this, bool_t utf8)
 	this->utf8 = utf8;
 }
 
+/*-------------------------------------------------------- */
+void tinyrl__set_timeout(tinyrl_t *this, int timeout)
+{
+	tinyrl_vt100__set_timeout(this->term, timeout);
+}
+
 /*-------------------------------------------------------- */
 bool_t tinyrl_is_quoting(const tinyrl_t * this)
 {

+ 2 - 0
tinyrl/tinyrl.h

@@ -108,6 +108,8 @@ extern bool_t tinyrl__get_utf8(const tinyrl_t * instance);
 
 extern void tinyrl__set_utf8(tinyrl_t * instance, bool_t utf8);
 
+extern void tinyrl__set_timeout(tinyrl_t *instance, int timeout);
+
 extern char *tinyrl_readline(tinyrl_t * instance,
 			     const char *prompt, void *context);
 

+ 1 - 0
tinyrl/vt100.h

@@ -97,6 +97,7 @@ extern int tinyrl_vt100_ieof(const tinyrl_vt100_t * instance);
 extern int tinyrl_vt100_getchar(const tinyrl_vt100_t * instance);
 extern unsigned tinyrl_vt100__get_width(const tinyrl_vt100_t * instance);
 extern unsigned tinyrl_vt100__get_height(const tinyrl_vt100_t * instance);
+extern void tinyrl_vt100__set_timeout(tinyrl_vt100_t *instance, int timeout);
 extern void
 tinyrl_vt100__set_istream(tinyrl_vt100_t * instance, FILE * istream);
 extern FILE *tinyrl_vt100__get_istream(const tinyrl_vt100_t * instance);

+ 1 - 0
tinyrl/vt100/private.h

@@ -3,4 +3,5 @@
 struct _tinyrl_vt100 {
 	FILE *istream;
 	FILE *ostream;
+	int   timeout; /* Input timeout in seconds */
 };

+ 36 - 2
tinyrl/vt100/vt100.c

@@ -5,6 +5,9 @@
 #include <fcntl.h>
 #include <string.h>
 #include <sys/ioctl.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/select.h>
 
 #include "private.h"
 
@@ -121,9 +124,33 @@ tinyrl_vt100_vprintf(const tinyrl_vt100_t * this, const char *fmt, va_list args)
 }
 
 /*-------------------------------------------------------- */
-int tinyrl_vt100_getchar(const tinyrl_vt100_t * this)
+int tinyrl_vt100_getchar(const tinyrl_vt100_t *this)
 {
-	return getc(this->istream);
+	int result = EOF;
+	int istream_fd;
+	fd_set rfds;
+	struct timeval tv;
+	int retval;
+
+	/* Just wait for the input if no timeout */
+	if (this->timeout <= 0)
+		return getc(this->istream);
+
+	/* Set timeout for the select() */
+	istream_fd = fileno(this->istream);
+	FD_ZERO(&rfds);
+	FD_SET(istream_fd, &rfds);
+	tv.tv_sec = this->timeout;
+	tv.tv_usec = 0;
+
+	retval = select(istream_fd + 1, &rfds, NULL, NULL, &tv);
+
+	if (retval < 0)
+		return EOF;
+	else if (retval)
+		result = getc(this->istream);
+
+	return result;
 }
 
 /*-------------------------------------------------------- */
@@ -196,6 +223,7 @@ tinyrl_vt100_init(tinyrl_vt100_t * this, FILE * istream, FILE * ostream)
 {
 	this->istream = istream;
 	this->ostream = ostream;
+	this->timeout = -1; /* No timeout by default */
 }
 
 /*-------------------------------------------------------- */
@@ -355,6 +383,12 @@ void tinyrl_vt100_erase(const tinyrl_vt100_t * this, unsigned count)
 	tinyrl_vt100_printf(this, "%c[%dP", KEY_ESC, count);
 }
 
+/*-------------------------------------------------------- */
+void tinyrl_vt100__set_timeout(tinyrl_vt100_t *this, int timeout)
+{
+	this->timeout = timeout;
+}
+
 /*-------------------------------------------------------- */
 void tinyrl_vt100_erase_down(const tinyrl_vt100_t * this)
 {