#![cfg_attr(any(target_os = "freebsd", target_os = "dragonfly", target_os = "ios",
target_os = "macos", target_os = "netbsd", target_os = "openbsd"),
doc = " ```rust,ignore")]
#![cfg_attr(not(any(target_os = "freebsd", target_os = "dragonfly", target_os = "ios",
target_os = "macos", target_os = "netbsd", target_os = "openbsd")),
doc = " ```rust")]
#![cfg_attr(any(target_os = "freebsd", target_os = "dragonfly", target_os = "ios",
target_os = "macos", target_os = "netbsd", target_os = "openbsd"),
doc = " ```rust")]
#![cfg_attr(not(any(target_os = "freebsd", target_os = "dragonfly", target_os = "ios",
target_os = "macos", target_os = "netbsd", target_os = "openbsd")),
doc = " ```rust,ignore")]
#![cfg_attr(any(target_os = "freebsd", target_os = "dragonfly", target_os = "ios",
target_os = "macos", target_os = "netbsd", target_os = "openbsd"),
doc = " ```rust")]
#![cfg_attr(not(any(target_os = "freebsd", target_os = "dragonfly", target_os = "ios",
target_os = "macos", target_os = "netbsd", target_os = "openbsd")),
doc = " ```rust,ignore")]
#![cfg_attr(any(target_os = "freebsd", target_os = "dragonfly", target_os = "ios",
target_os = "macos", target_os = "netbsd", target_os = "openbsd"),
doc = " ```rust")]
#![cfg_attr(not(any(target_os = "freebsd", target_os = "dragonfly", target_os = "ios",
target_os = "macos", target_os = "netbsd", target_os = "openbsd")),
doc = " ```rust,ignore")]
use {Error, Result};
use errno::Errno;
use libc::{self, c_int, tcflag_t};
use std::cell::{Ref, RefCell};
use std::convert::{From, TryFrom};
use std::mem;
use std::os::unix::io::RawFd;
use ::unistd::Pid;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Termios {
inner: RefCell<libc::termios>,
pub input_flags: InputFlags,
pub output_flags: OutputFlags,
pub control_flags: ControlFlags,
pub local_flags: LocalFlags,
pub control_chars: [libc::cc_t; NCCS],
}
impl Termios {
#[doc(hidden)]
pub fn get_libc_termios(&self) -> Ref<libc::termios> {
{
let mut termios = self.inner.borrow_mut();
termios.c_iflag = self.input_flags.bits();
termios.c_oflag = self.output_flags.bits();
termios.c_cflag = self.control_flags.bits();
termios.c_lflag = self.local_flags.bits();
termios.c_cc = self.control_chars;
}
self.inner.borrow()
}
unsafe fn get_libc_termios_mut(&mut self) -> *mut libc::termios {
{
let mut termios = self.inner.borrow_mut();
termios.c_iflag = self.input_flags.bits();
termios.c_oflag = self.output_flags.bits();
termios.c_cflag = self.control_flags.bits();
termios.c_lflag = self.local_flags.bits();
termios.c_cc = self.control_chars;
}
self.inner.as_ptr()
}
#[doc(hidden)]
pub unsafe fn default_uninit() -> Self {
Termios {
inner: RefCell::new(mem::zeroed()),
input_flags: InputFlags::empty(),
output_flags: OutputFlags::empty(),
control_flags: ControlFlags::empty(),
local_flags: LocalFlags::empty(),
control_chars: [0 as libc::cc_t; NCCS],
}
}
#[doc(hidden)]
pub fn update_wrapper(&mut self) {
let termios = *self.inner.borrow_mut();
self.input_flags = InputFlags::from_bits_truncate(termios.c_iflag);
self.output_flags = OutputFlags::from_bits_truncate(termios.c_oflag);
self.control_flags = ControlFlags::from_bits_truncate(termios.c_cflag);
self.local_flags = LocalFlags::from_bits_truncate(termios.c_lflag);
self.control_chars = termios.c_cc;
}
}
impl From<libc::termios> for Termios {
fn from(termios: libc::termios) -> Self {
Termios {
inner: RefCell::new(termios),
input_flags: InputFlags::from_bits_truncate(termios.c_iflag),
output_flags: OutputFlags::from_bits_truncate(termios.c_oflag),
control_flags: ControlFlags::from_bits_truncate(termios.c_cflag),
local_flags: LocalFlags::from_bits_truncate(termios.c_lflag),
control_chars: termios.c_cc,
}
}
}
impl From<Termios> for libc::termios {
fn from(termios: Termios) -> Self {
termios.inner.into_inner()
}
}
libc_enum!{
#[cfg_attr(all(any(target_os = "ios", target_os = "macos"), target_pointer_width = "64"), repr(u64))]
#[cfg_attr(not(all(any(target_os = "ios", target_os = "macos"), target_pointer_width = "64")), repr(u32))]
pub enum BaudRate {
B0,
B50,
B75,
B110,
B134,
B150,
B200,
B300,
B600,
B1200,
B1800,
B2400,
B4800,
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
B7200,
B9600,
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
B14400,
B19200,
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
B28800,
B38400,
B57600,
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
B76800,
B115200,
B230400,
#[cfg(any(target_os = "android",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd"))]
B460800,
#[cfg(any(target_os = "android", target_os = "linux"))]
B500000,
#[cfg(any(target_os = "android", target_os = "linux"))]
B576000,
#[cfg(any(target_os = "android",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd"))]
B921600,
#[cfg(any(target_os = "android", target_os = "linux"))]
B1000000,
#[cfg(any(target_os = "android", target_os = "linux"))]
B1152000,
#[cfg(any(target_os = "android", target_os = "linux"))]
B1500000,
#[cfg(any(target_os = "android", target_os = "linux"))]
B2000000,
#[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))]
B2500000,
#[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))]
B3000000,
#[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))]
B3500000,
#[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))]
B4000000,
}
}
impl TryFrom<libc::speed_t> for BaudRate {
type Error = Error;
fn try_from(s: libc::speed_t) -> Result<BaudRate> {
use libc::{B0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400, B4800,
B9600, B19200, B38400, B57600, B115200, B230400};
#[cfg(any(target_os = "android", target_os = "linux"))]
use libc::{B500000, B576000, B1000000, B1152000, B1500000, B2000000};
#[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))]
use libc::{B2500000, B3000000, B3500000, B4000000};
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
use libc::{B7200, B14400, B28800, B76800};
#[cfg(any(target_os = "android",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd"))]
use libc::{B460800, B921600};
match s {
B0 => Ok(BaudRate::B0),
B50 => Ok(BaudRate::B50),
B75 => Ok(BaudRate::B75),
B110 => Ok(BaudRate::B110),
B134 => Ok(BaudRate::B134),
B150 => Ok(BaudRate::B150),
B200 => Ok(BaudRate::B200),
B300 => Ok(BaudRate::B300),
B600 => Ok(BaudRate::B600),
B1200 => Ok(BaudRate::B1200),
B1800 => Ok(BaudRate::B1800),
B2400 => Ok(BaudRate::B2400),
B4800 => Ok(BaudRate::B4800),
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
B7200 => Ok(BaudRate::B7200),
B9600 => Ok(BaudRate::B9600),
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
B14400 => Ok(BaudRate::B14400),
B19200 => Ok(BaudRate::B19200),
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
B28800 => Ok(BaudRate::B28800),
B38400 => Ok(BaudRate::B38400),
B57600 => Ok(BaudRate::B57600),
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
B76800 => Ok(BaudRate::B76800),
B115200 => Ok(BaudRate::B115200),
B230400 => Ok(BaudRate::B230400),
#[cfg(any(target_os = "android",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd"))]
B460800 => Ok(BaudRate::B460800),
#[cfg(any(target_os = "android", target_os = "linux"))]
B500000 => Ok(BaudRate::B500000),
#[cfg(any(target_os = "android", target_os = "linux"))]
B576000 => Ok(BaudRate::B576000),
#[cfg(any(target_os = "android",
target_os = "freebsd",
target_os = "linux",
target_os = "netbsd"))]
B921600 => Ok(BaudRate::B921600),
#[cfg(any(target_os = "android", target_os = "linux"))]
B1000000 => Ok(BaudRate::B1000000),
#[cfg(any(target_os = "android", target_os = "linux"))]
B1152000 => Ok(BaudRate::B1152000),
#[cfg(any(target_os = "android", target_os = "linux"))]
B1500000 => Ok(BaudRate::B1500000),
#[cfg(any(target_os = "android", target_os = "linux"))]
B2000000 => Ok(BaudRate::B2000000),
#[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))]
B2500000 => Ok(BaudRate::B2500000),
#[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))]
B3000000 => Ok(BaudRate::B3000000),
#[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))]
B3500000 => Ok(BaudRate::B3500000),
#[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "sparc64"))))]
B4000000 => Ok(BaudRate::B4000000),
_ => Err(Error::invalid_argument())
}
}
}
#[cfg(any(target_os = "freebsd",
target_os = "dragonfly",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
impl From<BaudRate> for u32 {
fn from(b: BaudRate) -> u32 {
b as u32
}
}
libc_enum! {
#[repr(i32)]
pub enum SetArg {
TCSANOW,
TCSADRAIN,
TCSAFLUSH,
}
}
libc_enum! {
#[repr(i32)]
pub enum FlushArg {
TCIFLUSH,
TCOFLUSH,
TCIOFLUSH,
}
}
libc_enum! {
#[repr(i32)]
pub enum FlowArg {
TCOOFF,
TCOON,
TCIOFF,
TCION,
}
}
libc_enum! {
#[repr(usize)]
pub enum SpecialCharacterIndices {
VDISCARD,
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
VDSUSP,
VEOF,
VEOL,
VEOL2,
VERASE,
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
VERASE2,
VINTR,
VKILL,
VLNEXT,
#[cfg(not(all(target_os = "linux", target_arch = "sparc64")))]
VMIN,
VQUIT,
VREPRINT,
VSTART,
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
VSTATUS,
VSTOP,
VSUSP,
#[cfg(target_os = "linux")]
VSWTC,
#[cfg(target_os = "haiku")]
VSWTCH,
#[cfg(not(all(target_os = "linux", target_arch = "sparc64")))]
VTIME,
VWERASE,
#[cfg(target_os = "dragonfly")]
VCHECKPT,
}
}
#[cfg(all(target_os = "linux", target_arch = "sparc64"))]
impl SpecialCharacterIndices {
pub const VMIN: SpecialCharacterIndices = SpecialCharacterIndices::VEOF;
pub const VTIME: SpecialCharacterIndices = SpecialCharacterIndices::VEOL;
}
pub use libc::NCCS;
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "linux",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
pub use libc::_POSIX_VDISABLE;
libc_bitflags! {
pub struct InputFlags: tcflag_t {
IGNBRK;
BRKINT;
IGNPAR;
PARMRK;
INPCK;
ISTRIP;
INLCR;
IGNCR;
ICRNL;
IXON;
IXOFF;
IXANY;
IMAXBEL;
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
IUTF8;
}
}
libc_bitflags! {
pub struct OutputFlags: tcflag_t {
OPOST;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "linux",
target_os = "openbsd"))]
OLCUC;
ONLCR;
OCRNL as tcflag_t;
ONOCR as tcflag_t;
ONLRET as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
OFILL as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
OFDEL as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
NL0 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
NL1 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
CR0 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
CR1 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
CR2 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
CR3 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "freebsd",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
TAB0 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
TAB1 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
TAB2 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "freebsd",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
TAB3 as tcflag_t;
#[cfg(any(target_os = "android", target_os = "linux"))]
XTABS;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
BS0 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
BS1 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
VT0 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
VT1 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
FF0 as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
FF1 as tcflag_t;
#[cfg(any(target_os = "freebsd",
target_os = "dragonfly",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
OXTABS;
#[cfg(any(target_os = "freebsd",
target_os = "dragonfly",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
ONOEOT as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
NLDLY as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
CRDLY as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "freebsd",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
TABDLY as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
BSDLY as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
VTDLY as tcflag_t;
#[cfg(any(target_os = "android",
target_os = "haiku",
target_os = "ios",
target_os = "linux",
target_os = "macos"))]
FFDLY as tcflag_t;
}
}
libc_bitflags! {
pub struct ControlFlags: tcflag_t {
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
CIGNORE;
CS5;
CS6;
CS7;
CS8;
CSTOPB;
CREAD;
PARENB;
PARODD;
HUPCL;
CLOCAL;
CRTSCTS;
#[cfg(any(target_os = "android", target_os = "linux"))]
CBAUD;
#[cfg(any(target_os = "android", all(target_os = "linux", not(target_arch = "mips"))))]
CMSPAR;
#[cfg(any(target_os = "android",
all(target_os = "linux",
not(any(target_arch = "powerpc", target_arch = "powerpc64")))))]
CIBAUD;
#[cfg(any(target_os = "android", target_os = "linux"))]
CBAUDEX;
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
MDMBUF;
#[cfg(any(target_os = "netbsd", target_os = "openbsd"))]
CHWFLOW;
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"))]
CCTS_OFLOW;
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"))]
CRTS_IFLOW;
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd"))]
CDTR_IFLOW;
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd"))]
CDSR_OFLOW;
#[cfg(any(target_os = "dragonfly",
target_os = "freebsd"))]
CCAR_OFLOW;
CSIZE;
}
}
libc_bitflags! {
pub struct LocalFlags: tcflag_t {
ECHOKE;
ECHOE;
ECHOK;
ECHO;
ECHONL;
ECHOPRT;
ECHOCTL;
ISIG;
ICANON;
#[cfg(any(target_os = "freebsd",
target_os = "dragonfly",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
ALTWERASE;
IEXTEN;
EXTPROC;
TOSTOP;
FLUSHO;
#[cfg(any(target_os = "freebsd",
target_os = "dragonfly",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
NOKERNINFO;
PENDIN;
NOFLSH;
}
}
cfg_if!{
if #[cfg(any(target_os = "freebsd",
target_os = "dragonfly",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))] {
pub fn cfgetispeed(termios: &Termios) -> u32 {
let inner_termios = termios.get_libc_termios();
unsafe { libc::cfgetispeed(&*inner_termios) as u32 }
}
pub fn cfgetospeed(termios: &Termios) -> u32 {
let inner_termios = termios.get_libc_termios();
unsafe { libc::cfgetospeed(&*inner_termios) as u32 }
}
pub fn cfsetispeed<T: Into<u32>>(termios: &mut Termios, baud: T) -> Result<()> {
let inner_termios = unsafe { termios.get_libc_termios_mut() };
let res = unsafe { libc::cfsetispeed(inner_termios, baud.into() as libc::speed_t) };
termios.update_wrapper();
Errno::result(res).map(drop)
}
pub fn cfsetospeed<T: Into<u32>>(termios: &mut Termios, baud: T) -> Result<()> {
let inner_termios = unsafe { termios.get_libc_termios_mut() };
let res = unsafe { libc::cfsetospeed(inner_termios, baud.into() as libc::speed_t) };
termios.update_wrapper();
Errno::result(res).map(drop)
}
pub fn cfsetspeed<T: Into<u32>>(termios: &mut Termios, baud: T) -> Result<()> {
let inner_termios = unsafe { termios.get_libc_termios_mut() };
let res = unsafe { libc::cfsetspeed(inner_termios, baud.into() as libc::speed_t) };
termios.update_wrapper();
Errno::result(res).map(drop)
}
} else {
use std::convert::TryInto;
pub fn cfgetispeed(termios: &Termios) -> BaudRate {
let inner_termios = termios.get_libc_termios();
unsafe { libc::cfgetispeed(&*inner_termios) }.try_into().unwrap()
}
pub fn cfgetospeed(termios: &Termios) -> BaudRate {
let inner_termios = termios.get_libc_termios();
unsafe { libc::cfgetospeed(&*inner_termios) }.try_into().unwrap()
}
pub fn cfsetispeed(termios: &mut Termios, baud: BaudRate) -> Result<()> {
let inner_termios = unsafe { termios.get_libc_termios_mut() };
let res = unsafe { libc::cfsetispeed(inner_termios, baud as libc::speed_t) };
termios.update_wrapper();
Errno::result(res).map(drop)
}
pub fn cfsetospeed(termios: &mut Termios, baud: BaudRate) -> Result<()> {
let inner_termios = unsafe { termios.get_libc_termios_mut() };
let res = unsafe { libc::cfsetospeed(inner_termios, baud as libc::speed_t) };
termios.update_wrapper();
Errno::result(res).map(drop)
}
pub fn cfsetspeed(termios: &mut Termios, baud: BaudRate) -> Result<()> {
let inner_termios = unsafe { termios.get_libc_termios_mut() };
let res = unsafe { libc::cfsetspeed(inner_termios, baud as libc::speed_t) };
termios.update_wrapper();
Errno::result(res).map(drop)
}
}
}
pub fn cfmakeraw(termios: &mut Termios) {
let inner_termios = unsafe { termios.get_libc_termios_mut() };
unsafe {
libc::cfmakeraw(inner_termios);
}
termios.update_wrapper();
}
#[cfg(target_os = "freebsd")]
pub fn cfmakesane(termios: &mut Termios) {
let inner_termios = unsafe { termios.get_libc_termios_mut() };
unsafe {
libc::cfmakesane(inner_termios);
}
termios.update_wrapper();
}
pub fn tcgetattr(fd: RawFd) -> Result<Termios> {
let mut termios = mem::MaybeUninit::uninit();
let res = unsafe { libc::tcgetattr(fd, termios.as_mut_ptr()) };
Errno::result(res)?;
unsafe { Ok(termios.assume_init().into()) }
}
pub fn tcsetattr(fd: RawFd, actions: SetArg, termios: &Termios) -> Result<()> {
let inner_termios = termios.get_libc_termios();
Errno::result(unsafe { libc::tcsetattr(fd, actions as c_int, &*inner_termios) }).map(drop)
}
pub fn tcdrain(fd: RawFd) -> Result<()> {
Errno::result(unsafe { libc::tcdrain(fd) }).map(drop)
}
pub fn tcflow(fd: RawFd, action: FlowArg) -> Result<()> {
Errno::result(unsafe { libc::tcflow(fd, action as c_int) }).map(drop)
}
pub fn tcflush(fd: RawFd, action: FlushArg) -> Result<()> {
Errno::result(unsafe { libc::tcflush(fd, action as c_int) }).map(drop)
}
pub fn tcsendbreak(fd: RawFd, duration: c_int) -> Result<()> {
Errno::result(unsafe { libc::tcsendbreak(fd, duration) }).map(drop)
}
pub fn tcgetsid(fd: RawFd) -> Result<Pid> {
let res = unsafe { libc::tcgetsid(fd) };
Errno::result(res).map(Pid::from_raw)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn try_from() {
assert_eq!(Ok(BaudRate::B0), BaudRate::try_from(libc::B0));
assert!(BaudRate::try_from(999999999).is_err());
}
}