Code

parser: Don't leak memory when building nested matchers.
[sysdb.git] / src / frontend / grammar.y
1 /*
2  * SysDB - src/frontend/grammar.y
3  * Copyright (C) 2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 %{
30 #include "frontend/connection-private.h"
31 #include "frontend/parser.h"
32 #include "frontend/grammar.h"
34 #include "core/store.h"
35 #include "core/store-private.h"
37 #include "utils/error.h"
38 #include "utils/llist.h"
40 #include <stdio.h>
41 #include <string.h>
43 int
44 sdb_fe_yylex(YYSTYPE *yylval, YYLTYPE *yylloc, sdb_fe_yyscan_t yyscanner);
46 sdb_fe_yyextra_t *
47 sdb_fe_yyget_extra(sdb_fe_yyscan_t scanner);
49 void
50 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg);
52 /* quick access to the current parse tree */
53 #define pt sdb_fe_yyget_extra(scanner)->parsetree
55 /* quick access to the parser mode */
56 #define parser_mode sdb_fe_yyget_extra(scanner)->mode
58 %}
60 %pure-parser
61 %lex-param {sdb_fe_yyscan_t scanner}
62 %parse-param {sdb_fe_yyscan_t scanner}
63 %locations
64 %error-verbose
65 %expect 0
66 %name-prefix="sdb_fe_yy"
68 %union {
69         char *str;
71         sdb_llist_t     *list;
72         sdb_conn_node_t *node;
74         sdb_store_matcher_t *m;
75 }
77 %start statements
79 %token SCANNER_ERROR
81 %token AND OR NOT WHERE
83 %token CMP_EQUAL CMP_NEQUAL CMP_REGEX CMP_NREGEX
85 %token <str> IDENTIFIER STRING
86 %token <node> FETCH LIST LOOKUP
88 /* Precedence (lowest first): */
89 %left OR
90 %left AND
91 %left NOT
92 %left CMP_EQUAL CMP_NEQUAL
93 %left CMP_REGEX CMP_NREGEX
94 %left '(' ')'
95 %left '.'
97 %type <list> statements
98 %type <node> statement
99         fetch_statement
100         list_statement
101         lookup_statement
102         expression
104 %type <m> matcher
105         compare_matcher
107 %%
109 statements:
110         statements ';' statement
111                 {
112                         /* only accept this in default parse mode */
113                         if (parser_mode != SDB_PARSE_DEFAULT) {
114                                 sdb_fe_yyerror(&yylloc, scanner,
115                                                 YY_("syntax error, unexpected statement, "
116                                                         "expecting expression"));
117                                 sdb_object_deref(SDB_OBJ($3));
118                                 YYABORT;
119                         }
121                         if ($3) {
122                                 sdb_llist_append(pt, SDB_OBJ($3));
123                                 sdb_object_deref(SDB_OBJ($3));
124                         }
125                 }
126         |
127         statement
128                 {
129                         /* only accept this in default parse mode */
130                         if (parser_mode != SDB_PARSE_DEFAULT) {
131                                 sdb_fe_yyerror(&yylloc, scanner,
132                                                 YY_("syntax error, unexpected statement, "
133                                                         "expecting expression"));
134                                 sdb_object_deref(SDB_OBJ($1));
135                                 YYABORT;
136                         }
138                         if ($1) {
139                                 sdb_llist_append(pt, SDB_OBJ($1));
140                                 sdb_object_deref(SDB_OBJ($1));
141                         }
142                 }
143         |
144         expression
145                 {
146                         /* only accept this in expression parse mode */
147                         if (! (parser_mode & SDB_PARSE_EXPR)) {
148                                 sdb_fe_yyerror(&yylloc, scanner,
149                                                 YY_("syntax error, unexpected expression, "
150                                                         "expecting statement"));
151                                 sdb_object_deref(SDB_OBJ($1));
152                                 YYABORT;
153                         }
155                         if ($1) {
156                                 sdb_llist_append(pt, SDB_OBJ($1));
157                                 sdb_object_deref(SDB_OBJ($1));
158                         }
159                 }
160         ;
162 statement:
163         fetch_statement
164         |
165         list_statement
166         |
167         lookup_statement
168         |
169         /* empty */
170                 {
171                         $$ = NULL;
172                 }
173         ;
175 /*
176  * FETCH <hostname>;
177  *
178  * Retrieve detailed information about a single host.
179  */
180 fetch_statement:
181         FETCH STRING
182                 {
183                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
184                                                 conn_fetch_t, conn_fetch_destroy));
185                         CONN_FETCH($$)->name = strdup($2);
186                         $$->cmd = CONNECTION_FETCH;
187                         free($2); $2 = NULL;
188                 }
189         ;
191 /*
192  * LIST;
193  *
194  * Returns a list of all hosts in the store.
195  */
196 list_statement:
197         LIST
198                 {
199                         $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
200                                                 sdb_conn_node_t));
201                         $$->cmd = CONNECTION_LIST;
202                 }
203         ;
205 /*
206  * LOOKUP <type> WHERE <expression>;
207  *
208  * Returns detailed information about <type> matching expression.
209  */
210 lookup_statement:
211         LOOKUP IDENTIFIER WHERE expression
212                 {
213                         /* TODO: support other types as well */
214                         if (strcasecmp($2, "hosts")) {
215                                 char errmsg[strlen($2) + 32];
216                                 snprintf(errmsg, sizeof(errmsg),
217                                                 YY_("unknown table %s"), $2);
218                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
219                                 free($2); $2 = NULL;
220                                 sdb_object_deref(SDB_OBJ($4));
221                                 YYABORT;
222                         }
224                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
225                                                 conn_lookup_t, conn_lookup_destroy));
226                         CONN_LOOKUP($$)->matcher = CONN_MATCHER($4);
227                         $$->cmd = CONNECTION_LOOKUP;
228                         free($2); $2 = NULL;
229                 }
230         ;
232 expression:
233         matcher
234                 {
235                         if (! $1) {
236                                 /* TODO: improve error reporting */
237                                 sdb_fe_yyerror(&yylloc, scanner,
238                                                 YY_("syntax error, invalid expression"));
239                                 YYABORT;
240                         }
242                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
243                                                 conn_node_matcher_t, conn_matcher_destroy));
244                         $$->cmd = CONNECTION_EXPR;
245                         CONN_MATCHER($$)->matcher = $1;
246                 }
247         ;
249 matcher:
250         '(' matcher ')'
251                 {
252                         $$ = $2;
253                 }
254         |
255         matcher AND matcher
256                 {
257                         $$ = sdb_store_con_matcher($1, $3);
258                         sdb_object_deref(SDB_OBJ($1));
259                         sdb_object_deref(SDB_OBJ($3));
260                 }
261         |
262         matcher OR matcher
263                 {
264                         $$ = sdb_store_dis_matcher($1, $3);
265                         sdb_object_deref(SDB_OBJ($1));
266                         sdb_object_deref(SDB_OBJ($3));
267                 }
268         |
269         NOT matcher
270                 {
271                         $$ = sdb_store_inv_matcher($2);
272                         sdb_object_deref(SDB_OBJ($2));
273                 }
274         |
275         compare_matcher
276                 {
277                         $$ = $1;
278                 }
279         ;
281 /*
282  * <object_type>.<object_attr> <op> <value>
283  *
284  * Parse matchers comparing object attributes with a value.
285  */
286 compare_matcher:
287         IDENTIFIER '.' IDENTIFIER CMP_EQUAL STRING
288                 {
289                         $$ = sdb_store_matcher_parse_cmp($1, $3, "=", $5);
290                         /* TODO: simplify memory management in the parser */
291                         free($1); $1 = NULL;
292                         free($3); $3 = NULL;
293                         free($5); $5 = NULL;
294                 }
295         |
296         IDENTIFIER '.' IDENTIFIER CMP_NEQUAL STRING
297                 {
298                         $$ = sdb_store_matcher_parse_cmp($1, $3, "!=", $5);
299                         /* TODO: simplify memory management in the parser */
300                         free($1); $1 = NULL;
301                         free($3); $3 = NULL;
302                         free($5); $5 = NULL;
303                 }
304         |
305         IDENTIFIER '.' IDENTIFIER CMP_REGEX STRING
306                 {
307                         $$ = sdb_store_matcher_parse_cmp($1, $3, "=~", $5);
308                         free($1); $1 = NULL;
309                         free($3); $3 = NULL;
310                         free($5); $5 = NULL;
311                 }
312         |
313         IDENTIFIER '.' IDENTIFIER CMP_NREGEX STRING
314                 {
315                         $$ = sdb_store_matcher_parse_cmp($1, $3, "!~", $5);
316                         free($1); $1 = NULL;
317                         free($3); $3 = NULL;
318                         free($5); $5 = NULL;
319                 }
320         ;
322 %%
324 /* XXX: on parse errors, allocated objects, strings, etc. need to be freed */
326 void
327 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
329         sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
330 } /* sdb_fe_yyerror */
332 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */