1 (* Copyright (C) 2004-2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
29 (** Functions "imported" from Http_getter_misc *)
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
35 let baseuri_of_baseuri_decl st =
37 | GrafiteAst.Executable (_, GrafiteAst.Command (_, GrafiteAst.Set (_, "baseuri", buri))) ->
41 let baseuri_of_file file =
43 let ic = open_in file in
44 let istream = Stream.of_channel ic in
48 let stm = GrafiteParser.parse_statement istream in
49 match baseuri_of_baseuri_decl stm with
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);
55 ignore(Http_getter.resolve u)
57 | Http_getter_types.Unresolvable_URI _ ->
58 MatitaLog.error (file ^ " sets an unresolvable baseuri: "^buri)
59 | Http_getter_types.Key_not_found _ -> ());
64 CicNotationParser.Parse_error _ as exn ->
65 prerr_endline ("Unable to parse: " ^ file);
66 prerr_endline (MatitaExcPp.to_string exn);
69 with End_of_file -> close_in ic);
72 | None -> failwith ("No baseuri defined in " ^ file)
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 ^ "/"))
81 let safe_remove fname = if Sys.file_exists fname then Sys.remove fname
85 (Unix.stat fname).Unix.st_kind = Unix.S_DIR
86 with Unix.Unix_error _ -> false
88 let is_regular fname =
90 (Unix.stat fname).Unix.st_kind = Unix.S_REG
91 with Unix.Unix_error _ -> false
93 let input_file fname =
94 let size = (Unix.stat fname).Unix.st_size in
95 let buf = Buffer.create size in
96 let ic = open_in fname in
97 Buffer.add_channel buf ic size;
101 let output_file data file =
102 let oc = open_out file in
103 output_string oc data;
107 let absolute_path file =
108 if file.[0] = '/' then file else Unix.getcwd () ^ "/" ^ file
110 let is_proof_script fname = true (** TODO Zack *)
111 let is_proof_object fname = true (** TODO Zack *)
113 let append_phrase_sep s =
114 if not (Pcre.pmatch ~pat:(sprintf "%s$" BuildTimeConf.phrase_sep) s) then
115 s ^ BuildTimeConf.phrase_sep
120 let components = Str.split (Str.regexp "/") path in
121 let rec aux where = function
124 let path = where ^ "/" ^ piece in
126 Unix.mkdir path 0o755
128 | Unix.Unix_error (Unix.EEXIST,_,_) -> ()
129 | Unix.Unix_error (e,_,_) -> raise (Failure (Unix.error_message e)));
135 let rex = Pcre.regexp "^\\s*(.*?)\\s*$" in
136 fun s -> (Pcre.extract ~rex s).(1)
138 let split ?(char = ' ') s =
139 let pieces = ref [] in
141 match (try Some (String.index_from s idx char) with Not_found -> None) with
143 pieces := String.sub s idx (pos - idx) :: !pieces;
145 | None -> pieces := String.sub s idx (String.length s - idx) :: !pieces
150 let empty_mathml () =
151 DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.mathml_ns)
152 ~qualifiedName:(Gdome.domString "math") ~doctype:None
155 DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.boxml_ns)
156 ~qualifiedName:(Gdome.domString "box") ~doctype:None
158 exception History_failure
160 type 'a memento = 'a array * int * int * int (* data, hd, tl, cur *)
162 class type ['a] history =
164 method add : 'a -> unit
167 method load: 'a memento -> unit
168 method save: 'a memento
169 method is_begin: bool
173 class basic_history (head, tail, cur) =
175 val mutable hd = head (* insertion point *)
176 val mutable tl = tail (* oldest inserted item *)
177 val mutable cur = cur (* current item for the history *)
179 method is_begin = cur <= tl
180 method is_end = cur >= hd
184 class shell_history size =
185 let size = size + 1 in
186 let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
187 let incr x = (x + 1) mod size in
189 val data = Array.create size ""
191 inherit basic_history (0, -1 , -1)
195 if tl = -1 then tl <- hd;
197 if hd = tl then tl <- incr tl;
200 if cur = tl then raise History_failure;
204 if cur = hd then raise History_failure;
206 if cur = hd then "" else data.(cur)
207 method load (data', hd', tl', cur') =
208 assert (Array.length data = Array.length data');
209 hd <- hd'; tl <- tl'; cur <- cur';
210 Array.blit data' 0 data 0 (Array.length data')
211 method save = (Array.copy data, hd, tl, cur)
214 class ['a] browser_history ?memento size init =
216 initializer match memento with Some m -> self#load m | _ -> ()
217 val data = Array.create size init
219 inherit basic_history (0, 0, 0)
222 if cur = tl then raise History_failure;
224 if cur = ~-1 then cur <- size - 1;
227 if cur = hd then raise History_failure;
229 if cur = size then cur <- 0;
232 if e <> data.(cur) then
235 if cur = size then cur <- 0;
236 if cur = tl then tl <- tl + 1;
237 if tl = size then tl <- 0;
241 method load (data', hd', tl', cur') =
242 assert (Array.length data = Array.length data');
243 hd <- hd'; tl <- tl'; cur <- cur';
244 Array.blit data' 0 data 0 (Array.length data')
245 method save = (Array.copy data, hd, tl, cur)
249 let instance = lazy (f ()) in
250 fun () -> Lazy.force instance
252 let get_proof_status status =
253 match status.proof_status with
254 | Incomplete_proof s -> s
255 | _ -> statement_error "no ongoing proof"
257 let get_proof_metasenv status =
258 match status.proof_status with
260 | Incomplete_proof ((_, metasenv, _, _), _) -> metasenv
261 | Proof (_, metasenv, _, _) -> metasenv
262 | Intermediate m -> m
264 let get_proof_context status =
265 match status.proof_status with
266 | Incomplete_proof ((_, metasenv, _, _), goal) ->
267 let (_, context, _) = CicUtil.lookup_meta goal metasenv in
271 let get_proof_conclusion status =
272 match status.proof_status with
273 | Incomplete_proof ((_, metasenv, _, _), goal) ->
274 let (_, _, conclusion) = CicUtil.lookup_meta goal metasenv in
276 | _ -> statement_error "no ongoing proof"
278 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
280 let unopt = function None -> failwith "unopt: None" | Some v -> v
282 let image_path n = sprintf "%s/%s" BuildTimeConf.images_dir n
284 let end_ma_RE = Pcre.regexp "\\.ma$"
286 let obj_file_of_baseuri baseuri =
288 Helm_registry.get "matita.basedir" ^ "/xml" ^
289 Pcre.replace ~pat:"^cic:" ~templ:"" baseuri
293 let obj_file_of_script f =
294 if f = "coq.ma" then BuildTimeConf.coq_notation_script else
295 let baseuri = baseuri_of_file f in
296 obj_file_of_baseuri baseuri
298 let rec list_uniq = function
301 | h1::h2::tl when h1 = h2 -> list_uniq (h2 :: tl)
302 | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq tl
304 let list_tl_at ?(equality=(==)) e l =
307 | [] -> raise Not_found
308 | hd :: tl as l when equality hd e -> l
313 let debug_wrap name f =
314 prerr_endline (sprintf "debug_wrap: ==>> %s" name);
316 prerr_endline (sprintf "debug_wrap: <<== %s" name);