run.ml 1.48 KB
Newer Older
1
let () = State.close ();;
2

3
4
let dump = ref None
let src  = ref []
5
let quiet = ref false
6

7
let specs =
8
9
10
11
12
  [ "-dump", Arg.String (fun s -> dump := Some s), 
    " specify filename for persistency";
    "-quiet", Arg.Set quiet, 
    "suppress normal output (typing, results)"
 ]
13

14
15
16
17
let () =
  Arg.parse specs (fun s -> src := s :: !src) 
    "cduce [options] [script]\n\nOptions:"

18
19
20
21
let ppf = 
  if !quiet then Format.formatter_of_buffer (Buffer.create 1023)
  else Format.std_formatter
let ppf_err = Format.err_formatter
22
23
24
25
26
27

let do_file s =
  let (src, chan) = 
    if s = "" then (`Stream, stdin) else (`File s, open_in s) in
  Location.set_source src;
  let input = Stream.of_channel chan in
28
  let ok = Cduce.run ppf ppf_err input in
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  if s <> "" then close_in chan;
  if not ok then exit 1

  

let main () =
  (match !dump with
     | Some f ->
	 (try 
	    Format.fprintf ppf "Restoring state: ";
	    let chan = open_in_bin f in
	    let s = Marshal.from_channel chan in
	    close_in chan;
	    State.set s;
	    Format.fprintf ppf "done ...@."
	  with Sys_error _ ->  
	    Format.fprintf ppf "failed ...@.")
     | None -> ());
  (match !src with
     | [] -> 
	 Format.fprintf ppf "No script specified; using stdin ...@.";
	 do_file ""
     | l -> List.iter do_file l);
  (match !dump with
     | Some f ->
	 Format.fprintf ppf "Saving state ...@\n";
	 let s = State.get () in
	 let chan = open_out_bin f in
	 Marshal.to_channel chan s [ Marshal.Closures ];
	 close_out chan
     | None -> ())




let () = main ()