package matrix
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=9e4e90d17f9b2af1b07071fe425bc2c519c849c4f1d1ab73cde512be2d874849
sha512=06e9c4a741590942e81a27738d0b5c0413fafec8cf3b7dae047ad69f155e7b718aa4223818dc161b7d028efffcfd3365905e264d6fd31d453910ddfa91dcf9b9
doc/matrix.pty/Pty/index.html
Module PtySource
Pseudo-terminals.
This module creates and manages pseudo-terminals (PTYs) for terminal emulation. A PTY is a bidirectional channel with a master side (used by the terminal emulator to send input and receive output) and a slave side (connected to a child process as its controlling terminal).
Warning. spawn and with_spawn use Unix.fork and are only available on POSIX systems (Linux, macOS, BSD). open_pty works on Windows 10 1809+ via ConPTY with limitations.
Creating
The simplest way to run a program in a PTY is with_spawn:
Pty.with_spawn ~prog:"/bin/echo" ~args:[ "hello" ] (fun pty ->
let buf = Bytes.create 1024 in
let n = Pty.read pty buf 0 1024 in
Bytes.sub_string buf 0 n)For manual lifetime control use spawn and close:
let pty = Pty.spawn ~prog:"/bin/bash" ~args:[] () in
(* … interact … *)
Pty.close ptyI/O
read and write are blocking by default. Enable non-blocking mode with set_nonblock; I/O then raises Unix.Unix_error (EAGAIN, _, _) instead of blocking.
The file descriptor returned by file_descr works with Unix.select, Unix.poll, and async wrappers such as Lwt_unix.of_unix_file_descr.
Window size
set_winsize (or the convenience resize) updates the PTY dimensions and delivers SIGWINCH to the child process group on POSIX systems. inherit_size copies the size from one PTY to another, useful for forwarding resize events.
Platform notes
POSIX (Linux, macOS, BSD)
Full support via posix_openpt, grantpt, unlockpt. File descriptors are real Unix file descriptors; Unix.select and Unix.poll work as expected. SIGWINCH is delivered to child processes on resize.
Windows (ConPTY)
Supported on Windows 10 version 1809 and later. Current limitations:
spawnis not available (requiresCreateProcessintegration).get_winsizereturns the last size set viaset_winsizeor the initial default, as ConPTY has no query API.- I/O uses Windows named pipes wrapped as Unix file descriptors.
Thread safety
This module is not thread-safe. The t type contains mutable state that must not be accessed concurrently without external synchronization. However, C stubs release the OCaml runtime lock during blocking I/O so other OCaml threads can run concurrently.
Resource management
Each t owns a file descriptor and possibly a child process. Always call close or use with_pty/with_spawn to prevent resource leaks and zombie processes. close is idempotent.
Types
type winsize = {rows : int;(*Row count (lines).
*)cols : int;(*Column count (characters per line).
*)xpixel : int;(*Width in pixels. Often unused; set to
*)0.ypixel : int;(*Height in pixels. Often unused; set to
*)0.
}The type for terminal window sizes. All fields are non-negative.
Creating
open_pty () is (master, slave), a fresh PTY pair.
Both handles must be closed with close. The slave should be connected to a child process via Unix.dup2.
winsize sets the initial terminal size. Omit to use the system default.
Raises Unix.Unix_error if PTY creation fails.
val spawn :
?env:string array ->
?cwd:string ->
?winsize:winsize ->
prog:string ->
args:string list ->
unit ->
tspawn ~prog ~args () is the master handle of a new PTY with prog running in it.
Creates a PTY pair, forks, configures the child with the slave as its controlling terminal (setsid + TIOCSCTTY), redirects the child's stdin/stdout/stderr to the slave, and executes prog. The slave is closed in the parent. The child's PID is available via pid.
Optional arguments:
envis the child environment as"KEY=value"strings. Defaults to the parent environment.cwdis the child working directory. Defaults to the parent's.winsizeis the initial terminal size. Defaults to the system default.
args are the command-line arguments excluding argv[0] which is set to prog. If prog is relative it is searched in PATH.
Raises Unix.Unix_error if PTY creation or fork fails. Exec failures in the child are not reported as exceptions; the child exits with the errno value as its exit code. Monitor via Unix.waitpid on pid.
Note. The child receives SIGHUP when the master is closed.
with_pty f is f master slave on a fresh PTY pair. Both handles are closed via Fun.protect when f returns or raises.
winsize sets the initial terminal size. Raises Unix.Unix_error if PTY creation fails.
Process management
close pty closes the file descriptor and, for spawn PTYs, terminates and reaps the child process:
- Sends
SIGTERM. - If
waitistrue(the default), sleeps 100ms, checkswaitpid WNOHANG, and escalates toSIGKILLif the child is still running. - Reaps with
waitpidto prevent zombies.
wait defaults to true. Set to false for non-blocking close; you must then call Unix.waitpid yourself.
Idempotent: subsequent calls are no-ops. Never raises. After close, I/O operations raise Unix.Unix_error (EBADF, _, _).
terminate pty sends SIGTERM to the child without closing the PTY. Unix errors from kill are silently ignored.
Raises Invalid_argument if pty has no child (created via open_pty or already closed).
kill pty sends SIGKILL to the child. Unlike terminate this signal cannot be caught. Unix errors from kill are silently ignored.
Raises Invalid_argument if pty has no child.
File descriptors
file_descr pty is the underlying Unix file descriptor. Invalid after close.
in_fd pty is file_descr pty. PTYs are bidirectional so the read and write descriptors are the same.
out_fd pty is file_descr pty.
Window size
get_winsize pty is the current terminal size.
On Windows (ConPTY) returns the last size set via set_winsize or the initial default, as ConPTY has no query API.
Raises Unix.Unix_error on failure.
set_winsize pty ws sets the terminal size to ws and delivers SIGWINCH to the child process group on POSIX.
Raises Unix.Unix_error on failure.
resize pty ~rows ~cols is set_winsize pty { rows; cols; xpixel = 0; ypixel = 0 }.
inherit_size ~src ~dst copies the window size from src to dst. Equivalent to set_winsize dst (get_winsize src).
Raises Unix.Unix_error if reading src or writing dst fails.
I/O
read pty buf off len reads up to len bytes into buf starting at off. Returns the number of bytes read, or 0 on EOF (child exited).
Blocks unless non-blocking mode is enabled via set_nonblock, in which case raises Unix.Unix_error (EAGAIN, _, _) when no data is ready.
Raises Unix.Unix_error on error.
write pty buf off len writes up to len bytes from buf starting at off. Returns the number of bytes written.
Blocks unless non-blocking mode is enabled.
Raises Unix.Unix_error on error (e.g. EPIPE if the child closed its end).
write_string pty s off len is like write but reads from a string.
Non-blocking mode
set_nonblock pty enables non-blocking I/O. read and write raise Unix.Unix_error (EAGAIN, _, _) instead of blocking. Persists until clear_nonblock or close.
Raises Unix.Unix_error on failure.