diff --git a/file_processor_io/yaml/yaml.anubis b/file_processor_io/yaml/yaml.anubis new file mode 100644 index 0000000..9485e92 --- /dev/null +++ b/file_processor_io/yaml/yaml.anubis @@ -0,0 +1,149 @@ +/* + * 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")) + . + diff --git a/file_processor_io/yaml/yaml_parser.anubis b/file_processor_io/yaml/yaml_parser.anubis new file mode 100644 index 0000000..460db7c --- /dev/null +++ b/file_processor_io/yaml/yaml_parser.anubis @@ -0,0 +1,96 @@ +/* + * Created by PyramIDE. + * User: フランスのトトロ aka (David RENÉ) + * Date: 07/06/2016 + * Time: 23:15 + * © Calexium + */ + + +read yaml_type.anubis +read tools/streams.anubis +read tools/string.anubis +read system/string.anubis + +public define Maybe(YAML_Document) + get_yaml_document_content + ( + Stream s, + YAML_Document doc + )= + if read_line(s) is + { + failure then success(doc), + success(_line) then + with line = trim(_line), + //check for comment + if start_with(line, "#") then + //println("skipping comment ["+line+"]"); + get_yaml_document_content(s, doc) + //check for blank line + else if length(line) = 0 then + //println("skipping blank line"); + get_yaml_document_content(s, doc) + //check for item of the list + else if start_with(line, "- ") then + println("list item ["+line+"]"); + get_yaml_document_content(s, doc) + //check for entry of the map + else + if find(":", line, 0) is + { + failure then + println("unrecognize line ["+line+"]"); + get_yaml_document_content(s, doc), + success(pos) then + println("map entry ["+line+"]"); + get_yaml_document_content(s, doc) + } + }. + +public define Maybe(YAML_Document) + get_yaml_document + ( + Stream s, + )= + if read_line(s) is + { + failure then failure, + success(line) then + if start_with(line, "---") then + println("Start parsing YAML document"); + get_yaml_document_content(s, yaml_document(var([]))) + else + get_yaml_document(s) + }. + +public define YAML + parse_YAML_file + ( + Stream s, + Var(List(YAML_Document)) docs + )= + if get_yaml_document(s) is + { + failure then yaml(var(reverse(*docs))), + success(doc) then + docs <- [doc . *docs]; + parse_YAML_file(s, docs) + }. + +public define YAML + parse_YAML_file + ( + Stream s + )= + if read_line(s) is + { + failure then yaml(var([])), + success(line) then + if start_with(line, "%YAML 1.1") then + println("%YAML 1.1 file"); + parse_YAML_file(s, var([])) + else + yaml(var([])) + }. + diff --git a/file_processor_io/yaml/yaml_type.anubis b/file_processor_io/yaml/yaml_type.anubis new file mode 100644 index 0000000..2bc25c5 --- /dev/null +++ b/file_processor_io/yaml/yaml_type.anubis @@ -0,0 +1,46 @@ +/* + * Created by PyramIDE. + * User: フランスのトトロ aka (David RENÉ) + * Date: 01/06/2016 + * Time: 21:01 + * © Calexium + */ + + + sequence list array (items) + map (key: value) + value : int + string + float + +public type YAML_Key_Value:... + +public type YAML_Value: + //comment(String value), + //blank, + string(Var(String) s_value), + int (Var(Int) i_value), + //float (Float value), + bool (Var(Bool) b_value), + map (Var(List(YAML_Key_Value)) key_values), + list (Var(List(YAML_Value)) values). + //list(List(YAML_Value) values), + //array(List(YAML_Key_Value)). + +public type YAML_Key_Value: + key_value(String key, Var(YAML_Value) y_value). + +public type YAML_Node: + map(Var(List(YAML_Key_Value)) key_values), + list(Var(List(YAML_Value)) values). + +public type YAML_Document: + yaml_document(Var(List(YAML_Node)) nodes). + +public type YAML: + yaml(Var(List(YAML_Document)) docs). + + /** + * get_value(document, ["outputs", "eve-log", "enabled"]) + */ + -- libgit2 0.21.4