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 static int parse_llint(struct ec_node_int_uint *node, const char *str,
47 int save_errno = errno;
50 *val = strtoll(str, &endptr, node->base);
52 if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) ||
53 (errno != 0 && *val == 0))
56 if (node->check_min && *val < node->min) {
61 if (node->check_max && *val > node->max) {
75 static int parse_ullint(struct ec_node_int_uint *node, const char *str,
79 int save_errno = errno;
81 /* since a negative input is silently converted to a positive
82 * one by strtoull(), first check that it is positive */
87 *val = strtoull(str, &endptr, node->base);
89 if ((errno == ERANGE && *val == ULLONG_MAX) ||
90 (errno != 0 && *val == 0))
93 if (node->check_min && *val < node->umin)
96 if (node->check_max && *val > node->umax)
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)
110 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
117 if (ec_strvec_len(strvec) == 0)
118 return EC_PARSE_NOMATCH;
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;
125 if (parse_ullint(node, str, &u64) < 0)
126 return EC_PARSE_NOMATCH;
132 ec_node_uint_init_priv(struct ec_node *gen_node)
134 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
136 node->is_signed = true;
141 static const struct ec_config_schema ec_node_int_schema[] = {
144 .desc = "The minimum valid value (included).",
145 .type = EC_CONFIG_TYPE_INT64,
149 .desc = "The maximum valid value (included).",
150 .type = EC_CONFIG_TYPE_INT64,
154 .desc = "The base to use. If unset or 0, try to guess.",
155 .type = EC_CONFIG_TYPE_UINT64,
158 .type = EC_CONFIG_TYPE_NONE,
162 static int ec_node_int_set_config(struct ec_node *gen_node,
163 const struct ec_config *config)
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;
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");
175 if (min_value && max_value && min_value->i64 > max_value->i64) {
180 if (min_value != NULL) {
181 node->check_min = true;
182 node->min = min_value->i64;
184 node->check_min = false;
186 if (max_value != NULL) {
187 node->check_max = true;
188 node->max = max_value->i64;
190 node->check_min = false;
192 if (base_value != NULL)
193 node->base = base_value->u64;
204 static struct ec_node_type ec_node_int_type = {
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,
214 EC_NODE_TYPE_REGISTER(ec_node_int_type);
216 struct ec_node *ec_node_int(const char *id, int64_t min,
217 int64_t max, unsigned int base)
219 struct ec_config *config = NULL;
220 struct ec_node *gen_node = NULL;
223 gen_node = ec_node_from_type(&ec_node_int_type, id);
224 if (gen_node == NULL)
227 config = ec_config_dict();
231 ret = ec_config_dict_set(config, "min", ec_config_i64(min));
234 ret = ec_config_dict_set(config, "max", ec_config_i64(max));
237 ret = ec_config_dict_set(config, "base", ec_config_u64(base));
241 ret = ec_node_set_config(gen_node, config);
249 ec_config_free(config);
250 ec_node_free(gen_node);
254 static const struct ec_config_schema ec_node_uint_schema[] = {
257 .desc = "The minimum valid value (included).",
258 .type = EC_CONFIG_TYPE_UINT64,
262 .desc = "The maximum valid value (included).",
263 .type = EC_CONFIG_TYPE_UINT64,
267 .desc = "The base to use. If unset or 0, try to guess.",
268 .type = EC_CONFIG_TYPE_UINT64,
271 .type = EC_CONFIG_TYPE_NONE,
275 static int ec_node_uint_set_config(struct ec_node *gen_node,
276 const struct ec_config *config)
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;
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");
288 if (min_value && max_value && min_value->u64 > max_value->u64) {
293 if (min_value != NULL) {
294 node->check_min = true;
295 node->min = min_value->u64;
297 node->check_min = false;
299 if (max_value != NULL) {
300 node->check_max = true;
301 node->max = max_value->u64;
303 node->check_min = false;
305 if (base_value != NULL)
306 node->base = base_value->u64;
317 static struct ec_node_type ec_node_uint_type = {
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),
326 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
328 struct ec_node *ec_node_uint(const char *id, uint64_t min,
329 uint64_t max, unsigned int base)
331 struct ec_config *config = NULL;
332 struct ec_node *gen_node = NULL;
335 gen_node = ec_node_from_type(&ec_node_uint_type, id);
336 if (gen_node == NULL)
339 config = ec_config_dict();
343 ret = ec_config_dict_set(config, "min", ec_config_u64(min));
346 ret = ec_config_dict_set(config, "max", ec_config_u64(max));
349 ret = ec_config_dict_set(config, "base", ec_config_u64(base));
353 ret = ec_node_set_config(gen_node, config);
361 ec_config_free(config);
362 ec_node_free(gen_node);
366 int ec_node_int_getval(const struct ec_node *gen_node, const char *str,
369 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
372 ret = ec_node_check_type(gen_node, &ec_node_int_type);
376 if (parse_llint(node, str, result) < 0)
382 int ec_node_uint_getval(const struct ec_node *gen_node, const char *str,
385 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
388 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
392 if (parse_ullint(node, str, result) < 0)
398 /* LCOV_EXCL_START */
399 static int ec_node_int_testcase(void)
402 struct ec_node *node;
408 node = ec_node_uint(EC_NO_ID, 1, 256, 0);
410 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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");
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");
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");
440 node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
442 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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");
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");
462 node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
464 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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");
474 /* test completion */
475 node = ec_node_int(EC_NO_ID, 0, 10, 0);
477 EC_LOG(EC_LOG_ERR, "cannot create node\n");
480 testres |= EC_TEST_CHECK_COMPLETE(node,
483 testres |= EC_TEST_CHECK_COMPLETE(node,
484 "x", EC_NODE_ENDLIST,
486 testres |= EC_TEST_CHECK_COMPLETE(node,
487 "1", EC_NODE_ENDLIST,
495 static struct ec_test ec_node_int_test = {
497 .test = ec_node_int_testcase,
500 EC_TEST_REGISTER(ec_node_int_test);