sieve.y
36.2 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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
%{
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999-2002, 2005-2012, 2014-2016 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, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sieve-priv.h>
#include <mailutils/stdstream.h>
mu_sieve_machine_t mu_sieve_machine;
int mu_sieve_error_count;
static struct mu_sieve_node *sieve_tree;
static struct mu_sieve_node *node_alloc (enum mu_sieve_node_type,
struct mu_locus_range *);
#define YYLLOC_DEFAULT(Current, Rhs, N) \
do \
{ \
if (N) \
{ \
(Current).beg = YYRHSLOC(Rhs, 1).beg; \
(Current).end = YYRHSLOC(Rhs, N).end; \
} \
else \
{ \
(Current).beg = YYRHSLOC(Rhs, 0).end; \
(Current).end = (Current).beg; \
} \
} while (0)
#define LOCUS_EQ(a,b) \
((((a)->mu_file == (b)->mu_file) \
|| ((a)->mu_file && (b)->mu_file \
&& strcmp((a)->mu_file, (b)->mu_file) == 0)) \
&& (a)->mu_line == (b)->mu_line)
#define YY_LOCATION_PRINT(File, Loc) \
do \
{ \
if (LOCUS_EQ(&(Loc).beg, &(Loc).end)) \
fprintf(File, "%s:%u.%u-%u.%u", \
(Loc).beg.mu_file, \
(Loc).beg.mu_line, (Loc).beg.mu_col, \
(Loc).end.mu_line, (Loc).end.mu_col); \
else \
fprintf(File, "%s:%u.%u-%s:%u.%u", \
(Loc).beg.mu_file, \
(Loc).beg.mu_line, (Loc).beg.mu_col, \
(Loc).end.mu_file, \
(Loc).end.mu_line, (Loc).end.mu_col); \
} \
while (0)
%}
%error-verbose
%locations
%union {
char *string;
size_t number;
size_t idx;
struct mu_sieve_slice slice;
struct
{
char *ident;
size_t first;
size_t count;
} command;
struct node_list
{
struct mu_sieve_node *head, *tail;
} node_list;
struct mu_sieve_node *node;
}
%token <string> IDENT TAG
%token <number> NUMBER
%token <string> STRING MULTILINE
%token REQUIRE IF ELSIF ELSE ANYOF ALLOF NOT FALSE TRUE
%type <idx> arg
%type <slice> arglist maybe_arglist slist stringlist stringorlist
%type <command> command
%type <node> action test statement block cond
%type <node> else_part
%type <node_list> list testlist elsif_branch maybe_elsif
%%
input : /* empty */
{
sieve_tree = NULL;
}
| list
{
sieve_tree = $1.head;
}
;
list : statement
{
$$.head = $$.tail = $1;
}
| list statement
{
if ($2)
{
$2->prev = $1.tail;
if ($1.tail)
$1.tail->next = $2;
else
$1.head = $2;
$1.tail = $2;
}
$$ = $1;
}
;
statement : REQUIRE stringorlist ';'
{
mu_sieve_require (mu_sieve_machine, &$2);
/* Reclaim string slots. The string data referred to by
$2 are registered in memory_pool, so we don't free them */
mu_sieve_machine->stringcount -= $2.count;
$$ = NULL;
}
| action ';'
/* 1 2 3 4 */
| IF cond block else_part
{
$$ = node_alloc (mu_sieve_node_cond, &@1);
$$->v.cond.expr = $2;
$$->v.cond.iftrue = $3;
$$->v.cond.iffalse = $4;
}
;
else_part : maybe_elsif
{
$$ = $1.head;
}
| maybe_elsif ELSE block
{
if ($1.tail)
{
$1.tail->v.cond.iffalse = $3;
$$ = $1.head;
}
else
$$ = $3;
}
;
maybe_elsif : /* empty */
{
$$.head = $$.tail = NULL;
}
| elsif_branch
;
/* elsif branches form a singly-linked version of node_list. Nodes in
this list are linked by v.cond.iffalse pointer */
elsif_branch : ELSIF cond block
{
struct mu_sieve_node *node =
node_alloc (mu_sieve_node_cond, &@1);
node->v.cond.expr = $2;
node->v.cond.iftrue = $3;
node->v.cond.iffalse = NULL;
$$.head = $$.tail = node;
}
| elsif_branch ELSIF cond block
{
struct mu_sieve_node *node =
node_alloc (mu_sieve_node_cond, &@2);
node->v.cond.expr = $3;
node->v.cond.iftrue = $4;
node->v.cond.iffalse = NULL;
$1.tail->v.cond.iffalse = node;
$1.tail = node;
$$ = $1;
}
;
block : '{' list '}'
{
$$ = $2.head;
}
;
testlist : cond
{
$$.head = $$.tail = $1;
}
| testlist ',' cond
{
$3->prev = $1.tail;
$1.tail->next = $3;
$1.tail = $3;
}
;
cond : test
| ANYOF '(' testlist ')'
{
$$ = node_alloc (mu_sieve_node_anyof, &@1);
$$->v.node = $3.head;
}
| ALLOF '(' testlist ')'
{
$$ = node_alloc (mu_sieve_node_allof, &@1);
$$->v.node = $3.head;
}
| NOT cond
{
$$ = node_alloc (mu_sieve_node_not, &@1);
$$->v.node = $2;
}
;
test : command
{
mu_sieve_registry_t *reg;
mu_sieve_machine->locus = @1.beg;
reg = mu_sieve_registry_lookup (mu_sieve_machine, $1.ident,
mu_sieve_record_test);
if (!reg)
{
mu_diag_at_locus (MU_LOG_ERROR, &@1.beg,
_("unknown test: %s"),
$1.ident);
mu_i_sv_error (mu_sieve_machine);
}
else if (!reg->required)
{
mu_diag_at_locus (MU_LOG_ERROR, &@1.beg,
_("test `%s' has not been required"),
$1.ident);
mu_i_sv_error (mu_sieve_machine);
}
$$ = node_alloc (mu_sieve_node_test, &@1);
$$->v.command.reg = reg;
$$->v.command.argstart = $1.first;
$$->v.command.argcount = $1.count;
$$->v.command.tagcount = 0;
$$->v.command.comparator = NULL;
mu_i_sv_lint_command (mu_sieve_machine, $$);
}
| TRUE
{
$$ = node_alloc (mu_sieve_node_true, &@1);
}
| FALSE
{
$$ = node_alloc (mu_sieve_node_false, &@1);
}
;
command : IDENT maybe_arglist
{
$$.ident = $1;
$$.first = $2.first;
$$.count = $2.count;
}
;
action : command
{
mu_sieve_registry_t *reg;
mu_sieve_machine->locus = @1.beg;
reg = mu_sieve_registry_lookup (mu_sieve_machine, $1.ident,
mu_sieve_record_action);
if (!reg)
{
mu_diag_at_locus (MU_LOG_ERROR, &@1.beg,
_("unknown action: %s"),
$1.ident);
mu_i_sv_error (mu_sieve_machine);
}
else if (!reg->required)
{
mu_diag_at_locus (MU_LOG_ERROR, &@1.beg,
_("action `%s' has not been required"),
$1.ident);
mu_i_sv_error (mu_sieve_machine);
}
$$ = node_alloc(mu_sieve_node_action, &@1);
$$->v.command.reg = reg;
$$->v.command.argstart = $1.first;
$$->v.command.argcount = $1.count;
$$->v.command.tagcount = 0;
mu_i_sv_lint_command (mu_sieve_machine, $$);
}
;
maybe_arglist: /* empty */
{
$$.first = 0;
$$.count = 0;
}
| arglist
;
arglist : arg
{
$$.first = $1;
$$.count = 1;
}
| arglist arg
{
$1.count++;
$$ = $1;
}
;
arg : stringlist
{
$$ = mu_sieve_value_create (mu_sieve_machine,
SVT_STRING_LIST, &$1);
}
| STRING
{
$$ = mu_sieve_value_create (mu_sieve_machine, SVT_STRING, $1);
}
| MULTILINE
{
$$ = mu_sieve_value_create (mu_sieve_machine, SVT_STRING, $1);
}
| NUMBER
{
$$ = mu_sieve_value_create (mu_sieve_machine, SVT_NUMBER, &$1);
}
| TAG
{
$$ = mu_sieve_value_create (mu_sieve_machine, SVT_TAG, $1);
}
;
stringorlist : STRING
{
$$.first = mu_i_sv_string_create (mu_sieve_machine, $1);
$$.count = 1;
}
| stringlist
;
stringlist : '[' slist ']'
{
$$ = $2;
}
;
slist : STRING
{
$$.first = mu_i_sv_string_create (mu_sieve_machine, $1);
$$.count = 1;
}
| slist ',' STRING
{
mu_i_sv_string_create (mu_sieve_machine, $3);
$1.count++;
$$ = $1;
}
;
%%
int
yyerror (const char *s)
{
extern struct mu_locus mu_sieve_locus;
mu_sieve_machine->locus = mu_sieve_locus;
mu_diag_at_locus (MU_LOG_ERROR, &mu_sieve_locus, "%s", s);
mu_i_sv_error (mu_sieve_machine);
return 0;
}
static struct mu_sieve_node *
node_alloc (enum mu_sieve_node_type type, struct mu_locus_range *lr)
{
struct mu_sieve_node *node = malloc (sizeof (*node));
if (node)
{
node->prev = node->next = NULL;
node->type = type;
node->locus = *lr;
}
return node;
}
static void node_optimize (struct mu_sieve_node *node);
static void node_free (struct mu_sieve_node *node);
static void node_replace (struct mu_sieve_node *node,
struct mu_sieve_node *repl);
static void node_code (struct mu_sieve_machine *mach,
struct mu_sieve_node *node);
static void node_dump (mu_stream_t str, struct mu_sieve_node *node,
unsigned level, struct mu_sieve_machine *mach);
static void tree_free (struct mu_sieve_node **tree);
static void tree_optimize (struct mu_sieve_node *tree);
static void tree_code (struct mu_sieve_machine *mach,
struct mu_sieve_node *tree);
static void tree_dump (mu_stream_t str,
struct mu_sieve_node *tree, unsigned level,
struct mu_sieve_machine *mach);
static void
indent (mu_stream_t str, unsigned level)
{
#define tab " "
#define tablen (sizeof (tab) - 1)
while (level--)
mu_stream_write (str, tab, tablen, NULL);
}
/* mu_sieve_node_noop */
static void
dump_node_noop (mu_stream_t str, struct mu_sieve_node *node, unsigned level,
struct mu_sieve_machine *mach)
{
indent (str, level);
mu_stream_printf (str, "NOOP\n");
}
/* mu_sieve_node_false */
static void
dump_node_false (mu_stream_t str, struct mu_sieve_node *node, unsigned level,
struct mu_sieve_machine *mach)
{
indent (str, level);
mu_stream_printf (str, "FALSE\n");
}
/* mu_sieve_node_true */
static void
dump_node_true (mu_stream_t str, struct mu_sieve_node *node, unsigned level,
struct mu_sieve_machine *mach)
{
indent (str, level);
mu_stream_printf (str, "TRUE\n");
}
/* mu_sieve_node_test & mu_sieve_node_action */
static void
free_node_command (struct mu_sieve_node *node)
{
/* nothing */
}
static void
code_node_test (struct mu_sieve_machine *mach, struct mu_sieve_node *node)
{
mu_i_sv_code_test (mach, node);
}
static void
code_node_action (struct mu_sieve_machine *mach, struct mu_sieve_node *node)
{
mu_i_sv_code_action (mach, node);
}
void
mu_i_sv_valf (mu_sieve_machine_t mach, mu_stream_t str, mu_sieve_value_t *val)
{
mu_stream_printf (str, " ");
if (val->tag)
{
mu_stream_printf (str, ":%s", val->tag);
if (val->type == SVT_VOID)
return;
mu_stream_printf (str, " ");
}
switch (val->type)
{
case SVT_VOID:
mu_stream_printf (str, "(void)");
break;
case SVT_NUMBER:
mu_stream_printf (str, "%zu", val->v.number);
break;
case SVT_STRING:
mu_stream_printf (str, "\"%s\"",
mu_sieve_string_raw (mach, &val->v.list, 0)->orig);
break;
case SVT_STRING_LIST:
{
size_t i;
mu_stream_printf (str, "[");
for (i = 0; i < val->v.list.count; i++)
{
if (i)
mu_stream_printf (str, ", ");
mu_stream_printf (str, "\"%s\"",
mu_sieve_string_raw (mach, &val->v.list, i)->orig);
}
mu_stream_printf (str, "]");
}
break;
case SVT_TAG:
mu_stream_printf (str, ":%s", val->v.string);
break;
default:
abort ();
}
}
static void
dump_node_command (mu_stream_t str, struct mu_sieve_node *node, unsigned level,
struct mu_sieve_machine *mach)
{
size_t i;
indent (str, level);
mu_stream_printf (str, "COMMAND %s", node->v.command.reg->name);
for (i = 0; i < node->v.command.argcount + node->v.command.tagcount; i++)
mu_i_sv_valf (mach, str, &mach->valspace[node->v.command.argstart + i]);
mu_stream_printf (str, "\n");
}
/* mu_sieve_node_cond */
static void
free_node_cond (struct mu_sieve_node *node)
{
tree_free (&node->v.cond.expr);
tree_free (&node->v.cond.iftrue);
tree_free (&node->v.cond.iffalse);
}
static void
optimize_node_cond (struct mu_sieve_node *node)
{
tree_optimize (node->v.cond.expr);
switch (node->v.cond.expr->type)
{
case mu_sieve_node_true:
tree_optimize (node->v.cond.iftrue);
node_replace (node, node->v.cond.iftrue);
break;
case mu_sieve_node_false:
tree_optimize (node->v.cond.iffalse);
node_replace (node, node->v.cond.iffalse);
break;
default:
tree_optimize (node->v.cond.iftrue);
tree_optimize (node->v.cond.iffalse);
}
}
static void
code_node_cond (struct mu_sieve_machine *mach, struct mu_sieve_node *node)
{
size_t br1;
tree_code (mach, node->v.cond.expr);
mu_i_sv_code (mach, (sieve_op_t) _mu_i_sv_instr_brz);
br1 = mach->pc;
mu_i_sv_code (mach, (sieve_op_t) 0);
tree_code (mach, node->v.cond.iftrue);
if (node->v.cond.iffalse)
{
size_t br2;
mu_i_sv_code (mach, (sieve_op_t) _mu_i_sv_instr_branch);
br2 = mach->pc;
mu_i_sv_code (mach, (sieve_op_t) 0);
mach->prog[br1].pc = mach->pc - br1 - 1;
tree_code (mach, node->v.cond.iffalse);
mach->prog[br2].pc = mach->pc - br2 - 1;
}
else
mach->prog[br1].pc = mach->pc - br1 - 1;
}
static void
dump_node_cond (mu_stream_t str, struct mu_sieve_node *node, unsigned level,
struct mu_sieve_machine *mach)
{
indent (str, level);
mu_stream_printf (str, "COND\n");
++level;
indent (str, level);
mu_stream_printf (str, "EXPR:\n");
tree_dump (str, node->v.cond.expr, level + 1, mach);
indent (str, level);
mu_stream_printf (str, "IFTRUE:\n");
tree_dump (str, node->v.cond.iftrue, level + 1, mach);
indent (str, level);
mu_stream_printf (str, "IFFALSE:\n");
tree_dump (str, node->v.cond.iffalse, level + 1, mach);
}
/* mu_sieve_node_anyof & mu_sieve_node_allof */
static void
free_node_x_of (struct mu_sieve_node *node)
{
tree_free (&node->v.node);
}
static void
optimize_x_of (struct mu_sieve_node *node, enum mu_sieve_node_type solve)
{
struct mu_sieve_node *cur;
tree_optimize (node->v.node);
cur = node->v.node;
while (cur)
{
struct mu_sieve_node *next = cur->next;
switch (cur->type)
{
case mu_sieve_node_false:
case mu_sieve_node_true:
if (cur->type == solve)
{
tree_free (&node->v.node);
node->type = solve;
return;
}
else
{
if (cur->prev)
cur->prev->next = next;
else
node->v.node = next;
if (next)
next->prev = cur->prev;
node_free (cur);
}
break;
default:
break;
}
cur = next;
}
if (!node->v.node)
node->type = solve == mu_sieve_node_false ? mu_sieve_node_true : mu_sieve_node_false;
}
static void
code_node_x_of (struct mu_sieve_machine *mach, struct mu_sieve_node *node,
sieve_op_t op)
{
struct mu_sieve_node *cur = node->v.node;
size_t pc = 0;
size_t end;
while (cur)
{
node_code (mach, cur);
if (cur->next)
{
mu_i_sv_code (mach, op);
mu_i_sv_code (mach, (sieve_op_t) pc);
pc = mach->pc - 1;
}
cur = cur->next;
}
/* Fix-up locations */
end = mach->pc;
while (pc != 0)
{
size_t prev = mach->prog[pc].pc;
mach->prog[pc].pc = end - pc - 1;
pc = prev;
}
}
static void
dump_node_x_of (mu_stream_t str, struct mu_sieve_node *node, unsigned level,
mu_sieve_machine_t mach)
{
indent (str, level);
mu_stream_printf (str, "%s:\n",
node->type == mu_sieve_node_allof ? "ALLOF" : "ANYOF");
++level;
node = node->v.node;
while (node)
{
node_dump (str, node, level + 1, mach);
node = node->next;
if (node)
{
indent (str, level);
mu_stream_printf (str, "%s:\n",
node->type == mu_sieve_node_allof ? "AND" : "OR");
}
}
}
/* mu_sieve_node_anyof */
static void
optimize_node_anyof (struct mu_sieve_node *node)
{
optimize_x_of (node, mu_sieve_node_true);
}
static void
code_node_anyof (struct mu_sieve_machine *mach, struct mu_sieve_node *node)
{
code_node_x_of (mach, node, (sieve_op_t) _mu_i_sv_instr_brnz);
}
/* mu_sieve_node_allof */
static void
optimize_node_allof (struct mu_sieve_node *node)
{
return optimize_x_of (node, mu_sieve_node_false);
}
static void
code_node_allof (struct mu_sieve_machine *mach, struct mu_sieve_node *node)
{
code_node_x_of (mach, node, (sieve_op_t) _mu_i_sv_instr_brz);
}
/* mu_sieve_node_not */
static void
free_node_not (struct mu_sieve_node *node)
{
tree_free (&node->v.node);
}
static void
optimize_node_not (struct mu_sieve_node *node)
{
tree_optimize (node->v.node);
switch (node->v.node->type)
{
case mu_sieve_node_false:
tree_free (&node->v.node);
node->type = mu_sieve_node_true;
break;
case mu_sieve_node_true:
tree_free (&node->v.node);
node->type = mu_sieve_node_false;
break;
default:
break;
}
}
static void
code_node_not (struct mu_sieve_machine *mach, struct mu_sieve_node *node)
{
node_code (mach, node->v.node);
mu_i_sv_code (mach, (sieve_op_t) _mu_i_sv_instr_not);
}
static void
dump_node_not (mu_stream_t str, struct mu_sieve_node *node, unsigned level,
struct mu_sieve_machine *mach)
{
indent (str, level);
mu_stream_printf (str, "NOT\n");
node_dump (str, node->v.node, level + 1, mach);
}
struct node_descr
{
void (*code_fn) (struct mu_sieve_machine *mach, struct mu_sieve_node *node);
void (*optimize_fn) (struct mu_sieve_node *node);
void (*free_fn) (struct mu_sieve_node *node);
void (*dump_fn) (mu_stream_t str, struct mu_sieve_node *node, unsigned level,
mu_sieve_machine_t);
};
static struct node_descr node_descr[] = {
[mu_sieve_node_noop] = { NULL, NULL, NULL, dump_node_noop },
[mu_sieve_node_false] = { NULL, NULL, NULL, dump_node_false },
[mu_sieve_node_true] = { NULL, NULL, NULL, dump_node_true },
[mu_sieve_node_test] = { code_node_test, NULL,
free_node_command, dump_node_command },
[mu_sieve_node_action] = { code_node_action, NULL,
free_node_command, dump_node_command },
[mu_sieve_node_cond] = { code_node_cond, optimize_node_cond,
free_node_cond, dump_node_cond },
[mu_sieve_node_anyof] = { code_node_anyof, optimize_node_anyof,
free_node_x_of, dump_node_x_of },
[mu_sieve_node_allof] = { code_node_allof, optimize_node_allof,
free_node_x_of, dump_node_x_of },
[mu_sieve_node_not] = { code_node_not, optimize_node_not,
free_node_not, dump_node_not },
};
static void
node_optimize (struct mu_sieve_node *node)
{
if ((int)node->type >= MU_ARRAY_SIZE (node_descr))
abort ();
if (node_descr[node->type].optimize_fn)
node_descr[node->type].optimize_fn (node);
}
static void
node_free (struct mu_sieve_node *node)
{
if ((int)node->type >= MU_ARRAY_SIZE (node_descr))
abort ();
if (node_descr[node->type].free_fn)
node_descr[node->type].free_fn (node);
free (node);
}
static void
node_replace (struct mu_sieve_node *node, struct mu_sieve_node *repl)
{
struct mu_sieve_node copy;
if ((int)node->type >= MU_ARRAY_SIZE (node_descr))
abort ();
copy = *node;
if (repl)
{
node->type = repl->type;
node->v = repl->v;
switch (copy.type)
{
case mu_sieve_node_cond:
if (repl == copy.v.cond.expr)
copy.v.cond.expr = NULL;
else if (repl == copy.v.cond.iftrue)
copy.v.cond.iftrue = NULL;
else if (repl == copy.v.cond.iffalse)
copy.v.cond.iffalse = NULL;
break;
case mu_sieve_node_not:
if (repl == copy.v.node)
copy.v.node = NULL;
break;
default:
break;
}
}
else
node->type = mu_sieve_node_noop;
if (node_descr[node->type].free_fn)
node_descr[node->type].free_fn (©);
}
static void
node_code (struct mu_sieve_machine *mach, struct mu_sieve_node *node)
{
if ((int)node->type >= MU_ARRAY_SIZE (node_descr))
abort ();
if (node_descr[node->type].code_fn)
{
mu_i_sv_locus (mach, &node->locus);
node_descr[node->type].code_fn (mach, node);
}
}
static void
node_dump (mu_stream_t str, struct mu_sieve_node *node, unsigned level,
struct mu_sieve_machine *mach)
{
if ((int)node->type >= MU_ARRAY_SIZE (node_descr)
|| !node_descr[node->type].dump_fn)
abort ();
node_descr[node->type].dump_fn (str, node, level, mach);
}
static void
tree_free (struct mu_sieve_node **tree)
{
struct mu_sieve_node *cur = *tree;
while (cur)
{
struct mu_sieve_node *next = cur->next;
node_free (cur);
cur = next;
}
}
static void
tree_optimize (struct mu_sieve_node *tree)
{
while (tree)
{
node_optimize (tree);
tree = tree->next;
}
}
static void
tree_code (struct mu_sieve_machine *mach, struct mu_sieve_node *tree)
{
while (tree)
{
node_code (mach, tree);
tree = tree->next;
}
}
static void
tree_dump (mu_stream_t str, struct mu_sieve_node *tree, unsigned level,
struct mu_sieve_machine *mach)
{
while (tree)
{
node_dump (str, tree, level, mach);
tree = tree->next;
}
}
void
mu_i_sv_error (mu_sieve_machine_t mach)
{
mach->state = mu_sieve_state_error;
}
int
mu_sieve_machine_create (mu_sieve_machine_t *pmach)
{
int rc;
mu_sieve_machine_t mach;
mu_sieve_debug_init ();
mach = malloc (sizeof (*mach));
if (!mach)
return ENOMEM;
memset (mach, 0, sizeof (*mach));
mach->memory_pool = NULL;
rc = mu_opool_create (&mach->string_pool, MU_OPOOL_DEFAULT);
if (rc)
{
mu_list_destroy (&mach->memory_pool);
free (mach);
return rc;
}
mach->data = NULL;
mu_sieve_set_diag_stream (mach, mu_strerr);
mu_sieve_set_dbg_stream (mach, mu_strerr);
*pmach = mach;
return 0;
}
void
mu_i_sv_free_stringspace (mu_sieve_machine_t mach)
{
size_t i;
for (i = 0; i < mach->stringcount; i++)
{
if (mach->stringspace[i].rx)
{
regex_t *rx = mach->stringspace[i].rx;
regfree (rx);
}
/* There's no need to free mach->stringspace[i].exp, because
it is allocated in mach's memory pool */
}
}
int
mu_sieve_machine_reset (mu_sieve_machine_t mach)
{
switch (mach->state)
{
case mu_sieve_state_init:
/* Nothing to do */
return 0;
case mu_sieve_state_error:
case mu_sieve_state_compiled:
/* Do the right thing */
break;
case mu_sieve_state_running:
case mu_sieve_state_disass:
/* Can't reset a running machine */
return MU_ERR_FAILURE;
}
mu_i_sv_free_stringspace (mach);
mu_list_clear (mach->memory_pool);
mu_list_clear (mach->destr_list);
mu_opool_free (mach->string_pool, NULL);
mu_i_sv_free_idspace (mach);
mu_list_clear (mach->registry);
mach->stringspace = NULL;
mach->stringcount = 0;
mach->stringmax = 0;
mach->valspace = NULL;
mach->valcount = 0;
mach->valmax = 0;
mach->progsize = 0;
mach->prog = NULL;
mu_assoc_destroy (&mach->vartab);
mach->match_string = NULL;
mach->match_buf = NULL;
mach->match_count = 0;
mach->match_max = 0;
mach->state = mu_sieve_state_init;
return 0;
}
static int
regdup (void *item, void *data)
{
mu_sieve_registry_t *reg = item;
mu_sieve_machine_t mach = data;
mu_sieve_registry_require (mach, reg->name, reg->type);
return 0;
}
static void
copy_stream_state (mu_sieve_machine_t child, mu_sieve_machine_t parent)
{
child->state_flags = parent->state_flags;
child->err_mode = parent->err_mode;
child->err_locus = parent->err_locus;
if (child->err_locus.mu_file)
child->err_locus.mu_file =
mu_sieve_strdup (child, child->err_locus.mu_file);
child->dbg_mode = parent->dbg_mode;
child->dbg_locus = parent->dbg_locus;
if (child->dbg_locus.mu_file)
child->dbg_locus.mu_file =
mu_sieve_strdup (child, child->dbg_locus.mu_file);
child->errstream = parent->errstream;
mu_stream_ref (child->errstream);
child->dbgstream = parent->dbgstream;
mu_stream_ref (child->dbgstream);
}
int
mu_sieve_machine_clone (mu_sieve_machine_t const parent,
mu_sieve_machine_t *pmach)
{
size_t i;
mu_sieve_machine_t child;
int rc;
if (!parent || parent->state == mu_sieve_state_error)
return EINVAL;
rc = mu_sieve_machine_create (&child);
if (rc)
return rc;
rc = setjmp (child->errbuf);
if (rc == 0)
{
child->state = mu_sieve_state_init;
mu_i_sv_register_standard_actions (child);
mu_i_sv_register_standard_tests (child);
mu_i_sv_register_standard_comparators (child);
/* Load necessary modules */
mu_list_foreach (parent->registry, regdup, child);
/* Copy identifiers */
child->idspace = mu_sieve_calloc (child, parent->idcount,
sizeof (child->idspace[0]));
child->idcount = child->idmax = parent->idcount;
for (i = 0; i < child->idcount; i++)
child->idspace[i] = mu_sieve_strdup (parent, parent->idspace[i]);
/* Copy string constants */
child->stringspace = mu_sieve_calloc (child, parent->stringcount,
sizeof (child->stringspace[0]));
child->stringcount = child->stringmax = parent->stringcount;
for (i = 0; i < parent->stringcount; i++)
{
memset (&child->stringspace[i], 0, sizeof (child->stringspace[0]));
child->stringspace[i].orig =
mu_sieve_strdup (parent, parent->stringspace[i].orig);
}
/* Copy value space */
child->valspace = mu_sieve_calloc (child, parent->valcount,
sizeof child->valspace[0]);
child->valcount = child->valmax = parent->valcount;
for (i = 0; i < child->valcount; i++)
{
child->valspace[i].type = parent->valspace[i].type;
child->valspace[i].tag =
mu_sieve_strdup (child, parent->valspace[i].tag);
switch (child->valspace[i].type)
{
case SVT_TAG:
child->valspace[i].v.string =
mu_sieve_strdup (child, parent->valspace[i].v.string);
break;
default:
child->valspace[i].v = parent->valspace[i].v;
}
}
/* Copy progspace */
child->progsize = parent->progsize;
child->prog = mu_sieve_calloc (child, parent->progsize,
sizeof child->prog[0]);
memcpy (child->prog, parent->prog,
parent->progsize * sizeof (child->prog[0]));
/* Copy variables */
if (mu_sieve_has_variables (parent))
{
mu_i_sv_copy_variables (child, parent);
child->match_string = NULL;
child->match_buf = NULL;
child->match_count = 0;
child->match_max = 0;
}
/* Copy user-defined settings */
child->dry_run = parent->dry_run;
copy_stream_state (child, parent);
child->data = parent->data;
child->logger = parent->logger;
child->daemon_email = parent->daemon_email;
*pmach = child;
}
else
mu_sieve_machine_destroy (&child);
return rc;
}
int
mu_sieve_machine_dup (mu_sieve_machine_t const in, mu_sieve_machine_t *out)
{
int rc;
mu_sieve_machine_t mach;
if (!in || in->state == mu_sieve_state_error)
return EINVAL;
mach = malloc (sizeof (*mach));
if (!mach)
return ENOMEM;
memset (mach, 0, sizeof (*mach));
rc = mu_list_create (&mach->memory_pool);
if (rc)
{
free (mach);
return rc;
}
mach->destr_list = NULL;
mach->registry = NULL;
mach->progsize = in->progsize;
mach->prog = in->prog;
switch (in->state)
{
case mu_sieve_state_running:
case mu_sieve_state_disass:
mach->state = mu_sieve_state_compiled;
break;
default:
mach->state = in->state;
}
rc = setjmp (mach->errbuf);
if (rc == 0)
{
mach->pc = 0;
mach->reg = 0;
mach->dry_run = in->dry_run;
mach->state_flags = in->state_flags;
mach->err_mode = in->err_mode;
mach->err_locus = in->err_locus;
mach->dbg_mode = in->dbg_mode;
mach->dbg_locus = in->dbg_locus;
copy_stream_state (mach, in);
mach->data = in->data;
mach->logger = in->logger;
mach->daemon_email = in->daemon_email;
*out = mach;
}
else
mu_sieve_machine_destroy (&mach);
return rc;
}
void
mu_sieve_get_diag_stream (mu_sieve_machine_t mach, mu_stream_t *pstr)
{
*pstr = mach->errstream;
mu_stream_ref (*pstr);
}
void
mu_sieve_set_diag_stream (mu_sieve_machine_t mach, mu_stream_t str)
{
mu_stream_unref (mach->errstream);
mach->errstream = str;
mu_stream_ref (mach->errstream);
}
void
mu_sieve_set_dbg_stream (mu_sieve_machine_t mach, mu_stream_t str)
{
mu_stream_unref (mach->dbgstream);
mach->dbgstream = str;
mu_stream_ref (mach->dbgstream);
}
void
mu_sieve_get_dbg_stream (mu_sieve_machine_t mach, mu_stream_t *pstr)
{
*pstr = mach->dbgstream;
mu_stream_ref (*pstr);
}
void
mu_sieve_set_logger (mu_sieve_machine_t mach, mu_sieve_action_log_t logger)
{
mach->logger = logger;
}
mu_mailer_t
mu_sieve_get_mailer (mu_sieve_machine_t mach)
{
if (!mach->mailer)
{
int rc;
rc = mu_mailer_create (&mach->mailer, NULL);
if (rc)
{
mu_sieve_error (mach,
_("%lu: cannot create mailer: %s"),
(unsigned long) mu_sieve_get_message_num (mach),
mu_strerror (rc));
return NULL;
}
rc = mu_mailer_open (mach->mailer, 0);
if (rc)
{
mu_url_t url = NULL;
mu_mailer_get_url (mach->mailer, &url);
mu_sieve_error (mach,
_("%lu: cannot open mailer %s: %s"),
(unsigned long) mu_sieve_get_message_num (mach),
mu_url_to_string (url), mu_strerror (rc));
mu_mailer_destroy (&mach->mailer);
return NULL;
}
}
return mach->mailer;
}
void
mu_sieve_set_mailer (mu_sieve_machine_t mach, mu_mailer_t mailer)
{
mu_mailer_destroy (&mach->mailer);
mach->mailer = mailer;
}
#define MAILER_DAEMON_PFX "MAILER-DAEMON@"
char *
mu_sieve_get_daemon_email (mu_sieve_machine_t mach)
{
if (!mach->daemon_email)
{
const char *domain = NULL;
mu_get_user_email_domain (&domain);
mach->daemon_email = mu_sieve_malloc (mach,
sizeof(MAILER_DAEMON_PFX) +
strlen (domain));
sprintf (mach->daemon_email, "%s%s", MAILER_DAEMON_PFX, domain);
}
return mach->daemon_email;
}
void
mu_sieve_set_daemon_email (mu_sieve_machine_t mach, const char *email)
{
mu_sieve_free (mach, (void *)mach->daemon_email);
mach->daemon_email = mu_sieve_strdup (mach, email);
}
struct sieve_destr_record
{
mu_sieve_destructor_t destr;
void *ptr;
};
static void
run_destructor (void *data)
{
struct sieve_destr_record *p = data;
p->destr (p->ptr);
free (data);
}
void
mu_sieve_machine_add_destructor (mu_sieve_machine_t mach,
mu_sieve_destructor_t destr,
void *ptr)
{
int rc;
struct sieve_destr_record *p;
if (!mach->destr_list)
{
rc = mu_list_create (&mach->destr_list);
if (rc)
{
mu_sieve_error (mach, "mu_list_create: %s", mu_strerror (rc));
destr (ptr);
mu_sieve_abort (mach);
}
mu_list_set_destroy_item (mach->destr_list, run_destructor);
}
p = malloc (sizeof (*p));
if (!p)
{
mu_sieve_error (mach, "%s", mu_strerror (errno));
destr (ptr);
mu_sieve_abort (mach);
}
p->destr = destr;
p->ptr = ptr;
rc = mu_list_prepend (mach->destr_list, p);
if (rc)
{
mu_sieve_error (mach, "mu_list_prepend: %s", mu_strerror (rc));
destr (ptr);
free (p);
mu_sieve_abort (mach);
}
}
void
mu_sieve_machine_destroy (mu_sieve_machine_t *pmach)
{
mu_sieve_machine_t mach = *pmach;
mu_i_sv_free_stringspace (mach);
mu_sieve_free (mach, mach->stringspace);
mu_stream_destroy (&mach->errstream);
mu_stream_destroy (&mach->dbgstream);
mu_mailer_destroy (&mach->mailer);
mu_list_destroy (&mach->destr_list);
mu_list_destroy (&mach->registry);
mu_sieve_free (mach, mach->idspace);
mu_opool_destroy (&mach->string_pool);
mu_list_destroy (&mach->memory_pool);
free (mach);
*pmach = NULL;
}
int
with_machine (mu_sieve_machine_t mach, char const *name,
int (*thunk) (void *), void *data)
{
int rc = 0;
mu_stream_t save_errstr;
rc = mu_sieve_machine_reset (mach);
if (rc)
return rc;
save_errstr = mu_strerr;
mu_stream_ref (save_errstr);
mu_strerr = mach->errstream;
mu_stream_ref (mu_strerr);
mu_sieve_machine = mach;
rc = setjmp (mach->errbuf);
if (rc == 0)
{
mach->state = mu_sieve_state_init;
mu_i_sv_register_standard_actions (mach);
mu_i_sv_register_standard_tests (mach);
mu_i_sv_register_standard_comparators (mach);
mu_sieve_stream_save (mach);
rc = thunk (data);
mu_sieve_stream_restore (mach);
mu_stream_unref (save_errstr);
mu_strerr = save_errstr;
mu_stream_unref (mu_strerr);
}
else
mach->state = mu_sieve_state_error;
return rc;
}
/* Rescan all registered strings to determine their properties */
static void
string_rescan (mu_sieve_machine_t mach)
{
size_t i;
int hasvar = mu_sieve_has_variables (mach);
for (i = 0; i < mach->stringcount; i++)
{
mach->stringspace[i].changed = 0;
if (hasvar)
{
mach->stringspace[i].constant = 0;
mu_sieve_string_get (mach, &mach->stringspace[i]);
mu_sieve_free (mach, mach->stringspace[i].exp);
mach->stringspace[i].exp = NULL;
mach->stringspace[i].constant = !mach->stringspace[i].changed;
mach->stringspace[i].changed = 0;
}
else
mach->stringspace[i].constant = 1;
}
}
static int
sieve_parse (void)
{
int rc;
sieve_tree = NULL;
yydebug = mu_debug_level_p (mu_sieve_debug_handle, MU_DEBUG_TRACE3);
rc = yyparse ();
mu_i_sv_lex_finish ();
if (rc)
mu_i_sv_error (mu_sieve_machine);
if (mu_sieve_machine->state == mu_sieve_state_init)
{
if (mu_debug_level_p (mu_sieve_debug_handle, MU_DEBUG_TRACE1))
{
mu_error (_("Unoptimized parse tree"));
tree_dump (mu_strerr, sieve_tree, 0, mu_sieve_machine);
}
tree_optimize (sieve_tree);
if (mu_debug_level_p (mu_sieve_debug_handle, MU_DEBUG_TRACE2))
{
mu_error (_("Optimized parse tree"));
tree_dump (mu_strerr, sieve_tree, 0, mu_sieve_machine);
}
mu_i_sv_code (mu_sieve_machine, (sieve_op_t) (sieve_instr_t) 0);
/* Clear location, so that mu_i_sv_locus will do its job. */
mu_sieve_machine->locus.mu_file = NULL;
mu_sieve_machine->locus.mu_line = 0;
mu_sieve_machine->locus.mu_col = 0;
tree_code (mu_sieve_machine, sieve_tree);
mu_i_sv_code (mu_sieve_machine, (sieve_op_t) (sieve_instr_t) 0);
}
if (rc == 0)
{
if (mu_sieve_machine->state == mu_sieve_state_error)
rc = MU_ERR_PARSE;
else
{
string_rescan (mu_sieve_machine);
mu_sieve_machine->state = mu_sieve_state_compiled;
}
}
tree_free (&sieve_tree);
return rc;
}
static int
sieve_compile_file (void *name)
{
if (mu_i_sv_lex_begin (name) == 0)
return sieve_parse ();
return MU_ERR_FAILURE;
}
int
mu_sieve_compile (mu_sieve_machine_t mach, const char *name)
{
return with_machine (mach, name, sieve_compile_file, (void *) name);
}
struct strbuf
{
const char *ptr;
size_t size;
const char *file;
int line;
};
static int
sieve_compile_strbuf (void *name)
{
struct strbuf *buf = name;
if (mu_i_sv_lex_begin_string (buf->ptr, buf->size, buf->file, buf->line) == 0)
return sieve_parse ();
return MU_ERR_FAILURE;
}
int
mu_sieve_compile_buffer (mu_sieve_machine_t mach,
const char *str, int strsize,
const char *fname, int line)
{
struct strbuf buf;
buf.ptr = str;
buf.size = strsize;
buf.file = fname;
buf.line = line;
return with_machine (mach, fname, sieve_compile_strbuf, &buf);
}