settings: Extend parser so we can parse settings from a string
[strongswan.git] / src / libstrongswan / settings / settings_lexer.l
index f287761..ce9d4ee 100644 (file)
  * for more details.
  */
 
  * for more details.
  */
 
-#include "settings_parser.h"
-
 #include <utils/parser_helper.h>
 
 #include <utils/parser_helper.h>
 
+#include "settings_parser.h"
+
 bool settings_parser_open_next_file(parser_helper_t *ctx);
 
 static void include_files(parser_helper_t *ctx);
 
 %}
 %option debug
 bool settings_parser_open_next_file(parser_helper_t *ctx);
 
 static void include_files(parser_helper_t *ctx);
 
 %}
 %option debug
-%option perf-report
-
 %option warn
 
 /* use start conditions stack */
 %option warn
 
 /* use start conditions stack */
@@ -84,12 +82,22 @@ static void include_files(parser_helper_t *ctx);
 <inc>{
        /* we allow all characters except #, } and spaces, they can be escaped */
        <<EOF>>                         |
 <inc>{
        /* we allow all characters except #, } and spaces, they can be escaped */
        <<EOF>>                         |
-       \n|#.*\n                        |
-       [\t ]                           {
-               if (*yytext && yytext[strlen(yytext) - 1] == '\n')
-               {       /* put the newline back to fix the line numbers */
-                       unput('\n');
-                       yy_set_bol(0);
+       [#}\n\t ]                       {
+               if (*yytext)
+               {
+                       switch (yytext[0])
+                       {
+                               case '\n':
+                                       /* put the newline back to fix the line numbers */
+                                       unput('\n');
+                                       yy_set_bol(0);
+                                       break;
+                               case '#':
+                               case '}':
+                                       /* these are parsed outside of this start condition */
+                                       unput(yytext[0]);
+                                       break;
+                       }
                }
                include_files(yyextra);
                yy_pop_state(yyscanner);
                }
                include_files(yyextra);
                yy_pop_state(yyscanner);
@@ -111,16 +119,11 @@ static void include_files(parser_helper_t *ctx);
 <str>{
        "\""                            |
        <<EOF>>                         |
 <str>{
        "\""                            |
        <<EOF>>                         |
-       \n                                      |
        \\                                      {
                if (!streq(yytext, "\""))
                {
        \\                                      {
                if (!streq(yytext, "\""))
                {
-                       if (streq(yytext, "\n"))
-                       {       /* put the newline back to fix the line numbers */
-                               unput('\n');
-                               yy_set_bol(0);
-                       }
                        PARSER_DBG1(yyextra, "unterminated string detected");
                        PARSER_DBG1(yyextra, "unterminated string detected");
+                       return STRING_ERROR;
                }
                if (yy_top_state(yyscanner) == inc)
                {       /* string include */
                }
                if (yy_top_state(yyscanner) == inc)
                {       /* string include */
@@ -138,12 +141,9 @@ static void include_files(parser_helper_t *ctx);
        \\n     yyextra->string_add(yyextra, "\n");
        \\r     yyextra->string_add(yyextra, "\r");
        \\t     yyextra->string_add(yyextra, "\t");
        \\n     yyextra->string_add(yyextra, "\n");
        \\r     yyextra->string_add(yyextra, "\r");
        \\t     yyextra->string_add(yyextra, "\t");
-       \\b     yyextra->string_add(yyextra, "\b");
-       \\f     yyextra->string_add(yyextra, "\f");
-       \\(.|\n)                        {
-               yyextra->string_add(yyextra, yytext+1);
-       }
-       [^\\\n"]+                       {
+       \\\r?\n /* merge lines that end with EOL characters */
+       \\.     yyextra->string_add(yyextra, yytext+1);
+       [^\\"]+                 {
                yyextra->string_add(yyextra, yytext);
        }
 }
                yyextra->string_add(yyextra, yytext);
        }
 }
@@ -163,7 +163,7 @@ static void include_files(parser_helper_t *ctx);
  */
 bool settings_parser_open_next_file(parser_helper_t *ctx)
 {
  */
 bool settings_parser_open_next_file(parser_helper_t *ctx)
 {
-       parser_helper_file_t *file;
+       FILE *file;
 
        file = ctx->file_next(ctx);
        if (!file)
 
        file = ctx->file_next(ctx);
        if (!file)
@@ -171,9 +171,9 @@ bool settings_parser_open_next_file(parser_helper_t *ctx)
                return FALSE;
        }
 
                return FALSE;
        }
 
-       settings_parser_set_in(file->file, ctx->scanner);
+       settings_parser_set_in(file, ctx->scanner);
        settings_parser_push_buffer_state(
        settings_parser_push_buffer_state(
-                       settings_parser__create_buffer(file->file, YY_BUF_SIZE,
+                       settings_parser__create_buffer(file, YY_BUF_SIZE,
                                                                                   ctx->scanner), ctx->scanner);
        return TRUE;
 }
                                                                                   ctx->scanner), ctx->scanner);
        return TRUE;
 }
@@ -191,3 +191,11 @@ static void include_files(parser_helper_t *ctx)
 
        settings_parser_open_next_file(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);
+}