Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
unix: Use cfmakeraw, tcsetattr, tcgetattr already exported by libc
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Loading branch information
andersk committed Oct 3, 2019
1 parent 4239d5f commit 5d15190
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
use std::{io, mem};

pub use libc::{c_int, termios as Termios};
pub use libc::termios as Termios;
use libc::{cfmakeraw, tcgetattr, tcsetattr, STDIN_FILENO, TCSANOW};

use crate::{ErrorKind, Result};

Expand All @@ -19,28 +20,19 @@ fn wrap_with_result(t: i32) -> Result<()> {

/// Transform the given mode into an raw mode (non-canonical) mode.
pub fn raw_terminal_attr(termios: &mut Termios) {
extern "C" {
pub fn cfmakeraw(termptr: *mut Termios);
}
unsafe { cfmakeraw(termios) }
}

pub fn get_terminal_attr() -> Result<Termios> {
extern "C" {
pub fn tcgetattr(fd: c_int, termptr: *mut Termios) -> c_int;
}
unsafe {
let mut termios = mem::zeroed();
wrap_with_result(tcgetattr(0, &mut termios))?;
wrap_with_result(tcgetattr(STDIN_FILENO, &mut termios))?;
Ok(termios)
}
}

pub fn set_terminal_attr(termios: &Termios) -> Result<()> {
extern "C" {
pub fn tcsetattr(fd: c_int, opt: c_int, termptr: *const Termios) -> c_int;
}
wrap_with_result(unsafe { tcsetattr(0, 0, termios) })
wrap_with_result(unsafe { tcsetattr(STDIN_FILENO, TCSANOW, termios) })
}

pub fn enable_raw_mode() -> Result<()> {
Expand Down

0 comments on commit 5d15190

Please sign in to comment.