From d0d6216f37860b7f8371b63a7bcc44153d76bade Mon Sep 17 00:00:00 2001 From: Stephan Bosch <stephan@rename-it.nl> Date: Sun, 6 Apr 2008 15:16:37 +0200 Subject: [PATCH] Variables: exported new operand definitions to separate file. --- README | 2 +- src/lib-sieve/plugins/variables/Makefile.am | 4 +- .../plugins/variables/ext-variables-common.c | 232 ---------------- .../variables/ext-variables-operands.c | 254 ++++++++++++++++++ .../variables/ext-variables-operands.h | 43 +++ .../plugins/variables/ext-variables.c | 1 + 6 files changed, 302 insertions(+), 234 deletions(-) create mode 100644 src/lib-sieve/plugins/variables/ext-variables-operands.c create mode 100644 src/lib-sieve/plugins/variables/ext-variables-operands.h diff --git a/README b/README index b057a9823..5c0d2bc96 100644 --- a/README +++ b/README @@ -133,7 +133,7 @@ Extensions and their implementation status: comparator-i;ascii-numeric: full relational: full copy: full - regex: full, but suboptimal + regex: full, but suboptimal, no UTF-8 body: full, but text body-transform implementation is simple include: almost full; needs more work (no variables; no external binaries) vacation: almost full; no support for required References header diff --git a/src/lib-sieve/plugins/variables/Makefile.am b/src/lib-sieve/plugins/variables/Makefile.am index 675423797..de6badd9b 100644 --- a/src/lib-sieve/plugins/variables/Makefile.am +++ b/src/lib-sieve/plugins/variables/Makefile.am @@ -17,6 +17,7 @@ libsieve_ext_variables_la_SOURCES = \ ext-variables-common.c \ ext-variables-name.c \ ext-variables-arguments.c \ + ext-variables-operands.c \ $(cmds) \ $(tsts) \ ext-variables.c @@ -25,5 +26,6 @@ noinst_HEADERS = \ ext-variables-common.h \ ext-variables-name.h \ ext-variables-arguments.h \ - sieve-ext-variables.h + ext-variables-operands.h \ + sieve-ext-variables.h diff --git a/src/lib-sieve/plugins/variables/ext-variables-common.c b/src/lib-sieve/plugins/variables/ext-variables-common.c index 096615e22..95c9beb53 100644 --- a/src/lib-sieve/plugins/variables/ext-variables-common.c +++ b/src/lib-sieve/plugins/variables/ext-variables-common.c @@ -264,238 +264,6 @@ struct sieve_variable_storage *ext_variables_interpreter_get_storage return ctx->local_storage; } -/* - * Operands - */ - -/* Variable operand */ - -static bool opr_variable_read_value - (const struct sieve_runtime_env *renv, sieve_size_t *address, string_t **str); -static bool opr_variable_dump - (const struct sieve_dumptime_env *denv, sieve_size_t *address); - -const struct sieve_opr_string_interface variable_interface = { - opr_variable_dump, - opr_variable_read_value -}; - -const struct sieve_operand variable_operand = { - "variable", - &variables_extension, - EXT_VARIABLES_OPERAND_VARIABLE, - &string_class, - &variable_interface -}; - -void ext_variables_opr_variable_emit - (struct sieve_binary *sbin, struct sieve_variable *var) -{ - (void) sieve_operand_emit_code(sbin, &variable_operand, ext_variables_my_id); - (void) sieve_binary_emit_integer(sbin, var->index); -} - -static bool opr_variable_dump - (const struct sieve_dumptime_env *denv, sieve_size_t *address) -{ - sieve_size_t index = 0; - - if (sieve_binary_read_integer(denv->sbin, address, &index) ) { - sieve_code_dumpf(denv, "VAR: %ld", (long) index); - - return TRUE; - } - - return FALSE; -} - -static bool opr_variable_read_value - (const struct sieve_runtime_env *renv, sieve_size_t *address, string_t **str) -{ - struct sieve_variable_storage *storage; - sieve_size_t index = 0; - - storage = ext_variables_interpreter_get_storage(renv->interp); - if ( storage == NULL ) return FALSE; - - if (sieve_binary_read_integer(renv->sbin, address, &index) ) { - /* Parameter str can be NULL if we are requested to only skip and not - * actually read the argument. - */ - if ( str != NULL ) { - sieve_variable_get(storage, index, str); - - if ( *str == NULL ) *str = t_str_new(0); - } - return TRUE; - } - - return FALSE; -} - -bool ext_variables_opr_variable_read - (const struct sieve_runtime_env *renv, sieve_size_t *address, - struct sieve_variable_storage **storage, unsigned int *var_index) -{ - const struct sieve_operand *operand = sieve_operand_read(renv->sbin, address); - sieve_size_t idx = 0; - - if ( operand != &variable_operand ) - return FALSE; - - *storage = ext_variables_interpreter_get_storage(renv->interp); - if ( *storage == NULL ) return FALSE; - - if (sieve_binary_read_integer(renv->sbin, address, &idx) ) { - *var_index = idx; - return TRUE; - } - - return FALSE; -} - -/* Match value operand */ - -static bool opr_match_value_read - (const struct sieve_runtime_env *renv, sieve_size_t *address, string_t **str); -static bool opr_match_value_dump - (const struct sieve_dumptime_env *denv, sieve_size_t *address); - -const struct sieve_opr_string_interface match_value_interface = { - opr_match_value_dump, - opr_match_value_read -}; - -const struct sieve_operand match_value_operand = { - "match-value", - &variables_extension, - EXT_VARIABLES_OPERAND_MATCH_VALUE, - &string_class, - &match_value_interface -}; - -void ext_variables_opr_match_value_emit - (struct sieve_binary *sbin, unsigned int index) -{ - (void) sieve_operand_emit_code - (sbin, &match_value_operand, ext_variables_my_id); - (void) sieve_binary_emit_integer(sbin, index); -} - -static bool opr_match_value_dump - (const struct sieve_dumptime_env *denv, sieve_size_t *address) -{ - sieve_size_t index = 0; - - if (sieve_binary_read_integer(denv->sbin, address, &index) ) { - sieve_code_dumpf(denv, "MVALUE: %ld", (long) index); - - return TRUE; - } - - return FALSE; -} - -static bool opr_match_value_read - (const struct sieve_runtime_env *renv, sieve_size_t *address, string_t **str) -{ - sieve_size_t index = 0; - - if (sieve_binary_read_integer(renv->sbin, address, &index) ) { - /* Parameter str can be NULL if we are requested to only skip and not - * actually read the argument. - */ - if ( str != NULL ) { - sieve_match_values_get(renv->interp, (unsigned int) index, str); - - if ( *str == NULL ) *str = t_str_new(0); - } - return TRUE; - } - - return FALSE; -} - -/* Variable string operand */ - -static bool opr_variable_string_read - (const struct sieve_runtime_env *renv, sieve_size_t *address, string_t **str); -static bool opr_variable_string_dump - (const struct sieve_dumptime_env *denv, sieve_size_t *address); - -const struct sieve_opr_string_interface variable_string_interface = { - opr_variable_string_dump, - opr_variable_string_read -}; - -const struct sieve_operand variable_string_operand = { - "variable-string", - &variables_extension, - EXT_VARIABLES_OPERAND_VARIABLE_STRING, - &string_class, - &variable_string_interface -}; - -void ext_variables_opr_variable_string_emit - (struct sieve_binary *sbin, unsigned int elements) -{ - (void) sieve_operand_emit_code - (sbin, &variable_string_operand, ext_variables_my_id); - (void) sieve_binary_emit_integer(sbin, elements); -} - -static bool opr_variable_string_dump - (const struct sieve_dumptime_env *denv, sieve_size_t *address) -{ - sieve_size_t elements = 0; - unsigned int i; - - if (!sieve_binary_read_integer(denv->sbin, address, &elements) ) - return FALSE; - - sieve_code_dumpf(denv, "VARSTR [%ld]:", (long) elements); - - sieve_code_descend(denv); - for ( i = 0; i < (unsigned int) elements; i++ ) { - sieve_opr_string_dump(denv, address); - } - sieve_code_ascend(denv); - - return TRUE; -} - -static bool opr_variable_string_read - (const struct sieve_runtime_env *renv, sieve_size_t *address, string_t **str) -{ - sieve_size_t elements = 0; - unsigned int i; - - if ( !sieve_binary_read_integer(renv->sbin, address, &elements) ) - return FALSE; - - /* Parameter str can be NULL if we are requested to only skip and not - * actually read the argument. - */ - if ( str == NULL ) { - for ( i = 0; i < (unsigned int) elements; i++ ) { - if ( !sieve_opr_string_read(renv, address, NULL) ) - return FALSE; - } - } else { - *str = t_str_new(128); - for ( i = 0; i < (unsigned int) elements; i++ ) { - string_t *strelm; - - if ( !sieve_opr_string_read(renv, address, &strelm) ) - return FALSE; - - str_append_str(*str, strelm); - } - } - - return TRUE; -} - /* Set modifier registration */ const struct ext_variables_set_modifier *ext_variables_set_modifier_find diff --git a/src/lib-sieve/plugins/variables/ext-variables-operands.c b/src/lib-sieve/plugins/variables/ext-variables-operands.c new file mode 100644 index 000000000..efdf06def --- /dev/null +++ b/src/lib-sieve/plugins/variables/ext-variables-operands.c @@ -0,0 +1,254 @@ +#include "lib.h" +#include "hash.h" +#include "str.h" +#include "array.h" + +#include "sieve-common.h" + +#include "sieve-ast.h" +#include "sieve-binary.h" +#include "sieve-code.h" +#include "sieve-match-types.h" + +#include "sieve-commands.h" +#include "sieve-validator.h" +#include "sieve-generator.h" +#include "sieve-code-dumper.h" +#include "sieve-interpreter.h" + +#include "ext-variables-common.h" +#include "ext-variables-name.h" + +/* + * Variable operand + */ + +static bool opr_variable_read_value + (const struct sieve_runtime_env *renv, sieve_size_t *address, string_t **str); +static bool opr_variable_dump + (const struct sieve_dumptime_env *denv, sieve_size_t *address); + +const struct sieve_opr_string_interface variable_interface = { + opr_variable_dump, + opr_variable_read_value +}; + +const struct sieve_operand variable_operand = { + "variable", + &variables_extension, + EXT_VARIABLES_OPERAND_VARIABLE, + &string_class, + &variable_interface +}; + +void ext_variables_opr_variable_emit + (struct sieve_binary *sbin, struct sieve_variable *var) +{ + (void) sieve_operand_emit_code(sbin, &variable_operand, ext_variables_my_id); + (void) sieve_binary_emit_integer(sbin, var->index); +} + +static bool opr_variable_dump + (const struct sieve_dumptime_env *denv, sieve_size_t *address) +{ + sieve_size_t index = 0; + + if (sieve_binary_read_integer(denv->sbin, address, &index) ) { + sieve_code_dumpf(denv, "VAR: %ld", (long) index); + + return TRUE; + } + + return FALSE; +} + +static bool opr_variable_read_value + (const struct sieve_runtime_env *renv, sieve_size_t *address, string_t **str) +{ + struct sieve_variable_storage *storage; + sieve_size_t index = 0; + + storage = ext_variables_interpreter_get_storage(renv->interp); + if ( storage == NULL ) return FALSE; + + if (sieve_binary_read_integer(renv->sbin, address, &index) ) { + /* Parameter str can be NULL if we are requested to only skip and not + * actually read the argument. + */ + if ( str != NULL ) { + sieve_variable_get(storage, index, str); + + if ( *str == NULL ) *str = t_str_new(0); + } + return TRUE; + } + + return FALSE; +} + +bool ext_variables_opr_variable_read + (const struct sieve_runtime_env *renv, sieve_size_t *address, + struct sieve_variable_storage **storage, unsigned int *var_index) +{ + const struct sieve_operand *operand = sieve_operand_read(renv->sbin, address); + sieve_size_t idx = 0; + + if ( operand != &variable_operand ) + return FALSE; + + *storage = ext_variables_interpreter_get_storage(renv->interp); + if ( *storage == NULL ) return FALSE; + + if (sieve_binary_read_integer(renv->sbin, address, &idx) ) { + *var_index = idx; + return TRUE; + } + + return FALSE; +} + +/* + * Match value operand + */ + +static bool opr_match_value_read + (const struct sieve_runtime_env *renv, sieve_size_t *address, string_t **str); +static bool opr_match_value_dump + (const struct sieve_dumptime_env *denv, sieve_size_t *address); + +const struct sieve_opr_string_interface match_value_interface = { + opr_match_value_dump, + opr_match_value_read +}; + +const struct sieve_operand match_value_operand = { + "match-value", + &variables_extension, + EXT_VARIABLES_OPERAND_MATCH_VALUE, + &string_class, + &match_value_interface +}; + +void ext_variables_opr_match_value_emit + (struct sieve_binary *sbin, unsigned int index) +{ + (void) sieve_operand_emit_code + (sbin, &match_value_operand, ext_variables_my_id); + (void) sieve_binary_emit_integer(sbin, index); +} + +static bool opr_match_value_dump + (const struct sieve_dumptime_env *denv, sieve_size_t *address) +{ + sieve_size_t index = 0; + + if (sieve_binary_read_integer(denv->sbin, address, &index) ) { + sieve_code_dumpf(denv, "MVALUE: %ld", (long) index); + + return TRUE; + } + + return FALSE; +} + +static bool opr_match_value_read + (const struct sieve_runtime_env *renv, sieve_size_t *address, string_t **str) +{ + sieve_size_t index = 0; + + if (sieve_binary_read_integer(renv->sbin, address, &index) ) { + /* Parameter str can be NULL if we are requested to only skip and not + * actually read the argument. + */ + if ( str != NULL ) { + sieve_match_values_get(renv->interp, (unsigned int) index, str); + + if ( *str == NULL ) *str = t_str_new(0); + } + return TRUE; + } + + return FALSE; +} + +/* + * Variable string operand + */ + +static bool opr_variable_string_read + (const struct sieve_runtime_env *renv, sieve_size_t *address, string_t **str); +static bool opr_variable_string_dump + (const struct sieve_dumptime_env *denv, sieve_size_t *address); + +const struct sieve_opr_string_interface variable_string_interface = { + opr_variable_string_dump, + opr_variable_string_read +}; + +const struct sieve_operand variable_string_operand = { + "variable-string", + &variables_extension, + EXT_VARIABLES_OPERAND_VARIABLE_STRING, + &string_class, + &variable_string_interface +}; + +void ext_variables_opr_variable_string_emit + (struct sieve_binary *sbin, unsigned int elements) +{ + (void) sieve_operand_emit_code + (sbin, &variable_string_operand, ext_variables_my_id); + (void) sieve_binary_emit_integer(sbin, elements); +} + +static bool opr_variable_string_dump + (const struct sieve_dumptime_env *denv, sieve_size_t *address) +{ + sieve_size_t elements = 0; + unsigned int i; + + if (!sieve_binary_read_integer(denv->sbin, address, &elements) ) + return FALSE; + + sieve_code_dumpf(denv, "VARSTR [%ld]:", (long) elements); + + sieve_code_descend(denv); + for ( i = 0; i < (unsigned int) elements; i++ ) { + sieve_opr_string_dump(denv, address); + } + sieve_code_ascend(denv); + + return TRUE; +} + +static bool opr_variable_string_read + (const struct sieve_runtime_env *renv, sieve_size_t *address, string_t **str) +{ + sieve_size_t elements = 0; + unsigned int i; + + if ( !sieve_binary_read_integer(renv->sbin, address, &elements) ) + return FALSE; + + /* Parameter str can be NULL if we are requested to only skip and not + * actually read the argument. + */ + if ( str == NULL ) { + for ( i = 0; i < (unsigned int) elements; i++ ) { + if ( !sieve_opr_string_read(renv, address, NULL) ) + return FALSE; + } + } else { + *str = t_str_new(128); + for ( i = 0; i < (unsigned int) elements; i++ ) { + string_t *strelm; + + if ( !sieve_opr_string_read(renv, address, &strelm) ) + return FALSE; + + str_append_str(*str, strelm); + } + } + + return TRUE; +} diff --git a/src/lib-sieve/plugins/variables/ext-variables-operands.h b/src/lib-sieve/plugins/variables/ext-variables-operands.h new file mode 100644 index 000000000..210ce2dfb --- /dev/null +++ b/src/lib-sieve/plugins/variables/ext-variables-operands.h @@ -0,0 +1,43 @@ +#ifndef __EXT_VARIABLES_OPERANDS_H +#define __EXT_VARIABLES_OPERANDS_H + +#include "lib.h" +#include "hash.h" +#include "str.h" +#include "array.h" + +#include "sieve-common.h" +#include "ext-variables-common.h" + +/* + * Variable operand + */ + +extern const struct sieve_operand variable_operand; + +void ext_variables_opr_variable_emit + (struct sieve_binary *sbin, struct sieve_variable *var); +bool ext_variables_opr_variable_read + (const struct sieve_runtime_env *renv, sieve_size_t *address, + struct sieve_variable_storage **storage, unsigned int *var_index); + +/* + * Match value operand + */ + +extern const struct sieve_operand match_value_operand; + +void ext_variables_opr_match_value_emit + (struct sieve_binary *sbin, unsigned int index); + +/* + * Variable string operand + */ + +extern const struct sieve_operand variable_string_operand; + +void ext_variables_opr_variable_string_emit + (struct sieve_binary *sbin, unsigned int elements); + +#endif /* __EXT_VARIABLES_OPERANDS_H */ + diff --git a/src/lib-sieve/plugins/variables/ext-variables.c b/src/lib-sieve/plugins/variables/ext-variables.c index aa2307803..e66c9bc0e 100644 --- a/src/lib-sieve/plugins/variables/ext-variables.c +++ b/src/lib-sieve/plugins/variables/ext-variables.c @@ -27,6 +27,7 @@ #include "ext-variables-common.h" #include "ext-variables-arguments.h" +#include "ext-variables-operands.h" #include <ctype.h> -- GitLab