]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaInit.ml
removed debug saving of "foo.conf.xml"
[helm.git] / helm / 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 open Printf
27
28 type thingsToInitilaize = 
29   ConfigurationFile | Db | Environment | Getter | Notation | 
30   Paramodulation | Makelib | CmdLine
31   
32 exception FailedToInitialize of thingsToInitilaize
33
34 let wants s l = 
35   List.iter (
36     fun item -> 
37       if not (List.exists (fun x -> x = item) l) then
38         raise (FailedToInitialize item)) 
39   s
40
41 let already_configured s l =
42   List.for_all (fun item -> List.exists (fun x -> x = item) l) s
43   
44 let load_configuration init_status =
45   if not (already_configured [ConfigurationFile] init_status) then
46     begin
47       Helm_registry.load_from BuildTimeConf.matita_conf;
48       ConfigurationFile::init_status 
49     end
50   else
51     init_status
52
53 let initialize_db init_status = 
54   wants [ ConfigurationFile; CmdLine ] init_status;
55   if not (already_configured [ Db ] init_status) then
56     begin
57       MetadataTypes.ownerize_tables (Helm_registry.get "matita.owner");
58       MatitaDb.create_owner_environment ();
59       Db::init_status
60     end
61   else
62     init_status
63
64 let initialize_paramodulation init_status = 
65   wants [] init_status;
66   if not (already_configured [Paramodulation] init_status) then
67     begin
68       Paramodulation.Saturation.init ();
69       Paramodulation::init_status
70     end
71   else
72     init_status
73
74 let initialize_makelib init_status = 
75   wants [ConfigurationFile] init_status;
76   if not (already_configured [Makelib] init_status) then
77     begin
78       MatitamakeLib.initialize (); 
79       Makelib::init_status
80     end
81   else
82     init_status
83
84 let initialize_notation init_status = 
85   wants [ConfigurationFile] init_status;
86   if not (already_configured [Notation] init_status) then
87     begin
88       CicNotation.load_notation BuildTimeConf.core_notation_script;
89       Notation::init_status
90     end
91   else
92     init_status
93
94 let initialize_environment init_status = 
95   wants [ConfigurationFile] init_status;
96   if not (already_configured [Getter;Environment] init_status) then
97     begin
98       Http_getter.init ();
99       CicEnvironment.set_trust (* environment trust *)
100         (let trust = Helm_registry.get_bool "matita.environment_trust" in
101          fun _ -> trust);
102       Getter::Environment::init_status
103     end
104   else
105     init_status 
106   
107 let status = ref []
108
109 let usages = Hashtbl.create 11
110 let _ =
111   List.iter
112     (fun (name, s) -> Hashtbl.replace usages name s)
113     [ "matitac", 
114         sprintf "MatitaC v%s
115 Usage: matitac [ OPTION ... ] FILE
116 Options:"
117           BuildTimeConf.version;
118       "matita",
119         sprintf "Matita v%s
120 Usage: matita [ OPTION ... ] [ FILE ... ]
121 Options:"
122           BuildTimeConf.version;
123       "cicbrowser",
124         sprintf
125           "CIC Browser v%s
126 Usage: cicbrowser [ URL | WHELP QUERY ]
127 Options:"
128           BuildTimeConf.version;
129       "matitadep",
130         sprintf "MatitaDep v%s
131 Usage: matitadep [ OPTION ... ] FILE ...
132 Options:"
133           BuildTimeConf.version;
134       "matitaclean",
135         sprintf "MatitaClean v%s
136 Usage: matitaclean all
137        matitaclean [ (FILE | URI) ... ]
138 Options:"
139           BuildTimeConf.version;
140     ]
141 let default_usage =
142   sprintf "Matita v%s\nUsage: matita [ ARG ]\nOptions:" BuildTimeConf.version
143
144 let usage () =
145   let basename = Filename.basename Sys.argv.(0) in
146   let usage_key =
147     try Filename.chop_extension basename with Invalid_argument  _ -> basename
148   in
149   try Hashtbl.find usages usage_key with Not_found -> default_usage
150
151 let registry_defaults =
152   [ "matita.debug", "false";
153     "matita.quiet", "false";
154     "matita.preserve", "false";
155     "db.nodb", "false";
156   ]
157
158 let set_registry_values =
159   List.iter (fun key, value -> Helm_registry.set ~key ~value)
160
161 let parse_cmdline init_status =
162   if not (already_configured [CmdLine] init_status) then begin
163     let includes = ref [] in
164     let args = ref [] in
165     let add_l l = fun s -> l := s :: !l in
166     let arg_spec =
167       let std_arg_spec = [
168         "-I", Arg.String (add_l includes),
169           ("<path> Adds path to the list of searched paths for the "
170            ^ "include command");
171         "-q", Arg.Unit (fun () -> Helm_registry.set_bool "matita.quiet" true),
172           "Turn off verbose compilation";
173         "-preserve",
174           Arg.Unit (fun () -> Helm_registry.set_bool "matita.preserve" true),
175           "Turns off automatic baseuri cleaning";
176         "-nodb", Arg.Unit (fun () -> Helm_registry.set_bool "db.nodb" true),
177               ("Avoid using external database connection "
178                ^ "(WARNING: disable many features)");
179       ] in
180       let debug_arg_spec =
181         if BuildTimeConf.debug then
182           [ "-debug",
183             Arg.Unit (fun () -> Helm_registry.set_bool "matita.debug" true),
184               ("Do not catch top-level exception "
185               ^ "(useful for backtrace inspection)");
186           ]
187         else []
188       in
189       std_arg_spec @ debug_arg_spec
190     in
191     let set_list ~key l =
192       Helm_registry.set_list Helm_registry.of_string ~key ~value:(List.rev !l)
193     in
194     set_registry_values registry_defaults;
195     Arg.parse arg_spec (add_l args) (usage ());
196     set_list ~key:"matita.includes" includes;
197     set_list ~key:"matita.args" args;
198     CmdLine :: init_status
199   end else
200     init_status
201
202 let die_usage () =
203   print_endline (usage ());
204   exit 1
205
206 let initialize_all () =
207   status := 
208     initialize_notation 
209       (initialize_environment 
210         (initialize_db 
211           (initialize_paramodulation 
212             (initialize_makelib
213               (load_configuration
214                 (parse_cmdline !status))))))
215
216 let load_configuration_file () =
217   status := load_configuration !status
218
219 let initialize_notation () =
220   status := initialize_notation (load_configuration !status)
221
222 let parse_cmdline () =
223   status := parse_cmdline !status
224