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