Something went wrong. Try again.
Shells in OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370open Importmodule VariableMap = Map.Make (String)module VariableSet = Set.Make (String)
type attributes = { readonly : bool; id : string }
let attributes_jsont = let make readonly id = { readonly; id } in Jsont.Object.map ~kind:"Attributes" make |> Jsont.Object.mem "readonly" Jsont.bool ~enc:(fun t -> t.readonly) |> Jsont.Object.mem "id" Jsont.string ~enc:(fun t -> t.id) |> Jsont.Object.finish
type 'a with_attr = { attr : attributes; value : 'a }
let with_attr_jsont jsont_type_var__a = let make attr value = { attr; value } in Jsont.Object.map ~kind:"With_attr" make |> Jsont.Object.mem "attr" attributes_jsont ~enc:(fun t -> t.attr) |> Jsont.Object.mem "value" jsont_type_var__a ~enc:(fun t -> t.value) |> Jsont.Object.finish
let make_with_attr attr value = { attr; value }let default_attribute = { readonly = false; id = "id" }
type scopes = Ast.fragments with_attr VariableMap.t Stack.t
let scopes_jsont : scopes Jsont.t = Stack.jsont (VariableMap.jsont (with_attr_jsont Ast.fragments_jsont))
type t = { cwd : Fpath.t; root : int; outermost : bool; home : string; variables : Ast.fragments with_attr VariableMap.t; exported : VariableSet.t; path : string list with_attr option; scopes : scopes;}
let empty = { cwd = Fpath.v "/"; root = 0; outermost = true; home = ""; variables = VariableMap.empty; exported = VariableSet.empty; path = None; scopes = Stack.empty; }
let jsont = let make cwd root outermost home variables exported path scopes = { cwd; root; outermost; home; variables; exported; path; scopes } in Jsont.Object.map ~kind:"T" make |> Jsont.Object.mem "cwd" Fpath.jsont ~enc:(fun t -> t.cwd) |> Jsont.Object.mem "root" Jsont.int ~enc:(fun t -> t.root) |> Jsont.Object.mem "outermost" Jsont.bool ~enc:(fun t -> t.outermost) |> Jsont.Object.mem "home" Jsont.string ~enc:(fun t -> t.home) |> Jsont.Object.mem "variables" (VariableMap.jsont (with_attr_jsont Ast.fragments_jsont)) ~enc:(fun t -> t.variables) |> Jsont.Object.mem "exported" VariableSet.jsont ~enc:(fun t -> t.exported) |> Jsont.Object.mem "path" (Jsont.option (with_attr_jsont (Jsont.list Jsont.string))) ~enc:(fun t -> t.path) |> Jsont.Object.mem "scopes" scopes_jsont ~enc:(fun t -> t.scopes) |> Jsont.Object.finish
let t = repr_of_jsont jsont
let path_as_variable t = match t.path with | None -> [] | Some { value = path; _ } -> [ Ast.Fragment.make (String.concat ":" path) ]
let path_attr t = match t.path with Some { attr; _ } -> attr | _ -> default_attribute
let all_variables t = VariableMap.add "PATH" (make_with_attr (path_attr t) (path_as_variable t)) t.variables
type variable = Ast.fragments
let pp_attr ppf (attr : attributes) = Fmt.pf ppf "{ readonly = %b; id = %a }" attr.readonly Fmt.(quote string) attr.id
let pp_with_attr pp ppf { attr; value } = Fmt.pf ppf "{ value = %a; attr = %a }" pp value pp_attr attr
let pp_variable ppf (k, v) = Fmt.pf ppf "%s=%a" k (pp_with_attr Import.Fmt.(lst Ast.Fragment.pp)) v
let pp_variables ppf vs = Fmt.(lst pp_variable) ppf (VariableMap.to_list vs)
let update ?(id = "") ?(export = false) ?(readonly = false) ?(local = false) t ~param (v : Ast.fragments) = let add_non_local param = (* First check if the variable has been defined locally *) let is_local scopes = match Stack.pop scopes with | Some (h, rest) -> ( match VariableMap.find_opt param h with | Some v -> Some (v, h, rest) | None -> None) | None -> None in match is_local t.scopes with | Some ({ attr; _ }, vs, rest) -> let variables' = VariableMap.add param (make_with_attr attr v) vs in Ok { t with scopes = Stack.push variables' rest } | None -> ( match VariableMap.find_opt param t.variables with | Some { attr = { readonly = true; _ }; _ } -> Error (Fmt.str "%s: readonly variable" param) | _ -> ( match param with | "PATH" -> let attr = { readonly; id } in let path = Ast.Fragment.join_list ~sep:"" v |> String.split_on_char ':' in Debug.Log.info (fun f -> f "Updating PATH to: %a" Fmt.(lst string) path); Ok { t with path = Some { attr; value = path } } | _ -> let already_exported = VariableSet.mem param t.exported in let exported = if already_exported && export then t.exported else if export then VariableSet.add param t.exported else t.exported in let attr = { readonly; id } in let variables' = VariableMap.add param (make_with_attr attr v) t.variables in Ok { t with exported; variables = variables' })) in let add_local param = let attr = default_attribute in match Stack.pop t.scopes with | Some (vs, rest) -> let variables' = VariableMap.add param (make_with_attr attr v) vs in Ok { t with scopes = Stack.push variables' rest } | None -> let variables' = VariableMap.add param (make_with_attr attr v) VariableMap.empty in Ok { t with scopes = Stack.push variables' t.scopes } in let trace = Trace.make_entry ~arg:param Env Write in if local then (add_local param, trace) else (add_non_local param, trace)
let seed_env () = let env = Eunix.env () in List.fold_left (fun vars (param, v) -> let v = [ Ast.Fragment.make v ] in VariableMap.add param (make_with_attr default_attribute v) vars) VariableMap.empty env
let make ?(root = 0) ?(outermost = true) ?(home = "/root") ?variables path cwd = let exported_vars = match variables with None -> seed_env () | Some v -> v in let exported = VariableMap.fold (fun param _ acc -> match param with "PATH" -> acc | _ -> VariableSet.add param acc) exported_vars VariableSet.empty in let path, exported_vars = match VariableMap.find_opt "PATH" exported_vars with | Some { value = path; _ } -> ( Some (Ast.Fragment.join_list ~sep:"" path |> String.split_on_char ':'), VariableMap.remove "PATH" exported_vars ) | None -> (path, exported_vars) in { cwd; root; outermost; home; exported; variables = exported_vars; scopes = Stack.empty; path = Option.map (fun v -> make_with_attr default_attribute v) path; }
let pop t = match Stack.pop t.scopes with | None -> t | Some (_, scopes) -> { t with scopes }
let push t = let scopes = Stack.push VariableMap.empty t.scopes in { t with scopes }
let cwd t = t.cwdlet set_cwd t cwd = { t with cwd }let expand t = function `Tilde -> t.homelet path t = t.path |> Option.map (fun t -> t.value)
let lookup_variables param t = match param with | "PATH" -> (Some (path_as_variable t), Trace.make_entry ~arg:"PATH" Env Read) | _ -> ( let trace = Trace.make_entry ~arg:param Env Read in match VariableMap.find_opt param t.variables with | None -> (None, trace) | Some { value; _ } -> (Some value, trace))
let lookup t ~param = match Stack.peek t.scopes with | None -> lookup_variables param t | Some vs -> ( let locals = VariableMap.find_opt param vs |> Option.map (fun t -> t.value) in match locals with | Some _ as v -> (v, Trace.make_entry ~arg:param Env Read) | None -> lookup_variables param t)
let remove ~param t = let trace k = Trace.make_entry ~arg:param Env k in match param with | "PATH" -> (true, { t with path = None }, trace Delete) | _ -> ( match VariableMap.find_opt param t.variables with | None -> (false, t, trace Read) | Some _ -> let exported = VariableSet.remove param t.exported in ( true, { t with exported; variables = VariableMap.remove param t.variables; }, trace Delete ))
let remove_group ~id t = let variables, removed_params = VariableMap.fold (fun param { attr = { id = id'; _ }; _ } (vs, ps) -> if String.equal id id' then begin (VariableMap.remove param vs, param :: ps) end else begin (vs, ps) end) t.variables (t.variables, []) in let exported = List.fold_left (fun acc p -> VariableSet.remove p acc) t.exported removed_params in { t with variables; exported }
let exports t = let variables = VariableSet.fold (fun param acc -> (param, VariableMap.find param t.variables) :: acc) t.exported [] in let rest = variables |> List.map (fun (k, { value; _ }) -> (k, Ast.Fragment.join_list ~sep:"" value)) in match path t with | None -> rest | Some p -> ("PATH", String.concat ":" p) :: rest
let locals t = match Stack.pop t.scopes with | None -> [] | Some (vars, _) -> VariableMap.to_list vars |> List.map (fun (n, t) -> (n, t.value))
let readonly t = VariableMap.to_list (all_variables t) |> List.filter_map (function | p, { attr = { readonly = true; _ }; value } -> Some (p, value) | _ -> None)
let pp_readonly fmt t = let rs = readonly t in let rs = List.map (fun (p, cst) -> ("readonly " ^ p, Ast.Fragment.join_list ~sep:"" cst)) rs in Fmt.(list ~sep:(Fmt.any "\n") (pair ~sep:(Fmt.any "=") string (quote string))) fmt rs
let pp_export fmt t = let rs = exports t in let rs = List.map (fun (p, cst) -> ("export " ^ p, cst)) rs in Fmt.(list ~sep:(Fmt.any "\n") (pair ~sep:(Fmt.any "=") string (quote string))) fmt rs
let dump ppf t = Fmt.braces (Fmt.record [ Fmt.field "variables" (fun t -> VariableMap.to_list t.variables) Fmt.(lst pp_variable); Fmt.field "root" (fun t -> t.root) Fmt.int; Fmt.field "exported" (fun t -> VariableSet.to_list t.exported) Fmt.(lst string); Fmt.field "outermost" (fun t -> t.outermost) Fmt.bool; Fmt.field "cwd" (fun t -> t.cwd) Fpath.pp; Fmt.field "home" (fun t -> t.home) Fmt.string; Fmt.field "path" (fun t -> t.path) Fmt.(option (pp_with_attr (lst string))); Fmt.field "scopes" (fun t -> t.scopes) (Stack.pp pp_variables); ]) ppf t
let equal_with_attr eq a b = a.attr = b.attr && eq a.value b.value
let equal_scopes a b = List.equal Stdlib.( = ) (Stack.to_list a) (Stack.to_list b)
let equal : t -> t -> Ppx_deriving_runtime.bool = let __6 = equal_scopes and __5 = equal_with_attr and __4 = VariableSet.equal and __3 = VariableMap.equal and __2 = equal_with_attr and __1 = Stdlib.( = ) and __0 = Fpath.equal in fun lhs rhs -> ((((((__0 lhs.cwd rhs.cwd && (fun (a : int) b -> a = b) lhs.root rhs.root) && (fun (a : bool) b -> a = b) lhs.outermost rhs.outermost) && (fun (a : string) b -> a = b) lhs.home rhs.home) && (__3 (__2 __1)) lhs.variables rhs.variables) && __4 lhs.exported rhs.exported) && (fun x y -> match (x, y) with | None, None -> true | Some a, Some b -> (__5 (let rec loop x y = match (x, y) with | [], [] -> true | a :: x, b :: y -> (fun (a : string) b -> a = b) a b && loop x y | _ -> false in fun x y -> loop x y)) a b | _ -> false) lhs.path rhs.path) && __6 lhs.scopes rhs.scopes
let merge ~old:_ _ v2 = Ok v2let merge = Irmin.Merge.option @@ Irmin.Merge.v t merge