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

View File

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