Code

frontend: Fixed memory holes in the parser.
[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 WHERE
83 %token CMP_EQUAL CMP_REGEX
85 %token <str> IDENTIFIER STRING
86 %token <node> FETCH LIST LOOKUP
88 /* Precedence (lowest first): */
89 %left OR
90 %left AND
91 %left CMP_EQUAL
92 %left CMP_REGEX
94 %type <list> statements
95 %type <node> statement
96         fetch_statement
97         list_statement
98         lookup_statement
99         expression
101 %type <m> matcher
102         compare_matcher
104 %%
106 statements:
107         statements ';' statement
108                 {
109                         /* only accept this in default parse mode */
110                         if (parser_mode != SDB_PARSE_DEFAULT) {
111                                 sdb_fe_yyerror(&yylloc, scanner,
112                                                 YY_("syntax error, unexpected statement, "
113                                                         "expecting expression"));
114                                 sdb_object_deref(SDB_OBJ($3));
115                                 YYABORT;
116                         }
118                         if ($3) {
119                                 sdb_llist_append(pt, SDB_OBJ($3));
120                                 sdb_object_deref(SDB_OBJ($3));
121                         }
122                 }
123         |
124         statement
125                 {
126                         /* only accept this in default parse mode */
127                         if (parser_mode != SDB_PARSE_DEFAULT) {
128                                 sdb_fe_yyerror(&yylloc, scanner,
129                                                 YY_("syntax error, unexpected statement, "
130                                                         "expecting expression"));
131                                 sdb_object_deref(SDB_OBJ($1));
132                                 YYABORT;
133                         }
135                         if ($1) {
136                                 sdb_llist_append(pt, SDB_OBJ($1));
137                                 sdb_object_deref(SDB_OBJ($1));
138                         }
139                 }
140         |
141         expression
142                 {
143                         /* only accept this in expression parse mode */
144                         if (! (parser_mode & SDB_PARSE_EXPR)) {
145                                 sdb_fe_yyerror(&yylloc, scanner,
146                                                 YY_("syntax error, unexpected expression, "
147                                                         "expecting statement"));
148                                 sdb_object_deref(SDB_OBJ($1));
149                                 YYABORT;
150                         }
152                         if ($1) {
153                                 sdb_llist_append(pt, SDB_OBJ($1));
154                                 sdb_object_deref(SDB_OBJ($1));
155                         }
156                 }
157         ;
159 statement:
160         fetch_statement
161         |
162         list_statement
163         |
164         lookup_statement
165         |
166         /* empty */
167                 {
168                         $$ = NULL;
169                 }
170         ;
172 /*
173  * FETCH <hostname>;
174  *
175  * Retrieve detailed information about a single host.
176  */
177 fetch_statement:
178         FETCH STRING
179                 {
180                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
181                                                 conn_fetch_t, conn_fetch_destroy));
182                         CONN_FETCH($$)->name = strdup($2);
183                         $$->cmd = CONNECTION_FETCH;
184                         free($2); $2 = NULL;
185                 }
186         ;
188 /*
189  * LIST;
190  *
191  * Returns a list of all hosts in the store.
192  */
193 list_statement:
194         LIST
195                 {
196                         $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
197                                                 sdb_conn_node_t));
198                         $$->cmd = CONNECTION_LIST;
199                 }
200         ;
202 /*
203  * LOOKUP <type> WHERE <expression>;
204  *
205  * Returns detailed information about <type> matching expression.
206  */
207 lookup_statement:
208         LOOKUP IDENTIFIER WHERE expression
209                 {
210                         /* TODO: support other types as well */
211                         if (strcasecmp($2, "hosts")) {
212                                 char errmsg[strlen($2) + 32];
213                                 snprintf(errmsg, sizeof(errmsg),
214                                                 YY_("unknown table %s"), $2);
215                                 sdb_fe_yyerror(&yylloc, scanner, errmsg);
216                                 free($2); $2 = NULL;
217                                 sdb_object_deref(SDB_OBJ($4));
218                                 YYABORT;
219                         }
221                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
222                                                 conn_lookup_t, conn_lookup_destroy));
223                         CONN_LOOKUP($$)->matcher = CONN_MATCHER($4);
224                         $$->cmd = CONNECTION_LOOKUP;
225                         free($2); $2 = NULL;
226                 }
227         ;
229 expression:
230         matcher
231                 {
232                         sdb_store_matcher_t *m = $1;
233                         if (! m) {
234                                 /* TODO: improve error reporting */
235                                 sdb_fe_yyerror(&yylloc, scanner,
236                                                 YY_("syntax error, invalid expression"));
237                                 YYABORT;
238                         }
240                         $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
241                                                 conn_node_matcher_t, conn_matcher_destroy));
242                         $$->cmd = CONNECTION_EXPR;
244                         if ((M(m)->type == MATCHER_HOST)
245                                         || (M(m)->type == MATCHER_AND)
246                                         || (M(m)->type == MATCHER_OR))
247                                 CONN_MATCHER($$)->matcher = m;
248                         else if (M(m)->type == MATCHER_SERVICE)
249                                 CONN_MATCHER($$)->matcher = sdb_store_host_matcher(NULL,
250                                                 /* name_re = */ NULL, /* service = */ m,
251                                                 /* attr = */ NULL);
252                         else if (M(m)->type == MATCHER_ATTR)
253                                 CONN_MATCHER($$)->matcher = sdb_store_host_matcher(NULL,
254                                                 /* name_re = */ NULL, /* service = */ NULL,
255                                                 /* attr = */ m);
256                         else {
257                                 char errbuf[1024];
258                                 snprintf(errbuf, sizeof(errbuf),
259                                                 YY_("syntax error, unexpected matcher type %d"),
260                                                 M(m)->type);
261                                 sdb_object_deref(SDB_OBJ($$));
262                                 sdb_fe_yyerror(&yylloc, scanner, errbuf);
263                                 YYABORT;
264                         }
265                 }
266         ;
268 matcher:
269         matcher AND matcher
270                 {
271                         $$ = sdb_store_con_matcher($1, $3);
272                 }
273         |
274         matcher OR matcher
275                 {
276                         $$ = sdb_store_dis_matcher($1, $3);
277                 }
278         |
279         compare_matcher
280                 {
281                         $$ = $1;
282                 }
283         ;
285 /*
286  * <object_type>.<object_attr> <op> <value>
287  *
288  * Parse matchers comparing object attributes with a value.
289  */
290 compare_matcher:
291         IDENTIFIER '.' IDENTIFIER CMP_EQUAL STRING
292                 {
293                         $$ = sdb_store_matcher_parse_cmp($1, $3, "=", $5);
294                         /* TODO: simplify memory management in the parser */
295                         free($1); $1 = NULL;
296                         free($3); $3 = NULL;
297                         free($5); $5 = NULL;
298                 }
299         |
300         IDENTIFIER '.' IDENTIFIER CMP_REGEX STRING
301                 {
302                         $$ = sdb_store_matcher_parse_cmp($1, $3, "=~", $5);
303                         free($1); $1 = NULL;
304                         free($3); $3 = NULL;
305                         free($5); $5 = NULL;
306                 }
307         ;
309 %%
311 void
312 sdb_fe_yyerror(YYLTYPE *lval, sdb_fe_yyscan_t scanner, const char *msg)
314         sdb_log(SDB_LOG_ERR, "frontend: parse error: %s", msg);
315 } /* sdb_fe_yyerror */
317 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */