config for int
[protos/libecoli.git] / lib / ecoli_node_int.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <stdbool.h>
10 #include <limits.h>
11 #include <ctype.h>
12 #include <errno.h>
13
14 #include <ecoli_log.h>
15 #include <ecoli_malloc.h>
16 #include <ecoli_strvec.h>
17 #include <ecoli_node.h>
18 #include <ecoli_config.h>
19 #include <ecoli_parse.h>
20 #include <ecoli_complete.h>
21 #include <ecoli_node_int.h>
22 #include <ecoli_test.h>
23
24 EC_LOG_TYPE_REGISTER(node_int);
25
26 /* common to int and uint */
27 struct ec_node_int_uint {
28         struct ec_node gen;
29         bool is_signed;
30         bool check_min;
31         bool check_max;
32         union {
33                 int64_t min;
34                 uint64_t umin;
35         };
36         union {
37                 int64_t max;
38                 uint64_t umax;
39         };
40         unsigned int base;
41 };
42
43 static int parse_llint(struct ec_node_int_uint *node, const char *str,
44         int64_t *val)
45 {
46         char *endptr;
47         int save_errno = errno;
48
49         errno = 0;
50         *val = strtoll(str, &endptr, node->base);
51
52         if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) ||
53                         (errno != 0 && *val == 0))
54                 return -1;
55
56         if (node->check_min && *val < node->min) {
57                 errno = ERANGE;
58                 return -1;
59         }
60
61         if (node->check_max && *val > node->max) {
62                 errno = ERANGE;
63                 return -1;
64         }
65
66         if (*endptr != 0) {
67                 errno = EINVAL;
68                 return -1;
69         }
70
71         errno = save_errno;
72         return 0;
73 }
74
75 static int parse_ullint(struct ec_node_int_uint *node, const char *str,
76                         uint64_t *val)
77 {
78         char *endptr;
79         int save_errno = errno;
80
81         /* since a negative input is silently converted to a positive
82          * one by strtoull(), first check that it is positive */
83         if (strchr(str, '-'))
84                 return -1;
85
86         errno = 0;
87         *val = strtoull(str, &endptr, node->base);
88
89         if ((errno == ERANGE && *val == ULLONG_MAX) ||
90                         (errno != 0 && *val == 0))
91                 return -1;
92
93         if (node->check_min && *val < node->umin)
94                 return -1;
95
96         if (node->check_max && *val > node->umax)
97                 return -1;
98
99         if (*endptr != 0)
100                 return -1;
101
102         errno = save_errno;
103         return 0;
104 }
105
106 static int ec_node_int_uint_parse(const struct ec_node *gen_node,
107                         struct ec_parse *state,
108                         const struct ec_strvec *strvec)
109 {
110         struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
111         const char *str;
112         uint64_t u64;
113         int64_t i64;
114
115         (void)state;
116
117         if (ec_strvec_len(strvec) == 0)
118                 return EC_PARSE_NOMATCH;
119
120         str = ec_strvec_val(strvec, 0);
121         if (node->is_signed) {
122                 if (parse_llint(node, str, &i64) < 0)
123                         return EC_PARSE_NOMATCH;
124         } else {
125                 if (parse_ullint(node, str, &u64) < 0)
126                         return EC_PARSE_NOMATCH;
127         }
128         return 1;
129 }
130
131 static int
132 ec_node_uint_init_priv(struct ec_node *gen_node)
133 {
134         struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
135
136         node->is_signed = true;
137
138         return 0;
139 }
140
141 static const struct ec_config_schema ec_node_int_schema[] = {
142         {
143                 .key = "min",
144                 .desc = "The minimum valid value (included).",
145                 .type = EC_CONFIG_TYPE_INT64,
146         },
147         {
148                 .key = "max",
149                 .desc = "The maximum valid value (included).",
150                 .type = EC_CONFIG_TYPE_INT64,
151         },
152         {
153                 .key = "base",
154                 .desc = "The base to use. If unset or 0, try to guess.",
155                 .type = EC_CONFIG_TYPE_UINT64,
156         },
157 };
158
159 static int ec_node_int_set_config(struct ec_node *gen_node,
160                                 const struct ec_config *config)
161 {
162         struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
163         const struct ec_config *min_value = NULL;
164         const struct ec_config *max_value = NULL;
165         const struct ec_config *base_value = NULL;
166         char *s = NULL;
167
168         min_value = ec_config_dict_get(config, "min");
169         max_value = ec_config_dict_get(config, "max");
170         base_value = ec_config_dict_get(config, "base");
171
172         if (min_value && max_value && min_value->i64 > max_value->i64) {
173                 errno = EINVAL;
174                 goto fail;
175         }
176
177         if (min_value != NULL) {
178                 node->check_min = true;
179                 node->min = min_value->i64;
180         } else {
181                 node->check_min = false;
182         }
183         if (max_value != NULL) {
184                 node->check_max = true;
185                 node->max = max_value->i64;
186         } else {
187                 node->check_min = false;
188         }
189         if (base_value != NULL)
190                 node->base = base_value->u64;
191         else
192                 node->base = 0;
193
194         return 0;
195
196 fail:
197         ec_free(s);
198         return -1;
199 }
200
201 static struct ec_node_type ec_node_int_type = {
202         .name = "int",
203         .schema = ec_node_int_schema,
204         .schema_len = EC_COUNT_OF(ec_node_int_schema),
205         .set_config = ec_node_int_set_config,
206         .parse = ec_node_int_uint_parse,
207         .complete = ec_node_complete_unknown,
208         .size = sizeof(struct ec_node_int_uint),
209         .init_priv = ec_node_uint_init_priv,
210 };
211
212 EC_NODE_TYPE_REGISTER(ec_node_int_type);
213
214 struct ec_node *ec_node_int(const char *id, int64_t min,
215         int64_t max, unsigned int base)
216 {
217         struct ec_config *config = NULL;
218         struct ec_node *gen_node = NULL;
219         int ret;
220
221         gen_node = __ec_node(&ec_node_int_type, id);
222         if (gen_node == NULL)
223                 return NULL;
224
225         config = ec_config_dict();
226         if (config == NULL)
227                 goto fail;
228
229         ret = ec_config_dict_set(config, "min", ec_config_i64(min));
230         if (ret < 0)
231                 goto fail;
232         ret = ec_config_dict_set(config, "max", ec_config_i64(max));
233         if (ret < 0)
234                 goto fail;
235         ret = ec_config_dict_set(config, "base", ec_config_u64(base));
236         if (ret < 0)
237                 goto fail;
238
239         ret = ec_node_set_config(gen_node, config);
240         config = NULL;
241         if (ret < 0)
242                 goto fail;
243
244         return gen_node;
245
246 fail:
247         ec_config_free(config);
248         ec_node_free(gen_node);
249         return NULL;
250 }
251
252 static const struct ec_config_schema ec_node_uint_schema[] = {
253         {
254                 .key = "min",
255                 .desc = "The minimum valid value (included).",
256                 .type = EC_CONFIG_TYPE_UINT64,
257         },
258         {
259                 .key = "max",
260                 .desc = "The maximum valid value (included).",
261                 .type = EC_CONFIG_TYPE_UINT64,
262         },
263         {
264                 .key = "base",
265                 .desc = "The base to use. If unset or 0, try to guess.",
266                 .type = EC_CONFIG_TYPE_UINT64,
267         },
268 };
269
270 static int ec_node_uint_set_config(struct ec_node *gen_node,
271                                 const struct ec_config *config)
272 {
273         struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
274         const struct ec_config *min_value = NULL;
275         const struct ec_config *max_value = NULL;
276         const struct ec_config *base_value = NULL;
277         char *s = NULL;
278
279         min_value = ec_config_dict_get(config, "min");
280         max_value = ec_config_dict_get(config, "max");
281         base_value = ec_config_dict_get(config, "base");
282
283         if (min_value && max_value && min_value->u64 > max_value->u64) {
284                 errno = EINVAL;
285                 goto fail;
286         }
287
288         if (min_value != NULL) {
289                 node->check_min = true;
290                 node->min = min_value->u64;
291         } else {
292                 node->check_min = false;
293         }
294         if (max_value != NULL) {
295                 node->check_max = true;
296                 node->max = max_value->u64;
297         } else {
298                 node->check_min = false;
299         }
300         if (base_value != NULL)
301                 node->base = base_value->u64;
302         else
303                 node->base = 0;
304
305         return 0;
306
307 fail:
308         ec_free(s);
309         return -1;
310 }
311
312 static struct ec_node_type ec_node_uint_type = {
313         .name = "uint",
314         .schema = ec_node_uint_schema,
315         .schema_len = EC_COUNT_OF(ec_node_uint_schema),
316         .set_config = ec_node_uint_set_config,
317         .parse = ec_node_int_uint_parse,
318         .complete = ec_node_complete_unknown,
319         .size = sizeof(struct ec_node_int_uint),
320 };
321
322 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
323
324 struct ec_node *ec_node_uint(const char *id, uint64_t min,
325         uint64_t max, unsigned int base)
326 {
327         struct ec_config *config = NULL;
328         struct ec_node *gen_node = NULL;
329         int ret;
330
331         gen_node = __ec_node(&ec_node_uint_type, id);
332         if (gen_node == NULL)
333                 return NULL;
334
335         config = ec_config_dict();
336         if (config == NULL)
337                 goto fail;
338
339         ret = ec_config_dict_set(config, "min", ec_config_u64(min));
340         if (ret < 0)
341                 goto fail;
342         ret = ec_config_dict_set(config, "max", ec_config_u64(max));
343         if (ret < 0)
344                 goto fail;
345         ret = ec_config_dict_set(config, "base", ec_config_u64(base));
346         if (ret < 0)
347                 goto fail;
348
349         ret = ec_node_set_config(gen_node, config);
350         config = NULL;
351         if (ret < 0)
352                 goto fail;
353
354         return gen_node;
355
356 fail:
357         ec_config_free(config);
358         ec_node_free(gen_node);
359         return NULL;
360 }
361
362 int ec_node_int_getval(const struct ec_node *gen_node, const char *str,
363                         int64_t *result)
364 {
365         struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
366         int ret;
367
368         ret = ec_node_check_type(gen_node, &ec_node_int_type);
369         if (ret < 0)
370                 return ret;
371
372         if (parse_llint(node, str, result) < 0)
373                 return -1;
374
375         return 0;
376 }
377
378 int ec_node_uint_getval(const struct ec_node *gen_node, const char *str,
379                         uint64_t *result)
380 {
381         struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
382         int ret;
383
384         ret = ec_node_check_type(gen_node, &ec_node_uint_type);
385         if (ret < 0)
386                 return ret;
387
388         if (parse_ullint(node, str, result) < 0)
389                 return -1;
390
391         return 0;
392 }
393
394 /* LCOV_EXCL_START */
395 static int ec_node_int_testcase(void)
396 {
397         struct ec_parse *p;
398         struct ec_node *node;
399         const char *s;
400         int testres = 0;
401         uint64_t u64;
402         int64_t i64;
403
404         node = ec_node_uint(EC_NO_ID, 1, 256, 0);
405         if (node == NULL) {
406                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
407                 return -1;
408         }
409         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
410         testres |= EC_TEST_CHECK_PARSE(node, -1, "0");
411         testres |= EC_TEST_CHECK_PARSE(node, 1, "1");
412         testres |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
413         testres |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
414         testres |= EC_TEST_CHECK_PARSE(node, 1, " 1");
415         testres |= EC_TEST_CHECK_PARSE(node, -1, "-1");
416         testres |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
417         testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
418         testres |= EC_TEST_CHECK_PARSE(node, -1, "0x100000000000000000");
419         testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
420
421         p = ec_node_parse(node, "1");
422         s = ec_strvec_val(ec_parse_strvec(p), 0);
423         testres |= EC_TEST_CHECK(s != NULL &&
424                 ec_node_uint_getval(node, s, &u64) == 0 &&
425                 u64 == 1, "bad integer value");
426         ec_parse_free(p);
427
428         p = ec_node_parse(node, "10");
429         s = ec_strvec_val(ec_parse_strvec(p), 0);
430         testres |= EC_TEST_CHECK(s != NULL &&
431                 ec_node_uint_getval(node, s, &u64) == 0 &&
432                 u64 == 10, "bad integer value");
433         ec_parse_free(p);
434         ec_node_free(node);
435
436         node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
437         if (node == NULL) {
438                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
439                 return -1;
440         }
441         testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
442         testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
443         testres |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
444         testres |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
445         testres |= EC_TEST_CHECK_PARSE(node, -1, "0x8000000000000000");
446         testres |= EC_TEST_CHECK_PARSE(node, -1, "-2");
447         testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
448         testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
449
450         p = ec_node_parse(node, "10");
451         s = ec_strvec_val(ec_parse_strvec(p), 0);
452         testres |= EC_TEST_CHECK(s != NULL &&
453                 ec_node_int_getval(node, s, &i64) == 0 &&
454                 i64 == 16, "bad integer value");
455         ec_parse_free(p);
456         ec_node_free(node);
457
458         node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
459         if (node == NULL) {
460                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
461                 return -1;
462         }
463         testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
464         testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
465         testres |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
466         testres |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
467         testres |= EC_TEST_CHECK_PARSE(node, -1, "1");
468         ec_node_free(node);
469
470         /* test completion */
471         node = ec_node_int(EC_NO_ID, 0, 10, 0);
472         if (node == NULL) {
473                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
474                 return -1;
475         }
476         testres |= EC_TEST_CHECK_COMPLETE(node,
477                 "", EC_NODE_ENDLIST,
478                 EC_NODE_ENDLIST);
479         testres |= EC_TEST_CHECK_COMPLETE(node,
480                 "x", EC_NODE_ENDLIST,
481                 EC_NODE_ENDLIST);
482         testres |= EC_TEST_CHECK_COMPLETE(node,
483                 "1", EC_NODE_ENDLIST,
484                 EC_NODE_ENDLIST);
485         ec_node_free(node);
486
487         return testres;
488 }
489 /* LCOV_EXCL_STOP */
490
491 static struct ec_test ec_node_int_test = {
492         .name = "node_int",
493         .test = ec_node_int_testcase,
494 };
495
496 EC_TEST_REGISTER(ec_node_int_test);