]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/registry/helm_registry.ml
- added support for boolean parameters
[helm.git] / helm / ocaml / registry / helm_registry.ml
index 30df896d66846dfee890251dfb163fb927f94740..91fcb67b57225510425da96f47d2f58d10beebaf 100644 (file)
 
 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 Cyclic_definition of string
 exception Type_error of string * string * string (* expected type, value, msg *)
-exception Parse_error of string * int (* file, lineno *)
+exception Parse_error of string * int * int * string  (* file, line, col, msg *)
 exception Invalid_value of (string * string) * string (* key, value, descr *)
 
-exception Unescape_failure
-
 type validator_id = int
 
+  (* root XML tag: used by save_to, ignored by load_from *)
+let root_tag = "helm_registry"
+
 let get_next_validator_id =
   let next_id = ref 0 in
   fun () ->
@@ -50,25 +56,33 @@ let restore_registry backup =
   Hashtbl.clear registry;
   Hashtbl.iter (fun key value -> Hashtbl.replace registry key value) backup
 
-let valid_key_rex_raw = "\\w+(\\.\\w+)*"
-let config_line_raw = sprintf "\\s*(%s)\\s*=\\s*\"(.*)\"\\s*" valid_key_rex_raw
-let comment_rex = Pcre.regexp "^\\s*(#|$)"
-let valid_key_rex = Pcre.regexp ("^" ^ valid_key_rex_raw ^ "$")
-let config_line_rex = Pcre.regexp ("^" ^ config_line_raw ^ "$")
+  (* 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 = Str.regexp ("^" ^ valid_key_rex_raw ^ "$")
+let interpolated_key_rex = Str.regexp ("\\$(" ^ valid_key_rex_raw ^ ")")
+let dot_rex = Str.regexp "\\."
+let spaces_rex = Str.regexp "[ \t\n\r]+"
+let heading_spaces_rex = Str.regexp "^[ \t\n\r]+"
 
-let is_comment s = Pcre.pmatch ~rex:comment_rex s
+let split s =
+  (* trailing blanks are removed per default by split *)
+  Str.split spaces_rex (Str.global_replace heading_spaces_rex "" s)
+let merge l = String.concat " " l
 
-let escape = String.escaped
-let unescape =
-  let lexer = lazy (Genlex.make_lexer []) in
-  fun s ->
-    let tok_stream = Lazy.force lexer (Stream.of_string ("\"" ^ s ^ "\"")) in
-    match Stream.peek tok_stream with
-    | Some (Genlex.String s) -> s
-    | _ -> raise Unescape_failure
+  (* 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
+(*   if not (Pcre.pmatch ~rex:valid_key_rex key) then *)
+  if not (Str.string_match valid_key_rex key 0) then
     raise (Malformed_key key)
 
 let value_is_valid ~key ~value =
@@ -79,23 +93,55 @@ let value_is_valid ~key ~value =
     (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 unset = Hashtbl.remove registry
+
+let env_var_of_key key =
+(*   Pcre.replace ~rex:dot_rex ~templ:"__" (String.uppercase key) *)
+  Str.global_replace dot_rex "__" (String.uppercase key)
+
 let get key =
-  key_is_valid key;
-  try
-    Hashtbl.find registry key
-  with Not_found -> raise (Key_not_found key)
+  let rec aux stack key =
+    key_is_valid key;
+    if List.mem key stack then begin
+      let msg = (String.concat " -> " (List.rev stack)) ^ " -> " ^ key in
+      raise (Cyclic_definition msg)
+    end;
+    let registry_value =  (* internal value *)
+      try
+        Some (Hashtbl.find registry key)
+      with Not_found -> None
+    in
+    let env_value = (* environment value *)
+      try
+        Some (Sys.getenv (env_var_of_key key))
+      with Not_found -> None
+    in
+    let value = (* resulting value *)
+      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)
+    in
+    interpolate (key :: stack) value
+  and interpolate stack value =
+    Str.global_substitute interpolated_key_rex
+      (fun s ->
+        let matched = Str.matched_string s in
+          (* "$(var)" -> "var" *)
+        let key = String.sub matched 2 (String.length matched - 3) in
+        aux stack key)
+      value
+  in
+  aux [] 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
@@ -107,54 +153,116 @@ let mk_get_set type_name (from_string: string -> 'a) (to_string: 'a -> string) =
   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 (get_bool, set_bool) = mk_get_set "bool" bool_of_string string_of_bool
+let (get_string_list, set_string_list) = mk_get_set "string list" split merge
+
+let get_opt getter key =
+  try
+    Some (getter key)
+  with Key_not_found _ -> None
+let set_opt setter ~key ~value =
+  match value with
+  | None -> unset key
+  | Some value -> setter ~key ~value
+
+(*
+let save_to =
+  let dtd = new dtd default_config.warner `Enc_utf8 in
+  let rec create_key node sections key value =
+    match sections with
+    | [] -> create_element_node ~valcheck:false default_spec dtd
+*)
 
 let save_to fname =
+  debug_print ("Saving configuration to " ^ fname);
   let oc = open_out fname in
+  output_string oc "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
+  output_string oc "<helm_registry>\n";
   try
     Hashtbl.iter
       (fun key value ->
-        output_string oc (sprintf "%s = \"%s\"" key (escape value)))
+        fprintf oc "  <value key=\"%s\">%s</value>\n" key (escape value))
       registry;
+    output_string oc "</helm_registry>";
     close_out oc
   with e ->
     close_out oc;
     raise e
 
-let load_from fname =
-  let backup = backup_registry () in
-  Hashtbl.clear registry;
-  let ic = open_in fname in
-  let lineno = ref 0 in
-  try
-    while true do
-      incr lineno;
-      let line = input_line ic in
-      if not (is_comment line) then
-        let subs = Pcre.extract ~rex:config_line_rex line in
-        let (key, value) = (subs.(1), unescape subs.(3)) in
-        set ~key ~value
-    done
-  with
-  | End_of_file -> close_in ic
-  | Malformed_key _ | Unescape_failure | Not_found ->
-      restore_registry backup;
-      raise (Parse_error (fname, !lineno))
-  | e ->
-      close_in ic;
-      restore_registry backup;
-      raise e
-
 let add_validator ~key ~validator ~descr =
   let id = get_next_validator_id () in
   Hashtbl.add validators key (validator, descr);
   id
 
-(*
-  (* DEBUGGING *)
+open Pxp_document
+open Pxp_types
+open Pxp_yacc
+
+let load_from_absolute =
+  let config = default_config in
+  let entry = `Entry_document [ `Extend_dtd_fully; `Parse_xml_decl ] in
+  let fold_key key_stack key =
+    match key_stack with
+    | [] -> key
+    | _ -> String.concat "." key_stack ^ "." ^ key
+  in
+  fun fname ->
+    debug_print ("Loading configuration from " ^ fname);
+    let document =
+      parse_wfdocument_entity config (from_file fname) default_spec
+    in
+    let rec aux key_stack node =
+      node#iter_nodes (fun n ->
+        try
+          (match n#node_type with
+          | T_element "section" ->
+              let section = n#required_string_attribute "name" in
+              aux (key_stack @ [section]) n
+          | T_element "key" ->
+              let key = n#required_string_attribute "name" in
+              let value = n#data in
+              set ~key:(fold_key key_stack 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
+      aux [] document#root
+    with exn ->
+      restore_registry backup;
+      raise exn
+
+let load_from ?path fname =
+  if Filename.is_relative fname then begin
+    let no_file_found = ref true in
+    let path =
+      match path with
+      | Some path -> path (* path given as argument *)
+      | None -> [ Sys.getcwd () ] (* no path given, try with cwd *)
+    in
+    List.iter
+      (fun dir ->
+        let conffile = dir ^ "/" ^ fname in
+        if Sys.file_exists conffile then begin
+          no_file_found := false;
+          load_from_absolute conffile
+        end)
+       path;
+    if !no_file_found then
+      failwith (sprintf
+        "Helm_registry.init: no configuration file named %s in [ %s ]"
+        fname (String.concat "; " path))
+  end else
+    load_from_absolute fname
+
+  (* DEBUGGING ONLY *)
+
 let dump () = Hashtbl.iter (fun k v -> printf "%s = %s\n" k v) registry
-*)