Commit c172a9cfc6acd58b6f7407e64c27215d08f69249

Authored by totoro
1 parent 7d062617

add yaml processing

file_processor_io/yaml/yaml.anubis 0 → 100644
  1 +/*
  2 + * Created by PyramIDE.
  3 + * User: フランスのトトロ aka (David RENÉ)
  4 + * Date: 07/06/2016
  5 + * Time: 23:15
  6 + * © Calexium
  7 + */
  8 +
  9 +read tools/streams.anubis
  10 +
  11 +transmit yaml_type.anubis
  12 +read yaml_parser.anubis
  13 +
  14 +public define Maybe(YAML_Value)
  15 + get_value
  16 + (
  17 + List(YAML_Key_Value) key_values,
  18 + String search_key
  19 + )=
  20 + if key_values is
  21 + {
  22 + [] then failure,
  23 + [h . t] then
  24 + since h is key_value(key, value),
  25 + if search_key = key then
  26 + success(*value)
  27 + else
  28 + get_value(t, search_key)
  29 + }.
  30 +
  31 +public define Maybe(List(YAML_Key_Value))
  32 + get_map_in_list_entry
  33 + (
  34 + List(YAML_Value) list_values,
  35 + String search_key
  36 + )=
  37 + if list_values is
  38 + {
  39 + [] then failure,
  40 + [h . t] then
  41 + //check if the first entry of the list value is a map
  42 + if h is map(key_values) then
  43 + //if yes check if the map's first entry key is equal to search key
  44 + if *key_values is
  45 + {
  46 + [] then get_map_in_list_entry(t, search_key),
  47 + [_key_value . _] then
  48 + since _key_value is key_value(key, value),
  49 + if key = search_key then
  50 + //check if the entry is map
  51 + if *value is map(_key_values) then
  52 + success(*_key_values)
  53 + else
  54 + get_map_in_list_entry(t, search_key)
  55 + else
  56 + get_map_in_list_entry(t, search_key)
  57 + }
  58 + else
  59 + get_map_in_list_entry(t, search_key)
  60 + }.
  61 +
  62 +public define Maybe(YAML_Value)
  63 + get_YAML_value
  64 + (
  65 + List(YAML_Key_Value) root,
  66 + List(String) path
  67 + )=
  68 + if path is
  69 + {
  70 + [] then failure,
  71 + [current_name . tail] then
  72 + with final = if tail is [] then true else false,
  73 + if get_value(root, current_name) is
  74 + {
  75 + failure then failure,
  76 + success(r_value) then
  77 + //if is the final round hence we return the result
  78 + if final then
  79 + success(r_value)
  80 +
  81 + // NOT Final round, continue to recurse on path to get targeted YAML_Value
  82 + else if r_value is map(new_root) then
  83 + //this is a map hencec recurse next
  84 + get_YAML_value(*new_root, tail)
  85 +
  86 + //this is a list.
  87 + else if r_value is list(current_list) then
  88 + //Hence try to get an entry (1st level only) with a yaml_value which is YAML_Key_Value
  89 + //get the map within a list which the name of the key is current_name
  90 + if get_map_in_list_entry(*current_list, current_name) is
  91 + {
  92 + failure then failure,
  93 + success(new_root) then get_YAML_value(new_root, tail)
  94 + }
  95 +
  96 + else
  97 + failure
  98 + }
  99 + }
  100 + .
  101 +
  102 +public define Maybe(Bool)
  103 + get_Bool
  104 + (
  105 + List(YAML_Key_Value) root,
  106 + List(String) path
  107 + )=
  108 + if get_YAML_value(root, path) is
  109 + {
  110 + failure then failure,
  111 + success(y_value) then
  112 + if y_value is bool(vb_value) then
  113 + success(*vb_value)
  114 + else
  115 + failure
  116 + }.
  117 +
  118 +public define Bool
  119 + get_Bool
  120 + (
  121 + List(YAML_Key_Value) root,
  122 + List(String) path,
  123 + Bool default
  124 + )=
  125 + if get_Bool(root, path) is
  126 + {
  127 + failure then default,
  128 + success(b_value) then b_value
  129 + }.
  130 +
  131 +public define YAML
  132 + load
  133 + (
  134 + String yaml_file
  135 + )=
  136 + if (Maybe(RStream))file(yaml_file, read) is
  137 + {
  138 + failure then yaml(var([])),
  139 + success(f) then parse_YAML_file(make_stream(f))
  140 + }.
  141 +
  142 +global define One
  143 + yaml_test
  144 + (
  145 + List(String) dummy
  146 + )=
  147 + forget(load(my_anubis_directory+"/IDPS/suricata/suricata.yaml"))
  148 + .
  149 +
... ...
file_processor_io/yaml/yaml_parser.anubis 0 → 100644
  1 +/*
  2 + * Created by PyramIDE.
  3 + * User: フランスのトトロ aka (David RENÉ)
  4 + * Date: 07/06/2016
  5 + * Time: 23:15
  6 + * © Calexium
  7 + */
  8 +
  9 +
  10 +read yaml_type.anubis
  11 +read tools/streams.anubis
  12 +read tools/string.anubis
  13 +read system/string.anubis
  14 +
  15 +public define Maybe(YAML_Document)
  16 + get_yaml_document_content
  17 + (
  18 + Stream s,
  19 + YAML_Document doc
  20 + )=
  21 + if read_line(s) is
  22 + {
  23 + failure then success(doc),
  24 + success(_line) then
  25 + with line = trim(_line),
  26 + //check for comment
  27 + if start_with(line, "#") then
  28 + //println("skipping comment ["+line+"]");
  29 + get_yaml_document_content(s, doc)
  30 + //check for blank line
  31 + else if length(line) = 0 then
  32 + //println("skipping blank line");
  33 + get_yaml_document_content(s, doc)
  34 + //check for item of the list
  35 + else if start_with(line, "- ") then
  36 + println("list item ["+line+"]");
  37 + get_yaml_document_content(s, doc)
  38 + //check for entry of the map
  39 + else
  40 + if find(":", line, 0) is
  41 + {
  42 + failure then
  43 + println("unrecognize line ["+line+"]");
  44 + get_yaml_document_content(s, doc),
  45 + success(pos) then
  46 + println("map entry ["+line+"]");
  47 + get_yaml_document_content(s, doc)
  48 + }
  49 + }.
  50 +
  51 +public define Maybe(YAML_Document)
  52 + get_yaml_document
  53 + (
  54 + Stream s,
  55 + )=
  56 + if read_line(s) is
  57 + {
  58 + failure then failure,
  59 + success(line) then
  60 + if start_with(line, "---") then
  61 + println("Start parsing YAML document");
  62 + get_yaml_document_content(s, yaml_document(var([])))
  63 + else
  64 + get_yaml_document(s)
  65 + }.
  66 +
  67 +public define YAML
  68 + parse_YAML_file
  69 + (
  70 + Stream s,
  71 + Var(List(YAML_Document)) docs
  72 + )=
  73 + if get_yaml_document(s) is
  74 + {
  75 + failure then yaml(var(reverse(*docs))),
  76 + success(doc) then
  77 + docs <- [doc . *docs];
  78 + parse_YAML_file(s, docs)
  79 + }.
  80 +
  81 +public define YAML
  82 + parse_YAML_file
  83 + (
  84 + Stream s
  85 + )=
  86 + if read_line(s) is
  87 + {
  88 + failure then yaml(var([])),
  89 + success(line) then
  90 + if start_with(line, "%YAML 1.1") then
  91 + println("%YAML 1.1 file");
  92 + parse_YAML_file(s, var([]))
  93 + else
  94 + yaml(var([]))
  95 + }.
  96 +
... ...
file_processor_io/yaml/yaml_type.anubis 0 → 100644
  1 +/*
  2 + * Created by PyramIDE.
  3 + * User: フランスのトトロ aka (David RENÉ)
  4 + * Date: 01/06/2016
  5 + * Time: 21:01
  6 + * © Calexium
  7 + */
  8 +
  9 +
  10 + sequence list array (items)
  11 + map (key: value)
  12 + value : int
  13 + string
  14 + float
  15 +
  16 +public type YAML_Key_Value:...
  17 +
  18 +public type YAML_Value:
  19 + //comment(String value),
  20 + //blank,
  21 + string(Var(String) s_value),
  22 + int (Var(Int) i_value),
  23 + //float (Float value),
  24 + bool (Var(Bool) b_value),
  25 + map (Var(List(YAML_Key_Value)) key_values),
  26 + list (Var(List(YAML_Value)) values).
  27 + //list(List(YAML_Value) values),
  28 + //array(List(YAML_Key_Value)).
  29 +
  30 +public type YAML_Key_Value:
  31 + key_value(String key, Var(YAML_Value) y_value).
  32 +
  33 +public type YAML_Node:
  34 + map(Var(List(YAML_Key_Value)) key_values),
  35 + list(Var(List(YAML_Value)) values).
  36 +
  37 +public type YAML_Document:
  38 + yaml_document(Var(List(YAML_Node)) nodes).
  39 +
  40 +public type YAML:
  41 + yaml(Var(List(YAML_Document)) docs).
  42 +
  43 + /**
  44 + * get_value(document, ["outputs", "eve-log", "enabled"])
  45 + */
  46 +
... ...