aboutsummaryrefslogtreecommitdiffstats
path: root/src/ios.rs
blob: f1e6e80d5259c2a08ea2f18a1a47c577a9046e83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use core::libc::{c_int,c_void};
use guard::guard;

/**
 * Put the terminal into cooked mode.
 *
 * This is the normal line-buffered mode.
 */
pub fn cooked () -> int {
    unsafe { c::cooked() as int }
}

/**
 * Put the terminal into cbreak mode.
 *
 * This is the normal unbuffered mode.
 */
pub fn cbreak () -> int {
    unsafe { c::cbreak() as int }
}

/**
 * Put the terminal into raw mode.
 *
 * This is like cbreak mode, except that control characters (like ^C) are not
 * translated into signals.
 */
pub fn raw () -> int {
    unsafe { c::raw() as int }
}

/**
 * Change the echo mode of the terminal.
 *
 * `true` turns echo on, and `false` turns echo off.
 */
pub fn echo (enable: bool) -> int {
    unsafe { c::echo(enable as c_int) as int }
}

/**
 * Run a block of code, restoring the terminal state when the block ends.
 *
 * This will ensure you don't leave the terminal in a broken state, even if
 * the current task fails.
 */
pub fn preserve<T> (body: &fn () -> T) -> T {
    let orig = unsafe { c::get() };
    do guard(|| { unsafe { c::set(orig) } }) {
        body()
    }
}

#[link_name = "termios_wrapper"]
extern mod c {
    fn cooked () -> c_int;
    fn cbreak () -> c_int;
    fn raw () -> c_int;
    fn echo (enable: c_int) -> c_int;

    fn get() -> *c_void;
    fn set(t: *c_void);
}