runtime.c
8.05 KB
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2005,
2007, 2008 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sieve.h>
#define SIEVE_ARG(m,n,t) ((m)->prog[(m)->pc+(n)].t)
#define SIEVE_ADJUST(m,n) (m)->pc+=(n)
#define INSTR_DEBUG(m) \
(((m)->debug_level & (MU_SIEVE_DEBUG_INSTR|MU_SIEVE_DEBUG_DISAS)) \
&& (m)->debug_printer)
#define INSTR_DISASS(m) ((m)->debug_level & MU_SIEVE_DEBUG_DISAS)
void
_mu_sv_instr_nop (mu_sieve_machine_t mach)
{
if (INSTR_DEBUG (mach))
mu_sieve_debug (mach, "%4lu: NOP\n",
(unsigned long) (mach->pc - 1));
}
void
_mu_sv_instr_source (mu_sieve_machine_t mach)
{
mach->locus.source_file = SIEVE_ARG (mach, 0, string);
if (INSTR_DEBUG (mach))
mu_sieve_debug (mach, "%4lu: SOURCE %s\n",
(unsigned long) (mach->pc - 1),
mach->locus.source_file);
SIEVE_ADJUST (mach, 1);
}
void
_mu_sv_instr_line (mu_sieve_machine_t mach)
{
mach->locus.source_line = SIEVE_ARG (mach, 0, line);
if (INSTR_DEBUG (mach))
mu_sieve_debug (mach, "%4lu: LINE %lu\n",
(unsigned long) (mach->pc - 1),
(unsigned long) mach->locus.source_line);
SIEVE_ADJUST (mach, 1);
}
static int
instr_run (mu_sieve_machine_t mach)
{
mu_sieve_handler_t han = SIEVE_ARG (mach, 0, handler);
mu_list_t arg_list = SIEVE_ARG (mach, 1, list);
mu_list_t tag_list = SIEVE_ARG (mach, 2, list);
int rc = 0;
SIEVE_ADJUST(mach, 4);
if (INSTR_DEBUG (mach))
{
mu_sieve_debug (mach, "Arguments: ");
sieve_print_value_list (arg_list, mach->debug_printer, mach->data);
mu_sieve_debug (mach, "\nTags:");
sieve_print_tag_list (tag_list, mach->debug_printer, mach->data);
mu_sieve_debug (mach, "\n");
}
if (!INSTR_DISASS(mach))
rc = han (mach, arg_list, tag_list);
return rc;
}
void
_mu_sv_instr_action (mu_sieve_machine_t mach)
{
mach->identifier = SIEVE_ARG (mach, 3, string);
if (INSTR_DEBUG (mach))
mu_sieve_debug (mach, "%4lu: ACTION: %s\n",
(unsigned long) (mach->pc - 1),
mach->identifier);
mach->action_count++;
instr_run (mach);
mach->identifier = NULL;
}
void
_mu_sv_instr_test (mu_sieve_machine_t mach)
{
mach->identifier = SIEVE_ARG (mach, 3, string);
if (INSTR_DEBUG (mach))
mu_sieve_debug (mach, "%4lu: TEST: %s\n",
(unsigned long) (mach->pc - 1),
mach->identifier);
mach->reg = instr_run (mach);
mach->identifier = NULL;
}
void
_mu_sv_instr_push (mu_sieve_machine_t mach)
{
if (INSTR_DEBUG (mach))
{
mu_sieve_debug (mach, "%4lu: PUSH\n", (unsigned long)(mach->pc - 1));
if (INSTR_DISASS (mach))
return;
}
if (!mach->stack && mu_list_create (&mach->stack))
{
mu_sieve_error (mach, _("cannot create stack"));
mu_sieve_abort (mach);
}
mu_list_prepend (mach->stack, (void*) mach->reg);
}
void
_mu_sv_instr_pop (mu_sieve_machine_t mach)
{
if (INSTR_DEBUG (mach))
{
mu_sieve_debug (mach, "%4lu: POP\n", (unsigned long)(mach->pc - 1));
if (INSTR_DISASS (mach))
return;
}
if (!mach->stack || mu_list_is_empty (mach->stack))
{
mu_sieve_error (mach, _("stack underflow"));
mu_sieve_abort (mach);
}
mu_list_get (mach->stack, 0, (void **)&mach->reg);
mu_list_remove (mach->stack, (void *)mach->reg);
}
void
_mu_sv_instr_not (mu_sieve_machine_t mach)
{
if (INSTR_DEBUG (mach))
{
mu_sieve_debug (mach, "%4lu: NOT\n", (unsigned long)(mach->pc - 1));
if (INSTR_DISASS (mach))
return;
}
mach->reg = !mach->reg;
}
void
_mu_sv_instr_branch (mu_sieve_machine_t mach)
{
long num = SIEVE_ARG (mach, 0, number);
SIEVE_ADJUST (mach, 1);
if (INSTR_DEBUG (mach))
{
mu_sieve_debug (mach, "%4lu: BRANCH %lu\n",
(unsigned long)(mach->pc-2),
(unsigned long)(mach->pc + num));
if (INSTR_DISASS (mach))
return;
}
mach->pc += num;
}
void
_mu_sv_instr_brz (mu_sieve_machine_t mach)
{
long num = SIEVE_ARG (mach, 0, number);
SIEVE_ADJUST (mach, 1);
if (INSTR_DEBUG (mach))
{
mu_sieve_debug (mach, "%4lu: BRZ %lu\n",
(unsigned long)(mach->pc-2),
(unsigned long)(mach->pc + num));
if (INSTR_DISASS (mach))
return;
}
if (!mach->reg)
mach->pc += num;
}
void
_mu_sv_instr_brnz (mu_sieve_machine_t mach)
{
long num = SIEVE_ARG (mach, 0, number);
SIEVE_ADJUST (mach, 1);
if (INSTR_DEBUG (mach))
{
mu_sieve_debug (mach, "%4lu: BRNZ %lu\n",
(unsigned long)(mach->pc-2),
(unsigned long)(mach->pc + num));
if (INSTR_DISASS (mach))
return;
}
if (mach->reg)
mach->pc += num;
}
void
mu_sieve_abort (mu_sieve_machine_t mach)
{
longjmp (mach->errbuf, 1);
}
void *
mu_sieve_get_data (mu_sieve_machine_t mach)
{
return mach->data;
}
int
mu_sieve_get_locus (mu_sieve_machine_t mach, mu_sieve_locus_t *loc)
{
if (mach->source_list)
{
*loc = mach->locus;
return 0;
}
return 1;
}
mu_message_t
mu_sieve_get_message (mu_sieve_machine_t mach)
{
if (!mach->msg)
mu_mailbox_get_message (mach->mailbox, mach->msgno, &mach->msg);
return mach->msg;
}
size_t
mu_sieve_get_message_num (mu_sieve_machine_t mach)
{
return mach->msgno;
}
int
mu_sieve_get_debug_level (mu_sieve_machine_t mach)
{
return mach->debug_level;
}
const char *
mu_sieve_get_identifier (mu_sieve_machine_t mach)
{
return mach->identifier;
}
int
mu_sieve_is_dry_run (mu_sieve_machine_t mach)
{
return mach->debug_level & MU_SIEVE_DRY_RUN;
}
int
sieve_run (mu_sieve_machine_t mach)
{
if (setjmp (mach->errbuf))
return 1;
mach->action_count = 0;
for (mach->pc = 1; mach->prog[mach->pc].handler; )
(*mach->prog[mach->pc++].instr) (mach);
if (mach->action_count == 0)
mu_sieve_log_action (mach, "IMPLICIT KEEP", NULL);
if (INSTR_DEBUG (mach))
mu_sieve_debug (mach, "%4lu: STOP\n", (unsigned long) mach->pc);
return 0;
}
int
mu_sieve_disass (mu_sieve_machine_t mach)
{
int level = mach->debug_level;
int rc;
mach->debug_level = MU_SIEVE_DEBUG_INSTR | MU_SIEVE_DEBUG_DISAS;
rc = sieve_run (mach);
mach->debug_level = level;
return rc;
}
static int
_sieve_action (mu_observer_t obs, size_t type, void *data, void *action_data)
{
mu_sieve_machine_t mach;
if (type != MU_EVT_MESSAGE_ADD)
return 0;
mach = mu_observer_get_owner (obs);
mach->msgno++;
mu_mailbox_get_message (mach->mailbox, mach->msgno, &mach->msg);
sieve_run (mach);
return 0;
}
int
mu_sieve_mailbox (mu_sieve_machine_t mach, mu_mailbox_t mbox)
{
int rc;
size_t total;
mu_observer_t observer;
mu_observable_t observable;
if (!mach || !mbox)
return EINVAL;
mu_observer_create (&observer, mach);
mu_observer_set_action (observer, _sieve_action, mach);
mu_mailbox_get_observable (mbox, &observable);
mu_observable_attach (observable, MU_EVT_MESSAGE_ADD, observer);
mach->mailbox = mbox;
mach->msgno = 0;
rc = mu_mailbox_scan (mbox, 1, &total);
if (rc)
mu_sieve_error (mach, _("mu_mailbox_scan: %s"), mu_strerror (errno));
mu_observable_detach (observable, observer);
mu_observer_destroy (&observer, mach);
mach->mailbox = NULL;
return rc;
}
int
mu_sieve_message (mu_sieve_machine_t mach, mu_message_t msg)
{
int rc;
if (!mach || !msg)
return EINVAL;
mach->msgno = 1;
mach->msg = msg;
mach->mailbox = NULL;
rc = sieve_run (mach);
mach->msg = NULL;
return rc;
}