CCUnixSourceSome useful functions built on top of Unix.
status: unstable
Escape a string so it can be a shell argument.
type call_result =
< stdout : string
; stderr : string
; status : Unix.process_status
; errcode : int >val call_full :
?bufsize:int ->
?stdin:[ `Gen of string gen | `Str of string ] ->
?env:string array ->
('a, Stdlib.Buffer.t, unit, call_result) Stdlib.format4 ->
'acall_full cmd wraps the result of Unix.open_process_full cmd into an object. It reads the full stdout and stderr of the subprocess before returning. cmd can be a format string as in Printf.
Example:
# CCUnix.call_full "ls %s" (CCUnix.escape_str "/");;val call :
?bufsize:int ->
?stdin:[ `Gen of string gen | `Str of string ] ->
?env:string array ->
('a, Stdlib.Buffer.t, unit, string * string * int) Stdlib.format4 ->
'acall cmd is similar to call_full but returns a tuple stdout, stderr, errcode instead of an object.
type async_call_result =
< stdout : line gen
; stderr : line gen
; stdin : line -> unit
; close_in : unit
; close_err : unit
; close_out : unit
; close_all : unit
; wait : Unix.process_status
; wait_errcode : int >A subprocess for interactive usage (read/write channels line by line)
val async_call :
?env:string array ->
('a, Stdlib.Buffer.t, unit, async_call_result) Stdlib.format4 ->
'aSpawns a subprocess, like call, but the subprocess's channels are line generators and line sinks (for stdin). If p is async_call "cmd", then p#wait waits for the subprocess to die. Channels can be closed independently.
val with_in :
?mode:int ->
?flags:Unix.open_flag list ->
string ->
f:(Stdlib.in_channel -> 'a) ->
'aOpen an input file with the given optional flag list, calls the function on the input channel. When the function raises or returns, the channel is closed.
val with_out :
?mode:int ->
?flags:Unix.open_flag list ->
string ->
f:(Stdlib.out_channel -> 'a) ->
'aSame as with_in but for an output channel.
Open a shell command in a subprocess and obtain a handle to its stdout.
CCUnix.with_process_in "ls /tmp" ~f:CCIO.read_lines_l;;Open a shell command in a subprocess and obtain a handle to its stdin.
type process_full =
< stdin : Stdlib.out_channel
; stdout : Stdlib.in_channel
; stderr : Stdlib.in_channel
; close : Unix.process_status >Handle to a subprocess.
Open a subprocess and obtain a handle to its channels.
On unix, call Unix.setsid() to make sure subprocesses die at the same time as the current process. Does nothing on windows. Idempotent: it can be called several times but will only have effects, if any, the first time.
val with_connection :
Unix.sockaddr ->
f:(Stdlib.in_channel -> Stdlib.out_channel -> 'a) ->
'aWrap Unix.open_connection with a handler.
val establish_server :
Unix.sockaddr ->
f:(Stdlib.in_channel -> Stdlib.out_channel -> _) ->
unitListen on the address and calls the handler in a blocking fashion. Using Thread is recommended if handlers might take time. The callback should raise ExitServer to stop the loop.
with_file_lock ~kind filename f puts a lock on the offset 0 of the file named filename, calls f and returns its result after the file is unlocked. If f () raises an exception the exception is re-raised after the file is unlocked.
Create a temporary directory, call the function, and then destroy the directory afterwards. Usage with_temp_dir pattern f.
Note that this is implemented following the discussion at: https://discuss.ocaml.org/t/how-to-create-a-temporary-directory-in-ocaml/1815/