util.c
12.6 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999-2002, 2005-2008, 2010-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 <stdarg.h>
#include <sieve-priv.h>
void *
mu_sieve_alloc (size_t size)
{
void *p = malloc (size);
if (!p)
{
mu_error ("not enough memory");
abort ();
}
return p;
}
void *
mu_sieve_palloc (mu_list_t *pool, size_t size)
{
void *p = malloc (size);
if (p)
{
if (!*pool && mu_list_create (pool))
{
free (p);
return NULL;
}
mu_list_append (*pool, p);
}
return p;
}
char *
mu_sieve_pstrdup (mu_list_t *pool, const char *str)
{
size_t len;
char *p;
if (!str)
return NULL;
len = strlen (str);
p = mu_sieve_palloc (pool, len + 1);
if (p)
{
memcpy (p, str, len);
p[len] = 0;
}
return p;
}
void *
mu_sieve_prealloc (mu_list_t *pool, void *ptr, size_t size)
{
void *newptr;
if (*pool)
mu_list_remove (*pool, ptr);
newptr = realloc (ptr, size);
if (newptr)
{
if (!*pool && mu_list_create (pool))
{
free (newptr);
return NULL;
}
mu_list_append (*pool, newptr);
}
return newptr;
}
void
mu_sieve_pfree (mu_list_t *pool, void *ptr)
{
if (*pool)
mu_list_remove (*pool, ptr);
free (ptr);
}
void *
mu_sieve_malloc (mu_sieve_machine_t mach, size_t size)
{
return mu_sieve_palloc (&mach->memory_pool, size);
}
char *
mu_sieve_mstrdup (mu_sieve_machine_t mach, const char *str)
{
return mu_sieve_pstrdup (&mach->memory_pool, str);
}
void *
mu_sieve_mrealloc (mu_sieve_machine_t mach, void *ptr, size_t size)
{
return mu_sieve_prealloc (&mach->memory_pool, ptr, size);
}
void
mu_sieve_mfree (mu_sieve_machine_t mach, void *ptr)
{
mu_sieve_pfree (&mach->memory_pool, ptr);
}
static int
_destroy_item (void *item, void *data)
{
free (item);
return 0;
}
void
mu_sieve_slist_destroy (mu_list_t *plist)
{
if (!plist)
return;
mu_list_foreach (*plist, _destroy_item, NULL);
mu_list_destroy (plist);
}
mu_sieve_value_t *
mu_sieve_value_create (mu_sieve_data_type type, void *data)
{
mu_sieve_value_t *val = mu_sieve_alloc (sizeof (*val));
val->type = type;
switch (type)
{
case SVT_NUMBER:
val->v.number = * (long *) data;
break;
case SVT_STRING:
val->v.string = data;
break;
case SVT_STRING_LIST:
val->v.list = data;
break;
case SVT_TAG:
case SVT_IDENT:
val->v.string = data;
break;
case SVT_POINTER:
val->v.ptr = data;
break;
default:
mu_error ("%s", _("invalid data type"));
abort ();
}
return val;
}
mu_sieve_value_t *
mu_sieve_get_arg_untyped (mu_sieve_machine_t mach, size_t index)
{
mu_sieve_value_t *val = NULL;
int rc = mu_list_get (mach->arg_list, index, (void **)&val);
if (rc)
{
mu_sieve_error (mach, _("can't get argument %zu: %s"),
index, mu_strerror (rc));
mu_sieve_abort (mach);
}
return val;
}
mu_sieve_value_t *
mu_sieve_get_arg_optional (mu_sieve_machine_t mach, size_t index)
{
mu_sieve_value_t *val = NULL;
int rc = mu_list_get (mach->arg_list, index, (void **)&val);
if (rc == MU_ERR_NOENT)
return NULL;
else if (rc)
{
mu_sieve_error (mach, _("can't get argument %zu: %s"),
index, mu_strerror (rc));
mu_sieve_abort (mach);
}
return val;
}
int
mu_sieve_get_arg (mu_sieve_machine_t mach, size_t index,
mu_sieve_data_type type, void *ret)
{
mu_sieve_value_t *val = mu_sieve_get_arg_untyped (mach, index);
if (val->type != type)
{
mu_sieve_error (mach,
_("bad type for argument %zu: %s, instead of expected %s"),
index,
mu_sieve_type_str (val->type),
mu_sieve_type_str (type));
mu_sieve_abort (mach);
}
*(union mu_sieve_value_storage *)ret = val->v;
return 0;
}
void
mu_sieve_error (mu_sieve_machine_t mach, const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
mu_stream_printf (mach->errstream, "\033s<%d>", MU_LOG_ERROR);
if (mach->locus.mu_file)
mu_stream_printf (mach->errstream, "\033O<%d>\033f<%u>%s\033l<%u>",
MU_LOGMODE_LOCUS,
(unsigned) strlen (mach->locus.mu_file),
mach->locus.mu_file,
mach->locus.mu_line);
if (mach->identifier)
mu_stream_printf (mach->errstream, "%s: ", mach->identifier);
mu_stream_vprintf (mach->errstream, fmt, ap);
mu_stream_write (mach->errstream, "\n", 1, NULL);
va_end (ap);
}
const char *
mu_sieve_type_str (mu_sieve_data_type type)
{
switch (type)
{
case SVT_VOID:
return "void";
case SVT_NUMBER:
return "number";
case SVT_STRING:
return "string";
case SVT_STRING_LIST:
return "string-list";
case SVT_TAG:
return "tag";
case SVT_IDENT:
return "ident";
case SVT_POINTER:
return "pointer";
}
return "unknown";
}
static int
tag_printer (void *item, void *data)
{
mu_sieve_runtime_tag_t *val = item;
mu_stream_t str = data;
mu_stream_printf (str, " :%s", val->tag);
if (val->arg)
mu_i_sv_valf (str, val->arg);
return 0;
}
void
mu_i_sv_tagf (mu_stream_t str, mu_list_t taglist)
{
mu_list_foreach (taglist, tag_printer, str);
}
void
mu_i_sv_debug (mu_sieve_machine_t mach, size_t pc, const char *fmt, ...)
{
va_list ap;
if (mach->state_flags & MU_SV_SAVED_DBG_STATE)
{
unsigned severity = MU_LOG_DEBUG;
mu_stream_ioctl (mach->dbgstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_SEVERITY, &severity);
if (mach->locus.mu_file)
{
int mode = mach->dbg_mode | MU_LOGMODE_LOCUS;
mu_stream_ioctl (mach->dbgstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_LOCUS, &mach->locus);
mu_stream_ioctl (mach->dbgstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_MODE, &mode);
}
}
va_start (ap, fmt);
mu_stream_printf (mach->dbgstream, "%4zu: ", pc);
mu_stream_vprintf (mach->dbgstream, fmt, ap);
mu_stream_write (mach->dbgstream, "\n", 1, NULL);
va_end (ap);
}
void
mu_i_sv_debug_command (mu_sieve_machine_t mach,
size_t pc,
char const *what)
{
if (mach->state_flags & MU_SV_SAVED_DBG_STATE)
{
unsigned severity = MU_LOG_DEBUG;
mu_stream_ioctl (mach->dbgstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_SEVERITY, &severity);
if (mach->locus.mu_file)
{
int mode = mach->dbg_mode | MU_LOGMODE_LOCUS;
mu_stream_ioctl (mach->dbgstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_LOCUS, &mach->locus);
mu_stream_ioctl (mach->dbgstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_MODE, &mode);
}
}
mu_stream_printf (mach->dbgstream, "%4zu: %s: %s",
pc, what, mach->identifier);
mu_i_sv_tagf (mach->dbgstream, mach->tag_list);
mu_i_sv_argf (mach->dbgstream, mach->arg_list);
mu_stream_write (mach->dbgstream, "\n", 1, NULL);
}
void
mu_i_sv_trace (mu_sieve_machine_t mach, const char *what)
{
if (!mu_debug_level_p (mu_sieve_debug_handle, MU_DEBUG_TRACE4))
return;
mu_stream_printf (mach->errstream, "\033s<%d>", MU_LOG_DEBUG);
if (mach->locus.mu_file)
mu_stream_printf (mach->errstream, "\033O<%d>\033f<%u>%s\033l<%u>",
MU_LOGMODE_LOCUS,
(unsigned) strlen (mach->locus.mu_file),
mach->locus.mu_file,
mach->locus.mu_line);
mu_stream_printf (mach->errstream, "%zu: %s %s", mach->msgno, what,
mach->identifier);
mu_i_sv_tagf (mach->errstream, mach->tag_list);
mu_i_sv_argf (mach->errstream, mach->arg_list);
mu_stream_printf (mach->errstream, "\n");
}
void
mu_sieve_log_action (mu_sieve_machine_t mach, const char *action,
const char *fmt, ...)
{
va_list ap;
if (!mach->logger)
return;
mu_stream_ioctl (mach->errstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_LOCUS, &mach->locus);
va_start (ap, fmt);
mach->logger (mach, action, fmt, ap);
va_end (ap);
}
static int
tag_finder (void *item, void *data)
{
mu_sieve_runtime_tag_t *val = item;
mu_sieve_runtime_tag_t *target = data;
if (strcmp (val->tag, target->tag) == 0)
{
target->arg = val->arg;
return 1;
}
return 0;
}
int
mu_sieve_get_tag_untyped (mu_sieve_machine_t mach,
char *name, mu_sieve_value_t **ret)
{
mu_sieve_runtime_tag_t t;
t.tag = name;
if (mach->tag_list && mu_list_foreach (mach->tag_list, tag_finder, &t))
{
if (ret)
*ret = t.arg;
return 1;
}
return 0;
}
int
mu_sieve_get_tag (mu_sieve_machine_t mach,
char *name, mu_sieve_data_type type,
void *ret)
{
mu_sieve_value_t *val;
int found = mu_sieve_get_tag_untyped (mach, name, &val);
if (found)
{
if (ret)
{
if (val->type != type)
{
mu_sieve_error (mach,
_("bad type for tag %s: %s, instead of expected %s"),
name,
mu_sieve_type_str (val->type),
mu_sieve_type_str (type));
mu_sieve_abort (mach);
}
*(union mu_sieve_value_storage *)ret = val->v;
}
}
return found;
}
int
mu_sieve_vlist_do (mu_sieve_value_t *val, mu_list_action_t ac, void *data)
{
switch (val->type)
{
case SVT_STRING_LIST:
return mu_list_foreach (val->v.list, ac, data);
case SVT_STRING:
return ac (val->v.string, data);
default:
mu_error ("mu_sieve_vlist_do: unexpected list type %d", val->type);
return EINVAL;
}
}
struct comp_data
{
mu_sieve_value_t *val;
mu_sieve_comparator_t comp;
mu_sieve_relcmp_t test;
mu_sieve_retrieve_t retr;
void *data;
size_t count;
};
struct comp_data2
{
char *sample;
mu_sieve_comparator_t comp;
mu_sieve_relcmp_t test;
};
int
_comp_action2 (void *item, void *data)
{
struct comp_data2 *cp = data;
return cp->test (cp->comp (item, cp->sample), 0);
}
int
_comp_action (void *item, void *data)
{
struct comp_data *cp = data;
struct comp_data2 d;
int rc = 0;
int i;
d.comp = cp->comp;
d.test = cp->test;
for (i = 0; rc == 0 && cp->retr (item, cp->data, i, &d.sample) == 0; i++)
if (d.sample)
{
cp->count++;
rc = mu_sieve_vlist_do (cp->val, _comp_action2, &d);
free (d.sample);
}
return rc;
}
int
mu_sieve_vlist_compare (mu_sieve_value_t *a, mu_sieve_value_t *b,
mu_sieve_comparator_t comp, mu_sieve_relcmp_t test,
mu_sieve_retrieve_t retr,
void *data, size_t *count)
{
struct comp_data d;
int rc;
d.comp = comp;
d.test = test;
d.retr = retr;
d.data = data;
d.val = b;
d.count = 0;
rc = mu_sieve_vlist_do (a, _comp_action, &d);
if (count)
*count = d.count;
return rc;
}
void
mu_sieve_stream_save (mu_sieve_machine_t mach)
{
if (mach->state_flags & MU_SV_SAVED_STATE)
return;
if (mu_stream_ioctl (mach->errstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_GET_MODE, &mach->err_mode) == 0
&& mu_stream_ioctl (mach->errstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_LOCUS, &mach->err_locus) == 0)
mach->state_flags |= MU_SV_SAVED_ERR_STATE;
if (mu_stream_ioctl (mach->dbgstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_GET_MODE, &mach->dbg_mode) == 0
&& mu_stream_ioctl (mach->dbgstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_LOCUS, &mach->dbg_locus) == 0)
mach->state_flags |= MU_SV_SAVED_DBG_STATE;
mach->state_flags |= MU_SV_SAVED_STATE;
}
void
mu_sieve_stream_restore (mu_sieve_machine_t mach)
{
if (!(mach->state_flags & MU_SV_SAVED_STATE))
return;
if (mach->state_flags & MU_SV_SAVED_ERR_STATE)
{
mu_stream_ioctl (mach->errstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_MODE, &mach->err_mode);
mu_stream_ioctl (mach->errstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_LOCUS, &mach->err_locus);
}
if (mach->dbgstream != mach->errstream
&& (mach->state_flags & MU_SV_SAVED_DBG_STATE))
{
mu_stream_ioctl (mach->dbgstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_MODE, &mach->dbg_mode);
mu_stream_ioctl (mach->dbgstream, MU_IOCTL_LOGSTREAM,
MU_IOCTL_LOGSTREAM_SET_LOCUS, &mach->dbg_locus);
}
mach->state_flags = 0;
}