From d728ffb3485d2f9574cb943ce2ecce0567bccb14 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Mon, 24 Mar 2008 23:12:00 +0100
Subject: [PATCH] Include: moved variables support to separate file.

---
 src/lib-sieve/plugins/include/Makefile.am     |  4 +-
 src/lib-sieve/plugins/include/cmd-export.c    |  5 +-
 src/lib-sieve/plugins/include/cmd-import.c    |  5 +-
 .../plugins/include/ext-include-binary.h      |  2 +-
 .../plugins/include/ext-include-common.c      | 88 +----------------
 .../plugins/include/ext-include-common.h      | 11 ---
 .../plugins/include/ext-include-variables.c   | 98 +++++++++++++++++++
 .../plugins/include/ext-include-variables.h   | 24 +++++
 .../plugins/include/include-variables1.sieve  |  2 +
 .../plugins/include/include-variables2.sieve  |  3 +
 10 files changed, 142 insertions(+), 100 deletions(-)
 create mode 100644 src/lib-sieve/plugins/include/ext-include-variables.c
 create mode 100644 src/lib-sieve/plugins/include/ext-include-variables.h

diff --git a/src/lib-sieve/plugins/include/Makefile.am b/src/lib-sieve/plugins/include/Makefile.am
index 585fac85b..4a3e840ab 100644
--- a/src/lib-sieve/plugins/include/Makefile.am
+++ b/src/lib-sieve/plugins/include/Makefile.am
@@ -18,8 +18,10 @@ libsieve_ext_include_la_SOURCES = \
 	$(cmds) \
 	ext-include-common.c \
 	ext-include-binary.c \
+	ext-include-variables.c \
 	ext-include.c
 
 noinst_HEADERS = \
 	ext-include-common.h \
-	ext-include-binary.h
+	ext-include-binary.h \
+	ext-include-variables.h
diff --git a/src/lib-sieve/plugins/include/cmd-export.c b/src/lib-sieve/plugins/include/cmd-export.c
index 94d450ffd..38a5794a6 100644
--- a/src/lib-sieve/plugins/include/cmd-export.c
+++ b/src/lib-sieve/plugins/include/cmd-export.c
@@ -9,6 +9,7 @@
 #include "sieve-ext-variables.h"
 
 #include "ext-include-common.h"
+#include "ext-include-variables.h"
 
 /* Forward declarations */
 
@@ -64,7 +65,7 @@ static bool cmd_export_validate
 		/* Single string */
 		const char *variable = sieve_ast_argument_strc(arg);
 		
-		if ( !ext_include_export_variable(arg->ast, variable) ) {
+		if ( !ext_include_variable_export(arg->ast, variable) ) {
 			sieve_command_validate_error(validator, cmd, 
 				"cannot export imported variable '%s'", variable);
 			return FALSE;
@@ -77,7 +78,7 @@ static bool cmd_export_validate
 		while ( stritem != NULL ) {
 			const char *variable = sieve_ast_argument_strc(stritem);
 			
-			if ( !ext_include_export_variable(arg->ast, variable) ) {
+			if ( !ext_include_variable_export(arg->ast, variable) ) {
 				sieve_command_validate_error(validator, cmd, 
 					"cannot export imported variable '%s'", variable);
 				return FALSE;
diff --git a/src/lib-sieve/plugins/include/cmd-import.c b/src/lib-sieve/plugins/include/cmd-import.c
index ad0102dd9..c8cf0213e 100644
--- a/src/lib-sieve/plugins/include/cmd-import.c
+++ b/src/lib-sieve/plugins/include/cmd-import.c
@@ -9,6 +9,7 @@
 #include "sieve-ext-variables.h"
 
 #include "ext-include-common.h"
+#include "ext-include-variables.h"
 
 /* Forward declarations */
 
@@ -63,7 +64,7 @@ static bool cmd_import_validate
 		/* Single string */
 		const char *variable = sieve_ast_argument_strc(arg);
 		
-		ext_include_import_variable(arg->ast, variable);
+		ext_include_variable_import(arg->ast, variable);
 
 	} else if ( sieve_ast_argument_type(arg) == SAAT_STRING_LIST ) {
 		/* String list */
@@ -71,7 +72,7 @@ static bool cmd_import_validate
 		
 		while ( stritem != NULL ) {
 			const char *variable = sieve_ast_argument_strc(stritem);
-			ext_include_import_variable(arg->ast, variable);
+			ext_include_variable_import(arg->ast, variable);
 	
 			stritem = sieve_ast_strlist_next(stritem);
 		}
diff --git a/src/lib-sieve/plugins/include/ext-include-binary.h b/src/lib-sieve/plugins/include/ext-include-binary.h
index 0a2cfbc26..7283cfce5 100644
--- a/src/lib-sieve/plugins/include/ext-include-binary.h
+++ b/src/lib-sieve/plugins/include/ext-include-binary.h
@@ -14,5 +14,5 @@ bool ext_include_binary_script_is_included
 	(struct ext_include_binary_context *binctx, struct sieve_script *script,
 		unsigned int *block_id);
 		
-#endif
+#endif /* __EXT_INCLUDE_BINARY_H */
 
diff --git a/src/lib-sieve/plugins/include/ext-include-common.c b/src/lib-sieve/plugins/include/ext-include-common.c
index 93c67d62b..5b13aa91a 100644
--- a/src/lib-sieve/plugins/include/ext-include-common.c
+++ b/src/lib-sieve/plugins/include/ext-include-common.c
@@ -6,28 +6,21 @@
 #include "sieve-commands.h"
 #include "sieve-generator.h"
 #include "sieve-interpreter.h"
-#include "sieve-ext-variables.h"
 
 #include "ext-include-common.h"
 #include "ext-include-binary.h"
+#include "ext-include-variables.h"
 
 /*
  * Forward declarations
  */
  
-/* AST context */
-
-struct ext_include_ast_context {
-	struct sieve_variable_scope *import_vars;
-	struct sieve_variable_scope *export_vars;
-};
-
 /* Generator context */
 
 struct ext_include_generator_context {
 	unsigned int nesting_level;
 	struct sieve_script *script;
-	struct ext_include_main_context *main;
+	struct ext_include_variables_scope *var_scope;
 	struct ext_include_generator_context *parent;
 };
 
@@ -71,81 +64,10 @@ const char *ext_include_get_script_path
 	return NULL;
 }
 
-/* 
- * Main context management 
- */
-
-static struct ext_include_main_context *ext_include_create_main_context
-(struct sieve_generator *gentr)
-{
-	pool_t pool = sieve_generator_pool(gentr);
-	
-	struct ext_include_main_context *ctx = 
-		p_new(pool, struct ext_include_main_context, 1);
-	
-	ctx->generator = gentr;
-	
-	return ctx;
-}
-
-/* 
- * AST context management
- */
-
-static struct ext_include_ast_context *ext_include_create_ast_context
-(struct sieve_ast *ast)
-{	
-	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);
-	
-	return ctx;
-}
-
-static inline struct ext_include_ast_context *
-	ext_include_get_ast_context(struct sieve_ast *ast)
-{
-	struct ext_include_ast_context *ctx = (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);
-	}
-	
-	return ctx;
-}
-
-/* 
- * Variable import-export
- */
- 
-void ext_include_import_variable(struct sieve_ast *ast, const char *variable)
-{
-	struct ext_include_ast_context *ctx = ext_include_get_ast_context(ast);
-	
-	(void)sieve_variable_scope_declare(ctx->import_vars, variable);
-}
-
-bool ext_include_export_variable(struct sieve_ast *ast, const char *variable)
-{
-	struct ext_include_ast_context *ctx = ext_include_get_ast_context(ast);
-	
-	if ( sieve_variable_scope_get_variable(ctx->import_vars, variable, FALSE) 
-		!= NULL )
-		return FALSE;
-		
-	(void)sieve_variable_scope_declare(ctx->export_vars, variable);
-	return TRUE;	
-}
-
 /* 
  * Generator context management 
  */
-
+ 
 static struct ext_include_generator_context *
 	ext_include_create_generator_context
 (struct sieve_generator *gentr, struct ext_include_generator_context *parent, 
@@ -159,10 +81,10 @@ static struct ext_include_generator_context *
 	ctx->script = script;
 	if ( parent == NULL ) {
 		ctx->nesting_level = 0;
-		ctx->main = ext_include_create_main_context(gentr);
+		ctx->var_scope = ext_include_variables_scope_create(pool);
 	} else {
 		ctx->nesting_level = parent->nesting_level + 1;
-		ctx->main = parent->main;
+		ctx->var_scope = parent->var_scope;
 	}
 	
 	return ctx;
diff --git a/src/lib-sieve/plugins/include/ext-include-common.h b/src/lib-sieve/plugins/include/ext-include-common.h
index ff238ce69..ffd05a9e5 100644
--- a/src/lib-sieve/plugins/include/ext-include-common.h
+++ b/src/lib-sieve/plugins/include/ext-include-common.h
@@ -43,12 +43,6 @@ enum ext_include_script_location {
 const char *ext_include_get_script_path
 	(enum ext_include_script_location location, const char *script_name);
 
-/* Main context, currently not used for anything and might be removed */
-
-struct ext_include_main_context {
-	struct sieve_generator *generator;
-};
-
 /* Generator */
 
 void ext_include_register_generator_context
@@ -67,9 +61,4 @@ bool ext_include_execute_include
 	(const struct sieve_runtime_env *renv, unsigned int block_id);
 void ext_include_execute_return(const struct sieve_runtime_env *renv);
 
-/* Variables import-export */
-
-void ext_include_import_variable(struct sieve_ast *ast, const char *variable);
-bool ext_include_export_variable(struct sieve_ast *ast, const char *variable);
-
 #endif /* __EXT_INCLUDE_COMMON_H */
diff --git a/src/lib-sieve/plugins/include/ext-include-variables.c b/src/lib-sieve/plugins/include/ext-include-variables.c
new file mode 100644
index 000000000..bca4b942e
--- /dev/null
+++ b/src/lib-sieve/plugins/include/ext-include-variables.c
@@ -0,0 +1,98 @@
+#include "sieve-common.h"
+#include "sieve-error.h"
+#include "sieve-ast.h"
+#include "sieve-generator.h"
+#include "sieve-interpreter.h"
+#include "sieve-ext-variables.h"
+
+#include "ext-include-common.h"
+#include "ext-include-variables.h"
+
+/*
+ * 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 
+ */
+
+struct ext_include_ast_context {
+	struct sieve_variable_scope *import_vars;
+	struct sieve_variable_scope *export_vars;
+};
+
+static struct ext_include_ast_context *ext_include_create_ast_context
+(struct sieve_ast *ast)
+{	
+	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);
+	
+	return ctx;
+}
+
+static inline struct ext_include_ast_context *
+	ext_include_get_ast_context(struct sieve_ast *ast)
+{
+	struct ext_include_ast_context *ctx = (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);
+	}
+	
+	return ctx;
+}
+
+/* 
+ * Variable import-export
+ */
+ 
+void ext_include_variable_import(struct sieve_ast *ast, const char *variable)
+{
+	struct ext_include_ast_context *ctx = ext_include_get_ast_context(ast);
+	
+	printf("VAR IMPORT: %s\n", variable);
+	
+	(void)sieve_variable_scope_declare(ctx->import_vars, variable);
+}
+
+bool ext_include_variable_export(struct sieve_ast *ast, const char *variable)
+{
+	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 )
+		return FALSE;
+		
+	(void)sieve_variable_scope_declare(ctx->export_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
new file mode 100644
index 000000000..99c6852a7
--- /dev/null
+++ b/src/lib-sieve/plugins/include/ext-include-variables.h
@@ -0,0 +1,24 @@
+#ifndef __EXT_INCLUDE_VARIABLES_H
+#define __EXT_INCLUDE_VARIABLES_H
+
+#include "sieve-common.h"
+#include "ext-include-common.h"
+
+/*
+ * Global variables scope
+ */
+ 
+struct ext_include_variables_scope;
+ 
+struct ext_include_variables_scope *ext_include_variables_scope_create
+	(pool_t pool);
+
+/* 
+ * 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);
+
+#endif /* __EXT_INCLUDE_VARIABLES_H */
+
diff --git a/src/lib-sieve/plugins/include/include-variables1.sieve b/src/lib-sieve/plugins/include/include-variables1.sieve
index 6bb7436bc..ec4cd8885 100644
--- a/src/lib-sieve/plugins/include/include-variables1.sieve
+++ b/src/lib-sieve/plugins/include/include-variables1.sieve
@@ -2,4 +2,6 @@ require "include";
 require "variables";
 require "fileinto";
 
+import ["value1", "value2"];
+
 fileinto "${value1} ${value2}";
diff --git a/src/lib-sieve/plugins/include/include-variables2.sieve b/src/lib-sieve/plugins/include/include-variables2.sieve
index 1c97235a6..1736dbd78 100644
--- a/src/lib-sieve/plugins/include/include-variables2.sieve
+++ b/src/lib-sieve/plugins/include/include-variables2.sieve
@@ -2,4 +2,7 @@ require "include";
 require "variables";
 require "fileinto";
 
+import "value3";
+import "value4";
+
 fileinto "${value3} ${value4}";
-- 
GitLab