@@ -301,27 +301,50 @@ let process proc : Eio.Process.t = object
301301 method signal i = Process. signal proc i
302302end
303303
304- let pipe_or_fd flow =
305- match Eio.Generic. probe flow FD with
306- | None -> assert false
307- | Some fd -> FD. to_rcfd fd
304+ let read_of_fd ~sw t =
305+ match get_fd_opt t with
306+ | None ->
307+ let r, w = Low_level. pipe ~sw in
308+ Some (flow w), r
309+ | Some fd -> None , fd
310+
311+ let write_of_fd ~sw t =
312+ match get_fd_opt t with
313+ | None ->
314+ let r, w = Low_level. pipe ~sw in
315+ Some (flow r), w
316+ | Some fd -> None , fd
308317
309318let process_mgr = object
310- method spawn ~sw ?cwd ~stdin ~stdout ~stderr prog args =
319+ inherit Eio.Process. mgr
320+
321+ method spawn ~sw ?cwd ~(stdin : #Eio.Flow.source ) ~(stdout : #Eio.Flow.sink ) ~(stderr : #Eio.Flow.sink ) prog args =
311322 let chdir = Option. to_list cwd |> List. map (fun (_ , s ) -> Process.Fork_action. chdir s) in
312- let stdin = pipe_or_fd stdin in
313- let stdout = pipe_or_fd stdout in
314- let stderr = pipe_or_fd stderr in
323+ let stdin_w, stdin_fd = read_of_fd ~sw stdin in
324+ let stdout_r, stdout_fd = write_of_fd ~sw stdout in
325+ let stderr_r, stderr_fd = write_of_fd ~sw stderr in
315326 let actions = Process.Fork_action. [
316327 Eio_unix.Private.Fork_action. inherit_fds [
317- 0 , stdin , `Blocking ;
318- 1 , stdout , `Blocking ;
319- 2 , stderr , `Blocking ;
328+ 0 , Fd. to_rcfd stdin_fd , `Blocking ;
329+ 1 , Fd. to_rcfd stdout_fd , `Blocking ;
330+ 2 , Fd. to_rcfd stderr_fd , `Blocking ;
320331 ];
321332 execve prog ~argv: (Array. of_list args) ~env: [||]
322333 ] in
323334 let actions = chdir @ actions in
324- process (Process. spawn ~sw actions)
335+ let proc = process (Process. spawn ~sw actions) in
336+ Option. iter (fun stdin_w ->
337+ Eio.Fiber. fork ~sw (fun () ->
338+ Eio.Flow. copy stdin stdin_w;
339+ Eio.Flow. close stdin_w
340+ )) stdin_w;
341+ Option. iter (fun stdout_r ->
342+ Fd. close stdout_fd;
343+ Eio.Fiber. fork ~sw (fun () -> Eio.Flow. copy stdout_r stdout)) stdout_r;
344+ Option. iter (fun stderr_r ->
345+ Fd. close stderr_fd;
346+ Eio.Fiber. fork ~sw (fun () -> Eio.Flow. copy stderr_r stdout)) stderr_r;
347+ proc
325348end
326349
327350type stdenv = <
0 commit comments