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