m8ta
You are not authenticated, login. |
|
{590} | ||
It is not obvious how to run an external command in ocaml & get it's output from stdin. Here is my hack, which simply polls the output of the program until there is nothing left to read. Not very highly tested, but I wanted to share, as I don't think there is an example of the same on pleac let run_command cmd = let inch = Unix.open_process_in cmd in let infd = Unix.descr_of_in_channel inch in let buf = String.create 20000 in let il = ref 1 in let offset = ref 0 in while !il > 0 do ( let inlen = Unix.read infd buf !offset (20000- !offset) in il := inlen ; offset := !offset + inlen; ) done; ignore(Unix.close_process_in inch); if !offset = 0 then "" else String.sub buf 0 !offset ;; Note: Fixed a nasty string-termination/memory-reuse bug Sept 10 2008 |