let run_and_open s kont =
  let ic = Unix.open_process_in s in
  let close () =
    match Unix.close_process_in ic with
    | Unix.WEXITED 0 -> ()
    | Unix.WEXITED _ | Unix.WSIGNALED _ | Unix.WSTOPPED _ ->
        failwith (Printf.sprintf "Error while running: %s" s) in
  let res = try
      kont ic
    with e -> (close (); raise e)
  in close (); res