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