Something went wrong. Try again.
Shells in OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505type env = < fs : Eio.Fs.dir_ty Eio.Path.t ; stdin : Eio_unix.source_ty Eio.Flow.source ; stdout : Eio_unix.sink_ty Eio.Flow.sink ; stderr : Eio_unix.sink_ty Eio.Flow.sink ; process_mgr : Eio_unix.Process.mgr_ty Eio.Process.mgr ; clock : float Eio.Time.clock_ty Eio.Time.clock ; mono_clock : Eio.Time.Mono.ty Eio.Time.Mono.t ; secure_random : Eio.Flow.source_ty Eio.Flow.source ; debug : Eio.Debug.t ; backend_id : string >
let override ?stdin ?stdout ?stderr ?process_mgr ?clock ?mono_clock ?fs ?secure_random ?debug ?backend_id (env : < env ; .. >) : env = object method stdin = Option.value ~default:env#stdin stdin method stdout = Option.value ~default:env#stdout stdout method stderr = Option.value ~default:env#stderr stderr method process_mgr = Option.value ~default:env#process_mgr process_mgr method clock = Option.value ~default:env#clock clock method mono_clock = Option.value ~default:env#mono_clock mono_clock method fs = Option.value ~default:env#fs fs method secure_random = Option.value ~default:env#secure_random secure_random method debug = Option.value ~default:env#debug debug method backend_id = Option.value ~default:env#backend_id backend_id end
module type State = sig type t (** State for the shell and operating system that is carried from one evaluation step to the next. *)
val empty : t (** Mainly used for testing purposes. *)
val jsont : t Jsont.t (** Jsont value for marshalling the data-structure. *)
include Irmin.Contents.S with type t := t
val cwd : t -> Fpath.t (** The current working directory *)
val set_cwd : t -> Fpath.t -> t (** Update the cwd *)
val expand : t -> [ `Tilde ] -> string (** Expansions *)
val lookup : t -> param:string -> Ast.fragments option * Trace.entry (** Parameter lookup. [None] means [unset]. *)
val path : t -> string list option (** Fast path for getting the [PATH] environment variable. *)
type variable = Ast.fragments
val update : ?id:string -> ?export:bool -> ?readonly:bool -> ?local:bool -> t -> param:string -> variable -> (t, string) result * Trace.entry (** Update the state with a new parameter mapping and whether or not it should exported to the environment (default false), if it is readonly (default false).
If [local] is [false] then the variable is globally scoped (the default), otherwise it will be added to the current scope.
[id] can be used to group variables together (for example, all of the variable that belong to a particular function call or pipeline). *)
val remove : param:string -> t -> bool * t * Trace.entry (** [remove ~param t] removes [param] from [t] if it exists. [bool] is [true] if a removal took place. *)
val remove_group : id:string -> t -> t (** [remove_group ~id t] removes all variables with the [id] group. *)
val exports : t -> (string * string) list (** All of the variables that must be exported to the environment *)
val readonly : t -> (string * variable) list (** All of the variables that must be exported to the environment *)
val locals : t -> (string * variable) list (** All of the currently scoped local variables. *)
(** {2 Local variables} *)
val push : t -> t (** Push a new frame of local variables. *)
val pop : t -> t (** Pop local variables from the stack. *)
val equal : t -> t -> bool val pp_readonly : t Fmt.t val pp_export : t Fmt.t val dump : t Fmt.tend
(* Abstract file descriptors. *)module type Fd = sig type t
val close : t -> unit (** Closes the file descriptor, also removes any switch hooks attached to the FD. *)
val pp : t Fmt.t (** A pretty printer for file descriptors. *)
val dup : ?cloexec:bool -> sw:Eio.Switch.t -> t -> t (** Implements [dup(3p)] behaviour. *)
val dup2 : ?cloexec:bool -> sw:Eio.Switch.t -> src:t -> int -> t (** A potentially unsafe function that takes a raw integer to make a new FD from. If that FD is already open it is closed. *)
val from_resource : _ Eio.Std.r -> t option (** Extract a resource's underlying file descritor, if any. *)
val to_int : t -> int (** Translate a file descriptor to a number. *)
val of_int : close_underlying:bool -> sw:Eio.Switch.t -> int -> t (** [of_int ~sw i] creates a new FD from an integer, only use this if you know what you are doing. *)
val stdin : t (** Default stdin file descriptor. *)
val stdout : t (** Default stdout file descriptor. *)
val stderr : t (** Default stderr file descriptor. *)end
type packed_fd = Fd : ((module Fd with type t = 'a) * 'a) -> packed_fd
let make_packed_fd (type a) : (module Fd with type t = a) -> a -> packed_fd = fun (module Fd) fd -> Fd ((module Fd), fd)
type ('t, _, _) Eio.Resource.pi += | T : ('t, 't -> packed_fd, [> `Fd ]) Eio.Resource.pi
let fd (Eio.Resource.T (t, ops)) = Eio.Resource.get ops T tlet close_packed_fd (Fd ((module F), fd)) = F.close fd
type fd_opt = { fd : 'a. 'a Eio.Std.r -> packed_fd option }type 'fd redirect = Redirect of bool * int * 'fd | Close of 'fd
let redirect_jsont jsont_type_var__fd = let jsont__Close = Jsont.Object.Case.map "Close" (Jsont.Object.map ~kind:"Close" Stdlib.Fun.id |> Jsont.Object.mem "v" ~doc:"Wrapper for Close" jsont_type_var__fd ~enc:Fun.id |> Jsont.Object.finish) ~dec:(fun arg0 -> Close arg0) and jsont__Redirect = Jsont.Object.Case.map "Redirect" (Jsont.Object.map ~kind:"Redirect" Stdlib.Fun.id |> Jsont.Object.mem "v" ~doc:"Wrapper for Redirect" (let get_or_raise = function | Ok r -> r | Error err -> raise (Jsont.Error err) in let enc f acc (e0, e1, e2) = let e0 = Jsont.Json.encode' Jsont.bool e0 |> get_or_raise in let e1 = Jsont.Json.encode' Jsont.int e1 |> get_or_raise in let e2 = Jsont.Json.encode' jsont_type_var__fd e2 |> get_or_raise in [ (0, e0); (1, e1); (2, e2) ] |> Stdlib.List.fold_left (fun acc (i, e) -> f acc i e) acc in let dec_empty () = (None, None, None) in let dec_add i elt (e0, e1, e2) = match i with | 0 -> let e = Jsont.Json.decode' Jsont.bool elt |> get_or_raise in (Some e, e1, e2) | 1 -> let e = Jsont.Json.decode' Jsont.int elt |> get_or_raise in (e0, Some e, e2) | 2 -> let e = Jsont.Json.decode' jsont_type_var__fd elt |> get_or_raise in (e0, e1, Some e) | _ -> Jsont.Error.msgf Jsont.Meta.none "Too many elements for tuple." in let dec_finish meta _ (e0, e1, e2) = let get_or_raise i o = match o with | Some v -> v | None -> Jsont.Error.msgf meta "Missing tuple member #%i" i in (get_or_raise 0 e0, get_or_raise 1 e1, get_or_raise 2 e2) in Jsont.Array.map ~enc:{ enc } ~dec_empty ~dec_add ~dec_finish Jsont.json |> Jsont.Array.array) ~enc:Fun.id |> Jsont.Object.finish) ~dec:(fun (arg0, arg1, arg2) -> Redirect (arg0, arg1, arg2)) in Jsont.Object.map ~kind:"Redirect" Fun.id |> Jsont.Object.case_mem "type" ~doc:"Cases for Redirect" Jsont.string ~enc:Fun.id ~enc_case:(function | Close arg0 -> Jsont.Object.Case.value jsont__Close arg0 | Redirect (arg0, arg1, arg2) -> Jsont.Object.Case.value jsont__Redirect (arg0, arg1, arg2)) [ Jsont.Object.Case.make jsont__Close; Jsont.Object.Case.make jsont__Redirect; ] |> Jsont.Object.finish
let redirect ?(for_parent = false) (i, j) = Redirect (for_parent, i, j)
let pp_redirect ~pp ppf = function | Redirect (fp, i, fd) -> Fmt.pf ppf "%i <=%s=> %a" i (if fp then "P" else "C") pp fd | Close fd -> Fmt.pf ppf "close %a" pp fd
type exec_mode = | Switched of Eio.Switch.t | Async (** How to execute a process. This mainly controls what happens at the end of the running a script or some commands. When a process is "switched", we use the same semantics as Eio, we sigkill the process and cleanup. If the process is complete Async then we do not wait. This allows us to exit before some of our child processes, which is a requirement for implementing the semantics of a shell! *)
module type Exec = sig type t (** An executor for commands *)
type process (** A potentially running process. *)
type fd (** A type for file descriptors. *)
val pipe : sw:Eio.Switch.t -> t -> fd * fd (** [pipe ~sw t] creates a pair of connected flows which must support the abstract file descriptor interface. *)
val import_ro : sw:Eio.Switch.t -> close_unix:bool -> fd -> Eio.File.ro_ty Eio.Resource.t
val import_rw : sw:Eio.Switch.t -> close_unix:bool -> fd -> Eio.File.rw_ty Eio.Resource.t
val signal : process -> int -> unit (** Send a signal to a process. *)
val pid : process -> int (** Access the process's identifier. *)
val exec : ?delay_reap:unit Eio.Promise.t -> ?fork_actions:Eio_unix__.Fork_action.t list -> ?fds:fd redirect list -> ?stdin:_ Eio.Flow.source -> ?stdout:_ Eio.Flow.sink -> ?stderr:_ Eio.Flow.sink -> ?env:string array -> mode:exec_mode -> pgid:int -> cwd:Eio.Fs.dir_ty Eio.Path.t -> executable:string -> t -> string list -> (process, int) result (** Run a command in a child process *)
val await : process -> unit Exit.tend
module type Job = sig type 'a t (** A job for job control *)
type process
val get_reaper : _ t -> unit Eio.Promise.t * unit Eio.Promise.u val reap : _ t -> unit
val make : ?sigint:Sys.signal_behavior -> int -> [ `Built_in of 'a Exit.t Eio.Promise.or_exn | `Noop of 'a Exit.t | `Error of int | `Exit of 'a Exit.t | `Process of 'a * process | `Rdr of 'a Exit.t ] list -> 'a t
val pp : 'a t Fmt.t (** Pretty printer for jobs *)
val get_id : _ t -> int (** Get the ID of the job. *)
val set_id : int -> 'a t -> 'a t (** Set the ID of the job. *)
val set_sigint : Sys.signal_behavior -> 'a t -> 'a t
val get_sigint : _ t -> Sys.signal_behavior option (** Get the previous signal behaviour for sigint. *)
val add_process : 'a -> process -> 'a t -> 'a t val add_built_in : 'a Exit.t Eio.Promise.or_exn -> 'a t -> 'a t val add_error : int -> 'a t -> 'a t val add_rdr : 'a Exit.t -> 'a t -> 'a t val add_exit : 'a Exit.t -> 'a t -> 'a t val add_noop : 'a Exit.t -> 'a t -> 'a t
val size : _ t -> int (** Number of processes in this job *)
val await_exit : default:'a -> pipefail:bool -> interactive:bool -> 'a t -> 'a Exit.t list (** Given a job, [await_exit] will wait for the job to finish and return the exit based on the various options passed in.
The list is in reverse order of the Job itself, i.e., the head of the list is the last process/built-in to be added to the job. *)
val last_process : 'a t -> process option (** The last process in the job, if any. *)end
module type History = sig type t (** A history of commands *)
val empty : t (** The empty history *)
type entry (** A history entry, usually a command. *)
val make_entry : string -> entry (** Construct an entry given a command. *)
val add : entry -> t -> t (** Add an entry to the history. *)
val save : t -> _ Eio.Path.t -> unit (** [save t path] should save history [t] to [path]. *)
val load : _ Eio.Path.t -> t (** [load path] should try to read a history from [path]. *)
val history : command:string -> t -> t (** [history ~command t] should return some subset of [t] (perhaps all of [t]) based on the current [command] to be used for history searching. *)
val commands : t -> string list (** Converts your (perhaps richer) history to just a series of commands. *)
val pp : t Fmt.t (** A pretty printer for the commands *)end
module type Context = sig type fd (** File descriptors. *)
type state (** The shell state *)
type ctx (** A shell context. *)
val default_ctx : ctx (** A default context. *)
val set_for_hash : ctx -> ctx (** Resets some fields for stable hashing. *)
(** {2 Context accessors}
All accessors take an optional argument, [trace], which defaults to [true] specifying whether or not that particular access should also be traced. *)
type 'a trace = ?trace:bool -> 'a
val find_function : (name:string -> ctx -> Ast.compound_command option * ctx) trace (** [lookup_function ~name ctx] looks for the function called [name]. *)
val add_function : (name:string -> Ast.compound_command -> ctx -> ctx) trace (** [add_function ~name cc ctx] adds a function to the context. *)
val get_option : (Options.option -> ctx -> bool * ctx) trace (** [get_option opt ctx] returns the value of [opt]. *)
val set_option : (Options.option -> bool -> ctx -> ctx) trace (** [set_option opt b ctx] sets the value of [opt] to [b] in [ctx]. *)
val with_ctx : ?interactive:bool -> ?toplevel:bool -> ?subshell:bool -> ?state:state -> ?local_state:(string * Ast.fragments) list Merry__Stack.t -> ?fd_table:Fd_table.t -> ?options:Options.t -> ?last_background_process:string -> ?last_pipeline_status:int option -> ?exit_handler:Ast.complete_commands option -> ?program:string -> ?argv:string array -> ?functions:(string * Sast.compound_command) list -> ?hash:Hash.t -> ?rdrs:fd redirect list -> ?quotes:[ `Double | `None | `Single ] -> ?umask:int -> ?current_pipeline:string option -> ?current_context:[ `CompoundCommand | `Function | `Toplevel ] -> ?command:Ast.complete_commands -> ?commit:bool -> ?trace:Trace.t -> ?diff:Diff.t -> ?build:Zfs_store.Build.t -> ?overlays:Zfs_store.Build.t list -> ?mode:Void.mode -> ctx -> ctx (** [with_ctx ctx] will create a fresh context using any of the optional parameters in place of the value that the current ctx has. *)
type t = ctx list
(** {2 Accessors} *)
val mode : ctx -> Void.mode val overlays : ctx -> Zfs_store.Build.t list val build : ctx -> Zfs_store.Build.t val diff : ctx -> Diff.t val trace : ctx -> Trace.t val commit : ctx -> bool val command : ctx -> Ast.complete_commands val current_context : ctx -> [ `CompoundCommand | `Function | `Toplevel ] val current_pipeline : ctx -> string option val umask : ctx -> int val quotes : ctx -> [ `Double | `None | `Single ] val rdrs : ctx -> fd redirect list val hash : ctx -> Hash.t val functions : ctx -> (string * Sast.compound_command) list val argv : ctx -> string array val program : ctx -> string val exit_handler : ctx -> Ast.complete_commands option val last_pipeline_status : ctx -> int option val last_background_process : ctx -> string val options : ctx -> Options.t val fd_table : ctx -> Fd_table.t val local_state : ctx -> (string * Ast.fragments) list Merry__Stack.t val state : ctx -> state val subshell : ctx -> bool val toplevel : ctx -> bool val interactive : ctx -> bool
(** {2 Runtime representations *)
val t : t Repr.t (** Runtime representation of a full context. *)
val ctx_t : ctx Repr.t (** Runtime representation for each individual context. *)
val jsont : t Jsont.t (** Jsont representation of a full context. *)
val ctx_jsont : ctx Jsont.t (** Jsont representation of a context. *)
val merge : t option Irmin.Merge.t (** The merge function for contexts. *)
val equal : t -> t -> bool (** Equality over contexts, do not use [Stdlib.( = )]. *)
val dump : t Fmt.t (** Dump pretty-printer for contexts. *)end