vartab.c
6.83 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2007, 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 <stdlib.h>
#include <string.h>
#include <mailutils/assoc.h>
#include <mailutils/stream.h>
#include <mailutils/errno.h>
#include <mailutils/iterator.h>
#define MU_SUPPRESS_DEPRECATION
#include <mailutils/vartab.h>
struct _mu_vartab
{
mu_assoc_t assoc;
mu_stream_t stream;
char *buf;
size_t bufsize;
};
#define MU_VARDEFN_STATIC 0x1
#define MU_VARDEFN_FUNC 0x2
struct vardefn
{
int flags;
char *value;
mu_var_expansion_fp fun;
mu_var_free_fp free;
void *data;
};
int
mu_vartab_create (mu_vartab_t *pvar)
{
int rc;
struct _mu_vartab *p = calloc (1, sizeof *p);
if (!p)
return ENOMEM;
rc = mu_assoc_create (&p->assoc, sizeof (struct vardefn), 0);
if (rc)
{
free (p);
return rc;
}
*pvar = p;
return 0;
}
static void
vardefn_free (struct vardefn *vd)
{
if (vd->value)
{
if (vd->free)
vd->free (vd->data, vd->value);
else if (!(vd->flags & MU_VARDEFN_STATIC))
free (vd->value);
}
memset (vd, 0, sizeof *vd);
}
int
mu_vartab_destroy (mu_vartab_t *pvar)
{
int rc;
mu_vartab_t var = *pvar;
mu_iterator_t itr;
if (!var)
return EINVAL;
rc = mu_assoc_get_iterator (var->assoc, &itr);
if (rc)
return rc;
for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
mu_iterator_next (itr))
{
struct vardefn *vd;
mu_iterator_current (itr, (void**)&vd);
vardefn_free (vd);
}
mu_iterator_destroy (&itr);
mu_assoc_destroy (&var->assoc);
mu_stream_destroy (&var->stream);
free (var->buf);
free (var);
*pvar = NULL;
return 0;
}
int
mu_vartab_define (mu_vartab_t var, const char *name, const char *value,
int isstatic)
{
int rc;
struct vardefn *vd;
if (!var)
return EINVAL;
rc = mu_assoc_ref_install (var->assoc, name, (void **) &vd);
if (rc == MU_ERR_EXISTS)
vardefn_free (vd);
else if (rc != 0)
return rc;
if (isstatic)
{
vd->flags = MU_VARDEFN_STATIC;
vd->value = (char*) value;
}
else
{
vd->flags = 0;
vd->value = strdup (value);
if (!vd->value)
return ENOMEM;
}
return 0;
}
int
mu_vartab_define_exp (mu_vartab_t var, const char *name,
mu_var_expansion_fp fun, mu_var_free_fp free,
void *data)
{
int rc;
struct vardefn *vd;
if (!var)
return EINVAL;
rc = mu_assoc_ref_install (var->assoc, name, (void **) &vd);
if (rc == MU_ERR_EXISTS)
vardefn_free (vd);
else if (rc != 0)
return rc;
vd->flags = MU_VARDEFN_FUNC;
vd->fun = fun;
vd->free = free;
vd->data = data;
return 0;
}
int
mu_vartab_count (mu_vartab_t vt, size_t *pcount)
{
if (!vt)
return EINVAL;
return mu_assoc_count (vt->assoc, pcount);
}
static int
vardefn_expand (const char *name, struct vardefn *vd, const char **pvalue)
{
if (!vd->value)
{
if (vd->fun)
{
int rc = vd->fun (name, vd->data, &vd->value);
if (rc)
return rc;
}
else
return EINVAL;
}
*pvalue = vd->value;
return 0;
}
int
mu_vartab_getvar (mu_vartab_t vt, const char *name, const char **pvalue)
{
struct vardefn *vdefn;
if (!vt)
return EINVAL;
vdefn = mu_assoc_ref (vt->assoc, name);
if (!vdefn)
return MU_ERR_NOENT;
return vardefn_expand (name, vdefn, pvalue);
}
static char *
copy_name (mu_vartab_t vt, const char *name, size_t len)
{
if (len + 1 > vt->bufsize)
{
char *p = realloc (vt->buf, len + 1);
if (!p)
return NULL;
vt->buf = p;
vt->bufsize = len + 1;
}
memcpy (vt->buf, name, len);
vt->buf[len] = 0;
return vt->buf;
}
int
mu_vartab_expand (mu_vartab_t vt, const char *str, char **pres)
{
int rc;
mu_off_t size;
const char *p;
if (!vt)
return EINVAL;
if (!vt->stream)
{
rc = mu_memory_stream_create (&vt->stream, 0);
if (rc)
return rc;
}
else
mu_stream_truncate (vt->stream, 0);
mu_stream_seek (vt->stream, 0, MU_SEEK_SET, NULL);
for (p = str; *p; )
{
if (*p == '$')
{
switch (*++p)
{
case '$':
mu_stream_write (vt->stream, str, p - str, NULL);
str = p + 1;
p = str + 1;
break;
case '{':
{
const char *e = strchr (p + 1, '}');
if (e)
{
const char *pvalue;
size_t len = e - p - 1;
char *name = copy_name (vt, p + 1, len);
rc = mu_vartab_getvar (vt, name, &pvalue);
if (rc == 0)
{
mu_stream_write (vt->stream, str, p - str - 1, NULL);
mu_stream_write (vt->stream, pvalue, strlen (pvalue),
NULL);
str = e + 1;
p = str + 1;
}
else if (rc == MU_ERR_NOENT)
p = e + 1;
else
return rc;
}
else
p++;
}
break;
default:
{
char *name = copy_name (vt, p, 1);
const char *pvalue;
rc = mu_vartab_getvar (vt, name, &pvalue);
if (rc == 0)
{
mu_stream_write (vt->stream, str, p - str - 1, NULL);
mu_stream_write (vt->stream, pvalue, strlen (pvalue),
NULL);
str = p + 1;
p = str + 1;
}
else if (rc == MU_ERR_NOENT)
p++;
else
return rc;
}
break;
}
}
else if (*p == '%')
{
/* allow `%' as prefix for single-character entities, for
compatibility with v. prior to 1.2.91 */
if (*++p == '%')
{
mu_stream_write (vt->stream, str, p - str, NULL);
str = p + 1;
p = str + 1;
}
else
{
char *name = copy_name (vt, p, 1);
const char *pvalue;
rc = mu_vartab_getvar (vt, name, &pvalue);
if (rc == 0)
{
mu_stream_write (vt->stream, str, p - str - 1, NULL);
mu_stream_write (vt->stream, pvalue, strlen (pvalue),
NULL);
str = p + 1;
p = str + 1;
}
else if (rc == MU_ERR_NOENT)
p++;
else
return rc;
}
}
else
p++;
}
if (p > str)
mu_stream_write (vt->stream, str, p - str, NULL);
mu_stream_size (vt->stream, &size);
*pres = malloc (size + 1);
if (!*pres)
return ENOMEM;
mu_stream_seek (vt->stream, 0, MU_SEEK_SET, NULL);
mu_stream_read (vt->stream, *pres, size, NULL);
(*pres)[size] = 0;
return 0;
}