diff src/sysdep.cc @ 3657:a908150a3a32

[project @ 2000-04-11 19:02:03 by jwe]
author jwe
date Tue, 11 Apr 2000 19:02:05 +0000
parents 97cf542676e1
children 808f399398c9
line wrap: on
line diff
--- a/src/sysdep.cc	Tue Apr 04 06:16:23 2000 +0000
+++ b/src/sysdep.cc	Tue Apr 11 19:02:05 2000 +0000
@@ -169,9 +169,9 @@
 // It doesn't matter whether an input \n is mapped to \r, or vice versa.
 
 void
-raw_mode (int on)
+raw_mode (bool on, bool wait)
 {
-  static int curr_on = 0;
+  static bool curr_on = false;
 
   int tty_fd = STDIN_FILENO;
   if (! isatty (tty_fd))
@@ -215,7 +215,10 @@
 #if defined (ONLRET)
 	s.c_oflag &= ~(ONLRET);
 #endif
-	s.c_cc[VMIN] = 1;
+	if (wait)
+	  s.c_cc[VMIN] = 1;
+	else
+	  s.c_cc[VMIN] = 0;		
 	s.c_cc[VTIME] = 0;
       }      
     else
@@ -257,7 +260,10 @@
 #if defined (ONLRET)
 	s.c_oflag &= ~(ONLRET);
 #endif
-	s.c_cc[VMIN] = 1;
+	if (wait)
+	  s.c_cc[VMIN] = 1;
+	else
+	  s.c_cc[VMIN] = 0;		
 	s.c_cc[VTIME] = 0;
       }      
     else
@@ -309,12 +315,14 @@
 // Read one character from the terminal.
 
 int
-kbhit (void)
+kbhit (bool wait)
 {
   int c;
-  raw_mode (1);
+  raw_mode (1, wait);
   c = std::cin.get ();
-  raw_mode (0);
+  if (std::cin.fail())
+	  std::cin.clear ();
+  raw_mode (0, 1);
   return c;
 }
 
@@ -396,10 +404,11 @@
 
 // XXX FIXME XXX -- perhaps kbhit should also be able to print a prompt?
 
-DEFUN (kbhit, , ,
+DEFUN (kbhit, args, ,
   "-*- texinfo -*-\n\
 @deftypefn {Built-in Function} {} kbhit ()\n\
-Read a single keystroke from the keyboard.  For example,\n\
+Read a single keystroke from the keyboard. If called with one\n\
+argument, don't wait for a keypress.  For example,\n\
 \n\
 @example\n\
 x = kbhit ();\n\
@@ -408,15 +417,34 @@
 @noindent\n\
 will set @var{x} to the next character typed at the keyboard as soon as\n\
 it is typed.\n\
+\n\
+@example\n\
+x = kbhit (1);\n\
+@end example\n\
+\n\
+@noindent\n\
+identical to the above example, but don't wait for a keypress,\n\
+returning the empty string if no key is available.\n\
 @end deftypefn")
 {
   octave_value_list retval;
 
   // XXX FIXME XXX -- add timeout and default value args?
 
+  int nargin = args.length ();
+	
   if (interactive)
     {
-      int c = kbhit ();
+    	int c;
+
+	if (nargin == 1)
+	  c = kbhit (false);
+      	else
+	  c = kbhit (true);
+
+	if (c == -1)
+	  c = 0;
+
       char *s = new char [2];
       s[0] = c;
       s[1] = '\0';