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.resultfind_tool ~search toolis the file path, if any, to the program executable for the tool specificationtool.- If
toolhas a single path segment. Thetoolfile is searched, in list order, for the first matching executable file in the directories ofsearch. These directories default to those that result from parsingPATHwithFpath.list_of_search_path. - If
toolhas multiple path segments the corresponding file is simply tested for existence and executability.Ok (Some tool)is returned if that is case andOk Noneotherwise.
- If
val must_find_tool : ?search:Fpath.t list -> Cmd.tool -> (Fpath.t, string) Stdlib.resultmust_find_toolis likefind_toolexcept it errors ifOk Noneis returned.
val find_first_tool : ?search:Fpath.t list -> Cmd.tool list -> (Fpath.t option, string) Stdlib.resultfind_first_toolis 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.resultfind ~search cmdresolvescmd's tool asfind_tooldoes.
val must_find : ?search:Fpath.t list -> Cmd.t -> (Cmd.t, string) Stdlib.resultmust_find ~search cmdresolvescmd's tool asmust_find_tooldoes.
val find_first : ?search:Fpath.t list -> Cmd.t list -> (Cmd.t option, string) Stdlib.resultfind_first ~search cmdsresolvescmds'sCmd.toos asfind_first_tooldoes.
Process completion statuses
Process standard inputs
val in_string : string -> stdiin_string sis a standard input that reads the strings.
val in_fd : close:bool -> Unix.file_descr -> stdiin_fd ~close fdis a standard input that reads from file descriptorfd. Ifcloseistrue,fdis closed after the process is spawn.
val in_stdin : stdiin_stdinisin_fd ~close:false Unix.stdin, a standard input that reads from the current process standard input.
val in_null : stdiin_nullisin_file File.null.
Process standard outputs
val out_fd : close:bool -> Unix.file_descr -> stdoout_fd ~close fdis a standard output that writes to file descriptorfd. Ifcloseistrue,fdis closed after the process spawn.
val out_stdout : stdoout_stdoutisout_fd ~close:false Unix.stdout
val out_stderr : stdoout_stderrisout_fd ~close:false Unix.stderr
val out_null : stdoout_nullisout_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.resultrun_status ~env ~cwd ~stdin ~stdout ~stderr cmdruns and waits for the completion ofcmdin environmentenvwith current directorycwdand standard IO connectionsstdin,stdoutandstderr.envdefaults toEnv.current_assignments()cwddefaults to Current working directory (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) Stdlib.resultrun_status_outis likerun_statusexceptstdoutis read from the process to a string. The string isString.trimed iftrimistrue(default). Ifstderris`Outthe process'stderris redirected tostdoutand 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.resultrunisrun_statuswith non-`Exited 0statuses 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.resultrunisrun_status_outwith non-`Exited 0statuses 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 -> intpid_to_int pidis 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.resultspawn ~env ~cwd ~stdin ~stdout ~stderr cmdspawns commandcmdin environmentenvwith current directorycwdand standard IO connectionsstdin,stdoutandstderr.envdefaults toEnv.current_assignments(),cwdtoDir.current(),stdintoin_stdin,stdouttoout_stdoutandstderrtoout_stderr.
val spawn_poll_status : pid -> (status option, string) Stdlib.resultspawn_poll_status pidtries to collect the exit status of command spawnpid. Ifblockisfalse,Ok Noneis immediately returned ifpidhas not terinated yet.
val spawn_wait_status : pid -> (status, string) Stdlib.resultspawn_wait_statusblocks and waits forpid's termination status to become available.
Tracing
type spawn_tracer= pid -> Env.assignments option -> cwd:Fpath.t option -> Cmd.t -> unitThe 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_tracerspawn_tracer_nopis a spawn tracer that does nothing. This is the initial spawn tracer.
val spawn_tracer : unit -> spawn_tracertracer ()is the current spawn tracer. Initially this isspawn_tracer_nop.
val set_spawn_tracer : spawn_tracer -> unitset_tracer tsets 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 exited with the status of the child.
val execv : ?env:Env.assignments -> ?cwd:Fpath.t -> Fpath.t -> Cmd.t -> (unit, string) Stdlib.resultexecv ~env ~cwd f argvexecutes filefas a new process in environmentenvwithargsas 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.envdefaults toB0.OS.Env.current_assignments(),cwdtoDir.current().