Shells in OCaml
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add exit built-in

+54 -7
+20 -1
src/lib/built_ins.ml
··· 1 1 (* Built-in Actions *) 2 - type t = Cd of { path : string option } | Pwd 2 + type t = Cd of { path : string option } | Pwd | Exit of int 3 3 4 4 (* Change Directory *) 5 5 module Cd = struct ··· 33 33 Cmd.v info term 34 34 end 35 35 36 + (* Exit *) 37 + module Exit = struct 38 + open Cmdliner 39 + 40 + let exit_code = 41 + let doc = "Exit code." in 42 + Arg.(value & pos 0 int 0 & info [] ~docv:"EXIT_CODE" ~doc) 43 + 44 + let t = 45 + let make_exit n = Exit n in 46 + let term = Term.(const make_exit $ exit_code) in 47 + let info = 48 + let doc = "Exit with a specified exit code (default is 0)" in 49 + Cmd.info "exit" ~doc 50 + in 51 + Cmd.v info term 52 + end 53 + 36 54 let of_args (w : string list) = 37 55 let open Cmdliner in 38 56 let exec_cmd cmd v = ··· 45 63 match w with 46 64 | "cd" :: _ as cmd -> exec_cmd cmd Cd.t 47 65 | "pwd" :: _ as cmd -> exec_cmd cmd Pwd.t 66 + | "exit" :: _ as cmd -> exec_cmd cmd Exit.t 48 67 | _ -> None
+1
src/lib/built_ins.mli
··· 2 2 | Cd of { path : string option } 3 3 (** Change directory to a path (if [None] then it should be [HOME]) *) 4 4 | Pwd 5 + | Exit of int 5 6 6 7 val of_args : string list -> t option 7 8 (** Parses a command-line to the built-ins *)
+10 -1
src/lib/dune
··· 3 3 (public_name merry) 4 4 (preprocess 5 5 (pps ppx_deriving_yojson ppxlib.traverse)) 6 - (libraries eio eio.unix morbig yojson ppxlib linenoise fpath cmdliner)) 6 + (libraries 7 + eio 8 + eio.unix 9 + morbig 10 + yojson 11 + ppxlib 12 + linenoise 13 + fpath 14 + cmdliner 15 + globlon))
+5
src/lib/eval.ml
··· 195 195 | Pwd -> 196 196 Fmt.pr "%s\n%!" (Eunix.cwd ()); 197 197 Exit.zero ctx 198 + | Exit n -> 199 + let should_exit = 200 + { Exit.default_should_exit with interactive = `Yes } 201 + in 202 + Exit.nonzero_msg ~should_exit ctx ~exit_code:n "exit" 198 203 199 204 let cwd_of_ctx ctx = S.cwd ctx.state |> Fpath.to_string |> ( / ) ctx.fs 200 205
+10 -5
src/lib/interactive.ml
··· 29 29 (Fpath.normalize @@ S.cwd state |> subst_tilde |> Fpath.to_string); 30 30 Format.flush_str_formatter () 31 31 32 - let with_stdin_in_raw_mode fn = 32 + let _with_stdin_in_raw_mode fn = 33 33 let saved_tio = Unix.tcgetattr Unix.stdin in 34 34 let tio = 35 35 { ··· 68 68 let rec loop (ctx : Eval.ctx Exit.t) = 69 69 let p = prompt ctx in 70 70 match LNoise.linenoise p with 71 - | None -> loop ctx 71 + | None -> 72 + Fmt.pr "exit\n%!"; 73 + exit 0 72 74 | Some c -> 73 75 let ast = Ast.of_string c in 74 - let ctx', _ast = 75 - with_stdin_in_raw_mode @@ fun () -> Eval.run ctx ast 76 - in 76 + let ctx', _ast = Eval.run ctx ast in 77 + (* TODO: Make better History abstraction *) 78 + let _ : (unit, string) result = LNoise.history_add c in 77 79 loop ctx' 80 + | exception Sys.Break -> 81 + let c = Exit.value ctx in 82 + loop (Exit.nonzero c 130) 78 83 in 79 84 loop initial_ctx 80 85 end
+8
test/built_ins.t
··· 9 9 . 10 10 .. 11 11 12 + 2. Exit 13 + 14 + $ osh -c "exit 123" 15 + exit 16 + [123] 17 + 18 + $ osh -c "exit" 19 + exit