Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Unix.open_process "cat"
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin_jambon@e...>
Subject: [Caml-list] Unix.open_process "cat"
Hello,

I would like to apply some external filters (e.g. cat, grep, sed, ...)
that will terminate successfully only if they reach an
end-of-file. For this I would like to use Unix.open_process, but I need to
close the output first (= the input of the command).
Is it legal to use close_out on the out_channel returned by
Unix.open_process, and later use Unix.close_process on the pair of
channels?

Here is my code (which seems to work):

#load "unix.cma"

let feed command data f =
  let (ic, oc) as channels = Unix.open_process command in
  output_string oc data;
  close_out oc;  (** <-- is this OK? **)
  (try
     while true do
       f (input_char ic)
     done
   with End_of_file -> ());
  match Unix.close_process channels with
      Unix.WEXITED 0 -> ()
    | _ -> invalid_arg ("feed: " ^ command)

let _ = feed "cat" "mouse" print_char



Martin

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners