ocaml-containers/2.5/containers/CCUnix/index.html
2019-02-02 14:05:01 -06:00

2 lines
No EOL
15 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>CCUnix (containers.CCUnix)</title><link rel="stylesheet" href="../../odoc.css"/><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body><div class="content"><header><nav><a href="../index.html">Up</a> <a href="../index.html">containers</a> &#x00BB; CCUnix</nav><h1>Module <code>CCUnix</code></h1><h2 id="high-level-functions-on-top-of-unix"><a href="#high-level-functions-on-top-of-unix" class="anchor"></a>High-level Functions on top of Unix</h2><p>Some useful functions built on top of Unix.</p><p><b>status: unstable</b></p><dl><dt>since</dt><dd>0.10</dd></dl><nav class="toc"><ul><li><a href="#calling-commands">Calling Commands</a></li><li><a href="#accessors">Accessors</a></li><li><a href="#simple-io">Simple IO</a></li><li><a href="#infix-functions">Infix Functions</a></li></ul></nav></header><dl><dt class="spec type" id="type-or_error"><a href="#type-or_error" class="anchor"></a><code><span class="keyword">type </span>'a or_error</code><code><span class="keyword"> = </span>(<span class="type-var">'a</span>, string) Result.result</code></dt><dt class="spec type" id="type-gen"><a href="#type-gen" class="anchor"></a><code><span class="keyword">type </span>'a gen</code><code><span class="keyword"> = </span>unit <span>&#45;&gt;</span> <span class="type-var">'a</span> option</code></dt></dl><section><header><h2 id="calling-commands"><a href="#calling-commands" class="anchor"></a>Calling Commands</h2></header><dl><dt class="spec value" id="val-escape_str"><a href="#val-escape_str" class="anchor"></a><code><span class="keyword">val </span>escape_str : string <span>&#45;&gt;</span> string</code></dt><dd><p>Escape a string so it can be a shell argument.</p></dd></dl><dl><dt class="spec type" id="type-call_result"><a href="#type-call_result" class="anchor"></a><code><span class="keyword">type </span>call_result</code><code><span class="keyword"> = </span>&lt; stdout : string; stderr : string; status : Unix.process_status; errcode : int; &gt;</code></dt></dl><dl><dt class="spec value" id="val-call_full"><a href="#val-call_full" class="anchor"></a><code><span class="keyword">val </span>call_full : ?&#8288;bufsize:int <span>&#45;&gt;</span> ?&#8288;stdin:[ `Gen of string <a href="index.html#type-gen">gen</a><span class="keyword"> | </span>`Str of string ] <span>&#45;&gt;</span> ?&#8288;env:string array <span>&#45;&gt;</span> (<span class="type-var">'a</span>, Buffer.t, unit, <a href="index.html#type-call_result">call_result</a>) Pervasives.format4 <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dd><p><code>call_full cmd</code> wraps the result of <code>Unix.open_process_full cmd</code> into an object. It reads the full stdout and stderr of the subprocess before returning.</p><dl><dt>parameter stdin</dt><dd><p>if provided, the generator or string is consumed and fed to the subprocess input channel, which is then closed.</p></dd></dl><dl><dt>parameter bufsize</dt><dd><p>buffer size used to read stdout and stderr.</p></dd></dl><dl><dt>parameter env</dt><dd><p>environment to run the command in.</p></dd></dl></dd></dl><dl><dt class="spec value" id="val-call"><a href="#val-call" class="anchor"></a><code><span class="keyword">val </span>call : ?&#8288;bufsize:int <span>&#45;&gt;</span> ?&#8288;stdin:[ `Gen of string <a href="index.html#type-gen">gen</a><span class="keyword"> | </span>`Str of string ] <span>&#45;&gt;</span> ?&#8288;env:string array <span>&#45;&gt;</span> (<span class="type-var">'a</span>, Buffer.t, unit, string<span class="keyword"> * </span>string<span class="keyword"> * </span>int) Pervasives.format4 <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dd><p><code>call cmd</code> is similar to <code>call_full cmd</code> but returns a tuple <code>stdout, stderr, errcode</code> instead of an object.</p></dd></dl><dl><dt class="spec value" id="val-call_stdout"><a href="#val-call_stdout" class="anchor"></a><code><span class="keyword">val </span>call_stdout : ?&#8288;bufsize:int <span>&#45;&gt;</span> ?&#8288;stdin:[ `Gen of string <a href="index.html#type-gen">gen</a><span class="keyword"> | </span>`Str of string ] <span>&#45;&gt;</span> ?&#8288;env:string array <span>&#45;&gt;</span> (<span class="type-var">'a</span>, Buffer.t, unit, string) Pervasives.format4 <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt></dl><dl><dt class="spec type" id="type-line"><a href="#type-line" class="anchor"></a><code><span class="keyword">type </span>line</code><code><span class="keyword"> = </span>string</code></dt><dt class="spec type" id="type-async_call_result"><a href="#type-async_call_result" class="anchor"></a><code><span class="keyword">type </span>async_call_result</code><code><span class="keyword"> = </span>&lt; stdout : <a href="index.html#type-line">line</a> <a href="index.html#type-gen">gen</a>; stderr : <a href="index.html#type-line">line</a> <a href="index.html#type-gen">gen</a>; stdin : <a href="index.html#type-line">line</a> <span>&#45;&gt;</span> unit; close_in : unit; close_err : unit; close_out : unit; close_all : unit; wait : Unix.process_status; wait_errcode : int; &gt;</code></dt><dd><p>A subprocess for interactive usage (read/write channels line by line)</p><dl><dt>since</dt><dd>0.11</dd></dl></dd></dl><dl><dt class="spec value" id="val-async_call"><a href="#val-async_call" class="anchor"></a><code><span class="keyword">val </span>async_call : ?&#8288;env:string array <span>&#45;&gt;</span> (<span class="type-var">'a</span>, Buffer.t, unit, <a href="index.html#type-async_call_result">async_call_result</a>) Pervasives.format4 <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dd><p>Spawns a subprocess, like <a href="index.html#val-call"><code>call</code></a>, but the subprocess's channels are line generators and line sinks (for stdin). If <code>p</code> is <code>async_call &quot;cmd&quot;</code>, then <code>p#wait</code> waits for the subprocess to die. Channels can be closed independently.</p><dl><dt>since</dt><dd>0.11</dd></dl></dd></dl></section><section><header><h2 id="accessors"><a href="#accessors" class="anchor"></a>Accessors</h2><dl><dt>since</dt><dd>0.11</dd></dl></header><dl><dt class="spec value" id="val-stdout"><a href="#val-stdout" class="anchor"></a><code><span class="keyword">val </span>stdout : &lt; stdout : <span class="type-var">'a</span>; .. &gt; <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dt class="spec value" id="val-stderr"><a href="#val-stderr" class="anchor"></a><code><span class="keyword">val </span>stderr : &lt; stderr : <span class="type-var">'a</span>; .. &gt; <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dt class="spec value" id="val-status"><a href="#val-status" class="anchor"></a><code><span class="keyword">val </span>status : &lt; status : <span class="type-var">'a</span>; .. &gt; <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dt class="spec value" id="val-errcode"><a href="#val-errcode" class="anchor"></a><code><span class="keyword">val </span>errcode : &lt; errcode : <span class="type-var">'a</span>; .. &gt; <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt></dl></section><section><header><h2 id="simple-io"><a href="#simple-io" class="anchor"></a>Simple IO</h2></header><dl><dt class="spec value" id="val-with_in"><a href="#val-with_in" class="anchor"></a><code><span class="keyword">val </span>with_in : ?&#8288;mode:int <span>&#45;&gt;</span> ?&#8288;flags:Unix.open_flag list <span>&#45;&gt;</span> string <span>&#45;&gt;</span> f:(Pervasives.in_channel <span>&#45;&gt;</span> <span class="type-var">'a</span>) <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dd><p>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.</p><dl><dt>parameter flags</dt><dd><p>opening flags. <code>Unix.O_RDONLY</code> is used in any cases.</p></dd></dl><dl><dt>since</dt><dd>0.16</dd></dl></dd></dl><dl><dt class="spec value" id="val-with_out"><a href="#val-with_out" class="anchor"></a><code><span class="keyword">val </span>with_out : ?&#8288;mode:int <span>&#45;&gt;</span> ?&#8288;flags:Unix.open_flag list <span>&#45;&gt;</span> string <span>&#45;&gt;</span> f:(Pervasives.out_channel <span>&#45;&gt;</span> <span class="type-var">'a</span>) <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dd><p>Same as <a href="index.html#val-with_in"><code>with_in</code></a> but for an output channel.</p><dl><dt>parameter flags</dt><dd><p>opening flags (default <code>[Unix.O_CREAT; Unix.O_TRUNC]</code>) <code>Unix.O_WRONLY</code> is used in any cases.</p></dd></dl><dl><dt>since</dt><dd>0.16</dd></dl></dd></dl><dl><dt class="spec value" id="val-with_process_in"><a href="#val-with_process_in" class="anchor"></a><code><span class="keyword">val </span>with_process_in : string <span>&#45;&gt;</span> f:(Pervasives.in_channel <span>&#45;&gt;</span> <span class="type-var">'a</span>) <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dd><p>Open a subprocess and obtain a handle to its stdout.</p><pre><code class="ml">CCUnix.with_process_in &quot;ls /tmp&quot; ~f:CCIO.read_lines_l;;</code></pre><dl><dt>since</dt><dd>0.16</dd></dl></dd></dl><dl><dt class="spec value" id="val-with_process_out"><a href="#val-with_process_out" class="anchor"></a><code><span class="keyword">val </span>with_process_out : string <span>&#45;&gt;</span> f:(Pervasives.out_channel <span>&#45;&gt;</span> <span class="type-var">'a</span>) <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dd><p>Open a subprocess and obtain a handle to its stdin.</p><dl><dt>since</dt><dd>0.16</dd></dl></dd></dl><dl><dt class="spec type" id="type-process_full"><a href="#type-process_full" class="anchor"></a><code><span class="keyword">type </span>process_full</code><code><span class="keyword"> = </span>&lt; stdin : Pervasives.out_channel; stdout : Pervasives.in_channel; stderr : Pervasives.in_channel; close : Unix.process_status; &gt;</code></dt><dd><p>Handle to a subprocess.</p><dl><dt>since</dt><dd>0.16</dd></dl></dd></dl><dl><dt class="spec value" id="val-with_process_full"><a href="#val-with_process_full" class="anchor"></a><code><span class="keyword">val </span>with_process_full : ?&#8288;env:string array <span>&#45;&gt;</span> string <span>&#45;&gt;</span> f:(<a href="index.html#type-process_full">process_full</a> <span>&#45;&gt;</span> <span class="type-var">'a</span>) <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dd><p>Open a subprocess and obtain a handle to its channels.</p><dl><dt>parameter env</dt><dd><p>environment to pass to the subprocess.</p></dd></dl><dl><dt>since</dt><dd>0.16</dd></dl></dd></dl><dl><dt class="spec value" id="val-with_connection"><a href="#val-with_connection" class="anchor"></a><code><span class="keyword">val </span>with_connection : Unix.sockaddr <span>&#45;&gt;</span> f:(Pervasives.in_channel <span>&#45;&gt;</span> Pervasives.out_channel <span>&#45;&gt;</span> <span class="type-var">'a</span>) <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dd><p>Wrap <span class="xref-unresolved" title="unresolved reference to &quot;Unix.open_connection&quot;"><code>Unix</code>.open_connection</span> with a handler.</p><dl><dt>since</dt><dd>0.16</dd></dl></dd></dl><dl><dt class="spec exception" id="exception-ExitServer"><a href="#exception-ExitServer" class="anchor"></a><code><span class="keyword">exception </span></code><code><span class="exception">ExitServer</span></code></dt></dl><dl><dt class="spec value" id="val-establish_server"><a href="#val-establish_server" class="anchor"></a><code><span class="keyword">val </span>establish_server : Unix.sockaddr <span>&#45;&gt;</span> f:(Pervasives.in_channel <span>&#45;&gt;</span> Pervasives.out_channel <span>&#45;&gt;</span> <span class="type-var">_</span>) <span>&#45;&gt;</span> unit</code></dt><dd><p>Listen on the address and calls the handler in a blocking fashion. Using <code>Thread</code> is recommended if handlers might take time. The callback should raise <a href="index.html#exception-ExitServer"><code>ExitServer</code></a> to stop the loop.</p><dl><dt>since</dt><dd>0.16</dd></dl></dd></dl><dl><dt class="spec value" id="val-with_file_lock"><a href="#val-with_file_lock" class="anchor"></a><code><span class="keyword">val </span>with_file_lock : kind:[ `Read<span class="keyword"> | </span>`Write ] <span>&#45;&gt;</span> string <span>&#45;&gt;</span> (unit <span>&#45;&gt;</span> <span class="type-var">'a</span>) <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dd><p><code>with_file_lock ~kind filename f</code> puts a lock on the offset 0 of the file named <code>filename</code>, calls <code>f</code> and returns its result after the file is unlocked. If <code>f ()</code> raises an exception the exception is re-raised after the file is unlocked.</p><dl><dt>parameter kind</dt><dd><p>specifies whether the lock is read-only or read-write.</p></dd></dl><dl><dt>since</dt><dd>1.2</dd></dl></dd></dl></section><section><header><h2 id="infix-functions"><a href="#infix-functions" class="anchor"></a>Infix Functions</h2></header><div class="spec module" id="module-Infix"><a href="#module-Infix" class="anchor"></a><code><span class="keyword">module </span><a href="Infix/index.html">Infix</a> : <span class="keyword">sig</span> ... <span class="keyword">end</span></code></div><div><div class="spec include"><div class="doc"><details open="open"><summary><span class="def"><code><span class="keyword">include </span><span class="keyword">module type of </span><a href="index.html#module-Infix">Infix</a></code></span></summary><dl><dt class="spec value" id="val-(?|)"><a href="#val-(?|)" class="anchor"></a><code><span class="keyword">val </span>(?|) : (<span class="type-var">'a</span>, Buffer.t, unit, <a href="index.html#type-call_result">call_result</a>) Pervasives.format4 <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dd><p>Infix version of <a href="index.html#val-call"><code>call</code></a>.</p><dl><dt>since</dt><dd>0.11</dd></dl></dd></dl><dl><dt class="spec value" id="val-(?|&amp;)"><a href="#val-(?|&amp;)" class="anchor"></a><code><span class="keyword">val </span>(?|&amp;) : (<span class="type-var">'a</span>, Buffer.t, unit, <a href="index.html#type-async_call_result">async_call_result</a>) Pervasives.format4 <span>&#45;&gt;</span> <span class="type-var">'a</span></code></dt><dd><p>Infix version of <a href="index.html#val-async_call"><code>async_call</code></a>.</p><dl><dt>since</dt><dd>0.11</dd></dl></dd></dl></details></div></div></div></section></div></body></html>