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

3
4
let dump = ref None
let src  = ref []
5

6
7
let specs =
  [ "-dump", Arg.String (fun s -> dump := Some s), " specify filename for persistency" ]
8

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
let () =
  Arg.parse specs (fun s -> src := s :: !src) 
    "cduce [options] [script]\n\nOptions:"

let ppf = Format.std_formatter

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
  let ok = Cduce.run ppf input in
  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 ()