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;
142 static size_t ec_node_int_uint_get_max_parse_len(const struct ec_node *gen_node)
149 ec_node_uint_init_priv(struct ec_node *gen_node)
151 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
153 node->is_signed = true;
158 static struct ec_node_type ec_node_int_type = {
160 .parse = ec_node_int_uint_parse,
161 .complete = ec_node_default_complete,
162 .get_max_parse_len = ec_node_int_uint_get_max_parse_len,
163 .size = sizeof(struct ec_node_int_uint),
164 .init_priv = ec_node_uint_init_priv,
167 EC_NODE_TYPE_REGISTER(ec_node_int_type);
169 struct ec_node *ec_node_int(const char *id, int64_t min,
170 int64_t max, unsigned int base)
172 struct ec_node *gen_node = NULL;
174 gen_node = __ec_node(&ec_node_int_type, id);
175 if (gen_node == NULL)
178 if (ec_node_int_set_limits(gen_node, min, max) < 0)
180 if (ec_node_int_set_base(gen_node, base) < 0)
186 ec_node_free(gen_node);
190 static struct ec_node_type ec_node_uint_type = {
192 .parse = ec_node_int_uint_parse,
193 .complete = ec_node_default_complete,
194 .get_max_parse_len = ec_node_int_uint_get_max_parse_len,
195 .size = sizeof(struct ec_node_int_uint),
198 EC_NODE_TYPE_REGISTER(ec_node_uint_type);
200 struct ec_node *ec_node_uint(const char *id, uint64_t min,
201 uint64_t max, unsigned int base)
203 struct ec_node *gen_node = NULL;
205 gen_node = __ec_node(&ec_node_uint_type, id);
206 if (gen_node == NULL)
209 if (ec_node_uint_set_limits(gen_node, min, max) < 0)
211 if (ec_node_uint_set_base(gen_node, base) < 0)
217 ec_node_free(gen_node);
221 int ec_node_int_disable_limits(struct ec_node *gen_node)
223 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
226 ret = ec_node_check_type(gen_node, &ec_node_int_type);
230 node->check_min = false;
231 node->check_max = false;
236 int ec_node_int_set_limits(struct ec_node *gen_node, int64_t min,
239 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
242 ret = ec_node_check_type(gen_node, &ec_node_int_type);
251 node->check_min = true;
253 node->check_max = true;
259 int ec_node_int_set_base(struct ec_node *gen_node, unsigned int base)
261 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
264 ret = ec_node_check_type(gen_node, &ec_node_int_type);
274 int ec_node_uint_disable_limits(struct ec_node *gen_node)
276 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
279 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
283 node->check_min = false;
284 node->check_max = false;
289 int ec_node_uint_set_limits(struct ec_node *gen_node, uint64_t min,
292 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
295 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
304 node->check_min = true;
306 node->check_max = true;
312 int ec_node_uint_set_base(struct ec_node *gen_node, unsigned int base)
314 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
317 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
326 int ec_node_int_getval(const struct ec_node *gen_node, const char *str,
329 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
332 ret = ec_node_check_type(gen_node, &ec_node_int_type);
336 if (parse_llint(node, str, result) < 0)
342 int ec_node_uint_getval(const struct ec_node *gen_node, const char *str,
345 struct ec_node_int_uint *node = (struct ec_node_int_uint *)gen_node;
348 ret = ec_node_check_type(gen_node, &ec_node_uint_type);
352 if (parse_ullint(node, str, result) < 0)
358 /* LCOV_EXCL_START */
359 static int ec_node_int_testcase(void)
362 struct ec_node *node;
368 node = ec_node_uint(EC_NO_ID, 0, 256, 0);
370 EC_LOG(EC_LOG_ERR, "cannot create node\n");
373 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
374 ret |= EC_TEST_CHECK_PARSE(node, 1, "256", "foo");
375 ret |= EC_TEST_CHECK_PARSE(node, 1, "0x100");
376 ret |= EC_TEST_CHECK_PARSE(node, 1, " 1");
377 ret |= EC_TEST_CHECK_PARSE(node, -1, "-1");
378 ret |= EC_TEST_CHECK_PARSE(node, -1, "0x101");
380 p = ec_node_parse(node, "0");
381 s = ec_strvec_val(ec_parsed_strvec(p), 0);
382 EC_TEST_ASSERT(s != NULL);
383 EC_TEST_ASSERT(ec_node_uint_getval(node, s, &u64) == 0);
384 EC_TEST_ASSERT (u64 == 0);
387 p = ec_node_parse(node, "10");
388 s = ec_strvec_val(ec_parsed_strvec(p), 0);
389 EC_TEST_ASSERT(s != NULL);
390 EC_TEST_ASSERT(ec_node_uint_getval(node, s, &u64) == 0);
391 EC_TEST_ASSERT(u64 == 10);
395 node = ec_node_int(EC_NO_ID, -1, LLONG_MAX, 16);
397 EC_LOG(EC_LOG_ERR, "cannot create node\n");
400 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
401 ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
402 ret |= EC_TEST_CHECK_PARSE(node, 1, "7fffffffffffffff");
403 ret |= EC_TEST_CHECK_PARSE(node, 1, "0x7fffffffffffffff");
404 ret |= EC_TEST_CHECK_PARSE(node, -1, "-2");
406 p = ec_node_parse(node, "10");
407 s = ec_strvec_val(ec_parsed_strvec(p), 0);
408 EC_TEST_ASSERT(s != NULL);
409 EC_TEST_ASSERT(ec_node_int_getval(node, s, &i64) == 0);
410 EC_TEST_ASSERT(i64 == 16);
414 node = ec_node_int(EC_NO_ID, LLONG_MIN, 0, 10);
416 EC_LOG(EC_LOG_ERR, "cannot create node\n");
419 ret |= EC_TEST_CHECK_PARSE(node, 1, "0");
420 ret |= EC_TEST_CHECK_PARSE(node, 1, "-1");
421 ret |= EC_TEST_CHECK_PARSE(node, 1, "-9223372036854775808");
422 ret |= EC_TEST_CHECK_PARSE(node, -1, "0x0");
423 ret |= EC_TEST_CHECK_PARSE(node, -1, "1");
426 /* test completion */
427 node = ec_node_int(EC_NO_ID, 0, 10, 0);
429 EC_LOG(EC_LOG_ERR, "cannot create node\n");
432 ret |= EC_TEST_CHECK_COMPLETE(node,
435 ret |= EC_TEST_CHECK_COMPLETE(node,
436 "x", EC_NODE_ENDLIST,
438 ret |= EC_TEST_CHECK_COMPLETE(node,
439 "1", EC_NODE_ENDLIST,
447 static struct ec_test ec_node_int_test = {
449 .test = ec_node_int_testcase,
452 EC_TEST_REGISTER(ec_node_int_test);