Trame

pty: a child on a pseudo-terminal

#import "trame:pty" (or the relative path of the submodule). Linux and Windows; elsewhere start answers io.Error.Unsupported. The design is in src/md/features/local-terminal.md.

A child on a pseudo-terminal believes it writes to a terminal: it prints colours, edits its line, answers isatty, and knows the terminal's size. A pipe gives none of this: a compiler and a shell then write as to a file. The child runs as a process tree (process.tree_start_terminal, process.md): it ends with stop, with its own end (what it left running is killed), and with the program however the program ends.

Formula
p, err := pty.start({command = {"bash", "-l"}, dir = folder, env = env, cols = 100, rows = 30})   // on a worker
// the reader's own thread:
buf: [4096]u8
for {
	n, rerr := pty.read(p, buf[:])
	if n <= 0 || rerr != nil { break }   // the child's end, or stop's
	feed(buf[:n])                        // post it to the interface's thread
}
// a worker (the keys' queue, the size once the grid rests):
pty.write(p, transmute([]u8)string("ls\r"))
pty.resize(p, 120, 40)
// the end:
pty.stop(p)                              // ends the tree, wakes the read
exited, code := pty.wait(p, 0)           // the code: its exit code, or 128 + the signal
pty.destroy(p)                           // after the reader returned
Procedure
start(Start_Options) -> (^Pty, Error)starts command on a new pseudo-terminal of cols by rows. A bare name is found in the absolute folders of PATH, as process.start finds it (os.General_Error.Not_Exist otherwise). On an error nothing is left open
read(p, buf) -> (n, Error)what the child wrote; waits until there is some. 0 with no error: the child's terminal closed (the child ended) or stop was called
write(p, data) -> (n, Error)bytes for the child, as typed; all of them unless an error. Waits while the terminal's input is full. Broken_Pipe once the child is gone
resize(p, cols, rows) -> Errorthe new size in cells: Linux sends SIGWINCH to the child's foreground group, Windows a window size event
stop(p)ends the child's tree, wakes a waiting read, waits STOP_BOUND at most for the end. Safe twice
wait(p, timeout_ms) -> (exited, code)whether the child ended within the limit (0: only asks; negative: no limit); code is its exit code, or 128 + the signal that ended it
destroy(p)stop, then releases the terminal and the tree and frees p
Start_Options
commandthe program and its arguments
dirits working folder ("": this process's)
envits whole environment, NAME=value (nil: this process's). TERM is the caller's to set: the package adds nothing
cols, rowsthe terminal's size in cells (clamped to at least 1)

The rules

  • Every call blocks (base.blocking), resize included: the interface's thread never calls them. Start and stop run on a chore; read runs on a thread of its own; write can wait for a child that does not read, so it runs on a worker too (a queue of keys that the same thread writes).
  • What runs beside what: read, write and resize may run beside each other and beside stop and wait. destroy runs alone, once, after the reader returned: a read still running would read a closed descriptor.
  • Linux: the secondary end of /dev/ptmx (opened by TIOCGPTPEER) is the child's three ends and its controlling terminal (setsid, TIOCSCTTY), then closes here. The child leads a session and a group of its own; the tree's keeper kills the group when the child ends, at stop, and when the program ends.
  • Windows: a pseudo console (CreatePseudoConsole) between two pipes, given to the child through its attribute list; the child is in a Job Object that ends with the program. The console's host keeps the output open after the child ends, so a wait on the child closes the console then: read sees the end, as on Linux. Before Windows 11 24H2, closing a console waits until its output is read: keep the reader reading until it returns.

Edit this page on GitHub