package shexp
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=b2ca616e5384d517a3bef878ed2fd364b2c51993f7f57f223954422e2eea0e1d
md5=d48e292eaa2f7e4f66fa5381eddc86a9
doc/shexp.process/Shexp_process/index.html
Module Shexp_process
module Std : sig ... endAn 'a t value represent a process pipeline description, that can be evaluated using eval into a value of type 'a. Note that creating an 'a t value has no effect. Effects are only performed when calling eval.
module Context : sig ... endExecution contexts
Evaluate the given process in the given environment.
If context is not specified, a temporary one is created. If you are calling eval many times, then creating a context and reusing it is more efficient.
module Logged : sig ... endmodule Traced : sig ... endmodule Prim : sig ... endmodule type Debugger = sig ... endshexp_process allows one to plug a debugger in the evaluator. Logged and Traced are essentially two non-interactive debuggers.
module With_debug (Dbg : sig ... end) : 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 sync function.
val fail : exn -> _ tCreate a process that fails with the given exception. Evaluation of this process will raise this exception.
fork a b reprensents two processes that are executed concurrently. The resulting process will block until both a and b have 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 Traced to get a full trace and see what exceptions are raised and where.
protect ~finally t protects against execution errors. finally is always executed, even if the evaluation of t raises.
Capture exceptions into the process monad. You can use this if the argument of protect is the result of a function call.
For instance in the following code finally wouldn'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 : sig ... endExit status of external processes
val 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 true if its exit code is part of true_v and false if it is part of false_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 command
E.g. call ["grep"; "-i"; pattern filename] is equivalent to run "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 tmodule 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 wait on 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.
val get_env_exn : string -> string tset_env var value k represents the process k evaluated in a context where the envornment variable var is set to value.
set_env var value k represents the process k evaluated in a context where the environment variable var is unset.
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.
chdir dir k represents the process k evaluated in a context where the current working directory is changed to dir.
IO
module Std_io : sig ... endOutput 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 read_all : string tConsume all standard input
Fold over lines in the input. f is given the line with the end of line. Both "\r\n" and "\n" are treated as end of lines.
Fold over chunks separated by sep in the input. This can be used in conjunction with commands that support ending entries in the output with a '\000' such as find -print0.
Pipes
pipe ~connect:(aios, bio) a b is a process obtain by connecting the aios of a to the bio of b. a and b are evaluated in parallel (as with fork).
(aio, bio) defaults to ([Stdout], Stdin).
pipe_pair a b is a the same as pipe but returns the results of both a and b.
capture ios t = pipe_both ~connect:(ios, Stdin) read_all
capture_unit ios t = pipe ~connect:(ios, Stdin) read_all
Redirections
val redirect :
Std_io.t list ->
?perm:int ->
flags:Unix.open_flag list ->
string ->
'a t ->
'a tredirect ios ?perm ~flags filename t redirects the following ios to a file in t. perm and flags are passed the same as for Unix.openfile.
Replace 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
set_temp_dir dir k represents the process k evaluated in a context where the current temporary directory is set to dir.
with_temp_file ~prefix ~suffix f is a process that creates a temporary file and passes it to f. The file is created inside the temporary directory.
When the process returned by f finishes, the file is removed.
Same as with_temp_file but creates a directory. The directory and its contents are deleted when the process finishes.
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 tSynchronisation
val new_channel : 'a Event.channel tval sync : 'a Event.event -> 'a tMisc
module Infix : sig ... endmodule Let_syntax : sig ... endOpen this module when using ppx_let
module List : sig ... end