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