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 let strip_trailing_slash =
30 let rex = Pcre.regexp "/$" in
31 fun s -> Pcre.replace ~rex s
33 let baseuri_of_baseuri_decl st =
35 | GrafiteAst.Executable (_, GrafiteAst.Command (_, GrafiteAst.Set (_, "baseuri", buri))) ->
39 let baseuri_of_file file =
41 let ic = open_in file in
42 let istream = Stream.of_channel ic in
46 let stm = GrafiteParser.parse_statement istream in
47 match baseuri_of_baseuri_decl stm with
49 let u = strip_trailing_slash buri in
50 if String.length u < 5 || String.sub u 0 5 <> "cic:/" then
51 MatitaLog.error (file ^ " sets an incorrect baseuri: " ^ buri);
53 ignore(Http_getter.resolve u)
55 | Http_getter_types.Unresolvable_URI _ ->
56 MatitaLog.error (file ^ " sets an unresolvable baseuri: "^buri)
57 | Http_getter_types.Key_not_found _ -> ());
62 CicNotationParser.Parse_error _ as exn ->
63 prerr_endline ("Unable to parse: " ^ file);
64 prerr_endline (MatitaExcPp.to_string exn);
67 with End_of_file -> close_in ic);
70 | None -> failwith ("No baseuri defined in " ^ file)
75 Http_getter_types.Ls_section _ -> true
76 | Http_getter_types.Ls_object _ -> false)
77 (Http_getter.ls (Http_getter_misc.strip_trailing_slash buri ^ "/"))
79 let safe_remove fname = if Sys.file_exists fname then Sys.remove fname
83 (Unix.stat fname).Unix.st_kind = Unix.S_DIR
84 with Unix.Unix_error _ -> false
86 let is_regular fname =
88 (Unix.stat fname).Unix.st_kind = Unix.S_REG
89 with Unix.Unix_error _ -> false
91 let input_file fname =
92 let size = (Unix.stat fname).Unix.st_size in
93 let buf = Buffer.create size in
94 let ic = open_in fname in
95 Buffer.add_channel buf ic size;
99 let output_file data file =
100 let oc = open_out file in
101 output_string oc data;
105 let absolute_path file =
106 if file.[0] = '/' then file else Unix.getcwd () ^ "/" ^ file
108 let is_proof_script fname = true (** TODO Zack *)
109 let is_proof_object fname = true (** TODO Zack *)
111 let append_phrase_sep s =
112 if not (Pcre.pmatch ~pat:(sprintf "%s$" BuildTimeConf.phrase_sep) s) then
113 s ^ BuildTimeConf.phrase_sep
117 let strip_trailing_blanks =
118 let rex = Pcre.regexp "\\s*$" in
119 fun s -> Pcre.replace ~rex s
121 let split ?(char = ' ') s =
122 let pieces = ref [] in
124 match (try Some (String.index_from s idx char) with Not_found -> None) with
126 pieces := String.sub s idx (pos - idx) :: !pieces;
128 | None -> pieces := String.sub s idx (String.length s - idx) :: !pieces
133 let empty_mathml () =
134 DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.mathml_ns)
135 ~qualifiedName:(Gdome.domString "math") ~doctype:None
138 DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.boxml_ns)
139 ~qualifiedName:(Gdome.domString "box") ~doctype:None
141 exception History_failure
143 type 'a memento = 'a array * int * int * int (* data, hd, tl, cur *)
145 class type ['a] history =
147 method add : 'a -> unit
150 method load: 'a memento -> unit
151 method save: 'a memento
152 method is_begin: bool
156 class basic_history (head, tail, cur) =
158 val mutable hd = head (* insertion point *)
159 val mutable tl = tail (* oldest inserted item *)
160 val mutable cur = cur (* current item for the history *)
162 method is_begin = cur <= tl
163 method is_end = cur >= hd
167 class shell_history size =
168 let size = size + 1 in
169 let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
170 let incr x = (x + 1) mod size in
172 val data = Array.create size ""
174 inherit basic_history (0, -1 , -1)
178 if tl = -1 then tl <- hd;
180 if hd = tl then tl <- incr tl;
183 if cur = tl then raise History_failure;
187 if cur = hd then raise History_failure;
189 if cur = hd then "" else data.(cur)
190 method load (data', hd', tl', cur') =
191 assert (Array.length data = Array.length data');
192 hd <- hd'; tl <- tl'; cur <- cur';
193 Array.blit data' 0 data 0 (Array.length data')
194 method save = (Array.copy data, hd, tl, cur)
197 class ['a] browser_history ?memento size init =
199 initializer match memento with Some m -> self#load m | _ -> ()
200 val data = Array.create size init
202 inherit basic_history (0, 0, 0)
205 if cur = tl then raise History_failure;
207 if cur = ~-1 then cur <- size - 1;
210 if cur = hd then raise History_failure;
212 if cur = size then cur <- 0;
215 if e <> data.(cur) then
218 if cur = size then cur <- 0;
219 if cur = tl then tl <- tl + 1;
220 if tl = size then tl <- 0;
224 method load (data', hd', tl', cur') =
225 assert (Array.length data = Array.length data');
226 hd <- hd'; tl <- tl'; cur <- cur';
227 Array.blit data' 0 data 0 (Array.length data')
228 method save = (Array.copy data, hd, tl, cur)
232 let instance = lazy (f ()) in
233 fun () -> Lazy.force instance
236 let errmsg = sprintf "Unable to create directory \"%s\"" d in
238 let dir = "mkdir -p " ^ d in
239 (match Unix.system dir with
240 | Unix.WEXITED 0 -> ()
242 MatitaLog.error ("'mkdir -p " ^ dir ^ "' failed with "^string_of_int n);
247 ("'mkdir -p " ^ dir ^ "' signaled with " ^ string_of_int s);
249 with Unix.Unix_error _ as exc ->
251 ("Unix error in makigin dir " ^ (MatitaExcPp.to_string exc));
254 let get_proof_status status =
255 match status.proof_status with
256 | Incomplete_proof s -> s
257 | _ -> statement_error "no ongoing proof"
259 let get_proof_metasenv status =
260 match status.proof_status with
262 | Incomplete_proof ((_, metasenv, _, _), _) -> metasenv
263 | Proof (_, metasenv, _, _) -> metasenv
264 | Intermediate m -> m
266 let get_proof_context status =
267 match status.proof_status with
268 | Incomplete_proof ((_, metasenv, _, _), goal) ->
269 let (_, context, _) = CicUtil.lookup_meta goal metasenv in
273 let get_proof_aliases status = status.aliases
275 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
277 let unopt = function None -> failwith "unopt: None" | Some v -> v
279 let image_path n = sprintf "%s/%s" BuildTimeConf.images_dir n
281 let end_ma_RE = Pcre.regexp "\\.ma$"
283 let obj_file_of_baseuri baseuri =
285 Helm_registry.get "matita.basedir" ^ "/xml" ^
286 Pcre.replace ~pat:"^cic:" ~templ:"" baseuri
290 let obj_file_of_script f =
291 let baseuri = baseuri_of_file f in
292 obj_file_of_baseuri baseuri
294 let rec list_uniq = function
297 | h1::h2::tl when h1 = h2 -> list_uniq (h2 :: tl)
298 | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq tl
300 let debug_wrap name f =
301 prerr_endline (sprintf "debug_wrap: ==>> %s" name);
303 prerr_endline (sprintf "debug_wrap: <<== %s" name);