Module Os.Cmd
Executing commands.
Tool search
Portability. In order to maximize portability no .exe
suffix should be added to executable names on Windows, tool search adds the suffix during the tool search procedure.
val find_tool : ?search:Fpath.t list -> Cmd.tool -> (Fpath.t option, string) Stdlib.result
find_tool ~search tool
is the file path, if any, to the program executable for the tool specificationtool
.- If
tool
has a single path segment. Thetool
file is searched, in list order, for the first matching executable file in the directories ofsearch
. These directories default to those that result from parsingPATH
withFpath.list_of_search_path
. - If
tool
has multiple path segments the corresponding file is simply tested for existence and executability.Ok (Some tool)
is returned if that is case andOk None
otherwise.
- If
val must_find_tool : ?search:Fpath.t list -> Cmd.tool -> (Fpath.t, string) Stdlib.result
must_find_tool
is likefind_tool
except it errors ifOk None
is returned.
val find_first_tool : ?search:Fpath.t list -> Cmd.tool list -> (Fpath.t option, string) Stdlib.result
find_first_tool
is the first tool that can be found in the list withfind_tool
.
val find : ?search:Fpath.t list -> Cmd.t -> (Cmd.t option, string) Stdlib.result
find ~search cmd
resolvescmd
's tool asfind_tool
does.
val must_find : ?search:Fpath.t list -> Cmd.t -> (Cmd.t, string) Stdlib.result
must_find ~search cmd
resolvescmd
's tool asmust_find_tool
does.
val find_first : ?search:Fpath.t list -> Cmd.t list -> (Cmd.t option, string) Stdlib.result
find_first ~search cmds
resolvescmds
'sCmd
.toos asfind_first_tool
does.
Process completion statuses
Process standard inputs
val in_string : string -> stdi
in_string s
is a standard input that reads the strings
.
val in_fd : close:bool -> Unix.file_descr -> stdi
in_fd ~close fd
is a standard input that reads from file descriptorfd
. Ifclose
istrue
,fd
is closed after the process is spawn.
val in_stdin : stdi
in_stdin
isin_fd ~close:false Unix.stdin
, a standard input that reads from the current process standard input.
val in_null : stdi
in_null
isin_file File.null
.
Process standard outputs
val out_fd : close:bool -> Unix.file_descr -> stdo
out_fd ~close fd
is a standard output that writes to file descriptorfd
. Ifclose
istrue
,fd
is closed after the process spawn.
val out_stdout : stdo
out_stdout
isout_fd ~close:false Unix.stdout
val out_stderr : stdo
out_stderr
isout_fd ~close:false Unix.stderr
val out_null : stdo
out_null
isout_file File.null
Command execution
Blocking
These functions wait for the command to complete before proceeding.
val run_status : ?env:Env.assignments -> ?cwd:Fpath.t -> ?stdin:stdi -> ?stdout:stdo -> ?stderr:stdo -> Cmd.t -> (status, string) Stdlib.result
run_status ~env ~cwd ~stdin ~stdout ~stderr cmd
runs and waits for the completion ofcmd
in environmentenv
with current directorycwd
and standard IO connectionsstdin
,stdout
andstderr
.env
defaults toEnv.current_assignments
()
cwd
defaults to Current working directory (cwd)()
stdin
defaults toin_stdin
stdout
defaults toout_stdout
stderr
defaults toout_stderr
.
val run_status_out : ?env:Env.assignments -> ?cwd:Fpath.t -> ?stdin:stdi -> ?stderr:[ `Stdo of stdo | `Out ] -> ?trim:bool -> Cmd.t -> (status * string, string) Stdlib.result
run_status_out
is likerun_status
exceptstdout
is read from the process to a string. The string isString.trim
ed iftrim
istrue
(default). Ifstderr
is`Out
the process'stderr
is redirected tostdout
and thus read back in the string aswell.
val run : ?env:Env.assignments -> ?cwd:Fpath.t -> ?stdin:stdi -> ?stdout:stdo -> ?stderr:stdo -> Cmd.t -> (unit, string) Stdlib.result
run
isrun_status
with non-`Exited 0
statuses turned into errors viapp_cmd_status
.
val run_out : ?env:Env.assignments -> ?cwd:Fpath.t -> ?stdin:stdi -> ?stderr:[ `Stdo of stdo | `Out ] -> ?trim:bool -> Cmd.t -> (string, string) Stdlib.result
run
isrun_status_out
with non-`Exited 0
statuses turned into errors viapp_cmd_status
.
Non-blocking
Note. In contrast to waitpid(2)
the following API does not allow to collect any child process completion. There are two reasons: first this is not supported on Windows, second this is anti-modular.
val pid_to_int : pid -> int
pid_to_int pid
is the system identifier for process identifierpid
.
val spawn : ?env:Env.assignments -> ?cwd:Fpath.t -> ?stdin:stdi -> ?stdout:stdo -> ?stderr:stdo -> Cmd.t -> (pid, string) Stdlib.result
spawn ~env ~cwd ~stdin ~stdout ~stderr cmd
spawns commandcmd
in environmentenv
with current directorycwd
and standard IO connectionsstdin
,stdout
andstderr
.env
defaults toEnv.current_assignments
()
,cwd
toDir
.current()
,stdin
toin_stdin
,stdout
toout_stdout
andstderr
toout_stderr
.
val spawn_poll_status : pid -> (status option, string) Stdlib.result
spawn_poll_status pid
tries to collect the exit status of command spawnpid
. Ifblock
isfalse
,Ok None
is immediately returned ifpid
has not terinated yet.
val spawn_wait_status : pid -> (status, string) Stdlib.result
spawn_wait_status
blocks and waits forpid
's termination status to become available.
Tracing
type spawn_tracer
= pid -> Env.assignments option -> cwd:Fpath.t option -> Cmd.t -> unit
The type for spawn tracers. Called with each blocking and non-blocking spawned command. The function is given the process identifier of the spawn, the environment if different from the program's one, the current working directory if different from the program's one and the acctual command.
val spawn_tracer_nop : spawn_tracer
spawn_tracer_nop
is a spawn tracer that does nothing. This is the initial spawn tracer.
val spawn_tracer : unit -> spawn_tracer
tracer ()
is the current spawn tracer. Initially this isspawn_tracer_nop
.
val set_spawn_tracer : spawn_tracer -> unit
set_tracer t
sets the current spawn tracer tot
.
Executing files
Windows. On Windows a program executing an execv*
function yields back control to the terminal as soon as the child starts (vs. ends on POSIX). This entails all sorts of unwanted behaviours. To workaround this, the following function executes, on Windows, the file as a spawned child process which is waited on for completion via waitpid(2)
. Once the child process has terminated the calling process is immediately exit
ed with the status of the child.
val execv : ?env:Env.assignments -> ?cwd:Fpath.t -> Fpath.t -> Cmd.t -> (unit, string) Stdlib.result
execv ~env ~cwd f argv
executes filef
as a new process in environmentenv
withargs
as theSys
.argv of this process (in particularSys.argv.(0)
is the name of the program not the first argument to the program). The function only recturns in case of error.env
defaults toB0
.OS.Env.current_assignments()
,cwd
toDir
.current()
.