(* Copyright (C) 2004, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic * Library of Mathematics, developed at the Computer Science * Department, University of Bologna, Italy. * * HELM is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * HELM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HELM; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * * For details, see the HELM World-Wide-Web page, * http://helm.cs.unibo.it/ *) open Printf let debug = false let debug_print s = if debug then prerr_endline ("Helm_registry debugging: " ^ s) exception Malformed_key of string exception Key_not_found of string exception Type_error of string * string * string (* expected type, value, msg *) exception Parse_error of string * int * int * string (* file, line, col, msg *) exception Invalid_value of (string * string) * string (* key, value, descr *) type validator_id = int let get_next_validator_id = let next_id = ref 0 in fun () -> incr next_id; !next_id let magic_size = 127 let validators = Hashtbl.create magic_size let registry = Hashtbl.create magic_size let backup_registry () = Hashtbl.copy registry let restore_registry backup = Hashtbl.clear registry; Hashtbl.iter (fun key value -> Hashtbl.replace registry key value) backup (* as \\w but: * - no sequences of '_' longer than 1 are permitted * - no uppercase letter are permitted *) let valid_step_rex_raw = "[a-z0-9]+(_[a-z0-9]+)*" let valid_key_rex_raw = sprintf "%s(\\.%s)*" valid_step_rex_raw valid_step_rex_raw let valid_key_rex = Pcre.regexp ("^" ^ valid_key_rex_raw ^ "$") (* escapes for xml configuration file *) let (escape, unescape) = let (in_enc, out_enc) = (`Enc_utf8, `Enc_utf8) in (Netencoding.Html.encode ~in_enc ~out_enc (), Netencoding.Html.decode ~in_enc ~out_enc ~entity_base:`Xml ()) let key_is_valid key = if not (Pcre.pmatch ~rex:valid_key_rex key) then raise (Malformed_key key) let value_is_valid ~key ~value = List.iter (fun (validator, descr) -> if not (validator value) then raise (Invalid_value ((key, value), descr))) (Hashtbl.find_all validators key) let set' registry ~key ~value = debug_print (sprintf "Setting %s = %s" key value); key_is_valid key; value_is_valid ~key ~value; Hashtbl.replace registry key value let env_var_of_key = let dot_RE = Pcre.regexp "\\." in fun key -> Pcre.replace ~rex:dot_RE ~templ:"__" (String.uppercase key) let get key = key_is_valid key; let registry_value = try Some (Hashtbl.find registry key) with Not_found -> None in let env_value = try Some (Sys.getenv (env_var_of_key key)) with Not_found -> None in match (registry_value, env_value) with | Some reg, Some env -> env | Some reg, None -> reg | None, Some env -> env | None, None -> raise (Key_not_found key) let set = set' registry let string_list_of_string s = (* trailing blanks are removed per default by Pcre.split *) Pcre.split ~pat:"\\s+" (Pcre.replace ~pat:"^\\s+" s) let string_of_string_list l = String.concat " " l let mk_get_set type_name (from_string: string -> 'a) (to_string: 'a -> string) = let getter key = let value = get key in try from_string value with exn -> raise (Type_error (type_name, value, Printexc.to_string exn)) in let setter ~key ~value = set ~key ~value:(to_string value) in (getter, setter) let (get_string, set_string) = (get, set) let (get_int, set_int) = mk_get_set "int" int_of_string string_of_int let (get_float, set_float) = mk_get_set "float" float_of_string string_of_float let (get_string_list, set_string_list) = mk_get_set "string list" string_list_of_string string_of_string_list let save_to fname = debug_print ("Saving configuration to " ^ fname); let oc = open_out fname in output_string oc "\n"; output_string oc "\n"; try Hashtbl.iter (fun key value -> fprintf oc " %s\n" key (escape value)) registry; output_string oc ""; close_out oc with e -> close_out oc; raise e let add_validator ~key ~validator ~descr = let id = get_next_validator_id () in Hashtbl.add validators key (validator, descr); id open Pxp_document open Pxp_types open Pxp_yacc let load_from = let config = default_config in let entry = `Entry_document [ `Extend_dtd_fully; `Parse_xml_decl ] in fun fname -> debug_print ("Loading configuration from " ^ fname); let document = parse_wfdocument_entity config (from_file fname) default_spec in let fill_registry () = document#root#iter_nodes (fun n -> try (match n#node_type with | T_element "value" -> let key = n#required_string_attribute "key" in let value = n#data in set ~key ~value | _ -> ()) with exn -> let (fname, line, pos) = n#position in raise (Parse_error (fname, line, pos, "Uncaught exception: " ^ Printexc.to_string exn))) in let backup = backup_registry () in Hashtbl.clear registry; try fill_registry () with exn -> restore_registry backup; raise exn (* DEBUGGING ONLY *) let dump () = Hashtbl.iter (fun k v -> printf "%s = %s\n" k v) registry