]> matita.cs.unibo.it Git - helm.git/blob - helm/http_getter/http_getter_env.ml
debian release 0.0.4-1
[helm.git] / helm / http_getter / http_getter_env.ml
1 (*
2  * Copyright (C) 2003:
3  *    Stefano Zacchiroli <zack@cs.unibo.it>
4  *    for the HELM Team http://helm.cs.unibo.it/
5  *
6  *  This file is part of HELM, an Hypertextual, Electronic
7  *  Library of Mathematics, developed at the Computer Science
8  *  Department, University of Bologna, Italy.
9  *
10  *  HELM is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public License
12  *  as published by the Free Software Foundation; either version 2
13  *  of the License, or (at your option) any later version.
14  *
15  *  HELM is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with HELM; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  *  MA  02111-1307, USA.
24  *
25  *  For details, see the HELM World-Wide-Web page,
26  *  http://helm.cs.unibo.it/
27  *)
28
29 open Http_getter_types;;
30 open Printf;;
31 open Pxp_document;;
32 open Pxp_types;;
33 open Pxp_yacc;;
34
35 let version = Http_getter_const.version
36
37 type setting_src =
38   | Environment (* read setting from environment variables *)
39   | Conffile    (* read setting from configuration file *)
40   | Both        (* read setting from both; environment override config file *)
41
42 let conf_file_tree = ref None
43
44 let (conf_file, conf_dir) =
45   try
46     let conf_dir =
47       Pcre.replace ~pat:"/$" (Sys.getenv "HELM_CONFIGURATION_DIR")
48     in
49     (conf_dir ^ "/" ^ Http_getter_const.conffile, conf_dir)
50   with Not_found -> failwith "HELM_CONFIGURATION_DIR undefined"
51
52 let safe_getenv ?(from = Both) var =
53   (let rec read_from_file () =
54     (match !conf_file_tree with
55     | None ->
56         conf_file_tree :=
57           Some
58             (parse_wfcontent_entity
59               default_config (from_file conf_file) default_spec);
60         read_from_file ()
61     | Some t ->
62         (try
63           Some (find_element (String.lowercase var) t)#data
64         with Not_found -> None))
65   in
66   let read_from_env () = try Some (Sys.getenv var) with Not_found -> None in
67   let return_value name = function
68     | Some v -> v
69     | None -> failwith ("Setting " ^ name ^ " is not defined")
70   in
71   (match from with
72   | Environment -> return_value var (read_from_env ())
73   | Conffile -> return_value var (read_from_file ())
74   | Both ->
75       (match read_from_env () with
76       | None -> return_value var (read_from_file ())
77       | v -> return_value var v)))
78
79 let servers_file = safe_getenv "HTTP_GETTER_SERVERS_FILE"
80 let parse_servers () =
81   (let cons hd tl = hd @ [ tl ] in
82   Http_getter_misc.fold_file cons [] servers_file)
83 let servers = ref (parse_servers ())
84 let reload_servers () = servers := parse_servers ()
85
86 let cic_dbm = safe_getenv "HTTP_GETTER_CIC_DBM"
87 let nuprl_dbm = safe_getenv "HTTP_GETTER_NUPRL_DBM"
88 let rdf_dbm = safe_getenv "HTTP_GETTER_RDF_DBM"
89 let xsl_dbm = safe_getenv "HTTP_GETTER_XSL_DBM"
90 let xml_index = safe_getenv "HTTP_GETTER_XML_INDEXNAME"
91 let rdf_index = safe_getenv "HTTP_GETTER_RDF_INDEXNAME"
92 let xsl_index = safe_getenv "HTTP_GETTER_XSL_INDEXNAME"
93 let cic_dir = safe_getenv "HTTP_GETTER_CIC_DIR"
94 let nuprl_dir = safe_getenv "HTTP_GETTER_NUPRL_DIR"
95 let rdf_dir = safe_getenv "HTTP_GETTER_RDF_DIR"
96 let dtd_dir = safe_getenv "HTTP_GETTER_DTD_DIR"
97
98 let port =
99   let port = safe_getenv "HTTP_GETTER_PORT" in
100   try
101     int_of_string port
102   with Failure "int_of_string" ->
103     failwith ("Invalid port value: " ^ port)
104 let host =
105   let buf = Buffer.create 20 in
106   Shell.call ~stdout:(Shell.to_buffer buf) [Shell.cmd "hostname" ["-f"]];
107   Pcre.replace ~pat:"\n+$" (Buffer.contents buf)
108 let my_own_url =
109   sprintf
110     "http://%s%s" (* without trailing '/' *)
111     host
112     (if port = 80 then "" else (sprintf ":%d" port))
113 let dtd_base_url = safe_getenv "HTTP_GETTER_DTD_BASE_URL"
114
115 let cache_mode =
116   match String.lowercase (safe_getenv "HTTP_GETTER_CACHE_MODE") with
117   | "normal" -> Enc_normal
118   | "gz" -> Enc_gzipped
119   | mode -> failwith ("Invalid cache mode: " ^ mode)
120
121 let reload () =
122   reload_servers ()
123
124 let dump_env () =
125   printf
126 "HTTP Getter %s (the OCaml one!)
127
128 cic_dbm:\t%s
129 nuprl_dbm:\t%s
130 rdf_dbm:\t%s
131 xsl_dbm:\t%s
132 xml_index:\t%s
133 rdf_index:\t%s
134 xsl_index:\t%s
135 cic_dir:\t%s
136 nuprl_dir:\t%s
137 rdf_dir:\t%s
138 dtd_dir:\t%s
139 servers_file:\t%s
140 host:\t\t%s
141 port:\t\t%d
142 my_own_url:\t%s
143 dtd_base_url:\t%s
144 cache_mode:\t%s
145 conf_file:\t%s
146 conf_dir:\t%s
147 servers:
148 \t%s
149 "
150     version cic_dbm nuprl_dbm rdf_dbm xsl_dbm xml_index rdf_index xsl_index
151     cic_dir nuprl_dir rdf_dir dtd_dir servers_file host port my_own_url
152     dtd_base_url
153     (match cache_mode with Enc_normal -> "Normal" | Enc_gzipped -> "GZipped")
154     conf_file conf_dir (String.concat "\n\t" !servers);
155   flush stdout
156