settings: Extend parser so we can parse settings from a string
authorTobias Brunner <tobias@strongswan.org>
Thu, 23 Oct 2014 14:57:24 +0000 (16:57 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 28 Jul 2015 11:27:32 +0000 (13:27 +0200)
src/libstrongswan/settings/settings_lexer.l
src/libstrongswan/settings/settings_parser.y

index 3c955aa..ce9d4ee 100644 (file)
@@ -191,3 +191,11 @@ static void include_files(parser_helper_t *ctx)
 
        settings_parser_open_next_file(ctx);
 }
+
+/**
+ * Load the given string to be parsed next
+ */
+void settings_parser_load_string(parser_helper_t *ctx, const char *content)
+{
+       settings_parser__scan_string(content, ctx->scanner);
+}
index ed30d19..96ab36f 100644 (file)
@@ -39,6 +39,7 @@ int settings_parser_get_leng(void *scanner);
 int settings_parser_get_lineno(void *scanner);
 /* Custom functions in lexer */
 bool settings_parser_open_next_file(parser_helper_t *ctx);
+bool settings_parser_load_string(parser_helper_t *ctx, const char *content);
 
 /**
  * Forward declarations
@@ -286,3 +287,39 @@ bool settings_parser_parse_file(section_t *root, char *name)
        helper->destroy(helper);
        return success;
 }
+
+/**
+ * Parse the given string and add all sections and key/value pairs to the
+ * given section.
+ */
+bool settings_parser_parse_string(section_t *root, char *settings)
+{
+       parser_helper_t *helper;
+       array_t *sections = NULL;
+       bool success = FALSE;
+
+       array_insert_create(&sections, ARRAY_TAIL, root);
+       helper = parser_helper_create(sections);
+       helper->get_lineno = settings_parser_get_lineno;
+       if (settings_parser_lex_init_extra(helper, &helper->scanner) != 0)
+       {
+               helper->destroy(helper);
+               array_destroy(sections);
+               return FALSE;
+       }
+       settings_parser_load_string(helper, settings);
+       if (getenv("DEBUG_SETTINGS_PARSER"))
+       {
+               yydebug = 1;
+               settings_parser_set_debug(1, helper->scanner);
+       }
+       success = yyparse(helper) == 0;
+       if (!success)
+       {
+               DBG1(DBG_CFG, "failed to parse settings '%s'", settings);
+       }
+       array_destroy(sections);
+       settings_parser_lex_destroy(helper->scanner);
+       helper->destroy(helper);
+       return success;
+}