Shells in OCaml

Built-in redirection

+10 -4
+1 -1
src/lib/eunix.ml
··· 48 48 (function 49 49 | Types.Redirect (i, fd, _) -> 50 50 Eio_unix.Fd.use_exn "with_redirections" fd @@ fun fd -> 51 - Unix.dup2 fd (fd_of_int i) 51 + if (Obj.magic fd : int) <> i then Unix.dup2 fd (fd_of_int i) 52 52 | Types.Close fd -> Eio_unix.Fd.close fd) 53 53 rdrs; 54 54 Fun.protect
+8 -2
src/lib/eval.ml
··· 428 428 let ctx = 429 429 handle_built_in ~rdrs ~stdout:some_write ctx bi 430 430 in 431 + let ctx = 432 + ctx >|= fun ctx -> clear_local_state ctx 433 + in 431 434 close_stdout ~is_global some_write; 432 435 let built_in = ctx >|= fun _ -> () in 433 436 let job = handle_job job (`Built_in built_in) in ··· 479 482 loop ctx job some_read rest))))) 480 483 | Some (Ok bi) -> 481 484 let ctx = handle_built_in ~rdrs ~stdout:some_write ctx bi in 485 + let ctx = ctx >|= fun ctx -> clear_local_state ctx in 482 486 close_stdout ~is_global some_write; 483 487 let built_in = ctx >|= fun _ -> () in 484 488 let job = handle_job job (`Built_in built_in) in ··· 989 993 (ctx, []) swc 990 994 991 995 and handle_built_in ~rdrs ~(stdout : Eio_unix.sink_ty Eio.Flow.sink) 992 - (ctx : ctx) = function 996 + (ctx : ctx) v = 997 + let rdrs = ctx.rdrs @ rdrs in 998 + Eunix.with_redirections rdrs @@ fun () -> 999 + match v with 993 1000 | Built_ins.Cd { path } -> 994 1001 let cwd = S.cwd ctx.state in 995 1002 let+ state = ··· 1009 1016 { ctx with state } 1010 1017 | Pwd -> 1011 1018 let () = 1012 - Eunix.with_redirections rdrs @@ fun () -> 1013 1019 Eio.Flow.copy_string 1014 1020 (Fmt.str "%a\n%!" Fpath.pp (S.cwd ctx.state)) 1015 1021 stdout
+1 -1
test/simple.t
··· 179 179 180 180 Redirection of fds 181 181 182 - $ msh -c "echo hello 3>out.txt >&3" 182 + $ msh -c "echo hello 789>out.txt >&789" 183 183 $ cat out.txt 184 184 hello 185 185