1
0
mirror of https://github.com/libretro/RetroArch synced 2024-07-05 09:48:42 +00:00
RetroArch/libretro-db/query.c

958 lines
20 KiB
C
Raw Normal View History

2015-01-20 02:34:00 +00:00
#ifdef _WIN32
#include <direct.h>
#else
2015-01-19 21:47:09 +00:00
#include <unistd.h>
2015-01-20 02:34:00 +00:00
#endif
2015-01-19 21:47:09 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <compat/fnmatch.h>
2015-06-22 09:53:02 +00:00
#include <compat/strl.h>
2015-01-19 21:47:09 +00:00
2015-09-06 12:55:42 +00:00
#include "libretrodb.h"
#include "query.h"
#include "rmsgpack_dom.h"
2015-01-19 21:47:09 +00:00
#define MAX_ERROR_LEN 256
#undef MAX_ARGS
2015-01-19 21:47:09 +00:00
#define MAX_ARGS 50
2015-06-26 16:35:35 +00:00
static char tmp_error_buff [MAX_ERROR_LEN] = {0};
2015-01-19 21:47:09 +00:00
2015-01-24 03:04:56 +00:00
struct buffer
{
2015-04-04 19:10:36 +00:00
const char *data;
size_t len;
off_t offset;
2015-01-19 21:47:09 +00:00
};
/* Errors */
2015-01-27 03:02:10 +00:00
static void raise_too_many_arguments(const char **error)
2015-01-24 03:04:56 +00:00
{
2015-06-22 09:53:02 +00:00
strlcpy(tmp_error_buff,
"Too many arguments in function call.", sizeof(tmp_error_buff));
2015-04-04 19:10:36 +00:00
*error = tmp_error_buff;
2015-01-19 21:47:09 +00:00
}
2015-01-27 03:02:10 +00:00
static void raise_expected_number(off_t where, const char **error)
2015-01-24 03:04:56 +00:00
{
snprintf(tmp_error_buff, MAX_ERROR_LEN,
2015-01-20 00:25:34 +00:00
#ifdef _WIN32
2015-01-24 03:04:56 +00:00
"%I64u::Expected number",
2015-01-20 00:25:34 +00:00
#else
2015-01-24 03:04:56 +00:00
"%llu::Expected number",
2015-01-20 00:25:34 +00:00
#endif
2015-01-24 03:04:56 +00:00
(unsigned long long)where);
*error = tmp_error_buff;
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
static void raise_expected_string(off_t where, const char ** error)
{
snprintf(tmp_error_buff, MAX_ERROR_LEN,
2015-01-20 00:25:34 +00:00
#ifdef _WIN32
2015-01-24 03:04:56 +00:00
"%I64u::Expected string",
2015-01-20 00:25:34 +00:00
#else
2015-01-24 03:04:56 +00:00
"%llu::Expected string",
2015-01-20 00:25:34 +00:00
#endif
2015-01-24 03:04:56 +00:00
(unsigned long long)where);
*error = tmp_error_buff;
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
static void raise_unexpected_eof(off_t where, const char ** error)
{
snprintf(tmp_error_buff, MAX_ERROR_LEN,
2015-01-20 00:25:34 +00:00
#ifdef _WIN32
2015-01-24 03:04:56 +00:00
"%I64u::Unexpected EOF",
2015-01-20 00:25:34 +00:00
#else
2015-01-24 03:04:56 +00:00
"%llu::Unexpected EOF",
2015-01-20 00:25:34 +00:00
#endif
2015-01-24 03:04:56 +00:00
(unsigned long long)where
);
*error = tmp_error_buff;
2015-01-19 21:47:09 +00:00
}
2015-02-01 14:34:55 +00:00
static void raise_enomem(const char **error)
2015-01-24 03:04:56 +00:00
{
2015-06-22 09:53:02 +00:00
strlcpy(tmp_error_buff, "Out of memory", sizeof(tmp_error_buff));
2015-01-24 03:04:56 +00:00
*error = tmp_error_buff;
2015-01-19 21:47:09 +00:00
}
2015-02-01 14:34:55 +00:00
static void raise_unknown_function(off_t where, const char *name,
2015-06-25 15:29:44 +00:00
ssize_t len, const char **error)
2015-01-24 03:04:56 +00:00
{
int n = snprintf(tmp_error_buff, MAX_ERROR_LEN,
2015-01-20 00:25:34 +00:00
#ifdef _WIN32
2015-01-24 03:04:56 +00:00
"%I64u::Unknown function '",
2015-01-20 00:25:34 +00:00
#else
2015-01-24 03:04:56 +00:00
"%llu::Unknown function '",
2015-01-20 00:25:34 +00:00
#endif
2015-01-24 03:04:56 +00:00
(unsigned long long)where
);
if (len < (MAX_ERROR_LEN - n - 3))
strncpy(tmp_error_buff + n, name, len);
strcpy(tmp_error_buff + n + len, "'");
*error = tmp_error_buff;
2015-01-19 21:47:09 +00:00
}
2015-02-01 14:34:55 +00:00
static void raise_expected_eof(off_t where, char found, const char **error)
2015-01-24 03:04:56 +00:00
{
snprintf(tmp_error_buff, MAX_ERROR_LEN,
2015-01-20 00:25:34 +00:00
#ifdef _WIN32
2015-01-24 03:04:56 +00:00
"%I64u::Expected EOF found '%c'",
2015-01-20 00:25:34 +00:00
#else
2015-01-24 03:04:56 +00:00
"%llu::Expected EOF found '%c'",
2015-01-20 00:25:34 +00:00
#endif
2015-01-24 03:04:56 +00:00
(unsigned long long)where,
found
);
*error = tmp_error_buff;
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
static void raise_unexpected_char(off_t where, char expected, char found,
2015-02-01 14:34:55 +00:00
const char **error)
2015-01-24 03:04:56 +00:00
{
snprintf(tmp_error_buff, MAX_ERROR_LEN,
2015-01-20 00:25:34 +00:00
#ifdef _WIN32
2015-01-24 03:04:56 +00:00
"%I64u::Expected '%c' found '%c'",
2015-01-20 00:25:34 +00:00
#else
2015-01-24 03:04:56 +00:00
"%llu::Expected '%c' found '%c'",
2015-01-20 00:25:34 +00:00
#endif
2015-01-24 03:04:56 +00:00
(unsigned long long)where, expected, found);
*error = tmp_error_buff;
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
enum argument_type
{
AT_FUNCTION,
AT_VALUE
2015-01-19 21:47:09 +00:00
};
struct argument;
2015-02-01 14:34:55 +00:00
typedef struct rmsgpack_dom_value (*rarch_query_func)(
2015-06-03 14:57:51 +00:00
struct rmsgpack_dom_value input,
unsigned argc,
const struct argument *argv
);
2015-01-19 21:47:09 +00:00
2015-01-24 03:04:56 +00:00
struct invocation
{
2015-06-03 14:57:51 +00:00
rarch_query_func func;
unsigned argc;
struct argument *argv;
2015-01-19 21:47:09 +00:00
};
2015-01-24 03:04:56 +00:00
struct argument
{
2015-06-03 14:57:51 +00:00
enum argument_type type;
union
2015-01-24 03:04:56 +00:00
{
2015-06-03 14:57:51 +00:00
struct rmsgpack_dom_value value;
struct invocation invocation;
2015-06-29 19:39:00 +00:00
} a;
2015-01-19 21:47:09 +00:00
};
2015-02-01 14:34:55 +00:00
static void argument_free(struct argument *arg)
2015-01-24 03:04:56 +00:00
{
unsigned i;
2015-01-19 21:47:09 +00:00
2015-01-24 03:04:56 +00:00
if (arg->type != AT_FUNCTION)
{
2015-06-29 19:39:00 +00:00
rmsgpack_dom_value_free(&arg->a.value);
2015-01-24 03:04:56 +00:00
return;
}
2015-06-29 19:39:00 +00:00
for (i = 0; i < arg->a.invocation.argc; i++)
argument_free(&arg->a.invocation.argv[i]);
2015-01-24 03:04:56 +00:00
}
struct query
{
2015-06-03 14:57:51 +00:00
unsigned ref_count;
struct invocation root;
2015-01-19 21:47:09 +00:00
};
2015-01-24 03:04:56 +00:00
struct registered_func
{
2015-06-03 14:57:51 +00:00
const char *name;
rarch_query_func func;
2015-01-19 21:47:09 +00:00
};
2015-02-01 14:34:55 +00:00
static struct buffer parse_argument(struct buffer buff, struct argument *arg,
const char **error);
2015-01-19 21:47:09 +00:00
2015-01-24 03:04:56 +00:00
static struct rmsgpack_dom_value is_true(struct rmsgpack_dom_value input,
2015-06-03 14:57:51 +00:00
unsigned argc, const struct argument *argv)
2015-01-24 03:04:56 +00:00
{
2015-06-07 22:24:16 +00:00
struct rmsgpack_dom_value res;
memset(&res, 0, sizeof(res));
2015-01-19 21:47:09 +00:00
2015-06-03 14:26:54 +00:00
res.type = RDT_BOOL;
2015-06-26 18:33:40 +00:00
res.val.bool_ = 0;
2015-01-24 03:04:56 +00:00
if (argc > 0 || input.type != RDT_BOOL)
2015-06-26 18:33:40 +00:00
res.val.bool_ = 0;
2015-01-24 03:04:56 +00:00
else
2015-06-26 18:33:40 +00:00
res.val.bool_ = input.val.bool_;
2015-01-24 03:04:56 +00:00
return res;
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
static struct rmsgpack_dom_value equals(struct rmsgpack_dom_value input,
2015-06-03 14:57:51 +00:00
unsigned argc, const struct argument * argv)
2015-01-24 03:04:56 +00:00
{
struct argument arg;
2015-06-07 22:24:16 +00:00
struct rmsgpack_dom_value res;
memset(&res, 0, sizeof(res));
2015-01-19 21:47:09 +00:00
2015-01-24 03:04:56 +00:00
res.type = RDT_BOOL;
if (argc != 1)
2015-06-26 18:33:40 +00:00
res.val.bool_ = 0;
2015-01-24 03:04:56 +00:00
else
{
arg = argv[0];
if (arg.type != AT_VALUE)
2015-06-26 18:33:40 +00:00
res.val.bool_ = 0;
2015-01-24 03:04:56 +00:00
else
{
2015-06-29 19:39:00 +00:00
if (input.type == RDT_UINT && arg.a.value.type == RDT_INT)
2015-01-24 03:04:56 +00:00
{
2015-06-29 19:39:00 +00:00
arg.a.value.type = RDT_UINT;
arg.a.value.val.uint_ = arg.a.value.val.int_;
2015-01-24 03:04:56 +00:00
}
2015-06-29 19:39:00 +00:00
res.val.bool_ = (rmsgpack_dom_value_cmp(&input, &arg.a.value) == 0);
2015-01-24 03:04:56 +00:00
}
}
return res;
}
static struct rmsgpack_dom_value operator_or(struct rmsgpack_dom_value input,
2015-06-03 14:57:51 +00:00
unsigned argc, const struct argument * argv)
2015-01-24 03:04:56 +00:00
{
unsigned i;
2015-06-07 22:24:16 +00:00
struct rmsgpack_dom_value res;
memset(&res, 0, sizeof(res));
2015-01-24 03:04:56 +00:00
res.type = RDT_BOOL;
2015-06-26 18:33:40 +00:00
res.val.bool_ = 0;
2015-01-24 03:04:56 +00:00
for (i = 0; i < argc; i++)
{
if (argv[i].type == AT_VALUE)
res = equals(input, 1, &argv[i]);
else
{
2015-06-29 19:39:00 +00:00
res = is_true(argv[i].a.invocation.func(input,
argv[i].a.invocation.argc,
argv[i].a.invocation.argv
2015-01-24 03:04:56 +00:00
), 0, NULL);
}
2015-06-26 18:33:40 +00:00
if (res.val.bool_)
2015-01-24 03:04:56 +00:00
return res;
}
return res;
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
static struct rmsgpack_dom_value between(struct rmsgpack_dom_value input,
unsigned argc, const struct argument * argv)
{
2015-06-07 22:24:16 +00:00
struct rmsgpack_dom_value res;
2015-06-03 14:26:54 +00:00
unsigned i = 0;
2015-06-07 22:24:16 +00:00
memset(&res, 0, sizeof(res));
2015-02-01 14:34:55 +00:00
2015-01-24 03:04:56 +00:00
res.type = RDT_BOOL;
2015-06-26 18:33:40 +00:00
res.val.bool_ = 0;
(void)i;
2015-01-24 03:04:56 +00:00
if (argc != 2)
return res;
if (argv[0].type != AT_VALUE || argv[1].type != AT_VALUE)
return res;
2015-06-29 19:39:00 +00:00
if (argv[0].a.value.type != RDT_INT || argv[1].a.value.type != RDT_INT)
2015-01-24 03:04:56 +00:00
return res;
2015-01-19 21:47:09 +00:00
2015-01-24 03:04:56 +00:00
switch (input.type)
{
case RDT_INT:
2015-06-29 19:39:00 +00:00
res.val.bool_ = ((input.val.int_ >= argv[0].a.value.val.int_) && (input.val.int_ <= argv[1].a.value.val.int_));
2015-01-24 03:04:56 +00:00
break;
case RDT_UINT:
2015-06-29 19:39:00 +00:00
res.val.bool_ = (((unsigned)input.val.int_ >= argv[0].a.value.val.uint_) && (input.val.int_ <= argv[1].a.value.val.int_));
2015-01-24 03:04:56 +00:00
break;
default:
return res;
}
2015-01-19 21:47:09 +00:00
2015-01-24 03:04:56 +00:00
return res;
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
static struct rmsgpack_dom_value operator_and(struct rmsgpack_dom_value input,
unsigned argc, const struct argument * argv)
{
unsigned i;
2015-06-07 22:24:16 +00:00
struct rmsgpack_dom_value res;
memset(&res, 0, sizeof(res));
2015-01-24 03:04:56 +00:00
res.type = RDT_BOOL;
2015-06-26 18:33:40 +00:00
res.val.bool_ = 0;
2015-01-24 03:04:56 +00:00
for (i = 0; i < argc; i++)
{
if (argv[i].type == AT_VALUE)
res = equals(input, 1, &argv[i]);
else
{
res = is_true(
2015-06-29 19:39:00 +00:00
argv[i].a.invocation.func(input,
argv[i].a.invocation.argc,
argv[i].a.invocation.argv
2015-01-24 03:04:56 +00:00
),
0, NULL);
}
2015-06-26 18:33:40 +00:00
if (!res.val.bool_)
2015-01-24 03:04:56 +00:00
return res;
}
return res;
}
static struct rmsgpack_dom_value q_glob(struct rmsgpack_dom_value input,
unsigned argc, const struct argument * argv)
{
2015-06-07 22:24:16 +00:00
struct rmsgpack_dom_value res;
2015-01-24 03:04:56 +00:00
unsigned i = 0;
2015-06-07 22:24:16 +00:00
memset(&res, 0, sizeof(res));
2015-01-24 03:04:56 +00:00
res.type = RDT_BOOL;
2015-06-26 18:33:40 +00:00
res.val.bool_ = 0;
(void)i;
2015-01-24 03:04:56 +00:00
if (argc != 1)
return res;
2015-06-29 19:39:00 +00:00
if (argv[0].type != AT_VALUE || argv[0].a.value.type != RDT_STRING)
2015-01-24 03:04:56 +00:00
return res;
if (input.type != RDT_STRING)
return res;
2015-06-26 18:33:40 +00:00
res.val.bool_ = rl_fnmatch(
2015-06-29 19:39:00 +00:00
argv[0].a.value.val.string.buff,
2015-06-26 18:33:40 +00:00
input.val.string.buff,
2015-01-24 03:04:56 +00:00
0
) == 0;
return res;
}
2015-02-01 14:34:55 +00:00
static struct rmsgpack_dom_value all_map(struct rmsgpack_dom_value input,
unsigned argc, const struct argument *argv)
{
2015-04-04 19:10:36 +00:00
unsigned i;
2015-02-01 14:34:55 +00:00
struct argument arg;
2015-06-07 22:24:16 +00:00
struct rmsgpack_dom_value res;
2015-02-01 14:34:55 +00:00
struct rmsgpack_dom_value nil_value;
2015-04-04 19:10:36 +00:00
struct rmsgpack_dom_value *value = NULL;
2015-06-07 22:24:16 +00:00
memset(&res, 0, sizeof(res));
2015-01-19 21:47:09 +00:00
2015-02-01 14:34:55 +00:00
nil_value.type = RDT_NULL;
res.type = RDT_BOOL;
2015-06-26 18:33:40 +00:00
res.val.bool_ = 1;
2015-02-01 14:34:55 +00:00
if (argc % 2 != 0)
{
2015-06-26 18:33:40 +00:00
res.val.bool_ = 0;
2015-02-01 14:34:55 +00:00
return res;
}
if (input.type != RDT_MAP)
return res;
2015-01-19 21:47:09 +00:00
2015-02-01 14:34:55 +00:00
for (i = 0; i < argc; i += 2)
{
2015-02-01 14:34:55 +00:00
arg = argv[i];
if (arg.type != AT_VALUE)
{
2015-06-26 18:33:40 +00:00
res.val.bool_ = 0;
2015-02-01 14:34:55 +00:00
goto clean;
}
2015-06-29 19:39:00 +00:00
value = rmsgpack_dom_value_map_value(&input, &arg.a.value);
2015-02-01 14:34:55 +00:00
if (!value) /* All missing fields are nil */
value = &nil_value;
arg = argv[i + 1];
if (arg.type == AT_VALUE)
res = equals(*value, 1, &arg);
else
{
2015-06-29 19:39:00 +00:00
res = is_true(arg.a.invocation.func(
2015-02-01 14:34:55 +00:00
*value,
2015-06-29 19:39:00 +00:00
arg.a.invocation.argc,
arg.a.invocation.argv
2015-02-01 14:34:55 +00:00
), 0, NULL);
value = NULL;
}
2015-06-26 18:33:40 +00:00
if (!res.val.bool_)
2015-02-01 14:34:55 +00:00
break;
}
2015-01-19 21:47:09 +00:00
clean:
2015-02-01 14:34:55 +00:00
return res;
2015-01-19 21:47:09 +00:00
}
struct registered_func registered_functions[100] = {
2015-06-03 14:57:51 +00:00
{"is_true", is_true},
{"or", operator_or},
{"and", operator_and},
{"between", between},
{"glob", q_glob},
{NULL, NULL}
2015-01-19 21:47:09 +00:00
};
2015-01-27 03:02:10 +00:00
static struct buffer chomp(struct buffer buff)
{
for (; (unsigned)buff.offset < buff.len && isspace((int)buff.data[buff.offset]); buff.offset++);
2015-01-27 03:02:10 +00:00
return buff;
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
static struct buffer expect_char(struct buffer buff,
char c, const char ** error)
{
if ((unsigned)buff.offset >= buff.len)
2015-01-24 03:04:56 +00:00
raise_unexpected_eof(buff.offset, error);
else if (buff.data[buff.offset] != c)
raise_unexpected_char(
buff.offset, c, buff.data[buff.offset], error);
else
buff.offset++;
return buff;
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
static struct buffer expect_eof(struct buffer buff, const char ** error)
{
buff = chomp(buff);
if ((unsigned)buff.offset < buff.len)
2015-01-24 03:04:56 +00:00
raise_expected_eof(buff.offset, buff.data[buff.offset], error);
return buff;
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
static int peek(struct buffer buff, const char * data)
{
size_t remain = buff.len - buff.offset;
if (remain < strlen(data))
return 0;
return (strncmp(buff.data + buff.offset,
data, strlen(data)) == 0);
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
static int is_eot(struct buffer buff)
{
return ((unsigned)buff.offset >= buff.len);
2015-01-19 21:47:09 +00:00
}
2015-02-01 14:34:55 +00:00
static void peek_char(struct buffer buff, char *c, const char **error)
2015-01-24 03:04:56 +00:00
{
if (is_eot(buff))
{
2015-01-24 03:04:56 +00:00
raise_unexpected_eof(buff.offset, error);
return;
}
*c = buff.data[buff.offset];
2015-01-19 21:47:09 +00:00
}
2015-01-27 03:02:10 +00:00
static struct buffer get_char(struct buffer buff, char * c,
const char ** error)
{
2015-06-03 14:57:51 +00:00
if (is_eot(buff))
2015-01-27 03:02:10 +00:00
{
2015-06-03 14:57:51 +00:00
raise_unexpected_eof(buff.offset, error);
return buff;
}
*c = buff.data[buff.offset];
buff.offset++;
return buff;
2015-01-19 21:47:09 +00:00
}
2015-02-01 14:34:55 +00:00
static struct buffer parse_string(struct buffer buff,
struct rmsgpack_dom_value *value, const char **error)
2015-01-27 03:02:10 +00:00
{
2015-06-12 22:33:31 +00:00
const char * str_start = NULL;
char terminator = '\0';
char c = '\0';
int is_binstr = 0;
(void)c;
2015-01-27 03:02:10 +00:00
buff = get_char(buff, &terminator, error);
2015-01-24 03:04:56 +00:00
2015-01-27 03:02:10 +00:00
if (*error)
return buff;
2015-01-24 03:04:56 +00:00
if (terminator == 'b')
{
is_binstr = 1;
buff = get_char(buff, &terminator, error);
}
2015-01-27 03:02:10 +00:00
if (terminator != '"' && terminator != '\'')
2015-01-24 03:04:56 +00:00
{
buff.offset--;
raise_expected_string(buff.offset, error);
}
2015-01-27 03:02:10 +00:00
str_start = buff.data + buff.offset;
buff = get_char(buff, &c, error);
2015-01-24 03:04:56 +00:00
2015-01-27 03:02:10 +00:00
while (!*error)
2015-01-24 03:04:56 +00:00
{
2015-01-27 03:02:10 +00:00
if (c == terminator)
break;
buff = get_char(buff, &c, error);
}
2015-01-24 03:04:56 +00:00
2015-01-27 03:02:10 +00:00
if (!*error)
2015-01-24 03:04:56 +00:00
{
2015-06-10 19:16:00 +00:00
size_t count;
value->type = is_binstr ? RDT_BINARY : RDT_STRING;
2015-06-26 18:33:40 +00:00
value->val.string.len = (buff.data + buff.offset) - str_start - 1;
2015-06-10 19:16:00 +00:00
2015-06-26 18:33:40 +00:00
count = is_binstr ? (value->val.string.len + 1) / 2 : (value->val.string.len + 1);
value->val.string.buff = (char*)calloc(count, sizeof(char));
2015-01-24 03:04:56 +00:00
2015-06-26 18:33:40 +00:00
if (!value->val.string.buff)
2015-01-24 03:04:56 +00:00
raise_enomem(error);
else if (is_binstr)
{
2015-06-26 18:33:40 +00:00
unsigned i;
const char *tok = str_start;
2015-06-26 18:33:40 +00:00
unsigned j = 0;
2015-06-26 18:33:40 +00:00
for (i = 0; i < value->val.string.len; i += 2)
{
uint8_t hi, lo;
char hic = tok[i];
char loc = tok[i + 1];
if (hic <= '9')
hi = hic - '0';
else
hi = (hic - 'A') + 10;
if (loc <= '9')
lo = loc - '0';
else
lo = (loc - 'A') + 10;
2015-06-26 18:33:40 +00:00
value->val.string.buff[j++] = hi * 16 + lo;
}
2015-06-26 18:33:40 +00:00
value->val.string.len = j;
}
2015-01-24 03:04:56 +00:00
else
2015-06-26 18:33:40 +00:00
memcpy(value->val.string.buff, str_start, value->val.string.len);
2015-01-24 03:04:56 +00:00
}
2015-01-27 03:02:10 +00:00
return buff;
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
static struct buffer parse_integer(struct buffer buff,
2015-02-01 14:34:55 +00:00
struct rmsgpack_dom_value *value, const char **error)
2015-01-24 03:04:56 +00:00
{
value->type = RDT_INT;
if (sscanf(buff.data + buff.offset,
2015-01-20 00:25:34 +00:00
#ifdef _WIN32
2015-01-24 03:04:56 +00:00
"%I64d",
2015-01-20 00:25:34 +00:00
#else
2015-01-24 03:04:56 +00:00
"%lld",
2015-01-20 00:25:34 +00:00
#endif
2015-06-26 18:33:40 +00:00
(signed long long*)&value->val.int_) == 0)
2015-01-24 03:04:56 +00:00
raise_expected_number(buff.offset, error);
else
{
while (isdigit((int)buff.data[buff.offset]))
2015-01-24 03:04:56 +00:00
buff.offset++;
}
return buff;
2015-01-19 21:47:09 +00:00
}
2015-01-27 03:02:10 +00:00
static struct buffer parse_value(struct buffer buff,
2015-02-01 14:34:55 +00:00
struct rmsgpack_dom_value *value, const char **error)
2015-01-27 03:02:10 +00:00
{
buff = chomp(buff);
2015-02-01 14:34:55 +00:00
2015-01-27 03:02:10 +00:00
if (peek(buff, "nil"))
{
buff.offset += strlen("nil");
value->type = RDT_NULL;
}
else if (peek(buff, "true"))
{
buff.offset += strlen("true");
value->type = RDT_BOOL;
2015-06-26 18:33:40 +00:00
value->val.bool_ = 1;
2015-01-27 03:02:10 +00:00
}
else if (peek(buff, "false"))
{
buff.offset += strlen("false");
value->type = RDT_BOOL;
2015-06-26 18:33:40 +00:00
value->val.bool_ = 0;
2015-01-27 03:02:10 +00:00
}
else if (peek(buff, "b") || peek(buff, "\"") || peek(buff, "'"))
2015-01-27 03:02:10 +00:00
buff = parse_string(buff, value, error);
else if (isdigit((int)buff.data[buff.offset]))
2015-01-27 03:02:10 +00:00
buff = parse_integer(buff, value, error);
return buff;
2015-01-19 21:47:09 +00:00
}
2015-02-01 14:34:55 +00:00
static struct buffer get_ident(struct buffer buff,
const char **ident,
size_t *len, const char **error)
2015-01-24 03:04:56 +00:00
{
2015-02-01 14:34:55 +00:00
char c = '\0';
2015-01-24 03:04:56 +00:00
2015-06-03 14:57:51 +00:00
if (is_eot(buff))
2015-01-24 03:04:56 +00:00
{
raise_unexpected_eof(buff.offset, error);
return buff;
}
2015-01-19 21:47:09 +00:00
2015-06-03 14:57:51 +00:00
*ident = buff.data + buff.offset;
*len = 0;
peek_char(buff, &c, error);
2015-01-24 03:04:56 +00:00
2015-06-03 14:57:51 +00:00
if (*error)
goto clean;
if (!isalpha((int)c))
2015-06-03 14:57:51 +00:00
return buff;
2015-01-24 03:04:56 +00:00
2015-06-03 14:57:51 +00:00
buff.offset++;
*len = *len + 1;
peek_char(buff, &c, error);
2015-01-24 03:04:56 +00:00
2015-06-03 14:57:51 +00:00
while (!*error)
2015-01-24 03:04:56 +00:00
{
if (!(isalpha((int)c) || isdigit((int)c) || c == '_'))
2015-01-24 03:04:56 +00:00
break;
buff.offset++;
*len = *len + 1;
peek_char(buff, &c, error);
}
2015-02-01 14:34:55 +00:00
2015-01-19 21:47:09 +00:00
clean:
2015-06-03 14:57:51 +00:00
return buff;
2015-01-19 21:47:09 +00:00
}
2015-02-01 14:34:55 +00:00
static struct buffer parse_method_call(struct buffer buff,
struct invocation *invocation, const char **error)
2015-01-27 03:02:10 +00:00
{
size_t func_name_len;
2015-02-01 14:34:55 +00:00
unsigned i;
2015-01-27 03:02:10 +00:00
struct argument args[MAX_ARGS];
unsigned argi = 0;
2015-06-12 22:33:31 +00:00
const char *func_name = NULL;
2015-06-03 14:26:54 +00:00
struct registered_func *rf = registered_functions;
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
invocation->func = NULL;
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
buff = get_ident(buff, &func_name, &func_name_len, error);
if (*error)
goto clean;
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
buff = chomp(buff);
buff = expect_char(buff, '(', error);
if (*error)
goto clean;
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
while (rf->name)
2015-01-24 03:04:56 +00:00
{
if (strncmp(rf->name, func_name, func_name_len) == 0)
{
invocation->func = rf->func;
break;
}
rf++;
}
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
if (!invocation->func)
2015-01-24 03:04:56 +00:00
{
raise_unknown_function(buff.offset, func_name,
func_name_len, error);
goto clean;
}
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
buff = chomp(buff);
while (!peek(buff, ")"))
2015-01-24 03:04:56 +00:00
{
if (argi >= MAX_ARGS)
{
raise_too_many_arguments(error);
goto clean;
}
buff = parse_argument(buff, &args[argi], error);
if (*error)
goto clean;
argi++;
buff = chomp(buff);
buff = expect_char(buff, ',', error);
if (*error)
{
*error = NULL;
break;
}
buff = chomp(buff);
}
2015-01-27 03:02:10 +00:00
buff = expect_char(buff, ')', error);
2015-01-24 03:04:56 +00:00
2015-01-27 03:02:10 +00:00
if (*error)
goto clean;
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
invocation->argc = argi;
invocation->argv = (struct argument*)
malloc(sizeof(struct argument) * argi);
2015-01-24 03:04:56 +00:00
2015-01-27 03:02:10 +00:00
if (!invocation->argv)
2015-01-24 03:04:56 +00:00
{
raise_enomem(error);
goto clean;
}
2015-01-27 03:02:10 +00:00
memcpy(invocation->argv, args,
sizeof(struct argument) * argi);
2015-01-19 21:47:09 +00:00
2015-01-27 03:02:10 +00:00
goto success;
2015-01-19 21:47:09 +00:00
clean:
2015-01-27 03:02:10 +00:00
for (i = 0; i < argi; i++)
argument_free(&args[i]);
2015-01-19 21:47:09 +00:00
success:
2015-01-27 03:02:10 +00:00
return buff;
2015-01-19 21:47:09 +00:00
}
2015-01-24 03:04:56 +00:00
static struct buffer parse_table(struct buffer buff,
2015-02-01 14:34:55 +00:00
struct invocation *invocation, const char **error)
2015-01-24 03:04:56 +00:00
{
unsigned i;
size_t ident_len;
2015-06-07 22:26:58 +00:00
struct argument args[MAX_ARGS];
2015-06-12 22:33:31 +00:00
const char *ident_name = NULL;
2015-01-24 03:04:56 +00:00
unsigned argi = 0;
2015-01-19 21:47:09 +00:00
2015-01-24 03:04:56 +00:00
buff = chomp(buff);
buff = expect_char(buff, '{', error);
2015-01-19 21:47:09 +00:00
2015-01-24 03:04:56 +00:00
if (*error)
goto clean;
2015-01-19 21:47:09 +00:00
2015-01-24 03:04:56 +00:00
buff = chomp(buff);
2015-01-27 03:02:10 +00:00
2015-01-24 03:04:56 +00:00
while (!peek(buff, "}"))
{
if (argi >= MAX_ARGS)
{
raise_too_many_arguments(error);
goto clean;
}
2015-01-19 21:47:09 +00:00
if (isalpha((int)buff.data[buff.offset]))
2015-01-24 03:04:56 +00:00
{
buff = get_ident(buff, &ident_name, &ident_len, error);
if (!*error)
{
2015-06-29 19:39:00 +00:00
args[argi].a.value.type = RDT_STRING;
args[argi].a.value.val.string.len = ident_len;
args[argi].a.value.val.string.buff = (char*)calloc(
2015-01-24 03:04:56 +00:00
ident_len + 1,
sizeof(char)
);
2015-06-29 19:39:00 +00:00
if (!args[argi].a.value.val.string.buff)
2015-01-24 03:04:56 +00:00
goto clean;
strncpy(
2015-06-29 19:39:00 +00:00
args[argi].a.value.val.string.buff,
2015-01-24 03:04:56 +00:00
ident_name,
ident_len
);
}
}
else
2015-06-29 19:39:00 +00:00
buff = parse_string(buff, &args[argi].a.value, error);
2015-01-24 03:04:56 +00:00
if (*error)
goto clean;
args[argi].type = AT_VALUE;
buff = chomp(buff);
argi++;
buff = expect_char(buff, ':', error);
if (*error)
goto clean;
buff = chomp(buff);
if (argi >= MAX_ARGS)
{
raise_too_many_arguments(error);
goto clean;
}
buff = parse_argument(buff, &args[argi], error);
if (*error)
goto clean;
argi++;
buff = chomp(buff);
buff = expect_char(buff, ',', error);
if (*error)
{
*error = NULL;
break;
}
buff = chomp(buff);
}
buff = expect_char(buff, '}', error);
if (*error)
goto clean;
invocation->func = all_map;
invocation->argc = argi;
2015-01-27 03:02:10 +00:00
invocation->argv = (struct argument*)
malloc(sizeof(struct argument) * argi);
2015-01-24 03:04:56 +00:00
if (!invocation->argv)
{
raise_enomem(error);
goto clean;
}
2015-01-27 03:02:10 +00:00
memcpy(invocation->argv, args,
sizeof(struct argument) * argi);
2015-01-24 03:04:56 +00:00
goto success;
2015-01-19 21:47:09 +00:00
clean:
2015-01-24 03:04:56 +00:00
for (i = 0; i < argi; i++)
argument_free(&args[i]);
2015-01-19 21:47:09 +00:00
success:
2015-01-24 03:04:56 +00:00
return buff;
2015-01-19 21:47:09 +00:00
}
2015-02-01 14:34:55 +00:00
static struct buffer parse_argument(struct buffer buff,
struct argument *arg, const char **error)
{
buff = chomp(buff);
if (
isalpha((int)buff.data[buff.offset])
2015-02-01 14:34:55 +00:00
&& !(
peek(buff, "nil")
|| peek(buff, "true")
|| peek(buff, "false")
|| peek(buff, "b\"") || peek(buff, "b'") /* bin string prefix*/
2015-02-01 14:34:55 +00:00
)
)
{
arg->type = AT_FUNCTION;
2015-06-29 19:39:00 +00:00
buff = parse_method_call(buff, &arg->a.invocation, error);
2015-02-01 14:34:55 +00:00
}
else if (peek(buff, "{"))
{
arg->type = AT_FUNCTION;
2015-06-29 19:39:00 +00:00
buff = parse_table(buff, &arg->a.invocation, error);
2015-02-01 14:34:55 +00:00
}
else
{
arg->type = AT_VALUE;
2015-06-29 19:39:00 +00:00
buff = parse_value(buff, &arg->a.value, error);
2015-02-01 14:34:55 +00:00
}
return buff;
}
2015-01-23 04:59:47 +00:00
void libretrodb_query_free(void *q)
{
2015-06-03 14:57:51 +00:00
unsigned i;
struct query *real_q = (struct query*)q;
2015-01-19 21:47:09 +00:00
2015-06-03 14:57:51 +00:00
real_q->ref_count--;
if (real_q->ref_count > 0)
return;
2015-01-19 21:47:09 +00:00
2015-06-03 14:57:51 +00:00
for (i = 0; i < real_q->root.argc; i++)
argument_free(&real_q->root.argv[i]);
2015-06-10 17:48:33 +00:00
free(real_q->root.argv);
real_q->root.argv = NULL;
2015-06-10 19:16:00 +00:00
real_q->root.argc = 0;
2015-06-10 17:48:33 +00:00
free(real_q);
2015-01-19 21:47:09 +00:00
}
2015-02-01 14:34:55 +00:00
void *libretrodb_query_compile(libretrodb_t *db,
const char *query, size_t buff_len, const char **error)
2015-01-23 04:59:47 +00:00
{
2015-01-24 03:04:56 +00:00
struct buffer buff;
2015-06-03 14:26:54 +00:00
struct query *q = (struct query*)calloc(1, sizeof(*q));
2015-01-19 21:47:09 +00:00
2015-01-24 03:04:56 +00:00
if (!q)
goto clean;
2015-01-19 21:47:09 +00:00
2015-02-01 14:34:55 +00:00
q->ref_count = 1;
buff.data = query;
buff.len = buff_len;
buff.offset = 0;
*error = NULL;
2015-01-19 21:53:14 +00:00
2015-04-04 19:10:36 +00:00
buff = chomp(buff);
2015-01-27 03:02:10 +00:00
2015-01-24 03:04:56 +00:00
if (peek(buff, "{"))
{
buff = parse_table(buff, &q->root, error);
if (*error)
goto clean;
}
else if (isalpha((int)buff.data[buff.offset]))
2015-01-24 03:04:56 +00:00
buff = parse_method_call(buff, &q->root, error);
buff = expect_eof(buff, error);
if (*error)
goto clean;
2015-02-01 14:34:55 +00:00
if (!q->root.func)
2015-01-24 03:04:56 +00:00
{
raise_unexpected_eof(buff.offset, error);
return NULL;
}
goto success;
2015-01-19 21:47:09 +00:00
clean:
2015-01-24 03:04:56 +00:00
if (q)
libretrodb_query_free(q);
2015-01-19 21:47:09 +00:00
success:
2015-01-24 03:04:56 +00:00
return q;
2015-01-19 21:47:09 +00:00
}
2015-01-23 04:59:47 +00:00
void libretrodb_query_inc_ref(libretrodb_query_t *q)
{
2015-02-01 14:34:55 +00:00
struct query *rq = (struct query*)q;
if (rq)
rq->ref_count += 1;
2015-01-19 21:47:09 +00:00
}
2015-02-01 14:34:55 +00:00
int libretrodb_query_filter(libretrodb_query_t *q,
struct rmsgpack_dom_value *v)
2015-01-24 03:04:56 +00:00
{
struct invocation inv = ((struct query *)q)->root;
struct rmsgpack_dom_value res = inv.func(*v, inv.argc, inv.argv);
2015-06-26 18:33:40 +00:00
return (res.type == RDT_BOOL && res.val.bool_);
2015-01-19 21:47:09 +00:00
}