Module
std.term — the controlling terminal: whose turn it is, and how it reads. Stability tier: core. The facade over wolf-lang's [os.term] (s219).
Whose turn. One process group at a time is the terminal's FOREGROUND: the group its Ctrl-C, Ctrl-\ and Ctrl-Z signal and the only one that may read it. A job-control shell hands the foreground to a job (std.process.start_job does it in the child, before the job runs) and takes it back with set_foreground(tty, group()) when the job ends. The setters block SIGTTOU for their own duration, so a shell outside the foreground is not stopped for taking its terminal back.
How it reads. A Mode is the things an interactive program changes: canonical (a line at a time, with the terminal's own editing), echo, signals (Ctrl-C, Ctrl-\ and Ctrl-Z become signals), flow (Ctrl-S and Ctrl-Q pause and resume output and never reach the program), extended (the host's extended input: literal next, word erase, reprint, discard), the three a terminal does not usually have (raw_cr: a typed carriage return reaches the program as CR, not LF; raw_output: a written LF reaches the terminal as LF, not CR LF; strip8: input stripped to seven bits) and, when not canonical, how a read waits — min bytes, or time tenths of a second. Every other setting is left as found, so a mode read and set back restores exactly what was changed. Save it, change it, put it back (term.set_mode(tty, saved)); or, since wolf 0.2.34, register the put back with the runtime (restore_at_exit), which then makes it on every exit the runtime owns, a trap's included.
**flow and extended before wolf 0.2.30.** The runtime gained the two bits in wolf 0.2.30 (s234); from then a mode without them turns flow control and extended input off. Before it the runtime never reports them (mode answers both false) and refuses a mode that carries them (invalid), and a set leaves both as found. A mode read and set back is therefore exact on either side; a Mode built by hand says what it wants of both.
**raw_cr, raw_output and strip8 before wolf 0.2.34.** The runtime gained bits 5, 6 and 7 in wolf 0.2.34 (s248, ruling #97 R7), each a setting the usual terminal does NOT have, so a 0 in each is the usual setting: ICRNL on, OPOST on, ISTRIP off. From then a mode without them writes the usual setting back where the terminal had another (stty -opost); before it the runtime never reports them (mode answers all three false), refuses a mode that carries them (invalid) and leaves the three flags as found. A mode read and set back through mode, unpack and pack is exact on either side, and on a usual terminal every mode pack made before them packs the same int.
What a line editor asks of it (wolf 0.2.30, s234, ruling #73): the window's size (and signal.resize() when it changes) and the keys the user chose for erase, kill, interrupt and the rest (chars) — spellings ruled #82. The wait that a key or a signal ends is signal.wait_fd (ruling #82: the signal family's, and any descriptor serves), so an editor blocks there instead of in a read a signal cannot tear; since wolf 0.2.34 signal.wait_fds waits on the terminal, a job's pipes and the signals at once, with a deadline (the 25 ms that tells a lone ESC from an escape sequence). Nothing here sets the special characters, and nothing restores the mode when a program dies of a signal nobody listened for (no code of the process runs then, the runtime's included).
Errors are rows. A handle that is closed, forged or not a terminal is io; a mode outside the bits is invalid (unreachable through Mode, whose fields are clamped, but for flow or extended before wolf 0.2.30 and the three bits before 0.2.34); every call is unsupported on windows. lupin answers the shape rows and declines every call that reaches the terminal by name; from lupin 0.1.56 its shape takes bits 5–7 (0.1.55 refused them invalid, wolf-interp#268) and os_term_restore_at_exit answers its shape and io rows.
A terminal for a program this one runs (sc81, wolf 0.2.39's s292: [os.term.pty], [os.term.size.set], [os.term.attr]). open_pty makes a pseudo-terminal, a Pty: what the program on its slave writes is read from its master, and what is written to the master is the terminal's input, taken by its line discipline. pty_name is the slave's name, set_size sets the window's size with RFC 4254's pixel fields (the foreground group hears signal.resize() when any of the four changes), and attr and set_attr read and set one setting by its RFC 4254 opcode, as an SSH client sends them. A child takes the slave as its controlling terminal with open_controlling, after std.process.lead_session; Pty says the whole order, which is an ssh server's answer to a pty-req. The master is an fs.File: signal.wait_fds waits on it, fs.set_nonblock and fs.write_some write to it without blocking, and fs.read_chunk reads it.
Capability: foreground, mode, set_mode, restore_at_exit, size, chars, pty_name, set_size, attr and set_attr are io (they act on a descriptor the program holds, as io.is_terminal); set_foreground, open_pty (with process.pipe: a pty is made to run a program on) and open_controlling (it changes the process's terminal) are exec, and group env (process context). pack, unpack, raw, raw_bytes, chars_from and char_disabled are pure, comptime-safe.
Types and traits
struct · Source line 102
pub struct Mode { canonical: bool, echo: bool, signals: bool, flow: bool, extended: bool, raw_cr: bool, raw_output: bool, strip8: bool, min: int, time: int, }
The terminal's mode, unpacked ([os.term.mode]). raw_cr, raw_output and strip8 (wolf 0.2.34) are each false on a usual terminal: a typed Enter reads as LF, a written LF reaches the screen as CR LF, and input keeps its eighth bit.
struct · Source line 307
pub struct Size { rows: int, columns: int, }
The terminal's window size ([os.term.size]): rows lines of columns cells each.
struct · Source line 399
pub struct Pty { master: fs.File, slave: fs.File, }
A pseudo-terminal ([os.term.pty]): two std.fs files, the master, which the program that made it keeps, and a slave, the terminal another program runs on. Bytes written to the master are the terminal's input, taken by its line discipline (echo, editing, the signal characters); what the program writes to the slave is read from the master. Both are close-on-exec: neither reaches a child except through a descriptor map.
Who closes what, in the order a server follows (an ssh server's answer to a pty-req, which std.process.fork and the calls here make together):
1. let p = term.open_pty()? and let name = term.pty_name(p.master)?. 2. term.set_size(p.master, …) and term.set_attr(p.master, op, v) for each mode the client sent (an op the host lacks is unsupported; pass over it). 3. let f = process.fork()?. In the child: process.lead_session()?, then var tty = term.open_controlling(name)?, then the program with tty on 0, 1 and 2 (process.set_stdin, set_stdout, set_stderr) by process.exec. The child keeps the slave it inherited until its own open (macOS puts a terminal's size and settings back when its slave is opened while no descriptor of it is open); the exec closes the inherited master and slave, being close-on-exec. 4. In the parent: fs.close(take p.slave)? after the fork, then relay the master: signal.wait_fds waits on it, fs.set_nonblock with fs.write_some writes what it takes, fs.read_chunk reads what is there; set_size at each window change; fs.close(take p.master) at the end, which hangs the terminal up for the program.
The end, read on the master. Once every descriptor of the slave is closed (the program and everything it started have ended), a read of the master answers io on linux (the host's EIO, 5 in os_error()) and eof on macOS; either ends the relay. After the master's last close the slave's name is gone.
struct · Source line 563
pub struct Chars { erase: int, kill: int, interrupt: int, quit: int, suspend: int, end_of_file: int, word_erase: int, literal_next: int, start: int, stop: int, reprint: int, discard: int, }
The terminal's special characters ([os.term.chars]): the byte the user chose for each (stty erase ^H), or char_disabled() for one that is disabled (stty intr undef). word_erase, literal_next, reprint and discard are not POSIX's; both hosts have them, and they act only with Mode.extended on.
Functions and methods
fn · Source line 131
pub fn pack(m: Mode) -> int
The packed int the builtin reads: canonical=1, echo=2, signals=4, flow=8, extended=16, raw_cr=32, raw_output=64, strip8=128, min in bits 8..15, time in bits 16..23 ([os.term.mode]). pack(unpack(v)) == v for every mode the runtime reports, and a Mode whose three new fields are false packs the int it packed before wolf 0.2.34.
fn · Source line 161
pub fn unpack(v: int) -> Mode
The Mode a packed int names.
fn · Source line 177
pub fn mode(tty: fs.File) -> Mode ! {unsupported, io}
The terminal's mode now.
fn · Source line 183
pub fn set_mode(tty: fs.File, m: Mode) -> () ! {unsupported, invalid, io}
Set the terminal's mode (output already written drains first; input already typed is kept).
fn · Source line 197
pub fn raw(m: Mode) -> Mode
m with input a key at a time: not canonical, no echo, no flow control and no extended input, one byte per read, no timer, so Ctrl-S, Ctrl-Q, Ctrl-V and Ctrl-O arrive as keys, as cfmakeraw(3) leaves them. signals is kept, so Ctrl-C still interrupts (ruling #81: raw clears flow and extended). Before wolf 0.2.30 the runtime cannot clear the two, so there they stay as found. raw_cr, raw_output and strip8 are kept as m has them, so raw sets on every terminal what it set before wolf 0.2.34: Enter still reads as LF and a written LF still reaches the screen as CR LF on a usual one (raw_bytes turns both off).
fn · Source line 229
pub fn raw_bytes(m: Mode) -> Mode
raw(m) with every byte passed through untranslated, both ways: raw_cr and raw_output on and strip8 off, so a typed Enter reads as CR (13), not LF (10, Ctrl-J's byte), a written LF reaches the terminal alone, and input keeps its eighth bit. That is an editor's raw mode, as C yew sets it (cfmakeraw(3)'s set as far as a Mode reaches it; signals is kept, as raw keeps it, and an editor that wants Ctrl-C as a key clears it). A program in this mode writes CR LF where it means a new line. Pure and comptime-safe (sc69, ruling #97 R7: wolf 0.2.34's bits 5–7).
let cooked = term.unpack(1 + 2 + 4 + 8 + 16)
let r = term.raw_bytes(cooked)
r.raw_cr == true && r.raw_output == true && r.strip8 == false
term.pack(r) == 4 + 32 + 64 + 256
term.pack(term.raw(cooked)) == 4 + 256
fn · Source line 282
pub fn restore_at_exit(tty: fs.File, m: Mode, bytes: List[byte]) -> () ! { invalid, io, unsupported, }
**s248 ([os.term.restore], ruling #93 R1 C): put the terminal back on every exit the runtime owns.** Registers, for this process, a restore of tty: when the process ends — a return from main, process.exit, an error from main, a trap (after its report), the stack guard's report, process.exit_signal (before its raise), the death of the proc root — the runtime sets m exactly as set_mode sets it and then writes bytes to the terminal (an editor's alternate-screen leave and cursor show). The restore runs in the runtime, never in a handler of the program's. One registration a process: the last call wins, and each restore runs once. The runtime holds its own duplicate of the descriptor from the call, so a tty the program closes first still restores, and the bytes are copied at the call. Register the mode to come back to (the one mode read before the program changed it); registering first and changing the mode after is the usual order.
What it does not cover: a death by a signal nobody listened for — KILL, a TERM or HUP under the default, a fault outside the stack guard — runs no code of the process, so nothing is restored (listen for terminate(), reload() and quit() and end through exit); process.exec replaces the image and the registration with it; a child a process.fork makes restores nothing.
Rows: invalid for a mode outside the bits (unreachable through Mode); then unsupported on windows; then io for a handle that is closed, forged or not a terminal, or a duplicate the host will not make (ruling #97 R1). Never traps. The checked machine restores on its own end, whatever the verdict (R8); lupin 0.1.56 answers the shape and io rows and refuses a well-formed registration by name (0.1.55 did not resolve the call, wolf-interp#268).
Capability io (uncharged, as set_mode; ruling #97 R5); refused at comptime. Delegates to os_term_restore_at_exit. The example is prose, since a handle comes from std.fs: an editor reads saved = term.mode(tty)?, registers term.restore_at_exit(tty, saved, leave)? with the bytes that leave its alternate screen, then sets term.raw_bytes(saved); a trap no longer leaves the shell raw.
fn · Source line 291
pub fn foreground(tty: fs.File) -> int ! {unsupported, io}
The terminal's foreground process group.
fn · Source line 296
pub fn set_foreground(tty: fs.File, group_id: int) -> () ! {unsupported, io}
Make group_id the terminal's foreground process group.
fn · Source line 301
pub fn group() -> int ! {unsupported}
This process's own process group.
fn · Source line 327
pub fn size(tty: fs.File) -> Size ! {unsupported, io}
The window's size as the terminal holds it: what the emulator last told it, or what stty rows … cols … set (TIOCGWINSZ).
A size the terminal was never told is 0 (a pseudo-terminal nobody sized): fall back on COLUMNS or 80, as without the call. When the window changes, the foreground group hears signal.resize(), and the next call answers the new size.
Rows: io for a handle that is closed, forged or not a terminal; unsupported on windows. Never traps. lupin declines by name (it has no ioctl), so the example is prose: term.size(tty)?.columns is the width an editor lays a line out in.
Capability io (uncharged, as mode). Delegates to os_term_size. Spelling ruled #82 (Size's named fields over the builtin's pair).
fn · Source line 356
pub fn set_size(tty: fs.File, s: Size, xpixels: int, ypixels: int) -> () ! { invalid, io, unsupported, }
**s292 ([os.term.size.set], sc81): set the window's size** of the terminal tty names, as an emulator or stty rows … cols … does: s is the rows and the columns size reads back, on either end of a pseudo-terminal, and xpixels and ypixels the window's width and height in pixels, RFC 4254's two other fields (0 when the client does not know). The pixels are written at every call, so a server cannot drop what a client sent: a change of the pixels alone signals too.
When any of the four changes, the host sends signal.resize() to the terminal's foreground group (the program on a pty's slave), and the same four again send nothing. An ssh server calls this on the master for the pty-req and for each window-change.
Rows, in the order they are decided: invalid for any of the four outside 0..65535, before the handle is read; unsupported on windows; io for a handle that is closed, forged or not a terminal. Never traps. lupin 0.1.61 answers the shape and handle rows and refuses a set on a terminal by name.
Capability io (uncharged, as set_mode); refused at comptime. Delegates to os_term_set_size. The example is prose, since a handle comes from std.fs: a window-change of 120 columns, 40 rows, 960 by 640 pixels is term.set_size(p.master, term.Size { rows: 40, columns: 120 }, 960, 640)?, and term.size(p.slave)?.columns is 120 after.
fn · Source line 418
pub fn open_pty() -> Pty ! {io, unsupported}
**s292 ([os.term.pty], sc81): a new pseudo-terminal**, its master and a slave, both close-on-exec; the slave granted and unlocked, and neither end this process's controlling terminal, whatever it leads.
Rows: unsupported on windows, by name; io for a host that cannot make one (no /dev/ptmx, none free, the descriptor limit), with the host's number in os_error(), and nothing left open. Never traps. The checked machine opens the pair in the wolf process. lupin 0.1.61 refuses the call by name (Rust's standard library has no posix_openpt), so the example is prose: let p = term.open_pty()?, then term.pty_name(p.master)? is /dev/pts/N on linux.
Capability exec (with process.pipe: a pty is made to run a program on); refused at comptime. Delegates to os_term_open_pty.
fn · Source line 437
pub fn pty_name(master: fs.File) -> str ! {io, unsupported}
**s292 ([os.term.pty], sc81): the slave's name** for the master master names: /dev/pts/N on linux and PAX, /dev/ttysN on macOS. A child opens it by this name to take it as its controlling terminal (open_controlling).
Rows: unsupported on windows; io for a handle that is closed, forged or not a master (a file, a slave, another terminal). Never traps. lupin 0.1.61 answers a closed or forged handle's io and refuses the rest by name.
Capability io (uncharged, with mode); refused at comptime. Delegates to os_term_pty_name. The example is prose, since a handle comes from std.fs or open_pty: term.pty_name(p.master)? names the slave, and term.pty_name(p.slave) is io.
fn · Source line 464
pub fn attr(tty: fs.File, op: int) -> int ! {invalid, io, unsupported}
**s292 ([os.term.attr], sc81): one setting of the terminal tty names, by its RFC 4254 section 8 opcode** (with RFC 8160's 42, IUTF8): a special character, 1 to 18, as its byte or -1 for disabled (as chars reads it); a flag (30 to 42 input, 50 to 62 local, 70 to 75 output, 92 and 93 control) as 0 or 1; the character size, 90 CS7 and 91 CS8, 1 for the size the terminal has; the speeds, 128 input and 129 output, in bits per second. The opcode is the published number an SSH client sends, and it is the vocabulary here.
Rows, in the order they are decided: invalid for an opcode outside the vocabulary (0, 19..29, 43..49, 63..69, 76..89, 94..127, 130 and up, a negative); unsupported on windows, for an op the host's terminal driver lacks (linux 11, 15 and 17; macOS 15, 16, 37, 52 and 71) and for a speed the runtime's table does not name; io for a handle that is closed, forged or not a terminal. Never traps. lupin 0.1.61 answers every row decided before a terminal is reached and refuses the rest by name.
Capability io (uncharged, as mode); refused at comptime. Delegates to os_term_attr. The example is prose, since a handle comes from std.fs or open_pty: term.attr(p.slave, 53)? is 1 while the terminal echoes, and term.attr(p.slave, 19) is invalid on every host.
fn · Source line 495
pub fn set_attr(tty: fs.File, op: int, value: int) -> () ! {invalid, io, unsupported}
**s292 ([os.term.attr], sc81): set one setting of the terminal tty names, by its RFC 4254 opcode**, every other field as found (output already written drains first, as set_mode). value is in the op's range (attr names them): a character -1..255 (a byte equal to the host's disabled value disables it too), a flag 0 or 1, a size 0 or 1 (1 selects it; 0 leaves the size as found, since a client sends both), a speed in bits per second from the runtime's table (0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400). The setting is read back after the set, so an answer of () means the terminal holds what was asked.
Rows, in the order they are decided: invalid for an opcode outside the vocabulary or a value outside its op's range; unsupported on windows, for an op the host lacks, for a speed not in the table, and for a set the host did not keep (linux's pseudo-terminal keeps neither CS7 nor PARENB; macOS keeps a set PENDIN), with every other field as it was; io for a handle that is closed, forged or not a terminal, and for any other refusal of the host's. Never traps. A server applying a client's modes passes over unsupported, as sshd does. lupin 0.1.61 answers every row decided before a terminal is reached and refuses the rest by name.
Capability io (uncharged, as set_mode); refused at comptime. Delegates to os_term_set_attr. The example is prose, since a handle comes from std.fs or open_pty: a client's ECHO 0 is term.set_attr(p.master, 53, 0)?, and term.set_attr(p.master, 53, 2) is invalid on every host.
fn · Source line 533
pub fn open_controlling(name: str) -> fs.File ! {denied, io, not_found, unsupported}
**sc81: open the terminal name as THIS process's controlling terminal** — the step a pty's program takes in its own session, after std.process.lead_session, so that the terminal's interrupt, quit and suspend keys, its hangup and signal.resize() reach it and /dev/tty opens ([os.term.pty], [os.term.session]). The answer is the terminal, open for reading and writing, to be placed on 0, 1 and 2 for the program's exec; it is this process's controlling terminal, with this process's group its foreground.
It is the open the runtime makes without O_NOCTTY: a session leader with no controlling terminal that opens a terminal no session holds takes it. The call checks that it did, because nothing else would say so: an open made before lead_session, or from a process that leads none, or of a terminal another session holds, gives a terminal that is not the controlling one, and a program on it gets no job control and no keys' signals.
Rows, in the order they are decided: unsupported on windows, before the name is looked at; not_found for a name that does not exist; io for one that is not a character device, without opening it (so nothing is created); denied for an open the host refuses; io for one that is not a terminal, or did not become this process's controlling terminal with its group in the foreground — the file is closed then, and the terminal is not held. Never traps. The checked machine opens in the wolf process. lupin 0.1.61 refuses the call by name (at os_pgid).
Capability exec (it changes the process's terminal, as set_foreground); refused at comptime. Delegates to fs_stat, fs_open_mode (mode 3), os_isatty, os_term_foreground and os_pgid. The example is prose, since the call that succeeds takes a terminal: in a fork's child, process.lead_session()?, then var tty = term.open_controlling(name)? and term.foreground(tty)? == term.group()?.
fn · Source line 585
pub fn char_disabled() -> int
The word Chars holds for a character that is disabled: -1, the builtin's own ([os.term.chars]; the host's disabled value never reaches the program). Total and pure.
term.char_disabled() == 0 - 1
fn · Source line 614
pub fn chars_from(v: List[int]) -> Chars
The Chars a list in [os.term.chars]'s order names: erase, kill, interrupt, quit, suspend, end of file, word erase, literal next, start, stop, reprint, discard. An entry the list does not reach is char_disabled(); entries past the twelfth are not read (the builtin's list grows only at its end). Total and pure.
var v = List[int]()
(mut v).push(127)
(mut v).push(21)
(mut v).push(3)
let c = term.chars_from(v)
c.erase == 127 && c.interrupt == 3 && c.discard == term.char_disabled()
fn · Source line 644
pub fn chars(tty: fs.File) -> Chars ! {unsupported, io}
The terminal's special characters now. A line editor that turns canonical and signals off reads these first, so erase, kill, interrupt and end of file keep the keys the user set. Nothing here sets them (stty does).
Rows: io for a handle that is closed, forged or not a terminal; unsupported on windows. Never traps. lupin declines by name (it has no tcgetattr); the example is prose: term.chars(tty)?.erase is the key that deletes a character.
Capability io (uncharged, as mode). Delegates to os_term_chars. Spelling ruled #82 (Chars' named fields, with chars_from and char_disabled).