1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
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>
24 EC_LOG_TYPE_REGISTER(node_int);
26 /* common to int and uint */
27 struct ec_node_int_uint {
43 /* XXX to utils.c ? */
44 static int parse_llint(struct ec_node_int_uint *node, const char *str,
48 int save_errno = errno;
51 *val = strtoll(str, &endptr, node->base);
53 if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) ||
54 (errno != 0 && *val == 0))
57 if (node->check_min && *val < node->min) {
62 if (node->check_max && *val > node->max) {
76 static int parse_ullint(struct ec_node_int_uint *node, const char *str,
80 int save_errno = errno;
82 /* since a negative input is silently converted to a positive
83 * one by strtoull(), first check that it is positive */
88 *val = strtoull(str, &endptr, node->base);
90 if ((errno == ERANGE && *val == ULLONG_MAX) ||
91 (errno != 0 && *val == 0))
94 if (node->check_min && *val < node->umin)
97 if (node->check_max && *val > node->umax)
107 static int ec_node_int_uint_parse(const struct ec_node *gen_node,
108 struct ec_parse *state,
109 const struct ec_strvec *strvec)
111 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
118 if (ec_strvec_len(strvec) == 0)
119 return EC_PARSE_NOMATCH;
121 str = ec_strvec_val(strvec, 0);
122 if (node->is_signed) {
123 if (parse_llint(node, str, &i64) < 0)
124 return EC_PARSE_NOMATCH;
126 if (parse_ullint(node, str, &u64) < 0)
127 return EC_PARSE_NOMATCH;
133 ec_node_uint_init_priv(struct ec_node *gen_node)
135 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
137 node->is_signed = true;
142 static const struct ec_config_schema ec_node_int_schema[] = {
145 .desc = "The minimum valid value (included).",
146 .type = EC_CONFIG_TYPE_INT64,
150 .desc = "The maximum valid value (included).",
151 .type = EC_CONFIG_TYPE_INT64,
155 .desc = "The base to use. If unset or 0, try to guess.",
156 .type = EC_CONFIG_TYPE_UINT64,
159 .type = EC_CONFIG_TYPE_NONE,
163 static int ec_node_int_set_config(struct ec_node *gen_node,
164 const struct ec_config *config)
166 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
167 const struct ec_config *min_value = NULL;
168 const struct ec_config *max_value = NULL;
169 const struct ec_config *base_value = NULL;
172 min_value = ec_config_dict_get(config, "min");
173 max_value = ec_config_dict_get(config, "max");
174 base_value = ec_config_dict_get(config, "base");
176 if (min_value && max_value && min_value->i64 > max_value->i64) {
181 if (min_value != NULL) {
182 node->check_min = true;
183 node->min = min_value->i64;
185 node->check_min = false;
187 if (max_value != NULL) {
188 node->check_max = true;
189 node->max = max_value->i64;
191 node->check_min = false;
193 if (base_value != NULL)
194 node->base = base_value->u64;
205 static struct ec_node_type ec_node_int_type = {
207 .schema = ec_node_int_schema,
208 .set_config = ec_node_int_set_config,
209 .parse = ec_node_int_uint_parse,
210 .complete = ec_node_complete_unknown,
211 .size = sizeof(struct ec_node_int_uint),
212 .init_priv = ec_node_uint_init_priv,
215 EC_NODE_TYPE_REGISTER(ec_node_int_type);
217 struct ec_node *ec_node_int(const char *id, int64_t min,
218 int64_t max, unsigned int base)
220 struct ec_config *config = NULL;
221 struct ec_node *gen_node = NULL;
224 gen_node = ec_node_from_type(&ec_node_int_type, id);
225 if (gen_node == NULL)
228 config = ec_config_dict();
232 ret = ec_config_dict_set(config, "min", ec_config_i64(min));
235 ret = ec_config_dict_set(config, "max", ec_config_i64(max));
238 ret = ec_config_dict_set(config, "base", ec_config_u64(base));
242 ret = ec_node_set_config(gen_node, config);
250 ec_config_free(config);
251 ec_node_free(gen_node);
255 static const struct ec_config_schema ec_node_uint_schema[] = {
258 .desc = "The minimum valid value (included).",
259 .type = EC_CONFIG_TYPE_UINT64,
263 .desc = "The maximum valid value (included).",
264 .type = EC_CONFIG_TYPE_UINT64,
268 .desc = "The base to use. If unset or 0, try to guess.",
269 .type = EC_CONFIG_TYPE_UINT64,
272 .type = EC_CONFIG_TYPE_NONE,
276 static int ec_node_uint_set_config(struct ec_node *gen_node,
277 const struct ec_config *config)
279 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
280 const struct ec_config *min_value = NULL;
281 const struct ec_config *max_value = NULL;
282 const struct ec_config *base_value = NULL;
285 min_value = ec_config_dict_get(config, "min");
286 max_value = ec_config_dict_get(config, "max");
287 base_value = ec_config_dict_get(config, "base");
289 if (min_value && max_value && min_value->u64 > max_value->u64) {
294 if (min_value != NULL) {
295 node->check_min = true;
296 node->min = min_value->u64;
298 node->check_min = false;
300 if (max_value != NULL) {
301 node->check_max = true;
302 node->max = max_value->u64;
304 node->check_min = false;
306 if (base_value != NULL)
307 node->base = base_value->u64;
318 static struct ec_node_type ec_node_uint_type = {
320 .schema = ec_node_uint_schema,
321 .set_config = ec_node_uint_set_config,
322 .parse = ec_node_int_uint_parse,
323 .complete = ec_node_complete_unknown,
324 .size = sizeof(struct ec_node_int_uint),
327 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
329 struct ec_node *ec_node_uint(const char *id, uint64_t min,
330 uint64_t max, unsigned int base)
332 struct ec_config *config = NULL;
333 struct ec_node *gen_node = NULL;
336 gen_node = ec_node_from_type(&ec_node_uint_type, id);
337 if (gen_node == NULL)
340 config = ec_config_dict();
344 ret = ec_config_dict_set(config, "min", ec_config_u64(min));
347 ret = ec_config_dict_set(config, "max", ec_config_u64(max));
350 ret = ec_config_dict_set(config, "base", ec_config_u64(base));
354 ret = ec_node_set_config(gen_node, config);
362 ec_config_free(config);
363 ec_node_free(gen_node);
367 int ec_node_int_getval(const struct ec_node *gen_node, const char *str,
370 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
373 ret = ec_node_check_type(gen_node, &ec_node_int_type);
377 if (parse_llint(node, str, result) < 0)
383 int ec_node_uint_getval(const struct ec_node *gen_node, const char *str,
386 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
389 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
393 if (parse_ullint(node, str, result) < 0)
399 /* LCOV_EXCL_START */
400 static int ec_node_int_testcase(void)
403 struct ec_node *node;
409 node = ec_node_uint(EC_NO_ID, 1, 256, 0);
411 EC_LOG(EC_LOG_ERR, "cannot create node\n");
414 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
415 testres |= EC_TEST_CHECK_PARSE(node, -1, "0");
416 testres |= EC_TEST_CHECK_PARSE(node, 1, "1");
417 testres |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
418 testres |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
419 testres |= EC_TEST_CHECK_PARSE(node, 1, " 1");
420 testres |= EC_TEST_CHECK_PARSE(node, -1, "-1");
421 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
422 testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
423 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x100000000000000000");
424 testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
426 p = ec_node_parse(node, "1");
427 s = ec_strvec_val(ec_parse_strvec(p), 0);
428 testres |= EC_TEST_CHECK(s != NULL &&
429 ec_node_uint_getval(node, s, &u64) == 0 &&
430 u64 == 1, "bad integer value");
433 p = ec_node_parse(node, "10");
434 s = ec_strvec_val(ec_parse_strvec(p), 0);
435 testres |= EC_TEST_CHECK(s != NULL &&
436 ec_node_uint_getval(node, s, &u64) == 0 &&
437 u64 == 10, "bad integer value");
441 node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
443 EC_LOG(EC_LOG_ERR, "cannot create node\n");
446 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
447 testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
448 testres |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
449 testres |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
450 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x8000000000000000");
451 testres |= EC_TEST_CHECK_PARSE(node, -1, "-2");
452 testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
453 testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
455 p = ec_node_parse(node, "10");
456 s = ec_strvec_val(ec_parse_strvec(p), 0);
457 testres |= EC_TEST_CHECK(s != NULL &&
458 ec_node_int_getval(node, s, &i64) == 0 &&
459 i64 == 16, "bad integer value");
463 node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
465 EC_LOG(EC_LOG_ERR, "cannot create node\n");
468 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
469 testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
470 testres |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
471 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
472 testres |= EC_TEST_CHECK_PARSE(node, -1, "1");
475 /* test completion */
476 node = ec_node_int(EC_NO_ID, 0, 10, 0);
478 EC_LOG(EC_LOG_ERR, "cannot create node\n");
481 testres |= EC_TEST_CHECK_COMPLETE(node,
484 testres |= EC_TEST_CHECK_COMPLETE(node,
485 "x", EC_NODE_ENDLIST,
487 testres |= EC_TEST_CHECK_COMPLETE(node,
488 "1", EC_NODE_ENDLIST,
496 static struct ec_test ec_node_int_test = {
498 .test = ec_node_int_testcase,
501 EC_TEST_REGISTER(ec_node_int_test);