1b620b77b21f8681da51c9c4d82b5e8dc459af36
[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                 .type = EC_CONFIG_TYPE_NONE,
159         },
160 };
161
162 static int ec_node_int_set_config(struct ec_node *gen_node,
163                                 const struct ec_config *config)
164 {
165         struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
166         const struct ec_config *min_value = NULL;
167         const struct ec_config *max_value = NULL;
168         const struct ec_config *base_value = NULL;
169         char *s = NULL;
170
171         min_value = ec_config_dict_get(config, "min");
172         max_value = ec_config_dict_get(config, "max");
173         base_value = ec_config_dict_get(config, "base");
174
175         if (min_value && max_value && min_value->i64 > max_value->i64) {
176                 errno = EINVAL;
177                 goto fail;
178         }
179
180         if (min_value != NULL) {
181                 node->check_min = true;
182                 node->min = min_value->i64;
183         } else {
184                 node->check_min = false;
185         }
186         if (max_value != NULL) {
187                 node->check_max = true;
188                 node->max = max_value->i64;
189         } else {
190                 node->check_min = false;
191         }
192         if (base_value != NULL)
193                 node->base = base_value->u64;
194         else
195                 node->base = 0;
196
197         return 0;
198
199 fail:
200         ec_free(s);
201         return -1;
202 }
203
204 static struct ec_node_type ec_node_int_type = {
205         .name = "int",
206         .schema = ec_node_int_schema,
207         .set_config = ec_node_int_set_config,
208         .parse = ec_node_int_uint_parse,
209         .complete = ec_node_complete_unknown,
210         .size = sizeof(struct ec_node_int_uint),
211         .init_priv = ec_node_uint_init_priv,
212 };
213
214 EC_NODE_TYPE_REGISTER(ec_node_int_type);
215
216 struct ec_node *ec_node_int(const char *id, int64_t min,
217         int64_t max, unsigned int base)
218 {
219         struct ec_config *config = NULL;
220         struct ec_node *gen_node = NULL;
221         int ret;
222
223         gen_node = ec_node_from_type(&ec_node_int_type, id);
224         if (gen_node == NULL)
225                 return NULL;
226
227         config = ec_config_dict();
228         if (config == NULL)
229                 goto fail;
230
231         ret = ec_config_dict_set(config, "min", ec_config_i64(min));
232         if (ret < 0)
233                 goto fail;
234         ret = ec_config_dict_set(config, "max", ec_config_i64(max));
235         if (ret < 0)
236                 goto fail;
237         ret = ec_config_dict_set(config, "base", ec_config_u64(base));
238         if (ret < 0)
239                 goto fail;
240
241         ret = ec_node_set_config(gen_node, config);
242         config = NULL;
243         if (ret < 0)
244                 goto fail;
245
246         return gen_node;
247
248 fail:
249         ec_config_free(config);
250         ec_node_free(gen_node);
251         return NULL;
252 }
253
254 static const struct ec_config_schema ec_node_uint_schema[] = {
255         {
256                 .key = "min",
257                 .desc = "The minimum valid value (included).",
258                 .type = EC_CONFIG_TYPE_UINT64,
259         },
260         {
261                 .key = "max",
262                 .desc = "The maximum valid value (included).",
263                 .type = EC_CONFIG_TYPE_UINT64,
264         },
265         {
266                 .key = "base",
267                 .desc = "The base to use. If unset or 0, try to guess.",
268                 .type = EC_CONFIG_TYPE_UINT64,
269         },
270         {
271                 .type = EC_CONFIG_TYPE_NONE,
272         },
273 };
274
275 static int ec_node_uint_set_config(struct ec_node *gen_node,
276                                 const struct ec_config *config)
277 {
278         struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
279         const struct ec_config *min_value = NULL;
280         const struct ec_config *max_value = NULL;
281         const struct ec_config *base_value = NULL;
282         char *s = NULL;
283
284         min_value = ec_config_dict_get(config, "min");
285         max_value = ec_config_dict_get(config, "max");
286         base_value = ec_config_dict_get(config, "base");
287
288         if (min_value && max_value && min_value->u64 > max_value->u64) {
289                 errno = EINVAL;
290                 goto fail;
291         }
292
293         if (min_value != NULL) {
294                 node->check_min = true;
295                 node->min = min_value->u64;
296         } else {
297                 node->check_min = false;
298         }
299         if (max_value != NULL) {
300                 node->check_max = true;
301                 node->max = max_value->u64;
302         } else {
303                 node->check_min = false;
304         }
305         if (base_value != NULL)
306                 node->base = base_value->u64;
307         else
308                 node->base = 0;
309
310         return 0;
311
312 fail:
313         ec_free(s);
314         return -1;
315 }
316
317 static struct ec_node_type ec_node_uint_type = {
318         .name = "uint",
319         .schema = ec_node_uint_schema,
320         .set_config = ec_node_uint_set_config,
321         .parse = ec_node_int_uint_parse,
322         .complete = ec_node_complete_unknown,
323         .size = sizeof(struct ec_node_int_uint),
324 };
325
326 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
327
328 struct ec_node *ec_node_uint(const char *id, uint64_t min,
329         uint64_t max, unsigned int base)
330 {
331         struct ec_config *config = NULL;
332         struct ec_node *gen_node = NULL;
333         int ret;
334
335         gen_node = ec_node_from_type(&ec_node_uint_type, id);
336         if (gen_node == NULL)
337                 return NULL;
338
339         config = ec_config_dict();
340         if (config == NULL)
341                 goto fail;
342
343         ret = ec_config_dict_set(config, "min", ec_config_u64(min));
344         if (ret < 0)
345                 goto fail;
346         ret = ec_config_dict_set(config, "max", ec_config_u64(max));
347         if (ret < 0)
348                 goto fail;
349         ret = ec_config_dict_set(config, "base", ec_config_u64(base));
350         if (ret < 0)
351                 goto fail;
352
353         ret = ec_node_set_config(gen_node, config);
354         config = NULL;
355         if (ret < 0)
356                 goto fail;
357
358         return gen_node;
359
360 fail:
361         ec_config_free(config);
362         ec_node_free(gen_node);
363         return NULL;
364 }
365
366 int ec_node_int_getval(const struct ec_node *gen_node, const char *str,
367                         int64_t *result)
368 {
369         struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
370         int ret;
371
372         ret = ec_node_check_type(gen_node, &ec_node_int_type);
373         if (ret < 0)
374                 return ret;
375
376         if (parse_llint(node, str, result) < 0)
377                 return -1;
378
379         return 0;
380 }
381
382 int ec_node_uint_getval(const struct ec_node *gen_node, const char *str,
383                         uint64_t *result)
384 {
385         struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
386         int ret;
387
388         ret = ec_node_check_type(gen_node, &ec_node_uint_type);
389         if (ret < 0)
390                 return ret;
391
392         if (parse_ullint(node, str, result) < 0)
393                 return -1;
394
395         return 0;
396 }
397
398 /* LCOV_EXCL_START */
399 static int ec_node_int_testcase(void)
400 {
401         struct ec_parse *p;
402         struct ec_node *node;
403         const char *s;
404         int testres = 0;
405         uint64_t u64;
406         int64_t i64;
407
408         node = ec_node_uint(EC_NO_ID, 1, 256, 0);
409         if (node == NULL) {
410                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
411                 return -1;
412         }
413         testres |= EC_TEST_CHECK_PARSE(node, -1, "");
414         testres |= EC_TEST_CHECK_PARSE(node, -1, "0");
415         testres |= EC_TEST_CHECK_PARSE(node, 1, "1");
416         testres |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
417         testres |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
418         testres |= EC_TEST_CHECK_PARSE(node, 1, " 1");
419         testres |= EC_TEST_CHECK_PARSE(node, -1, "-1");
420         testres |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
421         testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
422         testres |= EC_TEST_CHECK_PARSE(node, -1, "0x100000000000000000");
423         testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
424
425         p = ec_node_parse(node, "1");
426         s = ec_strvec_val(ec_parse_strvec(p), 0);
427         testres |= EC_TEST_CHECK(s != NULL &&
428                 ec_node_uint_getval(node, s, &u64) == 0 &&
429                 u64 == 1, "bad integer value");
430         ec_parse_free(p);
431
432         p = ec_node_parse(node, "10");
433         s = ec_strvec_val(ec_parse_strvec(p), 0);
434         testres |= EC_TEST_CHECK(s != NULL &&
435                 ec_node_uint_getval(node, s, &u64) == 0 &&
436                 u64 == 10, "bad integer value");
437         ec_parse_free(p);
438         ec_node_free(node);
439
440         node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
441         if (node == NULL) {
442                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
443                 return -1;
444         }
445         testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
446         testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
447         testres |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
448         testres |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
449         testres |= EC_TEST_CHECK_PARSE(node, -1, "0x8000000000000000");
450         testres |= EC_TEST_CHECK_PARSE(node, -1, "-2");
451         testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
452         testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
453
454         p = ec_node_parse(node, "10");
455         s = ec_strvec_val(ec_parse_strvec(p), 0);
456         testres |= EC_TEST_CHECK(s != NULL &&
457                 ec_node_int_getval(node, s, &i64) == 0 &&
458                 i64 == 16, "bad integer value");
459         ec_parse_free(p);
460         ec_node_free(node);
461
462         node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
463         if (node == NULL) {
464                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
465                 return -1;
466         }
467         testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
468         testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
469         testres |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
470         testres |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
471         testres |= EC_TEST_CHECK_PARSE(node, -1, "1");
472         ec_node_free(node);
473
474         /* test completion */
475         node = ec_node_int(EC_NO_ID, 0, 10, 0);
476         if (node == NULL) {
477                 EC_LOG(EC_LOG_ERR, "cannot create node\n");
478                 return -1;
479         }
480         testres |= EC_TEST_CHECK_COMPLETE(node,
481                 "", EC_NODE_ENDLIST,
482                 EC_NODE_ENDLIST);
483         testres |= EC_TEST_CHECK_COMPLETE(node,
484                 "x", EC_NODE_ENDLIST,
485                 EC_NODE_ENDLIST);
486         testres |= EC_TEST_CHECK_COMPLETE(node,
487                 "1", EC_NODE_ENDLIST,
488                 EC_NODE_ENDLIST);
489         ec_node_free(node);
490
491         return testres;
492 }
493 /* LCOV_EXCL_STOP */
494
495 static struct ec_test ec_node_int_test = {
496         .name = "node_int",
497         .test = ec_node_int_testcase,
498 };
499
500 EC_TEST_REGISTER(ec_node_int_test);