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
64
65
66
67
68
69
70
71
72
73
74
75
extern crate libc;

use std::io;
use std::mem;
use std::os::unix::io::RawFd;

use libc::c_int;

#[cfg(any(target_os = "linux", target_os = "android"))]
pub use os::linux::*;

#[cfg(target_os = "macos")]
pub use os::macos::*;

#[cfg(target_os = "freebsd")]
pub use os::freebsd::*;

#[cfg(target_os = "dragonfly")]
pub use os::dragonfly::*;

#[cfg(target_os = "openbsd")]
pub use os::openbsd::*;

mod os;


/// Put the terminal in exclusive mode.
pub fn tiocexcl(fd: RawFd) -> io::Result<()> {
    match unsafe { ioctl(fd, TIOCEXCL) } {
        0 => Ok(()),
        _ => Err(io::Error::last_os_error())
    }
}

/// Disable exclusive mode.
pub fn tiocnxcl(fd: RawFd) -> io::Result<()> {
    match unsafe { ioctl(fd, TIOCNXCL) } {
        0 => Ok(()),
        _ => Err(io::Error::last_os_error())
    }
}

/// Get the status of modem bits.
pub fn tiocmget(fd: RawFd) -> io::Result<c_int> {
    let mut bits: c_int = unsafe { mem::uninitialized() };

    match unsafe { ioctl(fd, TIOCMGET, &mut bits) } {
        0 => Ok(bits),
        _ => Err(io::Error::last_os_error())
    }
}

/// Set the status of modem bits.
pub fn tiocmset(fd: RawFd, bits: c_int) -> io::Result<()> {
    match unsafe { ioctl(fd, TIOCMSET, &bits) } {
        0 => Ok(()),
        _ => Err(io::Error::last_os_error())
    }
}

/// Set the indicated modem bits.
pub fn tiocmbis(fd: RawFd, bits: c_int) -> io::Result<()> {
    match unsafe { ioctl(fd, TIOCMBIS, &bits) } {
        0 => Ok(()),
        _ => Err(io::Error::last_os_error())
    }
}

/// Clear the indicated modem bits.
pub fn tiocmbic(fd: RawFd, bits: c_int) -> io::Result<()> {
    match unsafe { ioctl(fd, TIOCMBIC, &bits) } {
        0 => Ok(()),
        _ => Err(io::Error::last_os_error())
    }
}