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);
125 /* parse an int or a float */
127 cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
129 struct cmdline_token_num_data nd;
130 enum num_parse_state_t st = START;
131 const char * buf = srcbuf;
134 #ifdef CMDLINE_HAVE_FLOAT
135 uint64_t res2 = 0, res3 = 1;
138 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
140 while (st != ERROR && c != '\0') {
141 debug_printf("%c %x -> ", c, c);
150 #ifdef CMDLINE_HAVE_FLOAT
156 else if (c >= '1' && c <= '9') {
157 if (add_to_res(c - '0', &res1, 10) < 0)
174 #ifdef CMDLINE_HAVE_FLOAT
180 else if (c >= '0' && c <= '7') {
181 if (add_to_res(c - '0', &res1, 10) < 0)
192 if (c >= '0' && c <= '9') {
193 if (add_to_res(c - '0', &res1, 10) < 0)
198 #ifdef CMDLINE_HAVE_FLOAT
210 if (c >= '0' && c <= '9') {
211 if (add_to_res(c - '0', &res1, 10) < 0)
214 #ifdef CMDLINE_HAVE_FLOAT
225 if (c >= '0' && c <= '9') {
226 if (add_to_res(c - '0', &res1, 10) < 0)
229 #ifdef CMDLINE_HAVE_FLOAT
243 if (c >= '0' && c <= '9') {
244 if (add_to_res(c - '0', &res1, 16) < 0)
247 else if (c >= 'a' && c <= 'f') {
248 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
251 else if (c >= 'A' && c <= 'F') {
252 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
262 if (c >= '0' && c <= '7') {
263 if (add_to_res(c - '0', &res1, 8) < 0)
275 if (c >= '0' && c <= '1') {
276 if (add_to_res(c - '0', &res1, 2) < 0)
284 #ifdef CMDLINE_HAVE_FLOAT
286 if (c >= '0' && c <= '9') {
287 if (add_to_res(c - '0', &res2, 10) < 0)
299 if (c >= '0' && c <= '9') {
300 if (add_to_res(c - '0', &res2, 10) < 0)
312 if (c >= '0' && c <= '9') {
313 if (add_to_res(c - '0', &res2, 10) < 0)
315 if (add_to_res(0, &res3, 10) < 0)
324 if (c >= '0' && c <= '9') {
325 if (add_to_res(c - '0', &res2, 10) < 0)
327 if (add_to_res(0, &res3, 10) < 0)
337 debug_printf("not impl ");
341 #ifdef CMDLINE_HAVE_FLOAT
342 debug_printf("(%"PRIu32") (%"PRIu32") (%"PRIu32")\n",
345 debug_printf("(%"PRIu32")\n", res1);
352 if (buf-srcbuf > 127)
362 if ( nd.type == INT8 && res1 <= INT8_MAX ) {
364 *(int8_t *)res = (int8_t) res1;
367 else if ( nd.type == INT16 && res1 <= INT16_MAX ) {
369 *(int16_t *)res = (int16_t) res1;
372 else if ( nd.type == INT32 && res1 <= INT32_MAX ) {
374 *(int32_t *)res = (int32_t) res1;
377 else if ( nd.type == UINT8 && res1 <= UINT8_MAX ) {
379 *(uint8_t *)res = (uint8_t) res1;
382 else if (nd.type == UINT16 && res1 <= UINT16_MAX ) {
384 *(uint16_t *)res = (uint16_t) res1;
387 else if ( nd.type == UINT32 ) {
389 *(uint32_t *)res = (uint32_t) res1;
392 else if ( nd.type == UINT64 ) {
394 *(uint64_t *)res = res1;
397 #ifdef CMDLINE_HAVE_FLOAT
398 else if ( nd.type == FLOAT ) {
400 *(float *)res = (float)res1;
410 if ( nd.type == INT8 && res1 <= INT8_MAX + 1 ) {
412 *(int8_t *)res = (int8_t) (-res1);
415 else if ( nd.type == INT16 && res1 <= (uint16_t)INT16_MAX + 1 ) {
417 *(int16_t *)res = (int16_t) (-res1);
420 else if ( nd.type == INT32 && res1 <= (uint32_t)INT32_MAX + 1 ) {
422 *(int32_t *)res = (int32_t) (-res1);
425 #ifdef CMDLINE_HAVE_FLOAT
426 else if ( nd.type == FLOAT ) {
428 *(float *)res = - (float)res1;
437 #ifdef CMDLINE_HAVE_FLOAT
440 if ( nd.type == FLOAT ) {
442 *(float *)res = (float)res1 + ((float)res2 / (float)res3);
453 if ( nd.type == FLOAT ) {
455 *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
465 debug_printf("error\n");
471 /* parse an int or a float */
473 cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
475 struct cmdline_token_num_data nd;
477 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
479 /* should not happen.... don't so this test */
480 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
483 strncpy(dstbuf, num_help[nd.type], size);
484 dstbuf[size-1] = '\0';