~ubuntu-branches/ubuntu/oneiric/menhir/oneiric

« back to all changes in this revision

Viewing changes to src/IO.ml

  • Committer: Bazaar Package Importer
  • Author(s): Mehdi Dogguy
  • Date: 2009-02-22 23:41:17 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090222234117-yxk115kvzv634utx
Tags: 20090204.dfsg-2
* New binary package libmenhir-ocaml-dev, Closes: #516134.
* Use dh-ocaml predefined variables.
* Use predefined variable OCAML_BEST (dh-ocaml >= 0.4).
* debian/svn-deblayout: remove no longer needed SVN setting

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(**************************************************************************)
 
2
(*                                                                        *)
 
3
(*  Menhir                                                                *)
 
4
(*                                                                        *)
 
5
(*  Fran�ois Pottier, INRIA Rocquencourt                                  *)
 
6
(*  Yann R�gis-Gianas, PPS, Universit� Paris Diderot                      *)
 
7
(*                                                                        *)
 
8
(*  Copyright 2005-2008 Institut National de Recherche en Informatique    *)
 
9
(*  et en Automatique. All rights reserved. This file is distributed      *)
 
10
(*  under the terms of the Q Public License version 1.0, with the change  *)
 
11
(*  described in file LICENSE.                                            *)
 
12
(*                                                                        *)
 
13
(**************************************************************************)
 
14
 
 
15
(* Input-output utilities. *)
 
16
 
 
17
(* ------------------------------------------------------------------------- *)
 
18
(* [exhaust channel] reads all of the data that's available on [channel]. *)
 
19
 
 
20
let chunk_size =
 
21
  2048
 
22
 
 
23
let exhaust channel =
 
24
  let buffer = Buffer.create chunk_size in
 
25
  let chunk = String.create chunk_size in
 
26
  let rec loop () =
 
27
    let length = input channel chunk 0 chunk_size in
 
28
    if length = 0 then
 
29
      Buffer.contents buffer
 
30
    else begin
 
31
      Buffer.add_substring buffer chunk 0 length;
 
32
      loop()
 
33
    end
 
34
  in
 
35
  loop()
 
36
 
 
37
(* ------------------------------------------------------------------------- *)
 
38
(* [invoke command] invokes an external command (which expects no
 
39
   input) and returns its output, if the command succeeds. It returns
 
40
   [None] if the command fails. *)
 
41
 
 
42
let invoke command =
 
43
  let ic = Unix.open_process_in command in
 
44
  let result = exhaust ic in
 
45
  match Unix.close_process_in ic with
 
46
  | Unix.WEXITED 0 ->
 
47
      Some result
 
48
  | _ ->
 
49
      None
 
50
 
 
51
(* ------------------------------------------------------------------------- *)
 
52
(* [winvoke writers command cleaners] invokes each of the [writer]
 
53
   functions, invokes the command [command], and runs each of the
 
54
   [cleaner] functions. Then, it either returns the command's output,
 
55
   if the command succeeded, or exits, otherwise. *)
 
56
 
 
57
let winvoke writers command cleaners =
 
58
  let call action =
 
59
    action ()
 
60
  in
 
61
  List.iter call writers;
 
62
  let output = invoke command in
 
63
  List.iter call cleaners;
 
64
 
 
65
  (* Stop if the command failed. Otherwise, return its output. *)
 
66
 
 
67
  match output with
 
68
  | None ->
 
69
      (* Presumably, the command printed an error message for us. *)
 
70
      exit 1
 
71
  | Some output ->
 
72
      output
 
73