package b0
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha512=00a6868b4dfa34565d0141b335622a81a0e8d5b9e3c6dfad025dabfa3df2db2a1302b492953bbbce30c3a4406c324fcec25250a00b38f6d18a69e15605e3b07e
doc/b0.std/B0_std/Os/Cmd/index.html
Module Os.CmdSource
Executing commands.
Tool search
val find_tool :
?win_exe:bool ->
?search:Fpath.t list ->
Cmd.tool ->
(Fpath.t option, string) resultfind_tool ~win_exe ~search tool is the file path, if any, to the program executable for the tool specification tool. For portability do not add an .exe suffix to tool on Windows, see the win_exe argument.
- If
toolhas a single path segment: the filenametoolis searched, in list order, for the first matching executable file in the directories ofsearch.searchdefaults to the env varPATHparsed withFpath.list_of_search_path. - If
toolhas multiple path segments: the file pathtoolis simply tested for existence and executability.Ok (Some tool)is returned if that is case andOk Noneotherwise. - If
win_exeistruea.exesuffix is added totoolif it doesn't already have one. Defaults toStdlib.Sys.win32.
get_tool is like find_tool except it errors if Ok None is returned.
val get_first_tool :
?win_exe:bool ->
?search:Fpath.t list ->
Cmd.tool list ->
(Fpath.t, string) resultget_first_tool tools is the first tool that can be found in the list with find_tool or an error if none is found. tools must be non-empty.
find cmd is like find_tool but looks and replaces cmd's B0_std.Cmd.tool.
get cmd is like get_tool but looks and replaces cmd's B0_std.Cmd.tool.
Process completion statuses
The type for process exit statuses.
pp_status is a formatter for process exit statuses of the form:
"exited %d"for`Exited _values"signaled %s"for`Signaled _value
pp_cmd_status is a formatter for command process exit statuses of the form: "cmd [%a]: %a".
Process standard inputs
The type for representing the standard input of a process.
in_fd ~close fd is a standard input that reads from file descriptor fd. If close is true, fd is closed after the process is spawn.
in_stdin is in_fd ~close:false Unix.stdin, a standard input that reads from the current process standard input.
Process standard outputs
The type for representing the standard output of a process.
out_file ~force ~make_path file is a standard output that writes to file file.
- If
make_pathistrueand the parent directory offiledoes not exist the whole path to the parent is created as needed with permission0o755(readable and traversable by everyone, writable by the user). - If
forceistrueandfileexists at call time as a regular file it tries to overwrite it, in all other cases the function errors iffileexists. modeare the permissions of the written file; they default to0o644, readable by everyone, writable by the user.
out_fd ~close fd is a standard output that writes to file descriptor fd. If close is true, fd is closed after the process spawn.
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) resultrun_status ~env ~cwd ~stdin ~stdout ~stderr cmd runs and waits for the completion of cmd in environment env with current directory cwd and standard IO connections stdin, stdout and stderr.
envdefaults toEnv.current_assignments()cwddefaults toDir.cwd()stdindefaults toin_stdinstdoutdefaults toout_stdoutstderrdefaults 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) resultrun_status_out is like run_status except stdout is read from the process to a string. The string is String.trimed if trim is true (default). If stderr is `Out the process' stderr is redirected to stdout 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) resultrun is run_status with non-`Exited 0 statuses turned into errors via pp_cmd_status.
val run_out :
?env:Env.assignments ->
?cwd:Fpath.t ->
?stdin:stdi ->
?stderr:[ `Stdo of stdo | `Out ] ->
trim:bool ->
Cmd.t ->
(string, string) resultrun_out is run_status_out with non-`Exited 0 statuses reporting the captured output (if any) prefixed by pp_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.
The type for process identifiers.
pid_to_int pid is the system identifier for process identifier pid.
val spawn :
?env:Env.assignments ->
?cwd:Fpath.t ->
?stdin:stdi ->
?stdout:stdo ->
?stderr:stdo ->
Cmd.t ->
(pid, string) resultspawn ~env ~cwd ~stdin ~stdout ~stderr cmd spawns command cmd in environment env with current directory cwd and standard IO connections stdin, stdout and stderr. env defaults to Env.current_assignments (), cwd to Dir.cwd (), stdin to in_stdin, stdout to out_stdout and stderr to out_stderr.
spawn_poll_status pid tries to collect the exit status of command spawn pid. If block is false, Ok None is immediately returned if pid has not terinated yet.
spawn_wait_status blocks and waits for pid's termination status to become available.
kill pid signal sends signal signal to the process pid.
Windows. Only the Sys.sigkill signal is emulated.
Tracing
type spawn_tracer =
pid option ->
Env.assignments option ->
cwd:Fpath.t option ->
Cmd.t ->
unitThe type for spawn tracers. Called with each blocking and non-blocking spawned command aswell as execv. The function is given the process identifier of the spawn (or None in case of execv), the environment if different from the program's one, the current working directory if different from the program's one and the actual command.
spawn_tracer_nop is a spawn tracer that does nothing. This is the initial spawn tracer.
tracer () is the current spawn tracer. Initially this is spawn_tracer_nop.
set_tracer t sets the current spawn tracer to t.
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 exited with the status of the child.
execv ~env ~cwd file argv executes file file as a new process in environment env with args as the Sys.argv of this process (in particular Sys.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 to B0_std.Os.Env.current_assignments (), cwd to B0_std.Dir.cwd ().
Exit needs that alias to refer to B0_std.Cmd.t.