|
| 1 | +# Set up the test environment |
| 2 | + |
| 3 | +```ocaml |
| 4 | +# #require "eio_luv";; |
| 5 | +# open Eio.Std;; |
| 6 | +# open Eio;; |
| 7 | +# module Process = Eio_luv.Low_level.Process;; |
| 8 | +module Process = Eio_luv.Low_level.Process |
| 9 | +``` |
| 10 | + |
| 11 | +A helper function for reading all of the bytes from a handle. |
| 12 | + |
| 13 | +```ocaml |
| 14 | +let read_all handle buf = |
| 15 | + let rec read acc = |
| 16 | + match Eio_luv.Low_level.Stream.read_into handle buf with |
| 17 | + | i -> read (acc + i) |
| 18 | + | exception End_of_file -> acc |
| 19 | + in read 0 |
| 20 | +``` |
| 21 | + |
| 22 | +A simple `echo hello` process redirects to stdout. |
| 23 | + |
| 24 | +```ocaml |
| 25 | +# Eio_luv.run @@ fun _env -> |
| 26 | + Switch.run @@ fun sw -> |
| 27 | + let redirect = Luv.Process.[ |
| 28 | + inherit_fd ~fd:stdout ~from_parent_fd:stdout () |
| 29 | + ] in |
| 30 | + let t = Process.spawn ~sw ~redirect "echo" [ "echo"; "hello" ] in |
| 31 | + Process.await_exit t;; |
| 32 | +hello |
| 33 | +- : int * int64 = (0, 0L) |
| 34 | +``` |
| 35 | + |
| 36 | +Using a pipe to redirect output to a buffer. |
| 37 | + |
| 38 | +```ocaml |
| 39 | +# Eio_luv.run @@ fun _env -> |
| 40 | + Switch.run @@ fun sw -> |
| 41 | + let parent_pipe = Eio_luv.Low_level.Pipe.init ~sw () in |
| 42 | + let buf = Luv.Buffer.create 32 in |
| 43 | + let redirect = Eio_luv.Low_level.Process.[ |
| 44 | + to_parent_pipe ~fd:Luv.Process.stdout ~parent_pipe () |
| 45 | + ] in |
| 46 | + let t = Process.spawn ~sw ~redirect "echo" [ "echo"; "Hello,"; "World!" ] in |
| 47 | + let read = read_all parent_pipe buf in |
| 48 | + let _ = Process.await_exit t in |
| 49 | + Luv.Buffer.to_string (Luv.Buffer.sub buf ~offset:0 ~length:read);; |
| 50 | +- : string = "Hello, World!\n" |
| 51 | +``` |
| 52 | + |
| 53 | +Writing to stdin of a process works. |
| 54 | + |
| 55 | +```ocaml |
| 56 | +# Eio_luv.run @@ fun _env -> |
| 57 | + Switch.run @@ fun sw -> |
| 58 | + let parent_pipe = Eio_luv.Low_level.Pipe.init ~sw () in |
| 59 | + let bufs = [ Luv.Buffer.from_string "Hello!" ] in |
| 60 | + let redirect = Luv.Process.[ |
| 61 | + inherit_fd ~fd:stdout ~from_parent_fd:stdout (); |
| 62 | + Process.to_parent_pipe ~fd:stdin ~parent_pipe () |
| 63 | + ] in |
| 64 | + let t = Process.spawn ~sw ~redirect "head" [ "head" ] in |
| 65 | + Eio_luv.Low_level.Stream.write parent_pipe bufs; |
| 66 | + Eio_luv.Low_level.Handle.close parent_pipe; |
| 67 | + Process.await_exit t;; |
| 68 | +Hello! |
| 69 | +- : int * int64 = (0, 0L) |
| 70 | +``` |
| 71 | + |
| 72 | +Stopping a process works. |
| 73 | + |
| 74 | +```ocaml |
| 75 | +# Eio_luv.run @@ fun _env -> |
| 76 | + Switch.run @@ fun sw -> |
| 77 | + let redirect = Luv.Process.[ |
| 78 | + inherit_fd ~fd:stdout ~from_parent_fd:stdout () |
| 79 | + ] in |
| 80 | + let t = Process.spawn ~sw ~redirect "sleep" [ "sleep"; "10" ] in |
| 81 | + Process.send_signal t Luv.Signal.sigkill; |
| 82 | + Process.await_exit t;; |
| 83 | +- : int * int64 = (9, 0L) |
| 84 | +``` |
| 85 | + |
| 86 | +Forgetting to wait for a process to finish stops the process. |
| 87 | + |
| 88 | +```ocaml |
| 89 | +# Eio_luv.run @@ fun _env -> |
| 90 | + let proc = |
| 91 | + Switch.run @@ fun sw -> |
| 92 | + let redirect = Luv.Process.[ |
| 93 | + inherit_fd ~fd:stdout ~from_parent_fd:stdout () |
| 94 | + ] in |
| 95 | + Process.spawn ~sw ~redirect "sleep" [ "sleep"; "10" ] |
| 96 | + in |
| 97 | + Process.await_exit proc;; |
| 98 | +- : int * int64 = (9, 0L) |
| 99 | +``` |
| 100 | + |
| 101 | +Stopping a process interacts nicely with switches. |
| 102 | + |
| 103 | +```ocaml |
| 104 | +# Eio_luv.run @@ fun _env -> |
| 105 | + let proc = |
| 106 | + Switch.run @@ fun sw -> |
| 107 | + let redirect = Luv.Process.[ |
| 108 | + inherit_fd ~fd:stdout ~from_parent_fd:stdout () |
| 109 | + ] in |
| 110 | + let t = Process.spawn ~sw ~redirect "sleep" [ "sleep"; "10" ] in |
| 111 | + Process.send_signal t Luv.Signal.sigkill; |
| 112 | + t |
| 113 | + in |
| 114 | + Process.await_exit proc;; |
| 115 | +- : int * int64 = (9, 0L) |
| 116 | +``` |
0 commit comments