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 {
44 static int parse_llint(struct ec_node_int_uint *node, const char *str,
58 return ec_str_parse_llint(str, node->base, min, max, val);
61 static int parse_ullint(struct ec_node_int_uint *node, const char *str,
75 return ec_str_parse_ullint(str, node->base, min, max, val);
78 static int ec_node_int_uint_parse(const struct ec_node *gen_node,
79 struct ec_parse *state,
80 const struct ec_strvec *strvec)
82 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
89 if (ec_strvec_len(strvec) == 0)
90 return EC_PARSE_NOMATCH;
92 str = ec_strvec_val(strvec, 0);
93 if (node->is_signed) {
94 if (parse_llint(node, str, &i64) < 0)
95 return EC_PARSE_NOMATCH;
97 if (parse_ullint(node, str, &u64) < 0)
98 return EC_PARSE_NOMATCH;
104 ec_node_uint_init_priv(struct ec_node *gen_node)
106 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
108 node->is_signed = true;
113 static const struct ec_config_schema ec_node_int_schema[] = {
116 .desc = "The minimum valid value (included).",
117 .type = EC_CONFIG_TYPE_INT64,
121 .desc = "The maximum valid value (included).",
122 .type = EC_CONFIG_TYPE_INT64,
126 .desc = "The base to use. If unset or 0, try to guess.",
127 .type = EC_CONFIG_TYPE_UINT64,
130 .type = EC_CONFIG_TYPE_NONE,
134 static int ec_node_int_set_config(struct ec_node *gen_node,
135 const struct ec_config *config)
137 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
138 const struct ec_config *min_value = NULL;
139 const struct ec_config *max_value = NULL;
140 const struct ec_config *base_value = NULL;
143 min_value = ec_config_dict_get(config, "min");
144 max_value = ec_config_dict_get(config, "max");
145 base_value = ec_config_dict_get(config, "base");
147 if (min_value && max_value && min_value->i64 > max_value->i64) {
152 if (min_value != NULL) {
153 node->check_min = true;
154 node->min = min_value->i64;
156 node->check_min = false;
158 if (max_value != NULL) {
159 node->check_max = true;
160 node->max = max_value->i64;
162 node->check_min = false;
164 if (base_value != NULL)
165 node->base = base_value->u64;
176 static struct ec_node_type ec_node_int_type = {
178 .schema = ec_node_int_schema,
179 .set_config = ec_node_int_set_config,
180 .parse = ec_node_int_uint_parse,
181 .complete = ec_node_complete_unknown,
182 .size = sizeof(struct ec_node_int_uint),
183 .init_priv = ec_node_uint_init_priv,
186 EC_NODE_TYPE_REGISTER(ec_node_int_type);
188 struct ec_node *ec_node_int(const char *id, int64_t min,
189 int64_t max, unsigned int base)
191 struct ec_config *config = NULL;
192 struct ec_node *gen_node = NULL;
195 gen_node = ec_node_from_type(&ec_node_int_type, id);
196 if (gen_node == NULL)
199 config = ec_config_dict();
203 ret = ec_config_dict_set(config, "min", ec_config_i64(min));
206 ret = ec_config_dict_set(config, "max", ec_config_i64(max));
209 ret = ec_config_dict_set(config, "base", ec_config_u64(base));
213 ret = ec_node_set_config(gen_node, config);
221 ec_config_free(config);
222 ec_node_free(gen_node);
226 static const struct ec_config_schema ec_node_uint_schema[] = {
229 .desc = "The minimum valid value (included).",
230 .type = EC_CONFIG_TYPE_UINT64,
234 .desc = "The maximum valid value (included).",
235 .type = EC_CONFIG_TYPE_UINT64,
239 .desc = "The base to use. If unset or 0, try to guess.",
240 .type = EC_CONFIG_TYPE_UINT64,
243 .type = EC_CONFIG_TYPE_NONE,
247 static int ec_node_uint_set_config(struct ec_node *gen_node,
248 const struct ec_config *config)
250 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
251 const struct ec_config *min_value = NULL;
252 const struct ec_config *max_value = NULL;
253 const struct ec_config *base_value = NULL;
256 min_value = ec_config_dict_get(config, "min");
257 max_value = ec_config_dict_get(config, "max");
258 base_value = ec_config_dict_get(config, "base");
260 if (min_value && max_value && min_value->u64 > max_value->u64) {
265 if (min_value != NULL) {
266 node->check_min = true;
267 node->min = min_value->u64;
269 node->check_min = false;
271 if (max_value != NULL) {
272 node->check_max = true;
273 node->max = max_value->u64;
275 node->check_min = false;
277 if (base_value != NULL)
278 node->base = base_value->u64;
289 static struct ec_node_type ec_node_uint_type = {
291 .schema = ec_node_uint_schema,
292 .set_config = ec_node_uint_set_config,
293 .parse = ec_node_int_uint_parse,
294 .complete = ec_node_complete_unknown,
295 .size = sizeof(struct ec_node_int_uint),
298 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
300 struct ec_node *ec_node_uint(const char *id, uint64_t min,
301 uint64_t max, unsigned int base)
303 struct ec_config *config = NULL;
304 struct ec_node *gen_node = NULL;
307 gen_node = ec_node_from_type(&ec_node_uint_type, id);
308 if (gen_node == NULL)
311 config = ec_config_dict();
315 ret = ec_config_dict_set(config, "min", ec_config_u64(min));
318 ret = ec_config_dict_set(config, "max", ec_config_u64(max));
321 ret = ec_config_dict_set(config, "base", ec_config_u64(base));
325 ret = ec_node_set_config(gen_node, config);
333 ec_config_free(config);
334 ec_node_free(gen_node);
338 int ec_node_int_getval(const struct ec_node *gen_node, const char *str,
341 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
344 ret = ec_node_check_type(gen_node, &ec_node_int_type);
348 if (parse_llint(node, str, result) < 0)
354 int ec_node_uint_getval(const struct ec_node *gen_node, const char *str,
357 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
360 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
364 if (parse_ullint(node, str, result) < 0)
370 /* LCOV_EXCL_START */
371 static int ec_node_int_testcase(void)
374 struct ec_node *node;
380 node = ec_node_uint(EC_NO_ID, 1, 256, 0);
382 EC_LOG(EC_LOG_ERR, "cannot create node\n");
385 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
386 testres |= EC_TEST_CHECK_PARSE(node, -1, "0");
387 testres |= EC_TEST_CHECK_PARSE(node, 1, "1");
388 testres |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
389 testres |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
390 testres |= EC_TEST_CHECK_PARSE(node, 1, " 1");
391 testres |= EC_TEST_CHECK_PARSE(node, -1, "-1");
392 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
393 testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
394 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x100000000000000000");
395 testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
397 p = ec_node_parse(node, "1");
398 s = ec_strvec_val(ec_parse_strvec(p), 0);
399 testres |= EC_TEST_CHECK(s != NULL &&
400 ec_node_uint_getval(node, s, &u64) == 0 &&
401 u64 == 1, "bad integer value");
404 p = ec_node_parse(node, "10");
405 s = ec_strvec_val(ec_parse_strvec(p), 0);
406 testres |= EC_TEST_CHECK(s != NULL &&
407 ec_node_uint_getval(node, s, &u64) == 0 &&
408 u64 == 10, "bad integer value");
412 node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
414 EC_LOG(EC_LOG_ERR, "cannot create node\n");
417 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
418 testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
419 testres |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
420 testres |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
421 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x8000000000000000");
422 testres |= EC_TEST_CHECK_PARSE(node, -1, "-2");
423 testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
424 testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
426 p = ec_node_parse(node, "10");
427 s = ec_strvec_val(ec_parse_strvec(p), 0);
428 testres |= EC_TEST_CHECK(s != NULL &&
429 ec_node_int_getval(node, s, &i64) == 0 &&
430 i64 == 16, "bad integer value");
434 node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
436 EC_LOG(EC_LOG_ERR, "cannot create node\n");
439 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
440 testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
441 testres |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
442 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
443 testres |= EC_TEST_CHECK_PARSE(node, -1, "1");
446 /* test completion */
447 node = ec_node_int(EC_NO_ID, 0, 10, 0);
449 EC_LOG(EC_LOG_ERR, "cannot create node\n");
452 testres |= EC_TEST_CHECK_COMPLETE(node,
455 testres |= EC_TEST_CHECK_COMPLETE(node,
456 "x", EC_NODE_ENDLIST,
458 testres |= EC_TEST_CHECK_COMPLETE(node,
459 "1", EC_NODE_ENDLIST,
467 static struct ec_test ec_node_int_test = {
469 .test = ec_node_int_testcase,
472 EC_TEST_REGISTER(ec_node_int_test);