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_parse *state,
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 .complete = ec_node_complete_unknown,
181 .size = sizeof(struct ec_node_int_uint),
182 .init_priv = ec_node_uint_init_priv,
185 EC_NODE_TYPE_REGISTER(ec_node_int_type);
187 struct ec_node *ec_node_int(const char *id, int64_t min,
188 int64_t max, unsigned int base)
190 struct ec_config *config = NULL;
191 struct ec_node *node = NULL;
194 node = ec_node_from_type(&ec_node_int_type, id);
198 config = ec_config_dict();
202 ret = ec_config_dict_set(config, "min", ec_config_i64(min));
205 ret = ec_config_dict_set(config, "max", ec_config_i64(max));
208 ret = ec_config_dict_set(config, "base", ec_config_u64(base));
212 ret = ec_node_set_config(node, config);
220 ec_config_free(config);
225 static const struct ec_config_schema ec_node_uint_schema[] = {
228 .desc = "The minimum valid value (included).",
229 .type = EC_CONFIG_TYPE_UINT64,
233 .desc = "The maximum valid value (included).",
234 .type = EC_CONFIG_TYPE_UINT64,
238 .desc = "The base to use. If unset or 0, try to guess.",
239 .type = EC_CONFIG_TYPE_UINT64,
242 .type = EC_CONFIG_TYPE_NONE,
246 static int ec_node_uint_set_config(struct ec_node *node,
247 const struct ec_config *config)
249 struct ec_node_int_uint *priv = ec_node_priv(node);
250 const struct ec_config *min_value = NULL;
251 const struct ec_config *max_value = NULL;
252 const struct ec_config *base_value = NULL;
255 min_value = ec_config_dict_get(config, "min");
256 max_value = ec_config_dict_get(config, "max");
257 base_value = ec_config_dict_get(config, "base");
259 if (min_value && max_value && min_value->u64 > max_value->u64) {
264 if (min_value != NULL) {
265 priv->check_min = true;
266 priv->min = min_value->u64;
268 priv->check_min = false;
270 if (max_value != NULL) {
271 priv->check_max = true;
272 priv->max = max_value->u64;
274 priv->check_min = false;
276 if (base_value != NULL)
277 priv->base = base_value->u64;
288 static struct ec_node_type ec_node_uint_type = {
290 .schema = ec_node_uint_schema,
291 .set_config = ec_node_uint_set_config,
292 .parse = ec_node_int_uint_parse,
293 .complete = ec_node_complete_unknown,
294 .size = sizeof(struct ec_node_int_uint),
297 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
299 struct ec_node *ec_node_uint(const char *id, uint64_t min,
300 uint64_t max, unsigned int base)
302 struct ec_config *config = NULL;
303 struct ec_node *node = NULL;
306 node = ec_node_from_type(&ec_node_uint_type, id);
310 config = ec_config_dict();
314 ret = ec_config_dict_set(config, "min", ec_config_u64(min));
317 ret = ec_config_dict_set(config, "max", ec_config_u64(max));
320 ret = ec_config_dict_set(config, "base", ec_config_u64(base));
324 ret = ec_node_set_config(node, config);
332 ec_config_free(config);
337 int ec_node_int_getval(const struct ec_node *node, const char *str,
340 struct ec_node_int_uint *priv = ec_node_priv(node);
343 ret = ec_node_check_type(node, &ec_node_int_type);
347 if (parse_llint(priv, str, result) < 0)
353 int ec_node_uint_getval(const struct ec_node *node, const char *str,
356 struct ec_node_int_uint *priv = ec_node_priv(node);
359 ret = ec_node_check_type(node, &ec_node_uint_type);
363 if (parse_ullint(priv, str, result) < 0)
369 /* LCOV_EXCL_START */
370 static int ec_node_int_testcase(void)
373 struct ec_node *node;
379 node = ec_node_uint(EC_NO_ID, 1, 256, 0);
381 EC_LOG(EC_LOG_ERR, "cannot create node\n");
384 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
385 testres |= EC_TEST_CHECK_PARSE(node, -1, "0");
386 testres |= EC_TEST_CHECK_PARSE(node, 1, "1");
387 testres |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
388 testres |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
389 testres |= EC_TEST_CHECK_PARSE(node, 1, " 1");
390 testres |= EC_TEST_CHECK_PARSE(node, -1, "-1");
391 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
392 testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
393 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x100000000000000000");
394 testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
396 p = ec_node_parse(node, "1");
397 s = ec_strvec_val(ec_parse_strvec(p), 0);
398 testres |= EC_TEST_CHECK(s != NULL &&
399 ec_node_uint_getval(node, s, &u64) == 0 &&
400 u64 == 1, "bad integer value");
403 p = ec_node_parse(node, "10");
404 s = ec_strvec_val(ec_parse_strvec(p), 0);
405 testres |= EC_TEST_CHECK(s != NULL &&
406 ec_node_uint_getval(node, s, &u64) == 0 &&
407 u64 == 10, "bad integer value");
411 node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
413 EC_LOG(EC_LOG_ERR, "cannot create node\n");
416 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
417 testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
418 testres |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
419 testres |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
420 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x8000000000000000");
421 testres |= EC_TEST_CHECK_PARSE(node, -1, "-2");
422 testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
423 testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
425 p = ec_node_parse(node, "10");
426 s = ec_strvec_val(ec_parse_strvec(p), 0);
427 testres |= EC_TEST_CHECK(s != NULL &&
428 ec_node_int_getval(node, s, &i64) == 0 &&
429 i64 == 16, "bad integer value");
433 node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
435 EC_LOG(EC_LOG_ERR, "cannot create node\n");
438 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
439 testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
440 testres |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
441 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
442 testres |= EC_TEST_CHECK_PARSE(node, -1, "1");
445 /* test completion */
446 node = ec_node_int(EC_NO_ID, 0, 10, 0);
448 EC_LOG(EC_LOG_ERR, "cannot create node\n");
451 testres |= EC_TEST_CHECK_COMPLETE(node,
454 testres |= EC_TEST_CHECK_COMPLETE(node,
455 "x", EC_NODE_ENDLIST,
457 testres |= EC_TEST_CHECK_COMPLETE(node,
458 "1", EC_NODE_ENDLIST,
466 static struct ec_test ec_node_int_test = {
468 .test = ec_node_int_testcase,
471 EC_TEST_REGISTER(ec_node_int_test);