summaryrefslogtreecommitdiff
path: root/libsieve/util.c
blob: 2fadeeb55b03e67cef25384c1e571e01ae192bfc (plain)
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
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.

   This program 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 2, or (at your option)
   any later version.

   This program 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 program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif  

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sieve.h>

void *
sieve_alloc (size_t size)
{
  void *p = malloc (size);
  if (!p)
    {
      mu_error ("not enough memory");
      abort ();
    }
  return p;
}

void *
sieve_palloc (list_t *pool, size_t size)
{
  void *p = malloc (size);
  if (p)
    {
      if (!*pool && list_create (pool))
	{
	  free (p);
	  return NULL;
	}
      list_append (*pool, p);
    }
  return p;
}

char *
sieve_pstrdup (list_t *pool, const char *str)
{
  size_t len;
  char *p;
  
  if (!str)
    return NULL;
  len = strlen (str);
  p = sieve_palloc (pool, len + 1);
  if (p)
    {
      memcpy (p, str, len);
      p[len] = 0;
    }
  return p;
}

void *
sieve_prealloc (list_t *pool, void *ptr, size_t size)
{
  void *newptr;
  
  if (*pool)
    list_remove (*pool, ptr);

  newptr = realloc (ptr, size);
  if (newptr)
    {
      if (!*pool && list_create (pool))
	{
	  free (newptr);
	  return NULL;
	}
      list_append (*pool, newptr);
    }
  return newptr;
}

void
sieve_pfree (list_t *pool, void *ptr)
{

  if (*pool)
    list_remove (*pool, ptr);
  free (ptr);
}  

void
sieve_slist_destroy (list_t *plist)
{
  iterator_t itr;

  if (!plist || iterator_create (&itr, *plist))
    return;

  for (iterator_first (itr); !iterator_is_done (itr); iterator_next (itr))
    {
      char *s;
      iterator_current (itr, (void **)&s);
      free (s);
    }
  iterator_destroy (&itr);
  list_destroy (plist);
}

sieve_value_t *
sieve_value_create (sieve_data_type type, void *data)
{
  sieve_value_t *val = 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_VALUE_LIST:
    case SVT_STRING_LIST:
      val->v.list = data;
      
    case SVT_TAG:
    case SVT_IDENT:
      val->v.string = data;
      break;

    default:
      sieve_error ("Invalid data type");
      abort ();
    }
  return val;
}
    
void
sieve_error (const char *fmt, ...)
{
  va_list ap;

  va_start (ap, fmt);
  sieve_error_count++;
  sieve_machine->error_printer (sieve_machine->data, fmt, ap);
  va_end (ap);
}

void
sieve_debug_internal (sieve_printf_t printer, void *data, const char *fmt, ...)
{
  va_list ap;

  va_start (ap, fmt);
  printer (data, fmt, ap);
  va_end (ap);
}

void
sieve_debug (sieve_machine_t *mach, const char *fmt, ...)
{
  va_list ap;

  va_start (ap, fmt);
  mach->debug_printer (mach->data, fmt, ap);
  va_end (ap);
}

int
_sieve_default_error_printer (void *unused, const char *fmt, va_list ap)
{
  return mu_verror (fmt, ap);
}

char *
sieve_type_str (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_VALUE_LIST:
      return "value-list";
    }

  return "unknown";
}

struct debug_data {
  sieve_printf_t printer;
  void *data;
};

static int
string_printer (char *s, struct debug_data *dbg)
{
  sieve_debug_internal (dbg->printer, dbg->data, "\"%s\" ", s);
  return 0;
}

static int
value_printer (sieve_value_t *val, struct debug_data *dbg)
{
  sieve_print_value (val, dbg->printer, dbg->data);
  sieve_debug_internal (dbg->printer, dbg->data, " ");
  return 0;
}

void
sieve_print_value (sieve_value_t *val, sieve_printf_t printer, void *data)
{
  struct debug_data dbg;

  dbg.printer = printer;
  dbg.data = data;

  sieve_debug_internal (printer, data, "%s(", sieve_type_str (val->type));
  switch (val->type)
    {
    case SVT_VOID:
      break;
      
    case SVT_NUMBER:
      sieve_debug_internal (printer, data, "%ld", val->v.number);
      break;
      
    case SVT_TAG:
    case SVT_IDENT:
    case SVT_STRING:
      sieve_debug_internal (printer, data, "%s", val->v.string);
      break;
      
    case SVT_STRING_LIST:
      list_do (val->v.list, (list_action_t*) string_printer, &dbg);
      break;

    case SVT_VALUE_LIST:
      list_do (val->v.list, (list_action_t*) value_printer, &dbg);
    }
  sieve_debug_internal (printer, data, ")");
} 

void
sieve_print_value_list (list_t list, sieve_printf_t printer, void *data)
{
  sieve_value_t val;
  
  val.type = SVT_VALUE_LIST;
  val.v.list = list;
  sieve_print_value (&val, printer, data);
}

static int
tag_printer (sieve_runtime_tag_t *val, struct debug_data *dbg)
{
  sieve_debug_internal (dbg->printer, dbg->data, "%d", val->tag);
  if (val->arg)
    {
      sieve_debug_internal (dbg->printer, dbg->data, "(");
      sieve_print_value (val->arg, dbg->printer, dbg->data);
      sieve_debug_internal (dbg->printer, dbg->data, ")");
    }
  sieve_debug_internal (dbg->printer, dbg->data, " ");
}

void
sieve_print_tag_list (list_t list, sieve_printf_t printer, void *data)
{
  struct debug_data dbg;

  dbg.printer = printer;
  dbg.data = data;
  list_do (list, (list_action_t*) tag_printer, &dbg);
}

void
sieve_set_debug (sieve_machine_t *mach, sieve_printf_t debug, int level)
{
  mach->debug_printer = debug;
  mach->debug_level = level;
}
  

Return to:

Send suggestions and report system problems to the System administrator.