I'm trying to build a new frontend in Ocaml for a terminal based application. The main idea is the spawn a new process with Lwt:
let cmd = shell "./otherterminalapp" in
let p = open_process_full cmd;
And then later write stuff to the process' stdin, to execute commands in the external app.
Lwt_io.write_line p#stdin "some command" >>= (fun _ -> Lwt_io.flush p#stdin)
When I read the result from the command back in with Lwt_io.read_line_opt. How do I read till there aren't any lines left?
The problem I'm encountering is that my program just hangs at a certain point. When I read with read_line_opt, while I reached the end it seems like it's just waiting for the process to redirect new output.
How can I approach this?
A concrete example of what I'm trying to do: (The terminal based application is ocamldebug)
Program source code:
open Lwt
open Lwt_unix
open Lwt_process
let () =
let run () =
let cmd = shell "ocamldebug test.d.byte" in
let dbgr = open_process_full cmd in
(((((((Lwt_io.write_line dbgr#stdin "info modules") >>=
(fun _ -> Lwt_io.flush dbgr#stdin))
>>= (fun _ -> Lwt_io.read_line_opt dbgr#stdout))
>>=
(fun s ->
(match s with
| Some l -> print_endline l
| None -> print_endline "nothing here! ");
Lwt_io.read_line_opt dbgr#stdout))
>>=
(fun s ->
(match s with
| Some l -> print_endline l
| None -> print_endline "nothing here! ");
Lwt_io.read_line_opt dbgr#stdout))
>>=
(fun s ->
(match s with
| Some l -> print_endline l
| None -> print_endline "nothing here! ");
Lwt_io.read_line_opt dbgr#stdout))
>>=
(fun s ->
(match s with
| Some l -> print_endline l
| None -> print_endline "nothing here! ");
Lwt_io.read_line_opt dbgr#stdout))
>>=
(fun s ->
(match s with
| Some l -> print_endline l
| None -> print_endline "nothing here! ");
Lwt.return ()) in
Lwt_main.run (run ())
If you would normally run ocamldebug with test.d.byte, you get the
following in your terminal:
OCaml Debugger version 4.03.0
(ocd) info modules
Loading program... done.
Used modules:
Std_exit Test Pervasives CamlinternalFormatBasics
(ocd)
When I execute the above program, I get the following printed:
OCaml Debugger version 4.03.0
(ocd) Loading program... Used modules:
Std_exit Test Pervasives CamlinternalFormatBasics
And here it just hangs..., my program doesn't exit. Even when I do Ctrl-c/Ctrl-c in my terminal, there's an active ocamlrun process. The terminal however becomes responsive though.
I am missing something obvious here?
read_line_optfor a while, then it doesn't "work" anymore? In what sense? When you say the "end," are you referring to the end of one line, the end of the output from the other process, or something else? Does the other process actually exit or close its output stream? - antron