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,
48 *val = strtoll(str, &endptr, node->base);
50 if ((errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN)) ||
51 (errno != 0 && *val == 0))
54 if (node->check_min && *val < node->min) {
59 if (node->check_max && *val > node->max) {
72 static int parse_ullint(struct ec_node_int_uint *node, const char *str,
77 /* since a negative input is silently converted to a positive
78 * one by strtoull(), first check that it is positive */
83 *val = strtoull(str, &endptr, node->base);
85 if ((errno == ERANGE && *val == ULLONG_MAX) ||
86 (errno != 0 && *val == 0))
89 if (node->check_min && *val < node->umin)
92 if (node->check_max && *val > node->umax)
101 static int ec_node_int_uint_parse(const struct ec_node *gen_node,
102 struct ec_parse *state,
103 const struct ec_strvec *strvec)
105 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
112 if (ec_strvec_len(strvec) == 0)
113 return EC_PARSE_NOMATCH;
115 str = ec_strvec_val(strvec, 0);
116 if (node->is_signed) {
117 if (parse_llint(node, str, &i64) < 0)
118 return EC_PARSE_NOMATCH;
120 if (parse_ullint(node, str, &u64) < 0)
121 return EC_PARSE_NOMATCH;
127 ec_node_uint_init_priv(struct ec_node *gen_node)
129 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
131 node->is_signed = true;
136 static struct ec_node_type ec_node_int_type = {
138 .parse = ec_node_int_uint_parse,
139 .complete = ec_node_complete_unknown,
140 .size = sizeof(struct ec_node_int_uint),
141 .init_priv = ec_node_uint_init_priv,
144 EC_NODE_TYPE_REGISTER(ec_node_int_type);
146 struct ec_node *ec_node_int(const char *id, int64_t min,
147 int64_t max, unsigned int base)
149 struct ec_node *gen_node = NULL;
151 gen_node = __ec_node(&ec_node_int_type, id);
152 if (gen_node == NULL)
155 if (ec_node_int_set_limits(gen_node, min, max) < 0)
157 if (ec_node_int_set_base(gen_node, base) < 0)
163 ec_node_free(gen_node);
167 static struct ec_node_type ec_node_uint_type = {
169 .parse = ec_node_int_uint_parse,
170 .complete = ec_node_complete_unknown,
171 .size = sizeof(struct ec_node_int_uint),
174 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
176 struct ec_node *ec_node_uint(const char *id, uint64_t min,
177 uint64_t max, unsigned int base)
179 struct ec_node *gen_node = NULL;
181 gen_node = __ec_node(&ec_node_uint_type, id);
182 if (gen_node == NULL)
185 if (ec_node_uint_set_limits(gen_node, min, max) < 0)
187 if (ec_node_uint_set_base(gen_node, base) < 0)
193 ec_node_free(gen_node);
197 int ec_node_int_disable_limits(struct ec_node *gen_node)
199 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
202 ret = ec_node_check_type(gen_node, &ec_node_int_type);
206 node->check_min = false;
207 node->check_max = false;
212 int ec_node_int_set_limits(struct ec_node *gen_node, int64_t min,
215 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
218 ret = ec_node_check_type(gen_node, &ec_node_int_type);
227 node->check_min = true;
229 node->check_max = true;
235 int ec_node_int_set_base(struct ec_node *gen_node, unsigned int base)
237 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
240 ret = ec_node_check_type(gen_node, &ec_node_int_type);
250 int ec_node_uint_disable_limits(struct ec_node *gen_node)
252 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
255 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
259 node->check_min = false;
260 node->check_max = false;
265 int ec_node_uint_set_limits(struct ec_node *gen_node, uint64_t min,
268 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
271 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
280 node->check_min = true;
282 node->check_max = true;
288 int ec_node_uint_set_base(struct ec_node *gen_node, unsigned int base)
290 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
293 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
302 int ec_node_int_getval(const struct ec_node *gen_node, const char *str,
305 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
308 ret = ec_node_check_type(gen_node, &ec_node_int_type);
312 if (parse_llint(node, str, result) < 0)
318 int ec_node_uint_getval(const struct ec_node *gen_node, const char *str,
321 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
324 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
328 if (parse_ullint(node, str, result) < 0)
334 /* LCOV_EXCL_START */
335 static int ec_node_int_testcase(void)
338 struct ec_node *node;
340 int testres = 0, ret;
344 node = ec_node_uint(EC_NO_ID, 1, 256, 0);
346 EC_LOG(EC_LOG_ERR, "cannot create node\n");
349 testres |= EC_TEST_CHECK_PARSE(node, -1, "");
350 testres |= EC_TEST_CHECK_PARSE(node, -1, "0");
351 testres |= EC_TEST_CHECK_PARSE(node, 1, "1");
352 testres |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
353 testres |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
354 testres |= EC_TEST_CHECK_PARSE(node, 1, " 1");
355 testres |= EC_TEST_CHECK_PARSE(node, -1, "-1");
356 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
357 testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
358 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x100000000000000000");
359 testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
360 ret = ec_node_uint_disable_limits(node);
361 testres |= EC_TEST_CHECK(ret == 0, "cannot disable limits");
362 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
364 p = ec_node_parse(node, "1");
365 s = ec_strvec_val(ec_parse_strvec(p), 0);
366 testres |= EC_TEST_CHECK(s != NULL &&
367 ec_node_uint_getval(node, s, &u64) == 0 &&
368 u64 == 1, "bad integer value");
371 p = ec_node_parse(node, "10");
372 s = ec_strvec_val(ec_parse_strvec(p), 0);
373 testres |= EC_TEST_CHECK(s != NULL &&
374 ec_node_uint_getval(node, s, &u64) == 0 &&
375 u64 == 10, "bad integer value");
379 node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
381 EC_LOG(EC_LOG_ERR, "cannot create node\n");
384 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
385 testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
386 testres |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
387 testres |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
388 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x8000000000000000");
389 testres |= EC_TEST_CHECK_PARSE(node, -1, "-2");
390 testres |= EC_TEST_CHECK_PARSE(node, -1, "zzz");
391 testres |= EC_TEST_CHECK_PARSE(node, -1, "4r");
392 ret = ec_node_int_disable_limits(node);
393 testres |= EC_TEST_CHECK(ret == 0, "cannot disable limits");
394 testres |= EC_TEST_CHECK_PARSE(node, 1, "-2");
396 p = ec_node_parse(node, "10");
397 s = ec_strvec_val(ec_parse_strvec(p), 0);
398 testres |= EC_TEST_CHECK(s != NULL &&
399 ec_node_int_getval(node, s, &i64) == 0 &&
400 i64 == 16, "bad integer value");
404 node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
406 EC_LOG(EC_LOG_ERR, "cannot create node\n");
409 testres |= EC_TEST_CHECK_PARSE(node, 1, "0");
410 testres |= EC_TEST_CHECK_PARSE(node, 1, "-1");
411 testres |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
412 testres |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
413 testres |= EC_TEST_CHECK_PARSE(node, -1, "1");
416 /* test completion */
417 node = ec_node_int(EC_NO_ID, 0, 10, 0);
419 EC_LOG(EC_LOG_ERR, "cannot create node\n");
422 testres |= EC_TEST_CHECK_COMPLETE(node,
425 testres |= EC_TEST_CHECK_COMPLETE(node,
426 "x", EC_NODE_ENDLIST,
428 testres |= EC_TEST_CHECK_COMPLETE(node,
429 "1", EC_NODE_ENDLIST,
437 static struct ec_test ec_node_int_test = {
439 .test = ec_node_int_testcase,
442 EC_TEST_REGISTER(ec_node_int_test);