2 * Copyright (c) 2009-2015, 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.
29 * Copyright (c) <2010>, Intel Corporation
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
36 * - Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
39 * - Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in
41 * the documentation and/or other materials provided with the
44 * - Neither the name of Intel Corporation nor the names of its
45 * contributors may be used to endorse or promote products derived
46 * from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
57 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
58 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
59 * OF THE POSSIBILITY OF SUCH DAMAGE.
68 #include "ucg_cmd_parse.h"
69 #include "ucg_cmd_parse_num.h"
71 //#define debug_printf(args...) printf(args)
72 #define debug_printf(args...) do {} while(0)
74 enum num_parse_state_t {
84 FIRST_OK, /* not used */
95 /* Keep it sync with enum in .h */
96 static const char * num_help[] = {
97 "<uint8>", "<uint16>", "<uint32>", "<uint64>",
98 "<int8>", "<int16>", "<int32>", "<int64>",
99 #ifdef UCG_CMD_HAVE_FLOAT
105 add_to_res(unsigned int c, uint64_t *res, unsigned int base)
108 if ( (UINT64_MAX - c) / base < *res ) {
112 *res = (uint64_t) (*res * base + c);
117 check_res_size(struct ucg_cmd_tk_num_data *nd, unsigned ressize)
122 if (ressize < sizeof(int8_t))
127 if (ressize < sizeof(int16_t))
132 if (ressize < sizeof(int32_t))
137 if (ressize < sizeof(int64_t))
140 #ifdef UCG_CMD_HAVE_FLOAT
142 if (ressize < sizeof(float))
152 /* parse an int or a float */
154 cmd_parse_num(ucg_cmd_tk_hdr_t *tk, const char *srcbuf,
155 void *res, unsigned ressize)
157 struct ucg_cmd_tk_num_data nd;
158 enum num_parse_state_t st = START;
159 const char * buf = srcbuf;
162 #ifdef UCG_CMD_HAVE_FLOAT
163 uint64_t res2 = 0, res3 = 1;
166 memcpy(&nd, &((struct ucg_cmd_tk_num *)tk)->num_data, sizeof(nd));
168 /* check that we have enough room in res */
170 if (check_res_size(&nd, ressize) < 0)
174 while (st != ERROR && c != '\0') {
175 debug_printf("%c %x -> ", c, c);
184 #ifdef UCG_CMD_HAVE_FLOAT
190 else if (c >= '1' && c <= '9') {
191 if (add_to_res(c - '0', &res1, 10) < 0)
208 #ifdef UCG_CMD_HAVE_FLOAT
214 else if (c >= '0' && c <= '7') {
215 if (add_to_res(c - '0', &res1, 10) < 0)
226 if (c >= '0' && c <= '9') {
227 if (add_to_res(c - '0', &res1, 10) < 0)
232 #ifdef UCG_CMD_HAVE_FLOAT
244 if (c >= '0' && c <= '9') {
245 if (add_to_res(c - '0', &res1, 10) < 0)
248 #ifdef UCG_CMD_HAVE_FLOAT
259 if (c >= '0' && c <= '9') {
260 if (add_to_res(c - '0', &res1, 10) < 0)
263 #ifdef UCG_CMD_HAVE_FLOAT
277 if (c >= '0' && c <= '9') {
278 if (add_to_res(c - '0', &res1, 16) < 0)
281 else if (c >= 'a' && c <= 'f') {
282 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
285 else if (c >= 'A' && c <= 'F') {
286 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
296 if (c >= '0' && c <= '7') {
297 if (add_to_res(c - '0', &res1, 8) < 0)
309 if (c >= '0' && c <= '1') {
310 if (add_to_res(c - '0', &res1, 2) < 0)
318 #ifdef UCG_CMD_HAVE_FLOAT
320 if (c >= '0' && c <= '9') {
321 if (add_to_res(c - '0', &res2, 10) < 0)
333 if (c >= '0' && c <= '9') {
334 if (add_to_res(c - '0', &res2, 10) < 0)
346 if (c >= '0' && c <= '9') {
347 if (add_to_res(c - '0', &res2, 10) < 0)
349 if (add_to_res(0, &res3, 10) < 0)
358 if (c >= '0' && c <= '9') {
359 if (add_to_res(c - '0', &res2, 10) < 0)
361 if (add_to_res(0, &res3, 10) < 0)
371 debug_printf("not impl ");
375 #ifdef UCG_CMD_HAVE_FLOAT
376 debug_printf("(%"PRIu32") (%"PRIu32") (%"PRIu32")\n",
379 debug_printf("(%"PRIu32")\n", res1);
386 if (buf-srcbuf > 127)
396 if ( nd.type == INT8 && res1 <= INT8_MAX ) {
398 *(int8_t *)res = (int8_t) res1;
401 else if ( nd.type == INT16 && res1 <= INT16_MAX ) {
403 *(int16_t *)res = (int16_t) res1;
406 else if ( nd.type == INT32 && res1 <= INT32_MAX ) {
408 *(int32_t *)res = (int32_t) res1;
411 else if ( nd.type == UINT8 && res1 <= UINT8_MAX ) {
413 *(uint8_t *)res = (uint8_t) res1;
416 else if (nd.type == UINT16 && res1 <= UINT16_MAX ) {
418 *(uint16_t *)res = (uint16_t) res1;
421 else if ( nd.type == UINT32 ) {
423 *(uint32_t *)res = (uint32_t) res1;
426 else if ( nd.type == UINT64 ) {
428 *(uint64_t *)res = res1;
431 #ifdef UCG_CMD_HAVE_FLOAT
432 else if ( nd.type == FLOAT ) {
434 *(float *)res = (float)res1;
444 if ( nd.type == INT8 && res1 <= INT8_MAX + 1 ) {
446 *(int8_t *)res = (int8_t) (-res1);
449 else if ( nd.type == INT16 && res1 <= (uint16_t)INT16_MAX + 1 ) {
451 *(int16_t *)res = (int16_t) (-res1);
454 else if ( nd.type == INT32 && res1 <= (uint32_t)INT32_MAX + 1 ) {
456 *(int32_t *)res = (int32_t) (-res1);
459 #ifdef UCG_CMD_HAVE_FLOAT
460 else if ( nd.type == FLOAT ) {
462 *(float *)res = - (float)res1;
471 #ifdef UCG_CMD_HAVE_FLOAT
474 if ( nd.type == FLOAT ) {
476 *(float *)res = (float)res1 +
477 ((float)res2 / (float)res3);
488 if ( nd.type == FLOAT ) {
490 *(float *)res = - ((float)res1 +
491 ((float)res2 / (float)res3));
501 debug_printf("error\n");
507 /* parse an int or a float */
509 cmd_help_num(ucg_cmd_tk_hdr_t *tk, char *dstbuf,
512 struct ucg_cmd_tk_num_data nd;
514 memcpy(&nd, &((struct ucg_cmd_tk_num *)tk)->num_data, sizeof(nd));
516 /* should not happen.... don't so this test */
517 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
520 strncpy(dstbuf, num_help[nd.type], size);
521 dstbuf[size-1] = '\0';
526 struct ucg_cmd_tk_ops ucg_cmd_tk_num_ops = {
527 .parse = cmd_parse_num,
528 .complete_start = NULL,
529 .complete_iterate = NULL,
530 .complete_end = NULL,
531 .help = cmd_help_num,