[][src]Crate termios

The termios crate provides Rust bindings for the POSIX termios API that is implemented on Unix operating systems. The termios API is defined in the IEEE Std 1003.1 ("POSIX.1") specification.

Getting Started

The termios API operates on file descriptors that are associated with terminal devices, e.g., /dev/tty*. When used with other file descriptors, termios functions return an error. All functions that are part of the POSIX standard are included in the termios crate. Where file descriptors are expected, the type std::os::unix::io::RawFd is used, and integer error codes are translated to std::io::Result.

A major feature of the termios API is configuring a terminal device's parameters. The POSIX standard defines a termios structure that contains the parameters and several functions for manipulating the parameters. The termios crate defines a safe constructor that returns a Termios struct populated with the parameters of an open terminal device:

use termios::*;
let mut termios = Termios::from_fd(fd).unwrap();

The Termios struct provides access to the fields defined in the POSIX standard (c_iflag, c_oflag, c_cflag, c_lflag, and c_cc):

termios.c_cflag |= CREAD | CLOCAL;
termios.c_lflag &= !(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ISIG | IEXTEN);
termios.c_oflag &= !OPOST;
termios.c_iflag &= !(INLCR | IGNCR | ICRNL | IGNBRK);

termios.c_cc[VMIN] = 0;
termios.c_cc[VTIME] = 0;

The Termios struct can also be manipulated using any of the standard termios API functions:

cfsetispeed(&mut termios, B9600).unwrap();
cfsetospeed(&mut termios, B9600).unwrap();
tcsetattr(fd, TCSANOW, &termios).unwrap();


The termios crate is organized in a way to help write portable code, while also allowing access to OS-specific functionality when necessary.

The crate root contains types, constants, and function definitions that are common across Unix operating systems. Most of the definitions in the crate root are from the POSIX standard; however, support for the standard may differ across operating systems. A couple functions in the crate root are not part of the POSIX standard, but are included in the crate root because they are widely available across Unix operating systems.

To write portable code, import the termios crate and use only the definitions from the crate root.

OS-Specific Extensions

Each operating system may define extensions to the POSIX API. To make it clear when code depends on OS-specific definitions, any non-standard definitions are exported in the termios::os module. Programs that depend on OS-specific functionality must explicity opt-in. When writing portable code that depends on OS-specific definitions, it will often be necessary to use #[cfg(...)] attributes to support alternative implementations. The following is an example of a portable function that sets the maximum speed on a Termios struct.

use std::io;
use termios::{Termios,cfsetspeed};

#[cfg(target_os = "linux")]
fn set_fastest_speed(termios: &mut Termios) -> io::Result<()> {
    cfsetspeed(termios, termios::os::linux::B4000000)

#[cfg(target_os = "macos")]
fn set_fastest_speed(termios: &mut Termios) -> io::Result<()> {
    cfsetspeed(termios, termios::os::macos::B230400)

#[cfg(target_os = "freebsd")]
fn set_fastest_speed(termios: &mut Termios) -> io::Result<()> {
    cfsetspeed(termios, termios::os::freebsd::B921600)

#[cfg(target_os = "openbsd")]
fn set_fastest_speed(termios: &mut Termios) -> io::Result<()> {
    cfsetspeed(termios, termios::os::openbsd::B921600)

#[cfg(target_os = "netbsd")]
fn set_fastest_speed(termios: &mut Termios) -> io::Result<()> {
    cfsetspeed(termios, termios::os::netbsd::B921600)

#[cfg(target_os = "dragonfly")]
fn set_fastest_speed(termios: &mut Termios) -> io::Result<()> {
    cfsetspeed(termios, termios::os::dragonfly::B230400)

#[cfg(target_os = "solaris")]
fn set_fastest_speed(termios: &mut Termios) -> io::Result<()> {
    cfsetspeed(termios, termios::os::solaris::B921600)

#[cfg(target_os = "illumos")]
fn set_fastest_speed(termios: &mut Termios) -> io::Result<()> {
    cfsetspeed(termios, termios::os::illumos::B921600)

let mut termios = Termios::from_fd(fd).unwrap();
set_fastest_speed(&mut termios).unwrap();


pub use os::target::cc_t;
pub use os::target::speed_t;
pub use os::target::tcflag_t;
pub use os::target::VEOF;
pub use os::target::VEOL;
pub use os::target::VERASE;
pub use os::target::VINTR;
pub use os::target::VKILL;
pub use os::target::VMIN;
pub use os::target::VQUIT;
pub use os::target::VSTART;
pub use os::target::VSTOP;
pub use os::target::VSUSP;
pub use os::target::VTIME;
pub use os::target::BRKINT;
pub use os::target::ICRNL;
pub use os::target::IGNBRK;
pub use os::target::IGNCR;
pub use os::target::IGNPAR;
pub use os::target::INLCR;
pub use os::target::INPCK;
pub use os::target::ISTRIP;
pub use os::target::IXANY;
pub use os::target::IXOFF;
pub use os::target::IXON;
pub use os::target::PARMRK;
pub use os::target::OPOST;
pub use os::target::ONLCR;
pub use os::target::OCRNL;
pub use os::target::ONOCR;
pub use os::target::ONLRET;
pub use os::target::B0;
pub use os::target::B50;
pub use os::target::B75;
pub use os::target::B110;
pub use os::target::B134;
pub use os::target::B150;
pub use os::target::B200;
pub use os::target::B300;
pub use os::target::B600;
pub use os::target::B1200;
pub use os::target::B1800;
pub use os::target::B2400;
pub use os::target::B4800;
pub use os::target::B9600;
pub use os::target::B19200;
pub use os::target::B38400;
pub use os::target::CSIZE;
pub use os::target::CS5;
pub use os::target::CS6;
pub use os::target::CS7;
pub use os::target::CS8;
pub use os::target::CSTOPB;
pub use os::target::CREAD;
pub use os::target::PARENB;
pub use os::target::PARODD;
pub use os::target::HUPCL;
pub use os::target::CLOCAL;
pub use os::target::ECHO;
pub use os::target::ECHOE;
pub use os::target::ECHOK;
pub use os::target::ECHONL;
pub use os::target::ICANON;
pub use os::target::IEXTEN;
pub use os::target::ISIG;
pub use os::target::NOFLSH;
pub use os::target::TOSTOP;
pub use os::target::TCSANOW;
pub use os::target::TCSADRAIN;
pub use os::target::TCSAFLUSH;
pub use os::target::TCIFLUSH;
pub use os::target::TCIOFLUSH;
pub use os::target::TCOFLUSH;
pub use os::target::TCIOFF;
pub use os::target::TCION;
pub use os::target::TCOOFF;
pub use os::target::TCOON;



Unsafe FFI bindings.


OS-specific definitions.



Unix terminal I/O control structure.



Gets the input baud rate stored in a Termios structure.


Gets the output baud rate stored in a Termios structure.


Sets flags to disable all input and output processing.


Sets the input baud rate.


Sets the output baud rate.


Sets input and output baud rates.


Blocks until all output written to the file descriptor is transmitted.


Suspends or restarts transmission or reception of data.


Discards data waiting in the terminal device's buffers.


Populates a Termios structure with parameters associated with a terminal.


Returns the process group ID of the controlling terminal's session.


Transmits data to generate a break condition.


Sets a terminal device's parameters.