package shexp

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

Install

Dune Dependency

Authors

Maintainers

Sources

shexp-v0.13.0.tar.gz
sha256=ff0e6ed52adf66dc1a1a9c85577dac043a0821feacfab09a5b8bea8f99ff3ba5
md5=13002fde6249703d06ff820906cd96c9

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: 20 Nov 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.13" & < "v0.14"
  5. base >= "v0.13" & < "v0.14"
  6. ocaml >= "4.07.0"

Dev Dependencies

None

Used by (2)

  1. satyrographos >= "0.0.2.7"
  2. timmy

Conflicts

None

OCaml

Innovation. Community. Security.