1 /*
2 * SysDB - src/frontend/parser.c
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 #include "sysdb.h"
30 #include "frontend/connection-private.h"
31 #include "frontend/parser.h"
32 #include "frontend/grammar.h"
34 #include "core/store.h"
36 #include "utils/llist.h"
38 #include <string.h>
40 /*
41 * private helper functions
42 */
44 static int
45 scanner_init(const char *input, int len,
46 sdb_fe_yyscan_t *scanner, sdb_fe_yyextra_t *extra)
47 {
48 if (! input)
49 return -1;
51 memset(extra, 0, sizeof(*extra));
52 extra->parsetree = sdb_llist_create();
53 extra->mode = SDB_PARSE_DEFAULT;
55 if (! extra->parsetree)
56 return -1;
58 *scanner = sdb_fe_scanner_init(input, len, extra);
59 if (! scanner) {
60 sdb_llist_destroy(extra->parsetree);
61 return -1;
62 }
63 return 0;
64 } /* scanner_init */
66 /*
67 * public API
68 */
70 sdb_llist_t *
71 sdb_fe_parse(const char *query, int len)
72 {
73 sdb_fe_yyscan_t scanner;
74 sdb_fe_yyextra_t yyextra;
75 int yyres;
77 if (scanner_init(query, len, &scanner, &yyextra))
78 return NULL;
80 yyres = sdb_fe_yyparse(scanner);
81 sdb_fe_scanner_destroy(scanner);
83 if (yyres) {
84 sdb_llist_destroy(yyextra.parsetree);
85 return NULL;
86 }
87 return yyextra.parsetree;
88 } /* sdb_fe_parse */
90 sdb_store_matcher_t *
91 sdb_fe_parse_matcher(const char *expr, int len)
92 {
93 sdb_fe_yyscan_t scanner;
94 sdb_fe_yyextra_t yyextra;
96 sdb_conn_node_t *node;
97 sdb_store_matcher_t *m;
99 int yyres;
101 if (scanner_init(expr, len, &scanner, &yyextra))
102 return NULL;
104 yyextra.mode = SDB_PARSE_EXPR;
106 yyres = sdb_fe_yyparse(scanner);
107 sdb_fe_scanner_destroy(scanner);
109 if (yyres) {
110 sdb_llist_destroy(yyextra.parsetree);
111 return NULL;
112 }
114 node = SDB_CONN_NODE(sdb_llist_get(yyextra.parsetree, 0));
115 if (! node)
116 return NULL;
118 if (node->cmd == CONNECTION_EXPR)
119 m = CONN_MATCHER(node)->matcher;
120 else
121 m = NULL;
123 CONN_MATCHER(node)->matcher = NULL;
124 sdb_llist_destroy(yyextra.parsetree);
125 sdb_object_deref(SDB_OBJ(node));
126 return m;
127 } /* sdb_fe_parse_matcher */
129 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */