From c4612dceeebc4a86fea770a13580499c1a646c40 Mon Sep 17 00:00:00 2001 From: Stephan Bosch <stephan@rename-it.nl> Date: Sat, 5 Apr 2008 16:58:18 +0200 Subject: [PATCH] Include: implemented global variable scope. --- src/lib-sieve/plugins/include/cmd-export.c | 10 +-- src/lib-sieve/plugins/include/cmd-import.c | 7 +- .../plugins/include/ext-include-common.c | 6 +- .../plugins/include/ext-include-variables.c | 80 +++++++++++-------- .../plugins/include/ext-include-variables.h | 20 ++--- 5 files changed, 68 insertions(+), 55 deletions(-) diff --git a/src/lib-sieve/plugins/include/cmd-export.c b/src/lib-sieve/plugins/include/cmd-export.c index 38a5794a6..6865f8c1d 100644 --- a/src/lib-sieve/plugins/include/cmd-export.c +++ b/src/lib-sieve/plugins/include/cmd-export.c @@ -65,11 +65,8 @@ static bool cmd_export_validate /* Single string */ const char *variable = sieve_ast_argument_strc(arg); - if ( !ext_include_variable_export(arg->ast, variable) ) { - sieve_command_validate_error(validator, cmd, - "cannot export imported variable '%s'", variable); + if ( !ext_include_variable_export(validator, cmd, variable) ) return FALSE; - } } else if ( sieve_ast_argument_type(arg) == SAAT_STRING_LIST ) { /* String list */ @@ -78,11 +75,8 @@ static bool cmd_export_validate while ( stritem != NULL ) { const char *variable = sieve_ast_argument_strc(stritem); - if ( !ext_include_variable_export(arg->ast, variable) ) { - sieve_command_validate_error(validator, cmd, - "cannot export imported variable '%s'", variable); + if ( !ext_include_variable_export(validator, cmd, variable) ) return FALSE; - } stritem = sieve_ast_strlist_next(stritem); } diff --git a/src/lib-sieve/plugins/include/cmd-import.c b/src/lib-sieve/plugins/include/cmd-import.c index c8cf0213e..0e7c5823d 100644 --- a/src/lib-sieve/plugins/include/cmd-import.c +++ b/src/lib-sieve/plugins/include/cmd-import.c @@ -64,7 +64,8 @@ static bool cmd_import_validate /* Single string */ const char *variable = sieve_ast_argument_strc(arg); - ext_include_variable_import(arg->ast, variable); + if ( !ext_include_variable_import(validator, cmd, variable) ) + return FALSE; } else if ( sieve_ast_argument_type(arg) == SAAT_STRING_LIST ) { /* String list */ @@ -72,7 +73,9 @@ static bool cmd_import_validate while ( stritem != NULL ) { const char *variable = sieve_ast_argument_strc(stritem); - ext_include_variable_import(arg->ast, variable); + + if ( !ext_include_variable_import(validator, cmd, variable) ) + return FALSE; stritem = sieve_ast_strlist_next(stritem); } diff --git a/src/lib-sieve/plugins/include/ext-include-common.c b/src/lib-sieve/plugins/include/ext-include-common.c index 5b13aa91a..d54c8dff4 100644 --- a/src/lib-sieve/plugins/include/ext-include-common.c +++ b/src/lib-sieve/plugins/include/ext-include-common.c @@ -20,7 +20,6 @@ struct ext_include_generator_context { unsigned int nesting_level; struct sieve_script *script; - struct ext_include_variables_scope *var_scope; struct ext_include_generator_context *parent; }; @@ -81,10 +80,8 @@ static struct ext_include_generator_context * ctx->script = script; if ( parent == NULL ) { ctx->nesting_level = 0; - ctx->var_scope = ext_include_variables_scope_create(pool); } else { ctx->nesting_level = parent->nesting_level + 1; - ctx->var_scope = parent->var_scope; } return ctx; @@ -253,6 +250,9 @@ bool ext_include_generate_include "failed to parse included script '%s'", script_name); return FALSE; } + + /* Included scripts inherit global variable scope */ + (void)ext_include_create_ast_context(ast, cmd->ast_node->ast); /* Validate */ if ( !sieve_validate(ast, ehandler) ) { diff --git a/src/lib-sieve/plugins/include/ext-include-variables.c b/src/lib-sieve/plugins/include/ext-include-variables.c index bca4b942e..e01275950 100644 --- a/src/lib-sieve/plugins/include/ext-include-variables.c +++ b/src/lib-sieve/plugins/include/ext-include-variables.c @@ -1,6 +1,8 @@ #include "sieve-common.h" #include "sieve-error.h" #include "sieve-ast.h" +#include "sieve-commands.h" +#include "sieve-validator.h" #include "sieve-generator.h" #include "sieve-interpreter.h" #include "sieve-ext-variables.h" @@ -11,26 +13,7 @@ /* * Forward declarations */ - -/* - * Variables scope - */ -struct ext_include_variables_scope { - struct sieve_variable_scope *global_vars; -}; - -struct ext_include_variables_scope *ext_include_variables_scope_create -(pool_t pool) -{ - struct ext_include_variables_scope *scope = - p_new(pool, struct ext_include_variables_scope, 1); - - scope->global_vars = sieve_variable_scope_create(pool); - - return scope; -} - /* * AST context @@ -38,18 +21,26 @@ struct ext_include_variables_scope *ext_include_variables_scope_create struct ext_include_ast_context { struct sieve_variable_scope *import_vars; - struct sieve_variable_scope *export_vars; + struct sieve_variable_scope *global_vars; }; -static struct ext_include_ast_context *ext_include_create_ast_context -(struct sieve_ast *ast) +struct ext_include_ast_context *ext_include_create_ast_context +(struct sieve_ast *ast, struct sieve_ast *parent) { struct ext_include_ast_context *ctx; pool_t pool = sieve_ast_pool(ast); ctx = p_new(pool, struct ext_include_ast_context, 1); ctx->import_vars = sieve_variable_scope_create(pool); - ctx->export_vars = sieve_variable_scope_create(pool); + + if ( parent != NULL ) { + struct ext_include_ast_context *parent_ctx = + (struct ext_include_ast_context *) + sieve_ast_extension_get_context(parent, ext_include_my_id); + ctx->global_vars = ( parent_ctx == NULL ? NULL : parent_ctx->global_vars ); + } + + sieve_ast_extension_set_context(ast, ext_include_my_id, ctx); return ctx; } @@ -61,8 +52,7 @@ static inline struct ext_include_ast_context * sieve_ast_extension_get_context(ast, ext_include_my_id); if ( ctx == NULL ) { - ctx = ext_include_create_ast_context(ast); - sieve_ast_extension_set_context(ast, ext_include_my_id, ctx); + ctx = ext_include_create_ast_context(ast, NULL); } return ctx; @@ -72,26 +62,50 @@ static inline struct ext_include_ast_context * * Variable import-export */ -void ext_include_variable_import(struct sieve_ast *ast, const char *variable) +bool ext_include_variable_import +(struct sieve_validator *valdtr, struct sieve_command_context *cmd, + const char *variable) { - struct ext_include_ast_context *ctx = ext_include_get_ast_context(ast); + struct ext_include_ast_context *ctx = + ext_include_get_ast_context(cmd->ast_node->ast); - printf("VAR IMPORT: %s\n", variable); + if ( ctx->global_vars == NULL || + sieve_variable_scope_get_variable(ctx->global_vars, variable, FALSE) + == NULL ) + { + sieve_command_validate_error(valdtr, cmd, + "importing unknown global variable '%s'", variable); + return FALSE; + } + printf("VAR IMPORT: %s\n", variable); (void)sieve_variable_scope_declare(ctx->import_vars, variable); + + return TRUE; } -bool ext_include_variable_export(struct sieve_ast *ast, const char *variable) +bool ext_include_variable_export +(struct sieve_validator *valdtr, struct sieve_command_context *cmd, + const char *variable) { + struct sieve_ast *ast = cmd->ast_node->ast; struct ext_include_ast_context *ctx = ext_include_get_ast_context(ast); - printf("VAR EXPORT: %s\n", variable); - if ( sieve_variable_scope_get_variable(ctx->import_vars, variable, FALSE) - != NULL ) + != NULL ) { + sieve_command_validate_error(valdtr, cmd, + "cannot export imported variable '%s'", variable); return FALSE; + } + + if ( ctx->global_vars == NULL ) { + pool_t pool = sieve_ast_pool(ast); + ctx->global_vars = sieve_variable_scope_create(pool); + } + + printf("VAR EXPORT: %s\n", variable); - (void)sieve_variable_scope_declare(ctx->export_vars, variable); + (void)sieve_variable_scope_declare(ctx->global_vars, variable); return TRUE; } diff --git a/src/lib-sieve/plugins/include/ext-include-variables.h b/src/lib-sieve/plugins/include/ext-include-variables.h index 99c6852a7..0fefff051 100644 --- a/src/lib-sieve/plugins/include/ext-include-variables.h +++ b/src/lib-sieve/plugins/include/ext-include-variables.h @@ -4,21 +4,23 @@ #include "sieve-common.h" #include "ext-include-common.h" -/* - * Global variables scope +/* + * AST Context */ -struct ext_include_variables_scope; - -struct ext_include_variables_scope *ext_include_variables_scope_create - (pool_t pool); +struct ext_include_ast_context *ext_include_create_ast_context + (struct sieve_ast *ast, struct sieve_ast *parent); /* * Variable import-export */ -void ext_include_variable_import(struct sieve_ast *ast, const char *variable); -bool ext_include_variable_export(struct sieve_ast *ast, const char *variable); - +bool ext_include_variable_import + (struct sieve_validator *valdtr, struct sieve_command_context *cmd, + const char *variable); +bool ext_include_variable_export + (struct sieve_validator *valdtr, struct sieve_command_context *cmd, + const char *variable); + #endif /* __EXT_INCLUDE_VARIABLES_H */ -- GitLab