]> matita.cs.unibo.it Git - helm.git/blob - matita/matitaInit.ml
beginning to see the light
[helm.git] / matita / matitaInit.ml
1 (* Copyright (C) 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 (* $Id$ *)
27
28 type thingsToInitilaize = 
29   ConfigurationFile | Db | Environment | Getter | CmdLine | Registry
30   
31 exception FailedToInitialize of thingsToInitilaize
32
33 let wants s l = 
34   List.iter (
35     fun item -> 
36       if not (List.exists (fun x -> x = item) l) then
37         raise (FailedToInitialize item)) 
38   s
39
40 let already_configured s l =
41   List.for_all (fun item -> List.exists (fun x -> x = item) l) s
42   
43 let conffile = ref BuildTimeConf.matita_conf
44
45 let registry_defaults = [
46   "matita.debug",                "false";
47   "matita.external_editor",      "gvim -f -c 'go %p' %f";
48   "matita.profile",              "true";
49   "matita.system",               "false";
50   "matita.verbose",              "false";
51   "matita.paste_unicode_as_tex", "false";
52   "matita.noinnertypes",         "false";
53   "matita.do_heavy_checks",      "true";
54   "matita.moo",                  "true";
55 ]
56
57 let set_registry_values =
58   List.iter 
59     (fun key, value -> 
60        if not (Helm_registry.has key) then Helm_registry.set ~key ~value)
61
62 let fill_registry init_status =
63   if not (already_configured [ Registry ] init_status) then begin
64     set_registry_values registry_defaults;
65     Registry :: init_status
66   end else
67     init_status
68
69 let load_configuration init_status =
70   if not (already_configured [ConfigurationFile] init_status) then
71     begin
72       Helm_registry.load_from !conffile;
73       if not (Helm_registry.has "user.name") then begin
74         let login = (Unix.getpwuid (Unix.getuid ())).Unix.pw_name in
75         Helm_registry.set "user.name" login
76       end;
77       let home = Helm_registry.get_string "matita.basedir" in
78       let user_conf_file = home ^ "/matita.conf.xml" in
79       if HExtlib.is_regular user_conf_file then
80         begin
81           HLog.message ("Loading additional conf file from " ^ user_conf_file);
82           try
83             Helm_registry.load_from user_conf_file
84           with exn -> 
85             HLog.error
86               ("While loading conf file: " ^ snd (MatitaExcPp.to_string exn))
87         end;
88       ConfigurationFile::init_status 
89     end
90   else
91     init_status
92
93 let initialize_db init_status = 
94   wants [ ConfigurationFile; CmdLine ] init_status;
95   if not (already_configured [ Db ] init_status) then
96     begin
97       if not (Helm_registry.get_bool "matita.system") then
98         MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
99       LibraryDb.create_owner_environment ();
100       Db::init_status
101     end
102   else
103     init_status
104
105 let initialize_environment init_status = 
106   wants [CmdLine] init_status;
107   if not (already_configured [Getter;Environment] init_status) then
108     begin
109       Http_getter.init ();
110       if Helm_registry.get_bool "matita.system" then
111         Http_getter_storage.activate_system_mode ();
112       CicEnvironment.set_trust (* environment trust *)
113         (let trust =
114           Helm_registry.get_opt_default Helm_registry.get_bool
115             ~default:true "matita.environment_trust" in
116          fun _ -> trust);
117       Getter::Environment::init_status
118     end
119   else
120     init_status 
121   
122 let status = ref []
123
124 let usages = Hashtbl.create 11 (** app name (e.g. "matitac") -> usage string *)
125 let _ =
126   List.iter
127     (fun (name, s) -> Hashtbl.replace usages name s)
128     [ "matitac", 
129         Printf.sprintf "Matita batch compiler v%s
130 Usage: matitac [ OPTION ... ] FILE
131 Options:"
132           BuildTimeConf.version;
133         "matitaprover",
134         Printf.sprintf "Matita (TPTP) prover v%s
135 Usage: matitaprover [ -tptppath ] FILE.p
136 Options:"
137           BuildTimeConf.version;
138       "matita",
139         Printf.sprintf "Matita interactive theorem prover v%s
140 Usage: matita [ OPTION ... ] [ FILE ]
141 Options:"
142           BuildTimeConf.version;
143       "matitadep",
144         Printf.sprintf "Matita depency file generator v%s
145 Usage: matitadep [ OPTION ... ] 
146 Options:"
147           BuildTimeConf.version;
148       "matitaclean",
149         Printf.sprintf "MatitaClean v%s
150 Usage: matitaclean all
151        matitaclean ( FILE | URI )
152 Options:"
153           BuildTimeConf.version;
154     ]
155 let default_usage =
156   Printf.sprintf 
157     "Matita v%s\nUsage: matita [ ARG ]\nOptions:" BuildTimeConf.version
158
159 let usage () =
160   let basename = Filename.basename Sys.argv.(0) in
161   let usage_key =
162     try Filename.chop_extension basename with Invalid_argument  _ -> basename
163   in
164   try Hashtbl.find usages usage_key with Not_found -> default_usage
165 ;;
166
167 let dump f =
168    let module G = GrafiteAst in
169    let module L = LexiconAst in
170    let module H = HExtlib in
171    Helm_registry.set_bool "matita.moo" false;
172    let floc = H.dummy_floc in
173    let nl_ast = G.Comment (floc, G.Note (floc, "")) in
174    let och = open_out f in
175    let atexit () = close_out och in
176    let out_comment och s =
177       let s = if s <> "" && s.[0] = '*' then "#" ^ s else s in 
178       Printf.fprintf och "%s%s%s\n\n" "(*" s "*)"
179    in
180    let out_line_comment och s =
181       let l = 70 - String.length s in
182       let s = Printf.sprintf " %s %s" s (String.make l '*') in
183       out_comment och s
184    in
185    let out_preamble och (path, lines) =
186       let ich = open_in path in
187       let rec print i =
188          if i > 0 then 
189             let s = input_line ich in
190             begin Printf.fprintf och "%s\n" s; print (pred i) end
191       in 
192       print lines;
193       out_line_comment och "This file was automatically generated: do not edit"
194    in
195    let pp_ast_statement st =
196      GrafiteAstPp.pp_statement ~term_pp:CicNotationPp.pp_term
197        ~map_unicode_to_tex:(Helm_registry.get_bool
198          "matita.paste_unicode_as_tex")
199        ~lazy_term_pp:CicNotationPp.pp_term 
200        ~obj_pp:(CicNotationPp.pp_obj CicNotationPp.pp_term) st
201    in
202    let nl () =  output_string och (pp_ast_statement nl_ast) in
203    let rt_base_dir = Filename.dirname Sys.argv.(0) in
204    let path = Filename.concat rt_base_dir "matita.ma.templ" in
205    let lines = 14 in
206    out_preamble och (path, lines);
207    let grafite_parser_cb fname = 
208       let ast = G.Executable 
209         (floc, G.Command (floc, G.Include (floc, fname))) in
210       output_string och (pp_ast_statement ast); nl (); nl ()
211    in
212    let matita_engine_cb = function
213       | G.Executable (_, G.Macro (_, G.Inline _)) 
214       | G.Executable (_, G.Command (_, G.Include _)) -> ()
215       | ast                                          ->
216          output_string och (pp_ast_statement ast); nl (); nl ()
217    in
218    let matitac_lib_cb = output_string och in
219    GrafiteParser.set_callback grafite_parser_cb;
220    MatitaEngine.set_callback matita_engine_cb;
221    MatitacLib.set_callback matitac_lib_cb;
222    at_exit atexit
223 ;;
224
225 let extra_cmdline_specs = ref []
226 let add_cmdline_spec l = extra_cmdline_specs := l @ !extra_cmdline_specs
227
228 let parse_cmdline init_status =
229   if not (already_configured [CmdLine] init_status) then begin
230     wants [Registry] init_status;
231     let includes = ref [] in
232     let default_includes = [ 
233       BuildTimeConf.stdlib_dir_devel;
234       BuildTimeConf.stdlib_dir_installed ; ] 
235     in
236     let absolutize s =
237       if Pcre.pmatch ~pat:"^/" s then s else Sys.getcwd () ^"/"^s
238     in
239     let args = ref [] in
240     let add_l l = fun s -> l := s :: !l in
241     let print_version () =
242             Printf.printf "%s\n" BuildTimeConf.version;exit 0 in
243     let no_innertypes () =
244       Helm_registry.set_bool "matita.noinnertypes" true in
245     let set_baseuri s =
246       match Str.split (Str.regexp "::") s with
247       | [path; uri] -> Helm_registry.set "matita.baseuri"
248           (HExtlib.normalize_path (absolutize path)^" "^uri)
249       | _ -> raise (Failure "bad baseuri, use -b 'path::uri'")
250     in
251     let arg_spec =
252       let std_arg_spec = [
253         "-b", Arg.String set_baseuri, "<path::uri> forces the baseuri of path";
254         "-I", Arg.String (add_l includes),
255           ("<path> Adds path to the list of searched paths for the "
256            ^ "include command");
257         "-conffile", Arg.Set_string conffile,
258           (Printf.sprintf ("<filename> Read configuration from filename"
259              ^^ "\n    Default: %s")
260             BuildTimeConf.matita_conf);
261         "-force",
262             Arg.Unit (fun () -> Helm_registry.set_bool "matita.force" true),
263             ("Force actions that would not be executed per default");
264         "-noprofile", 
265           Arg.Unit (fun () -> Helm_registry.set_bool "matita.profile" false),
266           "Turns off profiling printings";
267         "-noinnertypes", Arg.Unit no_innertypes, 
268           "Turns off inner types generation while publishing";
269         "-profile-only",
270           Arg.String (fun rex -> Helm_registry.set "matita.profile_only" rex),
271           "Activates only profiler with label matching the provided regex";
272         "-system", Arg.Unit (fun () ->
273               Helm_registry.set_bool "matita.system" true),
274             ("Act on the system library instead of the user one"
275              ^ "\n    WARNING: not for the casual user");
276         "-dump", Arg.String dump,
277           "<filename> Dump with expanded macros to <filename>";
278         "-v", 
279           Arg.Unit (fun () -> Helm_registry.set_bool "matita.verbose" true), 
280           "Verbose mode";
281         "--version", Arg.Unit print_version, "Prints version";
282       ] in
283       let debug_arg_spec =
284         if BuildTimeConf.debug then
285           [ "-debug",
286             Arg.Unit (fun () -> Helm_registry.set_bool "matita.debug" true),
287               ("Do not catch top-level exception "
288               ^ "(useful for backtrace inspection)");
289             "-onepass",
290             Arg.Unit (fun () -> GrafiteDisambiguator.only_one_pass := true),
291             "Enable only one disambiguation pass";    
292           ]
293         else []
294       in
295       std_arg_spec @ debug_arg_spec @ !extra_cmdline_specs
296     in
297     let set_list ~key l =
298       Helm_registry.set_list Helm_registry.of_string ~key ~value:l
299     in
300     Arg.parse arg_spec (add_l args) (usage ());
301     let includes = 
302       List.map (fun x -> HExtlib.normalize_path (absolutize x)) 
303        ((List.rev !includes) @ default_includes) 
304     in
305     set_list ~key:"matita.includes" includes;
306     let args = List.rev (List.filter (fun x -> x <> "") !args) in
307     set_list ~key:"matita.args" args;
308     HExtlib.set_profiling_printings 
309       (fun s -> 
310         Helm_registry.get_bool "matita.profile" && 
311         Pcre.pmatch 
312           ~pat:(Helm_registry.get_opt_default 
313                   Helm_registry.string ~default:".*" "matita.profile_only") 
314           s);
315     CmdLine :: init_status
316   end else
317     init_status
318
319 let die_usage () =
320   print_endline (usage ());
321   exit 1
322
323 let conf_components = 
324   [ load_configuration; fill_registry; parse_cmdline]
325
326 let other_components =
327   [ initialize_db; initialize_environment ]
328
329 let initialize_all () =
330   status := 
331     List.fold_left (fun s f -> f s) !status
332     (conf_components @ other_components)
333
334 let parse_cmdline_and_configuration_file () =
335   status := List.fold_left (fun s f -> f s) !status conf_components
336
337 let initialize_environment () =
338   status := initialize_environment !status
339
340 let _ =
341   Inversion_principle.init ()