package shexp

  1. Overview
  2. Docs
Process library and s-expression based shell

Install

Dune Dependency

Authors

Maintainers

Sources

shexp-v0.12.0.tar.gz
sha256=b2ca616e5384d517a3bef878ed2fd364b2c51993f7f57f223954422e2eea0e1d
md5=d48e292eaa2f7e4f66fa5381eddc86a9

Description

Shexp is composed of two parts: a library providing a process monad for shell scripting in OCaml as well as a simple s-expression based shell interpreter. Shexp works on both Unix and Windows.

Published: 01 Mar 2019

README

README.org

[[./images/logo.png]]

Shexp is composed of two parts: a library providing a process monad
for shell scripting in OCaml as well as a simple s-expression based
shell interpreter. Both provide good debugging support.

Shexp works on both Unix and Windows and depends only on Base.

** The Shexp_process library

The Shexp_process library exposes a single =Process= module allowing
one to construct complex pipelines such as pipes and other
redirections. It is intended to replace shell scripts as well as
provide a more complete alternative to =Async.Process= like
modules.

On Unix, Shexp_process uses the specific *at system calls (such as
=openat=) to reliably maintain several working directories inside the
same system process as well as =vfork= to avoid performance problems
with large processes.

*** Usage

One creates a value of type ='a Process.t= which represent a process
pipeline. Using the combinators of the process module, one can modify
the execution environment (current working directory, environment
variables, ...) as well as construct complex redirections.

To effectively execute the pipeline and get a result, one has to call
=Process.eval=.

Essentially you get the same primitives as what you would get from a
shell, except that everything is typed:

#+begin_src ocaml
(** Run an external program *)
val run : string -> string list -> unit t

(** Equivalent of a shell pipe *)
val pipe : unit t -> 'a t -> 'a t

(** Same thing, but you get the values from both sides of the pipe *)
val pipe_both : 'a t -> 'b t -> ('a * 'b) t

(** Read all of the process' standard input  *)
val read_all : string t

(** [chdir dir k] exexutes [k] with the current directory set to [dir] *)
val chdir : string -> 'a t -> 'a t
#+end_src

For instance, to run an external command in a given directory and
capture its standard output, where =|-= is infix operator for =pipe=:

#+begin_src ocaml
let f ~dir prog args = eval (chdir dir (run prog args |- read_all))
#+end_src

*** Debugging

Shexp_process allows one to plug a debugger in the evaluator. A
debugger is essentially a set of hooks called at the appropriate
places by Shexp_process. Shexp_process itself provides two
non-interactive debuggers: a logger and a tracer.

The logger is intended for printing command synchrously as they are a
run, a bit like =set -x= in bash.

The tracer provides a full trace of execution. The trace is presented
as a tree, so you can see clearly what happens on both sides of a
fork.

For instance the following process:

#+begin_src ocaml
echo "Hello, world!" |- run "blah" []
#+end_src

would produce the following trace:

#+begin_src scheme
((create-pipe)
 (-> (4 5))
 (fork
   (
    (do
      (set-ios (stdout) 5)
      (echo "Hello, world!"))
    (close-fd 36)
   )
   (
    (do
      (set-ios (stdin) 4)
      (run blah ())
      (raised (Failure "blah: command not found")))
    (close-fd 35)
   )
 )
)
#+end_src

Dependencies (6)

  1. spawn >= "v0.12"
  2. dune >= "1.5.1"
  3. base-threads
  4. posixat >= "v0.12" & < "v0.13"
  5. base >= "v0.12" & < "v0.13"
  6. ocaml >= "4.07.0"

Dev Dependencies

None

Used by (2)

  1. satyrographos >= "0.0.2.1"
  2. timmy

Conflicts

None

OCaml

Innovation. Community. Security.