]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaMisc.ml
some prerr to better understand the mkdir -p error
[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 let safe_remove fname = if Sys.file_exists fname then Sys.remove fname
30
31 let is_dir fname =
32   try
33     (Unix.stat fname).Unix.st_kind = Unix.S_DIR
34   with Unix.Unix_error _ -> false
35
36 let is_regular fname =
37   try
38     (Unix.stat fname).Unix.st_kind = Unix.S_REG
39   with Unix.Unix_error _ -> false
40
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;
46   close_in ic;
47   Buffer.contents buf
48
49 let output_file data file = 
50   let oc = open_out file in
51   output_string oc data;
52   close_out oc
53
54
55 let absolute_path file =
56   if file.[0] = '/' then file else Unix.getcwd () ^ "/" ^ file
57   
58 let is_proof_script fname = true  (** TODO Zack *)
59 let is_proof_object fname = true  (** TODO Zack *)
60
61 let append_phrase_sep s =
62   if not (Pcre.pmatch ~pat:(sprintf "%s$" BuildTimeConf.phrase_sep) s) then
63     s ^ BuildTimeConf.phrase_sep
64   else
65     s
66
67 let strip_trailing_blanks =
68   let rex = Pcre.regexp "\\s*$" in
69   fun s -> Pcre.replace ~rex s
70
71 let strip_trailing_slash =
72   let rex = Pcre.regexp "/$" in
73   fun s -> Pcre.replace ~rex s
74
75 let empty_mathml () =
76   DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.mathml_ns)
77     ~qualifiedName:(Gdome.domString "math") ~doctype:None
78
79 let empty_boxml () =
80   DomMisc.domImpl#createDocument ~namespaceURI:(Some DomMisc.boxml_ns) 
81     ~qualifiedName:(Gdome.domString "box") ~doctype:None
82
83 exception History_failure
84
85 type 'a memento = 'a array * int * int * int  (* data, hd, tl, cur *)
86
87 class type ['a] history =
88   object
89     method add : 'a -> unit
90     method next : 'a
91     method previous : 'a
92     method load: 'a memento -> unit
93     method save: 'a memento
94     method is_begin: bool
95     method is_end: bool
96   end
97
98 class basic_history (head, tail, cur) =
99   object
100     val mutable hd = head  (* insertion point *)
101     val mutable tl = tail (* oldest inserted item *)
102     val mutable cur = cur  (* current item for the history *)
103     
104     method is_begin = cur <= tl
105     method is_end = cur >= hd
106   end
107   
108   
109 class shell_history size =
110   let size = size + 1 in
111   let decr x = let x' = x - 1 in if x' < 0 then size + x' else x' in
112   let incr x = (x + 1) mod size in
113   object (self)
114     val data = Array.create size ""
115
116     inherit basic_history (0, -1 , -1)
117     
118     method add s =
119       data.(hd) <- s;
120       if tl = -1 then tl <- hd;
121       hd <- incr hd;
122       if hd = tl then tl <- incr tl;
123       cur <- hd
124     method previous =
125       if cur = tl then raise History_failure;
126       cur <- decr cur;
127       data.(cur)
128     method next =
129       if cur = hd then raise History_failure;
130       cur <- incr cur;
131       if cur = hd then "" else data.(cur)
132     method load (data', hd', tl', cur') =
133       assert (Array.length data = Array.length data');
134       hd <- hd'; tl <- tl'; cur <- cur';
135       Array.blit data' 0 data 0 (Array.length data')
136     method save = (Array.copy data, hd, tl, cur)
137   end
138
139 class ['a] browser_history ?memento size init =
140   object (self)
141     initializer match memento with Some m -> self#load m | _ -> ()
142     val data = Array.create size init
143
144     inherit basic_history (0, 0, 0)
145     
146     method previous =
147       if cur = tl then raise History_failure;
148       cur <- cur - 1;
149       if cur = ~-1 then cur <- size - 1;
150       data.(cur)
151     method next =
152       if cur = hd then raise History_failure;
153       cur <- cur + 1;
154       if cur = size then cur <- 0;
155       data.(cur)
156     method add (e:'a) =
157       if e <> data.(cur) then
158         begin
159           cur <- cur + 1;
160           if cur = size then cur <- 0;
161           if cur = tl then tl <- tl + 1;
162           if tl = size then tl <- 0;
163           hd <- cur;
164           data.(cur) <- e
165         end
166     method load (data', hd', tl', cur') =
167       assert (Array.length data = Array.length data');
168       hd <- hd'; tl <- tl'; cur <- cur';
169       Array.blit data' 0 data 0 (Array.length data')
170     method save = (Array.copy data, hd, tl, cur)
171   end
172
173 let singleton f =
174   let instance = lazy (f ()) in
175   fun () -> Lazy.force instance
176
177 let mkdir d =
178   let errmsg = sprintf "Unable to create directory \"%s\"" d in
179   try
180     let dir = "mkdir -p " ^ d in 
181     (match Unix.system dir with
182     | Unix.WEXITED 0 -> ()
183     | Unix.WEXITED n -> 
184         MatitaLog.error ("'mkdir -p " ^ dir ^ "' failed with "^string_of_int n);
185         failwith errmsg
186     | Unix.WSIGNALED s 
187     | Unix.WSTOPPED s -> 
188         MatitaLog.error 
189           ("'mkdir -p " ^ dir ^ "' signaled with " ^ string_of_int s);
190         failwith errmsg)
191   with Unix.Unix_error _ as exc -> 
192     MatitaLog.error 
193       ("Unix error in makigin dir " ^ (MatitaExcPp.to_string exc));
194     failwith errmsg
195
196 let get_proof_status status =
197   match status.proof_status with
198   | Incomplete_proof s -> s
199   | _ -> statement_error "no ongoing proof"
200
201 let get_proof_metasenv status =
202   match status.proof_status with
203   | No_proof -> []
204   | Incomplete_proof ((_, metasenv, _, _), _) -> metasenv
205   | Proof (_, metasenv, _, _) -> metasenv
206   | Intermediate m -> m
207
208 let get_proof_context status =
209   match status.proof_status with
210   | Incomplete_proof ((_, metasenv, _, _), goal) ->
211       let (_, context, _) = CicUtil.lookup_meta goal metasenv in
212       context
213   | _ -> []
214  
215 let get_proof_aliases status = status.aliases
216
217 let qualify status name = get_string_option status "baseuri" ^ "/" ^ name
218
219 let unopt = function None -> failwith "unopt: None" | Some v -> v
220
221 let image_path n = sprintf "%s/%s" BuildTimeConf.images_dir n
222
223 let end_ma_RE = Pcre.regexp "\\.ma$"
224 let obj_file_of_script f = Pcre.replace ~rex:end_ma_RE ~templ:".moo" f
225
226 let rec list_uniq = function 
227   | [] -> []
228   | h::[] -> [h]
229   | h1::h2::tl when h1 = h2 -> list_uniq (h2 :: tl) 
230   | h1::tl (* when h1 <> h2 *) -> h1 :: list_uniq tl
231
232 let debug_wrap name f =
233   prerr_endline (sprintf "debug_wrap: ==>> %s" name);
234   let res = f () in
235   prerr_endline (sprintf "debug_wrap: <<== %s" name);
236   res
237