2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <ecoli_log.h>
38 #include <ecoli_malloc.h>
39 #include <ecoli_strvec.h>
40 #include <ecoli_node.h>
41 #include <ecoli_parsed.h>
42 #include <ecoli_completed.h>
43 #include <ecoli_node_int.h>
44 #include <ecoli_test.h>
46 EC_LOG_TYPE_REGISTER(node_int);
48 /* common to int and uint */
49 struct ec_node_int_uint {
65 static int parse_llint(struct ec_node_int_uint *node, const char *str,
71 *val = strtoll(str, &endptr, node->base);
73 if (errno == ERANGE && (*val == LLONG_MAX || *val == LLONG_MIN))
76 if (errno != 0 && *val == 0)
79 if (node->check_min && *val < node->min)
82 if (node->check_max && *val > node->max)
91 static int parse_ullint(struct ec_node_int_uint *node, const char *str,
96 /* since a negative input is silently converted to a positive
97 * one by strtoull(), first check that it is positive */
102 *val = strtoull(str, &endptr, node->base);
104 if (errno == ERANGE && *val == ULLONG_MAX)
107 if (errno != 0 && *val == 0)
110 if (node->check_min && *val < node->umin)
113 if (node->check_max && *val > node->umax)
122 static int ec_node_int_uint_parse(const struct ec_node *gen_node,
123 struct ec_parsed *state,
124 const struct ec_strvec *strvec)
126 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
132 if (ec_strvec_len(strvec) == 0)
133 return EC_PARSED_NOMATCH;
135 str = ec_strvec_val(strvec, 0);
136 if (parse_llint(node, str, &val) < 0)
137 return EC_PARSED_NOMATCH;
143 ec_node_uint_init_priv(struct ec_node *gen_node)
145 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
147 node->is_signed = true;
152 static struct ec_node_type ec_node_int_type = {
154 .parse = ec_node_int_uint_parse,
155 .complete = ec_node_default_complete,
156 .size = sizeof(struct ec_node_int_uint),
157 .init_priv = ec_node_uint_init_priv,
160 EC_NODE_TYPE_REGISTER(ec_node_int_type);
162 struct ec_node *ec_node_int(const char *id, int64_t min,
163 int64_t max, unsigned int base)
165 struct ec_node *gen_node = NULL;
167 gen_node = __ec_node(&ec_node_int_type, id);
168 if (gen_node == NULL)
171 if (ec_node_int_set_limits(gen_node, min, max) < 0)
173 if (ec_node_int_set_base(gen_node, base) < 0)
179 ec_node_free(gen_node);
183 static struct ec_node_type ec_node_uint_type = {
185 .parse = ec_node_int_uint_parse,
186 .complete = ec_node_default_complete,
187 .size = sizeof(struct ec_node_int_uint),
190 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
192 struct ec_node *ec_node_uint(const char *id, uint64_t min,
193 uint64_t max, unsigned int base)
195 struct ec_node *gen_node = NULL;
197 gen_node = __ec_node(&ec_node_uint_type, id);
198 if (gen_node == NULL)
201 if (ec_node_uint_set_limits(gen_node, min, max) < 0)
203 if (ec_node_uint_set_base(gen_node, base) < 0)
209 ec_node_free(gen_node);
213 int ec_node_int_disable_limits(struct ec_node *gen_node)
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);
222 node->check_min = false;
223 node->check_max = false;
228 int ec_node_int_set_limits(struct ec_node *gen_node, int64_t min,
231 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
234 ret = ec_node_check_type(gen_node, &ec_node_int_type);
243 node->check_min = true;
245 node->check_max = true;
251 int ec_node_int_set_base(struct ec_node *gen_node, unsigned int base)
253 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
256 ret = ec_node_check_type(gen_node, &ec_node_int_type);
266 int ec_node_uint_disable_limits(struct ec_node *gen_node)
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);
275 node->check_min = false;
276 node->check_max = false;
281 int ec_node_uint_set_limits(struct ec_node *gen_node, uint64_t min,
284 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
287 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
296 node->check_min = true;
298 node->check_max = true;
304 int ec_node_uint_set_base(struct ec_node *gen_node, unsigned int base)
306 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
309 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
318 int ec_node_int_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_int_type);
328 if (parse_llint(node, str, result) < 0)
334 int ec_node_uint_getval(const struct ec_node *gen_node, const char *str,
337 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
340 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
344 if (parse_ullint(node, str, result) < 0)
350 /* LCOV_EXCL_START */
351 static int ec_node_int_testcase(void)
354 struct ec_node *node;
360 node = ec_node_uint(EC_NO_ID, 0, 256, 0);
362 EC_LOG(EC_LOG_ERR, "cannot create node\n");
365 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
366 ret |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
367 ret |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
368 ret |= EC_TEST_CHECK_PARSE(node, 1, " 1");
369 ret |= EC_TEST_CHECK_PARSE(node, -1, "-1");
370 ret |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
372 p = ec_node_parse(node, "0");
373 s = ec_strvec_val(ec_parsed_strvec(p), 0);
374 EC_TEST_ASSERT(s != NULL &&
375 ec_node_uint_getval(node, s, &u64) == 0 &&
379 p = ec_node_parse(node, "10");
380 s = ec_strvec_val(ec_parsed_strvec(p), 0);
381 EC_TEST_ASSERT(s != NULL &&
382 ec_node_uint_getval(node, s, &u64) == 0 &&
387 node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
389 EC_LOG(EC_LOG_ERR, "cannot create node\n");
392 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
393 ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
394 ret |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
395 ret |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
396 ret |= EC_TEST_CHECK_PARSE(node, -1, "-2");
398 p = ec_node_parse(node, "10");
399 s = ec_strvec_val(ec_parsed_strvec(p), 0);
400 EC_TEST_ASSERT(s != NULL &&
401 ec_node_int_getval(node, s, &i64) == 0 &&
406 node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
408 EC_LOG(EC_LOG_ERR, "cannot create node\n");
411 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
412 ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
413 ret |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
414 ret |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
415 ret |= EC_TEST_CHECK_PARSE(node, -1, "1");
418 /* test completion */
419 node = ec_node_int(EC_NO_ID, 0, 10, 0);
421 EC_LOG(EC_LOG_ERR, "cannot create node\n");
424 ret |= EC_TEST_CHECK_COMPLETE(node,
427 ret |= EC_TEST_CHECK_COMPLETE(node,
428 "x", EC_NODE_ENDLIST,
430 ret |= EC_TEST_CHECK_COMPLETE(node,
431 "1", EC_NODE_ENDLIST,
439 static struct ec_test ec_node_int_test = {
441 .test = ec_node_int_testcase,
444 EC_TEST_REGISTER(ec_node_int_test);