prog.c
9.74 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2004,
2005, 2006, 2007, 2008, 2009 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-priv.h>
int
mu_sv_code (sieve_op_t *op)
{
if (mu_sieve_machine->pc >= mu_sieve_machine->progsize)
{
size_t newsize = mu_sieve_machine->progsize + SIEVE_CODE_INCR;
sieve_op_t *newprog = mu_sieve_mrealloc (mu_sieve_machine,
mu_sieve_machine->prog,
newsize *
sizeof mu_sieve_machine->prog[0]);
if (!newprog)
{
mu_sv_compile_error (&mu_sieve_locus, _("not enough memory"));
return 1;
}
mu_sieve_machine->prog = newprog;
mu_sieve_machine->progsize = newsize;
}
mu_sieve_machine->prog[mu_sieve_machine->pc++] = *op;
return 0;
}
int
mu_sv_code_instr (sieve_instr_t instr)
{
sieve_op_t op;
op.instr = instr;
return mu_sv_code (&op);
}
int
mu_sv_code_handler (mu_sieve_handler_t handler)
{
sieve_op_t op;
op.handler = handler;
return mu_sv_code (&op);
}
int
mu_sv_code_list (mu_list_t list)
{
sieve_op_t op;
op.list = list;
return mu_sv_code (&op);
}
int
mu_sv_code_number (long num)
{
sieve_op_t op;
op.number = num;
return mu_sv_code (&op);
}
int
mu_sv_code_string (const char *string)
{
sieve_op_t op;
op.string = string;
return mu_sv_code (&op);
}
mu_sieve_tag_def_t *
find_tag (mu_sieve_tag_group_t *taglist, char *tagname,
mu_sieve_tag_checker_t *checker)
{
*checker = NULL;
if (!taglist)
return NULL;
for (; taglist->tags; taglist++)
{
mu_sieve_tag_def_t *def;
for (def = taglist->tags; def->name; def++)
if (strcmp (def->name, tagname) == 0)
{
*checker = taglist->checker;
return def;
}
}
return NULL;
}
static int
_compare_ptr (void *item, void *data)
{
return item == data;
}
struct check_arg {
const char *name;
mu_list_t args;
mu_list_t tags;
};
static int
_run_checker (void *item, void *data)
{
struct check_arg *arg = data;
return (*(mu_sieve_tag_checker_t)item) (arg->name, arg->tags, arg->args);
}
int
mu_sv_code_command (mu_sieve_register_t *reg, mu_list_t arglist)
{
mu_iterator_t itr;
mu_list_t arg_list = NULL;
mu_list_t tag_list = NULL;
mu_list_t chk_list = NULL;
mu_sieve_data_type *exp_arg;
int rc, err = 0;
static mu_sieve_data_type empty[] = { SVT_VOID };
if (mu_sv_code_handler (reg->handler))
return 1;
exp_arg = reg->req_args ? reg->req_args : empty;
if (arglist)
{
rc = mu_list_get_iterator (arglist, &itr);
if (rc)
{
mu_sv_compile_error (&mu_sieve_locus,
_("cannot create iterator: %s"),
mu_strerror (rc));
return 1;
}
for (mu_iterator_first (itr); !mu_iterator_is_done (itr); mu_iterator_next (itr))
{
mu_sieve_value_t *val;
mu_sieve_runtime_tag_t tagrec, *tagptr;
mu_iterator_current (itr, (void **)&val);
if (val->type == SVT_TAG)
{
mu_sieve_tag_checker_t cf;
mu_sieve_tag_def_t *tag = find_tag (reg->tags, val->v.string, &cf);
if (!tag)
{
mu_sv_compile_error (&mu_sieve_locus,
_("invalid tag name `%s' for `%s'"),
val->v.string, reg->name);
err = 1;
break;
}
if (!tag_list && (rc = mu_list_create (&tag_list)))
{
mu_sv_compile_error (&mu_sieve_locus,
_("cannot create tag list: %s"),
mu_strerror (rc));
err = 1;
break;
}
tagrec.tag = tag->name;
if (tag->argtype != SVT_VOID)
{
mu_iterator_next (itr);
if (mu_iterator_is_done (itr))
{
mu_sv_compile_error (&mu_sieve_locus,
_("required argument for tag %s is missing"),
tag->name);
err = 1;
break;
}
mu_iterator_current (itr, (void **)&tagrec.arg);
if (tagrec.arg->type != tag->argtype)
{
mu_sv_compile_error (&mu_sieve_locus,
_("type mismatch in argument to "
"tag `%s'"),
tag->name);
mu_sv_compile_error (&mu_sieve_locus,
_("expected %s but passed %s"),
mu_sieve_type_str (tag->argtype),
mu_sieve_type_str
(tagrec.arg->type));
err = 1;
break;
}
}
else
tagrec.arg = NULL;
tagptr = mu_sieve_malloc (mu_sieve_machine, sizeof (*tagptr));
*tagptr = tagrec;
mu_list_append (tag_list, tagptr);
if (cf)
{
if (!chk_list && (rc = mu_list_create (&chk_list)))
{
mu_sv_compile_error (&mu_sieve_locus,
_("cannot create check list: %s"),
mu_strerror (rc));
err = 1;
break;
}
if (mu_list_do (chk_list, _compare_ptr, cf) == 0)
mu_list_append (chk_list, cf);
}
}
else if (*exp_arg == SVT_VOID)
{
mu_sv_compile_error (&mu_sieve_locus,
_("too many arguments in call to `%s'"),
reg->name);
err = 1;
break;
}
else
{
if (*exp_arg != val->type)
{
if (*exp_arg == SVT_STRING_LIST && val->type == SVT_STRING)
{
mu_list_t list;
mu_list_create (&list);
mu_list_append (list, val->v.string);
mu_sieve_mfree (mu_sieve_machine, val);
val = mu_sieve_value_create (SVT_STRING_LIST, list);
}
else
{
mu_sv_compile_error (&mu_sieve_locus,
_("type mismatch in argument %lu to `%s'"),
(unsigned long) (exp_arg - reg->req_args + 1),
reg->name);
mu_sv_compile_error (&mu_sieve_locus,
_("expected %s but passed %s"),
mu_sieve_type_str (*exp_arg),
mu_sieve_type_str (val->type));
err = 1;
break;
}
}
if (!arg_list && (rc = mu_list_create (&arg_list)))
{
mu_sv_compile_error (&mu_sieve_locus,
_("cannot create arg list: %s"),
mu_strerror (rc));
err = 1;
break;
}
mu_list_append (arg_list, val);
exp_arg++;
}
}
mu_iterator_destroy (&itr);
}
if (!err)
{
if (*exp_arg != SVT_VOID)
{
mu_sv_compile_error (&mu_sieve_locus,
_("too few arguments in call to `%s'"),
reg->name);
err = 1;
}
if (chk_list)
{
struct check_arg chk_arg;
chk_arg.name = reg->name;
chk_arg.tags = tag_list;
chk_arg.args = arg_list;
err = mu_list_do (chk_list, _run_checker, &chk_arg);
}
}
if (!err)
err = mu_sv_code_list (arg_list)
|| mu_sv_code_list (tag_list)
|| mu_sv_code_string (reg->name);
if (err)
{
mu_list_destroy (&arg_list);
mu_list_destroy (&tag_list);
mu_list_destroy (&chk_list);
}
return err;
}
int
mu_sv_code_source (const char *name)
{
char *s;
if (mu_list_locate (mu_sieve_machine->source_list,
(void*) name, (void **) &s))
{
s = mu_sieve_mstrdup (mu_sieve_machine, name);
mu_list_append (mu_sieve_machine->source_list, s);
}
return mu_sv_code_instr (_mu_sv_instr_source)
|| mu_sv_code_string (s);
}
int
mu_sv_code_line (size_t line)
{
sieve_op_t op;
op.line = line;
return mu_sv_code_instr (_mu_sv_instr_line)
|| mu_sv_code (&op);
}
static int sieve_source_changed;
void
mu_sv_change_source ()
{
sieve_source_changed = 1;
}
static int
sieve_check_source_changed ()
{
if (sieve_source_changed)
{
sieve_source_changed = 0;
return mu_sv_code_source (mu_sieve_locus.source_file);
}
return 0;
}
int
mu_sv_code_action (mu_sieve_register_t *reg, mu_list_t arglist)
{
return sieve_check_source_changed ()
|| mu_sv_code_line (mu_sieve_locus.source_line)
|| mu_sv_code_instr (_mu_sv_instr_action)
|| mu_sv_code_command (reg, arglist);
}
int
mu_sv_code_test (mu_sieve_register_t *reg, mu_list_t arglist)
{
return sieve_check_source_changed ()
|| mu_sv_code_line (mu_sieve_locus.source_line)
|| mu_sv_code_instr (_mu_sv_instr_test)
|| mu_sv_code_command (reg, arglist);
}
void
mu_sv_code_anyof (size_t start)
{
size_t end = mu_sieve_machine->pc;
while (mu_sieve_machine->prog[start+1].pc != 0)
{
size_t next = mu_sieve_machine->prog[start+1].pc;
mu_sieve_machine->prog[start].instr = _mu_sv_instr_brnz;
mu_sieve_machine->prog[start+1].pc = end - start - 2;
start = next;
}
mu_sieve_machine->prog[start].instr = _mu_sv_instr_nop;
mu_sieve_machine->prog[start+1].instr = _mu_sv_instr_nop;
}
void
mu_sv_code_allof (size_t start)
{
size_t end = mu_sieve_machine->pc;
while (mu_sieve_machine->prog[start+1].pc != 0)
{
size_t next = mu_sieve_machine->prog[start+1].pc;
mu_sieve_machine->prog[start+1].pc = end - start - 2;
start = next;
}
mu_sieve_machine->prog[start].instr = _mu_sv_instr_nop;
mu_sieve_machine->prog[start+1].instr = _mu_sv_instr_nop;
}