cmd \- interface to host operating system commands
.SH SYNOPSIS
.B bind -a '#C' /
.PP
.B /cmd/clone
.br
.BI /cmd/ n /ctl
.br
.BI /cmd/ n /data
.br
.BI /cmd/ n /stderr
.br
.BI /cmd/ n /status
.br
.BI /cmd/ n /wait
.SH DESCRIPTION
.I Cmd
provides a way to run commands in the underlying operating system's
command interpreter of drawterm or when Inferno is running hosted.
It serves a three-level directory that is conventionally bound
behind the root directory.
The top of the hierarchy is a directory
.BR cmd ,
that contains a
.B clone
file and zero or more numbered directories.
Each directory represents a distinct connection to the host's command interpreter.
The directory contains five files:
.BR ctl ,
.BR data ,
.BR stderr ,
.B status
and
.BR wait ,
used as described below.
Opening the
.B clone
file reserves a connection: it is equivalent to opening the
.B ctl
file of an unused connection directory, creating a new one if necessary.
.PP
The file
.B ctl
controls a connection.
When read, it returns the decimal number
.I n
of its connection directory.
Thus, opening and reading
.B clone
allocates a connection directory and reveals the number of the allocated directory,
allowing the other files to be named (eg,
.BI /cmd/ n /data\fR).
.PP
.B Ctl
accepts the following textual commands, allowing quoting as interpreted by
.IR parsecmd (2):
.TP
.BI "dir " wdir
Run the host command in directory
.IR wdir ,
which is a directory
.I "on the host system" .
Issue this request before starting the command.
By default, commands are run in the Inferno root directory on the host system.
.TP
.BI "exec " "command args ..."
Spawn a host process to run the
.I command
with arguments as given.
The write returns with an error, setting the error string, if anything prevents
starting the command.
If write returns successfully, the command has started, and its standard input and
output may be accessed through
.BR data ,
and its error output accessed through
.B stderr
(see below).
If arguments containing white space are quoted (following the conventions of
.IR rc (1)
or
.IR parsecmd (2)),
they are requoted by
.I cmd
using the host command interpreter's conventions so that
.I command
sees exactly the same arguments as were written to
.BR ctl .
.TP
.B kill
Kill the host command immediately.
.TP
.B killonclose
Set the device to kill the host command when the
.B ctl
file is closed (normally all files must be closed, see below).
.TP
.BI nice " \fR[\fPn\fR]\fP"
Run the host command at less than normal scheduling priority.
Issue this request before starting the command.
The optional value
.IR n ,
in the range 1 to 3,
indicates the degree of `niceness' (default: 1).
.PP
The
.B data
file provides a connection to the input and output of a previously-started
host command.
It must be opened separately for reading and for writing.
When opened for reading, it returns data that the command writes to its standard output; when closed, further writes by the command will receive the host
equivalent of `write to closed pipe'.
When opened for writing, data written to the file
can be read by the command on its standard input; when closed, further reads by
the command will see the host equivalent of `end of file'.
(Unfortunately there is no way to know when the command needs input.)
.PP
The
.B stderr
file provides a similar read-only connection to the error output from the command.
If the
.B stderr
file is not opened, the error output will be discarded.
.PP
Once started, a host command runs until it terminates or until it is killed,
by using the
.B kill
or
.B killonclose
requests above, or by closing all
.BR ctl ,
.B data
and
.B wait
files for a connection.
.PP
The read-only
.B status
file provides a single line giving the status of the connection (not the command), of the form:
.IP
.BI cmd/ "n opens state wdir arg0"
.PP
where the fields are separated by white space. The meaning of each field is: