4 * Copyright(c) 2010-2012 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * 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 FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED 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.
69 #include <rte_string_fns.h>
71 #include "cmdline_parse.h"
72 #include "cmdline_parse_num.h"
74 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
75 #define debug_printf(args...) printf(args)
77 #define debug_printf(args...) do {} while(0)
80 struct cmdline_token_ops cmdline_token_num_ops = {
81 .parse = cmdline_parse_num,
82 .complete_get_nb = NULL,
83 .complete_get_elt = NULL,
84 .get_help = cmdline_get_help_num,
88 enum num_parse_state_t {
98 FIRST_OK, /* not used */
109 /* Keep it sync with enum in .h */
110 static const char * num_help[] = {
111 "UINT8", "UINT16", "UINT32", "UINT64",
112 "INT8", "INT16", "INT32", "INT64",
113 #ifdef CMDLINE_HAVE_FLOAT
119 add_to_res(unsigned int c, uint64_t *res, unsigned int base)
122 if ( (UINT64_MAX - c) / base < *res ) {
126 *res = (uint64_t) (*res * base + c);
131 /* parse an int or a float */
133 cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
135 struct cmdline_token_num_data nd;
136 enum num_parse_state_t st = START;
137 const char * buf = srcbuf;
140 #ifdef CMDLINE_HAVE_FLOAT
141 uint64_t res2 = 0, res3 = 1;
144 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
146 while ( st != ERROR && c && ! cmdline_isendoftoken(c) ) {
147 debug_printf("%c %x -> ", c, c);
156 #ifdef CMDLINE_HAVE_FLOAT
162 else if (c >= '1' && c <= '9') {
163 if (add_to_res(c - '0', &res1, 10) < 0)
180 #ifdef CMDLINE_HAVE_FLOAT
186 else if (c >= '0' && c <= '7') {
187 if (add_to_res(c - '0', &res1, 10) < 0)
198 if (c >= '0' && c <= '9') {
199 if (add_to_res(c - '0', &res1, 10) < 0)
204 #ifdef CMDLINE_HAVE_FLOAT
216 if (c >= '0' && c <= '9') {
217 if (add_to_res(c - '0', &res1, 10) < 0)
220 #ifdef CMDLINE_HAVE_FLOAT
231 if (c >= '0' && c <= '9') {
232 if (add_to_res(c - '0', &res1, 10) < 0)
235 #ifdef CMDLINE_HAVE_FLOAT
249 if (c >= '0' && c <= '9') {
250 if (add_to_res(c - '0', &res1, 16) < 0)
253 else if (c >= 'a' && c <= 'f') {
254 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
257 else if (c >= 'A' && c <= 'F') {
258 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
268 if (c >= '0' && c <= '7') {
269 if (add_to_res(c - '0', &res1, 8) < 0)
281 if (c >= '0' && c <= '1') {
282 if (add_to_res(c - '0', &res1, 2) < 0)
290 #ifdef CMDLINE_HAVE_FLOAT
292 if (c >= '0' && c <= '9') {
293 if (add_to_res(c - '0', &res2, 10) < 0)
305 if (c >= '0' && c <= '9') {
306 if (add_to_res(c - '0', &res2, 10) < 0)
318 if (c >= '0' && c <= '9') {
319 if (add_to_res(c - '0', &res2, 10) < 0)
321 if (add_to_res(0, &res3, 10) < 0)
330 if (c >= '0' && c <= '9') {
331 if (add_to_res(c - '0', &res2, 10) < 0)
333 if (add_to_res(0, &res3, 10) < 0)
343 debug_printf("not impl ");
347 #ifdef CMDLINE_HAVE_FLOAT
348 debug_printf("(%"PRIu32") (%"PRIu32") (%"PRIu32")\n",
351 debug_printf("(%"PRIu32")\n", res1);
358 if (buf-srcbuf > 127)
368 if ( nd.type == INT8 && res1 <= INT8_MAX ) {
370 *(int8_t *)res = (int8_t) res1;
373 else if ( nd.type == INT16 && res1 <= INT16_MAX ) {
375 *(int16_t *)res = (int16_t) res1;
378 else if ( nd.type == INT32 && res1 <= INT32_MAX ) {
380 *(int32_t *)res = (int32_t) res1;
383 else if ( nd.type == UINT8 && res1 <= UINT8_MAX ) {
385 *(uint8_t *)res = (uint8_t) res1;
388 else if (nd.type == UINT16 && res1 <= UINT16_MAX ) {
390 *(uint16_t *)res = (uint16_t) res1;
393 else if ( nd.type == UINT32 ) {
395 *(uint32_t *)res = (uint32_t) res1;
398 else if ( nd.type == UINT64 ) {
400 *(uint64_t *)res = res1;
403 #ifdef CMDLINE_HAVE_FLOAT
404 else if ( nd.type == FLOAT ) {
406 *(float *)res = (float)res1;
416 if ( nd.type == INT8 && res1 <= INT8_MAX + 1 ) {
418 *(int8_t *)res = (int8_t) (-res1);
421 else if ( nd.type == INT16 && res1 <= (uint16_t)INT16_MAX + 1 ) {
423 *(int16_t *)res = (int16_t) (-res1);
426 else if ( nd.type == INT32 && res1 <= (uint32_t)INT32_MAX + 1 ) {
428 *(int32_t *)res = (int32_t) (-res1);
431 #ifdef CMDLINE_HAVE_FLOAT
432 else if ( nd.type == FLOAT ) {
434 *(float *)res = - (float)res1;
443 #ifdef CMDLINE_HAVE_FLOAT
446 if ( nd.type == FLOAT ) {
448 *(float *)res = (float)res1 + ((float)res2 / (float)res3);
459 if ( nd.type == FLOAT ) {
461 *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
471 debug_printf("error\n");
477 /* parse an int or a float */
479 cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
481 struct cmdline_token_num_data nd;
483 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
485 /* should not happen.... don't so this test */
486 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
489 rte_snprintf(dstbuf, size, "%s", num_help[nd.type]);
490 dstbuf[size-1] = '\0';