]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacLib.ml
* new binary matitatop
[helm.git] / helm / matita / matitacLib.ml
1 (* Copyright (C) 2004-2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 open MatitaTypes
29
30 (** {2 Initialization} *)
31
32 let arg_spec = [
33 (*   "-opt", Arg...., "set bla bla bla"; *)
34 ]
35 let usage =
36   sprintf "MatitaC v%s\nUsage: matitac [option ...] file\nOptions:"
37     BuildTimeConf.version
38
39 let _ =
40   Helm_registry.load_from "matita.conf.xml";
41   Http_getter.init ();
42   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
43   MatitaDb.clean_owner_environment ();
44   MatitaDb.create_owner_environment ()
45
46 let status = ref (Lazy.force MatitaEngine.initial_status) ;;
47
48 let run_script is eval_function  =
49   let slash_n_RE = Pcre.regexp "\\n" in
50   let cb status stm = 
51     (* dump_status status; *)
52     let stm = TacticAstPp.pp_statement stm in
53     let stm = Pcre.replace ~rex:slash_n_RE stm in
54     let stm = 
55       if String.length stm > 50 then
56         String.sub stm 0 50 ^ " ..."
57       else
58         stm
59     in
60     MatitaLog.debug ("Executing: ``" ^ stm ^ "''")
61   in
62   try
63     eval_function status is cb
64   with
65   | MatitaEngine.Drop  
66   | CicTextualParser2.Parse_error _ as exn -> raise exn
67   | exn -> 
68       MatitaLog.error (Printexc.to_string exn);
69       raise exn
70
71 let fname () =
72   let acc = ref [] in
73   let add_script fname = acc := fname :: !acc in
74   Arg.parse arg_spec add_script usage;
75   match !acc with
76   | [x] -> x
77   | _ -> prerr_endline usage; exit 1
78
79 let pp_ocaml_mode () = 
80   MatitaLog.message "";
81   MatitaLog.message " ** Entering Ocaml mode ** ";
82   MatitaLog.message ""
83   
84 let rec go () = 
85   try
86     run_script (Stream.of_channel stdin) MatitaEngine.eval_from_stream_greedy
87   with 
88   | MatitaEngine.Drop -> pp_ocaml_mode ()
89   | Sys.Break -> MatitaLog.error "user break!"; go ()
90   | MatitaTypes.Command_error _ -> go ()
91   | CicTextualParser2.Parse_error (floc,err) ->
92      let (x, y) = CicAst.loc_of_floc floc in
93      MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
94      go ()
95   | exn -> MatitaLog.error (Printexc.to_string exn); go ()
96   
97 let main ~mode = 
98   at_exit
99     (fun () ->
100        Http_getter_logger.log "Sync map tree to disk...";
101        Http_getter.sync_dump_file ();
102        print_endline "\nThanks for using Matita!\n");
103   Sys.catch_break true;
104   let fname = fname () in
105   try
106     let time = Unix.time () in
107     MatitaLog.message (sprintf "execution of %s started:" fname);
108     let is =
109       Stream.of_channel
110         (match fname with
111         | "stdin" -> stdin
112         | fname -> open_in fname)
113     in
114     run_script is MatitaEngine.eval_from_stream;
115     let elapsed = Unix.time () -. time in
116     let tm = Unix.gmtime elapsed in
117     let sec = 
118       if tm.Unix.tm_sec > 0 then  (string_of_int tm.Unix.tm_sec ^ "''") else "" 
119     in
120     let min = 
121       if tm.Unix.tm_min > 0 then  (string_of_int tm.Unix.tm_min ^ "' ") else "" 
122     in
123     let hou = 
124       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else "" 
125     in
126     let proof_status = !status.proof_status in
127     if proof_status <> MatitaTypes.No_proof then
128      begin
129       MatitaLog.error
130        "there are still incomplete proofs at the end of the script";
131       exit 2
132      end
133     else
134      begin
135        MatitaLog.message 
136          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
137        exit 0
138      end
139   with 
140   | Sys.Break -> MatitaLog.error "user break!"; exit ~-1
141   | MatitaEngine.Drop ->
142       if mode = `COMPILER then 
143         exit 1 
144       else 
145         pp_ocaml_mode ()
146   | CicTextualParser2.Parse_error (floc,err) ->
147      let (x, y) = CicAst.loc_of_floc floc in
148      MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
149      Http_getter.sync_dump_file ();
150      exit 1
151