]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaMisc.ml
- added integrity checks on .moo files
[helm.git] / helm / matita / matitaMisc.ml
1 (* Copyright (C) 2004-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 open MatitaTypes 
28
29 (** Functions "imported" from Http_getter_misc *)
30
31 let strip_trailing_slash = Http_getter_misc.strip_trailing_slash
32 let normalize_dir = Http_getter_misc.normalize_dir
33 let strip_suffix = Http_getter_misc.strip_suffix
34
35 let baseuri_of_baseuri_decl st =
36   match st with
37   | GrafiteAst.Executable (_, GrafiteAst.Command (_, GrafiteAst.Set (_, "baseuri", buri))) ->
38       Some buri
39   | _ -> None
40
41 let baseuri_of_file file = 
42   let uri = ref None in
43   let ic = open_in file in
44   let istream = Ulexing.from_utf8_channel ic in
45   (try
46     while true do
47       try 
48         let stm = GrafiteParser.parse_statement istream in
49         match baseuri_of_baseuri_decl stm with
50         | Some buri -> 
51             let u = strip_trailing_slash buri in
52             if String.length u < 5 || String.sub u 0 5 <> "cic:/" then
53               MatitaLog.error (file ^ " sets an incorrect baseuri: " ^ buri);
54             (try 
55               ignore(Http_getter.resolve u)
56             with
57             | Http_getter_types.Unresolvable_URI _ -> 
58                 MatitaLog.error (file ^ " sets an unresolvable baseuri: "^buri)
59             | Http_getter_types.Key_not_found _ -> ());
60             uri := Some u;
61             raise End_of_file
62         | None -> ()
63       with
64         CicNotationParser.Parse_error _ as exn ->
65           prerr_endline ("Unable to parse: " ^ file);
66           prerr_endline (MatitaExcPp.to_string exn);
67           ()
68     done
69   with End_of_file -> close_in ic);
70   match !uri with
71   | Some uri -> uri
72   | None -> failwith ("No baseuri defined in " ^ file)
73
74 let is_empty buri =
75  List.for_all
76   (function
77       Http_getter_types.Ls_section _ -> true
78     | Http_getter_types.Ls_object _ -> false)
79   (Http_getter.ls (Http_getter_misc.strip_trailing_slash buri ^ "/"))
80
81 let safe_remove fname = if Sys.file_exists fname then Sys.remove fname
82
83
84 let absolute_path file =
85   if file.[0] = '/' then file else Unix.getcwd () ^ "/" ^ file
86   
87 let is_proof_script fname = true  (** TODO Zack *)
88 let is_proof_object fname = true  (** TODO Zack *)
89
90 let append_phrase_sep s =
91   if not (Pcre.pmatch ~pat:(sprintf "%s$" BuildTimeConf.phrase_sep) s) then
92     s ^ BuildTimeConf.phrase_sep
93   else
94     s
95
96 let empty_mathml () =
97   DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.mathml_ns)
98     ~qualifiedName:(Gdome.domString "math") ~doctype:None
99
100 let empty_boxml () =
101   DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.boxml_ns) 
102     ~qualifiedName:(Gdome.domString "box") ~doctype:None
103
104 exception History_failure
105
106 type 'a memento = 'a array * int * int * int  (* data, hd, tl, cur *)
107
108 class type ['a] history =
109   object
110     method add : 'a -> unit
111     method next : 'a
112     method previous : 'a
113     method load: 'a memento -> unit
114     method save: 'a memento
115     method is_begin: bool
116     method is_end: bool
117   end
118
119 class basic_history (head, tail, cur) =
120   object
121     val mutable hd = head  (* insertion point *)
122     val mutable tl = tail (* oldest inserted item *)
123     val mutable cur = cur  (* current item for the history *)
124     
125     method is_begin = cur <= tl
126     method is_end = cur >= hd
127   end
128   
129   
130 class shell_history size =
131   let size = size + 1 in
132   let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
133   let incr x = (x + 1) mod size in
134   object (self)
135     val data = Array.create size ""
136
137     inherit basic_history (0, -1 , -1)
138     
139     method add s =
140       data.(hd) <- s;
141       if tl = -1 then tl <- hd;
142       hd <- incr hd;
143       if hd = tl then tl <- incr tl;
144       cur <- hd
145     method previous =
146       if cur = tl then raise History_failure;
147       cur <- decr cur;
148       data.(cur)
149     method next =
150       if cur = hd then raise History_failure;
151       cur <- incr cur;
152       if cur = hd then "" else data.(cur)
153     method load (data', hd', tl', cur') =
154       assert (Array.length data = Array.length data');
155       hd <- hd'; tl <- tl'; cur <- cur';
156       Array.blit data' 0 data 0 (Array.length data')
157     method save = (Array.copy data, hd, tl, cur)
158   end
159
160 class ['a] browser_history ?memento size init =
161   object (self)
162     initializer match memento with Some m -> self#load m | _ -> ()
163     val data = Array.create size init
164
165     inherit basic_history (0, 0, 0)
166     
167     method previous =
168       if cur = tl then raise History_failure;
169       cur <- cur - 1;
170       if cur = ~-1 then cur <- size - 1;
171       data.(cur)
172     method next =
173       if cur = hd then raise History_failure;
174       cur <- cur + 1;
175       if cur = size then cur <- 0;
176       data.(cur)
177     method add (e:'a) =
178       if e <> data.(cur) then
179         begin
180           cur <- cur + 1;
181           if cur = size then cur <- 0;
182           if cur = tl then tl <- tl + 1;
183           if tl = size then tl <- 0;
184           hd <- cur;
185           data.(cur) <- e
186         end
187     method load (data', hd', tl', cur') =
188       assert (Array.length data = Array.length data');
189       hd <- hd'; tl <- tl'; cur <- cur';
190       Array.blit data' 0 data 0 (Array.length data')
191     method save = (Array.copy data, hd, tl, cur)
192   end
193
194 let singleton f =
195   let instance = lazy (f ()) in
196   fun () -> Lazy.force instance
197
198 let get_proof_status status =
199   match status.proof_status with
200   | Incomplete_proof s -> s
201   | _ -> statement_error "no ongoing proof"
202
203 let get_proof_metasenv status =
204   match status.proof_status with
205   | No_proof -> []
206   | Incomplete_proof ((_, metasenv, _, _), _) -> metasenv
207   | Proof (_, metasenv, _, _) -> metasenv
208   | Intermediate m -> m
209
210 let get_proof_context status =
211   match status.proof_status with
212   | Incomplete_proof ((_, metasenv, _, _), goal) ->
213       let (_, context, _) = CicUtil.lookup_meta goal metasenv in
214       context
215   | _ -> []
216  
217 let get_proof_conclusion status =
218   match status.proof_status with
219   | Incomplete_proof ((_, metasenv, _, _), goal) ->
220       let (_, _, conclusion) = CicUtil.lookup_meta goal metasenv in
221       conclusion
222   | _ -> statement_error "no ongoing proof"
223  
224 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
225
226 let image_path n = sprintf "%s/%s" BuildTimeConf.images_dir n
227
228 let end_ma_RE = Pcre.regexp "\\.ma$"
229
230 let obj_file_of_baseuri baseuri =
231  let path =
232   Helm_registry.get "matita.basedir" ^ "/xml" ^
233    Pcre.replace ~pat:"^cic:" ~templ:"" baseuri
234  in
235   path ^ ".moo"
236
237 let obj_file_of_script f =
238  if f = "coq.ma" then BuildTimeConf.coq_notation_script else
239   let baseuri = baseuri_of_file f in
240    obj_file_of_baseuri baseuri
241
242 let rec list_uniq = function 
243   | [] -> []
244   | h::[] -> [h]
245   | h1::h2::tl when h1 = h2 -> list_uniq (h2 :: tl) 
246   | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq tl
247
248 let list_tl_at ?(equality=(==)) e l =
249   let rec aux =
250     function
251     | [] -> raise Not_found
252     | hd :: tl as l when equality hd e -> l
253     | hd :: tl -> aux tl
254   in
255   aux l
256