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.
33 * version: DPDK.L.1.2.3-3
37 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
38 * All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions are met:
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * * Neither the name of the University of California, Berkeley nor the
48 * names of its contributors may be used to endorse or promote products
49 * derived from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
52 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
55 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
56 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
58 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
60 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70 #include <rte_string_fns.h>
72 #include "cmdline_parse.h"
73 #include "cmdline_parse_num.h"
75 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
76 #define debug_printf(args...) printf(args)
78 #define debug_printf(args...) do {} while(0)
81 struct cmdline_token_ops cmdline_token_num_ops = {
82 .parse = cmdline_parse_num,
83 .complete_get_nb = NULL,
84 .complete_get_elt = NULL,
85 .get_help = cmdline_get_help_num,
89 enum num_parse_state_t {
99 FIRST_OK, /* not used */
110 /* Keep it sync with enum in .h */
111 static const char * num_help[] = {
112 "UINT8", "UINT16", "UINT32", "UINT64",
113 "INT8", "INT16", "INT32", "INT64",
114 #ifdef CMDLINE_HAVE_FLOAT
120 add_to_res(unsigned int c, uint64_t *res, unsigned int base)
123 if ( (UINT64_MAX - c) / base < *res ) {
127 *res = (uint64_t) (*res * base + c);
132 /* parse an int or a float */
134 cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res)
136 struct cmdline_token_num_data nd;
137 enum num_parse_state_t st = START;
138 const char * buf = srcbuf;
141 #ifdef CMDLINE_HAVE_FLOAT
142 uint64_t res2 = 0, res3 = 1;
145 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
147 while ( st != ERROR && c && ! cmdline_isendoftoken(c) ) {
148 debug_printf("%c %x -> ", c, c);
157 #ifdef CMDLINE_HAVE_FLOAT
163 else if (c >= '1' && c <= '9') {
164 if (add_to_res(c - '0', &res1, 10) < 0)
181 #ifdef CMDLINE_HAVE_FLOAT
187 else if (c >= '0' && c <= '7') {
188 if (add_to_res(c - '0', &res1, 10) < 0)
199 if (c >= '0' && c <= '9') {
200 if (add_to_res(c - '0', &res1, 10) < 0)
205 #ifdef CMDLINE_HAVE_FLOAT
217 if (c >= '0' && c <= '9') {
218 if (add_to_res(c - '0', &res1, 10) < 0)
221 #ifdef CMDLINE_HAVE_FLOAT
232 if (c >= '0' && c <= '9') {
233 if (add_to_res(c - '0', &res1, 10) < 0)
236 #ifdef CMDLINE_HAVE_FLOAT
250 if (c >= '0' && c <= '9') {
251 if (add_to_res(c - '0', &res1, 16) < 0)
254 else if (c >= 'a' && c <= 'f') {
255 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
258 else if (c >= 'A' && c <= 'F') {
259 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
269 if (c >= '0' && c <= '7') {
270 if (add_to_res(c - '0', &res1, 8) < 0)
282 if (c >= '0' && c <= '1') {
283 if (add_to_res(c - '0', &res1, 2) < 0)
291 #ifdef CMDLINE_HAVE_FLOAT
293 if (c >= '0' && c <= '9') {
294 if (add_to_res(c - '0', &res2, 10) < 0)
306 if (c >= '0' && c <= '9') {
307 if (add_to_res(c - '0', &res2, 10) < 0)
319 if (c >= '0' && c <= '9') {
320 if (add_to_res(c - '0', &res2, 10) < 0)
322 if (add_to_res(0, &res3, 10) < 0)
331 if (c >= '0' && c <= '9') {
332 if (add_to_res(c - '0', &res2, 10) < 0)
334 if (add_to_res(0, &res3, 10) < 0)
344 debug_printf("not impl ");
348 #ifdef CMDLINE_HAVE_FLOAT
349 debug_printf("(%"PRIu32") (%"PRIu32") (%"PRIu32")\n",
352 debug_printf("(%"PRIu32")\n", res1);
359 if (buf-srcbuf > 127)
369 if ( nd.type == INT8 && res1 <= INT8_MAX ) {
371 *(int8_t *)res = (int8_t) res1;
374 else if ( nd.type == INT16 && res1 <= INT16_MAX ) {
376 *(int16_t *)res = (int16_t) res1;
379 else if ( nd.type == INT32 && res1 <= INT32_MAX ) {
381 *(int32_t *)res = (int32_t) res1;
384 else if ( nd.type == UINT8 && res1 <= UINT8_MAX ) {
386 *(uint8_t *)res = (uint8_t) res1;
389 else if (nd.type == UINT16 && res1 <= UINT16_MAX ) {
391 *(uint16_t *)res = (uint16_t) res1;
394 else if ( nd.type == UINT32 ) {
396 *(uint32_t *)res = (uint32_t) res1;
399 else if ( nd.type == UINT64 ) {
401 *(uint64_t *)res = res1;
404 #ifdef CMDLINE_HAVE_FLOAT
405 else if ( nd.type == FLOAT ) {
407 *(float *)res = (float)res1;
417 if ( nd.type == INT8 && res1 <= INT8_MAX + 1 ) {
419 *(int8_t *)res = (int8_t) (-res1);
422 else if ( nd.type == INT16 && res1 <= (uint16_t)INT16_MAX + 1 ) {
424 *(int16_t *)res = (int16_t) (-res1);
427 else if ( nd.type == INT32 && res1 <= (uint32_t)INT32_MAX + 1 ) {
429 *(int32_t *)res = (int32_t) (-res1);
432 #ifdef CMDLINE_HAVE_FLOAT
433 else if ( nd.type == FLOAT ) {
435 *(float *)res = - (float)res1;
444 #ifdef CMDLINE_HAVE_FLOAT
447 if ( nd.type == FLOAT ) {
449 *(float *)res = (float)res1 + ((float)res2 / (float)res3);
460 if ( nd.type == FLOAT ) {
462 *(float *)res = - ((float)res1 + ((float)res2 / (float)res3));
472 debug_printf("error\n");
478 /* parse an int or a float */
480 cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
482 struct cmdline_token_num_data nd;
484 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
486 /* should not happen.... don't so this test */
487 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
490 rte_snprintf(dstbuf, size, "%s", num_help[nd.type]);
491 dstbuf[size-1] = '\0';