aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-04-01 02:06:35 -0500
committerJesse Luehrs <doy@tozt.net>2013-04-01 02:06:35 -0500
commitb7646b2d791cf10bd604aaee830fe22598b7d10d (patch)
tree9ba44d9aa729587e61b9b7c8671367d91fa8ef7e /src/util.rs
parent10255902df82b54afa68290cd7ce59cdcf2014fa (diff)
downloadrust-term-b7646b2d791cf10bd604aaee830fe22598b7d10d.tar.gz
rust-term-b7646b2d791cf10bd604aaee830fe22598b7d10d.zip
provide a timeout for reads when trying to match an escape
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
index 7ea5389..8ed877a 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -1,3 +1,5 @@
+use core::libc::c_int;
+
pub fn guard<T> (finally: ~fn (), body: &fn () -> T) -> T {
let _guard = Guard { finally: finally };
body()
@@ -218,3 +220,28 @@ fn check_has_prefix (trie: &Trie<int>, find: &str) {
fn check_not_has_prefix (trie: &Trie<int>, find: &str) {
fail_unless!(!trie.has_prefix(find));
}
+
+// XXX huge hack until there's a better built-in way to do this
+pub fn timed_read (timeout: int) -> Option<char> {
+ let first = unsafe { io_helper::timed_read(timeout as c_int) };
+ if first < 0 {
+ return None;
+ }
+
+ let mut buf = ~[first as u8];
+ let nbytes = str::utf8_char_width(first as u8);
+
+ for uint::range(0, nbytes - 1) |_| {
+ let next = unsafe { io_helper::timed_read(-1 as c_int) };
+ if next < 0 {
+ return None;
+ }
+ vec::push(&mut buf, next as u8);
+ }
+
+ Some(str::char_at(unsafe { str::raw::from_bytes(buf) }, 0))
+}
+
+extern mod io_helper {
+ fn timed_read (timeout: c_int) -> c_int;
+}