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