]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacLib.ml
all initialization code is now in the new matitaInit.ml module.
[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   (** If set to true matitac wont catch top level exception, so that backtrace
33    * could be inspected setting OCAMLRUNPARAM=b.  This flag is enabled passing
34    * -debug *)
35 let debug = ref false
36 let paths_to_search_in = ref [];;
37 let quiet_compilation = ref false;;
38 let dont_delete_baseuri = ref false;;
39
40 let add_l l = fun s -> l := s :: !l ;;
41
42 let arg_spec =
43   let std_arg_spec = [
44     "-I", Arg.String (add_l paths_to_search_in), 
45       "<path> Adds path to the list of searched paths for the include command";
46     "-q", Arg.Set quiet_compilation, "Turn off verbose compilation";
47     "-preserve", Arg.Set dont_delete_baseuri,
48       "Turns off automatic baseuri cleaning";
49   ] in
50   let debug_arg_spec =
51     if BuildTimeConf.debug then
52       [ "-debug", Arg.Set debug,
53         "Do not catch top-level exception (useful for backtrace inspection)"; ]
54     else []
55   in
56   std_arg_spec @ debug_arg_spec
57
58 let usage =
59   sprintf "MatitaC v%s\nUsage: matitac [option ...] file\nOptions:"
60     BuildTimeConf.version
61
62 let status = ref None
63
64 let run_script is eval_function  =
65   let status = 
66     match !status with
67     | None -> assert false
68     | Some s -> s
69   in
70   let slash_n_RE = Pcre.regexp "\\n" in
71   let cb = 
72     if !quiet_compilation then 
73       fun _ _ -> () 
74     else 
75       fun status stm ->
76         (* dump_status status; *)
77         let stm = GrafiteAstPp.pp_statement stm in
78         let stm = Pcre.replace ~rex:slash_n_RE stm in
79         let stm = 
80           if String.length stm > 50 then
81             String.sub stm 0 50 ^ " ..."
82           else
83             stm
84         in
85         MatitaLog.debug ("Executing: ``" ^ stm ^ "''")
86   in
87   try
88     eval_function status is cb
89   with
90   | MatitaEngine.Drop  
91   | End_of_file
92   | CicNotationParser.Parse_error _ as exn -> raise exn
93   | exn -> 
94       MatitaLog.error (MatitaExcPp.to_string exn);
95       raise exn
96
97 let fname () =
98   let acc = ref [] in
99   let add_script fname = acc := fname :: !acc in
100   Arg.parse arg_spec add_script usage;
101   match !acc with
102   | [x] -> x
103   | _ -> print_endline usage; exit 1
104
105 let pp_ocaml_mode () = 
106   MatitaLog.message "";
107   MatitaLog.message "                      ** Entering Ocaml mode ** ";
108   MatitaLog.message "";
109   MatitaLog.message "Type 'go ();;' to enter an interactive matitac";
110   MatitaLog.message ""
111   
112 let clean_exit n =
113  let opt_exit =
114   function
115      None -> ()
116    | Some n -> exit n
117  in
118   match !status with
119      None -> opt_exit n
120    | Some status ->
121       try
122        let baseuri = MatitaTypes.get_string_option !status "baseuri" in
123        MatitacleanLib.clean_baseuris ~verbose:false [baseuri];
124        opt_exit n
125       with MatitaTypes.Option_error("baseuri", "not found") ->
126        (* no baseuri ==> nothing to clean yet *)
127        opt_exit n
128   
129 let rec interactive_loop () = 
130  let str = Stream.of_channel stdin in
131   try
132     run_script str 
133       (MatitaEngine.eval_from_stream_greedy ~include_paths:!paths_to_search_in)
134   with 
135   | MatitaEngine.Drop -> pp_ocaml_mode ()
136   | Sys.Break -> MatitaLog.error "user break!"; interactive_loop ()
137   | MatitaTypes.Command_error _ -> interactive_loop ()
138   | End_of_file ->
139      print_newline ();
140      clean_exit (Some 0)
141   | CicNotationParser.Parse_error (floc,err) ->
142      let (x, y) = CicNotationPt.loc_of_floc floc in
143      MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
144      interactive_loop ()
145   | exn -> MatitaLog.error (Printexc.to_string exn); interactive_loop ()
146
147 let go () =
148   Helm_registry.load_from BuildTimeConf.matita_conf;
149   CicNotation.load_notation BuildTimeConf.core_notation_script;
150   Http_getter.init ();
151   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
152   MatitaDb.create_owner_environment ();
153   CicEnvironment.set_trust (* environment trust *)
154     (let trust = Helm_registry.get_bool "matita.environment_trust" in
155      fun _ -> trust);
156   status := Some (ref (Lazy.force MatitaEngine.initial_status));
157   Sys.catch_break true;
158   interactive_loop ()
159
160 let dump_moo_to_file file moo =
161  let os = open_out (MatitaMisc.obj_file_of_script file) in
162  let output s = output_string os s in
163  output "(* GENERATED FILE: DO NOT EDIT! *)\n\n";
164  List.iter
165   (fun cmd -> output (GrafiteAstPp.pp_command cmd ^ "\n"))
166   (List.rev moo);
167  close_out os
168   
169 let main ~mode = 
170   MatitaInit.initialize_all ();
171   status := Some (ref (Lazy.force MatitaEngine.initial_status));
172   Sys.catch_break true;
173   let origcb = MatitaLog.get_log_callback () in
174   let newcb tag s =
175         match tag with
176         | `Debug | `Message -> ()
177         | `Warning | `Error -> origcb tag s
178   in
179   let fname = fname () in
180   if !quiet_compilation then
181     MatitaLog.set_log_callback newcb;
182   try
183     let time = Unix.time () in
184     if !quiet_compilation then
185       origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
186     else
187       MatitaLog.message (sprintf "execution of %s started:" fname);
188     let is =
189       Stream.of_channel
190         (match fname with
191         | "stdin" -> stdin
192         | fname -> open_in fname)
193     in
194     run_script is 
195       (MatitaEngine.eval_from_stream 
196         ~include_paths:!paths_to_search_in
197         ~clean_baseuri:(not !dont_delete_baseuri));
198     let elapsed = Unix.time () -. time in
199     let tm = Unix.gmtime elapsed in
200     let sec = 
201       if tm.Unix.tm_sec > 0 then (string_of_int tm.Unix.tm_sec ^ "''") else "" 
202     in
203     let min = 
204       if tm.Unix.tm_min > 0 then (string_of_int tm.Unix.tm_min ^ "' ") else "" 
205     in
206     let hou = 
207       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else ""
208     in
209     let proof_status,moo_content_rev = 
210       match !status with
211       | Some s -> !s.proof_status, !s.moo_content_rev
212       | None -> assert false
213     in
214     if proof_status <> MatitaTypes.No_proof then
215      begin
216       MatitaLog.error
217        "there are still incomplete proofs at the end of the script";
218       clean_exit (Some 2)
219      end
220     else
221      begin
222        dump_moo_to_file fname moo_content_rev;
223        MatitaLog.message 
224          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
225        exit 0
226      end
227   with 
228   | Sys.Break ->
229       MatitaLog.error "user break!";
230       if mode = `COMPILER then
231         clean_exit (Some ~-1)
232       else
233         pp_ocaml_mode ()
234   | MatitaEngine.Drop ->
235       if mode = `COMPILER then 
236         clean_exit (Some 1)
237       else 
238         pp_ocaml_mode ()
239   | CicNotationParser.Parse_error (floc,err) ->
240      let (x, y) = CicNotationPt.loc_of_floc floc in
241      MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
242      if mode = `COMPILER then
243        clean_exit (Some 1)
244      else
245        pp_ocaml_mode ()
246   | exn ->
247       if !debug then raise exn;
248       if mode = `COMPILER then 
249         clean_exit (Some 3)
250       else 
251         pp_ocaml_mode ()
252