Module CCUnix

module CCUnix: sig .. end

High-level Functions on top of Unix

Some useful functions built on top of Unix.

status: unstable
Since 0.10


type 'a or_error = [ `Error of string | `Ok of 'a ] 
type 'a gen = unit -> 'a option 

Calling Commands


val escape_str : Buffer.t -> string -> unit
Escape a string so it can be a shell argument.
type call_result = <
   errcode : int; (*
Extracted from status
*)
   status : Unix.process_status;
   stderr : string;
   stdout : string;
>
val call : ?bufsize:int ->
?stdin:[ `Gen of string gen | `Str of string ] ->
?env:string array ->
('a, Buffer.t, unit, call_result) Pervasives.format4 -> 'a
call 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.
bufsize : buffer size used to read stdout and stderr
stdin : if provided, the generator or string is consumed and fed to the subprocess input channel, which is then closed.
env : environment to run the command in
type line = string 
type async_call_result = <
   close_all : unit;
   close_err : unit;
   close_in : unit;
   close_out : unit;
   stderr : line gen;
   stdin : line -> unit;
   stdout : line gen;
   wait : Unix.process_status;
   wait_errcode : int;
>
A subprocess for interactive usage (read/write channels line by line)
Since 0.11
val async_call : ?env:string array ->
('a, Buffer.t, unit, async_call_result) Pervasives.format4 -> 'a
Spawns a subprocess, like CCUnix.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.
Since 0.11

Accessors


val stdout : < stdout : 'a; .. > -> 'a
val stderr : < stderr : 'a; .. > -> 'a
val status : < status : 'a; .. > -> 'a
val errcode : < errcode : 'a; .. > -> 'a

Simple IO


val with_in : ?mode:int ->
?flags:Unix.open_flag list -> string -> f:(Pervasives.in_channel -> 'a) -> 'a
Open 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.
Since 0.16
flags : opening flags. Unix.O_RDONLY is used in any cases
val with_out : ?mode:int ->
?flags:Unix.open_flag list ->
string -> f:(Pervasives.out_channel -> 'a) -> 'a
Same as CCUnix.with_in but for an output channel
Since 0.16
flags : opening flags (default [Unix.O_CREAT; Unix.O_TRUNC]) Unix.O_WRONLY is used in any cases.
val with_process_in : string -> f:(Pervasives.in_channel -> 'a) -> 'a
Open a subprocess and obtain a handle to its stdout
Since 0.16
val with_process_out : string -> f:(Pervasives.out_channel -> 'a) -> 'a
Open a subprocess and obtain a handle to its stdin
Since 0.16
type process_full = <
   close : Unix.process_status;
   stderr : Pervasives.in_channel;
   stdin : Pervasives.out_channel;
   stdout : Pervasives.in_channel;
>
Handle to a subprocess.
Since 0.16
val with_process_full : ?env:string array -> string -> f:(process_full -> 'a) -> 'a
Open a subprocess and obtain a handle to its channels.
Since 0.16
env : environment to pass to the subprocess.
val with_connection : Unix.sockaddr ->
f:(Pervasives.in_channel -> Pervasives.out_channel -> 'a) -> 'a
Wrap Unix.open_connection with a handler
Since 0.16
exception ExitServer
val establish_server : Unix.sockaddr ->
f:(Pervasives.in_channel -> Pervasives.out_channel -> 'a) -> unit
Listen on the address and calls the handler in a blocking fashion. Using Thread is recommended if handlers might take time. The callback should raise CCUnix.ExitServer to stop the loop.
Since 0.16

Infix Functions


module Infix: sig .. end
include CCUnix.Infix