1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
14 #include <rte_string_fns.h>
16 #include "cmdline_parse.h"
17 #include "cmdline_parse_num.h"
19 #ifdef RTE_LIBRTE_CMDLINE_DEBUG
20 #define debug_printf(args...) printf(args)
22 #define debug_printf(args...) do {} while(0)
25 struct cmdline_token_ops cmdline_token_num_ops
= {
26 .parse
= cmdline_parse_num
,
27 .complete_get_nb
= NULL
,
28 .complete_get_elt
= NULL
,
29 .get_help
= cmdline_get_help_num
,
33 enum num_parse_state_t
{
41 FIRST_OK
, /* not used */
50 /* Keep it sync with enum in .h */
51 static const char * num_help
[] = {
52 "UINT8", "UINT16", "UINT32", "UINT64",
53 "INT8", "INT16", "INT32", "INT64",
57 add_to_res(unsigned int c
, uint64_t *res
, unsigned int base
)
60 if ( (UINT64_MAX
- c
) / base
< *res
) {
64 *res
= (uint64_t) (*res
* base
+ c
);
69 check_res_size(struct cmdline_token_num_data
*nd
, unsigned ressize
)
74 if (ressize
< sizeof(int8_t))
79 if (ressize
< sizeof(int16_t))
84 if (ressize
< sizeof(int32_t))
89 if (ressize
< sizeof(int64_t))
100 cmdline_parse_num(cmdline_parse_token_hdr_t
*tk
, const char *srcbuf
, void *res
,
103 struct cmdline_token_num_data nd
;
104 enum num_parse_state_t st
= START
;
112 if (!srcbuf
|| !*srcbuf
)
118 memcpy(&nd
, &((struct cmdline_token_num
*)tk
)->num_data
, sizeof(nd
));
120 /* check that we have enough room in res */
122 if (check_res_size(&nd
, ressize
) < 0)
126 while ( st
!= ERROR
&& c
&& ! cmdline_isendoftoken(c
) ) {
127 debug_printf("%c %x -> ", c
, c
);
136 else if (c
>= '1' && c
<= '9') {
137 if (add_to_res(c
- '0', &res1
, 10) < 0)
154 else if (c
>= '0' && c
<= '7') {
155 if (add_to_res(c
- '0', &res1
, 10) < 0)
166 if (c
>= '0' && c
<= '9') {
167 if (add_to_res(c
- '0', &res1
, 10) < 0)
178 if (c
>= '0' && c
<= '9') {
179 if (add_to_res(c
- '0', &res1
, 10) < 0)
188 if (c
>= '0' && c
<= '9') {
189 if (add_to_res(c
- '0', &res1
, 10) < 0)
199 /* fall-through no break */
201 if (c
>= '0' && c
<= '9') {
202 if (add_to_res(c
- '0', &res1
, 16) < 0)
205 else if (c
>= 'a' && c
<= 'f') {
206 if (add_to_res(c
- 'a' + 10, &res1
, 16) < 0)
209 else if (c
>= 'A' && c
<= 'F') {
210 if (add_to_res(c
- 'A' + 10, &res1
, 16) < 0)
220 if (c
>= '0' && c
<= '7') {
221 if (add_to_res(c
- '0', &res1
, 8) < 0)
233 if (c
>= '0' && c
<= '1') {
234 if (add_to_res(c
- '0', &res1
, 2) < 0)
242 debug_printf("not impl ");
246 debug_printf("(%"PRIu64
")\n", res1
);
252 if (buf
-srcbuf
> 127)
262 if ( nd
.type
== INT8
&& res1
<= INT8_MAX
) {
263 if (res
) *(int8_t *)res
= (int8_t) res1
;
266 else if ( nd
.type
== INT16
&& res1
<= INT16_MAX
) {
267 if (res
) *(int16_t *)res
= (int16_t) res1
;
270 else if ( nd
.type
== INT32
&& res1
<= INT32_MAX
) {
271 if (res
) *(int32_t *)res
= (int32_t) res1
;
274 else if ( nd
.type
== INT64
&& res1
<= INT64_MAX
) {
275 if (res
) *(int64_t *)res
= (int64_t) res1
;
278 else if ( nd
.type
== UINT8
&& res1
<= UINT8_MAX
) {
279 if (res
) *(uint8_t *)res
= (uint8_t) res1
;
282 else if (nd
.type
== UINT16
&& res1
<= UINT16_MAX
) {
283 if (res
) *(uint16_t *)res
= (uint16_t) res1
;
286 else if ( nd
.type
== UINT32
&& res1
<= UINT32_MAX
) {
287 if (res
) *(uint32_t *)res
= (uint32_t) res1
;
290 else if ( nd
.type
== UINT64
) {
291 if (res
) *(uint64_t *)res
= res1
;
300 if ( nd
.type
== INT8
&& res1
<= INT8_MAX
+ 1 ) {
301 if (res
) *(int8_t *)res
= (int8_t) (-res1
);
304 else if ( nd
.type
== INT16
&& res1
<= (uint16_t)INT16_MAX
+ 1 ) {
305 if (res
) *(int16_t *)res
= (int16_t) (-res1
);
308 else if ( nd
.type
== INT32
&& res1
<= (uint32_t)INT32_MAX
+ 1 ) {
309 if (res
) *(int32_t *)res
= (int32_t) (-res1
);
312 else if ( nd
.type
== INT64
&& res1
<= (uint64_t)INT64_MAX
+ 1 ) {
313 if (res
) *(int64_t *)res
= (int64_t) (-res1
);
321 debug_printf("error\n");
329 cmdline_get_help_num(cmdline_parse_token_hdr_t
*tk
, char *dstbuf
, unsigned int size
)
331 struct cmdline_token_num_data nd
;
337 memcpy(&nd
, &((struct cmdline_token_num
*)tk
)->num_data
, sizeof(nd
));
339 /* should not happen.... don't so this test */
340 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
343 ret
= snprintf(dstbuf
, size
, "%s", num_help
[nd
.type
]);
346 dstbuf
[size
-1] = '\0';