1 (* Copyright (C) 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/
26 exception FileNotFound of string
27 exception NoRootFor of string
31 if String.length path > 0 && path.[0] <> '/' then
32 Sys.getcwd () ^ "/" ^ path
36 HExtlib.normalize_path path
41 let path = absolutize path in
42 let paths = List.rev (Str.split (Str.regexp "/") path) in
43 let rec build = function
44 | he::tl as l -> ("/" ^ String.concat "/" (List.rev l) ^ "/") :: build tl
47 let paths = List.map HExtlib.normalize_path (build paths) in
48 try HExtlib.find_in paths "root"
49 with Failure "find_in" ->
50 raise (NoRootFor (path ^ " (" ^ String.concat ", " paths ^ ")"))
53 let ensure_trailing_slash s =
54 if s = "" then "/" else
55 if s.[String.length s-1] <> '/' then s^"/" else s
58 let remove_trailing_slash s =
59 if s = "" then "" else
60 let len = String.length s in
61 if s.[len-1] = '/' then String.sub s 0 (len-1) else s
64 let load_root_file rootpath =
65 let data = HExtlib.input_file rootpath in
66 let lines = Str.split (Str.regexp "\n") data in
68 Pcre.replace ~pat:"[ \t]+" ~templ:" "
69 (Pcre.replace ~pat:"^ *" (Pcre.replace ~pat:" *$" s))
73 match Str.split (Str.regexp "=") l with
74 | [k;v] -> clean k, Http_getter_misc.strip_trailing_slash (clean v)
75 | _ -> raise (Failure ("Malformed root file: " ^ rootpath)))
79 let find_root_for ~include_paths file =
80 let include_paths = "" :: Sys.getcwd () :: include_paths in
81 let rec find_path_for file =
82 try HExtlib.find_in include_paths file
83 with Failure "find_in" ->
84 HLog.error ("We are in: " ^ Sys.getcwd ());
85 HLog.error ("Unable to find: "^file^"\nPaths explored:");
86 List.iter (fun x -> HLog.error (" - "^x)) include_paths;
87 raise (FileNotFound file)
89 let path = find_path_for file in
90 let path = absolutize path in
91 (* HLog.debug ("file "^file^" resolved as "^path); *)
92 let rootpath, root, buri =
94 let mburi = Helm_registry.get "matita.baseuri" in
95 match Str.split (Str.regexp " ") mburi with
96 | [root; buri] when HExtlib.is_prefix_of root path ->
97 ":registry:", root, buri
98 | _ -> raise (Helm_registry.Key_not_found "matita.baseuri")
99 with Helm_registry.Key_not_found "matita.baseuri" ->
100 let rootpath = find_root path in
101 let buri = List.assoc "baseuri" (load_root_file rootpath) in
102 rootpath, Filename.dirname rootpath, buri
104 (* HLog.debug ("file "^file^" rooted by "^rootpath^""); *)
105 let uri = Http_getter_misc.strip_trailing_slash buri in
106 if String.length uri < 5 || String.sub uri 0 5 <> "cic:/" then
107 HLog.error (rootpath ^ " sets an incorrect baseuri: " ^ buri);
108 ensure_trailing_slash root, remove_trailing_slash uri, path
112 let mk_baseuri root extra =
114 assert(Filename.check_suffix name ".ma" ||
115 Filename.check_suffix name ".mma" ||
116 Filename.check_suffix name ".mad");
117 try Filename.chop_extension name
118 with Invalid_argument "Filename.chop_extension" -> name
120 remove_trailing_slash (HExtlib.normalize_path (root ^ "/" ^ chop extra))
123 let baseuri_of_script ~include_paths file =
124 let root, buri, path = find_root_for ~include_paths file in
125 let path = HExtlib.normalize_path path in
126 let root = HExtlib.normalize_path root in
127 let lpath = Str.split (Str.regexp "/") path in
128 let lroot = Str.split (Str.regexp "/") root in
129 let rec substract l1 l2 =
131 | h1::tl1,h2::tl2 when h1 = h2 -> substract tl1 tl2
135 let extra_buri = substract lpath lroot in
136 let extra = String.concat "/" extra_buri in
138 mk_baseuri buri extra,
143 let find_roots_in_dir dir =
144 HExtlib.find ~test:(fun f ->
145 Filename.basename f = "root" &&
146 try (Unix.stat f).Unix.st_kind = Unix.S_REG
147 with Unix.Unix_error _ -> false)
151 (* scheme uri part as defined in URI Generic Syntax (RFC 3986) *)
152 let uri_scheme_rex = Pcre.regexp "^[[:alpha:]][[:alnum:]\-+.]*:"
154 let is_uri str = Pcre.pmatch ~rex:uri_scheme_rex str