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 struct cmdline_token_ops cmdline_token_num_ops = {
75 .parse = cmdline_parse_num,
76 .complete_get_nb = NULL,
77 .complete_get_elt = NULL,
78 .get_help = cmdline_get_help_num,
82 enum num_parse_state_t {
92 FIRST_OK, /* not used */
103 /* Keep it sync with enum in .h */
104 static const char * num_help[] = {
105 "UINT8", "UINT16", "UINT32", "UINT64",
106 "INT8", "INT16", "INT32", "INT64",
107 #ifdef CMDLINE_HAVE_FLOAT
113 add_to_res(unsigned int c, uint64_t *res, unsigned int base)
116 if ( (UINT64_MAX - c) / base < *res ) {
120 *res = (uint64_t) (*res * base + c);
124 static int check_res_size(struct cmdline_token_num_data *nd, unsigned ressize)
129 if (ressize < sizeof(int8_t))
134 if (ressize < sizeof(int16_t))
139 if (ressize < sizeof(int32_t))
144 if (ressize < sizeof(int64_t))
147 #ifdef CMDLINE_HAVE_FLOAT
149 if (ressize < sizeof(float))
159 /* parse an int or a float */
161 cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf,
162 void *res, unsigned ressize)
164 struct cmdline_token_num_data nd;
165 enum num_parse_state_t st = START;
166 const char * buf = srcbuf;
169 #ifdef CMDLINE_HAVE_FLOAT
170 uint64_t res2 = 0, res3 = 1;
173 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
175 /* check that we have enough room in res */
177 if (check_res_size(&nd, ressize) < 0)
181 while (st != ERROR && c != '\0') {
182 debug_printf("%c %x -> ", c, c);
191 #ifdef CMDLINE_HAVE_FLOAT
197 else if (c >= '1' && c <= '9') {
198 if (add_to_res(c - '0', &res1, 10) < 0)
215 #ifdef CMDLINE_HAVE_FLOAT
221 else if (c >= '0' && c <= '7') {
222 if (add_to_res(c - '0', &res1, 10) < 0)
233 if (c >= '0' && c <= '9') {
234 if (add_to_res(c - '0', &res1, 10) < 0)
239 #ifdef CMDLINE_HAVE_FLOAT
251 if (c >= '0' && c <= '9') {
252 if (add_to_res(c - '0', &res1, 10) < 0)
255 #ifdef CMDLINE_HAVE_FLOAT
266 if (c >= '0' && c <= '9') {
267 if (add_to_res(c - '0', &res1, 10) < 0)
270 #ifdef CMDLINE_HAVE_FLOAT
284 if (c >= '0' && c <= '9') {
285 if (add_to_res(c - '0', &res1, 16) < 0)
288 else if (c >= 'a' && c <= 'f') {
289 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
292 else if (c >= 'A' && c <= 'F') {
293 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
303 if (c >= '0' && c <= '7') {
304 if (add_to_res(c - '0', &res1, 8) < 0)
316 if (c >= '0' && c <= '1') {
317 if (add_to_res(c - '0', &res1, 2) < 0)
325 #ifdef CMDLINE_HAVE_FLOAT
327 if (c >= '0' && c <= '9') {
328 if (add_to_res(c - '0', &res2, 10) < 0)
340 if (c >= '0' && c <= '9') {
341 if (add_to_res(c - '0', &res2, 10) < 0)
353 if (c >= '0' && c <= '9') {
354 if (add_to_res(c - '0', &res2, 10) < 0)
356 if (add_to_res(0, &res3, 10) < 0)
365 if (c >= '0' && c <= '9') {
366 if (add_to_res(c - '0', &res2, 10) < 0)
368 if (add_to_res(0, &res3, 10) < 0)
378 debug_printf("not impl ");
382 #ifdef CMDLINE_HAVE_FLOAT
383 debug_printf("(%"PRIu32") (%"PRIu32") (%"PRIu32")\n",
386 debug_printf("(%"PRIu32")\n", res1);
393 if (buf-srcbuf > 127)
403 if ( nd.type == INT8 && res1 <= INT8_MAX ) {
405 *(int8_t *)res = (int8_t) res1;
408 else if ( nd.type == INT16 && res1 <= INT16_MAX ) {
410 *(int16_t *)res = (int16_t) res1;
413 else if ( nd.type == INT32 && res1 <= INT32_MAX ) {
415 *(int32_t *)res = (int32_t) res1;
418 else if ( nd.type == UINT8 && res1 <= UINT8_MAX ) {
420 *(uint8_t *)res = (uint8_t) res1;
423 else if (nd.type == UINT16 && res1 <= UINT16_MAX ) {
425 *(uint16_t *)res = (uint16_t) res1;
428 else if ( nd.type == UINT32 ) {
430 *(uint32_t *)res = (uint32_t) res1;
433 else if ( nd.type == UINT64 ) {
435 *(uint64_t *)res = res1;
438 #ifdef CMDLINE_HAVE_FLOAT
439 else if ( nd.type == FLOAT ) {
441 *(float *)res = (float)res1;
451 if ( nd.type == INT8 && res1 <= INT8_MAX + 1 ) {
453 *(int8_t *)res = (int8_t) (-res1);
456 else if ( nd.type == INT16 && res1 <= (uint16_t)INT16_MAX + 1 ) {
458 *(int16_t *)res = (int16_t) (-res1);
461 else if ( nd.type == INT32 && res1 <= (uint32_t)INT32_MAX + 1 ) {
463 *(int32_t *)res = (int32_t) (-res1);
466 #ifdef CMDLINE_HAVE_FLOAT
467 else if ( nd.type == FLOAT ) {
469 *(float *)res = - (float)res1;
478 #ifdef CMDLINE_HAVE_FLOAT
481 if ( nd.type == FLOAT ) {
483 *(float *)res = (float)res1 + ((float)res2 / (float)res3);
494 if ( nd.type == FLOAT ) {
496 *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
506 debug_printf("error\n");
512 /* parse an int or a float */
514 cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
516 struct cmdline_token_num_data nd;
518 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
520 /* should not happen.... don't so this test */
521 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
524 strncpy(dstbuf, num_help[nd.type], size);
525 dstbuf[size-1] = '\0';