···11(* Built-in Actions *)
22-type t = Cd of { path : string option } | Pwd
22+type t = Cd of { path : string option } | Pwd | Exit of int
3344(* Change Directory *)
55module Cd = struct
···3333 Cmd.v info term
3434end
35353636+(* Exit *)
3737+module Exit = struct
3838+ open Cmdliner
3939+4040+ let exit_code =
4141+ let doc = "Exit code." in
4242+ Arg.(value & pos 0 int 0 & info [] ~docv:"EXIT_CODE" ~doc)
4343+4444+ let t =
4545+ let make_exit n = Exit n in
4646+ let term = Term.(const make_exit $ exit_code) in
4747+ let info =
4848+ let doc = "Exit with a specified exit code (default is 0)" in
4949+ Cmd.info "exit" ~doc
5050+ in
5151+ Cmd.v info term
5252+end
5353+3654let of_args (w : string list) =
3755 let open Cmdliner in
3856 let exec_cmd cmd v =
···4563 match w with
4664 | "cd" :: _ as cmd -> exec_cmd cmd Cd.t
4765 | "pwd" :: _ as cmd -> exec_cmd cmd Pwd.t
6666+ | "exit" :: _ as cmd -> exec_cmd cmd Exit.t
4867 | _ -> None
+1
src/lib/built_ins.mli
···22 | Cd of { path : string option }
33 (** Change directory to a path (if [None] then it should be [HOME]) *)
44 | Pwd
55+ | Exit of int
5667val of_args : string list -> t option
78(** Parses a command-line to the built-ins *)
···2929 (Fpath.normalize @@ S.cwd state |> subst_tilde |> Fpath.to_string);
3030 Format.flush_str_formatter ()
31313232- let with_stdin_in_raw_mode fn =
3232+ let _with_stdin_in_raw_mode fn =
3333 let saved_tio = Unix.tcgetattr Unix.stdin in
3434 let tio =
3535 {
···6868 let rec loop (ctx : Eval.ctx Exit.t) =
6969 let p = prompt ctx in
7070 match LNoise.linenoise p with
7171- | None -> loop ctx
7171+ | None ->
7272+ Fmt.pr "exit\n%!";
7373+ exit 0
7274 | Some c ->
7375 let ast = Ast.of_string c in
7474- let ctx', _ast =
7575- with_stdin_in_raw_mode @@ fun () -> Eval.run ctx ast
7676- in
7676+ let ctx', _ast = Eval.run ctx ast in
7777+ (* TODO: Make better History abstraction *)
7878+ let _ : (unit, string) result = LNoise.history_add c in
7779 loop ctx'
8080+ | exception Sys.Break ->
8181+ let c = Exit.value ctx in
8282+ loop (Exit.nonzero c 130)
7883 in
7984 loop initial_ctx
8085end