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_string.h>
22 #include <ecoli_test.h>
23 #include <ecoli_node_int.h>
25 EC_LOG_TYPE_REGISTER(node_int);
27 /* common to int and uint */
28 struct ec_node_int_uint {
43 static int parse_llint(struct ec_node_int_uint *priv, const char *str,
57 return ec_str_parse_llint(str, priv->base, min, max, val);
60 static int parse_ullint(struct ec_node_int_uint *priv, const char *str,
74 return ec_str_parse_ullint(str, priv->base, min, max, val);
77 static int ec_node_int_uint_parse(const struct ec_node *node,
78 struct ec_pnode *pstate,
79 const struct ec_strvec *strvec)
81 struct ec_node_int_uint *priv = ec_node_priv(node);
88 if (ec_strvec_len(strvec) == 0)
89 return EC_PARSE_NOMATCH;
91 str = ec_strvec_val(strvec, 0);
92 if (priv->is_signed) {
93 if (parse_llint(priv, str, &i64) < 0)
94 return EC_PARSE_NOMATCH;
96 if (parse_ullint(priv, str, &u64) < 0)
97 return EC_PARSE_NOMATCH;
103 ec_node_uint_init_priv(struct ec_node *node)
105 struct ec_node_int_uint *priv = ec_node_priv(node);
107 priv->is_signed = true;
112 static const struct ec_config_schema ec_node_int_schema[] = {
115 .desc = "The minimum valid value (included).",
116 .type = EC_CONFIG_TYPE_INT64,
120 .desc = "The maximum valid value (included).",
121 .type = EC_CONFIG_TYPE_INT64,
125 .desc = "The base to use. If unset or 0, try to guess.",
126 .type = EC_CONFIG_TYPE_UINT64,
129 .type = EC_CONFIG_TYPE_NONE,
133 static int ec_node_int_set_config(struct ec_node *node,
134 const struct ec_config *config)
136 struct ec_node_int_uint *priv = ec_node_priv(node);
137 const struct ec_config *min_value = NULL;
138 const struct ec_config *max_value = NULL;
139 const struct ec_config *base_value = NULL;
142 min_value = ec_config_dict_get(config, "min");
143 max_value = ec_config_dict_get(config, "max");
144 base_value = ec_config_dict_get(config, "base");
146 if (min_value && max_value && min_value->i64 > max_value->i64) {
151 if (min_value != NULL) {
152 priv->check_min = true;
153 priv->min = min_value->i64;
155 priv->check_min = false;
157 if (max_value != NULL) {
158 priv->check_max = true;
159 priv->max = max_value->i64;
161 priv->check_min = false;
163 if (base_value != NULL)
164 priv->base = base_value->u64;
175 static struct ec_node_type ec_node_int_type = {
177 .schema = ec_node_int_schema,
178 .set_config = ec_node_int_set_config,
179 .parse = ec_node_int_uint_parse,
180 .size = sizeof(struct ec_node_int_uint),
181 .init_priv = ec_node_uint_init_priv,
184 EC_NODE_TYPE_REGISTER(ec_node_int_type);
186 struct ec_node *ec_node_int(const char *id, int64_t min,
187 int64_t max, unsigned int base)
189 struct ec_config *config = NULL;
190 struct ec_node *node = NULL;
193 node = ec_node_from_type(&ec_node_int_type, id);
197 config = ec_config_dict();
201 ret = ec_config_dict_set(config, "min", ec_config_i64(min));
204 ret = ec_config_dict_set(config, "max", ec_config_i64(max));
207 ret = ec_config_dict_set(config, "base", ec_config_u64(base));
211 ret = ec_node_set_config(node, config);
219 ec_config_free(config);
224 static const struct ec_config_schema ec_node_uint_schema[] = {
227 .desc = "The minimum valid value (included).",
228 .type = EC_CONFIG_TYPE_UINT64,
232 .desc = "The maximum valid value (included).",
233 .type = EC_CONFIG_TYPE_UINT64,
237 .desc = "The base to use. If unset or 0, try to guess.",
238 .type = EC_CONFIG_TYPE_UINT64,
241 .type = EC_CONFIG_TYPE_NONE,
245 static int ec_node_uint_set_config(struct ec_node *node,
246 const struct ec_config *config)
248 struct ec_node_int_uint *priv = ec_node_priv(node);
249 const struct ec_config *min_value = NULL;
250 const struct ec_config *max_value = NULL;
251 const struct ec_config *base_value = NULL;
254 min_value = ec_config_dict_get(config, "min");
255 max_value = ec_config_dict_get(config, "max");
256 base_value = ec_config_dict_get(config, "base");
258 if (min_value && max_value && min_value->u64 > max_value->u64) {
263 if (min_value != NULL) {
264 priv->check_min = true;
265 priv->min = min_value->u64;
267 priv->check_min = false;
269 if (max_value != NULL) {
270 priv->check_max = true;
271 priv->max = max_value->u64;
273 priv->check_min = false;
275 if (base_value != NULL)
276 priv->base = base_value->u64;
287 static struct ec_node_type ec_node_uint_type = {
289 .schema = ec_node_uint_schema,
290 .set_config = ec_node_uint_set_config,
291 .parse = ec_node_int_uint_parse,
292 .size = sizeof(struct ec_node_int_uint),
295 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
297 struct ec_node *ec_node_uint(const char *id, uint64_t min,
298 uint64_t max, unsigned int base)
300 struct ec_config *config = NULL;
301 struct ec_node *node = NULL;
304 node = ec_node_from_type(&ec_node_uint_type, id);
308 config = ec_config_dict();
312 ret = ec_config_dict_set(config, "min", ec_config_u64(min));
315 ret = ec_config_dict_set(config, "max", ec_config_u64(max));
318 ret = ec_config_dict_set(config, "base", ec_config_u64(base));
322 ret = ec_node_set_config(node, config);
330 ec_config_free(config);
335 int ec_node_int_getval(const struct ec_node *node, const char *str,
338 struct ec_node_int_uint *priv = ec_node_priv(node);
341 ret = ec_node_check_type(node, &ec_node_int_type);
345 if (parse_llint(priv, str, result) < 0)
351 int ec_node_uint_getval(const struct ec_node *node, const char *str,
354 struct ec_node_int_uint *priv = ec_node_priv(node);
357 ret = ec_node_check_type(node, &ec_node_uint_type);
361 if (parse_ullint(priv, str, result) < 0)
367 /* LCOV_EXCL_START */
368 static int ec_node_int_testcase(void)
371 struct ec_node *node;
377 node = ec_node_uint(EC_NO_ID, 1, 256, 0);
379 EC_LOG(EC_LOG_ERR, "cannot create node\n");
382 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
383 testres |= EC_TEST_CHECK_PARSE(node, -1, "0");
384 testres |= EC_TEST_CHECK_PARSE(node, 1, "1");
385 testres |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
386 testres |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
387 testres |= EC_TEST_CHECK_PARSE(node, 1, " 1");
388 testres |= EC_TEST_CHECK_PARSE(node, -1, "-1");
389 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
390 testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
391 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x100000000000000000");
392 testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
394 p = ec_parse(node, "1");
395 s = ec_strvec_val(ec_pnode_get_strvec(p), 0);
396 testres |= EC_TEST_CHECK(s != NULL &&
397 ec_node_uint_getval(node, s, &u64) == 0 &&
398 u64 == 1, "bad integer value");
401 p = ec_parse(node, "10");
402 s = ec_strvec_val(ec_pnode_get_strvec(p), 0);
403 testres |= EC_TEST_CHECK(s != NULL &&
404 ec_node_uint_getval(node, s, &u64) == 0 &&
405 u64 == 10, "bad integer value");
409 node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
411 EC_LOG(EC_LOG_ERR, "cannot create node\n");
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, "7fffffffffffffff");
417 testres |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
418 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x8000000000000000");
419 testres |= EC_TEST_CHECK_PARSE(node, -1, "-2");
420 testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
421 testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
423 p = ec_parse(node, "10");
424 s = ec_strvec_val(ec_pnode_get_strvec(p), 0);
425 testres |= EC_TEST_CHECK(s != NULL &&
426 ec_node_int_getval(node, s, &i64) == 0 &&
427 i64 == 16, "bad integer value");
431 node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
433 EC_LOG(EC_LOG_ERR, "cannot create node\n");
436 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
437 testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
438 testres |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
439 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
440 testres |= EC_TEST_CHECK_PARSE(node, -1, "1");
443 /* test completion */
444 node = ec_node_int(EC_NO_ID, 0, 10, 0);
446 EC_LOG(EC_LOG_ERR, "cannot create node\n");
449 testres |= EC_TEST_CHECK_COMPLETE(node,
452 testres |= EC_TEST_CHECK_COMPLETE(node,
455 testres |= EC_TEST_CHECK_COMPLETE(node,
464 static struct ec_test ec_node_int_test = {
466 .test = ec_node_int_testcase,
469 EC_TEST_REGISTER(ec_node_int_test);