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