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,
159 static int ec_node_int_set_config(struct ec_node *gen_node,
160 const struct ec_config *config)
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;
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");
172 if (min_value && max_value && min_value->i64 > max_value->i64) {
177 if (min_value != NULL) {
178 node->check_min = true;
179 node->min = min_value->i64;
181 node->check_min = false;
183 if (max_value != NULL) {
184 node->check_max = true;
185 node->max = max_value->i64;
187 node->check_min = false;
189 if (base_value != NULL)
190 node->base = base_value->u64;
201 static struct ec_node_type ec_node_int_type = {
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,
212 EC_NODE_TYPE_REGISTER(ec_node_int_type);
214 struct ec_node *ec_node_int(const char *id, int64_t min,
215 int64_t max, unsigned int base)
217 struct ec_config *config = NULL;
218 struct ec_node *gen_node = NULL;
221 gen_node = __ec_node(&ec_node_int_type, id);
222 if (gen_node == NULL)
225 config = ec_config_dict();
229 ret = ec_config_dict_set(config, "min", ec_config_i64(min));
232 ret = ec_config_dict_set(config, "max", ec_config_i64(max));
235 ret = ec_config_dict_set(config, "base", ec_config_u64(base));
239 ret = ec_node_set_config(gen_node, config);
247 ec_config_free(config);
248 ec_node_free(gen_node);
252 static const struct ec_config_schema ec_node_uint_schema[] = {
255 .desc = "The minimum valid value (included).",
256 .type = EC_CONFIG_TYPE_UINT64,
260 .desc = "The maximum valid value (included).",
261 .type = EC_CONFIG_TYPE_UINT64,
265 .desc = "The base to use. If unset or 0, try to guess.",
266 .type = EC_CONFIG_TYPE_UINT64,
270 static int ec_node_uint_set_config(struct ec_node *gen_node,
271 const struct ec_config *config)
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;
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");
283 if (min_value && max_value && min_value->u64 > max_value->u64) {
288 if (min_value != NULL) {
289 node->check_min = true;
290 node->min = min_value->u64;
292 node->check_min = false;
294 if (max_value != NULL) {
295 node->check_max = true;
296 node->max = max_value->u64;
298 node->check_min = false;
300 if (base_value != NULL)
301 node->base = base_value->u64;
312 static struct ec_node_type ec_node_uint_type = {
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),
322 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
324 struct ec_node *ec_node_uint(const char *id, uint64_t min,
325 uint64_t max, unsigned int base)
327 struct ec_config *config = NULL;
328 struct ec_node *gen_node = NULL;
331 gen_node = __ec_node(&ec_node_uint_type, id);
332 if (gen_node == NULL)
335 config = ec_config_dict();
339 ret = ec_config_dict_set(config, "min", ec_config_u64(min));
342 ret = ec_config_dict_set(config, "max", ec_config_u64(max));
345 ret = ec_config_dict_set(config, "base", ec_config_u64(base));
349 ret = ec_node_set_config(gen_node, config);
357 ec_config_free(config);
358 ec_node_free(gen_node);
362 int ec_node_int_getval(const struct ec_node *gen_node, const char *str,
365 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
368 ret = ec_node_check_type(gen_node, &ec_node_int_type);
372 if (parse_llint(node, str, result) < 0)
378 int ec_node_uint_getval(const struct ec_node *gen_node, const char *str,
381 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
384 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
388 if (parse_ullint(node, str, result) < 0)
394 /* LCOV_EXCL_START */
395 static int ec_node_int_testcase(void)
398 struct ec_node *node;
404 node = ec_node_uint(EC_NO_ID, 1, 256, 0);
406 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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");
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");
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");
436 node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
438 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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");
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");
458 node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
460 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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");
470 /* test completion */
471 node = ec_node_int(EC_NO_ID, 0, 10, 0);
473 EC_LOG(EC_LOG_ERR, "cannot create node\n");
476 testres |= EC_TEST_CHECK_COMPLETE(node,
479 testres |= EC_TEST_CHECK_COMPLETE(node,
480 "x", EC_NODE_ENDLIST,
482 testres |= EC_TEST_CHECK_COMPLETE(node,
483 "1", EC_NODE_ENDLIST,
491 static struct ec_test ec_node_int_test = {
493 .test = ec_node_int_testcase,
496 EC_TEST_REGISTER(ec_node_int_test);