2 * Copyright (c) <2010>, Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * - Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
37 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions are met:
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * * Neither the name of the University of California, Berkeley nor the
47 * names of its contributors may be used to endorse or promote products
48 * derived from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
51 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
54 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 #include "cmdline_parse.h"
69 #include "cmdline_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 CMDLINE_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 cmdline_token_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 CMDLINE_HAVE_FLOAT
142 if (ressize < sizeof(float))
152 /* parse an int or a float */
154 cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf,
155 void *res, unsigned ressize)
157 struct cmdline_token_num_data nd;
158 enum num_parse_state_t st = START;
159 const char * buf = srcbuf;
162 #ifdef CMDLINE_HAVE_FLOAT
163 uint64_t res2 = 0, res3 = 1;
166 memcpy(&nd, &((struct cmdline_token_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 CMDLINE_HAVE_FLOAT
190 else if (c >= '1' && c <= '9') {
191 if (add_to_res(c - '0', &res1, 10) < 0)
208 #ifdef CMDLINE_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 CMDLINE_HAVE_FLOAT
244 if (c >= '0' && c <= '9') {
245 if (add_to_res(c - '0', &res1, 10) < 0)
248 #ifdef CMDLINE_HAVE_FLOAT
259 if (c >= '0' && c <= '9') {
260 if (add_to_res(c - '0', &res1, 10) < 0)
263 #ifdef CMDLINE_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 CMDLINE_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 CMDLINE_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 CMDLINE_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 CMDLINE_HAVE_FLOAT
460 else if ( nd.type == FLOAT ) {
462 *(float *)res = - (float)res1;
471 #ifdef CMDLINE_HAVE_FLOAT
474 if ( nd.type == FLOAT ) {
476 *(float *)res = (float)res1 + ((float)res2 / (float)res3);
487 if ( nd.type == FLOAT ) {
489 *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
499 debug_printf("error\n");
505 /* parse an int or a float */
507 cmdline_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
509 struct cmdline_token_num_data nd;
511 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
513 /* should not happen.... don't so this test */
514 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
517 strncpy(dstbuf, num_help[nd.type], size);
518 dstbuf[size-1] = '\0';
523 struct cmdline_token_ops cmdline_token_num_ops = {
524 .parse = cmdline_parse_num,
525 .complete_start = NULL,
526 .complete_iterate = NULL,
527 .complete_end = NULL,
528 .help = cmdline_help_num,