4 * Copyright(c) 2010-2014 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.
35 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
36 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions are met:
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * * Neither the name of the University of California, Berkeley nor the
46 * names of its contributors may be used to endorse or promote products
47 * derived from this software without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
50 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
53 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
54 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
56 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
58 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 #include <rte_string_fns.h>
70 #include "cmdline_parse.h"
71 #include "cmdline_parse_num.h"
73 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
74 #define debug_printf(args...) printf(args)
76 #define debug_printf(args...) do {} while(0)
79 struct cmdline_token_ops cmdline_token_num_ops
= {
80 .parse
= cmdline_parse_num
,
81 .complete_get_nb
= NULL
,
82 .complete_get_elt
= NULL
,
83 .get_help
= cmdline_get_help_num
,
87 enum num_parse_state_t
{
95 FIRST_OK
, /* not used */
104 /* Keep it sync with enum in .h */
105 static const char * num_help
[] = {
106 "UINT8", "UINT16", "UINT32", "UINT64",
107 "INT8", "INT16", "INT32", "INT64",
111 add_to_res(unsigned int c
, uint64_t *res
, unsigned int base
)
114 if ( (UINT64_MAX
- c
) / base
< *res
) {
118 *res
= (uint64_t) (*res
* base
+ c
);
123 check_res_size(struct cmdline_token_num_data
*nd
, unsigned ressize
)
128 if (ressize
< sizeof(int8_t))
133 if (ressize
< sizeof(int16_t))
138 if (ressize
< sizeof(int32_t))
143 if (ressize
< sizeof(int64_t))
154 cmdline_parse_num(cmdline_parse_token_hdr_t
*tk
, const char *srcbuf
, void *res
,
157 struct cmdline_token_num_data nd
;
158 enum num_parse_state_t st
= START
;
166 if (!srcbuf
|| !*srcbuf
)
172 memcpy(&nd
, &((struct cmdline_token_num
*)tk
)->num_data
, sizeof(nd
));
174 /* check that we have enough room in res */
176 if (check_res_size(&nd
, ressize
) < 0)
180 while ( st
!= ERROR
&& c
&& ! cmdline_isendoftoken(c
) ) {
181 debug_printf("%c %x -> ", c
, c
);
190 else if (c
>= '1' && c
<= '9') {
191 if (add_to_res(c
- '0', &res1
, 10) < 0)
208 else if (c
>= '0' && c
<= '7') {
209 if (add_to_res(c
- '0', &res1
, 10) < 0)
220 if (c
>= '0' && c
<= '9') {
221 if (add_to_res(c
- '0', &res1
, 10) < 0)
232 if (c
>= '0' && c
<= '9') {
233 if (add_to_res(c
- '0', &res1
, 10) < 0)
242 if (c
>= '0' && c
<= '9') {
243 if (add_to_res(c
- '0', &res1
, 10) < 0)
255 if (c
>= '0' && c
<= '9') {
256 if (add_to_res(c
- '0', &res1
, 16) < 0)
259 else if (c
>= 'a' && c
<= 'f') {
260 if (add_to_res(c
- 'a' + 10, &res1
, 16) < 0)
263 else if (c
>= 'A' && c
<= 'F') {
264 if (add_to_res(c
- 'A' + 10, &res1
, 16) < 0)
274 if (c
>= '0' && c
<= '7') {
275 if (add_to_res(c
- '0', &res1
, 8) < 0)
287 if (c
>= '0' && c
<= '1') {
288 if (add_to_res(c
- '0', &res1
, 2) < 0)
296 debug_printf("not impl ");
300 debug_printf("(%"PRIu64
")\n", res1
);
306 if (buf
-srcbuf
> 127)
316 if ( nd
.type
== INT8
&& res1
<= INT8_MAX
) {
317 if (res
) *(int8_t *)res
= (int8_t) res1
;
320 else if ( nd
.type
== INT16
&& res1
<= INT16_MAX
) {
321 if (res
) *(int16_t *)res
= (int16_t) res1
;
324 else if ( nd
.type
== INT32
&& res1
<= INT32_MAX
) {
325 if (res
) *(int32_t *)res
= (int32_t) res1
;
328 else if ( nd
.type
== INT64
&& res1
<= INT64_MAX
) {
329 if (res
) *(int64_t *)res
= (int64_t) res1
;
332 else if ( nd
.type
== UINT8
&& res1
<= UINT8_MAX
) {
333 if (res
) *(uint8_t *)res
= (uint8_t) res1
;
336 else if (nd
.type
== UINT16
&& res1
<= UINT16_MAX
) {
337 if (res
) *(uint16_t *)res
= (uint16_t) res1
;
340 else if ( nd
.type
== UINT32
&& res1
<= UINT32_MAX
) {
341 if (res
) *(uint32_t *)res
= (uint32_t) res1
;
344 else if ( nd
.type
== UINT64
) {
345 if (res
) *(uint64_t *)res
= res1
;
354 if ( nd
.type
== INT8
&& res1
<= INT8_MAX
+ 1 ) {
355 if (res
) *(int8_t *)res
= (int8_t) (-res1
);
358 else if ( nd
.type
== INT16
&& res1
<= (uint16_t)INT16_MAX
+ 1 ) {
359 if (res
) *(int16_t *)res
= (int16_t) (-res1
);
362 else if ( nd
.type
== INT32
&& res1
<= (uint32_t)INT32_MAX
+ 1 ) {
363 if (res
) *(int32_t *)res
= (int32_t) (-res1
);
366 else if ( nd
.type
== INT64
&& res1
<= (uint64_t)INT64_MAX
+ 1 ) {
367 if (res
) *(int64_t *)res
= (int64_t) (-res1
);
375 debug_printf("error\n");
383 cmdline_get_help_num(cmdline_parse_token_hdr_t
*tk
, char *dstbuf
, unsigned int size
)
385 struct cmdline_token_num_data nd
;
391 memcpy(&nd
, &((struct cmdline_token_num
*)tk
)->num_data
, sizeof(nd
));
393 /* should not happen.... don't so this test */
394 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
397 ret
= snprintf(dstbuf
, size
, "%s", num_help
[nd
.type
]);
400 dstbuf
[size
-1] = '\0';