X-Git-Url: https://git.octo.it/?p=liboconfig.git;a=blobdiff_plain;f=src%2Fparser.y;fp=src%2Fparser.y;h=82e89a987a307e28a5f00bd9f8cff3da02cbd04a;hp=a4ebce0a022ac63999c5f8bf039bc36a008c05a2;hb=87b8488ffc92d9ed9952146486ef42b43f44b5f5;hpb=042ec8b4b5b7cabc9ff8833faab7fc9e6ce8b0f3 diff --git a/src/parser.y b/src/parser.y index a4ebce0..82e89a9 100644 --- a/src/parser.y +++ b/src/parser.y @@ -17,6 +17,7 @@ struct argument_list_s }; typedef struct argument_list_s argument_list_t; +static char *unquote (const char *orig); static void dump_ci (oconfig_item_t *ci, int shift); %} @@ -53,8 +54,8 @@ static void dump_ci (oconfig_item_t *ci, int shift); %% string: - QUOTED_STRING {$$ = $1;} - | UNQUOTED_STRING {$$ = $1;} + QUOTED_STRING {$$ = unquote ($1);} + | UNQUOTED_STRING {$$ = strdup ($1);} ; argument: @@ -81,7 +82,7 @@ argument_list: ; identifier: - UNQUOTED_STRING {$$ = $1;} + UNQUOTED_STRING {$$ = strdup ($1);} ; option: @@ -115,7 +116,11 @@ block: block_begin statement_list block_end { if (strcmp ($1.key, $3) != 0) - yyerror ("Block %s not closed..\n", $1); + { + printf ("block_begin = %s; block_end = %s;\n", $1.key, $3); + yyerror ("Block not closed..\n"); + exit (1); + } $$ = $1; $$.children = $2.statement; $$.children_num = $2.statement_num; @@ -125,22 +130,33 @@ block: statement: option {$$ = $1;} | block {$$ = $1;} - | EOL {/* ignore */} + | EOL {$$.values_num = 0;} ; statement_list: statement_list statement { $$ = $1; - $$.statement_num++; - $$.statement = realloc ($$.statement, $$.statement_num * sizeof (oconfig_item_t)); - $$.statement[$$.statement_num-1] = $2; + if ($2.values_num > 0) + { + $$.statement_num++; + $$.statement = realloc ($$.statement, $$.statement_num * sizeof (oconfig_item_t)); + $$.statement[$$.statement_num-1] = $2; + } } | statement { - $$.statement = malloc (sizeof (oconfig_item_t)); - $$.statement[0] = $1; - $$.statement_num = 1; + if ($1.values_num > 0) + { + $$.statement = malloc (sizeof (oconfig_item_t)); + $$.statement[0] = $1; + $$.statement_num = 1; + } + else + { + $$.statement = NULL; + $$.statement_num = 0; + } } ; @@ -174,6 +190,36 @@ int main (int argc, char **argv) return (0); } +static char *unquote (const char *orig) +{ + char *ret = strdup (orig); + int len; + int i; + + if (ret == NULL) + return (NULL); + + len = strlen (ret); + + if ((len < 2) || (ret[0] != '"') || (ret[len - 1] != '"')) + return (ret); + + ret++; + len -= 2; + ret[len] = '\0'; + + for (i = 0; i < len; i++) + { + if (ret[i] == '\\') + { + memmove (ret + i, ret + (i + 1), len - i); + len--; + } + } + + return (ret); +} /* char *unquote */ + static void dump_ci (oconfig_item_t *ci, int shift) { int i;