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_parse.h>
19 #include <ecoli_complete.h>
20 #include <ecoli_node_int.h>
21 #include <ecoli_test.h>
23 EC_LOG_TYPE_REGISTER(node_int);
25 /* common to int and uint */
26 struct ec_node_int_uint {
42 static int parse_llint(struct ec_node_int_uint *node, const char *str,
46 int save_errno = errno;
49 *val = strtoll(str, &endptr, node->base);
51 if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) ||
52 (errno != 0 && *val == 0))
55 if (node->check_min && *val < node->min) {
60 if (node->check_max && *val > node->max) {
74 static int parse_ullint(struct ec_node_int_uint *node, const char *str,
78 int save_errno = errno;
80 /* since a negative input is silently converted to a positive
81 * one by strtoull(), first check that it is positive */
86 *val = strtoull(str, &endptr, node->base);
88 if ((errno == ERANGE && *val == ULLONG_MAX) ||
89 (errno != 0 && *val == 0))
92 if (node->check_min && *val < node->umin)
95 if (node->check_max && *val > node->umax)
105 static int ec_node_int_uint_parse(const struct ec_node *gen_node,
106 struct ec_parse *state,
107 const struct ec_strvec *strvec)
109 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
116 if (ec_strvec_len(strvec) == 0)
117 return EC_PARSE_NOMATCH;
119 str = ec_strvec_val(strvec, 0);
120 if (node->is_signed) {
121 if (parse_llint(node, str, &i64) < 0)
122 return EC_PARSE_NOMATCH;
124 if (parse_ullint(node, str, &u64) < 0)
125 return EC_PARSE_NOMATCH;
131 ec_node_uint_init_priv(struct ec_node *gen_node)
133 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
135 node->is_signed = true;
140 static struct ec_node_type ec_node_int_type = {
142 .parse = ec_node_int_uint_parse,
143 .complete = ec_node_complete_unknown,
144 .size = sizeof(struct ec_node_int_uint),
145 .init_priv = ec_node_uint_init_priv,
148 EC_NODE_TYPE_REGISTER(ec_node_int_type);
150 struct ec_node *ec_node_int(const char *id, int64_t min,
151 int64_t max, unsigned int base)
153 struct ec_node *gen_node = NULL;
155 gen_node = __ec_node(&ec_node_int_type, id);
156 if (gen_node == NULL)
159 if (ec_node_int_set_limits(gen_node, min, max) < 0)
161 if (ec_node_int_set_base(gen_node, base) < 0)
167 ec_node_free(gen_node);
171 static struct ec_node_type ec_node_uint_type = {
173 .parse = ec_node_int_uint_parse,
174 .complete = ec_node_complete_unknown,
175 .size = sizeof(struct ec_node_int_uint),
178 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
180 struct ec_node *ec_node_uint(const char *id, uint64_t min,
181 uint64_t max, unsigned int base)
183 struct ec_node *gen_node = NULL;
185 gen_node = __ec_node(&ec_node_uint_type, id);
186 if (gen_node == NULL)
189 if (ec_node_uint_set_limits(gen_node, min, max) < 0)
191 if (ec_node_uint_set_base(gen_node, base) < 0)
197 ec_node_free(gen_node);
201 int ec_node_int_disable_limits(struct ec_node *gen_node)
203 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
206 ret = ec_node_check_type(gen_node, &ec_node_int_type);
210 node->check_min = false;
211 node->check_max = false;
216 int ec_node_int_set_limits(struct ec_node *gen_node, int64_t min,
219 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
222 ret = ec_node_check_type(gen_node, &ec_node_int_type);
231 node->check_min = true;
233 node->check_max = true;
239 int ec_node_int_set_base(struct ec_node *gen_node, unsigned int base)
241 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
244 ret = ec_node_check_type(gen_node, &ec_node_int_type);
254 int ec_node_uint_disable_limits(struct ec_node *gen_node)
256 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
259 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
263 node->check_min = false;
264 node->check_max = false;
269 int ec_node_uint_set_limits(struct ec_node *gen_node, uint64_t min,
272 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
275 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
284 node->check_min = true;
286 node->check_max = true;
292 int ec_node_uint_set_base(struct ec_node *gen_node, unsigned int base)
294 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
297 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
306 int ec_node_int_getval(const struct ec_node *gen_node, const char *str,
309 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
312 ret = ec_node_check_type(gen_node, &ec_node_int_type);
316 if (parse_llint(node, str, result) < 0)
322 int ec_node_uint_getval(const struct ec_node *gen_node, const char *str,
325 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
328 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
332 if (parse_ullint(node, str, result) < 0)
338 /* LCOV_EXCL_START */
339 static int ec_node_int_testcase(void)
342 struct ec_node *node;
344 int testres = 0, ret;
348 node = ec_node_uint(EC_NO_ID, 1, 256, 0);
350 EC_LOG(EC_LOG_ERR, "cannot create node\n");
353 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
354 testres |= EC_TEST_CHECK_PARSE(node, -1, "0");
355 testres |= EC_TEST_CHECK_PARSE(node, 1, "1");
356 testres |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
357 testres |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
358 testres |= EC_TEST_CHECK_PARSE(node, 1, " 1");
359 testres |= EC_TEST_CHECK_PARSE(node, -1, "-1");
360 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
361 testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
362 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x100000000000000000");
363 testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
364 ret = ec_node_uint_disable_limits(node);
365 testres |= EC_TEST_CHECK(ret == 0, "cannot disable limits");
366 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
368 p = ec_node_parse(node, "1");
369 s = ec_strvec_val(ec_parse_strvec(p), 0);
370 testres |= EC_TEST_CHECK(s != NULL &&
371 ec_node_uint_getval(node, s, &u64) == 0 &&
372 u64 == 1, "bad integer value");
375 p = ec_node_parse(node, "10");
376 s = ec_strvec_val(ec_parse_strvec(p), 0);
377 testres |= EC_TEST_CHECK(s != NULL &&
378 ec_node_uint_getval(node, s, &u64) == 0 &&
379 u64 == 10, "bad integer value");
383 node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
385 EC_LOG(EC_LOG_ERR, "cannot create node\n");
388 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
389 testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
390 testres |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
391 testres |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
392 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x8000000000000000");
393 testres |= EC_TEST_CHECK_PARSE(node, -1, "-2");
394 testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
395 testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
396 ret = ec_node_int_disable_limits(node);
397 testres |= EC_TEST_CHECK(ret == 0, "cannot disable limits");
398 testres |= EC_TEST_CHECK_PARSE(node, 1, "-2");
400 p = ec_node_parse(node, "10");
401 s = ec_strvec_val(ec_parse_strvec(p), 0);
402 testres |= EC_TEST_CHECK(s != NULL &&
403 ec_node_int_getval(node, s, &i64) == 0 &&
404 i64 == 16, "bad integer value");
408 node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
410 EC_LOG(EC_LOG_ERR, "cannot create node\n");
413 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
414 testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
415 testres |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
416 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
417 testres |= EC_TEST_CHECK_PARSE(node, -1, "1");
420 /* test completion */
421 node = ec_node_int(EC_NO_ID, 0, 10, 0);
423 EC_LOG(EC_LOG_ERR, "cannot create node\n");
426 testres |= EC_TEST_CHECK_COMPLETE(node,
429 testres |= EC_TEST_CHECK_COMPLETE(node,
430 "x", EC_NODE_ENDLIST,
432 testres |= EC_TEST_CHECK_COMPLETE(node,
433 "1", EC_NODE_ENDLIST,
441 static struct ec_test ec_node_int_test = {
443 .test = ec_node_int_testcase,
446 EC_TEST_REGISTER(ec_node_int_test);