-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.c
More file actions
1126 lines (979 loc) · 27.3 KB
/
expr.c
File metadata and controls
1126 lines (979 loc) · 27.3 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
#include "expr.h"
#include "config.h"
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <limits.h> /* NAME_MAX */
#include <regex.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "libks/arena-buffer.h"
#include "libks/arena.h"
#include "libks/buffer.h"
#include "libks/compiler.h"
#include "libks/consistency.h"
#include "libks/list.h"
#include "libks/vector.h"
#include "date-time.h"
#include "environment.h"
#include "match.h"
#include "message.h"
#include "string-list.h"
#include "util.h"
struct expr_regex {
regex_t pattern;
regmatch_t *matches;
size_t nmatches;
unsigned int flags;
};
static int expr_eval_add_header(struct expr *, struct expr_eval_arg *);
static int expr_eval_all(struct expr *, struct expr_eval_arg *);
static int expr_eval_and(struct expr *, struct expr_eval_arg *);
static int expr_eval_attachment(struct expr *, struct expr_eval_arg *);
static int expr_eval_attachment_block(struct expr *,
struct expr_eval_arg *);
static int expr_eval_block(struct expr *, struct expr_eval_arg *);
static int expr_eval_body(struct expr *, struct expr_eval_arg *);
static int expr_eval_break(struct expr *, struct expr_eval_arg *);
static int expr_eval_command(struct expr *, struct expr_eval_arg *);
static int expr_eval_date(struct expr *, struct expr_eval_arg *);
static int expr_eval_discard(struct expr *, struct expr_eval_arg *);
static int expr_eval_exec(struct expr *, struct expr_eval_arg *);
static int expr_eval_flag(struct expr *, struct expr_eval_arg *);
static int expr_eval_flags(struct expr *, struct expr_eval_arg *);
static int expr_eval_header(struct expr *, struct expr_eval_arg *);
static int expr_eval_label(struct expr *, struct expr_eval_arg *);
static int expr_eval_match(struct expr *, struct expr_eval_arg *);
static int expr_eval_move(struct expr *, struct expr_eval_arg *);
static int expr_eval_neg(struct expr *, struct expr_eval_arg *);
static int expr_eval_new(struct expr *, struct expr_eval_arg *);
static int expr_eval_old(struct expr *, struct expr_eval_arg *);
static int expr_eval_or(struct expr *, struct expr_eval_arg *);
static int expr_eval_pass(struct expr *, struct expr_eval_arg *);
static int expr_eval_reject(struct expr *, struct expr_eval_arg *);
static int expr_eval_stat(struct expr *, struct expr_eval_arg *);
static const char *expr_inspect_add_header(const struct expr *,
const struct match *, const struct message *, struct arena_scope *);
static const char *expr_inspect_attachment_block(const struct expr *,
const struct match *, const struct message *, struct arena_scope *);
static const char *expr_inspect_break(const struct expr *,
const struct match *, const struct message *, struct arena_scope *);
static const char *expr_inspect_discard(const struct expr *,
const struct match *, const struct message *, struct arena_scope *);
static const char *expr_inspect_exec(const struct expr *,
const struct match *, const struct message *, struct arena_scope *);
static const char *expr_inspect_label(const struct expr *,
const struct match *, const struct message *, struct arena_scope *);
static const char *expr_inspect_move(const struct expr *,
const struct match *, const struct message *, struct arena_scope *);
static const char *expr_inspect_pass(const struct expr *,
const struct match *, const struct message *, struct arena_scope *);
static const char *expr_inspect_reject(const struct expr *,
const struct match *, const struct message *, struct arena_scope *);
static size_t expr_inspect_prefix(const struct expr *,
const struct environment *);
static int expr_match(struct expr *, struct expr_eval_arg *);
static int expr_regexec(struct expr *, struct expr_eval_arg *,
const char *, const char *);
static void expr_regcopy(const struct expr *, struct match *, const char *,
struct arena_scope *);
static size_t strnwidth(const char *, size_t);
struct expr *
expr_alloc(enum expr_type type, unsigned int lno, struct expr *lhs,
struct expr *rhs, struct arena_scope *s)
{
struct expr *ex;
ex = arena_calloc(s, 1, sizeof(*ex));
ex->ex_type = type;
ex->ex_lno = lno;
ex->ex_lhs = lhs;
ex->ex_rhs = rhs;
switch (ex->ex_type) {
case EXPR_TYPE_BLOCK:
ex->ex_eval = &expr_eval_block;
break;
case EXPR_TYPE_AND:
ex->ex_eval = &expr_eval_and;
break;
case EXPR_TYPE_OR:
ex->ex_eval = &expr_eval_or;
break;
case EXPR_TYPE_NEG:
ex->ex_eval = &expr_eval_neg;
break;
case EXPR_TYPE_MATCH:
ex->ex_eval = &expr_eval_match;
break;
case EXPR_TYPE_ALL:
ex->ex_eval = &expr_eval_all;
break;
case EXPR_TYPE_ATTACHMENT:
ex->ex_eval = &expr_eval_attachment;
break;
case EXPR_TYPE_BODY:
ex->ex_eval = &expr_eval_body;
ex->ex_flags = EXPR_FLAG_INSPECT | EXPR_FLAG_INTERPOLATE;
break;
case EXPR_TYPE_DATE:
ex->ex_eval = &expr_eval_date;
ex->ex_flags = EXPR_FLAG_INSPECT;
break;
case EXPR_TYPE_HEADER:
ex->ex_eval = &expr_eval_header;
ex->ex_flags = EXPR_FLAG_INSPECT | EXPR_FLAG_INTERPOLATE;
break;
case EXPR_TYPE_NEW:
ex->ex_eval = &expr_eval_new;
break;
case EXPR_TYPE_OLD:
ex->ex_eval = &expr_eval_old;
break;
case EXPR_TYPE_STAT:
ex->ex_eval = &expr_eval_stat;
break;
case EXPR_TYPE_COMMAND:
ex->ex_eval = &expr_eval_command;
break;
case EXPR_TYPE_MOVE:
ex->ex_eval = &expr_eval_move;
ex->ex_inspect = &expr_inspect_move;
ex->ex_flags = EXPR_FLAG_ACTION | EXPR_FLAG_PATH;
break;
case EXPR_TYPE_FLAG:
ex->ex_eval = &expr_eval_flag;
ex->ex_inspect = &expr_inspect_move;
ex->ex_flags = EXPR_FLAG_ACTION | EXPR_FLAG_PATH;
break;
case EXPR_TYPE_FLAGS:
ex->ex_eval = &expr_eval_flags;
ex->ex_inspect = &expr_inspect_move;
ex->ex_flags = EXPR_FLAG_ACTION | EXPR_FLAG_PATH;
break;
case EXPR_TYPE_DISCARD:
ex->ex_eval = &expr_eval_discard;
ex->ex_inspect = &expr_inspect_discard;
ex->ex_flags = EXPR_FLAG_ACTION;
break;
case EXPR_TYPE_BREAK:
ex->ex_eval = &expr_eval_break;
ex->ex_inspect = &expr_inspect_break;
ex->ex_flags = EXPR_FLAG_ACTION;
break;
case EXPR_TYPE_LABEL:
ex->ex_eval = &expr_eval_label;
ex->ex_inspect = &expr_inspect_label;
ex->ex_flags = EXPR_FLAG_ACTION | EXPR_FLAG_PATH;
break;
case EXPR_TYPE_PASS:
ex->ex_eval = &expr_eval_pass;
ex->ex_inspect = &expr_inspect_pass;
ex->ex_flags = EXPR_FLAG_ACTION;
break;
case EXPR_TYPE_REJECT:
ex->ex_eval = &expr_eval_reject;
ex->ex_inspect = &expr_inspect_reject;
ex->ex_flags = EXPR_FLAG_ACTION;
break;
case EXPR_TYPE_EXEC:
ex->ex_eval = &expr_eval_exec;
ex->ex_inspect = &expr_inspect_exec;
ex->ex_flags = EXPR_FLAG_ACTION;
break;
case EXPR_TYPE_ATTACHMENT_BLOCK:
ex->ex_eval = &expr_eval_attachment_block;
ex->ex_inspect = &expr_inspect_attachment_block;
ex->ex_flags = EXPR_FLAG_ACTION;
break;
case EXPR_TYPE_ADD_HEADER:
ex->ex_eval = &expr_eval_add_header;
ex->ex_inspect = &expr_inspect_add_header;
ex->ex_flags = EXPR_FLAG_ACTION | EXPR_FLAG_PATH;
break;
}
ASSERT_CONSISTENCY(ex->ex_flags & EXPR_FLAG_ACTION, ex->ex_inspect);
return ex;
}
void
expr_set_add_header(struct expr *ex, const char *key, const char *val)
{
ex->ex_add_header.key = key;
ex->ex_add_header.val = val;
}
void
expr_set_date(struct expr *ex, enum expr_date_field field,
enum expr_date_cmp cmp, long long int age, struct arena_scope *s)
{
assert(ex->ex_type == EXPR_TYPE_DATE);
ex->ex_date.field = field;
ex->ex_date.cmp = cmp;
ex->ex_date.age = age;
/* Cheat a bit by adding a match all pattern used during dry run. */
(void)expr_set_pattern(ex, ".*", 0, NULL, s);
}
void
expr_set_stat(struct expr *ex, const char *path, enum expr_stat stat,
struct arena_scope *s)
{
struct string_list *strings;
assert(ex->ex_type == EXPR_TYPE_STAT);
strings = strings_alloc(s);
strings_append(strings, path);
expr_set_strings(ex, strings);
ex->ex_stat.stat = stat;
}
int
expr_set_exec(struct expr *ex, struct string_list *cmd, unsigned int flags)
{
if ((flags & (EXPR_EXEC_STDIN | EXPR_EXEC_BODY)) == EXPR_EXEC_BODY)
return 1;
expr_set_strings(ex, cmd);
ex->ex_exec.flags = flags;
return 0;
}
void
expr_set_strings(struct expr *ex, struct string_list *strings)
{
ex->ex_strings = strings;
}
static void
regex_free(void *arg)
{
struct expr_regex *re = arg;
regfree(&re->pattern);
}
/*
* Associate the given pattern with the expression.
*
* The flags may be any combination of the following values:
*
* EXPR_PATTERN_ICASE Ignore case.
*
* EXPR_PATTERN_LCASE Lowercase the matched string from a subexpression
* before interpolation.
*
* EXPR_PATTERN_UCASE Uppercase the matched string from a subexpression
* before interpolation.
*
* Returns zero if pattern is successfully compiled into a regular expression.
* Otherwise, returns non-zero and if errstr is not NULL it will point to an
* explanation on why the compilation failed.
*/
int
expr_set_pattern(struct expr *ex, const char *pattern, unsigned int flags,
const char **errstr, struct arena_scope *s)
{
struct {
unsigned int eflag; /* expr pattern flag */
unsigned int pflag; /* propagate expr pattern flag */
int rflag; /* regcomp() flag(s) */
} fflags[] = {
{ EXPR_PATTERN_ICASE, 0, REG_ICASE },
{ EXPR_PATTERN_LCASE, 1, 0 },
{ EXPR_PATTERN_UCASE, 1, 0 },
{ 0, 0, 0 },
};
int rflags = REG_EXTENDED | REG_NEWLINE;
int error, i;
assert(ex->ex_re == NULL);
ex->ex_re = arena_calloc(s, 1, sizeof(*ex->ex_re));
arena_cleanup(s, regex_free, ex->ex_re);
for (i = 0; fflags[i].eflag != 0; i++) {
if ((flags & fflags[i].eflag) == 0)
continue;
if (fflags[i].pflag)
ex->ex_re->flags |= fflags[i].eflag;
if (fflags[i].rflag)
rflags |= fflags[i].rflag;
flags &= ~fflags[i].eflag;
}
assert(flags == 0);
if ((error = regcomp(&ex->ex_re->pattern, pattern, rflags)) != 0) {
if (errstr != NULL) {
static char buf[1024];
regerror(error, &ex->ex_re->pattern, buf, sizeof(buf));
*errstr = buf;
}
return 1;
}
ex->ex_re->nmatches = ex->ex_re->pattern.re_nsub + 1;
ex->ex_re->matches = arena_calloc(s, ex->ex_re->nmatches,
sizeof(*ex->ex_re->matches));
return 0;
}
/*
* Returns 0 if the expression matches the given message. The given match list
* will be populated with the matching expressions.
* Otherwise, non-zero is returned.
*/
int
expr_eval(struct expr *ex, struct expr_eval_arg *ea)
{
return ex->ex_eval(ex, ea);
}
/*
* Returns the number of expressions with the given type.
*/
int
expr_count(const struct expr *ex, enum expr_type type)
{
int n = 0;
if (ex == NULL)
return 0;
if (ex->ex_type == type)
n = 1;
return n + expr_count(ex->ex_lhs, type) + expr_count(ex->ex_rhs, type);
}
/*
* Returns the number of actions.
*/
int
expr_count_actions(const struct expr *ex)
{
int n = 0;
if (ex == NULL)
return 0;
if (ex->ex_flags & EXPR_FLAG_ACTION)
n = 1;
return n + expr_count_actions(ex->ex_lhs) +
expr_count_actions(ex->ex_rhs);
}
const char *
expr_inspect(const struct expr *ex, const struct match *mh,
const struct message *msg, struct arena_scope *s)
{
return ex->ex_inspect(ex, mh, msg, s);
}
/*
* Writes a human readable representation of the latest match to fh.
*/
void
expr_inspect_matches(const struct expr *ex, const struct match *mh,
const struct environment *env)
{
const char *lbeg, *lend, *p;
size_t indent, len, pindent;
unsigned int i;
int printkey = 1;
if ((ex->ex_flags & EXPR_FLAG_INSPECT) == 0)
return;
pindent = strlen(mh->mh_key) + 2;
for (i = 0; i < mh->mh_nmatches; i++) {
size_t beg, end, plen;
beg = mh->mh_matches[i].m_beg;
end = mh->mh_matches[i].m_end;
if (beg == end)
continue;
lbeg = mh->mh_val;
for (;;) {
if ((p = strchr(lbeg, '\n')) == NULL ||
p > mh->mh_val + beg)
break;
lbeg = p + 1;
}
lbeg += nspaces(lbeg);
lend = strchr(lbeg, '\n');
if (lend == NULL)
lend = mh->mh_val + strlen(mh->mh_val);
len = strnwidth(mh->mh_val + beg, end - beg);
/* Try to compensate for the "^$" markers. */
if (len >= 2)
len -= 2;
else
len = 0;
if (printkey) {
pindent += expr_inspect_prefix(ex, env);
printkey = 0;
fprintf(stdout, "%s: ", mh->mh_key);
} else {
fprintf(stdout, "%*s", (int)pindent, "");
}
plen = beg - (size_t)(lbeg - mh->mh_val);
indent = pindent + strnwidth(lbeg, plen);
fprintf(stdout, "%.*s\n%*s^%*s$\n",
(int)(lend - lbeg), lbeg, (int)indent, "", (int)len, "");
}
}
static int
expr_eval_add_header(struct expr *ex, struct expr_eval_arg *ea)
{
struct match *mh;
mh = match_alloc(ex, ea->ea_msg, ea->ea_arena.eternal_scope);
if (matches_append(ea->ea_ml, mh))
return EXPR_ERROR;
return EXPR_MATCH;
}
static int
expr_eval_all(struct expr *UNUSED(ex), struct expr_eval_arg *UNUSED(ea))
{
return EXPR_MATCH;
}
static int
expr_eval_and(struct expr *ex, struct expr_eval_arg *ea)
{
int ev;
if ((ev = expr_eval(ex->ex_lhs, ea)) != EXPR_MATCH)
return ev; /* no match or error, short-circuit */
return expr_eval(ex->ex_rhs, ea);
}
static int
expr_eval_attachment(struct expr *ex, struct expr_eval_arg *ea)
{
VECTOR(struct message *) attachments;
struct message *msg = ea->ea_msg;
size_t i;
int ev = EXPR_NOMATCH;
attachments = message_get_attachments(msg);
if (attachments == NULL)
return EXPR_ERROR;
for (i = 0; i < VECTOR_LENGTH(attachments); i++) {
struct message *attach = attachments[i];
ea->ea_msg = attach;
ev = expr_eval(ex->ex_lhs, ea);
ea->ea_msg = msg;
if (ev == EXPR_NOMATCH)
continue;
break; /* match or error, return */
}
message_free_attachments(attachments);
return ev;
}
static int
expr_eval_attachment_block(struct expr *ex, struct expr_eval_arg *ea)
{
VECTOR(struct message *) attachments;
struct message *msg = ea->ea_msg;
int ev = EXPR_NOMATCH;
size_t i;
attachments = message_get_attachments(msg);
if (attachments == NULL)
return EXPR_ERROR;
for (i = 0; i < VECTOR_LENGTH(attachments); i++) {
struct message *attach = attachments[i];
int ev2;
ea->ea_msg = attach;
ev2 = expr_eval(ex->ex_lhs, ea);
ea->ea_msg = msg;
switch (ev2) {
case EXPR_ERROR:
return EXPR_ERROR;
case EXPR_MATCH:
ev = EXPR_MATCH;
break;
}
}
message_free_attachments(attachments);
return ev;
}
static int
expr_eval_block(struct expr *ex, struct expr_eval_arg *ea)
{
int ev;
ev = expr_eval(ex->ex_lhs, ea);
if (ev == EXPR_ERROR)
return EXPR_ERROR;
if (matches_find(ea->ea_ml, EXPR_TYPE_BREAK) != NULL) {
matches_remove_by_type(ea->ea_ml, EXPR_TYPE_BREAK);
return EXPR_NOMATCH; /* break, continue evaluation */
}
if (matches_find(ea->ea_ml, EXPR_TYPE_PASS) != NULL) {
/*
* If removing the pass action results in a match list without
* any actions left, we got a pass followed by no effective
* action. Therefore treat it as a no match.
*/
if (matches_remove_by_type(ea->ea_ml, EXPR_TYPE_PASS) == 0)
return EXPR_NOMATCH;
return EXPR_MATCH;
}
return ev;
}
static int
expr_eval_body(struct expr *ex, struct expr_eval_arg *ea)
{
const char *body;
body = message_get_body(ea->ea_msg);
if (body == NULL)
return EXPR_ERROR;
return expr_regexec(ex, ea, "Body", body);
}
static int
expr_eval_break(struct expr *ex, struct expr_eval_arg *ea)
{
struct match *mh;
mh = match_alloc(ex, ea->ea_msg, ea->ea_arena.eternal_scope);
if (matches_append(ea->ea_ml, mh))
return EXPR_ERROR;
/*
* Return match in order to continue evaluation. The return value is
* later inverted by expr_eval_block().
*/
return EXPR_MATCH;
}
static int
expr_eval_command(struct expr *ex, struct expr_eval_arg *ea)
{
struct match *mh;
int ev = EXPR_NOMATCH;
int error;
mh = match_alloc(ex, ea->ea_msg, ea->ea_arena.eternal_scope);
if (matches_append(ea->ea_ml, mh))
return EXPR_ERROR;
if (match_interpolate(mh, NULL, ea->ea_arena.eternal_scope,
ea->ea_arena.scratch)) {
ev = EXPR_ERROR;
} else if ((error = exec(mh->mh_exec, -1)) != 0) {
/* A non-zero exit is not considered fatal. */
if (error < 0)
ev = EXPR_ERROR;
} else {
ev = EXPR_MATCH;
}
matches_remove(ea->ea_ml, mh);
return ev;
}
static int
expr_eval_date(struct expr *ex, struct expr_eval_arg *ea)
{
char buf[32];
const char *date;
long long int delta, tim;
if (ex->ex_date.field == EXPR_DATE_FIELD_HEADER) {
date = message_get_header1(ea->ea_msg, "Date");
if (date == NULL)
return EXPR_NOMATCH;
if (time_parse(date, &tim, ea->ea_env))
return EXPR_ERROR;
} else {
struct stat st;
/*
* Initial value might look redundant but otherwise GCC will
* complain about it might being used uninitialized.
*/
const struct timespec *ts = NULL;
const char *path;
switch (ex->ex_date.field) {
case EXPR_DATE_FIELD_HEADER:
return EXPR_ERROR; /* UNREACHABLE */
case EXPR_DATE_FIELD_ACCESS:
ts = &st.st_atim;
break;
case EXPR_DATE_FIELD_MODIFIED:
ts = &st.st_mtim;
break;
case EXPR_DATE_FIELD_CREATED:
ts = &st.st_ctim;
break;
}
path = message_get_path(ea->ea_msg);
if (stat(path, &st) == -1) {
warn("stat: %s", path);
return EXPR_ERROR;
}
tim = ts->tv_sec;
date = time_format(tim, buf, sizeof(buf));
if (date == NULL)
return EXPR_ERROR;
}
delta = ea->ea_env->ev_now - tim;
switch (ex->ex_date.cmp) {
case EXPR_DATE_CMP_LT:
if (!(delta < ex->ex_date.age))
return EXPR_NOMATCH;
break;
case EXPR_DATE_CMP_GT:
if (!(delta > ex->ex_date.age))
return EXPR_NOMATCH;
break;
}
/* Populate matches, only used during dry run. */
return expr_regexec(ex, ea, "Date", date);
}
static int
expr_eval_exec(struct expr *ex, struct expr_eval_arg *ea)
{
return expr_match(ex, ea);
}
static int
expr_eval_discard(struct expr *ex, struct expr_eval_arg *ea)
{
return expr_match(ex, ea);
}
static int
expr_eval_flag(struct expr *ex, struct expr_eval_arg *ea)
{
struct match *mh;
const char *subdir;
size_t siz;
mh = match_alloc(ex, ea->ea_msg, ea->ea_arena.eternal_scope);
subdir = LIST_FIRST(ex->ex_strings)->val;
siz = sizeof(mh->mh_subdir);
if (strlcpy(mh->mh_subdir, subdir, siz) >= siz) {
warnc(ENAMETOOLONG, "%s", __func__);
return EXPR_ERROR;
}
if (matches_append(ea->ea_ml, mh))
return EXPR_ERROR;
return EXPR_MATCH;
}
static int
expr_eval_flags(struct expr *ex, struct expr_eval_arg *ea)
{
struct match *mh;
struct message *msg = ea->ea_msg;
const char *flags;
int error = 0;
flags = LIST_FIRST(ex->ex_strings)->val;
for (; *flags != '\0'; flags++) {
if (message_flags_set(message_get_flags(msg), *flags))
error = 1;
}
if (error)
return EXPR_ERROR;
mh = match_alloc(ex, msg, ea->ea_arena.eternal_scope);
if (matches_append(ea->ea_ml, mh))
return EXPR_ERROR;
return EXPR_MATCH;
}
static int
expr_eval_header(struct expr *ex, struct expr_eval_arg *ea)
{
const struct string *key;
LIST_FOREACH(key, ex->ex_strings) {
VECTOR(const char *const) values;
size_t j;
values = message_get_header(ea->ea_msg, key->val);
if (values == NULL)
continue;
for (j = 0; j < VECTOR_LENGTH(values); j++) {
int ev;
ev = expr_regexec(ex, ea, key->val, values[j]);
if (ev == EXPR_NOMATCH)
continue;
return ev; /* match or error, return */
}
}
return EXPR_NOMATCH;
}
static int
expr_eval_label(struct expr *ex, struct expr_eval_arg *ea)
{
return expr_match(ex, ea);
}
static int
expr_eval_match(struct expr *ex, struct expr_eval_arg *ea)
{
struct match *mh;
/*
* Behaves like EXPR_TYPE_AND with the exception of adding itself to the
* match list. Such match is used as a sentinel when finding matches
* eligble for interpolation.
*/
mh = match_alloc(ex, ea->ea_msg, ea->ea_arena.eternal_scope);
if (matches_append(ea->ea_ml, mh))
return EXPR_ERROR;
return expr_eval_and(ex, ea);
}
static int
expr_eval_move(struct expr *ex, struct expr_eval_arg *ea)
{
struct match *mh;
const char *maildir;
size_t siz;
maildir = LIST_FIRST(ex->ex_strings)->val;
mh = match_alloc(ex, ea->ea_msg, ea->ea_arena.eternal_scope);
siz = sizeof(mh->mh_maildir);
if (strlcpy(mh->mh_maildir, maildir, siz) >= siz) {
warnc(ENAMETOOLONG, "%s", __func__);
return EXPR_ERROR;
}
if (matches_append(ea->ea_ml, mh))
return EXPR_ERROR;
return EXPR_MATCH;
}
static int
expr_eval_neg(struct expr *ex, struct expr_eval_arg *ea)
{
struct match *mh;
assert(ex->ex_rhs == NULL);
mh = match_alloc(ex, ea->ea_msg, ea->ea_arena.eternal_scope);
if (matches_append(ea->ea_ml, mh))
return EXPR_ERROR;
switch (expr_eval(ex->ex_lhs, ea)) {
case EXPR_ERROR:
return EXPR_ERROR;
case EXPR_NOMATCH:
matches_remove(ea->ea_ml, mh);
return EXPR_MATCH;
}
/* No match, invalidate match below current expression. */
matches_remove_until(ea->ea_ml, mh);
matches_remove(ea->ea_ml, mh);
return EXPR_NOMATCH;
}
static int
expr_eval_new(struct expr *UNUSED(ex), struct expr_eval_arg *ea)
{
char buf[NAME_MAX + 1];
const char *path;
path = message_get_path(ea->ea_msg);
if (pathslice(path, buf, sizeof(buf), -2, -2) == NULL ||
strcmp(buf, "new") != 0)
return EXPR_NOMATCH;
return EXPR_MATCH;
}
static int
expr_eval_old(struct expr *UNUSED(ex), struct expr_eval_arg *ea)
{
char buf[NAME_MAX + 1];
const char *path;
if (message_flags_isset(message_get_flags(ea->ea_msg), 'S'))
return EXPR_NOMATCH;
path = message_get_path(ea->ea_msg);
if (pathslice(path, buf, sizeof(buf), -2, -2) == NULL ||
strcmp(buf, "cur") != 0)
return EXPR_NOMATCH;
return EXPR_MATCH;
}
static int
expr_eval_or(struct expr *ex, struct expr_eval_arg *ea)
{
int ev;
if ((ev = expr_eval(ex->ex_lhs, ea)) != EXPR_NOMATCH)
return ev; /* match or error, short-circuit */
return expr_eval(ex->ex_rhs, ea);
}
static int
expr_eval_pass(struct expr *ex, struct expr_eval_arg *ea)
{
struct match *mh;
mh = match_alloc(ex, ea->ea_msg, ea->ea_arena.eternal_scope);
if (matches_append(ea->ea_ml, mh))
return EXPR_ERROR;
/*
* Return no match in order stop evaluation and move on to the next
* expression within the same block. The return value is later inverted
* by expr_eval_block() in order to not continue evaluation outside of
* the current block.
*/
return EXPR_NOMATCH;
}
static int
expr_eval_reject(struct expr *ex, struct expr_eval_arg *ea)
{
return expr_match(ex, ea);
}
static int
expr_eval_stat(struct expr *ex, struct expr_eval_arg *ea)
{
struct stat st;
struct match *mh;
const char *str;
size_t siz;
int ev = EXPR_NOMATCH;
mh = match_alloc(ex, ea->ea_msg, ea->ea_arena.eternal_scope);
if (matches_append(ea->ea_ml, mh)) {
ev = EXPR_ERROR;
goto out;
}
str = LIST_FIRST(ex->ex_strings)->val;
siz = sizeof(mh->mh_path);
if (strlcpy(mh->mh_path, str, siz) >= siz) {
warnc(ENAMETOOLONG, "%s", __func__);
ev = EXPR_ERROR;
} else if (match_interpolate(mh, NULL, ea->ea_arena.eternal_scope,
ea->ea_arena.scratch)) {
ev = EXPR_ERROR;
} else if (stat(mh->mh_path, &st) == 0) {
switch (ex->ex_stat.stat) {
case EXPR_STAT_DIR:
if (S_ISDIR(st.st_mode))
ev = EXPR_MATCH;
break;
}
}
out:
matches_remove(ea->ea_ml, mh);
return ev;
}
static const char *
expr_inspect_add_header(const struct expr *ex, const struct match *UNUSED(mh),
const struct message *msg, struct arena_scope *s)
{
return arena_sprintf(s, "<add-header \"%s\" \"%s\">",
ex->ex_add_header.key,
message_get_header1(msg, ex->ex_add_header.key));
}
static const char *
expr_inspect_attachment_block(const struct expr *UNUSED(ex),
const struct match *UNUSED(mh), const struct message *UNUSED(msg),
struct arena_scope *UNUSED(s))
{
return "<attachment>";
}
static const char *
expr_inspect_break(const struct expr *UNUSED(ex),
const struct match *UNUSED(mh), const struct message *UNUSED(msg),
struct arena_scope *UNUSED(s))
{
return "<break>";
}
static const char *
expr_inspect_discard(const struct expr *UNUSED(ex),
const struct match *UNUSED(mh), const struct message *UNUSED(msg),
struct arena_scope *UNUSED(s))
{
return "<discard>";
}
static const char *
expr_inspect_exec(const struct expr *UNUSED(ex), const struct match *mh,
const struct message *UNUSED(msg), struct arena_scope *s)
{
struct buffer *bf;
size_t i;
bf = arena_buffer_alloc(s, 1 << 8);
buffer_printf(bf, "<exec");
/* Exclude NULL-terminator. */
for (i = 0; i < mh->mh_nexec - 1; i++)
buffer_printf(bf, " \"%s\"", mh->mh_exec[i]);
buffer_printf(bf, ">");
return buffer_str(bf);
}
static const char *
expr_inspect_label(const struct expr *UNUSED(ex),
const struct match *UNUSED(mh), const struct message *msg,
struct arena_scope *s)
{
return arena_sprintf(s, "<label \"%s\">",
message_get_header1(msg, "X-Label"));
}
static const char *
expr_inspect_move(const struct expr *UNUSED(ex), const struct match *mh,
const struct message *UNUSED(mh), struct arena_scope *s)
{
return arena_sprintf(s, "<move \"%s\">", mh->mh_path);
}
static const char *
expr_inspect_pass(const struct expr *UNUSED(ex),
const struct match *UNUSED(mh), const struct message *UNUSED(msg),
struct arena_scope *UNUSED(s))
{
return "<pass>";
}
static const char *
expr_inspect_reject(const struct expr *UNUSED(ex),
const struct match *UNUSED(mh), const struct message *UNUSED(msg),
struct arena_scope *UNUSED(s))