1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
*/
#include "lib.h"
#include "sieve-common.h"
#include "sieve-code.h"
#include "sieve-commands.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-binary.h"
#include "sieve-interpreter.h"
#include "sieve-dump.h"
#include "sieve-ext-variables.h"
#include "ext-include-common.h"
#include "ext-include-binary.h"
#include "ext-include-variables.h"
/*
* Commands
*/
static bool cmd_global_validate
(struct sieve_validator *valdtr, struct sieve_command *cmd);
static bool cmd_global_generate
(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd);
const struct sieve_command_def cmd_global = {
"global",
SCT_COMMAND,
1, 0, FALSE, FALSE,
NULL, NULL,
cmd_global_validate,
NULL,
cmd_global_generate,
NULL
};
/* DEPRICATED:
*/
/* Import command
*
* Syntax
* import
*/
const struct sieve_command_def cmd_import = {
"import",
SCT_COMMAND,
1, 0, FALSE, FALSE,
NULL, NULL,
cmd_global_validate,
NULL,
cmd_global_generate,
NULL
};
/* Export command
*
* Syntax
* export
*/
const struct sieve_command_def cmd_export = {
"export",
SCT_COMMAND,
1, 0, FALSE, FALSE,
NULL, NULL,
cmd_global_validate,
NULL,
cmd_global_generate,
NULL
};
/*
* Operations
*/
static bool opc_global_dump
(const struct sieve_dumptime_env *denv, sieve_size_t *address);
static int opc_global_execute
(const struct sieve_runtime_env *renv, sieve_size_t *address);
/* Global operation */
const struct sieve_operation_def global_operation = {
"global",
&include_extension,
EXT_INCLUDE_OPERATION_GLOBAL,
opc_global_dump,
opc_global_execute
};
/*
* Validation
*/
static inline struct sieve_argument *_create_variable_argument
(struct sieve_command *cmd, struct sieve_variable *var)
{
struct sieve_argument *argument = sieve_argument_create
(cmd->ast_node->ast, NULL, cmd->ext, 0);
argument->data = (void *) var;
return argument;
}
static bool cmd_global_validate
(struct sieve_validator *valdtr, struct sieve_command *cmd)
{
const struct sieve_extension *this_ext = cmd->ext;
struct sieve_ast_argument *arg = cmd->first_positional;
struct sieve_command *prev = sieve_command_prev(cmd);
/* DEPRECATED: Check valid command placement */
if ( !sieve_command_is(cmd, cmd_global) ) {
if ( !sieve_command_is_toplevel(cmd) ||
( !sieve_command_is_first(cmd) && prev != NULL &&
!sieve_command_is(prev, cmd_require) &&
!sieve_command_is(prev, cmd_import) &&
!sieve_command_is(prev, cmd_export) ) ) {
sieve_command_validate_error(valdtr, cmd,
"the DEPRECATED %s command can only be placed at top level "
"at the beginning of the file after any require or "
"import/export commands",
sieve_command_identifier(cmd));
return FALSE;
}
}
/* Check for use of variables extension */
if ( !ext_include_validator_have_variables(this_ext, valdtr) ) {
sieve_command_validate_error(valdtr, cmd,
"%s command requires that variables extension is active",
sieve_command_identifier(cmd));
return FALSE;
}
/* Register global variable */
if ( sieve_ast_argument_type(arg) == SAAT_STRING ) {
/* Single string */
const char *identifier = sieve_ast_argument_strc(arg);
struct sieve_variable *var;
if ( (var=ext_include_variable_import_global
(valdtr, cmd, identifier)) == NULL )
return FALSE;
arg->argument = _create_variable_argument(cmd, var);
} else if ( sieve_ast_argument_type(arg) == SAAT_STRING_LIST ) {
/* String list */
struct sieve_ast_argument *stritem = sieve_ast_strlist_first(arg);
while ( stritem != NULL ) {
const char *identifier = sieve_ast_argument_strc(stritem);
struct sieve_variable *var;
if ( (var=ext_include_variable_import_global
(valdtr, cmd, identifier)) == NULL )
return FALSE;
stritem->argument = _create_variable_argument(cmd, var);
stritem = sieve_ast_strlist_next(stritem);
}
} else {
/* Something else */
sieve_argument_validate_error(valdtr, arg,
"the %s command accepts a single string or string list argument, "
"but %s was found", sieve_command_identifier(cmd),
sieve_ast_argument_name(arg));
return FALSE;
}
/* Join global commands with predecessors if possible */
if ( sieve_commands_equal(prev, cmd) ) {
/* Join this command's string list with the previous one */
prev->first_positional = sieve_ast_stringlist_join
(prev->first_positional, cmd->first_positional);
if ( prev->first_positional == NULL ) {
/* Not going to happen unless MAXINT stringlist items are specified */
sieve_command_validate_error(valdtr, cmd,
"compiler reached AST limit (script too complex)");
return FALSE;
}
/* Detach this command node */
sieve_ast_node_detach(cmd->ast_node);
}
return TRUE;
}
/*
* Code generation
*/
static bool cmd_global_generate
(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd)
{
struct sieve_ast_argument *arg = cmd->first_positional;
sieve_operation_emit(cgenv->sblock, cmd->ext, &global_operation);
if ( sieve_ast_argument_type(arg) == SAAT_STRING ) {
/* Single string */
struct sieve_variable *var = (struct sieve_variable *) arg->argument->data;
(void)sieve_binary_emit_unsigned(cgenv->sblock, 1);
(void)sieve_binary_emit_unsigned(cgenv->sblock, var->index);
} else if ( sieve_ast_argument_type(arg) == SAAT_STRING_LIST ) {
/* String list */
struct sieve_ast_argument *stritem = sieve_ast_strlist_first(arg);
(void)sieve_binary_emit_unsigned
(cgenv->sblock, sieve_ast_strlist_count(arg));
while ( stritem != NULL ) {
struct sieve_variable *var =
(struct sieve_variable *) stritem->argument->data;
(void)sieve_binary_emit_unsigned(cgenv->sblock, var->index);
stritem = sieve_ast_strlist_next(stritem);
}
} else {
i_unreached();
}
return TRUE;
}
/*
* Code dump
*/
static bool opc_global_dump
(const struct sieve_dumptime_env *denv, sieve_size_t *address)
{
const struct sieve_extension *this_ext = denv->oprtn->ext;
unsigned int count, i, var_count;
struct sieve_variable_scope_binary *global_vars;
struct sieve_variable_scope *global_scope;
struct sieve_variable * const *vars;
if ( !sieve_binary_read_unsigned(denv->sblock, address, &count) )
return FALSE;
sieve_code_dumpf(denv, "GLOBAL (count: %u):", count);
global_vars = ext_include_binary_get_global_scope(this_ext, denv->sbin);
global_scope = sieve_variable_scope_binary_get(global_vars);
vars = sieve_variable_scope_get_variables(global_scope, &var_count);
sieve_code_descend(denv);
for ( i = 0; i < count; i++ ) {
unsigned int index;
sieve_code_mark(denv);
if ( !sieve_binary_read_unsigned(denv->sblock, address, &index) ||
index >= var_count )
return FALSE;
sieve_code_dumpf(denv, "%d: VAR[%d]: '%s'", i, index, vars[index]->identifier);
}
return TRUE;
}
/*
* Execution
*/
static int opc_global_execute
(const struct sieve_runtime_env *renv, sieve_size_t *address)
{
const struct sieve_extension *this_ext = renv->oprtn->ext;
struct sieve_variable_scope_binary *global_vars;
struct sieve_variable_scope *global_scope;
struct sieve_variable_storage *storage;
struct sieve_variable * const *vars;
unsigned int var_count, count, i;
if ( !sieve_binary_read_unsigned(renv->sblock, address, &count) ) {
sieve_runtime_trace_error(renv,
"global: count operand invalid");
return SIEVE_EXEC_BIN_CORRUPT;
}
global_vars = ext_include_binary_get_global_scope(this_ext, renv->sbin);
global_scope = sieve_variable_scope_binary_get(global_vars);
vars = sieve_variable_scope_get_variables(global_scope, &var_count);
storage = ext_include_interpreter_get_global_variables
(this_ext, renv->interp);
for ( i = 0; i < count; i++ ) {
unsigned int index;
if ( !sieve_binary_read_unsigned(renv->sblock, address, &index) ) {
sieve_runtime_trace_error(renv,
"global: variable index operand invalid");
return SIEVE_EXEC_BIN_CORRUPT;
}
if ( index >= var_count ) {
sieve_runtime_trace_error(renv,
"global: variable index %u is invalid in global storage (> %u)",
index, var_count);
return SIEVE_EXEC_BIN_CORRUPT;
}
sieve_runtime_trace(renv, SIEVE_TRLVL_COMMANDS,
"global: exporting variable '%s' [gvid: %u, vid: %u]",
vars[index]->identifier, i, index);
/* Make sure variable is initialized (export) */
(void)sieve_variable_get_modifiable(storage, index, NULL);
}
return SIEVE_EXEC_OK;
}
|