prog.c
8.06 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999-2002, 2004-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>
void
mu_i_sv_code (struct mu_sieve_machine *mach, sieve_op_t op)
{
if (mach->pc >= mach->progsize)
{
mu_i_sv_2nrealloc (mach, (void**) &mach->prog, &mach->progsize,
sizeof mach->prog[0]);
}
mach->prog[mach->pc++] = op;
}
static int
file_eq (char const *a, char const *b)
{
if (a)
return b ? (strcmp (a, b) == 0) : 1;
return b ? 0 : 1;
}
/* FIXME: 1. Only beg is stored
2. mu_col is not used
*/
int
mu_i_sv_locus (struct mu_sieve_machine *mach, struct mu_locus_range *lr)
{
if (!file_eq (mach->locus.mu_file, lr->beg.mu_file))
{
mu_i_sv_code (mach, (sieve_op_t) _mu_i_sv_instr_source);
mu_i_sv_code (mach, (sieve_op_t) lr->beg.mu_file);
}
if (mach->locus.mu_line != lr->beg.mu_line)
{
mu_i_sv_code (mach, (sieve_op_t) _mu_i_sv_instr_line);
mu_i_sv_code (mach, (sieve_op_t) lr->beg.mu_line);
}
mach->locus = lr->beg;
return 0;
}
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
{
struct mu_sieve_machine *mach;
struct mu_sieve_node *node;
};
static int
_run_checker (void *item, void *data)
{
struct check_arg *arg = data;
mu_sieve_machine_t mach = arg->mach;
struct mu_sieve_node *node = arg->node;
mu_sieve_tag_checker_t checker = item;
int rc;
mach->comparator = node->v.command.comparator;
mach->argstart = node->v.command.argstart;
mach->argcount = node->v.command.argcount;
mach->tagcount = node->v.command.tagcount;
mach->identifier = node->v.command.reg->name;
rc = checker (arg->mach);
/* checker is allowed to alter these values */
node->v.command.comparator = mach->comparator;
node->v.command.argcount = mach->argcount;
node->v.command.tagcount = mach->tagcount;
mach->argstart = 0;
mach->argcount = 0;
mach->tagcount = 0;
mach->identifier = NULL;
return rc;
}
void
mu_i_sv_lint_command (struct mu_sieve_machine *mach,
struct mu_sieve_node *node)
{
size_t i;
mu_sieve_register_t *reg = node->v.command.reg;
mu_sieve_value_t *start = mach->valspace + node->v.command.argstart;
mu_list_t chk_list = NULL;
mu_sieve_data_type *exp_arg;
int opt_args = 0;
int rc, err = 0;
static mu_sieve_data_type empty[] = { SVT_VOID };
exp_arg = reg->req_args ? reg->req_args : empty;
/* Pass 1: consolidation */
for (i = 0; i < node->v.command.argcount; i++)
{
mu_sieve_value_t *val = start + i;
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_diag_at_locus (MU_LOG_ERROR, &mach->locus,
_("invalid tag name `%s' for `%s'"),
val->v.string, reg->name);
mu_i_sv_error (mach);
err = 1;
break;
}
node->v.command.tagcount++;
if (tag->argtype == SVT_VOID)
{
val->type = SVT_VOID;
val->tag = val->v.string;
val->v.string = NULL;
}
else
{
if (i + 1 == node->v.command.argcount)
{
mu_diag_at_locus (MU_LOG_ERROR, &mach->locus,
_("required argument for tag %s is missing"),
tag->name);
mu_i_sv_error (mach);
err = 1;
break;
}
val[1].tag = val->v.string;
*val = val[1];
memmove (val + 1, val + 2,
(node->v.command.argcount - i - 2) * sizeof (val[0]));
mach->valcount--;
node->v.command.argcount--;
if (val->type != tag->argtype)
{
mu_diag_at_locus (MU_LOG_ERROR, &mach->locus,
_("type mismatch in argument to "
"tag `%s'"),
tag->name);
mu_diag_at_locus (MU_LOG_ERROR, &mach->locus,
_("expected %s but passed %s"),
mu_sieve_type_str (tag->argtype),
mu_sieve_type_str (val->type));
mu_i_sv_error (mach);
err = 1;
break;
}
}
if (cf)
{
if (!chk_list && (rc = mu_list_create (&chk_list)))
{
mu_diag_at_locus (MU_LOG_ERROR, &mach->locus,
_("cannot create check list: %s"),
mu_strerror (rc));
mu_i_sv_error (mach);
err = 1;
break;
}
//FIXME
if (mu_list_foreach (chk_list, _compare_ptr, cf) == 0)
mu_list_append (chk_list, cf);
}
}
else
{
if (*exp_arg == SVT_VOID)
{
if (reg->opt_args)
{
exp_arg = reg->opt_args;
opt_args = 1;
}
else
{
mu_diag_at_locus (MU_LOG_ERROR, &mach->locus,
_("too many arguments in call to `%s'"),
reg->name);
mu_i_sv_error (mach);
err = 1;
break;
}
}
if (*exp_arg != val->type)
{
if (*exp_arg == SVT_STRING_LIST && val->type == SVT_STRING)
/* compatible types */;
else
{
mu_diag_at_locus (MU_LOG_ERROR, &mach->locus,
_("type mismatch in argument %lu to `%s'"),
(unsigned long) (exp_arg - reg->req_args + 1),
reg->name);
mu_diag_at_locus (MU_LOG_ERROR, &mach->locus,
_("expected %s but passed %s"),
mu_sieve_type_str (*exp_arg),
mu_sieve_type_str (val->type));
mu_i_sv_error (mach);
err = 1;
break;
}
}
exp_arg++;
}
}
if (!err && !opt_args && *exp_arg != SVT_VOID)
{
mu_diag_at_locus (MU_LOG_ERROR, &mach->locus,
_("too few arguments in call to `%s'"),
reg->name);
mu_i_sv_error (mach);
err = 1;
}
if (err)
{
mu_list_destroy (&chk_list);
return;
}
if (node->v.command.tagcount)
{
/* Move tags to the end of the list */
for (i = 1; i < node->v.command.argcount; i++)
{
int j;//FIXME
mu_sieve_value_t tmp = start[i];
for (j = i - 1; j >= 0; j--)
{
if (!tmp.tag && start[j].tag)
start[j + 1] = start[j];
else
break;
}
start[j + 1] = tmp;
}
}
node->v.command.argcount -= node->v.command.tagcount;
if (chk_list)
{
struct check_arg chk_arg;
chk_arg.mach = mach;
chk_arg.node = node;
err = mu_list_foreach (chk_list, _run_checker, &chk_arg);
}
}
static void
sv_code_command (struct mu_sieve_machine *mach,
struct mu_sieve_node *node)
{
mu_i_sv_code (mach, (sieve_op_t) node->v.command.reg->handler);
mu_i_sv_code (mach, (sieve_op_t) node->v.command.argstart);
mu_i_sv_code (mach, (sieve_op_t) node->v.command.argcount);
mu_i_sv_code (mach, (sieve_op_t) node->v.command.tagcount);
mu_i_sv_code (mach, (sieve_op_t) (char*) node->v.command.reg->name);
mu_i_sv_code (mach, (sieve_op_t) node->v.command.comparator);
}
void
mu_i_sv_code_action (struct mu_sieve_machine *mach,
struct mu_sieve_node *node)
{
mu_i_sv_code (mach, (sieve_op_t) _mu_i_sv_instr_action);
sv_code_command (mach, node);
}
void
mu_i_sv_code_test (struct mu_sieve_machine *mach, struct mu_sieve_node *node)
{
mu_i_sv_code (mach, (sieve_op_t) _mu_i_sv_instr_test);
sv_code_command (mach, node);
}