]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitacLib.ml
Missing initialization of the trusting function (for the kernel).
[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   CicEnvironment.set_trust (* environment trust *)
141     (let trust = Helm_registry.get_bool "matita.environment_trust" in
142      fun _ -> trust)
143   status := Some (ref (Lazy.force MatitaEngine.initial_status));
144   Sys.catch_break true;
145   interactive_loop ()
146
147 let dump_moo_to_file file moo =
148  let os = open_out (MatitaMisc.obj_file_of_script file) in
149  let output s = output_string os s in
150  output "(* GENERATED FILE: DO NOT EDIT! *)\n\n";
151  List.iter output (List.rev moo);
152  close_out os
153   
154 let main ~mode = 
155   Helm_registry.load_from BuildTimeConf.matita_conf;
156   CicNotation.load_notation BuildTimeConf.core_notation_script;
157   Http_getter.init ();
158   MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
159   MatitaDb.create_owner_environment ();
160   CicEnvironment.set_trust (* environment trust *)
161     (let trust = Helm_registry.get_bool "matita.environment_trust" in
162      fun _ -> trust)
163   status := Some (ref (Lazy.force MatitaEngine.initial_status));
164   Sys.catch_break true;
165   let origcb = MatitaLog.get_log_callback () in
166   let newcb tag s =
167         match tag with
168         | `Debug | `Message -> ()
169         | `Warning | `Error -> origcb tag s
170   in
171   let fname = fname () in
172   if !quiet_compilation then
173     MatitaLog.set_log_callback newcb;
174   try
175     let time = Unix.time () in
176     if !quiet_compilation then
177       origcb `Message ("compiling " ^ Filename.basename fname ^ "...")
178     else
179       MatitaLog.message (sprintf "execution of %s started:" fname);
180     let is =
181       Stream.of_channel
182         (match fname with
183         | "stdin" -> stdin
184         | fname -> open_in fname)
185     in
186     run_script is 
187       (MatitaEngine.eval_from_stream 
188         ~include_paths:!paths_to_search_in
189         ~clean_baseuri:(not !dont_delete_baseuri));
190     let elapsed = Unix.time () -. time in
191     let tm = Unix.gmtime elapsed in
192     let sec = 
193       if tm.Unix.tm_sec > 0 then  (string_of_int tm.Unix.tm_sec ^ "''") else "" 
194     in
195     let min = 
196       if tm.Unix.tm_min > 0 then  (string_of_int tm.Unix.tm_min ^ "' ") else "" 
197     in
198     let hou = 
199       if tm.Unix.tm_hour > 0 then (string_of_int tm.Unix.tm_hour ^ "h ") else "" 
200     in
201     let proof_status,moo_content_rev = 
202       match !status with
203       | Some s -> !s.proof_status, !s.moo_content_rev
204       | None -> assert false
205     in
206     if proof_status <> MatitaTypes.No_proof then
207      begin
208       MatitaLog.error
209        "there are still incomplete proofs at the end of the script";
210       clean_exit (Some 2)
211      end
212     else
213      begin
214        dump_moo_to_file fname moo_content_rev;
215        MatitaLog.message 
216          (sprintf "execution of %s completed in %s." fname (hou^min^sec));
217        exit 0
218      end
219   with 
220   | Sys.Break ->
221       MatitaLog.error "user break!";
222       if mode = `COMPILER then
223         clean_exit (Some ~-1)
224       else
225         pp_ocaml_mode ()
226   | MatitaEngine.Drop ->
227       if mode = `COMPILER then 
228         clean_exit (Some 1)
229       else 
230         pp_ocaml_mode ()
231   | CicNotationParser.Parse_error (floc,err) ->
232      let (x, y) = CicNotationPt.loc_of_floc floc in
233      MatitaLog.error (sprintf "Parse error at %d-%d: %s" x y err);
234      if mode = `COMPILER then
235        clean_exit (Some 1)
236      else
237        pp_ocaml_mode ()
238   | _ ->
239      if mode = `COMPILER then 
240        clean_exit (Some 3)
241      else 
242        pp_ocaml_mode ()