libamxp
1.4.0
Patterns C Implementation
|
Logical expression parser and evaluator. More...
Typedefs | |
typedef enum _expr_status | amxp_expr_status_t |
Expression status/error codes. More... | |
typedef ssize_t(* | amxp_expr_reader_t) (amxp_expr_t *expr, void *buf, size_t max_size) |
Expression reader function. More... | |
typedef amxp_expr_status_t(* | amxp_expr_get_field_t) (amxp_expr_t *expr, amxc_var_t *value, const char *path, void *priv) |
Field fetcher function signature. More... | |
typedef amxp_expr_status_t(* | amxp_expr_value_func_t) (amxp_expr_t *expr, amxc_var_t *args, amxc_var_t *ret) |
Callback function that can "calculate" a value. More... | |
typedef bool(* | amxp_expr_bool_func_t) (amxp_expr_t *expr, amxc_var_t *args) |
Callback function that evaluates to true or false. More... | |
Enumerations | |
enum | _expr_status { amxp_expr_status_ok , amxp_expr_status_unknown_error , amxp_expr_status_syntax_error , amxp_expr_status_field_not_found , amxp_expr_status_invalid_regexp , amxp_expr_status_invalid_value , amxp_expr_status_function_not_found } |
Expression status/error codes. More... | |
Functions | |
amxp_expr_status_t | amxp_expr_new (amxp_expr_t **expr, const char *expression) |
Allocates and initializes an expression. More... | |
amxp_expr_status_t | amxp_expr_buildf_new (amxp_expr_t **expr, const char *expr_fmt,...) __attribute__((format(printf |
Allocates and initializes an expression with format-string and safety checks. More... | |
amxp_expr_status_t amxp_expr_status_t | amxp_expr_vbuildf_new (amxp_expr_t **expr, const char *expr_fmt, va_list args) |
va_list version of amxp_expr_buildf_new More... | |
void | amxp_expr_delete (amxp_expr_t **expr) |
Deletes a previously allocated expression structure. More... | |
amxp_expr_status_t | amxp_expr_buildf (amxp_expr_t *expr, const char *expr_fmt,...) __attribute__((format(printf |
Initializes an expression structure. More... | |
amxp_expr_status_t amxp_expr_status_t | amxp_expr_vbuildf (amxp_expr_t *expr, const char *expr_fmt, va_list args) |
va_list version of amxp_expr_buildf More... | |
amxp_expr_status_t | amxp_expr_init (amxp_expr_t *expr, const char *expression) |
Initializes an expression structure. More... | |
void | amxp_expr_clean (amxp_expr_t *expr) |
Clean-up the expression structure. More... | |
void | amxp_expr_dump_tree (amxp_expr_t *expr) |
Dumps the binary tree in dot formatted text file to stdout. More... | |
bool | amxp_expr_evaluate (amxp_expr_t *expr, amxp_expr_get_field_t fn, void *priv, amxp_expr_status_t *status) |
Evaluates an expression. More... | |
bool | amxp_expr_eval (amxp_expr_t *expr, amxp_expr_status_t *status) |
Evaluates an expression. More... | |
bool | amxp_expr_eval_var (amxp_expr_t *expr, const amxc_var_t *const data, amxp_expr_status_t *status) |
Evaluates an expression against a composite variant. More... | |
int | amxp_expr_find_var_paths (const amxc_var_t *const var, amxc_llist_t *paths, const char *path) |
Search matching variant paths in a composite variant. More... | |
int | amxp_expr_find_var_values (const amxc_var_t *const var, amxc_htable_t *values, const char *path) |
Search matching variant paths in a composite variant. More... | |
amxc_var_t * | amxp_expr_find_var (const amxc_var_t *const var, const char *path) |
Search a matching variant and returns a pointer to that variant. More... | |
amxp_expr_status_t | amxp_expr_get_field_var (amxp_expr_t *expr, amxc_var_t *value, const char *path, void *priv) |
Variant field fetcher implementation. More... | |
bool | amxp_expr_eval_set (amxp_expr_t *expr, const amxc_set_t *const data, amxp_expr_status_t *status) |
Evaluates an expression against a set. More... | |
amxp_expr_status_t | amxp_expr_get_field_set (amxp_expr_t *expr, amxc_var_t *value, const char *path, void *priv) |
Flag field fetcher implementation for sets. More... | |
int | amxp_expr_add_value_fn (const char *fn_name, amxp_expr_value_func_t fn) |
Adds a value calculation function. More... | |
int | amxp_expr_add_bool_fn (const char *fn_name, amxp_expr_bool_func_t fn) |
Adds a boolean evaluation function. More... | |
const char * | amxp_expr_get_string (amxp_expr_t *expr) |
Returns the string representation of the given expression. More... | |
Logical expression parser and evaluator.
An logical expression evaluates to true or to false.
An expression can consist out of multiple expression parts, concatenated with a logical "OR" or logical "AND"
Syntax of an expression part
Each expression part has compare operator, a left value and a right value.
An VALUE can be:
Boolean values are case insensitive. There will be a match for true, True or even TrUe. The same comment applies to false.
The supported COMPARE OPERATORS are:
Supported logical operators:
typedef bool(* amxp_expr_bool_func_t) (amxp_expr_t *expr, amxc_var_t *args) |
Callback function that evaluates to true or false.
Not all values can be expressed as a field or a constant, sometimes more complex operations are used. It is possible to define custom boolean method that evaluates the provided arguments and returns true or false.
Boolean functions can be used in any location in an expression when a logical expression is needed.
expr | pointer to the expression structure |
args | variant containing the arguments of the function |
Definition at line 240 of file amxp_expression.h.
typedef amxp_expr_status_t(* amxp_expr_get_field_t) (amxp_expr_t *expr, amxc_var_t *value, const char *path, void *priv) |
Field fetcher function signature.
An logical expression with only constant values is not very usefull.
A field fetcher function can be implemented and is used during the expression evaluation to get values for the fields used in the expression.
Depending on the situation or the specific problem a custom field fetcher is needed. When using libamxc variants as data, the field fetcher amxp_expr_get_field_var can be used.
expr | pointer to the expression structure |
value | the variant in which the value must be stored |
path | the field |
priv | the private user data |
Definition at line 193 of file amxp_expression.h.
typedef ssize_t(* amxp_expr_reader_t) (amxp_expr_t *expr, void *buf, size_t max_size) |
Expression reader function.
A ringbuffer is used to store the expression and read from it during parsing of the expression.
Currently it is not possible to implement a custom reader function and set that function pointer.
Definition at line 166 of file amxp_expression.h.
typedef enum _expr_status amxp_expr_status_t |
Expression status/error codes.
typedef amxp_expr_status_t(* amxp_expr_value_func_t) (amxp_expr_t *expr, amxc_var_t *args, amxc_var_t *ret) |
Callback function that can "calculate" a value.
Not all values can be expressed as a field or a constant, sometimes more complex operations are used. It is possible to define custom value calculation methods and register them using amxp_expr_add_value_fn.
Value functions can be used in any location in an expression where a value is needed.
expr | pointer to the expression structure |
args | variant containing the arguments of the function |
ret | The calculated value |
Definition at line 218 of file amxp_expression.h.
enum _expr_status |
Expression status/error codes.
Definition at line 144 of file amxp_expression.h.
int amxp_expr_add_bool_fn | ( | const char * | fn_name, |
amxp_expr_bool_func_t | fn | ||
) |
Adds a boolean evaluation function.
Adds a callback function with a given name to the possible boolean evaluation functions.
fn_name | name of the function with which it can be used in the expressions |
fn | the function pointer and must match amxp_expr_bool_func_t signature |
Definition at line 838 of file amxp_expression_main.c.
int amxp_expr_add_value_fn | ( | const char * | fn_name, |
amxp_expr_value_func_t | fn | ||
) |
Adds a value calculation function.
Adds a callback function with a given name to the possible value calculation functions.
fn_name | name of the function with which it can be used in the expressions |
fn | the function pointer and must match amxp_expr_value_func_t signature |
Definition at line 820 of file amxp_expression_main.c.
amxp_expr_status_t amxp_expr_buildf | ( | amxp_expr_t * | expr, |
const char * | expr_fmt, | ||
... | |||
) |
Initializes an expression structure.
The same as amxp_expr_buildf_new but without new heap-allocation.
expr | a pointer to the location where the pointer to the new expression can be stored |
expr_fmt | a string containing a logical expression with placeholders. |
amxp_expr_status_t amxp_expr_buildf_new | ( | amxp_expr_t ** | expr, |
const char * | expr_fmt, | ||
... | |||
) |
Allocates and initializes an expression with format-string and safety checks.
Allocates memory on the heap to store the expression data structure.
The provided expression format string and the replacement values are used to build the expression string. In the expression format string the nth placeholders is replaced with the nth replacement value in the variadic arguments (...
).
The supported placeholders ('s', 'i', ...) are the ones supported by amxc_string_appendf_escape.
Example of a expression format string:
When the variadic arguments are:
The expression string that is build will be:
Each value in the values variant is verified to avoid the expression, or the context of the expression, being interpreted in an unintended way.
Example of invalid values:
This would resolve into the following expression string:
Which is not the intention, so the first value is considered invalid and the expression build will fail.
The builded expression string is parsed and stored for later use. When the expression is invalid an error is returned and no memory is allocated.
Once an expression structure is available, it can be reused many times.
expr | a pointer to the location where the pointer to the new expression can be stored |
expr_fmt | a string containing a logical expression with placeholders. |
void amxp_expr_clean | ( | amxp_expr_t * | expr | ) |
Clean-up the expression structure.
Cleans up all internally allocated memory. After calling this function the expression data structure must be re-initialized before use.
expr | a pointer to the expression structure |
Definition at line 766 of file amxp_expression_main.c.
void amxp_expr_delete | ( | amxp_expr_t ** | expr | ) |
Deletes a previously allocated expression structure.
Frees memory previously allocated.
expr | a pointer to the location where the pointer to the allocated expression |
Definition at line 652 of file amxp_expression_main.c.
void amxp_expr_dump_tree | ( | amxp_expr_t * | expr | ) |
Dumps the binary tree in dot formatted text file to stdout.
Using the output as input to the dot tool a graph can be created the represents the builded binary tree by the expression parser.
expr | a pointer to the expression structure |
Definition at line 783 of file amxp_expression_main.c.
bool amxp_expr_eval | ( | amxp_expr_t * | expr, |
amxp_expr_status_t * | status | ||
) |
Evaluates an expression.
Calls amxp_expr_evaluate without a field fetcher callback function.
Can only be used with expressions that don't contain any fields
expr | a pointer to the expression structure |
status | will contain the status code, |
Definition at line 816 of file amxp_expression_main.c.
bool amxp_expr_eval_set | ( | amxp_expr_t * | expr, |
const amxc_set_t *const | data, | ||
amxp_expr_status_t * | status | ||
) |
Evaluates an expression against a set.
Calls amxp_expr_evaluate with a field fetcher callback function (see amxp_expr_get_field_set) The values of the fields in the expression are flags that are or are not in the given set.
expr | a pointer to the expression structure |
data | a set |
status | will contain the status code, |
Definition at line 59 of file amxp_expression_set.c.
bool amxp_expr_eval_var | ( | amxp_expr_t * | expr, |
const amxc_var_t *const | data, | ||
amxp_expr_status_t * | status | ||
) |
Evaluates an expression against a composite variant.
Calls amxp_expr_evaluate with a field fetcher callback function (see amxp_expr_get_field_var) The values of the fields in the expression are fetched from the given variant.
expr | a pointer to the expression structure |
data | a variant |
status | will contain the status code, |
Definition at line 315 of file amxp_expression_var.c.
bool amxp_expr_evaluate | ( | amxp_expr_t * | expr, |
amxp_expr_get_field_t | fn, | ||
void * | priv, | ||
amxp_expr_status_t * | status | ||
) |
Evaluates an expression.
If the expression provided (see amxp_expr_init or amxp_expr_new) contains fields, the field fetcher callback is called for each field encountered in the expression. The field fetcher callback function must return the value represented by the field.
How the field fetcher works depends on the implementation.
If no field fetcher callback is set and the expression contains fields, the expression will evaluate to false and the status code is set to amxp_expr_status_field_not_found.
expr | a pointer to the expression structure |
fn | a field fetcher callback function. |
priv | private data pointer, is passed as is to the field fetcher callback function |
status | will contain the status code, |
Definition at line 787 of file amxp_expression_main.c.
amxc_var_t* amxp_expr_find_var | ( | const amxc_var_t *const | var, |
const char * | path | ||
) |
Search a matching variant and returns a pointer to that variant.
Using expressions or a wildcard '*' at the position of a list variant or a table variant the mathcing variants are filtered out. The expressions must be put between square brackets.
When multiple variants are matching the path, a NULL pointer is returned.
When multiple variants paths can match you can use amxp_expr_find_var_values or amxp_expr_find_var_paths instead.
var | the composite variant |
path | the variant path, it may contain wildcards or expressions |
Definition at line 412 of file amxp_expression_var.c.
int amxp_expr_find_var_paths | ( | const amxc_var_t *const | var, |
amxc_llist_t * | paths, | ||
const char * | path | ||
) |
Search matching variant paths in a composite variant.
Using expressions or a wildcard '*' at the position of a list variant or a table variant the mathcing variants are filtered out. The expressions must be put between square brackets.
All matching variant paths are added to the linked list. The value of each matching variant can be fetched using the GET macros provided in libamxc
Example:
Data set in json notation
Search path: "94:83:c4:06:da:66.[MACAddress == '00:01:02:03:44:f0'].Extender" Result: ["'94:83:c4:06:da:66'.0.'Extender'"]
Search path: "94:83:c4:06:da:66.*.Extender" Result: ["'94:83:c4:06:da:66'.0.'Extender'", "'94:83:c4:06:da:66'.1.'Extender'"]
var | the composite variant |
paths | a linked list that will be filled with all paths to the variants that match |
path | the variant path, it may contain wildcards or expressions |
Definition at line 348 of file amxp_expression_var.c.
int amxp_expr_find_var_values | ( | const amxc_var_t *const | var, |
amxc_htable_t * | values, | ||
const char * | path | ||
) |
Search matching variant paths in a composite variant.
Using expressions or a wildcard '*' at the position of a list variant or a table variant the mathcing variants are filtered out. The expressions must be put between square brackets.
All matching variant paths are added to the hash table. The keys in the table are the matching variant paths, the values are copies of the mathcing variants.
Example:
Data set in json notation
Search path: "94:83:c4:06:da:66.[MACAddress == '00:01:02:03:44:f0'].Extender" Result: {"'94:83:c4:06:da:66'.0.'Extender'" = true}
Search path: "94:83:c4:06:da:66.*.Extender" Result: {"'94:83:c4:06:da:66'.0.'Extender'" = true, "'94:83:c4:06:da:66'.1.'Extender'" = false }
var | the composite variant |
values | a htable that will be filled with all paths and values of the variants that match the given path |
path | the variant path, it may contain wildcards or expressions |
Definition at line 379 of file amxp_expression_var.c.
amxp_expr_status_t amxp_expr_get_field_set | ( | amxp_expr_t * | expr, |
amxc_var_t * | value, | ||
const char * | path, | ||
void * | priv | ||
) |
Flag field fetcher implementation for sets.
Used by amxp_expr_eval_set. This function should not be called directly.
expr | a pointer to the expression structure |
value | the value of the field, filled by this function |
path | the flag name |
priv | the private data as provided to amxp_expr_evaluate |
amxp_expr_status_t amxp_expr_get_field_var | ( | amxp_expr_t * | expr, |
amxc_var_t * | value, | ||
const char * | path, | ||
void * | priv | ||
) |
Variant field fetcher implementation.
Used by amxp_expr_eval_var. This function should not be called directly.
expr | a pointer to the expression structure |
value | the value of the field, filled by this function |
path | the field name |
priv | the private data as provided to amxp_expr_evaluate |
const char* amxp_expr_get_string | ( | amxp_expr_t * | expr | ) |
Returns the string representation of the given expression.
expr | a pointer to the expression structure |
Definition at line 857 of file amxp_expression_main.c.
amxp_expr_status_t amxp_expr_init | ( | amxp_expr_t * | expr, |
const char * | expression | ||
) |
Initializes an expression structure.
The provided expression is parsed and stored for later use. When the expression is invalid an error is returned.
Once an expression structure is initialized, it can be reused many times.
expr | a pointer to the expression structure |
expression | a string containing a logical expression. |
Definition at line 696 of file amxp_expression_main.c.
amxp_expr_status_t amxp_expr_new | ( | amxp_expr_t ** | expr, |
const char * | expression | ||
) |
Allocates and initializes an expression.
Allocates memory on the heap to store the expression data structure. The provided expression is parsed and stored for later use. When the expression is invalid an error is returned and no memory is allocated.
Once an expression structure is available, it can be reused many times.
expr | a pointer to the location where the pointer to the new expression can be stored |
expression | a string containing a logical expression. |
Definition at line 633 of file amxp_expression_main.c.
amxp_expr_status_t amxp_expr_status_t amxp_expr_vbuildf | ( | amxp_expr_t * | expr, |
const char * | expr_fmt, | ||
va_list | args | ||
) |
va_list version of amxp_expr_buildf
expr | a pointer to the location where the pointer to the new expression can be stored |
expr_fmt | a string containing a logical expression with placeholders. |
args | va list of arguments. |
Definition at line 732 of file amxp_expression_main.c.
amxp_expr_status_t amxp_expr_status_t amxp_expr_vbuildf_new | ( | amxp_expr_t ** | expr, |
const char * | expr_fmt, | ||
va_list | args | ||
) |
va_list version of amxp_expr_buildf_new
expr | a pointer to the location where the pointer to the new expression can be stored |
expr_fmt | a string containing a logical expression with placeholders. |
args | va list of arguments. |
Definition at line 664 of file amxp_expression_main.c.