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