Shells in OCaml
3
fork

Configure Feed

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

Fix EIO, tcsetpgrp and better error messages

+53 -14
+19 -11
src/lib/ast.ml
··· 724 724 Yojson.Safe.pretty_print ppf yjs 725 725 end 726 726 727 - let of_string s = 728 - let f = Morbig.parse_string "-" s in 729 - (* Fmt.pr "MORBIG====\n%!"; *) 730 - (* Morbig.JsonHelpers.save_as_json true Out_channel.stdout f; *) 731 - (* Fmt.pr "MORBIG====\n%!"; *) 727 + let () = 728 + Printexc.register_printer (function 729 + | Morbig.Errors.DuringParsing pos -> 730 + Some 731 + (Fmt.str "Error during parsing (potentially non-POSIX): %s" 732 + (Morbig.string_of_lexing_position pos)) 733 + | Morbig.Errors.DuringLexing (pos, s) -> 734 + Some 735 + (Fmt.str "Error during lexing of \"%s\": %s" s 736 + (Morbig.string_of_lexing_position pos)) 737 + | Morbig.Errors.DuringAliasing (pos, s) -> 738 + Some 739 + (Fmt.str "Error during aliasing of \"%s\": %s" s 740 + (Morbig.string_of_lexing_position pos)) 741 + | _ -> None) 742 + 743 + let of_string ?(filename = "-") s = 744 + let f = Morbig.parse_string filename s in 732 745 of_program f 733 746 734 747 let of_file path = 735 748 let fname = Eio.Path.native_exn path in 736 - 737 - let f = Eio.Path.load path |> Morbig.parse_string fname in 738 - (* Fmt.pr "MORBIG====\n%!"; *) 739 - (* Morbig.JsonHelpers.save_as_json true Out_channel.stdout f; *) 740 - (* Fmt.pr "MORBIG====\n%!"; *) 741 - of_program f 749 + Eio.Path.load path |> of_string ~filename:fname 742 750 743 751 let rec word_component_to_string : word_component -> string = function 744 752 | WordName s -> s
+1 -1
src/lib/ast.mli
··· 9 9 val of_program : Morbig.CST.program -> t 10 10 (** An AST from a Morbig program *) 11 11 12 - val of_string : string -> t 12 + val of_string : ?filename:string -> string -> t 13 13 (** Construct an AST from a string *) 14 14 15 15 val of_file : _ Eio.Path.t -> t
+12 -1
src/lib/eunix.ml
··· 17 17 let host = Unix.gethostname () in 18 18 Fmt.str "%s@%s" name host 19 19 20 + external getpgrp : unit -> int = "caml_merry_getpgrp" 21 + external tcgetpgrp : Unix.file_descr -> int = "caml_merry_tcgetpgrp" 20 22 external tcsetpgrp : int -> int -> int = "caml_merry_tcsetpgrp" 21 23 22 24 let delegate_control ~pgid fn = 23 25 let shell_pid = Unix.getpid () in 24 26 Fun.protect 25 27 ~finally:(fun () -> 28 + Fmt.pr "Back to the shell\n%!"; 26 29 let _ : int = tcsetpgrp 0 shell_pid in 27 30 ()) 28 31 (fun () -> 32 + Fmt.pr "Delegating...\n%!"; 29 33 match tcsetpgrp (Obj.magic Unix.stdin : int) pgid with 30 34 | 0 -> fn () 31 35 | n -> Fmt.failwith "tcsetpgrp: %i" n) 32 36 33 37 external setpgrp : int -> int -> int = "caml_merry_setpgid" 34 38 35 - let make_process_group () = match setpgrp 0 0 with 0 -> () | n -> exit n 39 + let make_process_group () = 40 + let pgrp = getpgrp () in 41 + let fg_prgp = tcgetpgrp Unix.stdin in 42 + if Int.equal pgrp fg_prgp then 43 + match setpgrp 0 0 with 44 + | 0 -> () 45 + | _ -> assert false 46 + | exception Unix.Unix_error (Unix.EPERM, _, _) -> () 36 47 37 48 let background () = 38 49 let _pgrid = Unix.getpid () in
+1 -1
src/lib/eval.ml
··· 1244 1244 1245 1245 and run ctx ast = 1246 1246 (* Make the shell its own process group *) 1247 - (* Eunix.make_process_group (); *) 1247 + Eunix.make_process_group (); 1248 1248 let ctx, cs = 1249 1249 let rec loop_commands (ctx, cs) (c : Ast.complete_commands) = 1250 1250 match c with
+20
src/lib/merry_stubs.c
··· 2 2 #include <caml/memory.h> 3 3 #include <caml/alloc.h> 4 4 #include <caml/fail.h> 5 + #include <caml/unixsupport.h> 5 6 6 7 #include <unistd.h> 7 8 #include <signal.h> ··· 14 15 res = tcsetpgrp(Int_val(v_fd), Long_val(v_pid_t)); 15 16 16 17 CAMLreturn(Val_int(res)); 18 + } 19 + 20 + value caml_merry_tcgetpgrp(value v_fd) { 21 + CAMLparam1(v_fd); 22 + int res; 23 + 24 + res = tcgetpgrp(Long_val(v_fd)); 25 + 26 + CAMLreturn(Val_long(res)); 17 27 } 18 28 19 29 value caml_merry_setpgid(value v_fd, value v_pid_t) { ··· 21 31 int res; 22 32 23 33 res = setpgid(Long_val(v_fd), Long_val(v_pid_t)); 34 + 35 + if (res == -1) caml_uerror("setpgid", Nothing); 24 36 25 37 CAMLreturn(Val_int(res)); 26 38 } 27 39 40 + value caml_merry_getpgrp (value v_unit) { 41 + CAMLparam1(v_unit); 42 + int res; 43 + 44 + res = getpgrp(); 45 + 46 + CAMLreturn(Val_long(res)); 47 + }