| 1 |
let input_channel = |
let () = State.close ();; |
|
match Array.length Sys.argv with |
|
|
| 1 -> Location.set_source `Stream; stdin |
|
|
| 2 -> let fn = Sys.argv.(1) in Location.set_source (`File fn); open_in fn |
|
|
| _ -> Printf.eprintf "Usage: cduce [script]\n"; exit 2 |
|
|
in |
|
|
let input = Stream.of_channel input_channel |
|
|
and ppf = Format.std_formatter in |
|
|
Cduce.run ppf input |
|
| 2 |
|
|
| 3 |
|
let dump = ref None |
| 4 |
|
let src = ref [] |
| 5 |
|
|
| 6 |
|
let specs = |
| 7 |
|
[ "-dump", Arg.String (fun s -> dump := Some s), " specify filename for persistency" ] |
| 8 |
|
|
| 9 |
|
let () = |
| 10 |
|
Arg.parse specs (fun s -> src := s :: !src) |
| 11 |
|
"cduce [options] [script]\n\nOptions:" |
| 12 |
|
|
| 13 |
|
let ppf = Format.std_formatter |
| 14 |
|
|
| 15 |
|
let do_file s = |
| 16 |
|
let (src, chan) = |
| 17 |
|
if s = "" then (`Stream, stdin) else (`File s, open_in s) in |
| 18 |
|
Location.set_source src; |
| 19 |
|
let input = Stream.of_channel chan in |
| 20 |
|
let ok = Cduce.run ppf input in |
| 21 |
|
if s <> "" then close_in chan; |
| 22 |
|
if not ok then exit 1 |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
let main () = |
| 27 |
|
(match !dump with |
| 28 |
|
| Some f -> |
| 29 |
|
(try |
| 30 |
|
Format.fprintf ppf "Restoring state: "; |
| 31 |
|
let chan = open_in_bin f in |
| 32 |
|
let s = Marshal.from_channel chan in |
| 33 |
|
close_in chan; |
| 34 |
|
State.set s; |
| 35 |
|
Format.fprintf ppf "done ...@." |
| 36 |
|
with Sys_error _ -> |
| 37 |
|
Format.fprintf ppf "failed ...@.") |
| 38 |
|
| None -> ()); |
| 39 |
|
(match !src with |
| 40 |
|
| [] -> |
| 41 |
|
Format.fprintf ppf "No script specified; using stdin ...@."; |
| 42 |
|
do_file "" |
| 43 |
|
| l -> List.iter do_file l); |
| 44 |
|
(match !dump with |
| 45 |
|
| Some f -> |
| 46 |
|
Format.fprintf ppf "Saving state ...@\n"; |
| 47 |
|
let s = State.get () in |
| 48 |
|
let chan = open_out_bin f in |
| 49 |
|
Marshal.to_channel chan s [ Marshal.Closures ]; |
| 50 |
|
close_out chan |
| 51 |
|
| None -> ()) |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
let () = main () |