(query.c) Cleanups

This commit is contained in:
twinaphex 2020-07-05 04:21:27 +02:00
parent c1634914d6
commit a3c1d4ac98
1 changed files with 71 additions and 93 deletions

View File

@ -100,14 +100,6 @@ static struct buffer query_parse_table(char *s,
struct invocation *invocation, const char **error); struct invocation *invocation, const char **error);
/* Errors */ /* Errors */
static void query_raise_too_many_arguments(
char *tmp, size_t len, const char **error)
{
strlcpy(tmp,
"Too many arguments in function call.", len);
*error = tmp;
}
static struct rmsgpack_dom_value query_func_is_true( static struct rmsgpack_dom_value query_func_is_true(
struct rmsgpack_dom_value input, struct rmsgpack_dom_value input,
unsigned argc, const struct argument *argv) unsigned argc, const struct argument *argv)
@ -288,43 +280,6 @@ struct registered_func registered_functions[100] = {
{NULL, NULL} {NULL, NULL}
}; };
static void query_raise_expected_number(
char *tmp, size_t len,
ssize_t where, const char **error)
{
snprintf(tmp, len,
"%" PRIu64 "::Expected number",
(uint64_t)where);
*error = tmp;
}
static void query_raise_expected_string(
char *tmp, size_t len,
ssize_t where, const char ** error)
{
snprintf(tmp, len,
"%" PRIu64 "::Expected string",
(uint64_t)where);
*error = tmp;
}
static void query_raise_unexpected_eof(
char *tmp, size_t len,
ssize_t where, const char ** error)
{
snprintf(tmp, len,
"%" PRIu64 "::Unexpected EOF",
(uint64_t)where
);
*error = tmp;
}
static void query_raise_enomem(char *s, size_t len, const char **error)
{
strlcpy(s, "Out of memory", len);
*error = s;
}
static void query_raise_unknown_function( static void query_raise_unknown_function(
char *s, size_t _len, char *s, size_t _len,
ssize_t where, const char *name, ssize_t where, const char *name,
@ -342,28 +297,6 @@ static void query_raise_unknown_function(
*error = s; *error = s;
} }
static void query_raise_expected_eof(char *s, size_t len,
ssize_t where, char found, const char **error)
{
snprintf(s, len,
"%" PRIu64 "::Expected EOF found '%c'",
(uint64_t)where,
found
);
*error = s;
}
static void query_raise_unexpected_char(
char *s, size_t len,
ssize_t where, char expected, char found,
const char **error)
{
snprintf(s, len,
"%" PRIu64 "::Expected '%c' found '%c'",
(uint64_t)where, expected, found);
*error = s;
}
static void query_argument_free(struct argument *arg) static void query_argument_free(struct argument *arg)
{ {
unsigned i; unsigned i;
@ -395,7 +328,12 @@ static struct buffer query_parse_integer(
(int64_t*)&value->val.int_) == 0); (int64_t*)&value->val.int_) == 0);
if (test) if (test)
query_raise_expected_number(s, len, buff.offset, error); {
snprintf(s, len,
"%" PRIu64 "::Expected number",
(uint64_t)buff.offset);
*error = s;
}
else else
{ {
while (isdigit((int)buff.data[buff.offset])) while (isdigit((int)buff.data[buff.offset]))
@ -417,8 +355,14 @@ static struct buffer query_expect_eof(char *s, size_t len,
{ {
buff = query_chomp(buff); buff = query_chomp(buff);
if ((unsigned)buff.offset < buff.len) if ((unsigned)buff.offset < buff.len)
query_raise_expected_eof(s, len, {
buff.offset, buff.data[buff.offset], error); snprintf(s, len,
"%" PRIu64 "::Expected EOF found '%c'",
(uint64_t)buff.offset,
buff.data[buff.offset]
);
*error = s;
}
return buff; return buff;
} }
@ -446,9 +390,11 @@ static struct buffer query_get_char(
{ {
if (query_is_eot(buff)) if (query_is_eot(buff))
{ {
query_raise_unexpected_eof( snprintf(s, len,
s, len, "%" PRIu64 "::Unexpected EOF",
buff.offset, error); (uint64_t)buff.offset
);
*error = s;
return buff; return buff;
} }
@ -481,8 +427,10 @@ static struct buffer query_parse_string(
if (terminator != '"' && terminator != '\'') if (terminator != '"' && terminator != '\'')
{ {
buff.offset--; buff.offset--;
query_raise_expected_string(s, len, snprintf(s, len,
buff.offset, error); "%" PRIu64 "::Expected string",
(uint64_t)buff.offset);
*error = s;
} }
str_start = buff.data + buff.offset; str_start = buff.data + buff.offset;
@ -506,7 +454,10 @@ static struct buffer query_parse_string(
value->val.string.buff = (char*)calloc(count, sizeof(char)); value->val.string.buff = (char*)calloc(count, sizeof(char));
if (!value->val.string.buff) if (!value->val.string.buff)
query_raise_enomem(s, len, error); {
strlcpy(s, "Out of memory", len);
*error = s;
}
else if (is_binstr) else if (is_binstr)
{ {
unsigned i; unsigned i;
@ -545,12 +496,12 @@ static struct buffer query_parse_value(
struct buffer buff, struct buffer buff,
struct rmsgpack_dom_value *value, const char **error) struct rmsgpack_dom_value *value, const char **error)
{ {
buff = query_chomp(buff); buff = query_chomp(buff);
if (query_peek(buff, "nil")) if (query_peek(buff, "nil"))
{ {
buff.offset += STRLEN_CONST("nil"); buff.offset += STRLEN_CONST("nil");
value->type = RDT_NULL; value->type = RDT_NULL;
} }
else if (query_peek(buff, "true")) else if (query_peek(buff, "true"))
{ {
@ -581,7 +532,11 @@ static void query_peek_char(char *s, size_t len,
{ {
if (query_is_eot(buff)) if (query_is_eot(buff))
{ {
query_raise_unexpected_eof(s, len, buff.offset, error); snprintf(s, len,
"%" PRIu64 "::Unexpected EOF",
(uint64_t)buff.offset
);
*error = s;
return; return;
} }
@ -598,7 +553,11 @@ static struct buffer query_get_ident(
if (query_is_eot(buff)) if (query_is_eot(buff))
{ {
query_raise_unexpected_eof(s, _len, buff.offset, error); snprintf(s, _len,
"%" PRIu64 "::Unexpected EOF",
(uint64_t)buff.offset
);
*error = s;
return buff; return buff;
} }
@ -634,10 +593,20 @@ static struct buffer query_expect_char(
char c, const char ** error) char c, const char ** error)
{ {
if ((unsigned)buff.offset >= buff.len) if ((unsigned)buff.offset >= buff.len)
query_raise_unexpected_eof(s, len, buff.offset, error); {
snprintf(s, len,
"%" PRIu64 "::Unexpected EOF",
(uint64_t)buff.offset
);
*error = s;
}
else if (buff.data[buff.offset] != c) else if (buff.data[buff.offset] != c)
query_raise_unexpected_char(s, len, {
buff.offset, c, buff.data[buff.offset], error); snprintf(s, len,
"%" PRIu64 "::Expected '%c' found '%c'",
(uint64_t)buff.offset, c, buff.data[buff.offset]);
*error = s;
}
else else
buff.offset++; buff.offset++;
return buff; return buff;
@ -726,7 +695,9 @@ static struct buffer query_parse_method_call(
{ {
if (argi >= QUERY_MAX_ARGS) if (argi >= QUERY_MAX_ARGS)
{ {
query_raise_too_many_arguments(s, len, error); strlcpy(s,
"Too many arguments in function call.", len);
*error = s;
goto clean; goto clean;
} }
@ -757,7 +728,8 @@ static struct buffer query_parse_method_call(
if (!invocation->argv) if (!invocation->argv)
{ {
query_raise_enomem(s, len, error); strlcpy(s, "Out of memory", len);
*error = s;
goto clean; goto clean;
} }
memcpy(invocation->argv, args, memcpy(invocation->argv, args,
@ -848,7 +820,9 @@ static struct buffer query_parse_table(
{ {
if (argi >= QUERY_MAX_ARGS) if (argi >= QUERY_MAX_ARGS)
{ {
query_raise_too_many_arguments(s, len, error); strlcpy(s,
"Too many arguments in function call.", len);
*error = s;
goto clean; goto clean;
} }
@ -895,7 +869,9 @@ static struct buffer query_parse_table(
if (argi >= QUERY_MAX_ARGS) if (argi >= QUERY_MAX_ARGS)
{ {
query_raise_too_many_arguments(s, len, error); strlcpy(s,
"Too many arguments in function call.", len);
*error = s;
goto clean; goto clean;
} }
@ -927,7 +903,8 @@ static struct buffer query_parse_table(
if (!invocation->argv) if (!invocation->argv)
{ {
query_raise_enomem(s, len, error); strlcpy(s, "Out of memory", len);
*error = s;
goto clean; goto clean;
} }
memcpy(invocation->argv, args, memcpy(invocation->argv, args,
@ -1004,10 +981,11 @@ void *libretrodb_query_compile(libretrodb_t *db,
if (!q->root.func) if (!q->root.func)
{ {
query_raise_unexpected_eof( snprintf(tmp_error_buff, error_buff_len,
tmp_error_buff, "%" PRIu64 "::Unexpected EOF",
error_buff_len, (uint64_t)buff.offset
buff.offset, error_string); );
*error_string = tmp_error_buff;
goto error; goto error;
} }