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

Commit

Permalink
Merge pull request #2 from andersk/libc
Browse files Browse the repository at this point in the history
Use cfmakeraw, tcsetattr, tcgetattr already exported by libc
  • Loading branch information
zrzka authored Oct 3, 2019
2 parents e473b4f + 5d15190 commit 7eac239
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
@@ -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};

@@ -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<()> {

0 comments on commit 7eac239

Please sign in to comment.