From 1644bbec6161ec971a2ba3c213ce285b995cac22 Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Thu, 1 Jun 2023 11:04:38 +0200 Subject: cooker: OP_CALL and OP_COPY stuff ...mostly. Signed-off-by: Stefano Brivio --- cooker/parse.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 9 deletions(-) (limited to 'cooker/parse.c') diff --git a/cooker/parse.c b/cooker/parse.c index acb6810..64901f5 100644 --- a/cooker/parse.c +++ b/cooker/parse.c @@ -12,18 +12,11 @@ #include "calls.h" #include "cooker.h" #include "gluten.h" +#include "call.h" #include "match.h" #include "emit.h" #include "util.h" -#include "calls/net.h" - -static void handle_call(struct gluten_ctx *g, JSON_Value *value) -{ - (void)g; - (void)value; -} - static void handle_inject(struct gluten_ctx *g, JSON_Value *value) { (void)g; @@ -64,7 +57,7 @@ struct rule_parser { void (*fn)(struct gluten_ctx *g, JSON_Value *value); } parsers[] = { { "match", handle_matches }, - { "call", handle_call }, + { "call", handle_calls }, { "inject", handle_inject }, { "limit", handle_limit }, { "return", handle_return }, @@ -73,6 +66,87 @@ struct rule_parser { { NULL, NULL }, }; +/** + * value_get_num() - Get numeric value from description matching JSON input + * @desc: Description of possible values from model + * @value: JSON value + * + * Return: numeric value + */ +long long value_get_num(struct num *desc, JSON_Value *value) +{ + const char *s = NULL; + long long n; + + if (desc) { + s = json_value_get_string(value); + for (; desc->name && s && strcmp(s, desc->name); desc++); + if (s && !desc->name) + die(" Invalid value %s", s); + + n = desc->value; + } + + if (!s) { + if (json_value_get_type(value) != JSONNumber) + die(" Invalid value type"); + + n = json_value_get_number(value); + } + + return n; +} + +/** + * value_get() - Get generic value from description matching JSON input + * @desc: Description of possible values from model + * @type: Data type from model + * @value: JSON value + * @out: Corresponding bytecode value, set on return + */ +void value_get(union desc desc, enum type type, JSON_Value *value, + union value *out) +{ + if (TYPE_IS_NUM(type)) + out->v_num = value_get_num(desc.d_num, value); +} + +/** + * select_field() - Select field based on value and description + * @g: gluten context + * @pos: Index of syscall argument being parsed + * @s: Possible selection choices + * @v: Selector value + * + * Return: pointer to field, NULL if selector refers to a different argument + */ +struct field *select_field(struct gluten_ctx *g, int pos, + struct select *s, union value v) +{ + if (TYPE_IS_NUM(s->field->type)) { + struct select_num *d_num; + + for (d_num = s->desc.d_num; d_num->target.f.type; d_num++) { + if (d_num->value == v.v_num) { + if (d_num->target.pos == pos) + return &d_num->target.f; + + if (g) { + pos = d_num->target.pos; + g->selected_arg[pos] = &d_num->target; + } + + return NULL; + } + } + + if (!d_num->target.f.type) + die(" No match for numeric selector %i", v.v_num); + } + + die(" not supported yet"); +} + /** * parse_block() - Parse a transformation block with rules * @g: gluten context -- cgit v1.2.3