sieve.y
11.7 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
%{
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
This program 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 2, or (at your option)
any later version.
This program 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 program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sieve.h>
sieve_machine_t sieve_machine;
int sieve_error_count;
static void branch_fixup __P((size_t start, size_t end));
%}
%union {
char *string;
long number;
sieve_instr_t instr;
sieve_value_t *value;
list_t list;
size_t pc;
struct {
char *ident;
list_t args;
} command;
struct {
size_t begin;
size_t cond;
size_t branch;
} branch;
}
%token <string> IDENT TAG
%token <number> NUMBER
%token <string> STRING MULTILINE
%token REQUIRE IF ELSIF ELSE ANYOF ALLOF NOT
%type <value> arg
%type <list> slist stringlist stringorlist arglist maybe_arglist
%type <command> command
%type <number> testlist
%type <pc> action test statement list elsif else cond begin if block
%type <branch> elsif_branch maybe_elsif else_part
%%
input : /* empty */
| list
{ /* to placate bison */ }
;
list : statement
| list statement
;
statement : REQUIRE stringorlist ';'
{
sieve_require ($2);
sieve_slist_destroy (&$2);
$$ = sieve_machine->pc;
}
| action ';'
/* 1 2 3 4 */
| if cond block else_part
{
sieve_machine->prog[$2].pc = $4.begin - $2 - 1;
if ($4.branch)
branch_fixup ($4.branch, sieve_machine->pc);
}
;
if : IF
{
$$ = sieve_machine->pc;
}
;
else_part : maybe_elsif
{
if ($1.begin)
sieve_machine->prog[$1.cond].pc =
sieve_machine->pc - $1.cond - 1;
else
{
$$.begin = sieve_machine->pc;
$$.branch = 0;
}
}
| maybe_elsif else block
{
if ($1.begin)
{
sieve_machine->prog[$1.cond].pc = $3 - $1.cond - 1;
sieve_machine->prog[$2].pc = $1.branch;
$$.begin = $1.begin;
$$.branch = $2;
}
else
{
$$.begin = $3;
$$.branch = $2;
}
}
;
maybe_elsif : /* empty */
{
$$.begin = 0;
}
| elsif_branch
;
elsif_branch : elsif begin cond block
{
$$.begin = $2;
$$.branch = $1;
$$.cond = $3;
}
| elsif_branch elsif begin cond block
{
sieve_machine->prog[$1.cond].pc = $3 - $1.cond - 1;
sieve_machine->prog[$2].pc = $1.branch;
$$.begin = $1.begin;
$$.branch = $2;
$$.cond = $4;
}
;
elsif : ELSIF
{
sieve_code_instr (instr_branch);
$$ = sieve_machine->pc;
sieve_code_number (0);
}
;
else : ELSE
{
sieve_code_instr (instr_branch);
$$ = sieve_machine->pc;
sieve_code_number (0);
}
;
block : '{' list '}'
{
$$ = $2;
}
;
testlist : cond_expr
{
if (sieve_code_instr (instr_push))
YYERROR;
$$ = 1;
}
| testlist ',' cond_expr
{
if (sieve_code_instr (instr_push))
YYERROR;
$$ = $1 + 1;
}
;
cond : cond_expr
{
sieve_code_instr (instr_brz);
$$ = sieve_machine->pc;
sieve_code_number (0);
}
;
cond_expr : test
{ /* to placate bison */ }
| ANYOF '(' testlist ')'
{
if (sieve_code_instr (instr_anyof)
|| sieve_code_number ($3))
YYERROR;
}
| ALLOF '(' testlist ')'
{
if (sieve_code_instr (instr_allof)
|| sieve_code_number ($3))
YYERROR;
}
| NOT cond_expr
{
if (sieve_code_instr (instr_not))
YYERROR;
}
;
begin : /* empty */
{
$$ = sieve_machine->pc;
}
;
test : command
{
sieve_register_t *reg = sieve_test_lookup (sieve_machine,
$1.ident);
$$ = sieve_machine->pc;
if (!reg)
sieve_compile_error (sieve_filename, sieve_line_num,
"unknown test: %s",
$1.ident);
else if (!reg->required)
sieve_compile_error (sieve_filename, sieve_line_num,
"test `%s' has not been required",
$1.ident);
else if (sieve_code_test (reg, $1.args))
YYERROR;
}
;
command : IDENT maybe_arglist
{
$$.ident = $1;
$$.args = $2;
}
;
action : command
{
sieve_register_t *reg = sieve_action_lookup (sieve_machine,
$1.ident);
$$ = sieve_machine->pc;
if (!reg)
sieve_compile_error (sieve_filename, sieve_line_num,
"unknown action: %s",
$1.ident);
else if (!reg->required)
sieve_compile_error (sieve_filename, sieve_line_num,
"action `%s' has not been required",
$1.ident);
else if (sieve_code_action (reg, $1.args))
YYERROR;
}
;
maybe_arglist: /* empty */
{
$$ = NULL;
}
| arglist
;
arglist : arg
{
list_create (&$$);
list_append ($$, $1);
}
| arglist arg
{
list_append ($1, $2);
$$ = $1;
}
;
arg : stringlist
{
$$ = sieve_value_create (SVT_STRING_LIST, $1);
}
| STRING
{
$$ = sieve_value_create (SVT_STRING, $1);
}
| MULTILINE
{
$$ = sieve_value_create (SVT_STRING, $1);
}
| NUMBER
{
$$ = sieve_value_create (SVT_NUMBER, &$1);
}
| TAG
{
$$ = sieve_value_create (SVT_TAG, $1);
}
;
stringorlist : STRING
{
list_create (&$$);
list_append ($$, $1);
}
| stringlist
;
stringlist : '[' slist ']'
{
$$ = $2;
}
;
slist : STRING
{
list_create (&$$);
list_append ($$, $1);
}
| slist ',' STRING
{
list_append ($1, $3);
$$ = $1;
}
;
%%
int
yyerror (char *s)
{
sieve_compile_error (sieve_filename, sieve_line_num, "%s", s);
return 0;
}
int
sieve_machine_init (sieve_machine_t *pmach, void *data)
{
int rc;
sieve_machine_t mach;
mach = malloc (sizeof (*mach));
if (!mach)
return ENOMEM;
memset (mach, 0, sizeof (*mach));
rc = list_create (&mach->memory_pool);
if (rc)
{
free (mach);
return rc;
}
mach->data = data;
mach->error_printer = _sieve_default_error_printer;
mach->parse_error_printer = _sieve_default_parse_error;
*pmach = mach;
return 0;
}
void
sieve_set_error (sieve_machine_t mach, sieve_printf_t error_printer)
{
mach->error_printer = error_printer ?
error_printer : _sieve_default_error_printer;
}
void
sieve_set_parse_error (sieve_machine_t mach, sieve_parse_error_t p)
{
mach->parse_error_printer = p ? p : _sieve_default_parse_error;
}
void
sieve_set_debug (sieve_machine_t mach, sieve_printf_t debug)
{
mach->debug_printer = debug;
}
void
sieve_set_debug_level (sieve_machine_t mach, mu_debug_t dbg, int level)
{
mach->mu_debug = dbg;
mach->debug_level = level;
}
void
sieve_set_logger (sieve_machine_t mach, sieve_action_log_t logger)
{
mach->logger = logger;
}
void
sieve_set_ticket (sieve_machine_t mach, ticket_t ticket)
{
mach->ticket = ticket;
}
ticket_t
sieve_get_ticket (sieve_machine_t mach)
{
return mach->ticket;
}
mailer_t
sieve_get_mailer (sieve_machine_t mach)
{
if (!mach->mailer)
{
mailer_create (&mach->mailer, NULL);
if (mach->mu_debug)
mailer_set_debug (mach->mailer, mach->mu_debug);
}
return mach->mailer;
}
void
sieve_set_mailer (sieve_machine_t mach, mailer_t mailer)
{
mailer_destroy (&mach->mailer);
mach->mailer = mailer;
}
#define MAILER_DAEMON_PFX "MAILER-DAEMON@"
char *
sieve_get_daemon_email (sieve_machine_t mach)
{
if (!mach->daemon_email)
{
const char *domain = NULL;
mu_get_user_email_domain (&domain);
mach->daemon_email = sieve_malloc (mach,
sizeof(MAILER_DAEMON_PFX) +
strlen (domain));
sprintf (mach->daemon_email, "%s%s", MAILER_DAEMON_PFX, domain);
}
return mach->daemon_email;
}
void
sieve_set_daemon_email (sieve_machine_t mach, const char *email)
{
sieve_mfree (mach, (void *)mach->daemon_email);
mach->daemon_email = sieve_mstrdup (mach, email);
}
struct sieve_destr_record
{
sieve_destructor_t destr;
void *ptr;
};
int
sieve_machine_add_destructor (sieve_machine_t mach, sieve_destructor_t destr,
void *ptr)
{
struct sieve_destr_record *p;
if (!mach->destr_list && list_create (&mach->destr_list))
return 1;
p = sieve_malloc (mach, sizeof (*p));
if (!p)
return 1;
p->destr = destr;
p->ptr = ptr;
return list_prepend (mach->destr_list, p);
}
static int
_run_destructor (void *data, void *unused)
{
struct sieve_destr_record *p = data;
p->destr (p->ptr);
return 0;
}
void
sieve_machine_destroy (sieve_machine_t *pmach)
{
sieve_machine_t mach = *pmach;
mailer_destroy (&mach->mailer);
list_do (mach->destr_list, _run_destructor, NULL);
list_destroy (&mach->destr_list);
list_destroy (&mach->action_list);
list_destroy (&mach->test_list);
list_destroy (&mach->comp_list);
sieve_slist_destroy (&mach->memory_pool);
free (mach);
*pmach = NULL;
}
/* FIXME: When posix thread support is added, sieve_machine_begin() should
acquire the global mutex, locking the current compilation session, and
sieve_machine_finish() should release it */
void
sieve_machine_begin (sieve_machine_t mach)
{
sieve_machine = mach;
sieve_error_count = 0;
sieve_code_instr (NULL);
sieve_register_standard_actions (mach);
sieve_register_standard_tests (mach);
sieve_register_standard_comparators (mach);
}
void
sieve_machine_finish (sieve_machine_t mach)
{
sieve_code_instr (NULL);
}
int
sieve_compile (sieve_machine_t mach, const char *name)
{
int rc;
sieve_machine_begin (mach);
if (sieve_lex_begin (name) == 0)
{
sieve_machine->filename = sieve_mstrdup (sieve_machine, name);
rc = yyparse ();
if (sieve_error_count)
rc = 1;
sieve_lex_finish ();
}
else
rc = 1;
sieve_machine_finish (mach);
if (rc)
sieve_machine_destroy (&mach);
return rc;
}
static void
_branch_fixup (size_t start, size_t end)
{
size_t prev = sieve_machine->prog[start].pc;
if (!prev)
return;
branch_fixup (prev, end);
sieve_machine->prog[prev].pc = end - prev - 1;
}
void
branch_fixup (size_t start, size_t end)
{
_branch_fixup (start, end);
sieve_machine->prog[start].pc = end - start - 1;
}