Module Shexp_process__.Process
type 'a tAn
'a tvalue represent a process pipeline description, that can be evaluated usingevalinto a value of type'a. Note that creating an'a tvalue has no effect. Effects are only performed when callingeval.
module Context : sig ... endExecution contexts
val eval : ?context:Context.t -> 'a t -> 'aEvaluate the given process in the given environment.
If
contextis not specified, a temporary one is created. If you are callingevalmany times, then creating a context and reusing it is more efficient.
module Logged : sig ... endmodule Traced : sig ... endmodule Prim : sig ... endmodule type Debugger = Shexp_process__.Debugger_intf.Sshexp_process allows one to plug a debugger in the evaluator.
LoggedandTracedare essentially two non-interactive debuggers.
module With_debug : functor (Dbg : Debugger) -> sig ... endBasic processes
val return : 'a -> 'a tClassic monad operations. Note that because shexp_process uses threads under the hood, you should be careful about using global mutable data structures. To communicate values between concurrent processes, use the
syncfunction.
val bind : 'a t -> f:('a -> 'b t) -> 'b tval map : 'a t -> f:('a -> 'b) -> 'b tval fail : exn -> _ tCreate a process that fails with the given exception. Evaluation of this process will raise this exception.
val fork : 'a t -> 'b t -> ('a * 'b) tfork a breprensents two processes that are executed concurrently. The resulting process will block until bothaandbhave finished and will return both of their results.This is essentially the same as forking a system process: both process can independently change the current working directory, change their standard out, etc...
Regarding errors, if the evaluation of both processes fail, then only one the exceptions will be kept. It is not specified which one. You should use
Tracedto get a full trace and see what exceptions are raised and where.
val protect : finally:unit t -> 'a t -> 'a tprotect ~finally tprotects against execution errors.finallyis always executed, even if the evaluation oftraises.
val reify_exn : ('a -> 'b t) -> 'a -> 'b tCapture exceptions into the process monad. You can use this if the argument of
protectis the result of a function call.For instance in the following code
finallywouldn't be executed:let f () = raise Not_found protect ~finally (f ())You can write instead:
protect ~finally (reify_exn f ())
Running commands
val run : string -> string list -> unit tRun an external program. This will fail if the external program does not exists, is signaled or exit with a non-zero exit code.
val run_exit_code : string -> string list -> int tRun an external program and return its exit code. This will fail if the external program is signaled.
module Exit_status = Shexp_process.Exit_statusval run_exit_status : string -> string list -> Exit_status.t tRun an external program and return its exit status.
val run_bool : ?true_v:int list -> ?false_v:int list -> string -> string list -> bool tRun an external program, returns
trueif its exit code is part oftrue_vandfalseif it is part offalse_v.
val call : string list -> unit tSame functions as the 'run' ones above, but take a string list instead. This way, the first element and the others are treated in a homogeneous way. It can ease prepending commands in specific circumstances, e.g.
if profile then call ("time" :: command) else call commandE.g.
call ["grep"; "-i"; pattern; filename]is equivalent torun "grep" ["-i"; pattern; filename]
val call_exit_code : string list -> int tval call_exit_status : string list -> Exit_status.t tval call_bool : ?true_v:int list -> ?false_v:int list -> string list -> bool t
module Background_command : sig ... endval spawn : string -> string list -> Background_command.t tStart an external program but do not wait for its termination. If you never call
waiton the result, the process will become a zombie after it terminates.
val wait : Background_command.t -> Exit_status.t tWait for a background command to terminate and return its exit status.
Unix environment
val find_executable : string -> string option tReturn the absolute path to the given command.
val find_executable_exn : string -> string tval get_env : string -> string option tReturn the value associated to the given environment variable.
Current working directory
val cwd_logical : string tReturn the current working directory. Note that there is no guarantee that this directory exists. For instance if a component in this path has is renamed during the evaluation of the process, then this will be a dangling directory.
IO
module Std_io = Shexp_process.Std_ioval echo : ?where:Std_io.t -> ?n:unit -> string -> unit tOutput a string on one of the standard io.
~n:()suppresses the newline output at the end.
val print : string -> unit techo ~where:Stdout
val eprint : string -> unit techo ~where:Stderr
val printf : ('a, unit, string, unit t) Stdlib.format4 -> 'aval eprintf : ('a, unit, string, unit t) Stdlib.format4 -> 'aval read_all : string tConsume all standard input
val fold_lines : init:'a -> f:('a -> string -> 'a t) -> 'a tFold over lines in the input.
fis given the line with the end of line. Both"\r\n"and"\n"are treated as end of lines.
Pipes
val pipe : ?connect:(Std_io.t list * Std_io.t) -> unit t -> 'a t -> 'a tpipe ~connect:(aios, bio) a bis a process obtain by connecting theaiosofato thebioofb.aandbare evaluated in parallel (as withfork).(aio, bio)defaults to([Stdout], Stdin).
val pipe_both : ?connect:(Std_io.t list * Std_io.t) -> 'a t -> 'b t -> ('a * 'b) tpipe_pair a bis a the same aspipebut returns the results of bothaandb.
Redirections
val redirect : Std_io.t list -> ?perm:int -> flags:Unix.open_flag list -> string -> 'a t -> 'a tredirect ios ?perm ~flags filename tredirects the following ios to a file int.permandflagsare passed the same as forUnix.openfile.
val stderr_to : ?append:unit -> string -> 'a t -> 'a tval outputs_to : ?append:unit -> string -> 'a t -> 'a tval stdin_from : string -> 'a t -> 'a tval replace_io : ?stdin:Std_io.t -> ?stdout:Std_io.t -> ?stderr:Std_io.t -> 'a t -> 'a tReplace the given standard io by the given one. For instance to redirect stdout to stderr:
replace_io ~stdout:Stderr t
Temporary files & directory
val temp_dir : string tReturn the current temporary directory
val set_temp_dir : string -> 'a t -> 'a tset_temp_dir dir krepresents the processkevaluated in a context where the current temporary directory is set todir.
FS operations
val chmod : string -> perm:Unix.file_perm -> unit tval chown : string -> uid:int -> gid:int -> unit tval mkdir : ?perm:Unix.file_perm -> ?p:unit -> string -> unit tval rm : string -> unit tval rmdir : string -> unit tval mkfifo : ?perm:Unix.file_perm -> string -> unit tval link : string -> string -> unit tval rename : string -> string -> unit tval symlink : string -> string -> unit tval stat : string -> Unix.LargeFile.stats tval lstat : string -> Unix.LargeFile.stats tval readlink : string -> string tval readdir : string -> string list tval file_exists : string -> bool tval rm_rf : string -> unit tRecursively remove a tree
Misc
val sleep : float -> unit t
Synchronisation
Misc
module Infix : sig ... endmodule Let_syntax : sig ... endOpen this module when using
ppx_let
module List : sig ... end