beetlebug▾
/

ztcl::pty spawn

Starts a program on a new pseudo terminal and gives a channel.

ztcl::pty spawn ?-isolation none|vm? ?-size {COLS ROWS}? ?-cwd DIR? ?-env {NAME VALUE ...}? ?VM OPTION ...? ?--? PROGRAM ?ARG ...?

Description

Starts PROGRAM with its arguments on a new PTY and gives a channel such as pty3. The channel is readable and writable, binary, and has no buffering, and fileevent and fconfigure -blocking 0 work on it as on a pipe. The program runs directly from its argument list, with no shell. Its environment is exec_host.env, then the pairs of -env, then TERM=xterm-256color when no entry sets TERM.

Arguments

ArgumentTypeDefaultDescription
PROGRAMprogram name or pathrequiredThe program to run.
ARGstringnoneAn argument of the program.

Options

OptionTypeDefaultDescription
-sizelist of two integers80 24Gives the size of the terminal.
-cwdpaththe current folder of fileGives the working folder of the program.
-envlist of names and valuesnoneAdds variables to the environment of the program.
-isolationnone or vmnoneRuns the program on the host, or on a PTY of a new guest of ztcl::vm.
--flagnoneEnds the options.

-size {COLS ROWS}

Gives the size of the terminal.

The count of columns and of rows, each 1 to 10,000 on Linux and macOS, and 1 to 32,767 on Windows. On macOS spawn sets the size on the other side of the terminal, since macOS gives EIO to a size on the master side before the other side has an open.

% set t [ztcl::pty spawn -size {100 30} $sh]
> set r [ztcl::pty size $t]
> close $t
> set r
100 30
% ztcl::pty spawn -size 5 x
bad size "5": must be {cols rows}
-errorcode NONE

% ztcl::pty spawn -size {a b} x
expected integer but got "a"
-errorcode TCL VALUE NUMBER

% ztcl::pty spawn -size
value for "-size" missing
-errorcode NONE

-cwd DIR

Gives the working folder of the program.

A folder of file, so it needs the capability file. The check is of the file system of the interpreter, so in host mode -cwd also works with no capability file (issue 729).

% set t [ztcl::pty spawn -cwd . $sh]
> puts -nonewline $t "exit 0\r"
> set s [finish $t]
> close $t
> set s
exit 0
% ztcl::pty spawn -cwd nosuchdir $sh
couldn't open the working folder: no such file or directory
-errorcode POSIX ENOENT {no such file or directory}

-env {NAME VALUE ...}

Adds variables to the environment of the program.

Pairs added to the environment of exec_host.env.

% set t [ztcl::pty spawn -env {CODE 5} $sh]
> puts -nonewline $t [expr {$sh eq "cmd.exe" ? "exit %CODE%\r" : "exit \$CODE\r"}]
> set s [finish $t]
> close $t
> set s
exit 5
% ztcl::pty spawn -env x x
-env needs a list of names and values
-errorcode NONE

% ztcl::pty spawn -env {= 1} x
bad environment name "="
-errorcode NONE

-isolation LEVEL

Runs the program on the host, or on a PTY of a new guest of ztcl::vm.

With vm, the program runs on a PTY of a new guest of ztcl::vm, which goes away at close. It needs the capability vm, and x86-64 Linux with KVM or x86-64 Windows with the Windows Hypervisor Platform. The options -image, -snapshot, -memory, -cpus, -net, -allow, -deny, -dns, -share, -kernel, -initrd, and -cmdline go to ztcl::vm create, and the default is -image ztcl-guest. Each -share {HOST GUEST ?rw?} goes at its folder GUEST of the guest. PROGRAM is a program of the guest, -cwd is a folder of the guest, and the environment is only the pairs of -env and TERM. resize, size, status, kill, and close work as for a PTY of the host. One of the vm options with no -isolation vm gives option "X" needs -isolation vm, and with no capability vm the error is ztcl::pty spawn -isolation vm needs the vm capability (ZTCL EXEC NOVM). The example runs on the host.

% set t [ztcl::pty spawn -isolation none $sh]
> set r [ztcl::pty status $t]
> close $t
> set r
running
% ztcl::pty spawn -isolation box $sh
bad isolation "box": must be none or vm
-errorcode TCL LOOKUP INDEX isolation

--

Ends the options.

The next argument is PROGRAM, also when it starts with -.

% set t [ztcl::pty spawn -- $sh]
> close $t
> string match pty* $t
1

A flag has no value to check.

Returns

The name of the channel.

Examples

Each example of this page runs after # The shell of the platform: in host mode the separator of Windows is a backslash. set sh [expr {[file separator] eq "/" ? "/bin/sh" : "cmd.exe"}] # Reads the output with no block until the program ends, and gives its status. proc finish {t} { fconfigure $t -blocking 0 for {set n 0} {[ztcl::pty status $t] eq "running" && $n < 1000} {incr n} { read $t; after 10 } ztcl::pty status $t } .

% set t [ztcl::pty spawn $sh]
> set r [fconfigure $t -translation]
> close $t
> set r
binary binary

% set t [ztcl::pty spawn -- $sh]
> set r [ztcl::pty status $t]
> close $t
> set r
running

Errors

% ztcl::pty spawn nosuchprogram_zz
couldn't execute "nosuchprogram_zz": no such file or directory
-errorcode POSIX ENOENT {no such file or directory}

% ztcl::pty spawn -bogus 1 x
bad option "-bogus": must be -cwd, -env, -isolation, or -size
-errorcode NONE

% ztcl::pty spawn
wrong # args: should be "ztcl::pty spawn ?-size {cols rows}? ?-cwd dir? ?-env list? program ?arg ...?"
-errorcode TCL WRONGARGS

Facts

Capability
exec. A new interpreter does not have it until the host turns it on.
Tcl release
Tcl 9.1 and 8.6.
Since
ztcl 1.0.0-beta.1

See also