1 /*
2 * SysDB - src/frontend/analyzer.c
3 * Copyright (C) 2014 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 "core/store-private.h"
31 #include "frontend/connection-private.h"
32 #include "frontend/parser.h"
33 #include "utils/error.h"
34 #include "utils/strbuf.h"
36 #include <assert.h>
38 /*
39 * private helper functions
40 */
42 static void
43 iter_error(sdb_strbuf_t *errbuf, int op, int oper, int context)
44 {
45 sdb_strbuf_sprintf(errbuf, "Cannot use %s %s in %s context",
46 MATCHER_SYM(op), SDB_STORE_TYPE_TO_NAME(oper),
47 SDB_STORE_TYPE_TO_NAME(context));
48 } /* iter_error */
50 static void
51 cmp_error(sdb_strbuf_t *errbuf, int op, int left, int right)
52 {
53 sdb_strbuf_sprintf(errbuf, "Invalid operator %s for types %s and %s",
54 MATCHER_SYM(op), SDB_TYPE_TO_STRING(left),
55 SDB_TYPE_TO_STRING(right));
56 } /* iter_error */
58 static int
59 analyze_matcher(int context, sdb_store_matcher_t *m, sdb_strbuf_t *errbuf)
60 {
61 if (! m)
62 return 0;
64 switch (m->type) {
65 case MATCHER_OR:
66 case MATCHER_AND:
67 assert(OP_M(m)->left && OP_M(m)->right);
68 if (analyze_matcher(context, OP_M(m)->left, errbuf))
69 return -1;
70 if (analyze_matcher(context, OP_M(m)->right, errbuf))
71 return -1;
72 break;
74 case MATCHER_NOT:
75 assert(UOP_M(m)->op);
76 if (analyze_matcher(context, UOP_M(m)->op, errbuf))
77 return -1;
78 break;
80 case MATCHER_ANY:
81 case MATCHER_ALL:
82 assert(ITER_M(m)->m);
83 if ((context != SDB_HOST)
84 && (context != SDB_SERVICE)
85 && (context != SDB_METRIC)) {
86 iter_error(errbuf, m->type, ITER_M(m)->type, context);
87 return -1;
88 }
89 if (ITER_M(m)->type == context) {
90 iter_error(errbuf, m->type, ITER_M(m)->type, context);
91 return -1;
92 }
93 if ((ITER_M(m)->type != SDB_SERVICE)
94 && (ITER_M(m)->type != SDB_METRIC)
95 && (ITER_M(m)->type != SDB_ATTRIBUTE)) {
96 iter_error(errbuf, m->type, ITER_M(m)->type, context);
97 return -1;
98 }
99 if ((context == SDB_SERVICE)
100 && (ITER_M(m)->type == SDB_METRIC)) {
101 iter_error(errbuf, m->type, ITER_M(m)->type, context);
102 return -1;
103 }
104 else if ((context == SDB_METRIC)
105 && (ITER_M(m)->type == SDB_SERVICE)) {
106 iter_error(errbuf, m->type, ITER_M(m)->type, context);
107 return -1;
108 }
109 if (analyze_matcher(ITER_M(m)->type, ITER_M(m)->m, errbuf))
110 return -1;
111 break;
113 case MATCHER_LT:
114 case MATCHER_LE:
115 case MATCHER_EQ:
116 case MATCHER_NE:
117 case MATCHER_GE:
118 case MATCHER_GT:
119 assert(CMP_M(m)->left && CMP_M(m)->right);
120 if ((CMP_M(m)->left->data_type > 0)
121 && (CMP_M(m)->left->data_type & SDB_TYPE_ARRAY)) {
122 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
123 CMP_M(m)->right->data_type);
124 return -1;
125 }
126 if ((CMP_M(m)->right->data_type > 0)
127 && (CMP_M(m)->right->data_type & SDB_TYPE_ARRAY)) {
128 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
129 CMP_M(m)->right->data_type);
130 return -1;
131 }
132 break;
134 case MATCHER_IN:
135 if ((CMP_M(m)->left->data_type > 0)
136 && (CMP_M(m)->left->data_type & SDB_TYPE_ARRAY)) {
137 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
138 CMP_M(m)->right->data_type);
139 return -1;
140 }
141 if ((CMP_M(m)->right->data_type > 0)
142 && (! (CMP_M(m)->right->data_type & SDB_TYPE_ARRAY))) {
143 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
144 CMP_M(m)->right->data_type);
145 return -1;
146 }
147 break;
149 case MATCHER_REGEX:
150 case MATCHER_NREGEX:
151 /* all types are supported for the left operand */
152 if ((CMP_M(m)->right->data_type > 0)
153 && (CMP_M(m)->right->data_type != SDB_TYPE_REGEX)
154 && (CMP_M(m)->right->data_type != SDB_TYPE_STRING)) {
155 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
156 CMP_M(m)->right->data_type);
157 return -1;
158 }
159 break;
161 case MATCHER_ISNULL:
162 case MATCHER_ISNNULL:
163 break;
165 default:
166 sdb_strbuf_sprintf(errbuf, "Unknown matcher type %d", m->type);
167 return -1;
168 }
169 return 0;
170 } /* analyze_matcher */
172 /*
173 * public API
174 */
176 int
177 sdb_fe_analyze(sdb_conn_node_t *node, sdb_strbuf_t *errbuf)
178 {
179 sdb_store_matcher_t *m = NULL, *filter = NULL;
180 int context = -1;
181 int status = 0;
183 if (! node)
184 return -1;
186 /* For now, this function checks basic matcher attributes only;
187 * later, this may be turned into one of multiple AST visitors. */
188 if (node->cmd == CONNECTION_FETCH) {
189 conn_fetch_t *fetch = CONN_FETCH(node);
190 if ((fetch->type == SDB_HOST) && fetch->name) {
191 sdb_strbuf_sprintf(errbuf, "Unexpected STRING '%s'", fetch->name);
192 return -1;
193 }
194 if ((fetch->type != SDB_HOST) && (! fetch->name)) {
195 sdb_strbuf_sprintf(errbuf, "Missing %s name",
196 SDB_STORE_TYPE_TO_NAME(fetch->type));
197 return -1;
198 }
199 if (fetch->filter)
200 filter = fetch->filter->matcher;
201 context = fetch->type;
202 }
203 else if (node->cmd == CONNECTION_LIST) {
204 if (CONN_LIST(node)->filter)
205 filter = CONN_LIST(node)->filter->matcher;
206 context = CONN_LIST(node)->type;
207 }
208 else if (node->cmd == CONNECTION_LOOKUP) {
209 if (CONN_LOOKUP(node)->matcher)
210 m = CONN_LOOKUP(node)->matcher->matcher;
211 if (CONN_LOOKUP(node)->filter)
212 filter = CONN_LOOKUP(node)->filter->matcher;
213 context = CONN_LOOKUP(node)->type;
214 }
215 else if (node->cmd == CONNECTION_TIMESERIES)
216 return 0;
217 else
218 return -1;
220 if (analyze_matcher(context, m, errbuf))
221 status = -1;
222 if (analyze_matcher(-1, filter, errbuf))
223 status = -1;
224 return status;
225 } /* sdb_fe_analyze */
227 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */