]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #2534 in SNORT/snort3 from ~SELYSENK/snort3:wizard_dump_config...
authorBhagya Tholpady (bbantwal) <bbantwal@cisco.com>
Tue, 20 Oct 2020 23:51:03 +0000 (23:51 +0000)
committerBhagya Tholpady (bbantwal) <bbantwal@cisco.com>
Tue, 20 Oct 2020 23:51:03 +0000 (23:51 +0000)
Squashed commit of the following:

commit c9a30bcd84350ec29b7e05a10dadf0740605a25d
Author: Serhii Lysenko <selysenk@cisco.com>
Date:   Thu Oct 8 16:35:43 2020 +0300

    dump_config: don't print names for list elements

src/dump_config/json_config_output.cc
src/dump_config/text_config_output.cc
src/dump_config/text_config_output.h
src/main/shell.cc

index 73d34d354449bebbcfe6d7951ff15ecc4f18665e..9e52c9cb129d65996df5840a824e735f0a940066 100644 (file)
@@ -27,7 +27,7 @@
 
 using namespace snort;
 
-static void dump_value(JsonStream& json, const BaseConfigNode* node)
+static void dump_value(JsonStream& json, const char* node_name, const BaseConfigNode* node)
 {
     const Value* value = node->get_value();
     if ( !value )
@@ -37,11 +37,11 @@ static void dump_value(JsonStream& json, const BaseConfigNode* node)
     {
     case Parameter::PT_BOOL:
     case Parameter::PT_IMPLIED:
-        value->get_bool() ? json.put_true(node->get_name().c_str()) :
-            json.put_false(node->get_name().c_str());
+        value->get_bool() ? json.put_true(node_name) :
+            json.put_false(node_name);
         break;
     case Parameter::PT_INT:
-        json.put(node->get_name().c_str(), value->get_long());
+        json.put(node_name, value->get_long());
         break;
     case Parameter::PT_REAL:
     {
@@ -51,35 +51,40 @@ static void dump_value(JsonStream& json, const BaseConfigNode* node)
         if ( pos != std::string::npos )
             precision = value_str.size() - pos - 1;
 
-        json.put(node->get_name().c_str(), value->get_real(), precision);
+        json.put(node_name, value->get_real(), precision);
         break;
     }
     default:
-        json.put(node->get_name().c_str(), value->get_origin_string());
+        json.put(node_name, value->get_origin_string());
         break;
     }
 }
 
-static void dump_modules(JsonStream& json, const BaseConfigNode* node)
+static void dump_tree(JsonStream& json, const BaseConfigNode* node, bool list_node = false)
 {
-    Parameter::Type type = node->get_type();
-    if ( type == Parameter::PT_LIST )
-        json.open_array(node->get_name().c_str());
-    else if ( type == Parameter::PT_TABLE )
-    {
-        std::string name = node->get_name();
-        name.empty() ? json.open() : json.open(name.c_str());
-    }
-    else
-        dump_value(json, node);
+    Parameter::Type node_type = node->get_type();
+    const std::string node_name = node->get_name();
+    const char* node_name_cstr = nullptr;
 
-    for ( const auto n : node->get_children() )
-        dump_modules(json, n);
+    if ( !list_node )
+        node_name_cstr = node_name.c_str();
 
-    if ( type == Parameter::PT_LIST )
-        json.close_array();
-    else if ( type == Parameter::PT_TABLE )
+    if ( node_type == Parameter::PT_TABLE )
+    {
+        json.open(node_name_cstr);
+        for ( const auto n : node->get_children() )
+            dump_tree(json, n);
         json.close();
+    }
+    else if ( node_type == Parameter::PT_LIST )
+    {
+        json.open_array(node_name_cstr);
+        for ( const auto n : node->get_children() )
+            dump_tree(json, n, true);
+        json.close_array();
+    }
+    else
+        dump_value(json, node_name_cstr, node);
 }
 
 JsonAllConfigOutput::JsonAllConfigOutput() :
@@ -96,7 +101,7 @@ void JsonAllConfigOutput::dump(const ConfigData& config_data)
     json.open("config");
 
     for ( const auto config_tree: config_data.config_trees )
-        dump_modules(json, config_tree);
+        dump_tree(json, config_tree);
 
     json.close();
     json.close();
@@ -107,7 +112,7 @@ void JsonTopConfigOutput::dump(const ConfigData& config_data)
     json.open();
 
     for ( const auto config_tree: config_data.config_trees )
-        dump_modules(json, config_tree);
+        dump_tree(json, config_tree);
 
     json.close();
 }
index f8607c5f5cca9a2063252f86f9b30b4d8fb9f3d0..75407ac885d792846c1cc29f0a4d66df934b43b0 100644 (file)
@@ -26,7 +26,7 @@
 
 using namespace snort;
 
-void TextConfigOutput::dump_value(const BaseConfigNode* node, const std::string& config_name)
+static void dump_value(const BaseConfigNode* node, const std::string& config_name)
 {
     const Value* value = node->get_value();
     if ( !value )
@@ -56,32 +56,29 @@ void TextConfigOutput::dump_value(const BaseConfigNode* node, const std::string&
     }
 }
 
-void TextConfigOutput::dump_modules(const BaseConfigNode* parent, const std::string& config_name)
+static void dump_tree(const BaseConfigNode* node, const std::string& config_name)
 {
-    static char buf[16];
-    int list_index = 0;
-    for ( const auto node : parent->get_children() )
-    {
-        std::string full_config_name(config_name);
-        std::string node_name = node->get_name();
+    Parameter::Type node_type = node->get_type();
 
-        if ( !node_name.empty() )
-        {
-            full_config_name += ".";
-            full_config_name += node_name;
-        }
-        else
+    if ( node_type == Parameter::PT_TABLE )
+    {
+        for ( const auto child : node->get_children() )
+            dump_tree(child, config_name + "." + child->get_name());
+    }
+    else if ( node_type == Parameter::PT_LIST )
+    {
+        char suffix[16];
+        int list_index = 0;
+        for ( const auto child : node->get_children() )
         {
-            sprintf(buf, "%i", list_index++);
-            full_config_name += "[";
-            full_config_name += buf;
-            full_config_name += "]";
+            snprintf(suffix, 16, "[%i]", list_index);
+            const std::string full_config_name = config_name + suffix;
+            dump_tree(child, full_config_name);
+            list_index++;
         }
-
-        dump_modules(node, full_config_name);
     }
-
-    dump_value(parent, config_name);
+    else
+        dump_value(node, config_name);
 }
 
 void TextConfigOutput::dump(const ConfigData& config_data)
@@ -91,6 +88,6 @@ void TextConfigOutput::dump(const ConfigData& config_data)
     std::cout << output << std::endl;
 
     for ( const auto config_tree: config_data.config_trees )
-        dump_modules(config_tree, config_tree->get_name());
+        dump_tree(config_tree, config_tree->get_name());
 }
 
index 92affbbf3673a5088c51cf34e7769ae8f9f7edfe..da36efea59e963d7c47d7ac4b8eb8e14fd85e262 100644 (file)
@@ -33,9 +33,6 @@ public:
 
 private:
     void dump(const ConfigData&) override;
-
-    void dump_modules(const BaseConfigNode* parent, const std::string& config_name);
-    void dump_value(const BaseConfigNode* parent, const std::string& config_name);
 };
 
 #endif // TEXT_CONFIG_OUTPUT_H
index 4fdc8b60c4965eac39b2366046a75ebdf78bcb87..c924536ffd76d19ed1429568e2142b32d2ca4e1d 100644 (file)
@@ -244,14 +244,10 @@ void Shell::set_config_value(const std::string& fqn, const snort::Value& value)
     if ( !s_config_output || !s_current_node )
         return;
 
-    // lua interpreter does not call open_table for simple list items like (string)
-    // we have to add tree node for this item
+    // don't give names to list elements
     if ( s_current_node->get_type() == Parameter::PT_LIST )
     {
-        add_config_child_node("", Parameter::PT_TABLE);
-        s_current_node->add_child_node(new ValueConfigNode(s_current_node, value));
-        s_current_node = s_current_node->get_parent_node();
-
+        s_current_node->add_child_node(new ValueConfigNode(s_current_node, value, ""));
         return;
     }