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