File: cmd-if.c

package info (click to toggle)
dovecot 1:2.2.13-12~deb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 38,792 kB
  • sloc: ansic: 341,472; sh: 16,920; makefile: 5,393; cpp: 1,474; perl: 265; xml: 44; python: 34; pascal: 27
file content (272 lines) | stat: -rw-r--r-- 6,477 bytes parent folder | download | duplicates (3)
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
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#include "sieve-common.h"
#include "sieve-commands.h"
#include "sieve-validator.h"
#include "sieve-generator.h"
#include "sieve-code.h"
#include "sieve-binary.h"

/*
 * Commands
 */

static bool cmd_if_validate
	(struct sieve_validator *valdtr, struct sieve_command *cmd);
static bool cmd_elsif_validate
	(struct sieve_validator *valdtr, struct sieve_command *cmd);
static bool cmd_if_validate_const
	(struct sieve_validator *valdtr, struct sieve_command *cmd,
		int *const_current, int const_next);
static bool cmd_if_generate
	(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd);
static bool cmd_else_generate
	(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd);

/* If command
 *
 * Syntax:
 *   if <test1: test> <block1: block>
 */

const struct sieve_command_def cmd_if = {
	"if",
	SCT_COMMAND,
	0, 1, TRUE, TRUE,
	NULL, NULL,
	cmd_if_validate,
	cmd_if_validate_const,
	cmd_if_generate,
	NULL
};

/* ElsIf command
 *
 * Santax:
 *   elsif <test2: test> <block2: block>
 */

const struct sieve_command_def cmd_elsif = {
    "elsif",
	SCT_COMMAND,
	0, 1, TRUE, TRUE,
	NULL, NULL,
	cmd_elsif_validate,
	cmd_if_validate_const,
	cmd_if_generate,
	NULL
};

/* Else command
 *
 * Syntax:
 *   else <block>
 */


const struct sieve_command_def cmd_else = {
    "else",
	SCT_COMMAND,
	0, 0, TRUE, TRUE,
	NULL, NULL,
	cmd_elsif_validate,
	cmd_if_validate_const,
	cmd_else_generate,
	NULL
};

/*
 * Context management
 */

struct cmd_if_context_data {
	struct cmd_if_context_data *previous;
	struct cmd_if_context_data *next;

	int const_condition;

	bool jump_generated;
	sieve_size_t exit_jump;
};

static void cmd_if_initialize_context_data
(struct sieve_command *cmd, struct cmd_if_context_data *previous)
{
	struct cmd_if_context_data *cmd_data;

	/* Assign context */
	cmd_data = p_new(sieve_command_pool(cmd), struct cmd_if_context_data, 1);
	cmd_data->exit_jump = 0;
	cmd_data->jump_generated = FALSE;

	/* Update linked list of contexts */
	cmd_data->previous = previous;
	cmd_data->next = NULL;
	if ( previous != NULL )
		previous->next = cmd_data;

	/* Check const status */
	cmd_data->const_condition = -1;
	while ( previous != NULL ) {
		if ( previous->const_condition > 0 ) {
			cmd_data->const_condition = 0;
			break;
		}
		previous = previous->previous;
	}

	/* Assign to command context */
	cmd->data = cmd_data;
}

/*
 * Validation
 */

static bool cmd_if_validate
(struct sieve_validator *valdtr ATTR_UNUSED, struct sieve_command *cmd)
{
	/* Start if-command structure */
	cmd_if_initialize_context_data(cmd, NULL);

	return TRUE;
}

static bool cmd_elsif_validate
(struct sieve_validator *valdtr, struct sieve_command *cmd)
{
	struct sieve_command *prev = sieve_command_prev(cmd);

	/* Check valid command placement */
	if ( prev == NULL ||
		( !sieve_command_is(prev, cmd_if) && !sieve_command_is(prev, cmd_elsif) ) )
	{
		sieve_command_validate_error(valdtr, cmd,
			"the %s command must follow an if or elseif command",
			sieve_command_identifier(cmd));
		return FALSE;
	}

	/* Previous command in this block is 'if' or 'elsif', so we can safely refer
	 * to its context data
	 */
	cmd_if_initialize_context_data(cmd, prev->data);

	return TRUE;
}

static bool cmd_if_validate_const
(struct sieve_validator *valdtr ATTR_UNUSED, struct sieve_command *cmd,
	int *const_current, int const_next)
{
	struct cmd_if_context_data *cmd_data =
		(struct cmd_if_context_data *) cmd->data;

	if ( cmd_data != NULL ) {
		if ( cmd_data->const_condition == 0 ) {
			*const_current = cmd_data->const_condition;
			return FALSE;
		}

		cmd_data->const_condition = const_next;
	}

	*const_current = const_next;

	return ( const_next < 0 );
}

/*
 * Code generation
 */

/* The if command does not generate specific IF-ELSIF-ELSE opcodes, but only uses
 * JMP instructions. This is why the implementation of the if command does not
 * include an opcode implementation.
 */

static void cmd_if_resolve_exit_jumps
(struct sieve_binary_block *sblock, struct cmd_if_context_data *cmd_data)
{
	struct cmd_if_context_data *if_ctx = cmd_data->previous;

	/* Iterate backwards through all if-command contexts and resolve the
	 * exit jumps to the current code position.
	 */
	while ( if_ctx != NULL ) {
		if ( if_ctx->jump_generated )
			sieve_binary_resolve_offset(sblock, if_ctx->exit_jump);
		if_ctx = if_ctx->previous;
	}
}

static bool cmd_if_generate
(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd)
{
	struct sieve_binary_block *sblock = cgenv->sblock;
	struct cmd_if_context_data *cmd_data =
		(struct cmd_if_context_data *) cmd->data;
	struct sieve_ast_node *test;
	struct sieve_jumplist jmplist;

	/* Generate test condition */
	if ( cmd_data->const_condition < 0 ) {
		/* Prepare jumplist */
		sieve_jumplist_init_temp(&jmplist, sblock);

		test = sieve_ast_test_first(cmd->ast_node);
		if ( !sieve_generate_test(cgenv, test, &jmplist, FALSE) )
			return FALSE;
	}

	/* Case true { */
	if ( cmd_data->const_condition != 0 ) {
		if ( !sieve_generate_block(cgenv, cmd->ast_node) )
			return FALSE;
	}

	/* Are we the final command in this if-elsif-else structure? */
	if ( cmd_data->next == NULL || cmd_data->const_condition == 1 ) {
		/* Yes, Resolve previous exit jumps to this point */
		cmd_if_resolve_exit_jumps(sblock, cmd_data);

	} else if ( cmd_data->const_condition < 0 ) {
		/* No, generate jump to end of if-elsif-else structure (resolved later)
		 * This of course is not necessary if the {} block contains a command
		 * like stop at top level that unconditionally exits the block already
		 * anyway.
		 */
		if ( !sieve_command_block_exits_unconditionally(cmd) ) {
			sieve_operation_emit(sblock, NULL, &sieve_jmp_operation);
			cmd_data->exit_jump = sieve_binary_emit_offset(sblock, 0);
			cmd_data->jump_generated = TRUE;
		}
	}

	if ( cmd_data->const_condition < 0 ) {
		/* Case false ... (subsequent elsif/else commands might generate more) */
		sieve_jumplist_resolve(&jmplist);
	}

	return TRUE;
}

static bool cmd_else_generate
(const struct sieve_codegen_env *cgenv, struct sieve_command *cmd)
{
	struct cmd_if_context_data *cmd_data =
		(struct cmd_if_context_data *) cmd->data;

	/* Else { */
	if ( cmd_data->const_condition != 0 ) {
		if ( !sieve_generate_block(cgenv, cmd->ast_node) )
			return FALSE;

		/* } End: resolve all exit blocks */
		cmd_if_resolve_exit_jumps(cgenv->sblock, cmd_data);
	}

	return TRUE;
}