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