Proper raw mode... by properly combining bitflags

This commit is contained in:
Timothy Warren 2019-08-23 13:33:18 -04:00
parent 144fd65412
commit 84e0cd89a8
1 changed files with 23 additions and 37 deletions

View File

@ -1,15 +1,10 @@
/// Helper functions, especially to reproduce C std/posix functions
use nix::errno::*;
use nix::Error as NixError;
use nix::sys::termios;
use nix::sys::termios::{
ControlFlags,
InputFlags,
LocalFlags,
OutputFlags,
SpecialCharacterIndices,
Termios
ControlFlags, InputFlags, LocalFlags, OutputFlags, SpecialCharacterIndices, Termios,
};
use nix::Error as NixError;
use std::io::Error;
use std::os::unix::io::RawFd;
@ -20,13 +15,6 @@ pub const STDIN_FILENO: i32 = 0;
// pub const STDOUT_FILENO: i32 = 1;
// pub const STDERR_FILENO: i32 = 2;
/// Is this character an ASCII control character?
pub fn is_cntrl(c: char) -> bool {
let code = c as u8;
code < 32 || code == 127
}
pub fn die(code: &Errno, msg: &str) -> ! {
eprintln!("{:?} ({})", code, msg);
@ -41,31 +29,29 @@ pub fn enable_raw_mode() -> Result<(), Error> {
let mut raw = get_termios(STDIN_FILENO);
raw.input_flags.remove(
InputFlags::BRKINT &
InputFlags::IGNCR &
InputFlags::IGNBRK &
InputFlags::ICRNL &
InputFlags::INLCR &
InputFlags::INPCK &
InputFlags::ISTRIP &
InputFlags::IXON &
InputFlags::PARMRK
InputFlags::BRKINT
| InputFlags::IGNCR
| InputFlags::IGNBRK
| InputFlags::ICRNL
| InputFlags::INLCR
| InputFlags::INPCK
| InputFlags::ISTRIP
| InputFlags::IXON
| InputFlags::PARMRK,
);
raw.output_flags.remove(OutputFlags::OPOST);
raw.local_flags.remove(
LocalFlags::ECHO &
LocalFlags::ECHONL &
LocalFlags::ICANON &
LocalFlags::IEXTEN &
LocalFlags::ISIG
LocalFlags::ECHO
| LocalFlags::ECHONL
| LocalFlags::ICANON
| LocalFlags::IEXTEN
| LocalFlags::ISIG,
);
raw.control_flags.remove(
ControlFlags::CSIZE &
ControlFlags::PARENB
);
raw.control_flags
.remove(ControlFlags::CSIZE | ControlFlags::PARENB);
// 8 bit characters
raw.control_flags |= termios::ControlFlags::CS8;
@ -77,8 +63,8 @@ pub fn enable_raw_mode() -> Result<(), Error> {
Ok(()) => Ok(()),
Err(e) => match e.as_errno() {
Some(errno) => die(&errno, "tcsetattr"),
None => panic!("Failed to enable raw mode")
}
None => panic!("Failed to enable raw mode"),
},
}
}
@ -87,7 +73,7 @@ pub fn disable_raw_mode(original: &Termios) -> Result<(), Error> {
Ok(()) => Ok(()),
Err(E) => match E.as_errno() {
Some(errno) => die(&errno, "tcsetattr"),
None => panic!("Failed to disable raw mode")
}
None => panic!("Failed to disable raw mode"),
},
}
}
}