libcosmos
Linux C++ System Programming Library
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Terminal.hxx File Reference
#include <sys/ioctl.h>
#include <chrono>
#include <utility>
#include <optional>
#include <cosmos/fs/FileBase.hxx>
#include <cosmos/fs/FileDescriptor.hxx>

Go to the source code of this file.

Classes

struct  cosmos::TermDimension
 Represents a terminal dimension in characters. More...
 
class  cosmos::Terminal
 Access to Terminal information and ioctls. More...
 

Functions

std::pair< FileDescriptor, FileDescriptorcosmos::openPTY (const std::optional< TermDimension > initial_size={})
 Creates a new pseudo terminal device and returns master/slave file descriptors for it.
 

Detailed Description

This header contains types and helper concerned with terminal / TTY features of the operating system.

Definition in file Terminal.hxx.

Function Documentation

◆ openPTY()

COSMOS_API std::pair< cosmos::FileDescriptor, cosmos::FileDescriptor > cosmos::openPTY ( const std::optional< TermDimension > initial_size = {})

Creates a new pseudo terminal device and returns master/slave file descriptors for it.

A pseudo terminal is a virtual terminal where the slave end behaves like an actual terminal device and can be passed to applications that expect one. The master end drives the application using the slave end.

Any writes to the master end will appear as input from a keyboard to the slave, any writes to the slave end will appear as output data from a program on the master end.

See openpty(2) and pty(7) man pages for more information.

Parameters
[in]initial_sizeif provided then this will be the initial PTY dimension registered by the kernel. See Terminal::setSize().
Returns
a pair of the master and slave file descriptor belonging to the new PTY. The caller is responsible for managing the lifetime of the returned file descriptors (i.e. closing them at the appropriate time). On error an ApiError exception is thrown.

Definition at line 63 of file Terminal.cxx.

63 {
64 int master, slave;
65
66 // the name parameter is unsafe since there is no standardized
67 // dimension limit.
68 // the other parameters are for setting initial TTY properties and
69 // terminal size.
70
71 const struct winsize *size = initial_size ? &(*initial_size) : nullptr;
72
73 if (::openpty(&master, &slave, nullptr, nullptr, size) < 0) {
74 cosmos_throw (ApiError("openpty()"));
75 }
76
77 return {
78 cosmos::FileDescriptor(FileNum{master}),
79 cosmos::FileDescriptor(FileNum{slave})
80 };
81}
Thin Wrapper around OS file descriptors.