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)
63 *res = (uint64_t) (*res * base + c);
68 check_res_size(struct cmdline_token_num_data *nd, unsigned ressize)
73 if (ressize < sizeof(int8_t))
78 if (ressize < sizeof(int16_t))
83 if (ressize < sizeof(int32_t))
88 if (ressize < sizeof(int64_t))
99 cmdline_parse_num(cmdline_parse_token_hdr_t *tk, const char *srcbuf, void *res,
102 struct cmdline_token_num_data nd;
103 enum num_parse_state_t st = START;
111 if (!srcbuf || !*srcbuf)
117 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
119 /* check that we have enough room in res */
121 if (check_res_size(&nd, ressize) < 0)
125 while (st != ERROR && c && !cmdline_isendoftoken(c)) {
126 debug_printf("%c %x -> ", c, c);
135 else if (c >= '1' && c <= '9') {
136 if (add_to_res(c - '0', &res1, 10) < 0)
153 else if (c >= '0' && c <= '7') {
154 if (add_to_res(c - '0', &res1, 10) < 0)
165 if (c >= '0' && c <= '9') {
166 if (add_to_res(c - '0', &res1, 10) < 0)
177 if (c >= '0' && c <= '9') {
178 if (add_to_res(c - '0', &res1, 10) < 0)
187 if (c >= '0' && c <= '9') {
188 if (add_to_res(c - '0', &res1, 10) < 0)
200 if (c >= '0' && c <= '9') {
201 if (add_to_res(c - '0', &res1, 16) < 0)
204 else if (c >= 'a' && c <= 'f') {
205 if (add_to_res(c - 'a' + 10, &res1, 16) < 0)
208 else if (c >= 'A' && c <= 'F') {
209 if (add_to_res(c - 'A' + 10, &res1, 16) < 0)
219 if (c >= '0' && c <= '7') {
220 if (add_to_res(c - '0', &res1, 8) < 0)
232 if (c >= '0' && c <= '1') {
233 if (add_to_res(c - '0', &res1, 2) < 0)
241 debug_printf("not impl ");
245 debug_printf("(%"PRIu64")\n", res1);
251 if (buf-srcbuf > 127)
261 if (nd.type == RTE_INT8 && res1 <= INT8_MAX) {
262 if (res) *(int8_t *)res = (int8_t) res1;
264 } else if (nd.type == RTE_INT16 && res1 <= INT16_MAX) {
265 if (res) *(int16_t *)res = (int16_t) res1;
267 } else if (nd.type == RTE_INT32 && res1 <= INT32_MAX) {
268 if (res) *(int32_t *)res = (int32_t) res1;
270 } else if (nd.type == RTE_INT64 && res1 <= INT64_MAX) {
271 if (res) *(int64_t *)res = (int64_t) res1;
273 } else if (nd.type == RTE_UINT8 && res1 <= UINT8_MAX) {
274 if (res) *(uint8_t *)res = (uint8_t) res1;
276 } else if (nd.type == RTE_UINT16 && res1 <= UINT16_MAX) {
277 if (res) *(uint16_t *)res = (uint16_t) res1;
279 } else if (nd.type == RTE_UINT32 && res1 <= UINT32_MAX) {
280 if (res) *(uint32_t *)res = (uint32_t) res1;
282 } else if (nd.type == RTE_UINT64) {
283 if (res) *(uint64_t *)res = res1;
291 if (nd.type == RTE_INT8 &&
292 res1 <= INT8_MAX + 1) {
293 if (res) *(int8_t *)res = (int8_t) (-res1);
295 } else if (nd.type == RTE_INT16 &&
296 res1 <= (uint16_t)INT16_MAX + 1) {
297 if (res) *(int16_t *)res = (int16_t) (-res1);
299 } else if (nd.type == RTE_INT32 &&
300 res1 <= (uint32_t)INT32_MAX + 1) {
301 if (res) *(int32_t *)res = (int32_t) (-res1);
303 } else if (nd.type == RTE_INT64 &&
304 res1 <= (uint64_t)INT64_MAX + 1) {
305 if (res) *(int64_t *)res = (int64_t) (-res1);
312 debug_printf("error\n");
320 cmdline_get_help_num(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
322 struct cmdline_token_num_data nd;
328 memcpy(&nd, &((struct cmdline_token_num *)tk)->num_data, sizeof(nd));
330 /* should not happen.... don't so this test */
331 /* if (nd.type >= (sizeof(num_help)/sizeof(const char *))) */
334 ret = strlcpy(dstbuf, num_help[nd.type], size);
337 dstbuf[size-1] = '\0';