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 safe_remove fname = if Sys.file_exists fname then Sys.remove fname
33 (Unix.stat fname).Unix.st_kind = Unix.S_DIR
34 with Unix.Unix_error _ -> false
36 let is_regular fname =
38 (Unix.stat fname).Unix.st_kind = Unix.S_REG
39 with Unix.Unix_error _ -> false
41 let input_file fname =
42 let size = (Unix.stat fname).Unix.st_size in
43 let buf = Buffer.create size in
44 let ic = open_in fname in
45 Buffer.add_channel buf ic size;
49 let output_file data file =
50 let oc = open_out file in
51 output_string oc data;
54 let is_proof_script fname = true (** TODO Zack *)
55 let is_proof_object fname = true (** TODO Zack *)
57 let append_phrase_sep s =
58 if not (Pcre.pmatch ~pat:(sprintf "%s$" BuildTimeConf.phrase_sep) s) then
59 s ^ BuildTimeConf.phrase_sep
63 let strip_trailing_blanks =
64 let rex = Pcre.regexp "\\s*$" in
65 fun s -> Pcre.replace ~rex s
67 let strip_trailing_slash =
68 let rex = Pcre.regexp "/$" in
69 fun s -> Pcre.replace ~rex s
72 DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.mathml_ns)
73 ~qualifiedName:(Gdome.domString "math") ~doctype:None
76 DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.boxml_ns)
77 ~qualifiedName:(Gdome.domString "box") ~doctype:None
79 exception History_failure
81 type 'a memento = 'a array * int * int * int (* data, hd, tl, cur *)
83 class type ['a] history =
85 method add : 'a -> unit
88 method load: 'a memento -> unit
89 method save: 'a memento
94 class basic_history (head, tail, cur) =
96 val mutable hd = head (* insertion point *)
97 val mutable tl = tail (* oldest inserted item *)
98 val mutable cur = cur (* current item for the history *)
100 method is_begin = cur <= tl
101 method is_end = cur >= hd
105 class shell_history size =
106 let size = size + 1 in
107 let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
108 let incr x = (x + 1) mod size in
110 val data = Array.create size ""
112 inherit basic_history (0, -1 , -1)
116 if tl = -1 then tl <- hd;
118 if hd = tl then tl <- incr tl;
121 if cur = tl then raise History_failure;
125 if cur = hd then raise History_failure;
127 if cur = hd then "" else data.(cur)
128 method load (data', hd', tl', cur') =
129 assert (Array.length data = Array.length data');
130 hd <- hd'; tl <- tl'; cur <- cur';
131 Array.blit data' 0 data 0 (Array.length data')
132 method save = (Array.copy data, hd, tl, cur)
135 class ['a] browser_history ?memento size init =
137 initializer match memento with Some m -> self#load m | _ -> ()
138 val data = Array.create size init
140 inherit basic_history (0, 0, 0)
143 if cur = tl then raise History_failure;
145 if cur = ~-1 then cur <- size - 1;
148 if cur = hd then raise History_failure;
150 if cur = size then cur <- 0;
153 if e <> data.(cur) then
156 if cur = size then cur <- 0;
157 if cur = tl then tl <- tl + 1;
158 if tl = size then tl <- 0;
162 method load (data', hd', tl', cur') =
163 assert (Array.length data = Array.length data');
164 hd <- hd'; tl <- tl'; cur <- cur';
165 Array.blit data' 0 data 0 (Array.length data')
166 method save = (Array.copy data, hd, tl, cur)
170 let instance = lazy (f ()) in
171 fun () -> Lazy.force instance
174 let errmsg = sprintf "Unable to create directory \"%s\"" d in
176 (match Unix.system ("mkdir -p " ^ d) with
177 | Unix.WEXITED 0 -> ()
178 | _ -> failwith errmsg)
179 with Unix.Unix_error _ -> failwith errmsg
181 let get_proof_status status =
182 match status.proof_status with
183 | Incomplete_proof s -> s
184 | _ -> statement_error "no ongoing proof"
186 let get_proof_metasenv status =
187 match status.proof_status with
189 | Incomplete_proof ((_, metasenv, _, _), _) -> metasenv
190 | Proof (_, metasenv, _, _) -> metasenv
191 | Intermediate m -> m
193 let get_proof_context status =
194 match status.proof_status with
195 | Incomplete_proof ((_, metasenv, _, _), goal) ->
196 let (_, context, _) = CicUtil.lookup_meta goal metasenv in
200 let get_proof_aliases status = status.aliases
202 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
204 let unopt = function None -> failwith "unopt: None" | Some v -> v
206 let image_path n = sprintf "%s/%s" BuildTimeConf.images_dir n
208 let end_ma_RE = Pcre.regexp "\\.ma$"
209 let obj_file_of_script f = Pcre.replace ~rex:end_ma_RE ~templ:".moo" f
211 let rec list_uniq = function
214 | h1::h2::tl when h1 = h2 -> list_uniq (h2 :: tl)
215 | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq tl
217 let debug_wrap name f =
218 prerr_endline (sprintf "debug_wrap: ==>> %s" name);
220 prerr_endline (sprintf "debug_wrap: <<== %s" name);