yaml.anubis 3.65 KB
/*
 * Created by PyramIDE.
 * User: フランスのトトロ aka (David RENÉ) 
 * Date: 07/06/2016
 * Time: 23:15
 * © Calexium 
 */

read tools/streams.anubis
    
transmit yaml_type.anubis
read yaml_parser.anubis

public define Maybe(YAML_Value)
  get_value
  (
    List(YAML_Key_Value)  key_values,
    String                search_key
  )=
  if key_values is 
  {
    []      then failure,
    [h . t] then
      since h is key_value(key, value),
      if search_key = key then
        success(*value)
      else
        get_value(t, search_key)
  }.
  
public define Maybe(List(YAML_Key_Value))
  get_map_in_list_entry
  (
    List(YAML_Value) list_values,
    String           search_key
  )=
  if list_values is
  {
    []      then failure,
    [h . t] then
      //check if the first entry of the list value is a map
      if h is map(key_values) then
      //if yes check if the map's first entry key is equal to search key
        if *key_values is
        {
          []                then  get_map_in_list_entry(t, search_key),
          [_key_value . _]  then
            since _key_value is key_value(key, value),
              if key = search_key then
                //check if the entry is map
                if *value is map(_key_values) then
                  success(*_key_values)
                else
                  get_map_in_list_entry(t, search_key)
              else
                get_map_in_list_entry(t, search_key)
        }
      else
        get_map_in_list_entry(t, search_key)
  }.
  
public define Maybe(YAML_Value)
  get_YAML_value
  (
    List(YAML_Key_Value)  root,
    List(String)          path
  )=
  if path is 
  {
    []                      then failure,
    [current_name . tail]   then
      with final = if tail is [] then true else false,
      if get_value(root, current_name) is
      {
        failure           then failure,
        success(r_value)  then
          //if is the final round hence we return the result
          if final then
            success(r_value)
            
          // NOT Final round, continue to recurse on path to get targeted YAML_Value
          else if r_value is map(new_root) then
            //this is a map hencec recurse next
            get_YAML_value(*new_root, tail)
            
            //this is a list. 
          else if r_value is list(current_list) then
            //Hence try to get an entry (1st level only) with a yaml_value which is YAML_Key_Value
            //get the map within a list which the name of the key is current_name
            if get_map_in_list_entry(*current_list, current_name) is
            {
              failure           then failure,
              success(new_root) then get_YAML_value(new_root, tail)
            }
            
          else
            failure
      }
  }
  .
  
public define Maybe(Bool)
  get_Bool
  (
    List(YAML_Key_Value)  root,
    List(String)          path
  )=
  if get_YAML_value(root, path) is
  {
    failure           then  failure,
    success(y_value)  then
      if y_value is bool(vb_value) then
        success(*vb_value)
      else
        failure
  }.
  
public define Bool
  get_Bool
  (
    List(YAML_Key_Value)  root,
    List(String)          path,
    Bool                  default
  )=
  if get_Bool(root, path) is
  {
    failure           then default,
    success(b_value)  then b_value
  }.
  
public define YAML
  load
  (
    String  yaml_file
  )=
  if (Maybe(RStream))file(yaml_file, read) is
  {
     failure    then yaml(var([])),
     success(f) then parse_YAML_file(make_stream(f))
  }.  
  
global define One
  yaml_test
  (
    List(String) dummy
  )=
  forget(load(my_anubis_directory+"/IDPS/suricata/suricata.yaml"))
  .