From 8933bf5f77417d557ad6e5b8d0caaccf29fcbc46 Mon Sep 17 00:00:00 2001 From: Patrick Ferris Date: Sun, 1 Feb 2026 00:40:55 +0100 Subject: [PATCH] Built-in redirection --- src/lib/eunix.ml | 2 +- src/lib/eval.ml | 10 ++++++++-- test/simple.t | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/lib/eunix.ml b/src/lib/eunix.ml index 99cf020..4afc2a0 100644 --- a/src/lib/eunix.ml +++ b/src/lib/eunix.ml @@ -48,7 +48,7 @@ let with_redirections (rdrs : Types.redirect list) fn = (function | Types.Redirect (i, fd, _) -> Eio_unix.Fd.use_exn "with_redirections" fd @@ fun fd -> - Unix.dup2 fd (fd_of_int i) + if (Obj.magic fd : int) <> i then Unix.dup2 fd (fd_of_int i) | Types.Close fd -> Eio_unix.Fd.close fd) rdrs; Fun.protect diff --git a/src/lib/eval.ml b/src/lib/eval.ml index b83d029..c2c4c7c 100644 --- a/src/lib/eval.ml +++ b/src/lib/eval.ml @@ -428,6 +428,9 @@ module Make (S : Types.State) (E : Types.Exec) = struct let ctx = handle_built_in ~rdrs ~stdout:some_write ctx bi in + let ctx = + ctx >|= fun ctx -> clear_local_state ctx + in close_stdout ~is_global some_write; let built_in = ctx >|= fun _ -> () in let job = handle_job job (`Built_in built_in) in @@ -479,6 +482,7 @@ module Make (S : Types.State) (E : Types.Exec) = struct loop ctx job some_read rest))))) | Some (Ok bi) -> let ctx = handle_built_in ~rdrs ~stdout:some_write ctx bi in + let ctx = ctx >|= fun ctx -> clear_local_state ctx in close_stdout ~is_global some_write; let built_in = ctx >|= fun _ -> () in let job = handle_job job (`Built_in built_in) in @@ -989,7 +993,10 @@ module Make (S : Types.State) (E : Types.Exec) = struct (ctx, []) swc and handle_built_in ~rdrs ~(stdout : Eio_unix.sink_ty Eio.Flow.sink) - (ctx : ctx) = function + (ctx : ctx) v = + let rdrs = ctx.rdrs @ rdrs in + Eunix.with_redirections rdrs @@ fun () -> + match v with | Built_ins.Cd { path } -> let cwd = S.cwd ctx.state in let+ state = @@ -1009,7 +1016,6 @@ module Make (S : Types.State) (E : Types.Exec) = struct { ctx with state } | Pwd -> let () = - Eunix.with_redirections rdrs @@ fun () -> Eio.Flow.copy_string (Fmt.str "%a\n%!" Fpath.pp (S.cwd ctx.state)) stdout diff --git a/test/simple.t b/test/simple.t index f4635e6..39e7e4c 100644 --- a/test/simple.t +++ b/test/simple.t @@ -179,7 +179,7 @@ Simple appending example Redirection of fds - $ msh -c "echo hello 3>out.txt >&3" + $ msh -c "echo hello 789>out.txt >&789" $ cat out.txt hello -- 2.51.2