diff --git a/src/frontend/grammar.y b/src/frontend/grammar.y
index f262f3e11d9ace504a2fa08dc4b3ef8bb7c9befe..6c52b74907b17163d1c15c7890f9f85d0ed959bf 100644 (file)
--- a/src/frontend/grammar.y
+++ b/src/frontend/grammar.y
#include "frontend/grammar.h"
#include "core/store.h"
+#include "core/store-private.h"
#include "utils/error.h"
#include "utils/llist.h"
#include <stdio.h>
+#include <string.h>
int
sdb_fe_yylex(YYSTYPE *yylval, YYLTYPE *yylloc, sdb_fe_yyscan_t yyscanner);
sdb_llist_t *list;
sdb_conn_node_t *node;
+
+ sdb_store_matcher_t *m;
}
%start statements
%token SCANNER_ERROR
-%token <str> IDENTIFIER
-%token <node> LIST
+%token WHERE
+
+%token CMP_EQUAL CMP_REGEX
+
+%token <str> IDENTIFIER STRING
+%token <node> FETCH LIST LOOKUP
%type <list> statements
%type <node> statement
+ fetch_statement
list_statement
+ lookup_statement
expression
+%type <m> compare_matcher
+
%%
statements:
statements ';' statement
{
+ /* only accept this in default parse mode */
+ if (parser_mode != SDB_PARSE_DEFAULT) {
+ sdb_fe_yyerror(&yylloc, scanner,
+ YY_("syntax error, unexpected statement, "
+ "expecting expression"));
+ YYABORT;
+ }
+
if ($3) {
sdb_llist_append(pt, SDB_OBJ($3));
sdb_object_deref(SDB_OBJ($3));
|
statement
{
+ /* only accept this in default parse mode */
+ if (parser_mode != SDB_PARSE_DEFAULT) {
+ sdb_fe_yyerror(&yylloc, scanner,
+ YY_("syntax error, unexpected statement, "
+ "expecting expression"));
+ YYABORT;
+ }
+
if ($1) {
sdb_llist_append(pt, SDB_OBJ($1));
sdb_object_deref(SDB_OBJ($1));
;
statement:
+ fetch_statement
+ |
list_statement
|
+ lookup_statement
+ |
/* empty */
{
$$ = NULL;
}
;
+/*
+ * FETCH <hostname>;
+ *
+ * Retrieve detailed information about a single host.
+ */
+fetch_statement:
+ FETCH STRING
+ {
+ $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+ conn_fetch_t, conn_fetch_destroy));
+ CONN_FETCH($$)->name = strdup($2);
+ $$->cmd = CONNECTION_FETCH;
+ free($2); $2 = NULL;
+ }
+ ;
+
+/*
+ * LIST;
+ *
+ * Returns a list of all hosts in the store.
+ */
list_statement:
LIST
{
}
;
+/*
+ * LOOKUP <type> WHERE <expression>;
+ *
+ * Returns detailed information about <type> matching expression.
+ */
+lookup_statement:
+ LOOKUP IDENTIFIER WHERE expression
+ {
+ /* TODO: support other types as well */
+ if (strcasecmp($2, "hosts")) {
+ char errmsg[strlen($2) + 32];
+ snprintf(errmsg, sizeof(errmsg),
+ YY_("unknown table %s"), $2);
+ sdb_fe_yyerror(&yylloc, scanner, errmsg);
+ YYABORT;
+ }
+
+ $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+ conn_lookup_t, conn_lookup_destroy));
+ CONN_LOOKUP($$)->matcher = CONN_MATCHER($4);
+ $$->cmd = CONNECTION_LOOKUP;
+ free($2); $2 = NULL;
+ }
+ ;
+
expression:
- IDENTIFIER
+ compare_matcher
{
+ sdb_store_matcher_t *m = $1;
+ if (! m) {
+ /* TODO: improve error reporting */
+ sdb_fe_yyerror(&yylloc, scanner,
+ YY_("syntax error, invalid expression"));
+ YYABORT;
+ }
+
$$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
conn_node_matcher_t));
$$->cmd = CONNECTION_EXPR;
- /* XXX: this is just a placeholder for now */
- CONN_MATCHER($$)->matcher = sdb_store_host_matcher($1,
- /* name_re = */ NULL, /* service = */ NULL,
- /* attr = */ NULL);
- free($1);
- $1 = NULL;
+
+ if (M(m)->type == MATCHER_HOST)
+ CONN_MATCHER($$)->matcher = m;
+ else if (M(m)->type == MATCHER_SERVICE)
+ CONN_MATCHER($$)->matcher = sdb_store_host_matcher(NULL,
+ /* name_re = */ NULL, /* service = */ m,
+ /* attr = */ NULL);
+ else if (M(m)->type == MATCHER_ATTR)
+ CONN_MATCHER($$)->matcher = sdb_store_host_matcher(NULL,
+ /* name_re = */ NULL, /* service = */ NULL,
+ /* attr = */ m);
+ }
+ ;
+
+/*
+ * <object_type>.<object_attr> <op> <value>
+ *
+ * Parse matchers comparing object attributes with a value.
+ */
+compare_matcher:
+ IDENTIFIER '.' IDENTIFIER CMP_EQUAL STRING
+ {
+ $$ = sdb_store_matcher_parse_cmp($1, $3, "=", $5);
+ /* TODO: simplify memory management in the parser */
+ free($1); $1 = NULL;
+ free($3); $3 = NULL;
+ free($5); $5 = NULL;
+ }
+ |
+ IDENTIFIER '.' IDENTIFIER CMP_REGEX STRING
+ {
+ $$ = sdb_store_matcher_parse_cmp($1, $3, "=~", $5);
+ free($1); $1 = NULL;
+ free($3); $3 = NULL;
+ free($5); $5 = NULL;
}
;