str_to_c.c
6.39 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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 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/>. */
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <inttypes.h>
#include <limits.h>
#include <string.h>
#include <mailutils/cidr.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/util.h>
#include <mailutils/alloc.h>
#include <mailutils/cctype.h>
#include <mailutils/io.h>
#include <mailutils/nls.h>
typedef int (*str_to_c_t) (void *tgt, char const *string, char **errmsg);
static int
to_string (void *tgt, char const *string, char **errmsg)
{
char **cptr = tgt;
if (string)
{
*cptr = mu_strdup (string);
if (!*cptr)
return errno;
}
else
*cptr = NULL;
return 0;
}
#define STR_TO_FUN to_short
#define STR_TO_TYPE signed short
#define STR_TO_MIN SHRT_MIN
#define STR_TO_MAX SHRT_MAX
#include "to_sn.c"
#define STR_TO_FUN to_ushort
#define STR_TO_TYPE unsigned short
#define STR_TO_MAX USHRT_MAX
#include "to_un.c"
#define STR_TO_FUN to_int
#define STR_TO_TYPE signed int
#define STR_TO_MIN INT_MIN
#define STR_TO_MAX INT_MAX
#include "to_sn.c"
#define STR_TO_FUN to_uint
#define STR_TO_TYPE unsigned int
#define STR_TO_MAX UINT_MAX
#include "to_un.c"
#define STR_TO_FUN to_long
#define STR_TO_TYPE signed long
#define STR_TO_MIN LONG_MIN
#define STR_TO_MAX LONG_MAX
#include "to_sn.c"
#define STR_TO_FUN to_ulong
#define STR_TO_TYPE unsigned long
#define STR_TO_MAX ULONG_MAX
#include "to_un.c"
#define STR_TO_FUN to_size_t
#define STR_TO_TYPE size_t
#define STR_TO_MAX ((size_t)-1)
#include "to_un.c"
static int
time_multiplier (const char *str, unsigned *m, unsigned *plen)
{
static struct timetab
{
char *name;
unsigned mul;
} tab[] = {
{ "seconds", 1 },
{ "minutes", 60 },
{ "hours", 60*60 },
{ "days", 24*60*60 },
{ "weeks", 7*24*60*60 },
{ "months", 31*7*24*60*60 },
{ NULL }
};
struct timetab *p;
int slen;
for (slen = 0; str[slen]; slen++)
if (mu_isspace (str[slen]))
break;
for (p = tab; p->name; p++)
{
if (p->name[0] == mu_tolower (str[0]))
{
int nlen = strlen (p->name);
if (nlen > slen)
nlen = slen;
if (strncasecmp (p->name, str, nlen) == 0) {
*m = p->mul;
if (plen)
*plen = nlen;
return 0;
}
}
}
return 1;
}
static int
to_time_t (void *tgt, char const *string, char **errmsg)
{
time_t *ptr = tgt;
int rc = 0;
time_t interval = 0;
while (*string)
{
char *p;
unsigned long n;
unsigned mul, len;
while (*string && mu_isspace (*string))
string++;
if (!mu_isdigit (*string) && time_multiplier (string, &mul, &len) == 0)
{
n = 1;
string += len;
}
else
{
n = strtoul (string, &p, 10);
if (*p && !mu_isspace (*p))
{
string = p;
rc = 1;
break;
}
while (*p && mu_isspace (*p))
p++;
string = p;
if (*string)
{
if ((rc = time_multiplier (string, &mul, &len)))
break;
string += len;
}
else
mul = 1;
}
interval += n * mul;
}
if (rc)
{
if (errmsg)
mu_asprintf (errmsg, _("invalid time specification near %s"), string);
return EINVAL;
}
*ptr = interval;
return 0;
}
static int
to_bool (void *tgt, char const *string, char **errmsg)
{
int *ptr = tgt;
if (strcmp (string, "yes") == 0
|| strcmp (string, "on") == 0
|| strcmp (string, "t") == 0
|| strcmp (string, "true") == 0
|| strcmp (string, "1") == 0)
*ptr = 1;
else if (strcmp (string, "no") == 0
|| strcmp (string, "off") == 0
|| strcmp (string, "nil") == 0
|| strcmp (string, "false") == 0
|| strcmp (string, "0") == 0)
*ptr = 0;
else
return EINVAL;
return 0;
}
#if 0
static int
to_ipv4 (void *tgt, char const *string, char **errmsg)
{
struct in_addr *ptr = tgt;
struct in_addr addr;
if (inet_aton (string, &addr) == 0)
{
mu_diag_at_locus (MU_LOG_ERROR, &mu_cfg_locus, _("not an IPv4"));
mu_cfg_error_count++;
return 1;
}
addr.s_addr = ntohl (addr.s_addr);
*ptr = addr;
return 0;
}
#endif
static int
to_cidr (void *tgt, char const *string, char **errmsg)
{
struct mu_cidr *ptr = tgt;
return mu_cidr_from_string (ptr, string);
}
static int
to_incr (void *tgt, char const *string, char **errmsg)
{
++*(int*)tgt;
return 0;
}
static str_to_c_t str_to_c[] = {
[mu_c_string] = to_string,
[mu_c_short] = to_short,
[mu_c_ushort] = to_ushort,
[mu_c_int] = to_int,
[mu_c_uint] = to_uint,
[mu_c_long] = to_long,
[mu_c_ulong] = to_ulong,
[mu_c_size] = to_size_t,
/* FIXME [mu_c_off] = { to_off, generic_dealloc }, */
[mu_c_time] = to_time_t,
[mu_c_bool] = to_bool,
/* FIXME [mu_c_ipv4] = to_ipv4, */
[mu_c_cidr] = to_cidr,
/* FIXME [mu_c_host] = { to_host, generic_dealloc } */
[mu_c_incr] = to_incr
};
char const *mu_c_type_str[] = {
[mu_c_string] = "mu_c_string",
[mu_c_short] = "mu_c_short",
[mu_c_ushort] = "mu_c_ushort",
[mu_c_int] = "mu_c_int",
[mu_c_uint] = "mu_c_uint",
[mu_c_long] = "mu_c_long",
[mu_c_ulong] = "mu_c_ulong",
[mu_c_size] = "mu_c_size",
[mu_c_off] = "mu_c_off",
[mu_c_time] = "mu_c_time",
[mu_c_bool] = "mu_c_bool",
[mu_c_ipv4] = "mu_c_ipv4",
[mu_c_cidr] = "mu_c_cidr",
[mu_c_host] = "mu_c_host",
[mu_c_incr] = "mu_c_incr",
[mu_c_void] = "mu_c_void"
};
int
mu_str_to_c (char const *string, enum mu_c_type type, void *tgt, char **errmsg)
{
if (errmsg)
*errmsg = NULL;
if ((size_t)type >= sizeof (str_to_c) / sizeof (str_to_c[0]))
return EINVAL;
if (!str_to_c[type])
return ENOSYS;
return str_to_c[type] (tgt, string, errmsg);
}